Merge pull request #477 from ISSOtm/sym_overhaul

Overhaul the symbol system
This commit is contained in:
Eldred Habert
2020-01-26 14:30:47 +01:00
committed by GitHub
17 changed files with 207 additions and 251 deletions

View File

@@ -40,7 +40,6 @@ void out_AbsByteGroup(char *s, int32_t length);
void out_RelByte(struct Expression *expr); void out_RelByte(struct Expression *expr);
void out_RelWord(struct Expression *expr); void out_RelWord(struct Expression *expr);
void out_PCRelByte(struct Expression *expr); void out_PCRelByte(struct Expression *expr);
void out_CheckErrors(void);
void out_WriteObject(void); void out_WriteObject(void);
void out_Skip(int32_t skip); void out_Skip(int32_t skip);
void out_BinaryFile(char *s); void out_BinaryFile(char *s);

View File

@@ -9,20 +9,33 @@
#ifndef RGBDS_SYMBOL_H #ifndef RGBDS_SYMBOL_H
#define RGBDS_SYMBOL_H #define RGBDS_SYMBOL_H
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "types.h" #include "types.h"
#define HASHSIZE (1 << 16) #define HASHSIZE (1 << 16)
#define MAXSYMLEN 256 #define MAXSYMLEN 256
enum SymbolType {
SYM_LABEL,
SYM_EQU,
SYM_SET,
SYM_MACRO,
SYM_EQUS,
SYM_REF // Forward reference to a label
};
struct sSymbol { struct sSymbol {
char tzName[MAXSYMLEN + 1]; char tzName[MAXSYMLEN + 1];
int32_t nValue; enum SymbolType type;
uint32_t nType; bool isConstant; /* Whether the symbol's value is currently known */
bool isExported; /* Whether the symbol is to be exported */
struct sSymbol *pScope; struct sSymbol *pScope;
struct sSymbol *pNext; struct sSymbol *pNext;
struct Section *pSection; struct Section *pSection;
int32_t nValue;
uint32_t ulMacroSize; uint32_t ulMacroSize;
char *pMacro; char *pMacro;
int32_t (*Callback)(struct sSymbol *self); int32_t (*Callback)(struct sSymbol *self);
@@ -30,26 +43,49 @@ struct sSymbol {
uint32_t nFileLine; /* Line where the symbol was defined. */ uint32_t nFileLine; /* Line where the symbol was defined. */
}; };
/* Symbol will be relocated during linking, it's absolute value is unknown */ static inline bool sym_IsDefined(struct sSymbol const *sym)
{
return sym->type != SYM_REF;
}
static inline bool sym_IsConstant(struct sSymbol const *sym)
{
return sym->isConstant;
}
static inline bool sym_IsNumeric(struct sSymbol const *sym)
{
return sym->type == SYM_LABEL || sym->type == SYM_EQU
|| sym->type == SYM_SET;
}
static inline bool sym_IsLocal(struct sSymbol const *sym)
{
return (sym->type == SYM_LABEL || sym->type == SYM_REF)
&& strchr(sym->tzName, '.');
}
static inline bool sym_IsExported(struct sSymbol const *sym)
{
return sym->isExported;
}
/* Symbol will be relocated during linking, it's absolute value is unknown
#define SYMF_RELOC 0x001 #define SYMF_RELOC 0x001
/* Symbol is defined using EQU, will not be changed during linking */ Symbol is defined using EQU, will not be changed during linking
#define SYMF_EQU 0x002 #define SYMF_EQU 0x002
/* Symbol is (re)defined using SET, will not be changed during linking */ Symbol is (re)defined using SET, will not be changed during linking
#define SYMF_SET 0x004 #define SYMF_SET 0x004
/* Symbol should be exported */ Symbol should be exported
#define SYMF_EXPORT 0x008 #define SYMF_EXPORT 0x008
/* Symbol referenced in RPN expression */ Symbol referenced in RPN expression
#define SYMF_REF 0x010 #define SYMF_REF 0x010
/* Symbol is a local symbol */ Symbol is a local symbol
#define SYMF_LOCAL 0x020 #define SYMF_LOCAL 0x020
/* Symbol has been defined, not only referenced */ Symbol has been defined, not only referenced
#define SYMF_DEFINED 0x040 #define SYMF_DEFINED 0x040
/* Symbol is a macro */ Symbol is a macro
#define SYMF_MACRO 0x080 #define SYMF_MACRO 0x080
/* Symbol is a stringsymbol */ Symbol is a stringsymbol
#define SYMF_STRING 0x100 #define SYMF_STRING 0x100
/* Symbol has a constant value, will not be changed during linking */ Symbol has a constant value, will not be changed during linking
#define SYMF_CONST 0x200 #define SYMF_CONST 0x200
*/
uint32_t sym_CalcHash(const char *s); uint32_t sym_CalcHash(const char *s);
void sym_SetExportAll(uint8_t set); void sym_SetExportAll(uint8_t set);
@@ -67,22 +103,18 @@ void sym_AddEqu(char *tzSym, int32_t value);
void sym_AddSet(char *tzSym, int32_t value); void sym_AddSet(char *tzSym, int32_t value);
void sym_Init(void); void sym_Init(void);
uint32_t sym_GetConstantValue(char *s); uint32_t sym_GetConstantValue(char *s);
uint32_t sym_isConstant(char *s);
struct sSymbol *sym_FindSymbol(char *tzName); struct sSymbol *sym_FindSymbol(char *tzName);
char *sym_FindMacroArg(int32_t i); char *sym_FindMacroArg(int32_t i);
char *sym_GetStringValue(char *tzSym); char *sym_GetStringValue(struct sSymbol const *sym);
void sym_UseCurrentMacroArgs(void); void sym_UseCurrentMacroArgs(void);
void sym_SetMacroArgID(uint32_t nMacroCount); void sym_SetMacroArgID(uint32_t nMacroCount);
uint32_t sym_isString(char *tzSym);
void sym_AddMacro(char *tzSym, int32_t nDefLineNo); void sym_AddMacro(char *tzSym, int32_t nDefLineNo);
void sym_Ref(char *tzSym); void sym_Ref(char *tzSym);
void sym_ShiftCurrentMacroArgs(void); void sym_ShiftCurrentMacroArgs(void);
void sym_AddString(char *tzSym, char *tzValue); void sym_AddString(char *tzSym, char *tzValue);
uint32_t sym_GetDefinedValue(char *s); uint32_t sym_GetDefinedValue(char *s);
uint32_t sym_isDefined(char *tzName);
void sym_Purge(char *tzName); void sym_Purge(char *tzName);
uint32_t sym_isConstDefined(char *tzName); bool sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2);
int32_t sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2);
/* Functions to save and restore the current symbol scope. */ /* Functions to save and restore the current symbol scope. */
struct sSymbol *sym_GetCurrentSymbolScope(void); struct sSymbol *sym_GetCurrentSymbolScope(void);

View File

@@ -19,7 +19,7 @@
struct Symbol { struct Symbol {
/* Info contained in the object files */ /* Info contained in the object files */
char *name; char *name;
enum SymbolType type; enum ExportLevel type;
char const *objFileName; char const *objFileName;
char *fileName; char *fileName;
int32_t lineNo; int32_t lineNo;

View File

@@ -59,7 +59,7 @@ enum SectionType {
SECTTYPE_INVALID SECTTYPE_INVALID
}; };
enum SymbolType { enum ExportLevel {
SYMTYPE_LOCAL, SYMTYPE_LOCAL,
SYMTYPE_IMPORT, SYMTYPE_IMPORT,
SYMTYPE_EXPORT SYMTYPE_EXPORT

View File

@@ -78,13 +78,14 @@ static void bankrangecheck(char *name, uint32_t secttype, int32_t org,
out_NewAbsSection(name, secttype, org, bank); out_NewAbsSection(name, secttype, org, bank);
} }
size_t symvaluetostring(char *dest, size_t maxLength, char *sym, 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);
if (sym_isString(sym)) { if (sym && sym->type == SYM_EQUS) {
char *src = sym_GetStringValue(sym); char const *src = sym_GetStringValue(sym);
size_t i; size_t i;
if (mode) if (mode)
@@ -100,7 +101,7 @@ size_t symvaluetostring(char *dest, size_t maxLength, char *sym,
length = i; length = i;
} else { } else {
uint32_t value = sym_GetConstantValue(sym); uint32_t value = sym_GetConstantValue(symName);
int32_t fullLength; int32_t fullLength;
/* Special cheat for binary */ /* Special cheat for binary */
@@ -1307,7 +1308,11 @@ relocconst : T_ID
oDontExpandStrings = true; oDontExpandStrings = true;
} '(' T_ID ')' } '(' T_ID ')'
{ {
rpn_Number(&$$, sym_isConstDefined($4)); struct sSymbol const *sym = sym_FindSymbol($4);
if (sym && !(sym_IsDefined(sym) && sym->type != SYM_LABEL))
yyerror("Label \"%s\" is not a valid argument to DEF",
$4);
rpn_Number(&$$, !!sym);
oDontExpandStrings = false; oDontExpandStrings = false;
} }
| T_OP_ROUND '(' const ')' | T_OP_ROUND '(' const ')'
@@ -1447,7 +1452,11 @@ const : T_ID { constexpr_Symbol(&$$, $1); }
oDontExpandStrings = true; oDontExpandStrings = true;
} '(' T_ID ')' } '(' T_ID ')'
{ {
constexpr_Number(&$$, sym_isConstDefined($4)); struct sSymbol const *sym = sym_FindSymbol($4);
if (sym && !(sym_IsDefined(sym) && sym->type != SYM_LABEL))
yyerror("Label \"%s\" is not a valid argument to DEF",
$4);
constexpr_Number(&$$, !!sym);
oDontExpandStrings = false; oDontExpandStrings = false;
} }
| T_OP_STRCMP '(' string comma string ')' | T_OP_STRCMP '(' string comma string ')'

View File

@@ -24,15 +24,13 @@
void constexpr_Symbol(struct ConstExpression *expr, char *tzSym) void constexpr_Symbol(struct ConstExpression *expr, char *tzSym)
{ {
if (!sym_isConstant(tzSym)) { struct sSymbol *sym = sym_FindSymbol(tzSym);
struct sSymbol *pSym = sym_FindSymbol(tzSym);
if (pSym != NULL) { if (!sym) {
expr->u.pSym = pSym; fatalerror("'%s' not defined", tzSym);
expr->isSym = 1; } else if (!sym_IsConstant(sym)) {
} else { expr->u.pSym = sym;
fatalerror("'%s' not defined", tzSym); expr->isSym = 1;
}
} else { } else {
constexpr_Number(expr, sym_GetConstantValue(tzSym)); constexpr_Number(expr, sym_GetConstantValue(tzSym));
} }
@@ -41,23 +39,21 @@ void constexpr_Symbol(struct ConstExpression *expr, char *tzSym)
void constexpr_BankSymbol(struct ConstExpression *expr, char *tzSym) void constexpr_BankSymbol(struct ConstExpression *expr, char *tzSym)
{ {
constexpr_Number(expr, 0); constexpr_Number(expr, 0);
struct sSymbol *sym = sym_FindSymbol(tzSym);
if (sym_FindSymbol(tzSym) == pPCSymbol) { if (!sym) {
yyerror("BANK argument doesn't exist");
} else if (sym == pPCSymbol) {
if (pCurrentSection->nBank == -1) if (pCurrentSection->nBank == -1)
yyerror("%s's bank is not known yet", tzSym); yyerror("Current bank is not known yet");
else else
constexpr_Number(expr, pCurrentSection->nBank); constexpr_Number(expr, pCurrentSection->nBank);
} else if (sym_isConstant(tzSym)) { } else if (sym->type != SYM_LABEL) {
yyerror("BANK argument must be a relocatable identifier"); yyerror("BANK argument must be a label");
} else if (sym->pSection->nBank == -1) {
yyerror("BANK argument's bank is not known yet'");
} else { } else {
struct sSymbol *pSymbol = sym_FindSymbol(tzSym); constexpr_Number(expr, sym->pSection->nBank);
if (!pSymbol)
yyerror("BANK argument doesn't exist");
else if (!pSymbol->pSection || pSymbol->pSection->nBank == -1)
yyerror("BANK argument must be a relocatable identifier");
else
constexpr_Number(expr, pSymbol->pSection->nBank);
} }
} }

View File

@@ -277,20 +277,24 @@ uint32_t ParseSymbol(char *src, uint32_t size)
yyunputstr(rest); yyunputstr(rest);
/* If the symbol is an EQUS, expand it */ /* If the symbol is an EQUS, expand it */
if (!oDontExpandStrings && sym_isString(dest)) { if (!oDontExpandStrings) {
char *s; struct sSymbol const *sym = sym_FindSymbol(dest);
lex_BeginStringExpansion(dest); if (sym && sym->type == SYM_EQUS) {
char *s;
/* Feed the symbol's contents into the buffer */ lex_BeginStringExpansion(dest);
yyunputstr(s = sym_GetStringValue(dest));
/* Lines inserted this way shall not increase nLineNo */ /* Feed the symbol's contents into the buffer */
while (*s) { yyunputstr(s = sym_GetStringValue(sym));
if (*s++ == '\n')
nLineNo--; /* Lines inserted this way shall not increase nLineNo */
while (*s) {
if (*s++ == '\n')
nLineNo--;
}
return 0;
} }
return 0;
} }
strcpy(yylval.tzSym, dest); strcpy(yylval.tzSym, dest);

View File

@@ -471,7 +471,6 @@ int main(int argc, char *argv[])
(int)(60 / timespent * nTotalLines)); (int)(60 / timespent * nTotalLines));
} }
out_CheckErrors();
/* If no path specified, don't write file */ /* If no path specified, don't write file */
if (tzObjectname != NULL) if (tzObjectname != NULL)
out_WriteObject(); out_WriteObject();

View File

@@ -45,7 +45,7 @@ struct Patch {
struct PatchSymbol { struct PatchSymbol {
uint32_t ID; uint32_t ID;
struct sSymbol *pSymbol; struct sSymbol const *pSymbol;
struct PatchSymbol *pNext; struct PatchSymbol *pNext;
struct PatchSymbol *pBucketNext; /* next symbol in hash table bucket */ struct PatchSymbol *pBucketNext; /* next symbol in hash table bucket */
}; };
@@ -186,7 +186,7 @@ static void fputlong(uint32_t i, FILE *f)
/* /*
* Write a NULL-terminated string to a file * Write a NULL-terminated string to a file
*/ */
static void fputstring(char *s, FILE *f) static void fputstring(char const *s, FILE *f)
{ {
while (*s) while (*s)
fputc(*s++, f); fputc(*s++, f);
@@ -259,15 +259,15 @@ static void writesection(struct Section *pSect, FILE *f)
/* /*
* Write a symbol to a file * Write a symbol to a file
*/ */
static void writesymbol(struct sSymbol *pSym, FILE *f) static void writesymbol(struct sSymbol const *pSym, FILE *f)
{ {
uint32_t type; uint32_t type;
uint32_t offset; uint32_t offset;
int32_t sectid; int32_t sectid;
if (!(pSym->nType & SYMF_DEFINED)) if (!sym_IsDefined(pSym))
type = SYMTYPE_IMPORT; type = SYMTYPE_IMPORT;
else if (pSym->nType & SYMF_EXPORT) else if (pSym->isExported)
type = SYMTYPE_EXPORT; type = SYMTYPE_EXPORT;
else else
type = SYMTYPE_LOCAL; type = SYMTYPE_LOCAL;
@@ -283,7 +283,7 @@ static void writesymbol(struct sSymbol *pSym, FILE *f)
break; break;
case SYMTYPE_EXPORT: case SYMTYPE_EXPORT:
offset = pSym->nValue; offset = pSym->nValue;
if (pSym->nType & SYMF_CONST) if (pSym->type != SYM_LABEL)
sectid = -1; sectid = -1;
else else
sectid = getsectid(pSym->pSection); sectid = getsectid(pSym->pSection);
@@ -307,7 +307,7 @@ static void writesymbol(struct sSymbol *pSym, FILE *f)
*/ */
static uint32_t nextID; static uint32_t nextID;
static uint32_t addsymbol(struct sSymbol *pSym) static uint32_t addsymbol(struct sSymbol const *pSym)
{ {
struct PatchSymbol *pPSym, **ppPSym; struct PatchSymbol *pPSym, **ppPSym;
uint32_t hash; uint32_t hash;
@@ -350,7 +350,7 @@ static void addexports(void)
pSym = tHashedSymbols[i]; pSym = tHashedSymbols[i];
while (pSym) { while (pSym) {
if (pSym->nType & SYMF_EXPORT) if (pSym->isExported)
addsymbol(pSym); addsymbol(pSym);
pSym = pSym->pNext; pSym = pSym->pNext;
} }
@@ -409,11 +409,16 @@ void createpatch(uint32_t type, struct Expression *expr)
rpnexpr[rpnptr++] = rpn_PopByte(expr); rpnexpr[rpnptr++] = rpn_PopByte(expr);
break; break;
case RPN_SYM: case RPN_SYM:
{
symptr = 0; symptr = 0;
while ((tzSym[symptr++] = rpn_PopByte(expr)) != 0) while ((tzSym[symptr++] = rpn_PopByte(expr)) != 0)
; ;
if (sym_isConstant(tzSym)) { struct sSymbol const *sym = sym_FindSymbol(tzSym);
if (!sym) {
break; // TODO: wtf?
} else if (sym_IsConstant(sym)) {
uint32_t value; uint32_t value;
value = sym_GetConstantValue(tzSym); value = sym_GetConstantValue(tzSym);
@@ -423,11 +428,6 @@ void createpatch(uint32_t type, struct Expression *expr)
rpnexpr[rpnptr++] = value >> 16; rpnexpr[rpnptr++] = value >> 16;
rpnexpr[rpnptr++] = value >> 24; rpnexpr[rpnptr++] = value >> 24;
} else { } else {
struct sSymbol *sym = sym_FindSymbol(tzSym);
if (sym == NULL)
break;
symptr = addsymbol(sym); symptr = addsymbol(sym);
rpnexpr[rpnptr++] = RPN_SYM; rpnexpr[rpnptr++] = RPN_SYM;
rpnexpr[rpnptr++] = symptr & 0xFF; rpnexpr[rpnptr++] = symptr & 0xFF;
@@ -436,6 +436,7 @@ void createpatch(uint32_t type, struct Expression *expr)
rpnexpr[rpnptr++] = symptr >> 24; rpnexpr[rpnptr++] = symptr >> 24;
} }
break; break;
}
case RPN_BANK_SYM: case RPN_BANK_SYM:
{ {
struct sSymbol *sym; struct sSymbol *sym;
@@ -526,34 +527,6 @@ static void checksectionoverflow(uint32_t delta_size)
} }
} }
/*
* Check for errors that could happen while writing an object file
* This is important as out_WriteObject is skipped entirely when `-o` is omitted
* Therefore, errors such as memory allocations still should be handled in
* out_WriteObject and not here
*/
void out_CheckErrors(void)
{
/* Local symbols cannot be imported from elsewhere */
struct PatchSymbol *pSym = pPatchSymbols;
while (pSym) {
struct sSymbol *pSymbol = pSym->pSymbol;
if (!(pSymbol->nType & SYMF_DEFINED)
&& pSymbol->nType & SYMF_LOCAL) {
char *name = pSymbol->tzName;
char *localPtr = strchr(name, '.');
if (localPtr)
name = localPtr;
errx(1, "%s(%u) : '%s' not defined",
pSymbol->tzFileName, pSymbol->nFileLine, name);
}
pSym = pSym->pNext;
}
}
/* /*
* Write an objectfile * Write an objectfile
*/ */
@@ -690,6 +663,7 @@ void out_SetCurrentSection(struct Section *pSect)
pPCSymbol->nValue = nPC; pPCSymbol->nValue = nPC;
pPCSymbol->pSection = pCurrentSection; pPCSymbol->pSection = pCurrentSection;
pPCSymbol->isConstant = pSect && pSect->nOrg != -1;
} }
/* /*

View File

@@ -143,7 +143,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)
{ {
if (!sym_isConstant(tzSym)) { struct sSymbol *sym = sym_FindSymbol(tzSym);
if (!sym || !sym_IsConstant(sym)) {
rpn_Init(expr); rpn_Init(expr);
sym_Ref(tzSym); sym_Ref(tzSym);
expr->isReloc = 1; expr->isReloc = 1;
@@ -176,13 +178,15 @@ void rpn_BankSelf(struct Expression *expr)
void rpn_BankSymbol(struct Expression *expr, char *tzSym) void rpn_BankSymbol(struct Expression *expr, char *tzSym)
{ {
struct sSymbol const *sym = sym_FindSymbol(tzSym);
/* The @ symbol is treated differently. */ /* The @ symbol is treated differently. */
if (sym_FindSymbol(tzSym) == pPCSymbol) { if (sym == pPCSymbol) {
rpn_BankSelf(expr); rpn_BankSelf(expr);
return; return;
} }
if (sym_isConstant(tzSym)) { if (sym && sym_IsConstant(sym)) {
yyerror("BANK argument must be a relocatable identifier"); yyerror("BANK argument must be a relocatable identifier");
} else { } else {
rpn_Init(expr); rpn_Init(expr);

View File

@@ -88,6 +88,9 @@ static int32_t getvaluefield(struct sSymbol *sym)
if (sym->Callback) if (sym->Callback)
return sym->Callback(sym); return sym->Callback(sym);
if (sym->type == SYM_LABEL)
return sym->nValue + sym->pSection->nOrg;
return sym->nValue; return sym->nValue;
} }
@@ -136,12 +139,13 @@ struct sSymbol *createsymbol(char *s)
if (snprintf((*ppsym)->tzName, MAXSYMLEN + 1, "%s", s) > MAXSYMLEN) if (snprintf((*ppsym)->tzName, 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);
(*ppsym)->nValue = 0; (*ppsym)->isConstant = false;
(*ppsym)->nType = 0; (*ppsym)->isExported = false;
(*ppsym)->pScope = NULL; (*ppsym)->pScope = NULL;
(*ppsym)->pNext = NULL; (*ppsym)->pNext = NULL;
(*ppsym)->pMacro = NULL;
(*ppsym)->pSection = NULL; (*ppsym)->pSection = NULL;
(*ppsym)->nValue = 0;
(*ppsym)->pMacro = NULL;
(*ppsym)->Callback = NULL; (*ppsym)->Callback = NULL;
updateSymbolFilename(*ppsym); updateSymbolFilename(*ppsym);
return *ppsym; return *ppsym;
@@ -251,58 +255,15 @@ void sym_Purge(char *tzName)
} }
} }
/*
* Determine if a symbol has been defined
*/
uint32_t sym_isConstDefined(char *tzName)
{
struct sSymbol *psym = sym_FindSymbol(tzName);
if (psym && (psym->nType & SYMF_DEFINED)) {
uint32_t mask = SYMF_EQU | SYMF_SET | SYMF_MACRO | SYMF_STRING;
if (psym->nType & mask)
return 1;
fatalerror("'%s' is not allowed as argument to the DEF function",
tzName);
}
return 0;
}
uint32_t sym_isDefined(char *tzName)
{
struct sSymbol *psym = sym_FindSymbol(tzName);
return (psym && (psym->nType & SYMF_DEFINED));
}
/*
* Determine if the symbol is a constant
*/
uint32_t sym_isConstant(char *s)
{
struct sSymbol *psym = sym_FindSymbol(s);
/* The @ symbol is handled differently */
if (psym == pPCSymbol)
return pCurrentSection->nOrg != -1;
return (psym && (psym->nType & SYMF_CONST));
}
/* /*
* Get a string equate's value * Get a string equate's value
*/ */
char *sym_GetStringValue(char *tzSym) char *sym_GetStringValue(struct sSymbol const *sym)
{ {
const struct sSymbol *pSym = sym_FindSymbol(tzSym); if (sym != NULL)
return sym->pMacro;
if (pSym != NULL) yyerror("String symbol '%s' not defined", sym->tzName);
return pSym->pMacro;
yyerror("String symbol '%s' not defined", tzSym);
return NULL; return NULL;
} }
@@ -318,10 +279,10 @@ uint32_t sym_GetConstantValue(char *s)
if (pCurrentSection->nOrg == -1) if (pCurrentSection->nOrg == -1)
yyerror("Expected constant PC but section is not fixed"); yyerror("Expected constant PC but section is not fixed");
else else
return pPCSymbol->nValue; return getvaluefield(psym);
} else if (psym != NULL) { } else if (psym != NULL) {
if (psym->nType & SYMF_CONST) if (sym_IsConstant(psym))
return getvaluefield(psym); return getvaluefield(psym);
fatalerror("Expression must have a constant value"); fatalerror("Expression must have a constant value");
@@ -340,8 +301,8 @@ uint32_t sym_GetDefinedValue(char *s)
struct sSymbol *psym = sym_FindSymbol(s); struct sSymbol *psym = sym_FindSymbol(s);
if (psym != NULL) { if (psym != NULL) {
if ((psym->nType & SYMF_DEFINED)) { if (sym_IsDefined(psym)) {
if (psym->nType & (SYMF_MACRO | SYMF_STRING)) if (!sym_IsNumeric(psym))
yyerror("'%s' is a macro or string symbol", s); yyerror("'%s' is a macro or string symbol", s);
return getvaluefield(psym); return getvaluefield(psym);
@@ -472,10 +433,10 @@ static struct sSymbol *createNonrelocSymbol(char *tzSym)
struct sSymbol *nsym = findsymbol(tzSym, NULL); struct sSymbol *nsym = findsymbol(tzSym, NULL);
if (nsym != NULL) { if (nsym != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (sym_IsDefined(nsym)) {
yyerror("'%s' already defined at %s(%u)", yyerror("'%s' already defined at %s(%u)",
tzSym, nsym->tzFileName, nsym->nFileLine); tzSym, nsym->tzFileName, nsym->nFileLine);
} else if (nsym->nType & SYMF_REF) { } else {
yyerror("'%s' referenced as label at %s(%u)", yyerror("'%s' referenced as label at %s(%u)",
tzSym, nsym->tzFileName, nsym->nFileLine); tzSym, nsym->tzFileName, nsym->nFileLine);
} }
@@ -493,12 +454,11 @@ void sym_AddEqu(char *tzSym, int32_t value)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct sSymbol *nsym = createNonrelocSymbol(tzSym);
if (nsym) { nsym->nValue = value;
nsym->nValue = value; nsym->type = SYM_EQU;
nsym->nType |= SYMF_EQU | SYMF_DEFINED | SYMF_CONST; nsym->isConstant = true;
nsym->pScope = NULL; nsym->pScope = NULL;
updateSymbolFilename(nsym); updateSymbolFilename(nsym);
}
} }
/* /*
@@ -517,28 +477,16 @@ void sym_AddString(char *tzSym, char *tzValue)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct sSymbol *nsym = createNonrelocSymbol(tzSym);
if (nsym) { nsym->pMacro = malloc(strlen(tzValue) + 1);
nsym->pMacro = malloc(strlen(tzValue) + 1);
if (nsym->pMacro != NULL) if (nsym->pMacro != NULL)
strcpy(nsym->pMacro, tzValue); strcpy(nsym->pMacro, tzValue);
else else
fatalerror("No memory for string equate"); fatalerror("No memory for string equate");
nsym->nType |= SYMF_STRING | SYMF_DEFINED; nsym->type = SYM_EQUS;
nsym->ulMacroSize = strlen(tzValue); nsym->ulMacroSize = strlen(tzValue);
nsym->pScope = NULL; nsym->pScope = NULL;
}
}
/*
* check if symbol is a string equated symbol
*/
uint32_t sym_isString(char *tzSym)
{
const struct sSymbol *pSym = findsymbol(tzSym, NULL);
return (pSym && (pSym->nType & SYMF_STRING));
} }
/* /*
@@ -549,18 +497,18 @@ void sym_AddSet(char *tzSym, int32_t value)
struct sSymbol *nsym = findsymbol(tzSym, NULL); struct sSymbol *nsym = findsymbol(tzSym, NULL);
if (nsym != NULL) { if (nsym != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (sym_IsDefined(nsym)) {
if (!(nsym->nType & SYMF_CONST)) if (nsym->type == SYM_LABEL)
yyerror("'%s' already defined as non-constant at %s(%u)", yyerror("'%s' already defined as non-constant at %s(%u)",
tzSym, tzSym,
nsym->tzFileName, nsym->tzFileName,
nsym->nFileLine); nsym->nFileLine);
else if (!(nsym->nType & SYMF_SET)) else if (nsym->type != SYM_SET)
yyerror("'%s' already defined as constant at %s(%u)", yyerror("'%s' already defined as constant at %s(%u)",
tzSym, tzSym,
nsym->tzFileName, nsym->tzFileName,
nsym->nFileLine); nsym->nFileLine);
} else if (nsym->nType & SYMF_REF) { } else if (nsym->type == SYM_REF) {
yyerror("'%s' already referenced at %s(%u)", yyerror("'%s' already referenced at %s(%u)",
tzSym, tzSym,
nsym->tzFileName, nsym->tzFileName,
@@ -570,12 +518,11 @@ void sym_AddSet(char *tzSym, int32_t value)
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
} }
if (nsym) { nsym->nValue = value;
nsym->nValue = value; nsym->type = SYM_SET;
nsym->nType |= SYMF_SET | SYMF_DEFINED | SYMF_CONST; nsym->isConstant = true;
nsym->pScope = NULL; nsym->pScope = NULL;
updateSymbolFilename(nsym); updateSymbolFilename(nsym);
}
} }
/* /*
@@ -626,7 +573,7 @@ void sym_AddReloc(char *tzSym)
nsym = findsymbol(tzSym, scope); nsym = findsymbol(tzSym, scope);
if (nsym != NULL) { if (nsym != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (sym_IsDefined(nsym)) {
yyerror("'%s' already defined in %s(%d)", tzSym, yyerror("'%s' already defined in %s(%d)", tzSym,
nsym->tzFileName, nsym->nFileLine); nsym->tzFileName, nsym->nFileLine);
} }
@@ -634,24 +581,21 @@ void sym_AddReloc(char *tzSym)
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
} }
if (nsym) { nsym->nValue = nPC;
nsym->nValue = nPC; nsym->type = SYM_LABEL;
nsym->nType |= SYMF_RELOC | SYMF_DEFINED; nsym->isConstant = pCurrentSection && pCurrentSection->nOrg != -1;
if (localPtr)
nsym->nType |= SYMF_LOCAL;
if (exportall) if (exportall)
nsym->nType |= SYMF_EXPORT; nsym->isExported = true;
nsym->pScope = scope; nsym->pScope = scope;
nsym->pSection = pCurrentSection; nsym->pSection = pCurrentSection;
/* Labels need to be assigned a section, except PC */ /* Labels need to be assigned a section, except PC */
if (!pCurrentSection && strcmp(tzSym, "@")) if (!pCurrentSection && strcmp(tzSym, "@"))
yyerror("Label \"%s\" created outside of a SECTION", yyerror("Label \"%s\" created outside of a SECTION",
tzSym); tzSym);
updateSymbolFilename(nsym); updateSymbolFilename(nsym);
}
pScope = findsymbol(tzSym, scope); pScope = findsymbol(tzSym, scope);
} }
@@ -663,7 +607,7 @@ void sym_AddReloc(char *tzSym)
* *
* It returns 1 if the difference is defined, 0 if not. * It returns 1 if the difference is defined, 0 if not.
*/ */
int32_t sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2) bool sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2)
{ {
const struct sSymbol *nsym1 = sym_FindSymbol(tzSym1); const struct sSymbol *nsym1 = sym_FindSymbol(tzSym1);
const struct sSymbol *nsym2 = sym_FindSymbol(tzSym2); const struct sSymbol *nsym2 = sym_FindSymbol(tzSym2);
@@ -675,25 +619,25 @@ int32_t sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2)
if (nsym2 == NULL) if (nsym2 == NULL)
fatalerror("Symbol \"%s\" isn't defined.", tzSym2); fatalerror("Symbol \"%s\" isn't defined.", tzSym2);
int32_t s1reloc = (nsym1->nType & SYMF_RELOC) != 0; int32_t s1const = sym_IsConstant(nsym1);
int32_t s2reloc = (nsym2->nType & SYMF_RELOC) != 0; int32_t s2const = sym_IsConstant(nsym2);
/* Both are non-relocatable */ /* Both are non-relocatable */
if (!s1reloc && !s2reloc) if (s1const && s2const)
return 1; return true;
/* One of them is relocatable, the other one is not. */ /* One of them is relocatable, the other one is not. */
if (s1reloc ^ s2reloc) if (s1const ^ s2const)
return 0; return false;
/* /*
* Both of them are relocatable. Make sure they are defined (internal * Both of them are relocatable. Make sure they are defined (internal
* coherency with sym_AddReloc and sym_AddLocalReloc). * coherency with sym_AddReloc and sym_AddLocalReloc).
*/ */
if (!(nsym1->nType & SYMF_DEFINED)) if (!sym_IsDefined(nsym1))
fatalerror("Relocatable symbol \"%s\" isn't defined.", tzSym1); fatalerror("Relocatable symbol \"%s\" isn't defined.", tzSym1);
if (!(nsym2->nType & SYMF_DEFINED)) if (!sym_IsDefined(nsym2))
fatalerror("Relocatable symbol \"%s\" isn't defined.", tzSym2); fatalerror("Relocatable symbol \"%s\" isn't defined.", tzSym2);
/* /*
@@ -708,13 +652,10 @@ int32_t sym_IsRelocDiffDefined(char *tzSym1, char *tzSym2)
*/ */
void sym_Export(char *tzSym) void sym_Export(char *tzSym)
{ {
sym_Ref(tzSym);
struct sSymbol *nsym = sym_FindSymbol(tzSym); struct sSymbol *nsym = sym_FindSymbol(tzSym);
if (nsym == NULL) nsym->isExported = true;
nsym = createsymbol(tzSym);
if (nsym)
nsym->nType |= SYMF_EXPORT;
} }
/* /*
@@ -724,18 +665,16 @@ void sym_AddMacro(char *tzSym, int32_t nDefLineNo)
{ {
struct sSymbol *nsym = createNonrelocSymbol(tzSym); struct sSymbol *nsym = createNonrelocSymbol(tzSym);
if (nsym) { nsym->type = SYM_MACRO;
nsym->nType |= SYMF_MACRO | SYMF_DEFINED; nsym->pScope = NULL;
nsym->pScope = NULL; nsym->ulMacroSize = ulNewMacroSize;
nsym->ulMacroSize = ulNewMacroSize; nsym->pMacro = tzNewMacro;
nsym->pMacro = tzNewMacro; updateSymbolFilename(nsym);
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;
nsym->nFileLine = nDefLineNo;
}
} }
/* /*
@@ -748,7 +687,6 @@ void sym_Ref(char *tzSym)
if (nsym == NULL) { if (nsym == NULL) {
char fullname[MAXSYMLEN + 1]; char fullname[MAXSYMLEN + 1];
int isLocal = 0;
if (*tzSym == '.') { if (*tzSym == '.') {
if (!pScope) if (!pScope)
@@ -757,17 +695,11 @@ void sym_Ref(char *tzSym)
fullSymbolName(fullname, sizeof(fullname), tzSym, fullSymbolName(fullname, sizeof(fullname), tzSym,
pScope); pScope);
tzSym = fullname; tzSym = fullname;
isLocal = 1;
} }
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
nsym->type = SYM_REF;
if (nsym && isLocal)
nsym->nType |= SYMF_LOCAL;
} }
if (nsym)
nsym->nType |= SYMF_REF;
} }
/* /*

View File

@@ -27,7 +27,7 @@ POPS ; Ensure we are in neither section
; TODO: uncomment all that can be, there is seriously room for improvement here ; TODO: uncomment all that can be, there is seriously room for improvement here
; Diffing two constants should work ; Diffing two constants should work
; But it doesn't yet print_diff Constant, Constant2 print_diff Constant, Constant2
; Diffing two labels in the same SECTION as well ; Diffing two labels in the same SECTION as well
print_diff Known2, Known print_diff Known2, Known
; Diffing a constant and a "floating" label cannot work ; Diffing a constant and a "floating" label cannot work
@@ -40,7 +40,7 @@ POPS ; Ensure we are in neither section
; Now let's fiddle with PC ; Now let's fiddle with PC
SECTION "fixed PC", ROM0[420] SECTION "fixed PC", ROM0[420]
; Diffing a constant and PC should work ; Diffing a constant and PC should work
; But it doesn't yet print_diff Constant, @ print_diff Constant, @
; Diffing a floating label and PC cannot work ; Diffing a floating label and PC cannot work
; ...And that causes a fatal error print_diff Known, @ ; ...And that causes a fatal error print_diff Known, @
; Diffinf a ref and PC cannot work ; Diffinf a ref and PC cannot work
@@ -49,7 +49,7 @@ SECTION "fixed PC", ROM0[420]
print_diff @, @ print_diff @, @
; Diffing PC and a label from here should work ; Diffing PC and a label from here should work
LocalFixed: LocalFixed:
; But it doesn't yet print_diff LocalFixed, @ print_diff LocalFixed, @
SECTION "Floating PC", ROM0 SECTION "Floating PC", ROM0
; Diffing a constant and PC cannot work ; Diffing a constant and PC cannot work

View File

@@ -1,5 +1,11 @@
$FFFFFFE5
$1B
$4 $4
$FFFFFFFC $FFFFFFFC
$FFFFFE86
$17A
$0
$0
$0 $0
$0 $0
$0 $0

View File

@@ -1,5 +1,5 @@
ERROR: pc-bank.asm(2): ERROR: pc-bank.asm(2):
Source address $2a00 not in $FF00 to $FFFF Source address $2a00 not in $FF00 to $FFFF
ERROR: pc-bank.asm(11): ERROR: pc-bank.asm(11):
@'s bank is not known yet Current bank is not known yet
error: Assembly aborted (2 errors)! error: Assembly aborted (2 errors)!

View File

@@ -1,2 +1,3 @@
ERROR: pc-def.asm(1): ERROR: pc-def.asm(1):
'@' is not allowed as argument to the DEF function Label "@" is not a valid argument to DEF
error: Assembly aborted (1 errors)!

View File

@@ -0,0 +1 @@
defined

View File

@@ -1 +0,0 @@
error: undefined-dot.asm(3) : '.' not defined