Clean up symbol system

Get rid of Hungarian notation
Improve encapsulation (the rest of the world should not touch PC directly)
This commit is contained in:
ISSOtm
2020-04-09 10:42:24 +02:00
parent 5ea8490e2b
commit f9f27d6f5a
17 changed files with 356 additions and 394 deletions

View File

@@ -34,7 +34,6 @@ extern uint32_t unionStart[MAXUNIONS];
extern uint32_t unionSize[MAXUNIONS]; extern uint32_t unionSize[MAXUNIONS];
extern char tzCurrentFileName[_MAX_PATH + 1]; extern char tzCurrentFileName[_MAX_PATH + 1];
extern struct Section *pCurrentSection; extern struct Section *pCurrentSection;
extern struct sSymbol *pPCSymbol;
extern bool oDontExpandStrings; extern bool oDontExpandStrings;
size_t symvaluetostring(char *dest, size_t maxLength, char *sym, size_t symvaluetostring(char *dest, size_t maxLength, char *sym,

View File

@@ -25,7 +25,7 @@ struct MacroArgs;
struct sContext { struct sContext {
YY_BUFFER_STATE FlexHandle; YY_BUFFER_STATE FlexHandle;
struct sSymbol *pMacro; struct Symbol const *pMacro;
struct sContext *pNext; struct sContext *pNext;
char tzFileName[_MAX_PATH + 1]; char tzFileName[_MAX_PATH + 1];
struct MacroArgs *macroArgs; struct MacroArgs *macroArgs;
@@ -48,7 +48,7 @@ void fstk_Dump(void);
void fstk_DumpToStr(char *buf, size_t len); void fstk_DumpToStr(char *buf, size_t len);
void fstk_DumpStringExpansions(void); void fstk_DumpStringExpansions(void);
void fstk_AddIncludePath(char *s); void fstk_AddIncludePath(char *s);
bool fstk_RunMacro(char *s, struct MacroArgs *args); void fstk_RunMacro(char *s, struct MacroArgs *args);
void fstk_RunRept(uint32_t count, int32_t nReptLineNo); void fstk_RunRept(uint32_t count, int32_t nReptLineNo);
FILE *fstk_FindFile(char const *fname, char **incPathUsed); FILE *fstk_FindFile(char const *fname, char **incPathUsed);
int32_t fstk_GetLine(void); int32_t fstk_GetLine(void);

View File

@@ -56,7 +56,7 @@ void setup_lexer(void);
void yy_set_state(enum eLexerState i); void yy_set_state(enum eLexerState i);
YY_BUFFER_STATE yy_create_buffer(FILE *f); YY_BUFFER_STATE yy_create_buffer(FILE *f);
YY_BUFFER_STATE yy_scan_bytes(char *mem, uint32_t size); YY_BUFFER_STATE yy_scan_bytes(char const *mem, uint32_t size);
void yy_delete_buffer(YY_BUFFER_STATE buf); void yy_delete_buffer(YY_BUFFER_STATE buf);
void yy_switch_to_buffer(YY_BUFFER_STATE buf); void yy_switch_to_buffer(YY_BUFFER_STATE buf);
uint32_t lex_FloatAlloc(const struct sLexFloat *tok); uint32_t lex_FloatAlloc(const struct sLexFloat *tok);

View File

@@ -29,53 +29,70 @@ enum SymbolType {
SYM_REF // Forward reference to a label SYM_REF // Forward reference to a label
}; };
struct sSymbol { struct Symbol {
char tzName[MAXSYMLEN + 1]; char name[MAXSYMLEN + 1];
enum SymbolType type; enum SymbolType type;
bool isExported; /* Whether the symbol is to be exported */ bool isExported; /* Whether the symbol is to be exported */
bool isBuiltin; /* Whether the symbol is a built-in */ bool isBuiltin; /* Whether the symbol is a built-in */
struct sSymbol *pScope; struct Symbol const *scope;
struct Section *pSection; struct Section *section;
int32_t nValue; char fileName[_MAX_PATH + 1]; /* File where the symbol was defined. */
uint32_t ulMacroSize; uint32_t fileLine; /* Line where the symbol was defined. */
char *pMacro;
int32_t (*Callback)(struct sSymbol const *self); union {
char tzFileName[_MAX_PATH + 1]; /* File where the symbol was defined. */ struct { /* If sym_IsNumeric */
uint32_t nFileLine; /* Line where the symbol was defined. */ int32_t value;
int32_t (*callback)(void);
};
struct { /* For SYM_MACRO */
uint32_t macroSize;
char *macro;
};
};
uint32_t ID; /* ID of the symbol in the object file (-1 if none) */ uint32_t ID; /* ID of the symbol in the object file (-1 if none) */
struct sSymbol *next; /* Next object to output in the object file */ struct Symbol *next; /* Next object to output in the object file */
}; };
static inline bool sym_IsDefined(struct sSymbol const *sym) bool sym_IsPC(struct Symbol const *sym);
static inline bool sym_IsDefined(struct Symbol const *sym)
{ {
return sym->type != SYM_REF; return sym->type != SYM_REF;
} }
static inline bool sym_IsConstant(struct sSymbol const *sym) static inline struct Section *sym_GetSection(struct Symbol const *sym)
{ {
return sym->type == SYM_EQU || sym->type == SYM_SET return sym_IsPC(sym) ? sect_GetSymbolSection() : sym->section;
|| (sym->type == SYM_LABEL && sym->pSection
&& sym->pSection->nOrg != -1);
} }
static inline bool sym_IsNumeric(struct sSymbol const *sym) static inline bool sym_IsConstant(struct Symbol const *sym)
{
if (sym->type == SYM_LABEL) {
struct Section const *sect = sym_GetSection(sym);
return sect && sect->nOrg != -1;
}
return sym->type == SYM_EQU || sym->type == SYM_SET;
}
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 sSymbol const *sym) static inline bool sym_IsLabel(struct Symbol const *sym)
{ {
return sym->type == SYM_LABEL || sym->type == SYM_REF; return sym->type == SYM_LABEL || sym->type == SYM_REF;
} }
static inline bool sym_IsLocal(struct sSymbol const *sym) static inline bool sym_IsLocal(struct Symbol const *sym)
{ {
return sym_IsLabel(sym) && strchr(sym->tzName, '.'); return sym_IsLabel(sym) && strchr(sym->name, '.');
} }
static inline bool sym_IsExported(struct sSymbol const *sym) static inline bool sym_IsExported(struct Symbol const *sym)
{ {
return sym->isExported; return sym->isExported;
} }
@@ -83,33 +100,32 @@ static inline bool sym_IsExported(struct sSymbol const *sym)
/* /*
* Get a string equate's value * Get a string equate's value
*/ */
static inline char *sym_GetStringValue(struct sSymbol const *sym) static inline char const *sym_GetStringValue(struct Symbol const *sym)
{ {
return sym->pMacro; return sym->macro;
} }
void sym_ForEach(void (*func)(struct sSymbol *, void *), void *arg); void sym_ForEach(void (*func)(struct Symbol *, void *), void *arg);
int32_t sym_GetValue(struct sSymbol const *sym); int32_t sym_GetValue(struct Symbol const *sym);
void sym_SetExportAll(bool set); void sym_SetExportAll(bool set);
struct sSymbol *sym_AddLocalReloc(char const *tzSym); struct Symbol *sym_AddLocalReloc(char const *symName);
struct sSymbol *sym_AddReloc(char const *tzSym); struct Symbol *sym_AddReloc(char const *symName);
void sym_Export(char const *tzSym); void sym_Export(char const *symName);
struct sSymbol *sym_FindMacro(char const *s); struct Symbol *sym_AddEqu(char const *symName, int32_t value);
struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value); struct Symbol *sym_AddSet(char const *symName, int32_t value);
struct sSymbol *sym_AddSet(char const *tzSym, int32_t value); uint32_t sym_GetPCValue(void);
void sym_Init(void);
uint32_t sym_GetConstantValue(char const *s); uint32_t sym_GetConstantValue(char const *s);
struct sSymbol *sym_FindSymbol(char const *tzName); struct Symbol *sym_FindSymbol(char const *symName);
char *sym_GetStringValue(struct sSymbol const *sym); struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo);
struct sSymbol *sym_AddMacro(char const *tzSym, int32_t nDefLineNo); struct Symbol *sym_Ref(char const *symName);
struct sSymbol *sym_Ref(char const *tzSym); struct Symbol *sym_AddString(char const *symName, char const *value);
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue);
uint32_t sym_GetDefinedValue(char const *s); uint32_t sym_GetDefinedValue(char const *s);
void sym_Purge(char const *tzName); void sym_Purge(char const *symName);
void sym_Init(void);
/* Functions to save and restore the current symbol scope. */ /* Functions to save and restore the current symbol scope. */
struct sSymbol *sym_GetCurrentSymbolScope(void); struct Symbol *sym_GetCurrentSymbolScope(void);
void sym_SetCurrentSymbolScope(struct sSymbol *pNewScope); void sym_SetCurrentSymbolScope(struct Symbol *newScope);
#endif /* RGBDS_SYMBOL_H */ #endif /* RGBDS_SYMBOL_H */

View File

@@ -43,7 +43,7 @@ size_t symvaluetostring(char *dest, size_t maxLength, char *symName,
const char *mode) const char *mode)
{ {
size_t length; size_t length;
struct sSymbol *sym = sym_FindSymbol(symName); struct Symbol *sym = sym_FindSymbol(symName);
if (sym && sym->type == SYM_EQUS) { if (sym && sym->type == SYM_EQUS) {
char const *src = sym_GetStringValue(sym); char const *src = sym_GetStringValue(sym);
@@ -710,8 +710,7 @@ macro : T_ID {
yy_set_state(LEX_STATE_MACROARGS); yy_set_state(LEX_STATE_MACROARGS);
} macroargs { } macroargs {
yy_set_state(LEX_STATE_NORMAL); yy_set_state(LEX_STATE_NORMAL);
if (!fstk_RunMacro($1, $3)) fstk_RunMacro($1, $3);
fatalerror("Macro '%s' not defined", $1);
} }
; ;
@@ -1334,7 +1333,7 @@ relocexpr_no_str : scoped_id { rpn_Symbol(&$$, $1); }
| T_OP_DEF { | T_OP_DEF {
oDontExpandStrings = true; oDontExpandStrings = true;
} '(' scoped_id ')' { } '(' scoped_id ')' {
struct sSymbol const *sym = sym_FindSymbol($4); struct Symbol const *sym = sym_FindSymbol($4);
rpn_Number(&$$, !!sym); rpn_Number(&$$, !!sym);

View File

@@ -34,7 +34,7 @@
static struct sContext *pFileStack; static struct sContext *pFileStack;
static unsigned int nFileStackDepth; static unsigned int nFileStackDepth;
unsigned int nMaxRecursionDepth; unsigned int nMaxRecursionDepth;
static struct sSymbol *pCurrentMacro; static struct Symbol const *pCurrentMacro;
static YY_BUFFER_STATE CurrentFlexHandle; static YY_BUFFER_STATE CurrentFlexHandle;
static FILE *pCurrentFile; static FILE *pCurrentFile;
static uint32_t nCurrentStatus; static uint32_t nCurrentStatus;
@@ -449,52 +449,38 @@ void fstk_RunInclude(char *tzFileName)
/* /*
* Set up a macro for parsing * Set up a macro for parsing
*/ */
bool fstk_RunMacro(char *s, struct MacroArgs *args) void fstk_RunMacro(char *s, struct MacroArgs *args)
{ {
struct sSymbol *sym = sym_FindMacro(s); struct Symbol const *sym = sym_FindSymbol(s);
int nPrintedChars; int nPrintedChars;
if (sym == NULL || sym->pMacro == NULL) if (sym == NULL) {
return false; yyerror("Macro \"%s\" not defined", s);
return;
}
if (sym->type != SYM_MACRO) {
yyerror("\"%s\" is not a macro", s);
return;
}
pushcontext(); pushcontext();
macro_SetUniqueID(nMacroCount++); macro_SetUniqueID(nMacroCount++);
/* Minus 1 because there is a newline at the beginning of the buffer */ /* Minus 1 because there is a newline at the beginning of the buffer */
nLineNo = sym->nFileLine - 1; nLineNo = sym->fileLine - 1;
macro_UseNewArgs(args); macro_UseNewArgs(args);
nCurrentStatus = STAT_isMacro; nCurrentStatus = STAT_isMacro;
nPrintedChars = snprintf(tzCurrentFileName, _MAX_PATH + 1, nPrintedChars = snprintf(tzCurrentFileName, _MAX_PATH + 1,
"%s::%s", sym->tzFileName, s); "%s::%s", sym->fileName, s);
if (nPrintedChars > _MAX_PATH) { if (nPrintedChars > _MAX_PATH) {
popcontext(); popcontext();
fatalerror("File name + macro name is too large to fit into buffer"); fatalerror("File name + macro name is too large to fit into buffer");
} }
pCurrentMacro = sym; pCurrentMacro = sym;
CurrentFlexHandle = yy_scan_bytes(pCurrentMacro->pMacro, /* TODO: why is `strlen` being used when there's a macro size field? */
strlen(pCurrentMacro->pMacro)); CurrentFlexHandle = yy_scan_bytes(pCurrentMacro->macro,
strlen(pCurrentMacro->macro));
yy_switch_to_buffer(CurrentFlexHandle); yy_switch_to_buffer(CurrentFlexHandle);
return true;
}
/*
* Set up a stringequate for parsing
*/
void fstk_RunString(char *s)
{
const struct sSymbol *pSym = sym_FindSymbol(s);
if (pSym != NULL) {
pushcontext();
nCurrentStatus = STAT_isMacroArg;
strcpy(tzCurrentFileName, s);
CurrentFlexHandle =
yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
yy_switch_to_buffer(CurrentFlexHandle);
} else {
yyerror("No such string symbol '%s'", s);
}
} }
/* /*

View File

@@ -277,10 +277,10 @@ uint32_t ParseSymbol(char *src, uint32_t size)
/* If the symbol is an EQUS, expand it */ /* If the symbol is an EQUS, expand it */
if (!oDontExpandStrings) { if (!oDontExpandStrings) {
struct sSymbol const *sym = sym_FindSymbol(dest); struct Symbol const *sym = sym_FindSymbol(dest);
if (sym && sym->type == SYM_EQUS) { if (sym && sym->type == SYM_EQUS) {
char *s; char const *s;
lex_BeginStringExpansion(dest); lex_BeginStringExpansion(dest);

View File

@@ -190,7 +190,7 @@ static void yy_buffer_append_newlines(YY_BUFFER_STATE buf, size_t capacity)
} }
} }
YY_BUFFER_STATE yy_scan_bytes(char *mem, uint32_t size) YY_BUFFER_STATE yy_scan_bytes(char const *mem, uint32_t size)
{ {
YY_BUFFER_STATE pBuffer = malloc(sizeof(struct yy_buffer_state)); YY_BUFFER_STATE pBuffer = malloc(sizeof(struct yy_buffer_state));

View File

@@ -55,8 +55,8 @@ char *tzObjectname;
struct Section *pSectionList, *pCurrentSection; struct Section *pSectionList, *pCurrentSection;
/* Linked list of symbols to put in the object file */ /* Linked list of symbols to put in the object file */
static struct sSymbol *objectSymbols = NULL; static struct Symbol *objectSymbols = NULL;
static struct sSymbol **objectSymbolsTail = &objectSymbols; static struct Symbol **objectSymbolsTail = &objectSymbols;
static uint32_t nbSymbols = 0; /* Length of the above list */ static uint32_t nbSymbols = 0; /* Length of the above list */
static struct Assertion *assertions = NULL; static struct Assertion *assertions = NULL;
@@ -196,17 +196,17 @@ static void writesection(struct Section const *pSect, FILE *f)
/* /*
* Write a symbol to a file * Write a symbol to a file
*/ */
static void writesymbol(struct sSymbol const *pSym, FILE *f) static void writesymbol(struct Symbol const *sym, FILE *f)
{ {
fputstring(pSym->tzName, f); fputstring(sym->name, f);
if (!sym_IsDefined(pSym)) { if (!sym_IsDefined(sym)) {
fputc(SYMTYPE_IMPORT, f); fputc(SYMTYPE_IMPORT, f);
} else { } else {
fputc(pSym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f); fputc(sym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f);
fputstring(pSym->tzFileName, f); fputstring(sym->fileName, f);
fputlong(pSym->nFileLine, f); fputlong(sym->fileLine, f);
fputlong(getSectIDIfAny(pSym->pSection), f); fputlong(getSectIDIfAny(sym_GetSection(sym)), f);
fputlong(pSym->nValue, f); fputlong(sym->value, f);
} }
} }
@@ -214,7 +214,7 @@ static void writesymbol(struct sSymbol const *pSym, FILE *f)
* Returns a symbol's ID within the object file * Returns a symbol's ID within the object file
* If the symbol does not have one, one is assigned by registering the symbol * If the symbol does not have one, one is assigned by registering the symbol
*/ */
static uint32_t getSymbolID(struct sSymbol *sym) static uint32_t getSymbolID(struct Symbol *sym)
{ {
if (sym->ID == -1) { if (sym->ID == -1) {
sym->ID = nbSymbols++; sym->ID = nbSymbols++;
@@ -247,7 +247,7 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
{ {
for (unsigned int i = -1; (tzSym[++i] = popbyte()); ) for (unsigned int i = -1; (tzSym[++i] = popbyte()); )
; ;
struct sSymbol *sym = sym_FindSymbol(tzSym); struct Symbol *sym = sym_FindSymbol(tzSym);
uint32_t value; uint32_t value;
if (sym_IsConstant(sym)) { if (sym_IsConstant(sym)) {
@@ -267,7 +267,7 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
{ {
for (unsigned int i = -1; (tzSym[++i] = popbyte()); ) for (unsigned int i = -1; (tzSym[++i] = popbyte()); )
; ;
struct sSymbol *sym = sym_FindSymbol(tzSym); struct Symbol *sym = sym_FindSymbol(tzSym);
uint32_t value = getSymbolID(sym); uint32_t value = getSymbolID(sym);
writebyte(RPN_BANK_SYM); writebyte(RPN_BANK_SYM);
@@ -367,10 +367,10 @@ static void writeassert(struct Assertion *assert, FILE *f)
fputstring(assert->message, f); fputstring(assert->message, f);
} }
static void registerExportedSymbol(struct sSymbol *symbol, void *arg) static void registerExportedSymbol(struct Symbol *symbol, void *arg)
{ {
(void)arg; (void)arg;
if (symbol->isExported && symbol->ID == -1) { if (sym_IsExported(symbol) && symbol->ID == -1) {
*objectSymbolsTail = symbol; *objectSymbolsTail = symbol;
objectSymbolsTail = &symbol->next; objectSymbolsTail = &symbol->next;
nbSymbols++; nbSymbols++;
@@ -396,7 +396,7 @@ void out_WriteObject(void)
fputlong(nbSymbols, f); fputlong(nbSymbols, f);
fputlong(countsections(), f); fputlong(countsections(), f);
for (struct sSymbol const *sym = objectSymbols; sym; sym = sym->next) for (struct Symbol const *sym = objectSymbols; sym; sym = sym->next)
writesymbol(sym, f); writesymbol(sym, f);
for (struct Section *sect = pSectionList; sect; sect = sect->pNext) for (struct Section *sect = pSectionList; sect; sect = sect->pNext)

View File

@@ -104,9 +104,9 @@ void rpn_Number(struct Expression *expr, uint32_t i)
void rpn_Symbol(struct Expression *expr, char *tzSym) void rpn_Symbol(struct Expression *expr, char *tzSym)
{ {
struct sSymbol *sym = sym_FindSymbol(tzSym); struct Symbol *sym = sym_FindSymbol(tzSym);
if (sym == pPCSymbol && !pPCSymbol->pSection) { if (sym_IsPC(sym) && !sect_GetSymbolSection()) {
yyerror("PC has no value outside a section"); yyerror("PC has no value outside a section");
rpn_Number(expr, 0); rpn_Number(expr, 0);
} else if (!sym || !sym_IsConstant(sym)) { } else if (!sym || !sym_IsConstant(sym)) {
@@ -114,9 +114,9 @@ void rpn_Symbol(struct Expression *expr, char *tzSym)
expr->isSymbol = true; expr->isSymbol = true;
sym_Ref(tzSym); sym_Ref(tzSym);
makeUnknown(expr, strcmp(tzSym, "@") makeUnknown(expr, sym_IsPC(sym)
? "'%s' is not constant at assembly time" ? "PC is not constant at assembly time"
: "PC is not constant at assembly time", : "'%s' is not constant at assembly time",
tzSym); tzSym);
expr->nRPNPatchSize += 5; /* 1-byte opcode + 4-byte symbol ID */ expr->nRPNPatchSize += 5; /* 1-byte opcode + 4-byte symbol ID */
@@ -126,7 +126,7 @@ void rpn_Symbol(struct Expression *expr, char *tzSym)
memcpy(ptr, tzSym, nameLen); memcpy(ptr, tzSym, nameLen);
/* RGBLINK assumes PC is at the byte being computed... */ /* RGBLINK assumes PC is at the byte being computed... */
if (sym == pPCSymbol && nPCOffset) { if (sym_IsPC(sym) && nPCOffset) {
struct Expression pc = *expr, offset; struct Expression pc = *expr, offset;
rpn_Number(&offset, nPCOffset); rpn_Number(&offset, nPCOffset);
@@ -157,10 +157,10 @@ void rpn_BankSelf(struct Expression *expr)
void rpn_BankSymbol(struct Expression *expr, char const *tzSym) void rpn_BankSymbol(struct Expression *expr, char const *tzSym)
{ {
struct sSymbol const *sym = sym_FindSymbol(tzSym); struct Symbol const *sym = sym_FindSymbol(tzSym);
/* The @ symbol is treated differently. */ /* The @ symbol is treated differently. */
if (sym == pPCSymbol) { if (sym_IsPC(sym)) {
rpn_BankSelf(expr); rpn_BankSelf(expr);
return; return;
} }
@@ -170,12 +170,13 @@ void rpn_BankSymbol(struct Expression *expr, char const *tzSym)
yyerror("BANK argument must be a label"); yyerror("BANK argument must be a label");
} else { } else {
sym_Ref(tzSym); sym_Ref(tzSym);
/* If the symbol didn't exist, `sym_Ref` created it */ if (!sym)
struct sSymbol *pSymbol = sym_FindSymbol(tzSym); /* If the symbol didn't exist, `sym_Ref` created it */
sym = sym_FindSymbol(tzSym);
if (pSymbol->pSection && pSymbol->pSection->nBank != -1) { if (sym_GetSection(sym) && sym_GetSection(sym)->nBank != -1) {
/* Symbol's section is known and bank is fixed */ /* Symbol's section is known and bank is fixed */
expr->nVal = pSymbol->pSection->nBank; expr->nVal = sym_GetSection(sym)->nBank;
} else { } else {
makeUnknown(expr, "\"%s\"'s bank is not known", tzSym); makeUnknown(expr, "\"%s\"'s bank is not known", tzSym);
expr->nRPNPatchSize += 5; /* opcode + 4-byte sect ID */ expr->nRPNPatchSize += 5; /* opcode + 4-byte sect ID */
@@ -293,7 +294,7 @@ static int32_t shift(int32_t shiftee, int32_t amount)
} }
} }
static struct sSymbol const *symbolOf(struct Expression const *expr) static struct Symbol const *symbolOf(struct Expression const *expr)
{ {
if (!rpn_isSymbol(expr)) if (!rpn_isSymbol(expr))
return NULL; return NULL;
@@ -304,14 +305,14 @@ static bool isDiffConstant(struct Expression const *src1,
struct Expression const *src2) struct Expression const *src2)
{ {
/* Check if both expressions only refer to a single symbol */ /* Check if both expressions only refer to a single symbol */
struct sSymbol const *symbol1 = symbolOf(src1); struct Symbol const *symbol1 = symbolOf(src1);
struct sSymbol const *symbol2 = symbolOf(src2); struct Symbol const *symbol2 = symbolOf(src2);
if (!symbol1 || !symbol2 if (!symbol1 || !symbol2
|| symbol1->type != SYM_LABEL || symbol2->type != SYM_LABEL) || symbol1->type != SYM_LABEL || symbol2->type != SYM_LABEL)
return false; return false;
return symbol1->pSection == symbol2->pSection; return sym_GetSection(symbol1) == sym_GetSection(symbol2);
} }
void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr, void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
@@ -424,8 +425,8 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
} }
} else if (op == RPN_SUB && isDiffConstant(src1, src2)) { } else if (op == RPN_SUB && isDiffConstant(src1, src2)) {
struct sSymbol const *symbol1 = symbolOf(src1); struct Symbol const *symbol1 = symbolOf(src1);
struct sSymbol const *symbol2 = symbolOf(src2); struct Symbol const *symbol2 = symbolOf(src2);
expr->nVal = sym_GetValue(symbol1) - sym_GetValue(symbol2); expr->nVal = sym_GetValue(symbol1) - sym_GetValue(symbol2);
expr->isKnown = true; expr->isKnown = true;

View File

@@ -15,7 +15,7 @@
struct SectionStackEntry { struct SectionStackEntry {
struct Section *pSection; struct Section *pSection;
struct sSymbol *pScope; /* Section's symbol scope */ struct Symbol *pScope; /* Section's symbol scope */
uint32_t offset; uint32_t offset;
struct SectionStackEntry *pNext; struct SectionStackEntry *pNext;
}; };
@@ -288,13 +288,11 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
/* /*
* Set the current section * Set the current section
*/ */
static void setSection(struct Section *pSect) static void changeSection(void)
{ {
if (nUnionDepth > 0) if (nUnionDepth > 0)
fatalerror("Cannot change the section within a UNION"); fatalerror("Cannot change the section within a UNION");
pPCSymbol->pSection = pSect;
sym_SetCurrentSymbolScope(NULL); sym_SetCurrentSymbolScope(NULL);
} }
@@ -309,7 +307,7 @@ void out_NewSection(char const *pzName, uint32_t type, uint32_t org,
struct Section *pSect = getSection(pzName, type, org, attribs, isUnion); struct Section *pSect = getSection(pzName, type, org, attribs, isUnion);
setSection(pSect); changeSection();
curOffset = isUnion ? 0 : pSect->size; curOffset = isUnion ? 0 : pSect->size;
pCurrentSection = pSect; pCurrentSection = pSect;
} }
@@ -329,7 +327,7 @@ void out_SetLoadSection(char const *name, uint32_t type, uint32_t org,
loadOffset = curOffset; loadOffset = curOffset;
curOffset = 0; /* curOffset -= loadOffset; */ curOffset = 0; /* curOffset -= loadOffset; */
setSection(pSect); changeSection();
currentLoadSection = pSect; currentLoadSection = pSect;
} }
@@ -339,7 +337,7 @@ void out_EndLoadSection(void)
yyerror("Found `ENDL` outside of a `LOAD` block"); yyerror("Found `ENDL` outside of a `LOAD` block");
currentLoadSection = NULL; currentLoadSection = NULL;
setSection(pCurrentSection); changeSection();
curOffset += loadOffset; curOffset += loadOffset;
loadOffset = 0; loadOffset = 0;
} }
@@ -359,9 +357,9 @@ void sect_AlignPC(uint8_t alignment, uint16_t offset)
struct Section *sect = sect_GetSymbolSection(); struct Section *sect = sect_GetSymbolSection();
if (sect->nOrg != -1) { if (sect->nOrg != -1) {
if ((sym_GetValue(pPCSymbol) - offset) % (1 << alignment)) if ((sym_GetPCValue() - offset) % (1 << alignment))
yyerror("Section's fixed address fails required alignment (PC = $%04x)", yyerror("Section's fixed address fails required alignment (PC = $%04x)",
sym_GetValue(pPCSymbol)); sym_GetPCValue());
} else if (sect->nAlign != 0) { } else if (sect->nAlign != 0) {
if ((((sect->alignOfs + curOffset) % (1 << sect->nAlign)) if ((((sect->alignOfs + curOffset) % (1 << sect->nAlign))
- offset) % (1 << alignment)) { - offset) % (1 << alignment)) {
@@ -553,7 +551,7 @@ void out_PCRelByte(struct Expression *expr)
writebyte(0); writebyte(0);
} else { } else {
/* Target is relative to the byte *after* the operand */ /* Target is relative to the byte *after* the operand */
uint16_t address = sym_GetValue(pPCSymbol) + 1; uint16_t address = sym_GetPCValue() + 1;
/* The offset wraps (jump from ROM to HRAM, for loopexample) */ /* The offset wraps (jump from ROM to HRAM, for loopexample) */
int16_t offset = expr->nVal - address; int16_t offset = expr->nVal - address;
@@ -698,6 +696,7 @@ void out_PushSection(void)
pSect->offset = curOffset; pSect->offset = curOffset;
pSect->pNext = pSectionStack; pSect->pNext = pSectionStack;
pSectionStack = pSect; pSectionStack = pSect;
/* TODO: maybe set current section to NULL? */
} }
void out_PopSection(void) void out_PopSection(void)
@@ -711,7 +710,7 @@ void out_PopSection(void)
struct SectionStackEntry *pSect; struct SectionStackEntry *pSect;
pSect = pSectionStack; pSect = pSectionStack;
setSection(pSect->pSection); changeSection();
pCurrentSection = pSect->pSection; pCurrentSection = pSect->pSection;
sym_SetCurrentSymbolScope(pSect->pScope); sym_SetCurrentSymbolScope(pSect->pScope);
curOffset = pSect->offset; curOffset = pSect->offset;

View File

@@ -35,112 +35,111 @@
HashMap symbols; HashMap symbols;
static struct sSymbol *pScope; /* Current section symbol scope */ static struct Symbol *symbolScope; /* Current section symbol scope */
struct sSymbol *pPCSymbol; static struct Symbol *PCSymbol;
static struct sSymbol *p_NARGSymbol; static char savedTIME[256];
static struct sSymbol *p__LINE__Symbol; static char savedDATE[256];
static char SavedTIME[256]; static char savedTIMESTAMP_ISO8601_LOCAL[256];
static char SavedDATE[256]; static char savedTIMESTAMP_ISO8601_UTC[256];
static char SavedTIMESTAMP_ISO8601_LOCAL[256]; static char savedDAY[3];
static char SavedTIMESTAMP_ISO8601_UTC[256]; static char savedMONTH[3];
static char SavedDAY[3]; static char savedYEAR[20];
static char SavedMONTH[3]; static char savedHOUR[3];
static char SavedYEAR[20]; static char savedMINUTE[3];
static char SavedHOUR[3]; static char savedSECOND[3];
static char SavedMINUTE[3];
static char SavedSECOND[3];
static bool exportall; static bool exportall;
bool sym_IsPC(struct Symbol const *sym)
{
return sym == PCSymbol;
}
struct ForEachArgs { struct ForEachArgs {
void (*func)(struct sSymbol *symbol, void *arg); void (*func)(struct Symbol *symbol, void *arg);
void *arg; void *arg;
}; };
static void forEachWrapper(void *_symbol, void *_argWrapper) static void forEachWrapper(void *_symbol, void *_argWrapper)
{ {
struct ForEachArgs *argWrapper = _argWrapper; struct ForEachArgs *argWrapper = _argWrapper;
struct sSymbol *symbol = _symbol; struct Symbol *symbol = _symbol;
argWrapper->func(symbol, argWrapper->arg); argWrapper->func(symbol, argWrapper->arg);
} }
void sym_ForEach(void (*func)(struct sSymbol *, void *), void *arg) void sym_ForEach(void (*func)(struct Symbol *, void *), void *arg)
{ {
struct ForEachArgs argWrapper = { .func = func, .arg = arg }; struct ForEachArgs argWrapper = { .func = func, .arg = arg };
hash_ForEach(symbols, forEachWrapper, &argWrapper); hash_ForEach(symbols, forEachWrapper, &argWrapper);
} }
static int32_t Callback_NARG(struct sSymbol const *self) static int32_t Callback_NARG(void)
{ {
(void)self;
return macro_NbArgs(); return macro_NbArgs();
} }
static int32_t Callback__LINE__(struct sSymbol const *self) static int32_t Callback__LINE__(void)
{ {
(void)self;
return nLineNo; return nLineNo;
} }
static int32_t CallbackPC(struct sSymbol const *self) static int32_t CallbackPC(void)
{ {
return self->pSection ? self->pSection->nOrg + curOffset : 0; struct Section const *section = sect_GetSymbolSection();
return section ? section->nOrg + curOffset : 0;
} }
/* /*
* Get the nValue field of a symbol * Get the value field of a symbol
*/ */
int32_t sym_GetValue(struct sSymbol const *sym) int32_t sym_GetValue(struct Symbol const *sym)
{ {
if (sym->Callback) if (sym_IsNumeric(sym) && sym->callback)
return sym->Callback(sym); return sym->callback();
if (sym->type == SYM_LABEL) if (sym->type == SYM_LABEL)
return sym->nValue + sym->pSection->nOrg; /* TODO: do not use section's org directly */
return sym->value + sym_GetSection(sym)->nOrg;
return sym->nValue; return sym->value;
} }
/* /*
* Update a symbol's definition filename and line * Update a symbol's definition filename and line
*/ */
static void updateSymbolFilename(struct sSymbol *nsym) static void updateSymbolFilename(struct Symbol *sym)
{ {
if (snprintf(nsym->tzFileName, _MAX_PATH + 1, "%s", if (snprintf(sym->fileName, _MAX_PATH + 1, "%s",
tzCurrentFileName) > _MAX_PATH) { tzCurrentFileName) > _MAX_PATH)
fatalerror("%s: File name is too long: '%s'", __func__, fatalerror("%s: File name is too long: '%s'", __func__,
tzCurrentFileName); tzCurrentFileName);
} sym->fileLine = fstk_GetLine();
nsym->nFileLine = fstk_GetLine();
} }
/* /*
* Create a new symbol by name * Create a new symbol by name
*/ */
static struct sSymbol *createsymbol(char const *s) static struct Symbol *createsymbol(char const *s)
{ {
struct sSymbol *symbol = malloc(sizeof(*symbol)); struct Symbol *symbol = malloc(sizeof(*symbol));
if (!symbol) if (!symbol)
fatalerror("Failed to create symbol: %s", strerror(errno)); fatalerror("Failed to create symbol: %s", strerror(errno));
if (snprintf(symbol->tzName, MAXSYMLEN + 1, "%s", s) > MAXSYMLEN) if (snprintf(symbol->name, MAXSYMLEN + 1, "%s", s) > MAXSYMLEN)
warning(WARNING_LONG_STR, "Symbol name is too long: '%s'", s); warning(WARNING_LONG_STR, "Symbol name is too long: '%s'", s);
hash_AddElement(symbols, symbol->tzName, symbol);
symbol->isExported = false; symbol->isExported = false;
symbol->isBuiltin = false; symbol->isBuiltin = false;
symbol->pScope = NULL; symbol->scope = NULL;
symbol->pSection = NULL; symbol->section = NULL;
symbol->nValue = 0; /* TODO: is this necessary? */ updateSymbolFilename(symbol);
symbol->pMacro = NULL;
symbol->Callback = NULL;
symbol->ID = -1; symbol->ID = -1;
symbol->next = NULL; symbol->next = NULL;
updateSymbolFilename(symbol);
hash_AddElement(symbols, symbol->name, symbol);
return symbol; return symbol;
} }
@@ -149,20 +148,20 @@ static struct sSymbol *createsymbol(char const *s)
* the name with the parent symbol's name. * the name with the parent symbol's name.
*/ */
static void fullSymbolName(char *output, size_t outputSize, static void fullSymbolName(char *output, size_t outputSize,
char const *localName, const struct sSymbol *scope) char const *localName, const struct Symbol *scope)
{ {
const struct sSymbol *parent = scope->pScope ? scope->pScope : scope; const struct Symbol *parent = scope->scope ? scope->scope : scope;
int n = snprintf(output, outputSize, "%s%s", parent->tzName, localName); int n = snprintf(output, outputSize, "%s%s", parent->name, localName);
if (n >= (int)outputSize) if (n >= (int)outputSize)
fatalerror("Symbol name is too long: '%s%s'", fatalerror("Symbol name is too long: '%s%s'", parent->name,
parent->tzName, localName); localName);
} }
/* /*
* Find a symbol by name and scope * Find a symbol by name and scope
*/ */
static struct sSymbol *findsymbol(char const *s, struct sSymbol const *scope) static struct Symbol *findsymbol(char const *s, struct Symbol const *scope)
{ {
char fullname[MAXSYMLEN + 1]; char fullname[MAXSYMLEN + 1];
@@ -183,19 +182,12 @@ static struct sSymbol *findsymbol(char const *s, struct sSymbol const *scope)
/* /*
* Find a symbol by name, with automatically determined scope * Find a symbol by name, with automatically determined scope
*/ */
struct sSymbol *sym_FindSymbol(char const *tzName) struct Symbol *sym_FindSymbol(char const *symName)
{ {
struct sSymbol *pscope; return findsymbol(symName, symName[0] == '.' ? symbolScope : NULL);
if (*tzName == '.')
pscope = pScope;
else
pscope = NULL;
return findsymbol(tzName, pscope);
} }
static inline bool isReferenced(struct sSymbol const *sym) static inline bool isReferenced(struct Symbol const *sym)
{ {
return sym->ID != -1; return sym->ID != -1;
} }
@@ -203,48 +195,54 @@ static inline bool isReferenced(struct sSymbol const *sym)
/* /*
* Purge a symbol * Purge a symbol
*/ */
void sym_Purge(char const *tzName) void sym_Purge(char const *symName)
{ {
struct sSymbol *scope = tzName[0] == '.' ? pScope : NULL; struct Symbol *scope = symName[0] == '.' ? symbolScope : NULL;
struct sSymbol *symbol = findsymbol(tzName, scope); struct Symbol *symbol = findsymbol(symName, scope);
if (!symbol) { if (!symbol) {
yyerror("'%s' not defined", tzName); yyerror("'%s' not defined", symName);
} else if (symbol->isBuiltin) { } else if (symbol->isBuiltin) {
yyerror("Built-in symbol '%s' cannot be purged", tzName); yyerror("Built-in symbol '%s' cannot be purged", symName);
} else if (isReferenced(symbol)) { } else if (isReferenced(symbol)) {
yyerror("Symbol \"%s\" is referenced and thus cannot be purged", yyerror("Symbol \"%s\" is referenced and thus cannot be purged",
tzName); symName);
} else { } else {
hash_RemoveElement(symbols, tzName); hash_RemoveElement(symbols, symName);
free(symbol->pMacro); if (symbol->type == SYM_MACRO)
free(symbol->macro);
free(symbol); free(symbol);
} }
} }
uint32_t sym_GetPCValue(void)
{
struct Section const *sect = sect_GetSymbolSection();
if (!sect)
yyerror("PC has no value outside a section");
else if (sect->nOrg == -1)
yyerror("Expected constant PC but section is not fixed");
else
return CallbackPC();
return 0;
}
/* /*
* Return a constant symbols value * Return a constant symbols value
*/ */
uint32_t sym_GetConstantValue(char const *s) uint32_t sym_GetConstantValue(char const *s)
{ {
struct sSymbol const *psym = sym_FindSymbol(s); struct Symbol const *sym = sym_FindSymbol(s);
if (psym == pPCSymbol) { if (sym == NULL)
if (!pCurrentSection)
yyerror("PC has no value outside a section");
else if (pCurrentSection->nOrg == -1)
yyerror("Expected constant PC but section is not fixed");
else
return sym_GetValue(psym);
} else if (psym != NULL) {
if (sym_IsConstant(psym))
return sym_GetValue(psym);
yyerror("\"%s\" does not have a constant value", s);
} else {
yyerror("'%s' not defined", s); yyerror("'%s' not defined", s);
} else if (sym == PCSymbol)
return sym_GetPCValue();
else if (!sym_IsConstant(sym))
yyerror("\"%s\" does not have a constant value", s);
else
return sym_GetValue(sym);
return 0; return 0;
} }
@@ -254,38 +252,26 @@ uint32_t sym_GetConstantValue(char const *s)
*/ */
uint32_t sym_GetDefinedValue(char const *s) uint32_t sym_GetDefinedValue(char const *s)
{ {
struct sSymbol const *psym = sym_FindSymbol(s); struct Symbol const *sym = sym_FindSymbol(s);
if (psym != NULL) { if (sym == NULL || !sym_IsDefined(sym))
if (sym_IsDefined(psym)) { yyerror("'%s' not defined", s);
if (!sym_IsNumeric(psym)) else if (!sym_IsNumeric(sym))
yyerror("'%s' is a macro or string symbol", s); yyerror("'%s' is a macro or string symbol", s);
else
return sym_GetValue(psym); return sym_GetValue(sym);
}
}
yyerror("'%s' not defined", s);
return 0; return 0;
} }
struct sSymbol *sym_GetCurrentSymbolScope(void) struct Symbol *sym_GetCurrentSymbolScope(void)
{ {
return pScope; return symbolScope;
} }
void sym_SetCurrentSymbolScope(struct sSymbol *pNewScope) void sym_SetCurrentSymbolScope(struct Symbol *newScope)
{ {
pScope = pNewScope; symbolScope = newScope;
}
/*
* Find a macro by name
*/
struct sSymbol *sym_FindMacro(char const *s)
{
return findsymbol(s, NULL);
} }
/* /*
@@ -293,15 +279,15 @@ struct sSymbol *sym_FindMacro(char const *s)
* 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
*/ */
static struct sSymbol *createNonrelocSymbol(char const *symbolName) static struct Symbol *createNonrelocSymbol(char const *symbolName)
{ {
struct sSymbol *symbol = findsymbol(symbolName, NULL); struct Symbol *symbol = findsymbol(symbolName, NULL);
if (!symbol) if (!symbol)
symbol = createsymbol(symbolName); symbol = createsymbol(symbolName);
else if (sym_IsDefined(symbol)) else if (sym_IsDefined(symbol))
yyerror("'%s' already defined at %s(%u)", symbolName, yyerror("'%s' already defined at %s(%u)", symbolName,
symbol->tzFileName, symbol->nFileLine); symbol->fileName, symbol->fileLine);
return symbol; return symbol;
} }
@@ -309,16 +295,15 @@ static struct sSymbol *createNonrelocSymbol(char const *symbolName)
/* /*
* Add an equated symbol * Add an equated symbol
*/ */
struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value) struct Symbol *sym_AddEqu(char const *symName, int32_t value)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct Symbol *sym = createNonrelocSymbol(symName);
nsym->nValue = value; sym->type = SYM_EQU;
nsym->type = SYM_EQU; sym->callback = NULL;
nsym->pScope = NULL; sym->value = value;
updateSymbolFilename(nsym);
return nsym; return sym;
} }
/* /*
@@ -333,190 +318,175 @@ struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value)
* of the string are enough: sym_AddString("M_PI", "3.1415"). This is the same * of the string are enough: sym_AddString("M_PI", "3.1415"). This is the same
* as ``` M_PI EQUS "3.1415" ``` * as ``` M_PI EQUS "3.1415" ```
*/ */
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue) struct Symbol *sym_AddString(char const *symName, char const *value)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct Symbol *sym = createNonrelocSymbol(symName);
size_t len = strlen(value);
char *string = malloc(len + 1);
nsym->pMacro = malloc(strlen(tzValue) + 1); if (string == NULL)
if (nsym->pMacro != NULL)
strcpy(nsym->pMacro, tzValue);
else
fatalerror("No memory for string equate"); fatalerror("No memory for string equate");
strcpy(string, value);
nsym->type = SYM_EQUS; sym->type = SYM_EQUS;
nsym->ulMacroSize = strlen(tzValue); /* TODO: use other fields */
nsym->pScope = NULL; sym->macroSize = len;
sym->macro = string;
return nsym; return sym;
} }
/* /*
* Alter a SET symbols value * Alter a SET symbols value
*/ */
struct sSymbol *sym_AddSet(char const *tzSym, int32_t value) struct Symbol *sym_AddSet(char const *symName, int32_t value)
{ {
struct sSymbol *nsym = findsymbol(tzSym, NULL); struct Symbol *sym = findsymbol(symName, NULL);
if (nsym != NULL) { if (sym == NULL)
if (sym_IsDefined(nsym)) { sym = createsymbol(symName);
if (nsym->type == SYM_LABEL) else if (sym_IsDefined(sym) && sym->type != SYM_SET)
yyerror("'%s' already defined as non-constant at %s(%u)", yyerror("'%s' already defined as %s at %s(%u)", symName,
tzSym, sym->type == SYM_LABEL ? "label" : "constant",
nsym->tzFileName, sym->fileName, sym->fileLine);
nsym->nFileLine); else
else if (nsym->type != SYM_SET) /* TODO: can the scope be incorrect when talking over refs? */
yyerror("'%s' already defined as constant at %s(%u)", updateSymbolFilename(sym);
tzSym,
nsym->tzFileName,
nsym->nFileLine);
} else if (nsym->type == SYM_REF) {
yyerror("'%s' already referenced at %s(%u)",
tzSym,
nsym->tzFileName,
nsym->nFileLine);
}
} else {
nsym = createsymbol(tzSym);
}
nsym->nValue = value; sym->type = SYM_SET;
nsym->type = SYM_SET; sym->callback = NULL;
nsym->pScope = NULL; sym->value = value;
updateSymbolFilename(nsym);
return nsym; return sym;
} }
/* /*
* Add a local (.name) relocatable symbol * Add a local (.name) relocatable symbol
*/ */
struct sSymbol *sym_AddLocalReloc(char const *tzSym) struct Symbol *sym_AddLocalReloc(char const *symName)
{ {
if (pScope) { if (!symbolScope) {
char fullname[MAXSYMLEN + 1]; yyerror("Local label '%s' in main scope", symName);
return NULL;
fullSymbolName(fullname, sizeof(fullname), tzSym, pScope);
return sym_AddReloc(fullname);
} }
yyerror("Local label '%s' in main scope", tzSym); char fullname[MAXSYMLEN + 1];
return NULL;
fullSymbolName(fullname, sizeof(fullname), symName, symbolScope);
return sym_AddReloc(fullname);
} }
/* /*
* Add a relocatable symbol * Add a relocatable symbol
*/ */
struct sSymbol *sym_AddReloc(char const *tzSym) struct Symbol *sym_AddReloc(char const *symName)
{ {
struct sSymbol *scope = NULL; struct Symbol const *scope = NULL;
struct sSymbol *nsym; char *localPtr = strchr(symName, '.');
char *localPtr = strchr(tzSym, '.');
if (localPtr != NULL) { if (localPtr != NULL) {
if (!pScope) { if (!symbolScope) {
yyerror("Local label in main scope"); yyerror("Local label in main scope");
return NULL; return NULL;
} }
struct sSymbol *parent = pScope->pScope ? scope = symbolScope->scope ? symbolScope->scope : symbolScope;
pScope->pScope : pScope; uint32_t parentLen = localPtr - symName;
uint32_t parentLen = localPtr - tzSym;
if (strchr(localPtr + 1, '.') != NULL) if (strchr(localPtr + 1, '.') != NULL)
fatalerror("'%s' is a nonsensical reference to a nested local symbol", fatalerror("'%s' is a nonsensical reference to a nested local symbol",
tzSym); symName);
else if (strlen(parent->tzName) != parentLen else if (strlen(scope->name) != parentLen
|| strncmp(tzSym, parent->tzName, parentLen) != 0) || strncmp(symName, scope->name, parentLen) != 0)
yyerror("Not currently in the scope of '%.*s'", yyerror("Not currently in the scope of '%.*s'",
parentLen, tzSym); parentLen, symName);
scope = parent;
} }
nsym = findsymbol(tzSym, scope); struct Symbol *sym = findsymbol(symName, scope);
if (!nsym) if (!sym)
nsym = createsymbol(tzSym); sym = createsymbol(symName);
else if (sym_IsDefined(nsym)) else if (sym_IsDefined(sym))
yyerror("'%s' already defined in %s(%d)", tzSym, yyerror("'%s' already defined in %s(%d)", symName,
nsym->tzFileName, nsym->nFileLine); sym->fileName, sym->fileLine);
/* If the symbol already exists as a ref, just "take over" it */ /* If the symbol already exists as a ref, just "take over" it */
nsym->nValue = curOffset; sym->type = SYM_LABEL;
nsym->type = SYM_LABEL; sym->callback = NULL;
sym->value = curOffset;
if (exportall) if (exportall)
nsym->isExported = true; sym->isExported = true;
nsym->pScope = scope; sym->scope = scope;
nsym->pSection = sect_GetSymbolSection(); sym->section = sect_GetSymbolSection();
/* Labels need to be assigned a section, except PC */ /* Labels need to be assigned a section, except PC */
if (!pCurrentSection && strcmp(tzSym, "@")) if (!sym->section && strcmp(symName, "@"))
yyerror("Label \"%s\" created outside of a SECTION", yyerror("Label \"%s\" created outside of a SECTION",
tzSym); symName);
updateSymbolFilename(nsym); updateSymbolFilename(sym);
pScope = findsymbol(tzSym, scope); /* Set the symbol as the new scope */
return pScope; /* TODO: don't do this for local labels */
symbolScope = findsymbol(symName, scope);
return symbolScope;
} }
/* /*
* Export a symbol * Export a symbol
*/ */
void sym_Export(char const *tzSym) void sym_Export(char const *symName)
{ {
struct sSymbol *nsym = sym_FindSymbol(tzSym); struct Symbol *sym = sym_FindSymbol(symName);
/* If the symbol doesn't exist, create a ref that can be purged */ /* If the symbol doesn't exist, create a ref that can be purged */
if (!nsym) if (!sym)
nsym = sym_Ref(tzSym); sym = sym_Ref(symName);
nsym->isExported = true; sym->isExported = true;
} }
/* /*
* Add a macro definition * Add a macro definition
*/ */
struct sSymbol *sym_AddMacro(char const *tzSym, int32_t nDefLineNo) struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct Symbol *sym = createNonrelocSymbol(symName);
nsym->type = SYM_MACRO; sym->type = SYM_MACRO;
nsym->pScope = NULL; sym->macroSize = ulNewMacroSize;
nsym->ulMacroSize = ulNewMacroSize; sym->macro = tzNewMacro;
nsym->pMacro = tzNewMacro; updateSymbolFilename(sym);
updateSymbolFilename(nsym);
/* /*
* The symbol is created at the line after the `endm`, * The symbol is created at the line after the `endm`,
* override this with the actual definition line * override this with the actual definition line
*/ */
nsym->nFileLine = nDefLineNo; sym->fileLine = defLineNo;
return nsym; return sym;
} }
/* /*
* Flag that a symbol is referenced in an RPN expression * Flag that a symbol is referenced in an RPN expression
* and create it if it doesn't exist yet * and create it if it doesn't exist yet
*/ */
struct sSymbol *sym_Ref(char const *tzSym) struct Symbol *sym_Ref(char const *symName)
{ {
struct sSymbol *nsym = sym_FindSymbol(tzSym); struct Symbol *nsym = sym_FindSymbol(symName);
if (nsym == NULL) { if (nsym == NULL) {
char fullname[MAXSYMLEN + 1]; char fullname[MAXSYMLEN + 1];
if (*tzSym == '.') { if (*symName == '.') {
if (!pScope) if (!symbolScope)
fatalerror("Local label reference '%s' in main scope", fatalerror("Local label reference '%s' in main scope",
tzSym); symName);
fullSymbolName(fullname, sizeof(fullname), tzSym, fullSymbolName(fullname, sizeof(fullname), symName,
pScope); symbolScope);
tzSym = fullname; symName = fullname;
} }
nsym = createsymbol(tzSym); nsym = createsymbol(symName);
nsym->type = SYM_REF; nsym->type = SYM_REF;
} }
@@ -547,22 +517,21 @@ static inline char const *removeLeadingZeros(char const *ptr)
*/ */
void sym_Init(void) void sym_Init(void)
{ {
pPCSymbol = sym_AddReloc("@"); struct Symbol *_NARGSymbol = sym_AddEqu("_NARG", 0);
pPCSymbol->Callback = CallbackPC; struct Symbol *__LINE__Symbol = sym_AddEqu("__LINE__", 0);
pPCSymbol->isBuiltin = true;
p_NARGSymbol = sym_AddEqu("_NARG", 0);
p_NARGSymbol->Callback = Callback_NARG;
p_NARGSymbol->isBuiltin = true;
p__LINE__Symbol = sym_AddEqu("__LINE__", 0);
p__LINE__Symbol->Callback = Callback__LINE__;
p__LINE__Symbol->isBuiltin = true;
struct sSymbol *_RSSymbol = sym_AddSet("_RS", 0);
_RSSymbol->isBuiltin = true; PCSymbol = sym_AddReloc("@"),
PCSymbol->isBuiltin = true;
PCSymbol->callback = CallbackPC;
_NARGSymbol->isBuiltin = true;
_NARGSymbol->callback = Callback_NARG;
__LINE__Symbol->isBuiltin = true;
__LINE__Symbol->callback = Callback__LINE__;
sym_AddSet("_RS", 0)->isBuiltin = true;
sym_AddEqu("__RGBDS_MAJOR__", PACKAGE_VERSION_MAJOR); sym_AddEqu("__RGBDS_MAJOR__", PACKAGE_VERSION_MAJOR)->isBuiltin = true;
sym_AddEqu("__RGBDS_MINOR__", PACKAGE_VERSION_MINOR); sym_AddEqu("__RGBDS_MINOR__", PACKAGE_VERSION_MINOR)->isBuiltin = true;
sym_AddEqu("__RGBDS_PATCH__", PACKAGE_VERSION_PATCH); sym_AddEqu("__RGBDS_PATCH__", PACKAGE_VERSION_PATCH)->isBuiltin = true;
time_t now = time(NULL); time_t now = time(NULL);
@@ -575,43 +544,38 @@ void sym_Init(void)
const struct tm *time_utc = gmtime(&now); const struct tm *time_utc = gmtime(&now);
const struct tm *time_local = localtime(&now); const struct tm *time_local = localtime(&now);
strftime(SavedTIME, sizeof(SavedTIME), "\"%H:%M:%S\"", strftime(savedTIME, sizeof(savedTIME), "\"%H:%M:%S\"", time_local);
time_local); strftime(savedDATE, sizeof(savedDATE), "\"%d %B %Y\"", time_local);
strftime(SavedDATE, sizeof(SavedDATE), "\"%d %B %Y\"", strftime(savedTIMESTAMP_ISO8601_LOCAL,
time_local); sizeof(savedTIMESTAMP_ISO8601_LOCAL), "\"%Y-%m-%dT%H-%M-%S%z\"",
strftime(SavedTIMESTAMP_ISO8601_LOCAL,
sizeof(SavedTIMESTAMP_ISO8601_LOCAL), "\"%Y-%m-%dT%H-%M-%S%z\"",
time_local); time_local);
strftime(SavedTIMESTAMP_ISO8601_UTC, strftime(savedTIMESTAMP_ISO8601_UTC,
sizeof(SavedTIMESTAMP_ISO8601_UTC), "\"%Y-%m-%dT%H-%M-%SZ\"", sizeof(savedTIMESTAMP_ISO8601_UTC), "\"%Y-%m-%dT%H-%M-%SZ\"",
time_utc); time_utc);
strftime(SavedYEAR, sizeof(SavedYEAR), "%Y", time_utc); strftime(savedYEAR, sizeof(savedYEAR), "%Y", time_utc);
strftime(SavedMONTH, sizeof(SavedMONTH), "%m", time_utc); strftime(savedMONTH, sizeof(savedMONTH), "%m", time_utc);
strftime(SavedDAY, sizeof(SavedDAY), "%d", time_utc); strftime(savedDAY, sizeof(savedDAY), "%d", time_utc);
strftime(SavedHOUR, sizeof(SavedHOUR), "%H", time_utc); strftime(savedHOUR, sizeof(savedHOUR), "%H", time_utc);
strftime(SavedMINUTE, sizeof(SavedMINUTE), "%M", time_utc); strftime(savedMINUTE, sizeof(savedMINUTE), "%M", time_utc);
strftime(SavedSECOND, sizeof(SavedSECOND), "%S", time_utc); strftime(savedSECOND, sizeof(savedSECOND), "%S", time_utc);
#define addString(name, val) do { \ #define addString(name, val) sym_AddString(name, val)->isBuiltin = true
struct sSymbol *symbol = sym_AddString(name, val); \ addString("__TIME__", savedTIME);
symbol->isBuiltin = true; \ addString("__DATE__", savedDATE);
} while (0) addString("__ISO_8601_LOCAL__", savedTIMESTAMP_ISO8601_LOCAL);
addString("__TIME__", SavedTIME); addString("__ISO_8601_UTC__", savedTIMESTAMP_ISO8601_UTC);
addString("__DATE__", SavedDATE);
addString("__ISO_8601_LOCAL__", SavedTIMESTAMP_ISO8601_LOCAL);
addString("__ISO_8601_UTC__", SavedTIMESTAMP_ISO8601_UTC);
/* This cannot start with zeros */ /* This cannot start with zeros */
addString("__UTC_YEAR__", SavedYEAR); addString("__UTC_YEAR__", savedYEAR);
addString("__UTC_MONTH__", removeLeadingZeros(SavedMONTH)); addString("__UTC_MONTH__", removeLeadingZeros(savedMONTH));
addString("__UTC_DAY__", removeLeadingZeros(SavedDAY)); addString("__UTC_DAY__", removeLeadingZeros(savedDAY));
addString("__UTC_HOUR__", removeLeadingZeros(SavedHOUR)); addString("__UTC_HOUR__", removeLeadingZeros(savedHOUR));
addString("__UTC_MINUTE__", removeLeadingZeros(SavedMINUTE)); addString("__UTC_MINUTE__", removeLeadingZeros(savedMINUTE));
addString("__UTC_SECOND__", removeLeadingZeros(SavedSECOND)); addString("__UTC_SECOND__", removeLeadingZeros(savedSECOND));
#undef addString #undef addString
pScope = NULL; symbolScope = NULL;
math_DefinePI(); math_DefinePI();
} }

View File

@@ -1,4 +1,5 @@
ERROR: macro-@.asm(1): ERROR: macro-@.asm(1):
Label "foo" created outside of a SECTION Label "foo" created outside of a SECTION
ERROR: macro-@.asm(1): ERROR: macro-@.asm(1):
Macro '@' not defined "@" is not a macro
error: Assembly aborted (2 errors)!

View File

@@ -1,4 +1,4 @@
SECTION "sec", ROM0 SECTION "sec", ROM0[0]
db X db X
X = 2 X = 2

View File

@@ -1,3 +0,0 @@
ERROR: reference-undefined-sym.asm(4):
'X' already referenced at reference-undefined-sym.asm(2)
error: Assembly aborted (1 errors)!

View File

View File

@@ -3,5 +3,5 @@ ERROR: symbol-override.asm(6):
ERROR: symbol-override.asm(10): ERROR: symbol-override.asm(10):
'X' already defined as constant at symbol-override.asm(9) 'X' already defined as constant at symbol-override.asm(9)
ERROR: symbol-override.asm(14): ERROR: symbol-override.asm(14):
'Y' already defined as non-constant at symbol-override.asm(13) 'Y' already defined as label at symbol-override.asm(13)
error: Assembly aborted (3 errors)! error: Assembly aborted (3 errors)!