Skip to content

Commit

Permalink
Clean up symbol system
Browse files Browse the repository at this point in the history
Get rid of Hungarian notation
Improve encapsulation (the rest of the world should not touch PC directly)
  • Loading branch information
ISSOtm committed Apr 9, 2020
1 parent 5ea8490 commit f9f27d6
Show file tree
Hide file tree
Showing 17 changed files with 360 additions and 398 deletions.
1 change: 0 additions & 1 deletion include/asm/asm.h
Expand Up @@ -34,7 +34,6 @@ extern uint32_t unionStart[MAXUNIONS];
extern uint32_t unionSize[MAXUNIONS];
extern char tzCurrentFileName[_MAX_PATH + 1];
extern struct Section *pCurrentSection;
extern struct sSymbol *pPCSymbol;
extern bool oDontExpandStrings;

size_t symvaluetostring(char *dest, size_t maxLength, char *sym,
Expand Down
4 changes: 2 additions & 2 deletions include/asm/fstack.h
Expand Up @@ -25,7 +25,7 @@ struct MacroArgs;

struct sContext {
YY_BUFFER_STATE FlexHandle;
struct sSymbol *pMacro;
struct Symbol const *pMacro;
struct sContext *pNext;
char tzFileName[_MAX_PATH + 1];
struct MacroArgs *macroArgs;
Expand All @@ -48,7 +48,7 @@ void fstk_Dump(void);
void fstk_DumpToStr(char *buf, size_t len);
void fstk_DumpStringExpansions(void);
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);
FILE *fstk_FindFile(char const *fname, char **incPathUsed);
int32_t fstk_GetLine(void);
Expand Down
2 changes: 1 addition & 1 deletion include/asm/lexer.h
Expand Up @@ -56,7 +56,7 @@ void setup_lexer(void);

void yy_set_state(enum eLexerState i);
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_switch_to_buffer(YY_BUFFER_STATE buf);
uint32_t lex_FloatAlloc(const struct sLexFloat *tok);
Expand Down
96 changes: 56 additions & 40 deletions include/asm/symbol.h
Expand Up @@ -29,87 +29,103 @@ enum SymbolType {
SYM_REF // Forward reference to a label
};

struct sSymbol {
char tzName[MAXSYMLEN + 1];
struct Symbol {
char name[MAXSYMLEN + 1];
enum SymbolType type;
bool isExported; /* Whether the symbol is to be exported */
bool isBuiltin; /* Whether the symbol is a built-in */
struct sSymbol *pScope;
struct Section *pSection;
int32_t nValue;
uint32_t ulMacroSize;
char *pMacro;
int32_t (*Callback)(struct sSymbol const *self);
char tzFileName[_MAX_PATH + 1]; /* File where the symbol was defined. */
uint32_t nFileLine; /* Line where the symbol was defined. */
struct Symbol const *scope;
struct Section *section;
char fileName[_MAX_PATH + 1]; /* File where the symbol was defined. */
uint32_t fileLine; /* Line where the symbol was defined. */

union {
struct { /* If sym_IsNumeric */
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) */
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;
}

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
|| (sym->type == SYM_LABEL && sym->pSection
&& sym->pSection->nOrg != -1);
return sym_IsPC(sym) ? sect_GetSymbolSection() : sym->section;
}

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
|| 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;
}

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;
}

/*
* 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);
struct sSymbol *sym_AddLocalReloc(char const *tzSym);
struct sSymbol *sym_AddReloc(char const *tzSym);
void sym_Export(char const *tzSym);
struct sSymbol *sym_FindMacro(char const *s);
struct sSymbol *sym_AddEqu(char const *tzSym, int32_t value);
struct sSymbol *sym_AddSet(char const *tzSym, int32_t value);
void sym_Init(void);
struct Symbol *sym_AddLocalReloc(char const *symName);
struct Symbol *sym_AddReloc(char const *symName);
void sym_Export(char const *symName);
struct Symbol *sym_AddEqu(char const *symName, int32_t value);
struct Symbol *sym_AddSet(char const *symName, int32_t value);
uint32_t sym_GetPCValue(void);
uint32_t sym_GetConstantValue(char const *s);
struct sSymbol *sym_FindSymbol(char const *tzName);
char *sym_GetStringValue(struct sSymbol const *sym);
struct sSymbol *sym_AddMacro(char const *tzSym, int32_t nDefLineNo);
struct sSymbol *sym_Ref(char const *tzSym);
struct sSymbol *sym_AddString(char const *tzSym, char const *tzValue);
struct Symbol *sym_FindSymbol(char const *symName);
struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo);
struct Symbol *sym_Ref(char const *symName);
struct Symbol *sym_AddString(char const *symName, char const *value);
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. */
struct sSymbol *sym_GetCurrentSymbolScope(void);
void sym_SetCurrentSymbolScope(struct sSymbol *pNewScope);
struct Symbol *sym_GetCurrentSymbolScope(void);
void sym_SetCurrentSymbolScope(struct Symbol *newScope);

#endif /* RGBDS_SYMBOL_H */
7 changes: 3 additions & 4 deletions src/asm/asmy.y
Expand Up @@ -43,7 +43,7 @@ size_t symvaluetostring(char *dest, size_t maxLength, char *symName,
const char *mode)
{
size_t length;
struct sSymbol *sym = sym_FindSymbol(symName);
struct Symbol *sym = sym_FindSymbol(symName);

if (sym && sym->type == SYM_EQUS) {
char const *src = sym_GetStringValue(sym);
Expand Down Expand Up @@ -710,8 +710,7 @@ macro : T_ID {
yy_set_state(LEX_STATE_MACROARGS);
} macroargs {
yy_set_state(LEX_STATE_NORMAL);
if (!fstk_RunMacro($1, $3))
fatalerror("Macro '%s' not defined", $1);
fstk_RunMacro($1, $3);
}
;

Expand Down Expand Up @@ -1334,7 +1333,7 @@ relocexpr_no_str : scoped_id { rpn_Symbol(&$$, $1); }
| T_OP_DEF {
oDontExpandStrings = true;
} '(' scoped_id ')' {
struct sSymbol const *sym = sym_FindSymbol($4);
struct Symbol const *sym = sym_FindSymbol($4);

rpn_Number(&$$, !!sym);

Expand Down
46 changes: 16 additions & 30 deletions src/asm/fstack.c
Expand Up @@ -34,7 +34,7 @@
static struct sContext *pFileStack;
static unsigned int nFileStackDepth;
unsigned int nMaxRecursionDepth;
static struct sSymbol *pCurrentMacro;
static struct Symbol const *pCurrentMacro;
static YY_BUFFER_STATE CurrentFlexHandle;
static FILE *pCurrentFile;
static uint32_t nCurrentStatus;
Expand Down Expand Up @@ -449,52 +449,38 @@ void fstk_RunInclude(char *tzFileName)
/*
* 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;

if (sym == NULL || sym->pMacro == NULL)
return false;
if (sym == NULL) {
yyerror("Macro \"%s\" not defined", s);
return;
}
if (sym->type != SYM_MACRO) {
yyerror("\"%s\" is not a macro", s);
return;
}

pushcontext();
macro_SetUniqueID(nMacroCount++);
/* Minus 1 because there is a newline at the beginning of the buffer */
nLineNo = sym->nFileLine - 1;
nLineNo = sym->fileLine - 1;
macro_UseNewArgs(args);
nCurrentStatus = STAT_isMacro;
nPrintedChars = snprintf(tzCurrentFileName, _MAX_PATH + 1,
"%s::%s", sym->tzFileName, s);
"%s::%s", sym->fileName, s);
if (nPrintedChars > _MAX_PATH) {
popcontext();
fatalerror("File name + macro name is too large to fit into buffer");
}

pCurrentMacro = sym;
CurrentFlexHandle = yy_scan_bytes(pCurrentMacro->pMacro,
strlen(pCurrentMacro->pMacro));
/* TODO: why is `strlen` being used when there's a macro size field? */
CurrentFlexHandle = yy_scan_bytes(pCurrentMacro->macro,
strlen(pCurrentMacro->macro));
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);
}
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/asm/globlex.c
Expand Up @@ -277,10 +277,10 @@ uint32_t ParseSymbol(char *src, uint32_t size)

/* If the symbol is an EQUS, expand it */
if (!oDontExpandStrings) {
struct sSymbol const *sym = sym_FindSymbol(dest);
struct Symbol const *sym = sym_FindSymbol(dest);

if (sym && sym->type == SYM_EQUS) {
char *s;
char const *s;

lex_BeginStringExpansion(dest);

Expand Down
2 changes: 1 addition & 1 deletion src/asm/lexer.c
Expand Up @@ -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));

Expand Down
32 changes: 16 additions & 16 deletions src/asm/output.c
Expand Up @@ -55,8 +55,8 @@ char *tzObjectname;
struct Section *pSectionList, *pCurrentSection;

/* Linked list of symbols to put in the object file */
static struct sSymbol *objectSymbols = NULL;
static struct sSymbol **objectSymbolsTail = &objectSymbols;
static struct Symbol *objectSymbols = NULL;
static struct Symbol **objectSymbolsTail = &objectSymbols;
static uint32_t nbSymbols = 0; /* Length of the above list */

static struct Assertion *assertions = NULL;
Expand Down Expand Up @@ -196,25 +196,25 @@ static void writesection(struct Section const *pSect, FILE *f)
/*
* 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);
if (!sym_IsDefined(pSym)) {
fputstring(sym->name, f);
if (!sym_IsDefined(sym)) {
fputc(SYMTYPE_IMPORT, f);
} else {
fputc(pSym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f);
fputstring(pSym->tzFileName, f);
fputlong(pSym->nFileLine, f);
fputlong(getSectIDIfAny(pSym->pSection), f);
fputlong(pSym->nValue, f);
fputc(sym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f);
fputstring(sym->fileName, f);
fputlong(sym->fileLine, f);
fputlong(getSectIDIfAny(sym_GetSection(sym)), f);
fputlong(sym->value, f);
}
}

/*
* Returns a symbol's ID within the object file
* 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) {
sym->ID = nbSymbols++;
Expand Down Expand Up @@ -247,7 +247,7 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
{
for (unsigned int i = -1; (tzSym[++i] = popbyte()); )
;
struct sSymbol *sym = sym_FindSymbol(tzSym);
struct Symbol *sym = sym_FindSymbol(tzSym);
uint32_t value;

if (sym_IsConstant(sym)) {
Expand All @@ -267,7 +267,7 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
{
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);

writebyte(RPN_BANK_SYM);
Expand Down Expand Up @@ -367,10 +367,10 @@ static void writeassert(struct Assertion *assert, FILE *f)
fputstring(assert->message, f);
}

static void registerExportedSymbol(struct sSymbol *symbol, void *arg)
static void registerExportedSymbol(struct Symbol *symbol, void *arg)
{
(void)arg;
if (symbol->isExported && symbol->ID == -1) {
if (sym_IsExported(symbol) && symbol->ID == -1) {
*objectSymbolsTail = symbol;
objectSymbolsTail = &symbol->next;
nbSymbols++;
Expand All @@ -396,7 +396,7 @@ void out_WriteObject(void)
fputlong(nbSymbols, 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);

for (struct Section *sect = pSectionList; sect; sect = sect->pNext)
Expand Down

0 comments on commit f9f27d6

Please sign in to comment.