aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-06-05 07:58:06 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-06-05 07:58:06 +0100
commit030704970dcfc29c7c82e2aefb4f2646634cc24c (patch)
tree814b81d10ef217b0eed625e34d2838ae08e8bbf9
parentc2b7c1c123955e71fe4be8cd3ad62d71b22986cb (diff)
downloadtr8vm-030704970dcfc29c7c82e2aefb4f2646634cc24c.tar.gz
tr8vm-030704970dcfc29c7c82e2aefb4f2646634cc24c.zip
Copy the filename to track location
-rw-r--r--tr8as.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/tr8as.c b/tr8as.c
index 9cdb11a..e150aeb 100644
--- a/tr8as.c
+++ b/tr8as.c
@@ -123,6 +123,15 @@ static uint8_t palette[16][3] =
{ 0xff, 0xff, 0xff },
};
+static const char *mstrdup(const char *src)
+{
+ char *p = malloc(strlen(src) + 1);
+ if (p)
+ strcpy(p, src);
+
+ return p;
+}
+
static uint8_t error_l(const char *msg, Location *loc, const char *reason)
{
fprintf(stderr, "%s (%s:%d): %s\n", msg, loc->filename, loc->line, reason);
@@ -237,7 +246,7 @@ static uint8_t new_define(As *as, char *id, char *value)
as->defs[as->dcnt].id = str_hash(id);
strcpy(as->defs[as->dcnt].value, value);
- as->defs[as->dcnt].loc.filename = as->loc.filename;
+ as->defs[as->dcnt].loc.filename = mstrdup(as->loc.filename);
as->defs[as->dcnt++].loc.line = as->loc.line;
if (as->dcnt == MAX_DEFS)
@@ -385,7 +394,7 @@ static uint8_t new_label(As *as, char *word)
as->labels[as->lcnt].id = str_hash(word);
- as->labels[as->lcnt].loc.filename = as->loc.filename;
+ as->labels[as->lcnt].loc.filename = mstrdup(as->loc.filename);
as->labels[as->lcnt].loc.line = as->loc.line;
as->labels[as->lcnt++].addr = as->addr;
@@ -421,7 +430,7 @@ static uint8_t new_ref(As *as, char *word, uint16_t mask, uint16_t addr)
strcpy(as->refs[as->rcnt].label, pt);
as->refs[as->rcnt].mod = mod;
as->refs[as->rcnt].mask = mask;
- as->refs[as->rcnt].loc.filename = as->loc.filename;
+ as->refs[as->rcnt].loc.filename = mstrdup(as->loc.filename);
as->refs[as->rcnt].loc.line = as->loc.line;
as->refs[as->rcnt++].addr = addr;
@@ -1641,6 +1650,8 @@ static uint8_t parse(As *as, char *c)
static uint8_t asm(As *as, const char *filename, FILE *in)
{
+ uint16_t i;
+ uint8_t rc;
char line[MAX_LINE];
memset(as, 0, sizeof(As));
@@ -1653,7 +1664,16 @@ static uint8_t asm(As *as, const char *filename, FILE *in)
return 0;
}
- return resolve(as);
+ rc = resolve(as);
+
+ for (i = 0; i < as->rcnt; i++)
+ free((void *)as->refs[i].loc.filename);
+ for (i = 0; i < as->dcnt; i++)
+ free((void *)as->defs[i].loc.filename);
+ for (i = 0; i < as->lcnt; i++)
+ free((void *)as->labels[i].loc.filename);
+
+ return rc;
}
#ifdef DO_MAIN