NEW
  PREFIX
AUTO 4,1
*---------------------------------------
* Global Symbol Record: (6+ bytes)
*  0 : Len
*  1 : Flags:
*		b7=0=pending,1=resolved
*		b6=R/W (.SE)
*		b2,b1,b0 : Size (in bytes)
*  2-5 : 32 bits Value
*  6+  : Local Symbols.....
*---------------------------------------
* Local Symbol Record: (5 bytes)
*  0 : Local ID (1->255)
*  1-4 : 32 bits Value (PC)
*---------------------------------------
SYM.Init		>SYSCALL SListNew
				bcs .9

				>STA.G SYM.hList
				
				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPSymbolBuf
				txa
				>STA.G SYM.hGlobalBuf
				
.9				rts
*---------------------------------------
SYM.Reset		>STZ.G SYM.bInGlobal
				rts
*---------------------------------------
SYM.Quit		>LDA.G SYM.hGlobalBuf
				beq .3
				
				>SYSCALL FreeMem
				
.3				>LDA.G SYM.hList
				beq SYM.Quit.8
				>SYSCALL SListFree
				
SYM.Quit.8		clc
				rts	
*---------------------------------------
SYM.Dump.End	>PUSHW ZPPtr2
				>PUSHBI 2
				>LDYA L.MSG.SUMMARY
				jmp OUT.Print
SYM.Dump.RTS	rts				
*---------------------------------------
SYM.Dump		>LDA.G ASM.LI.ON
				bpl SYM.Quit.8
				
				>LDYA L.MSG.SYMBOLS
				>SYSCALL puts

				stz ZPPtr2
				stz ZPPtr2+1

.1				>LDYA ZPPtr2
				>STYA ZPPtr1

				>PUSHW ZPTmpBuf	
				>PUSHW ZPPtr1
				>LDA.G SYM.hList
				>SYSCALL SListGetByID
				bcs SYM.Dump.End
				
				>STYA ZPPtr2			Save Next ID
				
				>PUSHWZ				Start
				>PUSHWI $ffff			End
				>PUSHW ZPSymbolBuf
				>PUSHW ZPPtr1			KeyID
				>LDA.G SYM.hList

				>SYSCALL SListGetData
				bcs SYM.Dump.RTS
				
				ldy #SYMG.SIZE			Get Symbol Size
				lda (ZPSymbolBuf),y
				
				tax
				beq .3

				phx
				
.2				iny
				lda (ZPSymbolBuf),y
				>PUSHA
				dex
				bne .2
				
				pla						Byte Count
				asl
				tax
				
.3				>PUSHA
				
				ldy L.MSG.EQU0,x
				lda L.MSG.EQU0+1,x
				>SYSCALL printf

				>PUSHBI 0
				>LDYA ZPTmpBuf			Label
				>SYSCALL printf

				lda (ZPSymbolBuf)

				bit #SYMG.F.LOCALS
				beq .7

				jsr SYM.Dump.LP
				
.7				jsr OUT.PrintCR
				jmp .1				
*---------------------------------------
SYM.Dump.LP		ldy #SYMG

.4				lda (ZPSymbolBuf),y
				beq .8
				
				phy

				iny
				
				ldx #4
				
.5				iny
				lda (ZPSymbolBuf),y
				>PUSHA
				dex
				bne .5
				
				ply
				iny
				lda (ZPSymbolBuf),y
				tax
				beq .55
				>PUSHA
				
.55				dey
				lda (ZPSymbolBuf),y
				>PUSHA
				phy
				
				txa
				beq .6
				>PUSHBI 6
				>LDYA L.MSG.PSYMBOL
				bra .66
				
.6				>PUSHBI 5
				>LDYA L.MSG.LSYMBOL
.66				>SYSCALL printf
				pla
				clc
				adc #SYML
				tay

				bra .4	
				
.8				rts
*---------------------------------------
SYM.NewOrGetGlobal
				>LDA.G ASM.PASS
				bne SYM.GetGlobal

SYM.NewGlobal	jsr SYM.StoreGlobal
				bcs .99

				>PUSHW ZPLinePtr		Pass #1; try to add global...
				>LDA.G SYM.hList
				>SYSCALL SListNewKey
				bcs .9					Already Defined
				>STYA.G SYM.GlobalID
				txa
				
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				
				inc ZPLinePtr+1
				
.1				jsr SYM.PC2Acc
				
				jsr SYM.Acc2Global
				
				lda #SYMG
				
				>STA.G SYM.GlobalPtr
				tay
				
				lda #0
				sta (ZPSymbolBuf),y
				
				lda #$ff
				>STA.G SYM.bInGlobal
				
*				>PUSHW.G SYM.GlobalID
*				>PUSHBI 2
*				>LDYA L.MSG.WORD
*				>SYSCALL printf
				clc
				rts
				
.9				lda #E.SYMBOL.REDEF
				sec
.99				rts					
*---------------------------------------
SYM.GetGlobal	>PUSHW ZPLinePtr		Pass #2: should be already defined...
				>LDA.G SYM.hList
				>SYSCALL SListLookup
				bcs .9
				>STYA.G SYM.GlobalID

				txa
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				
				inc ZPLinePtr+1
				
.1				>PUSHWZ				Start
				>PUSHWI $ffff			End
				>PUSHW ZPSymbolBuf
				>PUSHW.G SYM.GlobalID
				>LDA.G SYM.hList
				>SYSCALL SListGetData
				bcs .99
				
				lda #$ff
				>STA.G SYM.bInGlobal
*				clc				
				rts
	
.9				lda #E.UNDEF.SYMBOL
				sec
.99				rts
*---------------------------------------
SYM.LookupGlobal
				>PUSHW ZPLinePtr
				>LDA.G SYM.hList
				>SYSCALL SListLookup
				bcs .9
				
				>STYA ZPPtr1
				
				txa
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				
				inc ZPLinePtr+1
				
.1				>LDA.G SYM.GlobalID
				cmp ZPPtr1
				bne .3
				iny						SYM.GlobalID+1
				lda (pData),y
				cmp ZPPtr1+1
				bne .3
				
				ldy #SYMG-1
				
.2				lda (ZPSymbolBuf),y
				sta SRC.ACC.F,y
				dey
				bpl .2

				clc
				rts

.3				>PUSHWZ				Start
				>PUSHWI SYMG			Get only Global value
				>PUSHW L.SRC.ACC.F
				>PUSHW ZPPtr1
				>LDA.G SYM.hList
				>SYSCALL SListGetData
				rts
				
.9				lda #E.UNDEF.SYMBOL
				sec
				rts
*---------------------------------------
SYM.StoreGlobal	>LDA.G ASM.PASS
				bne .8

				>LDA.G SYM.bInGlobal
				bpl .8
	
				>LDA.G SYM.GlobalPtr
				tay
				lda #0
				cpy #SYMG
				beq .1
				
				lda (ZPSymbolBuf)
				ora #SYMG.F.LOCALS
				sta (ZPSymbolBuf)
				
				lda #0
				sta (ZPSymbolBuf),y
				iny
				bne .1
				inc
				
.1				>PUSHYA

				>PUSHW ZPSymbolBuf
				>PUSHW.G SYM.GlobalID
				>LDA.G SYM.hList
				>SYSCALL SListAddData
				bcs .9
				
				lda #0
				>STA.G SYM.bInGlobal
				>STA.G MAC.CtxID
				
				rts
				
.8				clc
.9				rts
*---------------------------------------
SYM.UpdateGlobal
				>PUSHWI SYMG
				>PUSHW L.SRC.ACC.F
				>PUSHW.G SYM.GlobalID
				>LDA.G SYM.hList
				>SYSCALL SListSetData
				rts
*---------------------------------------
SYM.PC2Acc		ldy #ASM.PC+3
				ldx #3
				
.1				lda (pData),y
				sta SRC.ACC,x
				dey
				dex
				bpl .1

				lda #2
				sta SRC.ACC.SIZE
				stz SRC.ACC.F
				
				rts
*---------------------------------------
SYM.Acc2Global	ldy #SYMG-1
				
.2				lda SRC.ACC.F,y				
				sta (ZPSymbolBuf),y
				dey
				bpl .2

				rts
*---------------------------------------
* Local/Private Symbol Record: (6 bytes)
*  0 : Local ID (1->255)
*  1 : Macro Context ID
*  2-5 : 32 bits Value (PC)
*---------------------------------------
SYM.NewPrivateA sec
				.HS 90					BCC

SYM.NewLocalA	clc
				
				ror ZPPtr1
				tax						Save Local ID
			
				>LDA.G SYM.bInGlobal	not in a global label
				bpl .91
				
				>LDA.G ASM.PASS			Pass #2: should be already defined...
				bne .8

				jsr SYM.GetLocalX		Pass #1; try to add
				bcc .9					Already Defined

				>LDA.G SYM.GlobalPtr
				cmp #252				6*42
				beq .90
				
				ldy #ASM.PC+4
				
.1				dey
				lda (pData),y
				pha
				cpy #ASM.PC
				bne .1					

				lda #0
				bit ZPPtr1
				bpl .11					local, Ctx = 0
				
				>LDA.G MAC.CtxID		Get Macro Context ID
.11				pha
				
				>LDA.G SYM.GlobalPtr
				tay
				
				txa						Get Back ID
				sta (ZPSymbolBuf),y

				ldx #5					MAC ID + PC (4)
				
.2				pla
				iny
				sta (ZPSymbolBuf),y
				dex
				bne .2

				iny
				lda #0
				sta (ZPSymbolBuf),y
				tya
				>STA.G SYM.GlobalPtr
				
				lda (ZPSymbolBuf)
				ora #SYMG.F.LOCALS
				sta (ZPSymbolBuf)
				
.8				clc
				rts

.91				lda #E.INV.LABEL				
				sec
				rts
					
.90				lda #E.TOO.MANY.LOCAL
				sec
				rts

.9				lda #E.SYMBOL.REDEF
				sec
				rts		
*---------------------------------------
SYM.GetLocalX	lda (ZPSymbolBuf)
				and #SYMG.F.LOCALS
				beq .9
				
				ldy #SYMG

.1				lda (ZPSymbolBuf),y
				beq .9
				
				txa
				cmp (ZPSymbolBuf),y
				bne .4

				phy
				>LDA.G MAC.CtxID
				ply
				iny
				cmp (ZPSymbolBuf),y
				bne .3

				ldx #0
				
.2				iny
				lda (ZPSymbolBuf),y
				sta SRC.ACC,x
				inx
				cpx #4
				bne .2

				lda #2
				sta SRC.ACC.SIZE
				
				clc
				rts
	
.3				dey	
.4				tya
				clc
				adc #SYML
				tay
				bra .1
				
.9				lda #E.UNDEF.SYMBOL
				sec
				rts	
*---------------------------------------
MAN
SAVE USR/SRC/BIN/ASM.S.SYM
LOAD USR/SRC/BIN/ASM.S
ASM
