Port some cleanup from the WIP 'strings' branch

This is mostly variable renaming
This commit is contained in:
Rangi
2021-04-25 19:50:54 -04:00
committed by Rangi
parent bba532193b
commit d37aa93a7d
5 changed files with 81 additions and 84 deletions

View File

@@ -61,9 +61,9 @@ rgbasm_obj := \
src/asm/lexer.o \ src/asm/lexer.o \
src/asm/macro.o \ src/asm/macro.o \
src/asm/main.o \ src/asm/main.o \
src/asm/parser.o \
src/asm/opt.o \ src/asm/opt.o \
src/asm/output.o \ src/asm/output.o \
src/asm/parser.o \
src/asm/rpn.o \ src/asm/rpn.o \
src/asm/section.o \ src/asm/section.o \
src/asm/symbol.o \ src/asm/symbol.o \
@@ -204,7 +204,7 @@ checkpatch:
# compilation and make the continous integration infrastructure return failure. # compilation and make the continous integration infrastructure return failure.
develop: develop:
$Qenv $(MAKE) -j WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \ $Qenv $(MAKE) WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \
-Wno-sign-compare -Wvla -Wformat -Wformat-security -Wformat-overflow=2 \ -Wno-sign-compare -Wvla -Wformat -Wformat-security -Wformat-overflow=2 \
-Wformat-truncation=1 -Wformat-y2k -Wswitch-enum -Wunused \ -Wformat-truncation=1 -Wformat-y2k -Wswitch-enum -Wunused \
-Wuninitialized -Wunknown-pragmas -Wstrict-overflow=4 \ -Wuninitialized -Wunknown-pragmas -Wstrict-overflow=4 \

View File

@@ -9,8 +9,8 @@
#ifndef RGBDS_FORMAT_SPEC_H #ifndef RGBDS_FORMAT_SPEC_H
#define RGBDS_FORMAT_SPEC_H #define RGBDS_FORMAT_SPEC_H
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
enum FormatState { enum FormatState {
FORMAT_SIGN, // expects '+' or ' ' (optional) FORMAT_SIGN, // expects '+' or ' ' (optional)

View File

@@ -82,8 +82,7 @@ static inline bool sym_IsConstant(struct Symbol const *sym)
static inline bool sym_IsNumeric(struct Symbol const *sym) static inline bool sym_IsNumeric(struct Symbol const *sym)
{ {
return sym->type == SYM_LABEL || sym->type == SYM_EQU return sym->type == SYM_LABEL || sym->type == SYM_EQU || sym->type == SYM_SET;
|| sym->type == SYM_SET;
} }
static inline bool sym_IsLabel(struct Symbol const *sym) static inline bool sym_IsLabel(struct Symbol const *sym)
@@ -124,19 +123,19 @@ struct Symbol *sym_AddEqu(char const *symName, int32_t value);
struct Symbol *sym_AddSet(char const *symName, int32_t value); struct Symbol *sym_AddSet(char const *symName, int32_t value);
uint32_t sym_GetPCValue(void); uint32_t sym_GetPCValue(void);
uint32_t sym_GetConstantSymValue(struct Symbol const *sym); uint32_t sym_GetConstantSymValue(struct Symbol const *sym);
uint32_t sym_GetConstantValue(char const *s); uint32_t sym_GetConstantValue(char const *symName);
/* /*
* Find a symbol by exact name, bypassing expansion checks * Find a symbol by exact name, bypassing expansion checks
*/ */
struct Symbol *sym_FindExactSymbol(char const *name); struct Symbol *sym_FindExactSymbol(char const *symName);
/* /*
* Find a symbol by exact name; may not be scoped, produces an error if it is * Find a symbol by exact name; may not be scoped, produces an error if it is
*/ */
struct Symbol *sym_FindUnscopedSymbol(char const *name); struct Symbol *sym_FindUnscopedSymbol(char const *symName);
/* /*
* Find a symbol, possibly scoped, by name * Find a symbol, possibly scoped, by name
*/ */
struct Symbol *sym_FindScopedSymbol(char const *name); struct Symbol *sym_FindScopedSymbol(char const *symName);
struct Symbol const *sym_GetPC(void); struct Symbol const *sym_GetPC(void);
struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size); struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size);
struct Symbol *sym_Ref(char const *symName); struct Symbol *sym_Ref(char const *symName);

View File

@@ -943,10 +943,10 @@ restart:
} else if (c == '{' && !lexerState->disableInterpolation) { } else if (c == '{' && !lexerState->disableInterpolation) {
/* If character is an open brace, do symbol interpolation */ /* If character is an open brace, do symbol interpolation */
shiftChar(); shiftChar();
char const *ptr = readInterpolation(0); char const *str = readInterpolation(0);
if (ptr && ptr[0]) if (str && str[0])
beginExpansion(ptr, false, ptr); beginExpansion(str, false, str);
goto restart; goto restart;
} }
@@ -1381,10 +1381,10 @@ static char const *readInterpolation(size_t depth)
if (c == '{') { /* Nested interpolation */ if (c == '{') { /* Nested interpolation */
shiftChar(); shiftChar();
char const *ptr = readInterpolation(depth + 1); char const *str = readInterpolation(depth + 1);
if (ptr && ptr[0]) if (str && str[0])
beginExpansion(ptr, false, ptr); beginExpansion(str, false, str);
continue; /* Restart, reading from the new buffer */ continue; /* Restart, reading from the new buffer */
} else if (c == EOF || c == '\r' || c == '\n' || c == '"') { } else if (c == EOF || c == '\r' || c == '\n' || c == '"') {
error("Missing }\n"); error("Missing }\n");
@@ -1490,6 +1490,7 @@ static void readString(void)
size_t i = 0; size_t i = 0;
bool multiline = false; bool multiline = false;
char const *str;
// We reach this function after reading a single quote, but we also support triple quotes // We reach this function after reading a single quote, but we also support triple quotes
if (peek() == '"') { if (peek() == '"') {
@@ -1584,8 +1585,7 @@ static void readString(void)
case '9': case '9':
case '<': case '<':
shiftChar(); shiftChar();
char const *str = readMacroArg(c); str = readMacroArg(c);
if (str) { if (str) {
while (*str) while (*str)
append_yylval_string(*str++); append_yylval_string(*str++);
@@ -1608,11 +1608,10 @@ static void readString(void)
// We'll be exiting the string scope, so re-enable expansions // We'll be exiting the string scope, so re-enable expansions
// (Not interpolations, since they're handled by the function itself...) // (Not interpolations, since they're handled by the function itself...)
lexerState->disableMacroArgs = false; lexerState->disableMacroArgs = false;
char const *ptr = readInterpolation(0); str = readInterpolation(0);
if (str) {
if (ptr) { while (*str)
while (*ptr) append_yylval_string(*str++);
append_yylval_string(*ptr++);
} }
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
continue; // Do not copy an additional character continue; // Do not copy an additional character
@@ -1642,6 +1641,7 @@ static size_t appendStringLiteral(size_t i)
lexerState->disableInterpolation = true; lexerState->disableInterpolation = true;
bool multiline = false; bool multiline = false;
char const *str;
// We reach this function after reading a single quote, but we also support triple quotes // We reach this function after reading a single quote, but we also support triple quotes
append_yylval_string('"'); append_yylval_string('"');
@@ -1733,9 +1733,8 @@ static size_t appendStringLiteral(size_t i)
case '9': case '9':
case '<': case '<':
shiftChar(); shiftChar();
char const *str = readMacroArg(c); str = readMacroArg(c);
if (str && str[0])
if (str)
i = appendEscapedSubstring(str, i); i = appendEscapedSubstring(str, i);
continue; // Do not copy an additional character continue; // Do not copy an additional character
@@ -1761,10 +1760,9 @@ static size_t appendStringLiteral(size_t i)
// We'll be exiting the string scope, so re-enable expansions // We'll be exiting the string scope, so re-enable expansions
// (Not interpolations, since they're handled by the function itself...) // (Not interpolations, since they're handled by the function itself...)
lexerState->disableMacroArgs = false; lexerState->disableMacroArgs = false;
char const *ptr = readInterpolation(0); str = readInterpolation(0);
if (str && str[0])
if (ptr && ptr[0]) i = appendEscapedSubstring(str, i);
i = appendEscapedSubstring(ptr, i);
lexerState->disableMacroArgs = true; lexerState->disableMacroArgs = true;
continue; // Do not copy an additional character continue; // Do not copy an additional character

View File

@@ -179,15 +179,15 @@ static void updateSymbolFilename(struct Symbol *sym)
/* /*
* Create a new symbol by name * Create a new symbol by name
*/ */
static struct Symbol *createsymbol(char const *s) static struct Symbol *createsymbol(char const *symName)
{ {
struct Symbol *symbol = malloc(sizeof(*symbol)); struct Symbol *symbol = malloc(sizeof(*symbol));
if (!symbol) if (!symbol)
fatalerror("Failed to create symbol '%s': %s\n", s, strerror(errno)); fatalerror("Failed to create symbol '%s': %s\n", symName, strerror(errno));
if (snprintf(symbol->name, MAXSYMLEN + 1, "%s", s) > MAXSYMLEN) if (snprintf(symbol->name, MAXSYMLEN + 1, "%s", symName) > MAXSYMLEN)
warning(WARNING_LONG_STR, "Symbol name is too long: '%s'\n", s); warning(WARNING_LONG_STR, "Symbol name is too long: '%s'\n", symName);
symbol->isExported = false; symbol->isExported = false;
symbol->isBuiltin = false; symbol->isBuiltin = false;
@@ -228,37 +228,37 @@ static void assignStringSymbol(struct Symbol *sym, char const *value)
sym->macroSize = strlen(string); sym->macroSize = strlen(string);
} }
struct Symbol *sym_FindExactSymbol(char const *name) struct Symbol *sym_FindExactSymbol(char const *symName)
{ {
return hash_GetElement(symbols, name); return hash_GetElement(symbols, symName);
} }
struct Symbol *sym_FindUnscopedSymbol(char const *name) struct Symbol *sym_FindUnscopedSymbol(char const *symName)
{ {
if (strchr(name, '.')) { if (strchr(symName, '.')) {
error("Expected non-scoped symbol name, not \"%s\"\n", name); error("Expected non-scoped symbol name, not \"%s\"\n", symName);
return NULL; return NULL;
} }
return sym_FindExactSymbol(name); return sym_FindExactSymbol(symName);
} }
struct Symbol *sym_FindScopedSymbol(char const *name) struct Symbol *sym_FindScopedSymbol(char const *symName)
{ {
char const *dotPtr = strchr(name, '.'); char const *dotPtr = strchr(symName, '.');
if (dotPtr) { if (dotPtr) {
if (strchr(dotPtr + 1, '.')) if (strchr(dotPtr + 1, '.'))
fatalerror("'%s' is a nonsensical reference to a nested local symbol\n", fatalerror("'%s' is a nonsensical reference to a nested local symbol\n",
name); symName);
/* If auto-scoped local label, expand the name */ /* If auto-scoped local label, expand the name */
if (dotPtr == name) { /* Meaning, the name begins with the dot */ if (dotPtr == symName) { /* Meaning, the name begins with the dot */
char fullname[MAXSYMLEN + 1]; char fullname[MAXSYMLEN + 1];
fullSymbolName(fullname, sizeof(fullname), name, labelScope); fullSymbolName(fullname, sizeof(fullname), symName, labelScope);
return sym_FindExactSymbol(fullname); return sym_FindExactSymbol(fullname);
} }
} }
return sym_FindExactSymbol(name); return sym_FindExactSymbol(symName);
} }
struct Symbol const *sym_GetPC(void) struct Symbol const *sym_GetPC(void)
@@ -287,7 +287,7 @@ void sym_Purge(char const *symName)
} else { } else {
/* Do not keep a reference to the label's name after purging it */ /* Do not keep a reference to the label's name after purging it */
if (symbol->name == labelScope) if (symbol->name == labelScope)
labelScope = NULL; sym_SetCurrentSymbolScope(NULL);
/* /*
* FIXME: this leaks symbol->macro for SYM_EQUS and SYM_MACRO, but this can't * FIXME: this leaks symbol->macro for SYM_EQUS and SYM_MACRO, but this can't
@@ -331,12 +331,12 @@ uint32_t sym_GetConstantSymValue(struct Symbol const *sym)
/* /*
* Return a constant symbol's value * Return a constant symbol's value
*/ */
uint32_t sym_GetConstantValue(char const *s) uint32_t sym_GetConstantValue(char const *symName)
{ {
struct Symbol const *sym = sym_FindScopedSymbol(s); struct Symbol const *sym = sym_FindScopedSymbol(symName);
if (sym == NULL) if (sym == NULL)
error("'%s' not defined\n", s); error("'%s' not defined\n", symName);
else else
return sym_GetConstantSymValue(sym); return sym_GetConstantSymValue(sym);
@@ -357,23 +357,23 @@ void sym_SetCurrentSymbolScope(char const *newScope)
* Create a symbol that will be non-relocatable and ensure that it * Create a symbol that will be non-relocatable and ensure that it
* hasn't already been defined or referenced in a context that would * hasn't already been defined or referenced in a context that would
* require that it be relocatable * require that it be relocatable
* @param symbolName The name of the symbol to create * @param symName The name of the symbol to create
* @param numeric If false, the symbol may not have been referenced earlier * @param numeric If false, the symbol may not have been referenced earlier
*/ */
static struct Symbol *createNonrelocSymbol(char const *symbolName, bool numeric) static struct Symbol *createNonrelocSymbol(char const *symName, bool numeric)
{ {
struct Symbol *symbol = sym_FindExactSymbol(symbolName); struct Symbol *symbol = sym_FindExactSymbol(symName);
if (!symbol) { if (!symbol) {
symbol = createsymbol(symbolName); symbol = createsymbol(symName);
} else if (sym_IsDefined(symbol)) { } else if (sym_IsDefined(symbol)) {
error("'%s' already defined at ", symbolName); error("'%s' already defined at ", symName);
dumpFilename(symbol); dumpFilename(symbol);
putc('\n', stderr); putc('\n', stderr);
return NULL; // Don't allow overriding the symbol, that'd be bad! return NULL; // Don't allow overriding the symbol, that'd be bad!
} else if (!numeric) { } else if (!numeric) {
// The symbol has already been referenced, but it's not allowed // The symbol has already been referenced, but it's not allowed
error("'%s' already referenced at ", symbolName); error("'%s' already referenced at ", symName);
dumpFilename(symbol); dumpFilename(symbol);
putc('\n', stderr); putc('\n', stderr);
return NULL; // Don't allow overriding the symbol, that'd be bad! return NULL; // Don't allow overriding the symbol, that'd be bad!
@@ -448,7 +448,7 @@ struct Symbol *sym_RedefString(char const *symName, char const *value)
} }
/* /*
* Alter a SET symbols value * Alter a SET symbol's value
*/ */
struct Symbol *sym_AddSet(char const *symName, int32_t value) struct Symbol *sym_AddSet(char const *symName, int32_t value)
{ {
@@ -474,18 +474,18 @@ struct Symbol *sym_AddSet(char const *symName, int32_t value)
/* /*
* Add a label (aka "relocatable symbol") * Add a label (aka "relocatable symbol")
* @param name The label's full name (so `.name` is invalid) * @param symName The label's full name (so `.name` is invalid)
* @return The created symbol * @return The created symbol
*/ */
static struct Symbol *addLabel(char const *name) static struct Symbol *addLabel(char const *symName)
{ {
assert(name[0] != '.'); /* The symbol name must have been expanded prior */ assert(symName[0] != '.'); /* The symbol name must have been expanded prior */
struct Symbol *sym = sym_FindExactSymbol(name); struct Symbol *sym = sym_FindExactSymbol(symName);
if (!sym) { if (!sym) {
sym = createsymbol(name); sym = createsymbol(symName);
} else if (sym_IsDefined(sym)) { } else if (sym_IsDefined(sym)) {
error("'%s' already defined at ", name); error("'%s' already defined at ", symName);
dumpFilename(sym); dumpFilename(sym);
putc('\n', stderr); putc('\n', stderr);
return NULL; return NULL;
@@ -500,62 +500,62 @@ static struct Symbol *addLabel(char const *name)
sym->section = sect_GetSymbolSection(); sym->section = sect_GetSymbolSection();
if (sym && !sym->section) if (sym && !sym->section)
error("Label \"%s\" created outside of a SECTION\n", name); error("Label \"%s\" created outside of a SECTION\n", symName);
return sym; return sym;
} }
/* /*
* Add a local (.name or Parent.name) relocatable symbol * Add a local (.name or Parent.name) relocatable symbol
*/ */
struct Symbol *sym_AddLocalLabel(char const *name) struct Symbol *sym_AddLocalLabel(char const *symName)
{ {
if (!labelScope) { if (!labelScope) {
error("Local label '%s' in main scope\n", name); error("Local label '%s' in main scope\n", symName);
return NULL; return NULL;
} }
char fullname[MAXSYMLEN + 1]; char fullname[MAXSYMLEN + 1];
if (name[0] == '.') { if (symName[0] == '.') {
/* If symbol is of the form `.name`, expand to the full `Parent.name` name */ /* If symbol is of the form `.name`, expand to the full `Parent.name` name */
fullSymbolName(fullname, sizeof(fullname), name, labelScope); fullSymbolName(fullname, sizeof(fullname), symName, labelScope);
name = fullname; /* Use the expanded name instead */ symName = fullname; /* Use the expanded name instead */
} else { } else {
size_t i = 0; size_t i = 0;
/* Otherwise, check that `Parent` is in fact the current scope */ /* Otherwise, check that `Parent` is in fact the current scope */
while (labelScope[i] && name[i] == labelScope[i]) while (labelScope[i] && symName[i] == labelScope[i])
i++; i++;
/* Assuming no dots in `labelScope` */ /* Assuming no dots in `labelScope` */
assert(strchr(&name[i], '.')); /* There should be at least one dot, though */ assert(strchr(&symName[i], '.')); /* There should be at least one dot, though */
size_t parentLen = i + (strchr(&name[i], '.') - name); size_t parentLen = i + (strchr(&symName[i], '.') - symName);
/* /*
* Check that `labelScope[i]` ended the check, guaranteeing that `name` is at least * Check that `labelScope[i]` ended the check, guaranteeing that `symName` is at
* as long, and then that this was the entirety of the `Parent` part of `name`. * least as long, and then that this was the entire `Parent` part of `symName`.
*/ */
if (labelScope[i] != '\0' || name[i] != '.') { if (labelScope[i] != '\0' || symName[i] != '.') {
assert(parentLen <= INT_MAX); assert(parentLen <= INT_MAX);
error("Not currently in the scope of '%.*s'\n", (int)parentLen, name); error("Not currently in the scope of '%.*s'\n", (int)parentLen, symName);
} }
if (strchr(&name[parentLen + 1], '.')) /* There will at least be a terminator */ if (strchr(&symName[parentLen + 1], '.')) /* There will at least be a terminator */
fatalerror("'%s' is a nonsensical reference to a nested local label\n", fatalerror("'%s' is a nonsensical reference to a nested local label\n",
name); symName);
} }
return addLabel(name); return addLabel(symName);
} }
/* /*
* Add a relocatable symbol * Add a relocatable symbol
*/ */
struct Symbol *sym_AddLabel(char const *name) struct Symbol *sym_AddLabel(char const *symName)
{ {
struct Symbol *sym = addLabel(name); struct Symbol *sym = addLabel(symName);
/* Set the symbol as the new scope */ /* Set the symbol as the new scope */
if (sym) if (sym)
labelScope = sym->name; sym_SetCurrentSymbolScope(sym->name);
return sym; return sym;
} }
@@ -677,9 +677,9 @@ void sym_SetExportAll(bool set)
exportall = set; exportall = set;
} }
static struct Symbol *createBuiltinSymbol(char const *name) static struct Symbol *createBuiltinSymbol(char const *symName)
{ {
struct Symbol *sym = createsymbol(name); struct Symbol *sym = createsymbol(symName);
sym->isBuiltin = true; sym->isBuiltin = true;
sym->hasCallback = true; sym->hasCallback = true;
@@ -755,7 +755,7 @@ void sym_Init(time_t now)
#undef addNumber #undef addNumber
#undef addString #undef addString
labelScope = NULL; sym_SetCurrentSymbolScope(NULL);
anonLabelID = 0; anonLabelID = 0;
/* _PI is deprecated */ /* _PI is deprecated */