NEW
  AUTO 3,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
				
				>PUSHA
				>SYSCALL SListFree
				
SYM.Quit.8		clc
				rts	
*---------------------------------------
SYM.Dump.End	>PUSHW L.MSG.SUMMARY
				>PUSHW ZPPtr2
				>PUSHBI 2
				jmp OUT.Print
				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

				>PUSHB.G SYM.hList
				>PUSHW ZPPtr1
				>PUSHW ZPTmpBuf	
				>SYSCALL SListGetByID
				bcs SYM.Dump.End

				>STYA ZPPtr2			Save Next ID

				>PUSHB.G SYM.hList
				>PUSHW ZPPtr1			KeyID
				>PUSHW ZPSymbolBuf
				>PUSHWI $ffff			All
				>PUSHWZ					from Start
				>SYSCALL SListGetData
				bcs .9

				ldy #SYMG.SIZE			Get Symbol Size
				lda (ZPSymbolBuf),y
				tax
				beq .3

				phx

				asl
				tax
				>PUSHB L.MSG.EQU0+1,x
				>PUSHB L.MSG.EQU0,x

				plx
				phx
				
.2				iny
				lda (ZPSymbolBuf),y
				>PUSHA
				dex
				bne .2
				
				pla						Byte Count

.3				>PUSHA
				
				>SYSCALL printf

				>PUSHW ZPTmpBuf			Label
				>PUSHBI 0
				>SYSCALL printf

				lda (ZPSymbolBuf)

				bit #SYMG.F.LOCALS
				beq .7

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

.1				lda (ZPSymbolBuf),y
				beq .8
				
				phy
				
				tax						ID
				
				iny
				lda (ZPSymbolBuf),y		MacID
				beq .2
				
				>PUSHW L.MSG.LSYMBOL
				txa
				>PUSHA
				
				>PUSHB (ZPSymbolBuf),y
				bra .3
				
.2				>PUSHW L.MSG.LSYMBOL
				txa
				>PUSHA
				
				phx
				
.3				ldx #4
				
.4				iny
				lda (ZPSymbolBuf),y
				>PUSHA
				dex
				bne .4
				
				lda #5
				plx
				beq .5
				
				inc
				
.5				>PUSHA
				
				>SYSCALL printf

				pla
				clc
				adc #SYML
				tay

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

SYM.NewGlobal	jsr SYM.StoreGlobal
				bcs .99

				>PUSHB.G SYM.hList
				>PUSHW ZPLinePtr		Pass #1: try to add global...
				>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	>PUSHB.G SYM.hList
				>PUSHW ZPLinePtr		Pass #2: should be already defined...
				>SYSCALL SListLookup
				bcs .9
				>STYA.G SYM.GlobalID

				txa
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				inc ZPLinePtr+1

.1				>PUSHB.G SYM.hList
				>PUSHW.G SYM.GlobalID
				>PUSHW ZPSymbolBuf
				>PUSHWI $ffff			all
				>PUSHWZ					from Start
				>SYSCALL SListGetData
				bcs .99
				
				lda #$ff
				>STA.G SYM.bInGlobal
*				clc
				rts

.9				lda #E.UNDEF.SYMBOL
				sec
.99				rts
*---------------------------------------
SYM.LookupGlobal
				>PUSHB.G SYM.hList
				>PUSHW ZPLinePtr
				>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				>PUSHB.G SYM.hList
				>PUSHW ZPPtr1
				>PUSHW L.SRC.ACC.F
				>PUSHWI SYMG		Get only Global value
				>PUSHWZ				Start
				>SYSCALL SListGetData
				rts
				
.9				lda #E.UNDEF.SYMBOL
				sec
				rts
*---------------------------------------
SYM.StoreGlobal	>LDA.G ASM.PASS
				bne .8

				>LDA.G SYM.bInGlobal
				bpl .8
	
				>PUSHB.G SYM.hList
				>PUSHW.G SYM.GlobalID
				>PUSHW ZPSymbolBuf
				
				>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

				>SYSCALL SListAddData
				bcs .9

				lda #0
				>STA.G SYM.bInGlobal
				>STA.G MAC.CtxID

.8				clc
.9				rts
*---------------------------------------
SYM.UpdateGlobal
				>PUSHB.G SYM.hList
				>PUSHW.G SYM.GlobalID
				>PUSHW L.SRC.ACC.F
				>PUSHWI SYMG
				>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
*			>DEBUG
				>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
