From f3e896857187f52962d91e9a104c601e6fa93da4 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 4 Dec 2023 15:22:51 +0100 Subject: [PATCH] Added .def and .set --- include/ulas.h | 5 +++- src/ulas.c | 71 +++++++++++++++++++++++++++++++++++-------------- tests/t0.bin | Bin 96 -> 98 bytes tests/t0.s | 4 ++- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index c078a04..7364b39 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -41,6 +41,7 @@ #define ULAS_ASMSTR_FILL ".fill" #define ULAS_ASMSTR_PAD ".pad" #define ULAS_ASMSTR_INCBIN ".incbin" +#define ULAS_ASMSTR_DEF ".def" // configurable tokens #define ULAS_TOK_COMMENT ';' @@ -327,7 +328,7 @@ enum ulas_asmdir { ULAS_ASMDIR_NONE = 0, // .org
ULAS_ASMDIR_ORG, - // .set name = + // .set name = ULAS_ASMDIR_SET, // .byte , , , ... ULAS_ASMDIR_BYTE, @@ -339,6 +340,8 @@ enum ulas_asmdir { ULAS_ASMDIR_PAD, // .incbin ULAS_ASMDIR_INCBIN, + // .def name = value + ULAS_ASMDIR_DEF, }; // amount of registers diff --git a/src/ulas.c b/src/ulas.c index 9f2aaa2..5eef3c2 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -2039,21 +2039,8 @@ int ulas_asmdirbyte(FILE *dst, const char *line, unsigned long n) { return 0; } -int ulas_asmdirset(const char **line, unsigned long n) { - // .set = name expr +int ulas_asmdirset(const char **line, unsigned long n, enum ulas_type t) { char name[ULAS_SYMNAMEMAX]; - - ulas_tok(&ulas.tok, line, n); - enum ulas_type t = ULAS_INT; - if (strncmp(ulas.tok.buf, "int", ulas.tok.maxlen) == 0) { - t = ULAS_INT; - } else if (strncmp(ulas.tok.buf, "char", ulas.tok.maxlen) == 0) { - t = ULAS_STR; - } else { - ULASERR("Type (str,int) expected. Got '%s'\n", ulas.tok.buf); - return -1; - } - ulas_tok(&ulas.tok, line, n); if (!ulas_isname(ulas.tok.buf, ulas.tok.maxlen)) { ULASERR("Unexpected token '%s'\n", ulas.tok.buf); @@ -2094,6 +2081,46 @@ fail: return rc; } +int ulas_asmdirset_lookup(const char **line, unsigned long n) { + if (ulas.pass != ULAS_PASS_FINAL) { + *line += strlen(*line); + return 0; + } + const char *start = *line; + ulas_tok(&ulas.tok, line, n); + + int rc = 0; + struct ulas_sym *found = ulas_symbolresolve(ulas.tok.buf, -1, &rc); + + if (rc == -1 || !found) { + ULASERR("Unable to set symbol '%s'\n", ulas.tok.buf); + return -1; + } + + enum ulas_type t = found->tok.type; + + *line = start; + + return ulas_asmdirset(line, n, t); +} + +int ulas_asmdirdef(const char **line, unsigned long n) { + // .set = name expr + + ulas_tok(&ulas.tok, line, n); + enum ulas_type t = ULAS_INT; + if (strncmp(ulas.tok.buf, "int", ulas.tok.maxlen) == 0) { + t = ULAS_INT; + } else if (strncmp(ulas.tok.buf, "char", ulas.tok.maxlen) == 0) { + t = ULAS_STR; + } else { + ULASERR("Type (str,int) expected. Got '%s'\n", ulas.tok.buf); + return -1; + } + + return ulas_asmdirset(line, n, t); +} + int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { // this buffer is written both to dst and to verbose output char outbuf[ULAS_OUTBUFMAX]; @@ -2114,11 +2141,12 @@ 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, NULL}; + ULAS_ASMSTR_ORG, ULAS_ASMSTR_SET, ULAS_ASMSTR_BYTE, + ULAS_ASMSTR_STR, ULAS_ASMSTR_FILL, ULAS_ASMSTR_PAD, + ULAS_ASMSTR_INCBIN, ULAS_ASMSTR_DEF, NULL}; enum ulas_asmdir dirs[] = { - ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, ULAS_ASMDIR_STR, - ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD, ULAS_ASMDIR_INCBIN}; + ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, ULAS_ASMDIR_STR, + ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD, ULAS_ASMDIR_INCBIN, ULAS_ASMDIR_DEF}; enum ulas_asmdir dir = ULAS_ASMDIR_NONE; @@ -2140,9 +2168,12 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { case ULAS_ASMDIR_ORG: ulas.address = ulas_intexpr(&line, strnlen(start, n), &rc); break; - case ULAS_ASMDIR_SET: + case ULAS_ASMDIR_DEF: // only do this in the final pass - rc = ulas_asmdirset(&line, n); + rc = ulas_asmdirdef(&line, n); + break; + case ULAS_ASMDIR_SET: + rc = ulas_asmdirset_lookup(&line, n); break; case ULAS_ASMDIR_BYTE: case ULAS_ASMDIR_STR: diff --git a/tests/t0.bin b/tests/t0.bin index 739357a260773e6b2129410b5613072d852fe917..29abba46eeac06c8b0823cd607569cb75d53ae96 100644 GIT binary patch delta 7 OcmYdDnvlR`#{vKe$pQBO delta 4 LcmYdFn2-Pf1S|ow diff --git a/tests/t0.s b/tests/t0.s index 5de1ad5..b64a432 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -81,5 +81,7 @@ l2: ld bc, $ jr z, $ - l2 -.set int s1 = 1 + 2 +.def int s1 = 1 + 2 + ld a, s1 +.set s1 = 4 ld a, s1 -- 2.30.2