commit 1e66542

Devine Lu Linvega  ·  2024-04-04 03:20:40 +0000 UTC
parent c298c6f
(uxnasm) References print file and line number
1 files changed,  +27, -24
+27, -24
  1@@ -18,8 +18,8 @@ WITH REGARD TO THIS SOFTWARE.
  2 typedef unsigned char Uint8;
  3 typedef signed char Sint8;
  4 typedef unsigned short Uint16;
  5-typedef struct { char *name, rune, *content; Uint16 addr, refs; } Item;
  6 typedef struct { int line; char *path; } Context;
  7+typedef struct { char *name, rune, *data; Uint16 addr, refs, line; } Item;
  8 
  9 static int ptr, length;
 10 static char token[0x40], scope[0x40], lambda[0x05];
 11@@ -37,12 +37,12 @@ static char ops[][4] = {
 12 	"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
 13 };
 14 
 15-static int   find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; } /* chr in str */
 16-static int   shex(char *s) { int n = 0; char c; while((c = *s++)) { if(find(hexad, c) < 0) return -1; n = n << 4, n |= find(hexad, c); } return n; } /* str to hex */
 17-static int   scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i]) if(!a[i] || ++i >= len) return 1; return 0; } /* str compare */
 18-static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; } /* str copy */
 19-static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */
 20-static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */
 21+static int   find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; }
 22+static int   shex(char *s) { int n = 0; char c; while((c = *s++)) { if(find(hexad, c) < 0) return -1; n = n << 4, n |= find(hexad, c); } return n; }
 23+static int   scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i]) if(!a[i] || ++i >= len) return 1; return 0; }
 24+static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; }
 25+static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; }
 26+static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; }
 27 
 28 #define ishex(x) (shex(x) >= 0)
 29 #define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
 30@@ -50,8 +50,9 @@ static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j),
 31 #define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
 32 #define findlabel(x) finditem(x, labels, labels_len)
 33 #define findmacro(x) finditem(x, macros, macro_len)
 34-#define error_top(name, msg) !printf("%s: %s\n", name, msg)
 35-#define error_asm(name) !printf("%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line)
 36+#define error_top(id, msg) !printf("%s: %s\n", id, msg)
 37+#define error_asm(id) !printf("%s: %s in @%s, %s:%d.\n", id, token, scope, ctx->path, ctx->line)
 38+#define error_ref(id) !printf("%s: %s, %s:%d\n", id, r->name, r->data, r->line)
 39 
 40 /* clang-format on */
 41 
 42@@ -121,8 +122,8 @@ walkcomment(FILE *f, Context *ctx)
 43 static int
 44 walkmacro(Item *m, Context *ctx)
 45 {
 46-	char c, *contentptr = m->content, *cptr = token;
 47-	while((c = *contentptr++)) {
 48+	char c, *dataptr = m->data, *cptr = token;
 49+	while((c = *dataptr++)) {
 50 		if(c < 0x21) {
 51 			*cptr++ = 0x00;
 52 			if(token[0] && !parse(token, NULL, ctx)) return 0;
 53@@ -174,7 +175,7 @@ makemacro(char *name, FILE *f, Context *ctx)
 54 	if(findmacro(name)) return error_asm("Macro is duplicate");
 55 	m = &macros[macro_len++];
 56 	m->name = push(name, 0);
 57-	m->content = dictnext;
 58+	m->data = dictnext;
 59 	while(f && fread(&c, 1, 1, f) && c != '{')
 60 		if(c == 0xa) ctx->line += 1;
 61 	while(f && fread(&c, 1, 1, f)) {
 62@@ -209,7 +210,7 @@ makelabel(char *name, int setscope, Context *ctx)
 63 }
 64 
 65 static int
 66-makeref(char *label, char rune, Uint16 addr)
 67+makeref(char *label, char rune, Uint16 addr, Context *ctx)
 68 {
 69 	Item *r;
 70 	if(refs_len >= 0x1000) return error_top("References limit exceeded", label);
 71@@ -223,6 +224,8 @@ makeref(char *label, char rune, Uint16 addr)
 72 		r->name = push(label, 0);
 73 	r->rune = rune;
 74 	r->addr = addr;
 75+	r->line = ctx->line;
 76+	r->data = ctx->path;
 77 	return 1;
 78 }
 79 
 80@@ -306,15 +309,15 @@ parse(char *w, FILE *f, Context *ctx)
 81 	case '&': return makelabel(w, 0, ctx);
 82 	case '}': return makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx);
 83 	case '#': return writehex(w, ctx);
 84-	case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
 85-	case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
 86-	case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
 87-	case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
 88+	case '_': return makeref(w + 1, w[0], ptr, ctx) && writebyte(0xff, ctx);
 89+	case ',': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
 90+	case '-': return makeref(w + 1, w[0], ptr, ctx) && writebyte(0xff, ctx);
 91+	case '.': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
 92 	case ':': printf("Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
 93-	case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff);
 94-	case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
 95-	case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
 96-	case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
 97+	case '=': return makeref(w + 1, w[0], ptr, ctx) && writeshort(0xffff);
 98+	case ';': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
 99+	case '?': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(0x20, ctx) && writeshort(0xffff);
100+	case '!': return makeref(w + 1, w[0], ptr + 1, ctx) && writebyte(0x40, ctx) && writeshort(0xffff);
101 	case '"': return writestring(w + 1, ctx);
102 	case '~': return !assemble(w + 1) ? error_asm("Include missing") : 1;
103 	case '$':
104@@ -325,7 +328,7 @@ parse(char *w, FILE *f, Context *ctx)
105 	if(ishex(w)) return writehex(w, ctx);
106 	if(isopc(w)) return writebyte(findopcode(w), ctx);
107 	if((m = findmacro(w))) return walkmacro(m, ctx);
108-	return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff);
109+	return makeref(w, ' ', ptr + 1, ctx) && writebyte(0x60, ctx) && writeshort(0xffff);
110 }
111 
112 static int
113@@ -336,13 +339,13 @@ resolve(char *filename)
114 	for(i = 0; i < refs_len; i++) {
115 		Item *r = &refs[i], *l = findlabel(r->name);
116 		Uint8 *rom = data + r->addr;
117-		if(!l) return error_top("Label unknown", r->name);
118+		if(!l) return error_ref("Label unknown");
119 		switch(r->rune) {
120 		case '_':
121 		case ',':
122 			*rom = rel = l->addr - r->addr - 2;
123 			if((Sint8)data[r->addr] != rel)
124-				return error_top("Relative reference too far", r->name);
125+				return error_ref("Relative reference too far");
126 			break;
127 		case '-':
128 		case '.':