commit 5fdd442

Devine Lu Linvega  ·  2024-03-29 00:52:50 +0000 UTC
parent b302645
(uxnasm) Updated
1 files changed,  +270, -396
+270, -396
  1@@ -1,7 +1,7 @@
  2 #include <stdio.h>
  3 
  4 /*
  5-Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick
  6+Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
  7 
  8 Permission to use, copy, modify, and distribute this software for any
  9 purpose with or without fee is hereby granted, provided that the above
 10@@ -11,43 +11,25 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 11 WITH REGARD TO THIS SOFTWARE.
 12 */
 13 
 14-#define TRIM 0x0100
 15-#define LENGTH 0x10000
 16+/* clang-format off */
 17+
 18+#define PAGE 0x0100
 19 
 20 typedef unsigned char Uint8;
 21 typedef signed char Sint8;
 22 typedef unsigned short Uint16;
 23+typedef struct { char *name, rune, *content; Uint16 addr, refs; } Item;
 24+typedef struct { int line; char *path; } Context;
 25 
 26-typedef struct {
 27-	char name[0x40], items[0x40][0x40];
 28-	Uint8 len;
 29-} Macro;
 30-
 31-typedef struct {
 32-	char name[0x40];
 33-	Uint16 addr, refs;
 34-} Label;
 35-
 36-typedef struct {
 37-	char name[0x40], rune;
 38-	Uint16 addr;
 39-} Reference;
 40-
 41-typedef struct {
 42-	Uint8 data[LENGTH];
 43-	Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
 44-	char scope[0x40], lambda[0x10], *location, *entry;
 45-	unsigned int ptr, length;
 46-	Uint16 label_len, macro_len, refs_len;
 47-	Label labels[0x400];
 48-	Macro macros[0x100];
 49-	Reference refs[0x1000];
 50-} Program;
 51-
 52-Program p;
 53-
 54-/* clang-format off */
 55+static int ptr, length;
 56+static char token[0x40], scope[0x40], lambda[0x05];
 57+static char dict[0x4000], *dictnext = dict;
 58+static Uint8 data[0x10000], lambda_stack[0x100], lambda_ptr, lambda_len;
 59+static Uint16 labels_len, refs_len, macro_len;
 60+static Item labels[0x400], refs[0x1000], macros[0x100];
 61 
 62+static char *runes = "|$@&,_.-;=!?#\"%~";
 63+static char *hexad = "0123456789abcdef";
 64 static char ops[][4] = {
 65 	"LIT", "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR",
 66 	"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
 67@@ -55,68 +37,47 @@ static char ops[][4] = {
 68 	"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
 69 };
 70 
 71-static char *runes = "|$@&,_.-;=!?#\"%~";
 72-
 73-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; } /* string compare */
 74-static int   sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f')) return 0; return i > 1; } /* string is hexadecimal */
 75-static int   shex(char *s) { int n = 0, i = 0; char c; while((c = s[i++])) if(c >= '0' && c <= '9') n = n * 16 + (c - '0'); else if(c >= 'a' && c <= 'f') n = n * 16 + 10 + (c - 'a'); return n; } /* string to num */
 76-static int   slen(char *s) { int i = 0; while(s[i]) i++; return i; } /* string length */
 77-static int   spos(char *s, char c) { Uint8 i = 0, j; while((j = s[i++])) if(j == c) return i; return -1; } /* character position */
 78-static char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
 79-static char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
 80+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 */
 81+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 */
 82+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 */
 83+static char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* str copy */
 84+static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */
 85+static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */
 86+
 87+#define ishex(x) (shex(x) >= 0)
 88+#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
 89+#define isinvalid(x) (!x[0] || ishex(x) || isopc(x) || find(runes, x[0]) >= 0)
 90+#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
 91+#define findlabel(x) finditem(x, labels, labels_len)
 92+#define findmacro(x) finditem(x, macros, macro_len)
 93+#define error_top(name, msg) !fprintf(stderr, "%s: %s\n", name, msg)
 94+#define error_asm(name) !fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line)
 95 
 96 /* clang-format on */
 97 
 98-static int parse(char *w, FILE *f);
 99-
100-static int
101-error_top(const char *name, const char *msg)
102-{
103-	fprintf(stderr, "%s: %s\n", name, msg);
104-	return 0;
105-}
106-
107-static int
108-error_asm(const char *name, const char *msg)
109-{
110-	fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, msg, p.scope, p.location, 123);
111-	return 0;
112-}
113-
114-static char *
115-setlocation(char *name)
116-{
117-	p.location = name;
118-	return name;
119-}
120+static int parse(char *w, FILE *f, Context *ctx);
121 
122 static char *
123-sublabel(char *src, char *scope, char *name)
124+push(char *s, char c)
125 {
126-	if(slen(scope) + slen(name) >= 0x3f) {
127-		error_asm("Sublabel length too long", name);
128-		return NULL;
129+	char *d = dict;
130+	for(d = dict; d < dictnext; d++) {
131+		char *ss = s, *dd = d, a, b;
132+		while((a = *dd++) == (b = *ss++))
133+			if(!a && !b) return d;
134 	}
135-	return scat(scat(scpy(scope, src, 0x40), "/"), name);
136+	return save(s, c);
137 }
138 
139-static Macro *
140-findmacro(char *name)
141+static Item *
142+finditem(char *name, Item *list, int len)
143 {
144 	int i;
145-	for(i = 0; i < p.macro_len; i++)
146-		if(scmp(p.macros[i].name, name, 0x40))
147-			return &p.macros[i];
148-	return NULL;
149-}
150-
151-static Label *
152-findlabel(char *name)
153-{
154-	int i;
155-	for(i = 0; i < p.label_len; i++)
156-		if(scmp(p.labels[i].name, name, 0x40))
157-			return &p.labels[i];
158+	if(name[0] == '&')
159+		name = join(scope, '/', name + 1);
160+	for(i = 0; i < len; i++)
161+		if(scmp(list[i].name, name, 0x40))
162+			return &list[i];
163 	return NULL;
164 }
165 
166@@ -125,19 +86,18 @@ findopcode(char *s)
167 {
168 	int i;
169 	for(i = 0; i < 0x20; i++) {
170-		int m = 0;
171-		if(!scmp(ops[i], s, 3))
172-			continue;
173-		if(!i) i |= (1 << 7); /* force keep for LIT */
174-		while(s[3 + m]) {
175-			if(s[3 + m] == '2')
176-				i |= (1 << 5); /* mode: short */
177-			else if(s[3 + m] == 'r')
178-				i |= (1 << 6); /* mode: return */
179-			else if(s[3 + m] == 'k')
180-				i |= (1 << 7); /* mode: keep */
181+		int m = 3;
182+		if(!scmp(ops[i], s, 3)) continue;
183+		if(!i) i |= (1 << 7);
184+		while(s[m]) {
185+			if(s[m] == '2')
186+				i |= (1 << 5);
187+			else if(s[m] == 'r')
188+				i |= (1 << 6);
189+			else if(s[m] == 'k')
190+				i |= (1 << 7);
191 			else
192-				return 0; /* failed to match */
193+				return 0;
194 			m++;
195 		}
196 		return i;
197@@ -146,395 +106,309 @@ findopcode(char *s)
198 }
199 
200 static int
201-makemacro(char *name, FILE *f)
202+walkcomment(FILE *f, Context *ctx)
203 {
204-	Macro *m;
205-	char word[0x40];
206-	if(findmacro(name))
207-		return error_asm("Macro duplicate", name);
208-	if(sihx(name) && slen(name) % 2 == 0)
209-		return error_asm("Macro name is hex number", name);
210-	if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
211-		return error_asm("Macro name is invalid", name);
212-	if(p.macro_len == 0x100)
213-		return error_asm("Macros limit exceeded", name);
214-	m = &p.macros[p.macro_len++];
215-	scpy(name, m->name, 0x40);
216-	while(fscanf(f, "%63s", word) == 1) {
217-		if(word[0] == '{') continue;
218-		if(word[0] == '}') break;
219-		if(word[0] == '%')
220-			return error_asm("Macro error", name);
221-		if(m->len >= 0x40)
222-			return error_asm("Macro size exceeded", name);
223-		scpy(word, m->items[m->len++], 0x40);
224+	char c;
225+	int depth = 1;
226+	while(f && fread(&c, 1, 1, f)) {
227+		if(c == 0xa) ctx->line += 1;
228+		if(c == '(') depth++;
229+		if(c == ')' && --depth < 1) return 1;
230 	}
231-	return 1;
232+	return 0;
233 }
234 
235 static int
236-isrune(char c)
237+walkmacro(Item *m, Context *ctx)
238 {
239-	char cc, *r = runes;
240-	while((cc = *r++))
241-		if(c == cc) return 1;
242-	return 0;
243+	char c, *contentptr = m->content, *cptr = token;
244+	while((c = *contentptr++)) {
245+		if(c < 0x21) {
246+			*cptr++ = 0x00;
247+			if(token[0] && !parse(token, NULL, ctx)) return 0;
248+			cptr = token;
249+		} else
250+			*cptr++ = c;
251+	}
252+	return 1;
253 }
254 
255 static int
256-makelabel(char *name)
257+walkfile(FILE *f, Context *ctx)
258 {
259-	Label *l;
260-	if(findlabel(name))
261-		return error_asm("Label duplicate", name);
262-	if(sihx(name) && (slen(name) == 2 || slen(name) == 4))
263-		return error_asm("Label name is hex number", name);
264-	if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
265-		return error_asm("Label name is invalid", name);
266-	if(isrune(name[0]))
267-		return error_asm("Label name is runic", name);
268-	if(p.label_len == 0x400)
269-		return error_asm("Labels limit exceeded", name);
270-	l = &p.labels[p.label_len++];
271-	l->addr = p.ptr;
272-	l->refs = 0;
273-	scpy(name, l->name, 0x40);
274+	char c, *cptr = token;
275+	while(f && fread(&c, 1, 1, f)) {
276+		if(c == 0xa) ctx->line += 1;
277+		if(c < 0x21) {
278+			*cptr++ = 0x00;
279+			if(token[0] && !parse(token, f, ctx))
280+				return 0;
281+			cptr = token;
282+		} else if(cptr - token < 0x3f)
283+			*cptr++ = c;
284+		else
285+			return error_asm("Token too long");
286+	}
287 	return 1;
288 }
289 
290 static char *
291 makelambda(int id)
292 {
293-	scpy("lambda", p.lambda, 0x07);
294-	p.lambda[6] = '0' + (id >> 0x4);
295-	p.lambda[7] = '0' + (id & 0xf);
296-	return p.lambda;
297+	lambda[0] = (char)0xce;
298+	lambda[1] = (char)0xbb;
299+	lambda[2] = hexad[id >> 0x4];
300+	lambda[3] = hexad[id & 0xf];
301+	return lambda;
302 }
303 
304 static int
305-makereference(char *scope, char *label, char rune, Uint16 addr)
306+makemacro(char *name, FILE *f, Context *ctx)
307 {
308-	char subw[0x40], parent[0x40];
309-	Reference *r;
310-	if(p.refs_len >= 0x1000)
311-		return error_asm("References limit exceeded", label);
312-	r = &p.refs[p.refs_len++];
313+	char c;
314+	Item *m;
315+	if(macro_len >= 0x100) return error_asm("Macros limit exceeded");
316+	if(isinvalid(name)) return error_asm("Macro is invalid");
317+	if(findmacro(name)) return error_asm("Macro is duplicate");
318+	m = &macros[macro_len++];
319+	m->name = push(name, 0);
320+	m->content = dictnext;
321+	while(f && fread(&c, 1, 1, f) && c != '{')
322+		if(c == 0xa) ctx->line += 1;
323+	while(f && fread(&c, 1, 1, f) && c != '}') {
324+		if(c == 0xa) ctx->line += 1;
325+		if(c == '%') return 0;
326+		if(c == '(')
327+			walkcomment(f, ctx);
328+		else
329+			*dictnext++ = c;
330+	}
331+	*dictnext++ = 0;
332+	return 1;
333+}
334+
335+static int
336+makelabel(char *name, int setscope, Context *ctx)
337+{
338+	Item *l;
339+	if(name[0] == '&')
340+		name = join(scope, '/', name + 1);
341+	if(labels_len >= 0x400) return error_asm("Labels limit exceeded");
342+	if(isinvalid(name)) return error_asm("Label is invalid");
343+	if(findlabel(name)) return error_asm("Label is duplicate");
344+	l = &labels[labels_len++];
345+	l->name = push(name, 0);
346+	l->addr = ptr;
347+	l->refs = 0;
348+	if(setscope) {
349+		int i = 0;
350+		while(name[i] != '/' && i < 0x3e && (scope[i] = name[i]))
351+			i++;
352+		scope[i] = '\0';
353+	}
354+	return 1;
355+}
356+
357+static int
358+makeref(char *label, char rune, Uint16 addr)
359+{
360+	Item *r;
361+	if(refs_len >= 0x1000) return error_top("References limit exceeded", label);
362+	r = &refs[refs_len++];
363 	if(label[0] == '{') {
364-		p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
365-		scpy(makelambda(p.lambda_count++), r->name, 0x40);
366+		lambda_stack[lambda_ptr++] = lambda_len;
367+		r->name = push(makelambda(lambda_len++), 0);
368 	} else if(label[0] == '&' || label[0] == '/') {
369-		if(!sublabel(subw, scope, label + 1))
370-			return error_asm("Invalid sublabel", label);
371-		scpy(subw, r->name, 0x40);
372-	} else {
373-		int pos = spos(label, '/');
374-		if(pos > 0) {
375-			Label *l;
376-			if((l = findlabel(scpy(label, parent, pos))))
377-				l->refs++;
378-		}
379-		scpy(label, r->name, 0x40);
380-	}
381+		r->name = join(scope, '/', label + 1);
382+	} else
383+		r->name = push(label, 0);
384 	r->rune = rune;
385 	r->addr = addr;
386 	return 1;
387 }
388 
389 static int
390-writebyte(Uint8 b)
391+writepad(char *w)
392 {
393-	if(p.ptr < TRIM)
394-		return error_asm("Writing in zero-page", "");
395-	else if(p.ptr > 0xffff)
396-		return error_asm("Writing after the end of RAM", "");
397-	else if(p.ptr < p.length)
398-		return error_asm("Memory overwrite", "");
399-	p.data[p.ptr++] = b;
400-	p.length = p.ptr;
401-	return 1;
402+	Item *l;
403+	int rel = w[0] == '$' ? ptr : 0;
404+	if(ishex(w + 1)) {
405+		ptr = shex(w + 1) + rel;
406+		return 1;
407+	}
408+	if((l = findlabel(w + 1))) {
409+		ptr = l->addr + rel;
410+		return 1;
411+	}
412+	return 0;
413 }
414 
415 static int
416-writeopcode(char *w)
417+writebyte(Uint8 b, Context *ctx)
418 {
419-	return writebyte(findopcode(w));
420+	if(ptr < PAGE)
421+		return error_asm("Writing in zero-page");
422+	else if(ptr >= 0x10000)
423+		return error_asm("Writing outside memory");
424+	else if(ptr < length)
425+		return error_asm("Writing rewind");
426+	data[ptr++] = b;
427+	if(b)
428+		length = ptr;
429+	return 1;
430 }
431 
432 static int
433-writeshort(Uint16 s, int lit)
434+writehex(char *w, Context *ctx)
435 {
436-	if(lit)
437-		if(!writebyte(findopcode("LIT2"))) return 0;
438-	return writebyte(s >> 8) && writebyte(s & 0xff);
439+	if(*w == '#')
440+		writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx);
441+	if(!w[2])
442+		return writebyte(shex(w), ctx);
443+	else if(!w[4])
444+		return writeshort(shex(w));
445+	else
446+		return 0;
447 }
448 
449 static int
450-writelitbyte(Uint8 b)
451+writestring(char *w, Context *ctx)
452 {
453-	return writebyte(findopcode("LIT")) && writebyte(b);
454+	char c;
455+	while((c = *(w++)))
456+		if(!writebyte(c, ctx)) return 0;
457+	return 1;
458 }
459 
460 static int
461-doinclude(char *filename)
462+assemble(char *filename)
463 {
464 	FILE *f;
465-	char w[0x40];
466-	if(!(f = fopen(setlocation(filename), "r")))
467-		return error_asm("Include missing", filename);
468-	while(fscanf(f, "%63s", w) == 1)
469-		if(!parse(w, f))
470-			return error_asm("Unknown token", w);
471+	int res = 0;
472+	Context ctx;
473+	ctx.line = 0;
474+	ctx.path = push(filename, 0);
475+	if(!(f = fopen(filename, "r")))
476+		return error_top("Invalid source", filename);
477+	res = walkfile(f, &ctx);
478 	fclose(f);
479-	return 1;
480+	return res;
481 }
482 
483 static int
484-parse(char *w, FILE *f)
485+parse(char *w, FILE *f, Context *ctx)
486 {
487-	int i;
488-	char word[0x40], subw[0x40], c;
489-	Label *l;
490-	Macro *m;
491-	if(slen(w) >= 63)
492-		return error_asm("Invalid token", w);
493+	Item *m;
494 	switch(w[0]) {
495-	case '(': /* comment */
496-		if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
497-		i = 1; /* track nested comment depth */
498-		while(fscanf(f, "%63s", word) == 1) {
499-			if(slen(word) != 1)
500-				continue;
501-			else if(word[0] == '(')
502-				i++;
503-			else if(word[0] == ')' && --i < 1)
504-				break;
505-		}
506-		break;
507-	case '~': /* include */
508-		if(!doinclude(w + 1))
509-			return error_asm("Invalid include", w);
510-		break;
511-	case '%': /* macro */
512-		if(!makemacro(w + 1, f))
513-			return error_asm("Invalid macro", w);
514-		break;
515-	case '|': /* pad-absolute */
516-		if(sihx(w + 1))
517-			p.ptr = shex(w + 1);
518-		else if(w[1] == '&') {
519-			if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
520-				return error_asm("Invalid sublabel", w);
521-			p.ptr = l->addr;
522-		} else {
523-			if(!(l = findlabel(w + 1)))
524-				return error_asm("Invalid label", w);
525-			p.ptr = l->addr;
526-		}
527-		break;
528-	case '$': /* pad-relative */
529-		if(sihx(w + 1))
530-			p.ptr += shex(w + 1);
531-		else if(w[1] == '&') {
532-			if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
533-				return error_asm("Invalid sublabel", w);
534-			p.ptr += l->addr;
535-		} else {
536-			if(!(l = findlabel(w + 1)))
537-				return error_asm("Invalid label", w);
538-			p.ptr += l->addr;
539-		}
540-		break;
541-	case '@': /* label */
542-		if(!makelabel(w + 1))
543-			return error_asm("Invalid label", w);
544-		i = 0;
545-		while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1]))
546-			i++;
547-		p.scope[i] = '\0';
548-		break;
549-	case '&': /* sublabel */
550-		if(!sublabel(subw, p.scope, w + 1) || !makelabel(subw))
551-			return error_asm("Invalid sublabel", w);
552-		break;
553-	case '#': /* literals hex */
554-		if(sihx(w + 1) && slen(w) == 3)
555-			return writelitbyte(shex(w + 1));
556-		else if(sihx(w + 1) && slen(w) == 5)
557-			return writeshort(shex(w + 1), 1);
558-		else
559-			return error_asm("Invalid hex literal", w);
560-		break;
561-	case '_': /* raw byte relative */
562-		return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
563-	case ',': /* literal byte relative */
564-		return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
565-	case '-': /* raw byte absolute */
566-		return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
567-	case '.': /* literal byte zero-page */
568-		return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
569-	case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1);
570-	case '=': /* raw short absolute */
571-		return makereference(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0);
572-	case ';': /* literal short absolute */
573-		return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1);
574-	case '?': /* JCI */
575-		return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0);
576-	case '!': /* JMI */
577-		return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0);
578-	case '"': /* raw string */
579-		i = 0;
580-		while((c = w[++i]))
581-			if(!writebyte(c)) return 0;
582-		break;
583-	case '}': /* lambda end */
584-		if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
585-			return error_asm("Invalid label", w);
586-		break;
587+	case '(': return !walkcomment(f, ctx) ? error_asm("Invalid comment") : 1;
588+	case '%': return !makemacro(w + 1, f, ctx) ? error_asm("Invalid macro") : 1;
589+	case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1;
590+	case '&': return !makelabel(w, 0, ctx) ? error_asm("Invalid sublabel") : 1;
591+	case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx) ? error_asm("Invalid label") : 1;
592+	case '#': return !ishex(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1;
593+	case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
594+	case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
595+	case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
596+	case '.': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
597+	case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
598+	case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff);
599+	case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
600+	case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
601+	case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
602+	case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1;
603+	case '~': return !assemble(w + 1) ? error_asm("Invalid include") : 1;
604+	case '$':
605+	case '|': return !writepad(w) ? error_asm("Invalid padding") : 1;
606 	case '[':
607-	case ']':
608-		if(slen(w) == 1) break; /* else fallthrough */
609-	default:
610-		/* opcode */
611-		if(findopcode(w) || scmp(w, "BRK", 4))
612-			return writeopcode(w);
613-		/* raw byte */
614-		else if(sihx(w) && slen(w) == 2)
615-			return writebyte(shex(w));
616-		/* raw short */
617-		else if(sihx(w) && slen(w) == 4)
618-			return writeshort(shex(w), 0);
619-		/* macro */
620-		else if((m = findmacro(w))) {
621-			for(i = 0; i < m->len; i++)
622-				if(!parse(m->items[i], f))
623-					return 0;
624-			return 1;
625-		} else
626-			return makereference(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0);
627+	case ']': return 1;
628 	}
629-	return 1;
630+	if(ishex(w)) return writehex(w, ctx);
631+	if(isopc(w)) return writebyte(findopcode(w), ctx);
632+	if((m = findmacro(w))) return walkmacro(m, ctx);
633+	return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff);
634 }
635 
636 static int
637 resolve(void)
638 {
639-	Label *l;
640-	int i;
641-	Uint16 a;
642-	for(i = 0; i < p.refs_len; i++) {
643-		Reference *r = &p.refs[i];
644+	int i, rel;
645+	if(!length) return error_top("Assembly", "Output rom is empty.");
646+	for(i = 0; i < refs_len; i++) {
647+		Item *r = &refs[i], *l = findlabel(r->name);
648+		Uint8 *rom = data + r->addr;
649+		if(!l) return 0;
650 		switch(r->rune) {
651 		case '_':
652 		case ',':
653-			if(!(l = findlabel(r->name)))
654-				return error_top("Unknown relative reference", r->name);
655-			p.data[r->addr] = (Sint8)(l->addr - r->addr - 2);
656-			if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2))
657-				return error_asm("Relative reference is too far", r->name);
658-			l->refs++;
659+			*rom = rel = l->addr - r->addr - 2;
660+			if((Sint8)data[r->addr] != rel)
661+				return error_top("Relative reference is too far", r->name);
662 			break;
663 		case '-':
664 		case '.':
665-			if(!(l = findlabel(r->name)))
666-				return error_top("Unknown zero-page reference", r->name);
667-			p.data[r->addr] = l->addr & 0xff;
668-			l->refs++;
669+			*rom = l->addr;
670 			break;
671 		case ':':
672 		case '=':
673 		case ';':
674-			if(!(l = findlabel(r->name)))
675-				return error_top("Unknown absolute reference", r->name);
676-			p.data[r->addr] = l->addr >> 0x8;
677-			p.data[r->addr + 1] = l->addr & 0xff;
678-			l->refs++;
679+			*rom++ = l->addr >> 8, *rom = l->addr;
680 			break;
681 		case '?':
682 		case '!':
683 		default:
684-			if(!(l = findlabel(r->name)))
685-				return error_top("Unknown absolute reference", r->name);
686-			a = l->addr - r->addr - 2;
687-			p.data[r->addr] = a >> 0x8;
688-			p.data[r->addr + 1] = a & 0xff;
689-			l->refs++;
690+			rel = l->addr - r->addr - 2;
691+			*rom++ = rel >> 8, *rom = rel;
692 			break;
693 		}
694+		l->refs++;
695 	}
696 	return 1;
697 }
698 
699 static int
700-assemble(FILE *f)
701-{
702-	char w[0x40];
703-	p.ptr = 0x100;
704-	scpy("on-reset", p.scope, 0x40);
705-	while(fscanf(f, "%62s", w) == 1)
706-		if(slen(w) > 0x3d || !parse(w, f))
707-			return error_asm("Invalid token", w);
708-	return resolve();
709-}
710-
711-static void
712-review(char *filename)
713+build(char *rompath)
714 {
715 	int i;
716-	for(i = 0; i < p.label_len; i++)
717-		if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
718-			continue; /* Ignore capitalized labels(devices) */
719-		else if(!p.labels[i].refs)
720-			fprintf(stdout, "-- Unused label: %s\n", p.labels[i].name);
721+	FILE *dst, *dstsym;
722+	char *sympath = join(rompath, '.', "sym");
723+	/* rom */
724+	if(!(dst = fopen(rompath, "wb")))
725+		return !error_top("Invalid output file", rompath);
726+	for(i = 0; i < labels_len; i++)
727+		if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
728+			fprintf(stdout, "-- Unused label: %s\n", labels[i].name);
729+	fwrite(data + PAGE, length - PAGE, 1, dst);
730 	fprintf(stdout,
731 		"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
732-		filename,
733-		p.length - TRIM,
734-		(p.length - TRIM) / 652.80,
735-		p.label_len,
736-		p.macro_len);
737-}
738-
739-static void
740-writesym(char *filename)
741-{
742-	int i;
743-	char symdst[0x60];
744-	FILE *fp;
745-	if(slen(filename) > 0x60 - 5)
746-		return;
747-	fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
748-	if(fp != NULL) {
749-		for(i = 0; i < p.label_len; i++) {
750-			Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff;
751-			fwrite(&hb, 1, 1, fp);
752-			fwrite(&lb, 1, 1, fp);
753-			fwrite(p.labels[i].name, slen(p.labels[i].name) + 1, 1, fp);
754-		}
755+		rompath,
756+		length - PAGE,
757+		(length - PAGE) / 652.80,
758+		labels_len,
759+		macro_len);
760+	/* sym */
761+	if(!(dstsym = fopen(sympath, "w")))
762+		return !error_top("Invalid symbols file", sympath);
763+	for(i = 0; i < labels_len; i++) {
764+		Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
765+		char c, d = 0, *name = labels[i].name;
766+		fwrite(&hb, 1, 1, dstsym);
767+		fwrite(&lb, 1, 1, dstsym);
768+		while((c = *name++)) fwrite(&c, 1, 1, dstsym);
769+		fwrite(&d, 1, 1, dstsym);
770 	}
771-	fclose(fp);
772+	fclose(dst), fclose(dstsym);
773+	return 1;
774 }
775 
776 int
777 main(int argc, char *argv[])
778 {
779-	FILE *src, *dst;
780-	if(argc == 1)
781-		return error_top("usage", "uxnasm [-v] input.tal output.rom");
782-	if(argv[1][0] == '-' && argv[1][1] == 'v')
783-		return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 7 Mar 2024.\n");
784-	if(!(src = fopen(setlocation(argv[1]), "r")))
785-		return !error_top("Invalid input", argv[1]);
786-	p.entry = argv[1];
787-	if(!assemble(src))
788-		return !error_top("Assembly", "Failed to assemble rom.");
789-	if(!(dst = fopen(argv[2], "wb")))
790-		return !error_top("Invalid Output", argv[2]);
791-	if(p.length <= TRIM)
792-		return !error_top("Assembly", "Output rom is empty.");
793-	fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
794-	if(!scmp(argv[2], "-", 2)) {
795-		review(argv[2]);
796-		writesym(argv[2]);
797-	}
798+	ptr = PAGE;
799+	scpy("on-reset", scope, 0x40);
800+	if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom");
801+	if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 28 Mar 2024.\n");
802+	if(!assemble(argv[1]) || !length) return !error_top("Assembly", "Failed to assemble rom.");
803+	if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols.");
804+	if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom.");
805 	return 0;
806-}
807+}