From f1954a7f4c6e01edf569bf5011f4bede8dbb9470 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 16 Dec 2023 09:06:08 +0100 Subject: [PATCH] Added very basic -w flags --- include/ulas.h | 5 +++++ src/main.c | 11 ++++++++++- src/ulas.c | 17 ++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index 8ea8f14..0419c3d 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -71,6 +71,7 @@ fprintf(ulaserr, __VA_ARGS__); \ exit(-1); \ } +#define ULASWARNLEVEL(level) ulascfg.warn_level & (level) // format macros #define ULAS_FMT(f, fmt) \ @@ -109,6 +110,8 @@ extern FILE *ulassymout; struct ulas_expr; struct ulas_tok; +enum ulas_warm { ULAS_WARN_OVERFLOW = 1, ULAS_WARN_ALL = 0x7FFFFFFF }; + struct ulas_config { // argv represents file names char **argv; @@ -124,6 +127,8 @@ struct ulas_config { // all include search paths char **incpaths; int incpathslen; + + enum ulas_warm warn_level; }; /** diff --git a/src/main.c b/src/main.c index 94d7b8b..24e4daf 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ #define ULAS_OPTS "hvVp" // args with value -#define ULAS_OPTS_ARG "o:l:s:i:" +#define ULAS_OPTS_ARG "o:l:s:i:w:" #define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc); @@ -43,11 +43,17 @@ void ulas_help(void) { ULAS_HELP("l=path", "Listing file"); ULAS_HELP("s=path", "Symbols file"); ULAS_HELP("i=path", "Add include search path"); + 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_getopt(int argc, char **argv, struct ulas_config *cfg) { + int warnings[255]; + memset(warnings, 0, 255 * sizeof(int)); + warnings['a'] = ULAS_WARN_ALL; + warnings['o'] = ULAS_WARN_OVERFLOW; + int c = 0; while ((c = getopt(argc, argv, ULAS_OPTS ULAS_OPTS_ARG)) != -1) { switch (c) { @@ -77,6 +83,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) { case 'i': assert(incpathslen < ULAS_INCPATHSMAX); incpaths[incpathslen++] = strndup(optarg, ULAS_PATHMAX); + case 'w': + cfg->warn_level ^= warnings[(int)optarg[0]]; + break; case '?': break; default: diff --git a/src/ulas.c b/src/ulas.c index 79159cd..17114a4 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -104,6 +104,8 @@ struct ulas_config ulas_cfg_from_env(void) { struct ulas_config cfg; memset(&cfg, 0, sizeof(cfg)); + cfg.warn_level = ULAS_WARN_OVERFLOW; + return cfg; } @@ -2150,10 +2152,19 @@ int ulas_asminstr(char *dst, unsigned long max, const char **line, } else if (tok[i] == ULAS_E8 || tok[i] == ULAS_E16) { assert(expridx < ULAS_INSTRDATMAX); int rc = 0; - exprres[expridx++] = ulas_intexpr(line, n, &rc); + int res = ulas_intexpr(line, n, &rc); + exprres[expridx++] = res; if (rc == -1) { return -1; } + + if (ULASWARNLEVEL(ULAS_WARN_OVERFLOW) && + (unsigned int)res > 0xFF && tok[i] == ULAS_E8) { + ULASWARN("Warning: 0x%X overflows the maximum allowed value of 0xFF\n", res); + } else if (ULASWARNLEVEL(ULAS_WARN_OVERFLOW) && + (unsigned int)res > 0xFFFF && tok[i] == ULAS_E16) { + ULASWARN("Warning: 0x%X overflows the maximum allowed value of 0xFFFF\n", res); + } } else { if (ulas_tok(&ulas.tok, line, n) == -1) { goto skip; @@ -2363,8 +2374,8 @@ int ulas_asmdirdefenum(const char **line, unsigned long n) { } union ulas_val val = {0}; - val.intv = ulas.enumv; - + val.intv = ulas.enumv; + int rc = 0; ULAS_EVALEXPRS(ulas.enumv += ulas_intexpr(line, n, &rc)); if (rc == -1) { -- 2.30.2