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.hGlobalList
				
				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPNameBuf
				txa
				>STA.G SYM.hNameBuf

				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPGlobalBuf
				txa
				>STA.G SYM.hGlobalBuf
				
				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPLookupBuf
				txa
				>STA.G SYM.hLookupBuf
				
.9				rts
*---------------------------------------
SYM.Reset		lda #0
				sta (ZPGlobalBuf)
				rts
*---------------------------------------
SYM.Quit		>LDA.G SYM.hLookupBuf
				beq .1
				
				>SYSCALL FreeMem
				
.1				>LDA.G SYM.hNameBuf
				beq .2
				
				>SYSCALL FreeMem
				
.2				>LDA.G SYM.hGlobalBuf
				beq .3
				
				>SYSCALL FreeMem
.3				>LDA.G SYM.hGlobalList
				beq .8
				>SYSCALL SListFree
				
.8				clc
				rts				
*---------------------------------------
SYM.Dump		stz ZPLinePtr
				stz ZPLinePtr+1

				>LDA.G ASM.LI.ON
				bpl .1
				
				>LDYA L.MSG.SYMBOLS
				>SYSCALL puts

.1				>PUSHB.G SYM.hGlobalList
				>PUSHW ZPLinePtr
				>PUSHW ZPGlobalBuf	
				>PUSHW ZPNameBuf	
				>SYSCALL SListGetByID
				bcc .10
				
				clc
				rts
				
.10				stx ZPLinePtr			Save Next ID
				sty ZPLinePtr+1
				
				>LDA.G ASM.LI.ON
				bpl .1
				
				ldy #SYMG.F				Get Symbol Size
				lda (ZPGlobalBuf),y
				and #SYMG.F.SIZE
				
				>PUSHA					Byte Count
				tax
				beq .3

				asl
				pha

.2				iny
				lda (ZPGlobalBuf),y
				>PUSHA
				dex
				bne .2
				
				plx

.3				ldy L.MSG.EQU0,x
				lda L.MSG.EQU0+1,x
				>SYSCALL printf

				>PUSHW ZPNameBuf		Label (PString)
				>PUSHBI 2
				>LDYA L.MSG.PSTR
				>SYSCALL printf
				bcs .9
				ldy #SYMG.SIZE

.4				tya
				
				cmp (ZPGlobalBuf)
				beq .7					No Local Symbol
				
				iny
				lda (ZPGlobalBuf),y		Get ID
				pha
				
				ldx #4
				
.5				iny				
				lda (ZPGlobalBuf),y
				>PUSHA
				dex
				bne .5
				
				pla
				>PUSHA
				phy
				>PUSHBI 6
				>LDYA L.MSG.LSYMBOL
				>SYSCALL printf
				ply
				bne .4
				
.7				>PUSHBI 0
				>LDYA L.MSG.CRLF
				>SYSCALL printf
				jmp .1
				rts
				
.8				clc
.9				rts
*---------------------------------------
SYM.NewOrGetGlobal
				>LDA.G ASM.PASS
				bne SYM.GetGlobal		Pass #2: should be already defined...

SYM.NewGlobal	jsr SYM.LookupGlobal	Pass #1; try to add global...
				bcc .9					Already Defined
				
				jsr SYM.GetName
				bcs .99
				
				jsr SYM.PC2Global		A=PC Size

				lda #SYMG.SIZE
				sta (ZPGlobalBuf)
				clc
				rts
				
.9				lda #ERR.SYMBOL.REDEF
				sec
.99				rts					
*---------------------------------------
SYM.GetGlobal	>PUSHB.G SYM.hGlobalList
				>PUSHW ZPLinePtr
				>PUSHW ZPGlobalBuf
				>SYSCALL SListLookup

				bcs .9
				
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				
				inc ZPLinePtr+1
				clc		
				
.1				tya
				>STA.G SYM.GlobalID+1
				txa
				>STA.G SYM.GlobalID
				rts
	
.9				lda #ERR.UNDEF.SYMBOL
				sec
				rts
*---------------------------------------
SYM.LookupGlobal
				>PUSHB.G SYM.hGlobalList
				>PUSHW ZPLinePtr
				>PUSHW ZPLookupBuf
				>SYSCALL SListLookup
				bcs .9
				
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .8
				
				inc ZPLinePtr+1
				clc			
.8	
.9				rts
*---------------------------------------
SYM.FlushGlobal	lda (ZPGlobalBuf)
				beq .8
				
				>PUSHB.G SYM.hGlobalList
				
				>LDA.G ASM.PASS
				bne .2

				>PUSHW ZPNameBuf
				>PUSHW ZPGlobalBuf
				>SYSCALL SListAdd
				bra .3
				
.2				>PUSHW.G SYM.GlobalID
				>PUSHW ZPGlobalBuf
				>SYSCALL SListUpdateByID
				
.3				bcs .9
				lda #0
				sta (ZPGlobalBuf)
				rts
				
.8				clc
.9				rts
*---------------------------------------
SYM.GetName		jsr SRC.GetChar
				bcs .9
				
				ldy #0

.1				jsr SRC.IsLetterOrDigit
				bcc .2
				cmp #'.'
				beq .2
				cmp #'_'
				bne .8
				
.2				sta (ZPNameBuf),y
				iny
				jsr SRC.GetNextChar
				bcc .1
				
.8				lda #0
				sta (ZPNameBuf),y
				clc
				rts
	
.9				lda #ERR.SYNTAX.ERROR
				sec	
				rts				
*---------------------------------------
SYM.PC2Global	ldx #4
				
				ldy #ASM.PC
				
.1				lda (pData),y
				pha
				iny
				dex
				bne .1
				
				ldx #4
				
				ldy #SYMG.V+3
				
.2				pla
				sta (ZPGlobalBuf),y
				dey
				dex
				bne .2
				
				ldx #4
				ldy #ASM.PC+3
				
.3				lda	(ZPGlobalBuf),y
				bne .4
				dex
				dey
				bne .3

.4				txa

				ldy #SYMG.F
				sta (ZPGlobalBuf),y

				txa						A=PC Size

				rts
*---------------------------------------
SYM.Acc2Global	ldx #4
				ldy #SYMG.F+4
				
.1				lda SRC.ACC.SIZE,x
				sta (ZPGlobalBuf),y
				dey
				dex
				bpl .1
				
				clc
				rts
*---------------------------------------
SYM.Lookup2Acc	ldy #SYMG.F+4
				ldx #4
				
.1				lda (ZPLookupBuf),y
				sta SRC.ACC.SIZE,x
				dey
				dex
				bne .1
				
				lda (ZPLookupBuf),y
				and #SYMG.F.SIZE
				sta SRC.ACC.SIZE

				clc
				rts
*---------------------------------------
* Local Symbol Record: (5 bytes)
*  0 : Local ID (1->255)
*  1-4 : 32 bits Value (PC)
*---------------------------------------
SYM.NewOrGetLocalA
				tax						Save ID
				
				lda (ZPGlobalBuf)		not in a global label
				bne .1
				
				lda #ERR.INV.LABEL				
				clc
				rts
	
.1				>LDA.G ASM.PASS
				bne SYM.LookupLocal.1	Pass #2: should be already defined...

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

				lda (ZPGlobalBuf)
				tay
				clc
				adc #SYML.SIZE
				bcs .90
				
				ldy #ASM.PC+4
				
.1				dey
				lda (pData),y
				pha
				cpy #ASM.PC
				bne .1					

				lda (ZPGlobalBuf)
				tay
				iny
				
				txa						Get Back ID
				
				sta (ZPGlobalBuf),y
				
				ldx #4
				
.2				pla
				iny
				sta (ZPGlobalBuf),y
				dex
				bne .2

				tya
				sta (ZPGlobalBuf)
				
				clc
				rts
				
.90				lda #ERR.TOO.MANY.LOCAL
*				sec
				rts

.9				lda #ERR.SYMBOL.REDEF
				sec
				rts		
*---------------------------------------
SYM.LookupLocal	tax
SYM.LookupLocal.1
				ldy #SYMG.SIZE
				
				tya
				
.1				cmp (ZPGlobalBuf)
				beq .9					CS if =
	
				txa
				iny
				cmp (ZPGlobalBuf),y
				beq .2
	
				tya
				clc
				adc #SYML.SIZE-1
				tay
				bcc .1
				
.9				lda #ERR.UNDEF.SYMBOL
*				sec
				rts
	
.2				ldx #0

.3				iny

				lda (ZPGlobalBuf),y
				sta SRC.ACC,x
				inx
				cpx #4
				bne .3

				clc
				rts
*---------------------------------------
* Private Symbol Record: (9 bytes)
*  0 : Len (8)
*  1 : Flags:
*		b7=0=pending,1=resolved
*  2-5 : 32 bits Value (PC)
*  6,7 : MCID (Macro Context ID)
*  8 : Private ID (0->255)
*---------------------------------------
SYM.AddPrivate		
				clc
				rts
*---------------------------------------
SYM.LookupPrivate
				clc
				rts
*---------------------------------------
MAN
SAVE USR/SRC/BIN/ASM.S.SYM
LOAD USR/SRC/BIN/ASM.S
ASM
