	.TITLE	LOAD
	.IDENT	"V1.1"
;
;   Author:	D. Mischler	23-MAY-87
;
;   This module implements the LOAD command to load
;   a .STB file into the symbol table.
;
	.MCALL	CLOSE$,FDBDF$,FDOP$A,FDRC$A,GET$,NMBLK$,OPEN$
	.MCALL	CSI$,CSI$1,CSI$2
	CSI$

	.ASECT
;
;   Symbol file record format codes.
;
RT.GSD	=	1		; Global symbol directory record.
RT.EGD	=	2		; End of global symbol directory record.
;
;   Global symbol directory entry offsets.
;
	.=0
G.NAM:	.BLKW	2		; RAD50 symbol name.
G.FLGS:	.BLKB	1		; Entry flags.
G.TYP:	.BLKB	1		; Global symbol directory entry type.
G.VAL:	.BLKW	1		; Symbol value.
G.LEN	=	.
;
;   Global symbol directory entry codes.
;
GT.GSN	=	4		; Global symbol name entry.
GT.ISN	=	2		; Internal symbol name entry.

;
;   Flag bit definitions.
;
GF.WK	=	1		; Weak definition.
GF.LIB	=	4		; Library offset definition.
GF.DEF	=	10		; Symbol definition.
GF.REL	=	40		; Relative symbol.
	.PAGE
	.PSECT	DATA,D,RW
;
;   Local data definitions.
;
STBCSI:	.BLKB	C.SIZE		; CSI control block for symbol table file.
STBLEN	=	128.		; Maximum allowed symbol record length.
STBREC:	.BLKW	<STBLEN+1>/2	; Symbol table record buffer.

;
;   Symbol file FDB.
;
STBFDB:	FDBDF$
	FDRC$A	FD.PLC,STBREC,STBLEN			; Record access data.
	FDOP$A	STBLUN,STBCSI+C.DSDS,STBDFB,FO.RD	; File open data.

STBDFB:	NMBLK$	DBG,STB,0,SY,0	; Default filename block for symbol file.
	.PAGE
	.PSECT	CODE,I,RO
;
;   Parse the command line and set up the data-set descriptor.
;
LOAD::
	CALL	U$FNXT			; Point to file name, OK?
	BCS	20$			; No, complain.
	MOV	R0,-(SP)		; Save filename string base address.
	CALL	U$FTRM			; Find filename terminator.
	SUB	(SP),R0			; Get filename string length.
	MOV	R0,STBCSI+C.CMLD	; Save length in CSI block.
	MOV	#STBCSI,R0		; Point to CSI control block.
	MOV	(SP)+,C.CMLD+2(R0)	; Save filename string address too.
	CSI$1				; Perform syntax analysis, OK?
	BCS	10$			; No, complain.
	CSI$2	,OUTPUT			; Build data-set descriptor, OK?
	BCS	10$			; No, complain.
	OPEN$	#STBFDB			; Open the symbol table file, OK?
	BCS	10$			; No, complain.
	CALL	SYMLDR			; Load the specified symbol table file.
	CLOSE$	#STBFDB			; CLose symbol table file.
	RETURN
;   Symbol file open error encountered.
10$:	MOV	#E.SOPN,R1		; Point to error message.
	BR	30$
;   No file name specified.
20$:	MOV	#E.RAMS,R1		; Point to error message.
30$:	CALLR	ERROR
	.PAGE
;
;   The symbol loader reads records from the symbol table file
;   and defines any symbols found in the main symbol table.
;
SYMLDR:	GET$	#STBFDB		; Read a record, OK?
	BCS	40$		; No, assume end of file.
	MOV	F.NRBD(R0),R5	; Get length of record, zero?
	BEQ	SYMLDR		; Yes, try again.
	MOV	F.NRBD+2(R0),R4	; Get record address.
	ADD	R4,R5		; Produce end of record pointer.
	MOV	(R4)+,R0	; Get record type.
	CMPB	#RT.EGD,R0	; End of global symbol directory?
	BEQ	40$		; Yes, ignore the rest of the file.
	CMPB	#RT.GSD,R0	; Global symbol directory record?
	BNE	SYMLDR		; No, try next record.
;   Loop through GSD record looking for symbol entries.
10$:	CMP	R4,R5			; End of record yet?
	BEQ	SYMLDR			; Yes, get next record.
	CMPB	#GT.GSN,G.TYP(R4)	; Global symbol name?
	BNE	20$			; No, try next entry.
	BITB	#GF.DEF,G.FLGS(R4)	; Is it a definition?
	BEQ	20$			; No, try next entry.
	BITB	#GF.WK!GF.LIB!GF.REL,G.FLGS(R4) ; Is it just too silly?
	BNE	20$			; Yes, try next entry.
	MOV	#SYMTBL,R0		; Point to main symbol table header.
	MOV	(R4),R2			; Get symbol name.
	MOV	2(R4),R3
	MOV	G.VAL(R4),R1		; Get symbol value.
	CALL	S$DEFN			; Define the symbol, OK?
	BCS	30$			; No, complain.
20$:	ADD	#G.LEN,R4		; Point to next entry.
	BR	10$			; Take a look at it.
;   An allocation error has occurred.
30$:	MOV	#E.MFUL,R1		; Point to error message.
	CALL	ERROR			; Dump it on the unsuspecting user.
40$:	RETURN
	.END
