In ASM6 README.txt, loopy wrote:
Labels defined inside macros are local (visible only to that macro).
Several of my ca65 projects have relied on the ability of macros to define global labels in order to allow a macro to assign a name to which other data tables can refer. For example:
Code:
; ca65 code
.macro entry nameofthing, part1, part2, part3
nameofthing = (* - tablestart) / 4
.word part1
.byte part2, part3
.endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this:
tablestart:
THING_ONE = (* - tablestart) / 4 ; that is, 0
.word $1111
.byte $01,$01
THING_TWO = (* - tablestart) / 4 ; that is, 1
.word $2222
.byte $02,$02
THING_THREE = (* - tablestart) / 4 ; that is, 2
.word $3333
.byte $03,$03
.macro entry nameofthing, part1, part2, part3
nameofthing = (* - tablestart) / 4
.word part1
.byte part2, part3
.endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this:
tablestart:
THING_ONE = (* - tablestart) / 4 ; that is, 0
.word $1111
.byte $01,$01
THING_TWO = (* - tablestart) / 4 ; that is, 1
.word $2222
.byte $02,$02
THING_THREE = (* - tablestart) / 4 ; that is, 2
.word $3333
.byte $03,$03
But now I'm being asked to port one of my libraries to ASM6, where each macro definition behaves as if its body were wrapped in a ca65 .scope.
Code:
; ASM6 code
macro entry nameofthing, part1, part2, part3
nameofthing = ($ - tablestart) / 4
word part1
byte part2, part3
endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this, where the rept 1 blocks make labels defined within
; invisible to the rest of the program
tablestart:
rept 1
THING_ONE = ($ - tablestart) / 4 ; that is, 0
word $1111
byte $01,$01
endr ; symbol THING_ONE unusable outside
rept 1
THING_TWO = ($ - tablestart) / 4 ; that is, 1
word $2222
byte $02,$02
endr ; symbol THING_TWO unusable outside
rept 1
THING_THREE = ($ - tablestart) / 4 ; that is, 2
word $3333
byte $03,$03
endr ; symbol THING_THREE unusable outside
macro entry nameofthing, part1, part2, part3
nameofthing = ($ - tablestart) / 4
word part1
byte part2, part3
endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this, where the rept 1 blocks make labels defined within
; invisible to the rest of the program
tablestart:
rept 1
THING_ONE = ($ - tablestart) / 4 ; that is, 0
word $1111
byte $01,$01
endr ; symbol THING_ONE unusable outside
rept 1
THING_TWO = ($ - tablestart) / 4 ; that is, 1
word $2222
byte $02,$02
endr ; symbol THING_TWO unusable outside
rept 1
THING_THREE = ($ - tablestart) / 4 ; that is, 2
word $3333
byte $03,$03
endr ; symbol THING_THREE unusable outside
What's the corresponding idiom for generating symbols for indices into a data table in ASM6? In theory, I could require users of the library to include boilerplate before each invocation of the macro to move the symbol definition out of the macro, but I imagine that would increase the opportunity for mistakes when a particular invocation fails to get copied.
Code:
; ASM6 code
macro entry nameofthing, part1, part2, part3
word part1
byte part2, part3
endmacro
tablestart:
THING_ONE = ($ - tablestart) / 4
hello THING_ONE, $1111,$01,$01
THING_TWO = ($ - tablestart) / 4
hello THING_TWO, $2222,$02,$02
THING_THREE = ($ - tablestart) / 4
hello THING_THREE, $3333,$03,$03
macro entry nameofthing, part1, part2, part3
word part1
byte part2, part3
endmacro
tablestart:
THING_ONE = ($ - tablestart) / 4
hello THING_ONE, $1111,$01,$01
THING_TWO = ($ - tablestart) / 4
hello THING_TWO, $2222,$02,$02
THING_THREE = ($ - tablestart) / 4
hello THING_THREE, $3333,$03,$03
Or do I need to implement my own preprocessor for this sort of thing?