From: Lukas Krickl Date: Wed, 19 Mar 2025 21:28:38 +0000 (+0100) Subject: scope: Added .beginscope and .endscope X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=e8a06ffe71dd21451684f842170c5fd00c1ba3bf;p=ulas%2F.git scope: Added .beginscope and .endscope --- diff --git a/man/ulas.5 b/man/ulas.5 index f2cc927..f37da73 100644 --- a/man/ulas.5 +++ b/man/ulas.5 @@ -153,6 +153,14 @@ The special value $ will evaluate to the current address. Sets the current bank. This is only used for symbol files. +.SH beginscope + \.beginscope + Starts a new scope + +.SH endscope + \.endscope + Returns to the previous scope + .SH SEE ALSO ulas(1) for a rference on how to use ulas. diff --git a/src/ulas.c b/src/ulas.c index 79d89ef..36b077c 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -394,8 +394,8 @@ int ulas_symbolout_mlbloc(FILE *dst, long addr) { } // fallback - // if no previous section was declared we fall back to the - // old default output + // if no previous section was declared we fall back to the + // old default output switch (ulas.arch.type) { case ULAS_ARCH_SM83: if (addr >= 0x0000 && addr <= 0x7FFF) { @@ -442,7 +442,7 @@ int ulas_symbolout(FILE *dst, struct ulas_sym *s) { break; case ULAS_SYM_FMT_SYM: fprintf(dst, "%02d:", ulas.bank); - + switch (s->tok.type) { case ULAS_INT: fprintf(dst, "%x", ulas_valint(&s->tok, &rc)); @@ -2487,8 +2487,8 @@ int ulas_asmdirsection(FILE *dst, FILE *src, const char **line, return 0; } -int ulas_asmdirbank(FILE *dst, FILE *src, const char **line, - unsigned long n, int *rc) { +int ulas_asmdirbank(FILE *dst, FILE *src, const char **line, unsigned long n, + int *rc) { ULAS_EVALEXPRS(ulas.bank = ulas_intexpr(line, strnlen(*line, n), rc)); return *rc; } @@ -2529,24 +2529,16 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { } if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) { - const char *dirstrs[] = {ULAS_ASMSTR_ORG, - ULAS_ASMSTR_SET, - ULAS_ASMSTR_BYTE, - ULAS_ASMSTR_STR, - ULAS_ASMSTR_FILL, - ULAS_ASMSTR_PAD, - ULAS_ASMSTR_INCBIN, - ULAS_ASMSTR_DEF, - ULAS_ASMSTR_CHKSM, - ULAS_ASMSTR_ADV, - ULAS_ASMSTR_SET_ENUM_DEF, - ULAS_ASMSTR_DEFINE_ENUM, - ULAS_ASMSTR_SETCHRCODE, - ULAS_ASMSTR_CHR, - ULAS_ASMSTR_REP, - ULAS_ASMSTR_SECTION, - ULAS_ASMSTR_BANK, - NULL}; + const char *dirstrs[] = {ULAS_ASMSTR_ORG, ULAS_ASMSTR_SET, + ULAS_ASMSTR_BYTE, ULAS_ASMSTR_STR, + ULAS_ASMSTR_FILL, ULAS_ASMSTR_PAD, + ULAS_ASMSTR_INCBIN, ULAS_ASMSTR_DEF, + ULAS_ASMSTR_CHKSM, ULAS_ASMSTR_ADV, + ULAS_ASMSTR_SET_ENUM_DEF, ULAS_ASMSTR_DEFINE_ENUM, + ULAS_ASMSTR_SETCHRCODE, ULAS_ASMSTR_CHR, + ULAS_ASMSTR_REP, ULAS_ASMSTR_SECTION, + ULAS_ASMSTR_BANK, ULAS_ASMSTR_BEGIN_SCOPE, + ULAS_ASMSTR_END_SCOPE, NULL}; enum ulas_asmdir dirs[] = { ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, ULAS_ASMDIR_STR, @@ -2555,7 +2547,9 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { ULAS_ASMDIR_CHKSM, ULAS_ASMDIR_ADV, ULAS_ASMDIR_SET_ENUM_DEF, ULAS_ASMDIR_DEFINE_ENUM, ULAS_ASMDIR_SETCHRCODE, ULAS_ASMDIR_CHR, - ULAS_ASMDIR_REP, ULAS_ASMDIR_SECTION, ULAS_ASMDIR_BANK}; + ULAS_ASMDIR_REP, ULAS_ASMDIR_SECTION, + ULAS_ASMDIR_BANK, ULAS_ASMDIR_BEGIN_SCOPE, + ULAS_ASMDIR_END_SCOPE}; enum ulas_asmdir dir = ULAS_ASMDIR_NONE; @@ -2624,7 +2618,13 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { break; case ULAS_ASMDIR_BANK: rc = ulas_asmdirbank(dst, src, &line, n, &rc); - break; + break; + case ULAS_ASMDIR_BEGIN_SCOPE: + ulas.scope++; + break; + case ULAS_ASMDIR_END_SCOPE: + ulas.scope--; + break; case ULAS_ASMDIR_PAD: // TODO: pad is the same as .fill n, $ - n case ULAS_ASMDIR_NONE: diff --git a/src/ulas.h b/src/ulas.h index 1260b11..5d301c4 100644 --- a/src/ulas.h +++ b/src/ulas.h @@ -79,6 +79,8 @@ extern unsigned long defslen; #define ULAS_ASMSTR_REP ".rep" #define ULAS_ASMSTR_SECTION ".section" #define ULAS_ASMSTR_BANK ".bank" +#define ULAS_ASMSTR_BEGIN_SCOPE ".beginscope" +#define ULAS_ASMSTR_END_SCOPE ".endscope" // configurable tokens #define ULAS_TOK_COMMENT ';' @@ -473,7 +475,15 @@ enum ulas_asmdir { // .bank // sets the current bank number - ULAS_ASMDIR_BANK + ULAS_ASMDIR_BANK, + + // .beginscope + // incements scope index by 1 + ULAS_ASMDIR_BEGIN_SCOPE, + + // .endscope + // decrements scope index by 1 + ULAS_ASMDIR_END_SCOPE, }; #define ULAS_INSTRTOKMAX 16 diff --git a/tests/t0.bin b/tests/t0.bin index aa12497..28305dd 100644 Binary files a/tests/t0.bin and b/tests/t0.bin differ diff --git a/tests/t0.s b/tests/t0.s index 77e90b1..41c83d8 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -182,3 +182,23 @@ toplevel 5, 6 #endmacro defarg DEFARG, 2 + +test_scoped: + nop + jp @redef + jp @postdef +@redef: +.beginscope + jp @redef + jp @postdef +@postdef: +@redef: + nop + nop +.endscope + jp @redef + jp @postdef +@postdef: +@behin_scope: + nop + diff --git a/tests/t0_dasm.s b/tests/t0_dasm.s index bddc3ef..3a4aa2b 100644 --- a/tests/t0_dasm.s +++ b/tests/t0_dasm.s @@ -123,4 +123,12 @@ halt ld [bc], a ld [bc], a -.db 0x1 + ld bc, 0xc300 +.db 0xe4 + ld bc, 0xf2c3 + ld bc, 0xeac3 + ld bc, 0xeac3 + ld bc, 0x0 + jp 0x1e4 + jp 0x1f2 +.db 0x0