Compare commits

...

26 Commits

Author SHA1 Message Date
Devine Lu Linvega c64fe94038 (uxnasm) Walk across comments in macros 2024-03-26 13:53:23 -07:00
Devine Lu Linvega 0ae8812680 (uxnasm) Read characters into char buffers 2024-03-26 13:45:08 -07:00
Devine Lu Linvega 832ba26e30 Improved comment walking 2024-03-26 13:41:52 -07:00
Devine Lu Linvega a197c24b2a Abstracted walk-comment 2024-03-26 13:35:37 -07:00
Devine Lu Linvega bcdd08bc4e (uxnasm) makelabel can set scope 2024-03-26 13:31:11 -07:00
Devine Lu Linvega fafc4e1734 (uxnasm) single-line handlers 2024-03-26 13:11:07 -07:00
Devine Lu Linvega ad6528be66 (uxnasm) Clearer error for subroutine ref error 2024-03-26 13:03:47 -07:00
Devine Lu Linvega a2ed2226f3 (uxnasm) Optimized writehex 2024-03-26 12:53:06 -07:00
Devine Lu Linvega a6fb9a22c5 (uxnasm) Abstracted writehex 2024-03-26 12:40:43 -07:00
Devine Lu Linvega f1fa4f6ea1 (uxnasm) Removed refinc to parent labels 2024-03-26 11:51:48 -07:00
Devine Lu Linvega 20a11c0f4c (uxnasm) makesub use global scope 2024-03-26 11:47:16 -07:00
Devine Lu Linvega 7819add659 (uxnasm) addref use global scope 2024-03-26 11:46:15 -07:00
Devine Lu Linvega 5d6d9ef4b1 (uxnasm) Program length should be int 2024-03-26 11:38:32 -07:00
Devine Lu Linvega e0c907f088 (uxnasm) Keep actual copy of source name 2024-03-26 11:33:25 -07:00
Devine Lu Linvega 5a109cb203 (uxnasm) Lambda string is public 2024-03-26 11:27:45 -07:00
Devine Lu Linvega 5c2cd9de04 (uxnasm) Make scope global 2024-03-26 11:22:02 -07:00
Devine Lu Linvega db25349114 (uxnasm) Removed setlocation 2024-03-26 11:19:01 -07:00
Devine Lu Linvega b1ec78a806 (uxnasm) Move setup in main 2024-03-26 11:16:36 -07:00
Devine Lu Linvega 26c8d5f6e7 Removed redundant primitive 2024-03-26 11:14:00 -07:00
Devine Lu Linvega 9f7391395f makelabel detects sublabels 2024-03-26 11:10:08 -07:00
Devine Lu Linvega 683eb6b859 (uxnasm)Catch overflow proper 2024-03-26 11:02:11 -07:00
Devine Lu Linvega c6674605fd (uxnasm) Merge tokenizers 2024-03-26 10:58:55 -07:00
Devine Lu Linvega e871d8dc0c Include uses tokenizer 2024-03-26 10:49:29 -07:00
Devine Lu Linvega 553e559e4e (uxnasm) Abstracted tokenizer 2024-03-26 10:45:25 -07:00
Devine Lu Linvega e632579243 (uxnasm) Minor optimizations 2024-03-26 10:34:20 -07:00
Devine Lu Linvega ac31bea4bb (uxnasm) Use cndx for runic 2024-03-26 10:01:54 -07:00
2 changed files with 195 additions and 241 deletions

View File

@ -1,24 +1,26 @@
( init )
%emit ( byte -- ) { #18 DEO }
|0100 @program
#1234 SWP
#010e DEO
#010f DEO
BRK
#1234 #5678 SWP2
BRK
;hello-word
&while
( send ) LDAk #18 DEO
( loop ) INC2 LDAk ,&while JCN
( send ) LDAk emit
( loop ) INC2 LDAk ?&while
POP2
#010f DEO
BRK
BRK
@program/extend BRK
@hello-word "Hello 20 "World! 00

View File

@ -1,7 +1,7 @@
#include <stdio.h>
/*
Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick
Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -34,17 +34,16 @@ typedef struct {
} Reference;
typedef struct {
int ptr, length;
Uint8 data[LENGTH];
Uint8 lambda_stack[0x100], lambda_ptr, lambda_len;
Uint16 ptr, line, length, label_len, macro_len, refs_len;
char scope[0x40], lambda_name[0x05], *location;
Uint16 line, label_len, macro_len, refs_len;
Label labels[0x400];
Macro macros[0x100];
Reference refs[0x1000];
} Program;
char sublabel[0x40];
char token[0x40];
char source[0x40], token[0x40], scope[0x40], sublabel[0x40], lambda[0x05];
Program p;
@ -60,18 +59,18 @@ static char ops[][4] = {
static char *runes = "|$@&,_.-;=!?#\"%~";
static char *hexad = "0123456789abcdef";
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 */
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 */
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 */
static int slen(char *s) { int i = 0; while(s[i]) i++; return i; } /* string length */
static int spos(char *s, char c) { Uint8 i = 0, j; while((j = s[i++])) if(j == c) return i; return -1; } /* character position */
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 */
static char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
static int cndx(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; } /* chr in str */
static int sihx(char *s) { char c; while((c = *s++)) if(cndx(hexad, c) < 0) return 0; return 1; } /* str is hex */
static int shex(char *s) { int n = 0; char c; while((c = *s++)) { n = n << 4, n |= cndx(hexad, c); } return n; } /* str to num */
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 */
static int slen(char *s) { int i = 0; while(s[i]) i++; return i; } /* str length */
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 */
static char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* str cat */
/* clang-format on */
static int parse(char *w, FILE *f);
static char *makesublabel(char *src, char *scope, char *name);
static char *makesublabel(char *src, char *name);
static int
error_top(const char *name, const char *msg)
@ -83,18 +82,10 @@ error_top(const char *name, const char *msg)
static int
error_asm(const char *name)
{
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, p.scope, p.location, p.line);
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, source, p.line);
return 0;
}
static char *
setlocation(char *name)
{
p.location = name;
p.line = 0;
return name;
}
static Macro *
findmacro(char *name)
{
@ -110,7 +101,7 @@ findlabel(char *name)
{
int i;
if(name[0] == '&')
name = makesublabel(sublabel, p.scope, name + 1);
name = makesublabel(sublabel, name + 1);
for(i = 0; i < p.label_len; i++)
if(scmp(p.labels[i].name, name, 0x40))
return &p.labels[i];
@ -143,21 +134,27 @@ findopcode(char *s)
return 0;
}
static int
isrune(char c)
{
char cc, *r = runes;
while((cc = *r++))
if(c == cc) return 1;
return 0;
}
static int
isopcode(char *s)
{
return findopcode(s) || scmp(s, "BRK", 4);
}
static int
walkcomment(char *w, FILE *f)
{
int depth = 1;
char c;
if(slen(w) == 1)
while(fread(&c, 1, 1, f)) {
if(c == '(')
depth++;
else if(c == ')' && --depth < 1)
return 1;
}
return 0;
}
static int
makemacro(char *name, FILE *f)
{
@ -173,44 +170,53 @@ makemacro(char *name, FILE *f)
while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue;
if(word[0] == '}') break;
if(word[0] == '%')
return error_asm("Macro error");
if(m->len >= 0x40)
return error_asm("Macro size exceeded");
if(word[0] == '%') return error_asm("Macro error");
if(m->len >= 0x40) return error_asm("Macro size exceeded");
if(word[0] == '(') {
walkcomment(word, f);
continue;
}
scpy(word, m->items[m->len++], 0x40);
}
return 1;
}
static int
makelabel(char *name)
makelabel(char *name, int setscope)
{
Label *l;
if(name[0] == '&')
name = makesublabel(sublabel, name + 1);
if(!slen(name)) return error_asm("Label is empty");
if(findlabel(name)) return error_asm("Label is duplicate");
if(sihx(name)) return error_asm("Label is hex number");
if(isopcode(name)) return error_asm("Label is opcode");
if(isrune(name[0])) return error_asm("Label name is runic");
if(cndx(runes, name[0]) >= 0) return error_asm("Label name is runic");
if(p.label_len == 0x400) return error_asm("Labels limit exceeded");
l = &p.labels[p.label_len++];
l->addr = p.ptr;
l->refs = 0;
scpy(name, l->name, 0x40);
if(setscope) {
int i = 0;
while(name[i] != '/' && i < 0x3e && (scope[i] = name[i])) i++;
scope[i] = '\0';
}
return 1;
}
static char *
makelambda(int id)
{
p.lambda_name[0] = (char)0xce;
p.lambda_name[1] = (char)0xbb;
p.lambda_name[2] = hexad[id >> 0x4];
p.lambda_name[3] = hexad[id & 0xf];
return p.lambda_name;
lambda[0] = (char)0xce;
lambda[1] = (char)0xbb;
lambda[2] = hexad[id >> 0x4];
lambda[3] = hexad[id & 0xf];
return lambda;
}
static char *
makesublabel(char *buf, char *scope, char *name)
makesublabel(char *buf, char *name)
{
if(slen(scope) + slen(name) >= 0x3f) {
error_asm("Sublabel length too long");
@ -226,18 +232,16 @@ makepad(char *w)
int rel = w[0] == '$' ? p.ptr : 0;
if(sihx(w + 1))
p.ptr = shex(w + 1) + rel;
else {
if(!(l = findlabel(w + 1)))
return error_asm("Invalid label");
else if((l = findlabel(w + 1)))
p.ptr = l->addr + rel;
}
else
return error_asm("Invalid padding");
return 1;
}
static int
addref(char *scope, char *label, char rune, Uint16 addr)
addref(char *label, char rune, Uint16 addr)
{
char parent[0x40];
Reference *r;
if(p.refs_len >= 0x1000)
return error_asm("References limit exceeded");
@ -246,17 +250,10 @@ addref(char *scope, char *label, char rune, Uint16 addr)
p.lambda_stack[p.lambda_ptr++] = p.lambda_len;
scpy(makelambda(p.lambda_len++), r->name, 0x40);
} else if(label[0] == '&' || label[0] == '/') {
if(!makesublabel(r->name, scope, label + 1))
if(!makesublabel(r->name, label + 1))
return error_asm("Invalid sublabel");
} else {
int pos = spos(label, '/');
if(pos > 0) {
Label *l;
if((l = findlabel(scpy(label, parent, pos))))
l->refs++;
}
} else
scpy(label, r->name, 0x40);
}
r->rune = rune;
r->addr = addr;
return 1;
@ -267,7 +264,7 @@ writebyte(Uint8 b)
{
if(p.ptr < TRIM)
return error_asm("Writing in zero-page");
else if(p.ptr == 0xffff)
else if(p.ptr >= 0x10000)
return error_asm("Writing outside memory");
else if(p.ptr < p.length)
return error_asm("Writing rewind");
@ -277,186 +274,30 @@ writebyte(Uint8 b)
}
static int
writeshort(Uint16 s, int lit)
writeshort(Uint16 s)
{
return (lit ? writebyte(findopcode("LIT2")) : 1) && writebyte(s >> 8) && writebyte(s & 0xff);
return writebyte(s >> 8) && writebyte(s & 0xff);
}
static int
doinclude(char *filename)
writehex(char *w)
{
FILE *f;
char w[0x40];
if(!(f = fopen(setlocation(filename), "r")))
return error_top("Include missing", filename);
while(fscanf(f, "%63s", w) == 1)
if(!parse(w, f))
return error_top("Unknown token", w);
fclose(f);
return 1;
if(*w == '#')
writebyte(findopcode("LIT") | (slen(++w) > 2) << 5);
if(slen(w) == 2)
return writebyte(shex(w));
else if(slen(w) == 4)
return writeshort(shex(w));
else
return 0;
}
static int
parse(char *w, FILE *f)
tokenize(FILE *f)
{
int i;
char word[0x40], subw[0x40], c;
Macro *m;
switch(w[0]) {
case '(': /* comment */
if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
i = 1; /* track nested comment depth */
while(fscanf(f, "%63s", word) == 1) {
if(slen(word) != 1)
continue;
else if(word[0] == '(')
i++;
else if(word[0] == ')' && --i < 1)
break;
}
break;
case '~': /* include */
if(!doinclude(w + 1))
return error_asm("Invalid include");
break;
case '%': /* macro */
if(!makemacro(w + 1, f))
return error_asm("Invalid macro");
break;
case '$': /* pad-relative */
case '|': /* pad-absolute */
if(!makepad(w))
return error_asm("Invalid padding");
break;
case '@': /* label */
if(!makelabel(w + 1))
return error_asm("Invalid label");
i = 0;
while(w[i + 1] != '/' && i < 0x3e && (p.scope[i] = w[i + 1]))
i++;
p.scope[i] = '\0';
break;
case '&': /* sublabel */
if(!makesublabel(subw, p.scope, w + 1) || !makelabel(subw))
return error_asm("Invalid sublabel");
break;
case '#': /* literals hex */
if(sihx(w + 1) && slen(w) == 3)
return writebyte(findopcode("LIT")) && writebyte(shex(w + 1));
else if(sihx(w + 1) && slen(w) == 5)
return writeshort(shex(w + 1), 1);
else
return error_asm("Invalid hex literal");
break;
case '_': /* raw byte relative */
return addref(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
case ',': /* literal byte relative */
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
case '-': /* raw byte absolute */
return addref(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
case '.': /* literal byte zero-page */
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1);
case '=': /* raw short absolute */
return addref(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0);
case ';': /* literal short absolute */
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1);
case '?': /* JCI */
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0);
case '!': /* JMI */
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0);
case '"': /* raw string */
i = 0;
while((c = w[++i]))
if(!writebyte(c)) return 0;
break;
case '}': /* lambda end */
if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
return error_asm("Invalid label");
break;
case '[':
case ']':
if(slen(w) == 1) break; /* else fallthrough */
default:
/* opcode */
if(isopcode(w))
return writebyte(findopcode(w));
/* raw byte */
else if(sihx(w) && slen(w) == 2)
return writebyte(shex(w));
/* raw short */
else if(sihx(w) && slen(w) == 4)
return writeshort(shex(w), 0);
/* macro */
else if((m = findmacro(w))) {
for(i = 0; i < m->len; i++)
if(!parse(m->items[i], f))
return 0;
return 1;
} else
return addref(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0);
}
return 1;
}
static int
resolve(void)
{
Label *l;
int i;
Uint16 a;
for(i = 0; i < p.refs_len; i++) {
Reference *r = &p.refs[i];
switch(r->rune) {
case '_':
case ',':
if(!(l = findlabel(r->name)))
return error_top("Unknown relative reference", r->name);
p.data[r->addr] = (Sint8)(l->addr - r->addr - 2);
if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2))
return error_top("Relative reference is too far", r->name);
l->refs++;
break;
case '-':
case '.':
if(!(l = findlabel(r->name)))
return error_top("Unknown zero-page reference", r->name);
p.data[r->addr] = l->addr & 0xff;
l->refs++;
break;
case ':':
case '=':
case ';':
if(!(l = findlabel(r->name)))
return error_top("Unknown absolute reference", r->name);
p.data[r->addr] = l->addr >> 0x8;
p.data[r->addr + 1] = l->addr & 0xff;
l->refs++;
break;
case '?':
case '!':
default:
if(!(l = findlabel(r->name)))
return error_top("Unknown absolute reference", r->name);
a = l->addr - r->addr - 2;
p.data[r->addr] = a >> 0x8;
p.data[r->addr + 1] = a & 0xff;
l->refs++;
break;
}
}
return 1;
}
static int
assemble(FILE *f)
{
unsigned int buf;
char c;
char *cptr = token;
p.ptr = 0x100;
scpy("on-reset", p.scope, 0x40);
while(fread(&buf, 1, 1, f)) {
char c = (char)buf;
while(fread(&c, 1, 1, f)) {
if(c < 0x21) {
*cptr++ = 0x00;
if(c == 0x0a)
@ -470,7 +311,117 @@ assemble(FILE *f)
else
return error_asm("Token too long");
}
return resolve();
return 1;
}
static int
makeinclude(char *filename)
{
FILE *f;
int res = 0;
if(!(f = fopen(filename, "r")))
return error_top("Invalid source", filename);
scpy(filename, source, 0x40);
p.line = 0;
res = tokenize(f);
fclose(f);
return res;
}
static int
parse(char *w, FILE *f)
{
int i;
char c;
Macro *m;
switch(w[0]) {
case '(': return !walkcomment(w, f) ? error_asm("Invalid comment") : 1;
case '~': return !makeinclude(w + 1) ? error_asm("Invalid include") : 1;
case '%': return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1;
case '@': return !makelabel(w + 1, 1) ? error_asm("Invalid label") : 1;
case '&': return !makelabel(w, 0) ? error_asm("Invalid sublabel") : 1;
case '#': return !sihx(w + 1) || !writehex(w) ? error_asm("Invalid hexadecimal") : 1;
case '_': return addref(w + 1, w[0], p.ptr) && writebyte(0xff);
case ',': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
case '-': return addref(w + 1, w[0], p.ptr) && writebyte(0xff);
case '.': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
case '=': return addref(w + 1, w[0], p.ptr) && writeshort(0xffff);
case ';': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT2")) && writeshort(0xffff);
case '?': return addref(w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff);
case '!': return addref(w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff);
case '}': return !makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]), 0) ? error_asm("Invalid label") : 1;
case '$':
case '|': return !makepad(w) ? error_asm("Invalid padding") : 1;
case '[':
case ']':
if(slen(w) == 1) break; /* else fallthrough */
case '"': /* raw string */
while((c = *(++w)))
if(!writebyte(c)) return 0;
break;
default:
if(sihx(w))
return writehex(w);
else if(isopcode(w))
return writebyte(findopcode(w));
else if((m = findmacro(w))) {
for(i = 0; i < m->len; i++)
if(!parse(m->items[i], f))
return 0;
return 1;
} else
return addref(w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff);
}
return 1;
}
static int
resolve(void)
{
Label *l;
int i;
Uint16 a;
for(i = 0; i < p.refs_len; i++) {
Reference *r = &p.refs[i];
Uint8 *rom = p.data + r->addr;
switch(r->rune) {
case '_':
case ',':
if(!(l = findlabel(r->name)))
return error_top("Unknown relative reference", r->name);
*rom = (Sint8)(l->addr - r->addr - 2);
if((Sint8)p.data[r->addr] != (l->addr - r->addr - 2))
return error_top("Relative reference is too far", r->name);
l->refs++;
break;
case '-':
case '.':
if(!(l = findlabel(r->name)))
return error_top("Unknown zero-page reference", r->name);
*rom = l->addr & 0xff;
l->refs++;
break;
case ':':
case '=':
case ';':
if(!(l = findlabel(r->name)))
return error_top("Unknown absolute reference", r->name);
*rom++ = l->addr >> 0x8, *rom = l->addr & 0xff;
l->refs++;
break;
case '?':
case '!':
default:
if(!(l = findlabel(r->name)))
return error_top("Unknown subroutine reference", r->name);
a = l->addr - r->addr - 2;
*rom++ = a >> 0x8, *rom = a & 0xff;
l->refs++;
break;
}
}
return 1;
}
static void
@ -512,11 +463,12 @@ writesym(char *filename)
int
main(int argc, char *argv[])
{
FILE *src, *dst;
FILE *dst;
p.ptr = 0x100;
scpy("on-reset", scope, 0x40);
if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom");
if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 25 Mar 2024.\n");
if(!(src = fopen(setlocation(argv[1]), "r"))) return !error_top("Invalid input", argv[1]);
if(!assemble(src)) return !error_top("Assembly", "Failed to assemble rom.");
if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 26 Mar 2024.\n");
if(!makeinclude(argv[1]) || !resolve()) return !error_top("Assembly", "Failed to assemble rom.");
if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]);
if(p.length <= TRIM) return !error_top("Assembly", "Output rom is empty.");
review(argv[2]);