Merge pull request #196 from error-msgs

Print more useful error messages when redefining symbols

Signed-off-by: Antonio Niño Díaz <antonio_nd@outlook.com>
This commit is contained in:
Antonio Niño Díaz
2017-08-01 19:46:58 +01:00
13 changed files with 130 additions and 42 deletions

View File

@@ -41,6 +41,8 @@ extern void fstk_RunRept(ULONG count);
FILE * FILE *
fstk_FindFile(char *); fstk_FindFile(char *);
int fstk_GetLine(void);
extern int yywrap(void); extern int yywrap(void);
#endif #endif

View File

@@ -16,6 +16,8 @@ struct sSymbol {
ULONG ulMacroSize; ULONG ulMacroSize;
char *pMacro; char *pMacro;
SLONG(*Callback) (struct sSymbol *); SLONG(*Callback) (struct sSymbol *);
char tzFileName[_MAX_PATH + 1]; /* File where the symbol was defined. */
ULONG nFileLine; /* Line where the symbol was defined. */
}; };
#define SYMF_RELOC 0x001 /* symbol will be reloc'ed during #define SYMF_RELOC 0x001 /* symbol will be reloc'ed during
* linking, it's absolute value is * linking, it's absolute value is

6
include/common.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef RGBDS_COMMON_H
#define RGBDS_COMMON_H
#define RGBDS_OBJECT_VERSION_STRING "RGB5"
#endif /* RGBDS_COMMON_H */

View File

@@ -89,6 +89,9 @@ struct sSymbol {
SLONG nSectionID; /* internal to object.c */ SLONG nSectionID; /* internal to object.c */
struct sSection *pSection; struct sSection *pSection;
SLONG nOffset; SLONG nOffset;
char *pzObjFileName; /* Object file where the symbol is located. */
char *pzFileName; /* Source file where the symbol was defined. */
ULONG nFileLine; /* Line where the symbol was defined. */
}; };
enum ePatchType { enum ePatchType {

View File

@@ -4,7 +4,8 @@
#include "types.h" #include "types.h"
void sym_Init(void); void sym_Init(void);
void sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank); void sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank,
char *tzObjFileName, char *tzFileName, ULONG nFileLine);
SLONG sym_GetValue(char *tzName); SLONG sym_GetValue(char *tzName);
SLONG sym_GetBank(char *tzName); SLONG sym_GetBank(char *tzName);

View File

@@ -42,7 +42,7 @@ extern FILE *dependfile;
/* /*
* defines for nCurrentStatus * defines for nCurrentStatus
*/ */
#define STAT_isInclude 0 #define STAT_isInclude 0 /* 'Normal' state as well */
#define STAT_isMacro 1 #define STAT_isMacro 1
#define STAT_isMacroArg 2 #define STAT_isMacroArg 2
#define STAT_isREPTBlock 3 #define STAT_isREPTBlock 3
@@ -151,6 +151,37 @@ popcontext(void)
return (1); return (1);
} }
int
fstk_GetLine(void)
{
struct sContext *pLastFile, **ppLastFile;
switch (nCurrentStatus) {
case STAT_isInclude:
/* This is the normal mode, also used when including a file. */
return nLineNo;
case STAT_isMacro:
break; /* Peek top file of the stack */
case STAT_isMacroArg:
return nLineNo; /* ??? */
case STAT_isREPTBlock:
break; /* Peek top file of the stack */
}
if ((pLastFile = pFileStack) != NULL) {
ppLastFile = &pFileStack;
while (pLastFile->pNext) {
ppLastFile = &(pLastFile->pNext);
pLastFile = *ppLastFile;
}
return pLastFile->nLine;
}
/* This is only reached if the lexer is in REPT or MACRO mode but there
* are no saved contexts with the origin of said REPT or MACRO. */
fatalerror("fstk_GetLine: Internal error.");
}
int int
yywrap(void) yywrap(void)
{ {

View File

@@ -15,6 +15,7 @@
#include "asm/main.h" #include "asm/main.h"
#include "asm/rpn.h" #include "asm/rpn.h"
#include "asm/fstack.h" #include "asm/fstack.h"
#include "common.h"
#include "extern/err.h" #include "extern/err.h"
void out_SetCurrentSection(struct Section * pSect); void out_SetCurrentSection(struct Section * pSect);
@@ -282,6 +283,9 @@ writesymbol(struct sSymbol * pSym, FILE * f)
fputc(type, f); fputc(type, f);
if (type != SYM_IMPORT) { if (type != SYM_IMPORT) {
fputstring(pSym->tzFileName, f);
fputlong(pSym->nFileLine, f);
fputlong(sectid, f); fputlong(sectid, f);
fputlong(offset, f); fputlong(offset, f);
} }
@@ -501,7 +505,9 @@ out_WriteObject(void)
struct PatchSymbol *pSym; struct PatchSymbol *pSym;
struct Section *pSect; struct Section *pSect;
fwrite("RGB4", 1, 4, f); fwrite(RGBDS_OBJECT_VERSION_STRING, 1,
strlen(RGBDS_OBJECT_VERSION_STRING), f);
fputlong(countsymbols(), f); fputlong(countsymbols(), f);
fputlong(countsections(), f); fputlong(countsections(), f);

View File

@@ -12,7 +12,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\" .\"
.Dd April 17, 2017 .Dd July 22, 2017
.Dt RGBASM 5 .Dt RGBASM 5
.Os RGBDS Manual .Os RGBDS Manual
.Sh NAME .Sh NAME

View File

@@ -8,6 +8,7 @@
#include <time.h> #include <time.h>
#include "asm/asm.h" #include "asm/asm.h"
#include "asm/fstack.h"
#include "asm/symbol.h" #include "asm/symbol.h"
#include "asm/main.h" #include "asm/main.h"
#include "asm/mymath.h" #include "asm/mymath.h"
@@ -109,6 +110,8 @@ createsymbol(char *s)
(*ppsym)->pMacro = NULL; (*ppsym)->pMacro = NULL;
(*ppsym)->pSection = NULL; (*ppsym)->pSection = NULL;
(*ppsym)->Callback = NULL; (*ppsym)->Callback = NULL;
strcpy((*ppsym)->tzFileName, tzCurrentFileName);
(*ppsym)->nFileLine = fstk_GetLine();
return (*ppsym); return (*ppsym);
} else { } else {
fatalerror("No memory for symbol"); fatalerror("No memory for symbol");
@@ -518,7 +521,8 @@ sym_AddEqu(char *tzSym, SLONG value)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
yyerror("'%s' already defined", tzSym); yyerror("'%s' already defined in %s(%d)",
tzSym, nsym->tzFileName, nsym->nFileLine);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -550,7 +554,8 @@ sym_AddString(char *tzSym, char *tzValue)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
yyerror("'%s' already defined", tzSym); yyerror("'%s' already defined in %s(%d)",
tzSym, nsym->tzFileName, nsym->nFileLine);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -653,7 +658,8 @@ sym_AddReloc(char *tzSym)
if ((nsym = findsymbol(tzSym, scope)) != NULL) { if ((nsym = findsymbol(tzSym, scope)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
yyerror("'%s' already defined", tzSym); yyerror("'%s' already defined in %s(%d)",
tzSym, nsym->tzFileName, nsym->nFileLine);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -782,7 +788,8 @@ sym_AddMacro(char *tzSym)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
yyerror("'%s' already defined", tzSym); yyerror("'%s' already defined in %s(%d)",
tzSym, nsym->tzFileName, nsym->nFileLine);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);

View File

@@ -566,16 +566,21 @@ CreateSymbolTable(void)
((pSect->tSymbols[i]->pSection == pSect) || ((pSect->tSymbols[i]->pSection == pSect) ||
(pSect->tSymbols[i]->pSection == NULL))) { (pSect->tSymbols[i]->pSection == NULL))) {
if (pSect->tSymbols[i]->pSection == NULL) if (pSect->tSymbols[i]->pSection == NULL)
sym_CreateSymbol(pSect->tSymbols[i]-> sym_CreateSymbol(
pzName, pSect->tSymbols[i]->pzName,
pSect->tSymbols[i]-> pSect->tSymbols[i]->nOffset,
nOffset, -1); -1,
pSect->tSymbols[i]->pzObjFileName,
pSect->tSymbols[i]->pzFileName,
pSect->tSymbols[i]->nFileLine);
else else
sym_CreateSymbol(pSect->tSymbols[i]-> sym_CreateSymbol(
pzName, pSect->tSymbols[i]->pzName,
pSect->nOrg + pSect->nOrg + pSect->tSymbols[i]->nOffset,
pSect->tSymbols[i]-> pSect->nBank,
nOffset, pSect->nBank); pSect->tSymbols[i]->pzObjFileName,
pSect->tSymbols[i]->pzFileName,
pSect->tSymbols[i]->nFileLine);
} }
} }
pSect = pSect->pNext; pSect = pSect->pNext;

View File

@@ -3,11 +3,13 @@
* *
*/ */
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h"
#include "extern/err.h" #include "extern/err.h"
#include "link/assign.h" #include "link/assign.h"
#include "link/mylink.h" #include "link/mylink.h"
@@ -116,7 +118,7 @@ AllocSection(void)
*/ */
struct sSymbol * struct sSymbol *
obj_ReadSymbol(FILE * f) obj_ReadSymbol(FILE * f, char *tzObjectfile)
{ {
struct sSymbol *pSym; struct sSymbol *pSym;
@@ -126,7 +128,14 @@ obj_ReadSymbol(FILE * f)
} }
readasciiz(&pSym->pzName, f); readasciiz(&pSym->pzName, f);
if ((pSym->Type = (enum eSymbolType) fgetc(f)) != SYM_IMPORT) { pSym->Type = (enum eSymbolType)fgetc(f);
pSym->pzObjFileName = tzObjectfile;
if (pSym->Type != SYM_IMPORT) {
readasciiz(&pSym->pzFileName, f);
pSym->nFileLine = readlong(f);
pSym->nSectionID = readlong(f); pSym->nSectionID = readlong(f);
pSym->nOffset = readlong(f); pSym->nOffset = readlong(f);
} }
@@ -263,7 +272,7 @@ obj_ReadRGBSection(FILE * f)
} }
void void
obj_ReadRGB(FILE * pObjfile) obj_ReadRGB(FILE * pObjfile, char *tzObjectfile)
{ {
struct sSection *pFirstSection; struct sSection *pFirstSection;
SLONG nNumberOfSymbols, nNumberOfSections, i; SLONG nNumberOfSymbols, nNumberOfSections, i;
@@ -280,7 +289,7 @@ obj_ReadRGB(FILE * pObjfile)
} }
for (i = 0; i < nNumberOfSymbols; i += 1) for (i = 0; i < nNumberOfSymbols; i += 1)
tSymbols[i] = obj_ReadSymbol(pObjfile); tSymbols[i] = obj_ReadSymbol(pObjfile, tzObjectfile);
} else } else
tSymbols = (struct sSymbol **) & dummymem; tSymbols = (struct sSymbol **) & dummymem;
@@ -325,21 +334,22 @@ obj_ReadRGB(FILE * pObjfile)
void void
obj_ReadOpenFile(FILE * pObjfile, char *tzObjectfile) obj_ReadOpenFile(FILE * pObjfile, char *tzObjectfile)
{ {
char tzHeader[8]; char tzHeader[strlen(RGBDS_OBJECT_VERSION_STRING) + 1];
fread(tzHeader, sizeof(char), 4, pObjfile); fread(tzHeader, sizeof(char), strlen(RGBDS_OBJECT_VERSION_STRING),
tzHeader[4] = 0; pObjfile);
if (strncmp(tzHeader, "RGB", 3) == 0) {
switch (tzHeader[3]) { tzHeader[strlen(RGBDS_OBJECT_VERSION_STRING)] = 0;
case '3':
case '4': // V4 supports OAM sections, but is otherwise identical if (strncmp(tzHeader, RGBDS_OBJECT_VERSION_STRING,
obj_ReadRGB(pObjfile); strlen(RGBDS_OBJECT_VERSION_STRING)) == 0) {
break; obj_ReadRGB(pObjfile, tzObjectfile);
default:
errx(1, "'%s' uses an unsupported object file version (%s). Please reassemble it.", tzObjectfile, tzHeader);
}
} else { } else {
errx(1, "'%s' is not a valid object", tzObjectfile); for (int i = 0; i < strlen(RGBDS_OBJECT_VERSION_STRING); i++)
if (!isprint(tzHeader[i]))
tzHeader[i] = '?';
errx(1, "%s: Invalid file or object file version [%s]",
tzObjectfile, tzHeader);
} }
} }

View File

@@ -12,8 +12,10 @@
struct ISymbol { struct ISymbol {
char *pzName; char *pzName;
SLONG nValue; SLONG nValue;
SLONG nBank; SLONG nBank; /* -1 = constant */
//-1 = const char tzObjFileName[_MAX_PATH + 1]; /* Object file where the symbol was defined. */
char tzFileName[_MAX_PATH + 1]; /* Source file where the symbol was defined. */
ULONG nFileLine; /* Line where the symbol was defined. */
struct ISymbol *pNext; struct ISymbol *pNext;
}; };
@@ -76,7 +78,8 @@ sym_GetBank(char *tzName)
} }
void void
sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank) sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank, char *tzObjFileName,
char *tzFileName, ULONG nFileLine)
{ {
if (strcmp(tzName, "@") == 0) if (strcmp(tzName, "@") == 0)
return; return;
@@ -92,7 +95,10 @@ sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank)
if (nBank == -1) if (nBank == -1)
return; return;
errx(1, "Symbol '%s' defined more than once", tzName); errx(1, "'%s' in both %s : %s(%d) and %s : %s(%d)",
tzName, tzObjFileName, tzFileName, nFileLine,
(*ppSym)->tzObjFileName,
(*ppSym)->tzFileName, (*ppSym)->nFileLine);
} }
} }
@@ -102,6 +108,11 @@ sym_CreateSymbol(char *tzName, SLONG nValue, SLONG nBank)
(*ppSym)->nValue = nValue; (*ppSym)->nValue = nValue;
(*ppSym)->nBank = nBank; (*ppSym)->nBank = nBank;
(*ppSym)->pNext = NULL; (*ppSym)->pNext = NULL;
strncpy((*ppSym)->tzObjFileName, tzObjFileName,
sizeof((*ppSym)->tzObjFileName));
strncpy((*ppSym)->tzFileName, tzFileName,
sizeof((*ppSym)->tzFileName));
(*ppSym)->nFileLine = nFileLine;
} }
} }
} }

View File

@@ -12,7 +12,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\" .\"
.Dd April 17, 2017 .Dd July 22, 2017
.Dt RGBDS 5 .Dt RGBDS 5
.Os RGBDS Manual .Os RGBDS Manual
.Sh NAME .Sh NAME
@@ -42,7 +42,7 @@ is a 0terminated string of
.Bd -literal .Bd -literal
; Header ; Header
BYTE ID[4] ; "RGB4" BYTE ID[4] ; "RGB5"
LONG NumberOfSymbols ; The number of symbols used in this file LONG NumberOfSymbols ; The number of symbols used in this file
LONG NumberOfSections ; The number of sections used in this file LONG NumberOfSections ; The number of sections used in this file
@@ -59,6 +59,10 @@ REPT NumberOfSymbols ; Number of symbols defined in this object file.
IF Type != 1 ; If symbol is defined in this object file. IF Type != 1 ; If symbol is defined in this object file.
STRING FileName ; File where the symbol is defined.
LONG LineNum ; Line number in the file where the symbol is defined.
LONG SectionID ; The section number (of this object file) in which LONG SectionID ; The section number (of this object file) in which
; this symbol is defined. ; this symbol is defined.