Mute scope reporting in resolve loop

This commit is contained in:
Devine Lu Linvega 2024-03-02 08:43:19 -08:00
parent d322e787fa
commit c5d8ba4e3a
1 changed files with 48 additions and 40 deletions

View File

@ -36,7 +36,7 @@ typedef struct {
typedef struct { typedef struct {
Uint8 data[LENGTH]; Uint8 data[LENGTH];
Uint8 lambda_stack[0x100], lambda_ptr, lambda_count; Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
char scope[0x40], lambda[0x10], *location; char scope[0x40], lambda[0x10], *location, *entry;
unsigned int ptr, length; unsigned int ptr, length;
Uint16 label_len, macro_len, refs_len; Uint16 label_len, macro_len, refs_len;
Label labels[0x400]; Label labels[0x400];
@ -77,9 +77,16 @@ error_top(const char *name, const char *msg)
} }
static int static int
error(const char *name, const char *msg) error_asm(const char *name, const char *msg)
{ {
fprintf(stderr, "%s: %s in @%s, %s.\n", name, msg, p.scope, p.location); fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, msg, p.scope, p.location, 123);
return 0;
}
static int
error_res(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s.\n", name, msg);
return 0; return 0;
} }
@ -94,7 +101,7 @@ static char *
sublabel(char *src, char *scope, char *name) sublabel(char *src, char *scope, char *name)
{ {
if(slen(scope) + slen(name) >= 0x3f) { if(slen(scope) + slen(name) >= 0x3f) {
error("Sublabel length too long", name); error_asm("Sublabel length too long", name);
return NULL; return NULL;
} }
return scat(scat(scpy(scope, src, 0x40), "/"), name); return scat(scat(scpy(scope, src, 0x40), "/"), name);
@ -151,22 +158,22 @@ makemacro(char *name, FILE *f)
Macro *m; Macro *m;
char word[0x40]; char word[0x40];
if(findmacro(name)) if(findmacro(name))
return error("Macro duplicate", name); return error_asm("Macro duplicate", name);
if(sihx(name) && slen(name) % 2 == 0) if(sihx(name) && slen(name) % 2 == 0)
return error("Macro name is hex number", name); return error_asm("Macro name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error("Macro name is invalid", name); return error_asm("Macro name is invalid", name);
if(p.macro_len == 0x100) if(p.macro_len == 0x100)
return error("Macros limit exceeded", name); return error_asm("Macros limit exceeded", name);
m = &p.macros[p.macro_len++]; m = &p.macros[p.macro_len++];
scpy(name, m->name, 0x40); scpy(name, m->name, 0x40);
while(fscanf(f, "%63s", word) == 1) { while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue; if(word[0] == '{') continue;
if(word[0] == '}') break; if(word[0] == '}') break;
if(word[0] == '%') if(word[0] == '%')
return error("Macro error", name); return error_asm("Macro error", name);
if(m->len >= 0x40) if(m->len >= 0x40)
return error("Macro size exceeded", name); return error_asm("Macro size exceeded", name);
scpy(word, m->items[m->len++], 0x40); scpy(word, m->items[m->len++], 0x40);
} }
return 1; return 1;
@ -186,15 +193,15 @@ makelabel(char *name)
{ {
Label *l; Label *l;
if(findlabel(name)) if(findlabel(name))
return error("Label duplicate", name); return error_asm("Label duplicate", name);
if(sihx(name) && (slen(name) == 2 || slen(name) == 4)) if(sihx(name) && (slen(name) == 2 || slen(name) == 4))
return error("Label name is hex number", name); return error_asm("Label name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error("Label name is invalid", name); return error_asm("Label name is invalid", name);
if(isrune(name[0])) if(isrune(name[0]))
return error("Label name is runic", name); return error_asm("Label name is runic", name);
if(p.label_len == 0x400) if(p.label_len == 0x400)
return error("Labels limit exceeded", name); return error_asm("Labels limit exceeded", name);
l = &p.labels[p.label_len++]; l = &p.labels[p.label_len++];
l->addr = p.ptr; l->addr = p.ptr;
l->refs = 0; l->refs = 0;
@ -217,14 +224,14 @@ makereference(char *scope, char *label, char rune, Uint16 addr)
char subw[0x40], parent[0x40]; char subw[0x40], parent[0x40];
Reference *r; Reference *r;
if(p.refs_len >= 0x800) if(p.refs_len >= 0x800)
return error("References limit exceeded", label); return error_asm("References limit exceeded", label);
r = &p.refs[p.refs_len++]; r = &p.refs[p.refs_len++];
if(label[0] == '{') { if(label[0] == '{') {
p.lambda_stack[p.lambda_ptr++] = p.lambda_count; p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
scpy(makelambda(p.lambda_count++), r->name, 0x40); scpy(makelambda(p.lambda_count++), r->name, 0x40);
} else if(label[0] == '&' || label[0] == '/') { } else if(label[0] == '&' || label[0] == '/') {
if(!sublabel(subw, scope, label + 1)) if(!sublabel(subw, scope, label + 1))
return error("Invalid sublabel", label); return error_asm("Invalid sublabel", label);
scpy(subw, r->name, 0x40); scpy(subw, r->name, 0x40);
} else { } else {
int pos = spos(label, '/'); int pos = spos(label, '/');
@ -244,11 +251,11 @@ static int
writebyte(Uint8 b) writebyte(Uint8 b)
{ {
if(p.ptr < TRIM) if(p.ptr < TRIM)
return error("Writing in zero-page", ""); return error_asm("Writing in zero-page", "");
else if(p.ptr > 0xffff) else if(p.ptr > 0xffff)
return error("Writing after the end of RAM", ""); return error_asm("Writing after the end of RAM", "");
else if(p.ptr < p.length) else if(p.ptr < p.length)
return error("Memory overwrite", ""); return error_asm("Memory overwrite", "");
p.data[p.ptr++] = b; p.data[p.ptr++] = b;
p.length = p.ptr; p.length = p.ptr;
return 1; return 1;
@ -280,10 +287,10 @@ doinclude(char *filename)
FILE *f; FILE *f;
char w[0x40]; char w[0x40];
if(!(f = fopen(setlocation(filename), "r"))) if(!(f = fopen(setlocation(filename), "r")))
return error("Include missing", filename); return error_asm("Include missing", filename);
while(fscanf(f, "%63s", w) == 1) while(fscanf(f, "%63s", w) == 1)
if(!parse(w, f)) if(!parse(w, f))
return error("Unknown token", w); return error_asm("Unknown token", w);
fclose(f); fclose(f);
return 1; return 1;
} }
@ -296,7 +303,7 @@ parse(char *w, FILE *f)
Label *l; Label *l;
Macro *m; Macro *m;
if(slen(w) >= 63) if(slen(w) >= 63)
return error("Invalid token", w); return error_asm("Invalid token", w);
switch(w[0]) { switch(w[0]) {
case '(': /* comment */ case '(': /* comment */
if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w); if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
@ -312,22 +319,22 @@ parse(char *w, FILE *f)
break; break;
case '~': /* include */ case '~': /* include */
if(!doinclude(w + 1)) if(!doinclude(w + 1))
return error("Invalid include", w); return error_asm("Invalid include", w);
break; break;
case '%': /* macro */ case '%': /* macro */
if(!makemacro(w + 1, f)) if(!makemacro(w + 1, f))
return error("Invalid macro", w); return error_asm("Invalid macro", w);
break; break;
case '|': /* pad-absolute */ case '|': /* pad-absolute */
if(sihx(w + 1)) if(sihx(w + 1))
p.ptr = shex(w + 1); p.ptr = shex(w + 1);
else if(w[1] == '&') { else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw))) if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error("Invalid sublabel", w); return error_asm("Invalid sublabel", w);
p.ptr = l->addr; p.ptr = l->addr;
} else { } else {
if(!(l = findlabel(w + 1))) if(!(l = findlabel(w + 1)))
return error("Invalid label", w); return error_asm("Invalid label", w);
p.ptr = l->addr; p.ptr = l->addr;
} }
break; break;
@ -336,17 +343,17 @@ parse(char *w, FILE *f)
p.ptr += shex(w + 1); p.ptr += shex(w + 1);
else if(w[1] == '&') { else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw))) if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error("Invalid sublabel", w); return error_asm("Invalid sublabel", w);
p.ptr += l->addr; p.ptr += l->addr;
} else { } else {
if(!(l = findlabel(w + 1))) if(!(l = findlabel(w + 1)))
return error("Invalid label", w); return error_asm("Invalid label", w);
p.ptr += l->addr; p.ptr += l->addr;
} }
break; break;
case '@': /* label */ case '@': /* label */
if(!makelabel(w + 1)) if(!makelabel(w + 1))
return error("Invalid label", w); return error_asm("Invalid label", w);
i = 0; i = 0;
while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1])) while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1]))
i++; i++;
@ -354,7 +361,7 @@ parse(char *w, FILE *f)
break; break;
case '&': /* sublabel */ case '&': /* sublabel */
if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw)) if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw))
return error("Invalid sublabel", w); return error_asm("Invalid sublabel", w);
break; break;
case '#': /* literals hex */ case '#': /* literals hex */
if(sihx(w + 1) && slen(w) == 3) if(sihx(w + 1) && slen(w) == 3)
@ -362,7 +369,7 @@ parse(char *w, FILE *f)
else if(sihx(w + 1) && slen(w) == 5) else if(sihx(w + 1) && slen(w) == 5)
return writeshort(shex(w + 1), 1); return writeshort(shex(w + 1), 1);
else else
return error("Invalid hex literal", w); return error_asm("Invalid hex literal", w);
break; break;
case '_': /* raw byte relative */ case '_': /* raw byte relative */
makereference(p.scope, w + 1, w[0], p.ptr); makereference(p.scope, w + 1, w[0], p.ptr);
@ -395,7 +402,7 @@ parse(char *w, FILE *f)
break; break;
case '}': /* lambda end */ case '}': /* lambda end */
if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]))) if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
return error("Invalid label", w); return error_asm("Invalid label", w);
break; break;
case '[': case '[':
case ']': case ']':
@ -436,23 +443,23 @@ resolve(void)
case '_': case '_':
case ',': case ',':
if(!(l = findlabel(r->name))) if(!(l = findlabel(r->name)))
return error("Unknown relative reference", r->name); return error_res("Unknown relative reference", r->name);
p.data[r->addr] = (Sint8)(l->addr - r->addr - 2); p.data[r->addr] = (Sint8)(l->addr - r->addr - 2);
if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2)) if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2))
return error("Relative reference is too far", r->name); return error_asm("Relative reference is too far", r->name);
l->refs++; l->refs++;
break; break;
case '-': case '-':
case '.': case '.':
if(!(l = findlabel(r->name))) if(!(l = findlabel(r->name)))
return error("Unknown zero-page reference", r->name); return error_res("Unknown zero-page reference", r->name);
p.data[r->addr] = l->addr & 0xff; p.data[r->addr] = l->addr & 0xff;
l->refs++; l->refs++;
break; break;
case '=': case '=':
case ';': case ';':
if(!(l = findlabel(r->name))) if(!(l = findlabel(r->name)))
return error("Unknown absolute reference", r->name); return error_res("Unknown absolute reference", r->name);
p.data[r->addr] = l->addr >> 0x8; p.data[r->addr] = l->addr >> 0x8;
p.data[r->addr + 1] = l->addr & 0xff; p.data[r->addr + 1] = l->addr & 0xff;
l->refs++; l->refs++;
@ -461,7 +468,7 @@ resolve(void)
case '!': case '!':
default: default:
if(!(l = findlabel(r->name))) if(!(l = findlabel(r->name)))
return error("Unknown absolute reference", r->name); return error_res("Unknown absolute reference", r->name);
a = l->addr - r->addr - 2; a = l->addr - r->addr - 2;
p.data[r->addr] = a >> 0x8; p.data[r->addr] = a >> 0x8;
p.data[r->addr + 1] = a & 0xff; p.data[r->addr + 1] = a & 0xff;
@ -480,7 +487,7 @@ assemble(FILE *f)
scpy("on-reset", p.scope, 0x40); scpy("on-reset", p.scope, 0x40);
while(fscanf(f, "%62s", w) == 1) while(fscanf(f, "%62s", w) == 1)
if(slen(w) > 0x3d || !parse(w, f)) if(slen(w) > 0x3d || !parse(w, f))
return error("Invalid token", w); return error_asm("Invalid token", w);
return resolve(); return resolve();
} }
@ -529,9 +536,10 @@ main(int argc, char *argv[])
if(argc == 1) if(argc == 1)
return error_top("usage", "uxnasm [-v] input.tal output.rom"); return error_top("usage", "uxnasm [-v] input.tal output.rom");
if(argv[1][0] == '-' && argv[1][1] == 'v') if(argv[1][0] == '-' && argv[1][1] == 'v')
return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 1 Mar 2024.\n"); return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 2 Mar 2024.\n");
if(!(src = fopen(setlocation(argv[1]), "r"))) if(!(src = fopen(setlocation(argv[1]), "r")))
return !error_top("Invalid input", argv[1]); return !error_top("Invalid input", argv[1]);
p.entry = argv[1];
if(!assemble(src)) if(!assemble(src))
return !error_top("Assembly", "Failed to assemble rom."); return !error_top("Assembly", "Failed to assemble rom.");
if(!(dst = fopen(argv[2], "wb"))) if(!(dst = fopen(argv[2], "wb")))