From: Lukas Krickl Date: Fri, 4 Oct 2024 17:27:26 +0000 (+0200) Subject: Added -D flag to allow defining preproc symbols using the command line X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=4ff1e90464612d279e0a284754a92c01cb8f47bf;p=ulas%2F.git Added -D flag to allow defining preproc symbols using the command line --- diff --git a/src/main.c b/src/main.c index 8156550..2d31a31 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,10 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) { case 'A': cfg->print_addrs = 1; break; + case 'D': + assert(defslen < ULAS_DEFSMAX); + defs[defslen++] = strndup(optarg, ULAS_PATHMAX); + break; case 'S': if (strcmp("ulas", optarg) == 0) { cfg->sym_fmt = ULAS_SYM_FMT_DEFAULT; @@ -91,6 +95,8 @@ int main(int argc, char **argv) { ulas_getopt(argc, argv, &cfg); cfg.incpaths = incpaths; cfg.incpathslen = incpathslen; + cfg.defs = defs; + cfg.defslen = defslen; int res = ulas_main(cfg); diff --git a/src/test.c b/src/test.c index 8410575..8b912b2 100644 --- a/src/test.c +++ b/src/test.c @@ -410,6 +410,8 @@ void test_symscope(void) { #define ASSERT_FULL_ASM(expect_rc, in_path, expect_path) \ { \ struct ulas_config cfg = ulas_cfg_from_env(); \ + char *defs[] = {"ULAS_PREDEF"}; \ + cfg.defs = defs; cfg.defslen = 1; \ ASSERT_FULL(expect_rc, in_path, expect_path) \ } diff --git a/src/ulas.c b/src/ulas.c index a4dedf5..de11a36 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -18,9 +18,13 @@ struct ulas ulas; char *incpaths[ULAS_INCPATHSMAX]; unsigned long incpathslen = 0; +char *defs[ULAS_DEFSMAX]; +unsigned long defslen = 0; + void ulas_help(void) { printf("%s\n", ULAS_NAME); - printf("Usage %s [-%s] [-o=path] [-i=path] [-l=path] [-a=initial-address] [-S=ulas|mlb] " + printf("Usage %s [-%s] [-o=path] [-i=path] [-l=path] [-a=initial-address] " + "[-S=ulas|mlb] " "[input]\n\n", ULAS_NAME, ULAS_OPTS); ULAS_HELP("h", "display this help and exit"); @@ -35,12 +39,12 @@ void ulas_help(void) { ULAS_HELP("A", "Print addresses in disassembler mode"); ULAS_HELP("d", "Disassemble a file"); ULAS_HELP("S", "Set the symbol format"); + ULAS_HELP("D=name", "Define a symbol"); ULAS_HELP("w=warning", "Toggle warnings: a=all, o=overflow"); } void ulas_version(void) { printf("%s version %s\n", ULAS_NAME, ULAS_VER); } - void ulas_init(struct ulas_config cfg) { // init global cfg if (ulasin == NULL) { @@ -365,8 +369,8 @@ int ulas_symbolout_mlbloc(FILE *dst, long addr) { fprintf(dst, "Unknown:"); return 0; } - - // TODO: maybe allow the user to define this by using + + // TODO: maybe allow the user to define this by using // .section and just trust the label location in the source // is correct switch (ulas.arch.type) { @@ -1216,6 +1220,14 @@ struct ulas_preproc ulas_preprocinit(void) { pp.macroparam[i] = ulas_str(8); } pp.macrobuf = ulas_str(8); + + // set up initial defs + for (int i = 0; i < ulascfg.defslen; i++) { + + struct ulas_ppdef def = {ULAS_PPDEF, strdup(ulascfg.defs[i]), strdup(""), 0}; + ulas_preprocdef(&pp, def); + } + return pp; } diff --git a/src/ulas.h b/src/ulas.h index 9a7ea12..f85c5bf 100644 --- a/src/ulas.h +++ b/src/ulas.h @@ -13,7 +13,7 @@ #define ULAS_OPTS "hvVpdA" // args with value -#define ULAS_OPTS_ARG "o:l:s:i:w:a:S:" +#define ULAS_OPTS_ARG "o:l:s:i:w:a:S:D:" #define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc); @@ -22,6 +22,10 @@ extern char *incpaths[ULAS_INCPATHSMAX]; extern unsigned long incpathslen; +#define ULAS_DEFSMAX 256 +extern char *defs[ULAS_DEFSMAX]; +extern unsigned long defslen; + // if this is used as a path use stdin or stdout instead #define ULAS_STDFILEPATH "-" #define ULAS_PATHSEP "/" @@ -161,6 +165,10 @@ struct ulas_config { unsigned int incpathslen; enum ulas_warm warn_level; + + // initial defs for preproc + char **defs; + unsigned int defslen; }; /** diff --git a/tests/t0.bin b/tests/t0.bin index 4f18f9a..bd436c2 100644 Binary files a/tests/t0.bin and b/tests/t0.bin differ diff --git a/tests/t0.s b/tests/t0.s index 18f76f8..684ffa3 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -160,3 +160,10 @@ toplevel 5, 6 : halt +#ifdef ULAS_PREDEF + halt +#endif + +#ifndef ULAS_PREDEF + xor a, a +#endif diff --git a/tests/t0_dasm.s b/tests/t0_dasm.s index 0ea1f10..c682cad 100644 --- a/tests/t0_dasm.s +++ b/tests/t0_dasm.s @@ -119,5 +119,5 @@ .db 0x74 dec b inc b -.db 0x6 + ld b, 0x76 .db 0x76