ref: effc58241df9802e51c1eab91351a70ffe42b40b
parent: 3697065afc1ee33375e4bc8d8097554071802232
author: ISSOtm <[email protected]>
date: Fri Jan 22 06:03:43 EST 2021
Rework defining variables on command-line Avoids allocating memory Detects CLI errors even when failing to open file Reports symbols as defined on command-line instead of input line 0 (!?)
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -104,6 +104,9 @@
struct FileStackNode *fstk_GetFileStack(void)
{
+ if (!contextStack)
+ return NULL;
+
struct FileStackNode *node = contextStack->fileInfo;
/* Mark node and all of its parents as referenced if not already so they don't get freed */
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -40,12 +40,6 @@
// Unfortunately, macOS still ships 2.3, which is from 2008...
int yyparse(void);
-size_t cldefines_index;
-size_t cldefines_numindices;
-size_t cldefines_bufsize;
-const size_t cldefine_entrysize = 2 * sizeof(void *);
-char **cldefines;
-
#if defined(YYDEBUG) && YYDEBUG
extern int yydebug;
#endif
@@ -61,44 +55,6 @@
bool verbose;
bool warnings; /* True to enable warnings, false to disable them. */
-void opt_AddDefine(char *s)
-{
- char *value, *equals;
-
- if (cldefines_index >= cldefines_numindices) {
- /* Check for overflows */
- if ((cldefines_numindices * 2) < cldefines_numindices)
- fatalerror("No memory for command line defines\n");
-
- if ((cldefines_bufsize * 2) < cldefines_bufsize)
- fatalerror("No memory for command line defines\n");
-
- cldefines_numindices *= 2;
- cldefines_bufsize *= 2;
-
- cldefines = realloc(cldefines, cldefines_bufsize);
- if (!cldefines)
- fatalerror("No memory for command line defines\n");
- }
- equals = strchr(s, '=');
- if (equals) {
- *equals = '\0';
- value = equals + 1;
- } else {
- value = "1";
- }
- cldefines[cldefines_index++] = s;
- cldefines[cldefines_index++] = value;
-}
-
-static void opt_ParseDefines(void)
-{
- uint32_t i;
-
- for (i = 0; i < cldefines_index; i += 2)
- sym_AddString(cldefines[i], cldefines[i + 1]);
-}
-
/* Escapes Make-special chars from a string */
static char *make_escape(const char *str)
{
@@ -193,17 +149,13 @@
dependfile = NULL;
- /* Initial number of allocated elements in array */
- cldefines_numindices = 32;
- cldefines_bufsize = cldefines_numindices * cldefine_entrysize;
- cldefines = malloc(cldefines_bufsize);
- if (!cldefines)
- fatalerror("No memory for command line defines\n");
-
#if defined(YYDEBUG) && YYDEBUG
yydebug = 1;
#endif
+ // Perform some init for below
+ sym_Init(now);
+
// Set defaults
oGeneratePhonyDeps = false;
@@ -218,9 +170,9 @@
haltnop = true;
verbose = false;
warnings = true;
+ sym_SetExportAll(false);
uint32_t maxRecursionDepth = 64;
size_t nTargetFileNameLen = 0;
- bool exportall = false;
while ((ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1) {
switch (ch) {
@@ -231,12 +183,19 @@
errx(1, "Must specify exactly 2 characters for option 'b'");
break;
+ char *equals;
case 'D':
- opt_AddDefine(optarg);
+ equals = strchr(optarg, '=');
+ if (equals) {
+ *equals = '\0';
+ sym_AddString(optarg, equals + 1);
+ } else {
+ sym_AddString(optarg, "1");
+ }
break;
case 'E':
- exportall = true;
+ sym_SetExportAll(true);
break;
case 'g':
@@ -264,8 +223,7 @@
else
dependfile = fopen(optarg, "w");
if (dependfile == NULL)
- err(1, "Could not open dependfile %s",
- optarg);
+ err(1, "Could not open dependfile %s", optarg);
break;
case 'o':
@@ -321,8 +279,7 @@
case 'Q':
case 'T':
if (optind == argc)
- errx(1, "-M%c takes a target file name argument",
- depType);
+ errx(1, "-M%c takes a target file name argument", depType);
ep = optarg;
if (depType == 'Q')
ep = make_escape(ep);
@@ -330,14 +287,12 @@
nTargetFileNameLen += strlen(ep) + 1;
if (!tzTargetFileName) {
/* On first alloc, make an empty str */
- tzTargetFileName =
- malloc(nTargetFileNameLen + 1);
+ tzTargetFileName = malloc(nTargetFileNameLen + 1);
if (tzTargetFileName)
*tzTargetFileName = '\0';
} else {
- tzTargetFileName =
- realloc(tzTargetFileName,
- nTargetFileNameLen + 1);
+ tzTargetFileName = realloc(tzTargetFileName,
+ nTargetFileNameLen + 1);
}
if (tzTargetFileName == NULL)
err(1, "Cannot append new file to target file list");
@@ -344,8 +299,8 @@
strcat(tzTargetFileName, ep);
if (depType == 'Q')
free(ep);
- char *ptr = tzTargetFileName +
- strlen(tzTargetFileName);
+ char *ptr = tzTargetFileName + strlen(tzTargetFileName);
+
*ptr++ = ' ';
*ptr = '\0';
break;
@@ -386,10 +341,6 @@
lexer_Init();
fstk_Init(mainFileName, maxRecursionDepth);
- sym_Init(now);
- sym_SetExportAll(exportall);
-
- opt_ParseDefines();
charmap_New("main", NULL);
if (yyparse() != 0 || nbErrors != 0)
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -153,10 +153,12 @@
static void dumpFilename(struct Symbol const *sym)
{
- if (!sym->src)
- fputs("<builtin>", stderr);
- else
+ if (sym->src)
fstk_Dump(sym->src, sym->fileLine);
+ else if (sym->fileLine == 0)
+ fputs("<command-line>", stderr);
+ else
+ fputs("<builtin>", stderr);
}
/*
@@ -165,7 +167,7 @@
static void setSymbolFilename(struct Symbol *sym)
{
sym->src = fstk_GetFileStack();
- sym->fileLine = lexer_GetLineNo();
+ sym->fileLine = sym->src ? lexer_GetLineNo() : 0; // This is (NULL, 1) for built-ins
}
/*
@@ -177,7 +179,7 @@
setSymbolFilename(sym);
/* If the old node was referenced, ensure the new one is */
- if (oldSrc->referenced && oldSrc->ID != -1)
+ if (oldSrc && oldSrc->referenced && oldSrc->ID != -1)
out_RegisterNode(sym->src);
/* TODO: unref the old node, and use `out_ReplaceNode` instead if deleting it */
}
@@ -679,7 +681,7 @@
sym->isBuiltin = true;
sym->hasCallback = true;
sym->src = NULL;
- sym->fileLine = 0;
+ sym->fileLine = 1; // This is 0 for CLI-defined symbols
return sym;
}