Compare commits

...

6 Commits

Author SHA1 Message Date
Devine Lu Linvega bdf45bbdaa (uxnasm) Housekeeping 2024-03-25 15:27:45 -07:00
Devine Lu Linvega acf79df9d5 (uxnasm) Keep global token for error 2024-03-25 15:20:43 -07:00
Devine Lu Linvega 858b38b967 (uxnasm) Optimizations 2024-03-25 15:09:56 -07:00
Devine Lu Linvega 3777f30281 (uxnasm) Removed writelitbyte 2024-03-25 14:58:45 -07:00
Devine Lu Linvega 0c13a40916 Abstracted isopcode 2024-03-25 14:48:37 -07:00
Devine Lu Linvega 6fdc6e6791 (uxnasm) Improved lambda symbols 2024-03-25 14:21:16 -07:00
1 changed files with 122 additions and 145 deletions

View File

@ -35,15 +35,16 @@ typedef struct {
typedef struct {
Uint8 data[LENGTH];
Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
char scope[0x40], lambda[0x10], *location, *entry;
unsigned int ptr, length;
Uint16 label_len, macro_len, refs_len;
Uint8 lambda_stack[0x100], lambda_ptr, lambda_len;
Uint16 ptr, length, label_len, macro_len, refs_len;
char scope[0x40], lambda_name[0x05], *location;
Label labels[0x400];
Macro macros[0x100];
Reference refs[0x1000];
} Program;
char token[0x40];
Program p;
/* clang-format off */
@ -56,6 +57,7 @@ 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 */
@ -77,9 +79,9 @@ error_top(const char *name, const char *msg)
}
static int
error_asm(const char *name, const char *msg)
error_asm(const char *name)
{
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, msg, p.scope, p.location, 123);
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, p.scope, p.location, 123);
return 0;
}
@ -90,16 +92,6 @@ setlocation(char *name)
return name;
}
static char *
sublabel(char *src, char *scope, char *name)
{
if(slen(scope) + slen(name) >= 0x3f) {
error_asm("Sublabel length too long", name);
return NULL;
}
return scat(scat(scpy(scope, src, 0x40), "/"), name);
}
static Macro *
findmacro(char *name)
{
@ -125,19 +117,20 @@ findopcode(char *s)
{
int i;
for(i = 0; i < 0x20; i++) {
int m = 0;
int m = 3;
if(!scmp(ops[i], s, 3))
continue;
if(!i) i |= (1 << 7); /* force keep for LIT */
while(s[3 + m]) {
if(s[3 + m] == '2')
i |= (1 << 5); /* mode: short */
else if(s[3 + m] == 'r')
i |= (1 << 6); /* mode: return */
else if(s[3 + m] == 'k')
i |= (1 << 7); /* mode: keep */
if(!i)
i |= (1 << 7);
while(s[m]) {
if(s[m] == '2')
i |= (1 << 5);
else if(s[m] == 'r')
i |= (1 << 6);
else if(s[m] == 'k')
i |= (1 << 7);
else
return 0; /* failed to match */
return 0;
m++;
}
return i;
@ -145,33 +138,6 @@ findopcode(char *s)
return 0;
}
static int
makemacro(char *name, FILE *f)
{
Macro *m;
char word[0x40];
if(findmacro(name))
return error_asm("Macro duplicate", name);
if(sihx(name) && slen(name) % 2 == 0)
return error_asm("Macro name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error_asm("Macro name is invalid", name);
if(p.macro_len == 0x100)
return error_asm("Macros limit exceeded", name);
m = &p.macros[p.macro_len++];
scpy(name, m->name, 0x40);
while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue;
if(word[0] == '}') break;
if(word[0] == '%')
return error_asm("Macro error", name);
if(m->len >= 0x40)
return error_asm("Macro size exceeded", name);
scpy(word, m->items[m->len++], 0x40);
}
return 1;
}
static int
isrune(char c)
{
@ -181,20 +147,46 @@ isrune(char c)
return 0;
}
static int
isopcode(char *s)
{
return findopcode(s) || scmp(s, "BRK", 4);
}
static int
makemacro(char *name, FILE *f)
{
Macro *m;
char word[0x40];
if(!slen(name)) return error_asm("Macro is empty");
if(findmacro(name)) return error_asm("Macro is duplicate");
if(sihx(name)) return error_asm("Macro is hex number");
if(isopcode(name)) return error_asm("Macro is opcode");
if(p.macro_len == 0x100) return error_asm("Macros limit exceeded");
m = &p.macros[p.macro_len++];
scpy(name, m->name, 0x40);
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");
scpy(word, m->items[m->len++], 0x40);
}
return 1;
}
static int
makelabel(char *name)
{
Label *l;
if(findlabel(name))
return error_asm("Label duplicate", name);
if(sihx(name) && (slen(name) == 2 || slen(name) == 4))
return error_asm("Label name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error_asm("Label name is invalid", name);
if(isrune(name[0]))
return error_asm("Label name is runic", name);
if(p.label_len == 0x400)
return error_asm("Labels limit exceeded", name);
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(p.label_len == 0x400) return error_asm("Labels limit exceeded");
l = &p.labels[p.label_len++];
l->addr = p.ptr;
l->refs = 0;
@ -205,26 +197,37 @@ makelabel(char *name)
static char *
makelambda(int id)
{
scpy("lambda", p.lambda, 0x07);
p.lambda[6] = '0' + (id >> 0x4);
p.lambda[7] = '0' + (id & 0xf);
return p.lambda;
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;
}
static char *
makesublabel(char *src, char *scope, char *name)
{
if(slen(scope) + slen(name) >= 0x3f) {
error_asm("Sublabel length too long");
return NULL;
}
return scat(scat(scpy(scope, src, 0x40), "/"), name);
}
static int
makereference(char *scope, char *label, char rune, Uint16 addr)
addref(char *scope, char *label, char rune, Uint16 addr)
{
char subw[0x40], parent[0x40];
Reference *r;
if(p.refs_len >= 0x1000)
return error_asm("References limit exceeded", label);
return error_asm("References limit exceeded");
r = &p.refs[p.refs_len++];
if(label[0] == '{') {
p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
scpy(makelambda(p.lambda_count++), r->name, 0x40);
p.lambda_stack[p.lambda_ptr++] = p.lambda_len;
scpy(makelambda(p.lambda_len++), r->name, 0x40);
} else if(label[0] == '&' || label[0] == '/') {
if(!sublabel(subw, scope, label + 1))
return error_asm("Invalid sublabel", label);
if(!makesublabel(subw, scope, label + 1))
return error_asm("Invalid sublabel");
scpy(subw, r->name, 0x40);
} else {
int pos = spos(label, '/');
@ -244,34 +247,20 @@ static int
writebyte(Uint8 b)
{
if(p.ptr < TRIM)
return error_asm("Writing in zero-page", "");
else if(p.ptr > 0xffff)
return error_asm("Writing after the end of RAM", "");
return error_asm("Writing in zero-page");
else if(p.ptr == 0xffff)
return error_asm("Writing outside memory");
else if(p.ptr < p.length)
return error_asm("Memory overwrite", "");
return error_asm("Writing rewind");
p.data[p.ptr++] = b;
p.length = p.ptr;
return 1;
}
static int
writeopcode(char *w)
{
return writebyte(findopcode(w));
}
static int
writeshort(Uint16 s, int lit)
{
if(lit)
if(!writebyte(findopcode("LIT2"))) return 0;
return writebyte(s >> 8) && writebyte(s & 0xff);
}
static int
writelitbyte(Uint8 b)
{
return writebyte(findopcode("LIT")) && writebyte(b);
return (lit ? writebyte(findopcode("LIT2")) : 1) && writebyte(s >> 8) && writebyte(s & 0xff);
}
static int
@ -280,10 +269,10 @@ doinclude(char *filename)
FILE *f;
char w[0x40];
if(!(f = fopen(setlocation(filename), "r")))
return error_asm("Include missing", filename);
return error_top("Include missing", filename);
while(fscanf(f, "%63s", w) == 1)
if(!parse(w, f))
return error_asm("Unknown token", w);
return error_top("Unknown token", w);
fclose(f);
return 1;
}
@ -296,7 +285,7 @@ parse(char *w, FILE *f)
Label *l;
Macro *m;
if(slen(w) >= 63)
return error_asm("Invalid token", w);
return error_asm("Invalid token");
switch(w[0]) {
case '(': /* comment */
if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
@ -312,22 +301,22 @@ parse(char *w, FILE *f)
break;
case '~': /* include */
if(!doinclude(w + 1))
return error_asm("Invalid include", w);
return error_asm("Invalid include");
break;
case '%': /* macro */
if(!makemacro(w + 1, f))
return error_asm("Invalid macro", w);
return error_asm("Invalid macro");
break;
case '|': /* pad-absolute */
if(sihx(w + 1))
p.ptr = shex(w + 1);
else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel", w);
if(!makesublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel");
p.ptr = l->addr;
} else {
if(!(l = findlabel(w + 1)))
return error_asm("Invalid label", w);
return error_asm("Invalid label");
p.ptr = l->addr;
}
break;
@ -335,52 +324,52 @@ parse(char *w, FILE *f)
if(sihx(w + 1))
p.ptr += shex(w + 1);
else if(w[1] == '&') {
if(!sublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel", w);
if(!makesublabel(subw, p.scope, w + 2) || !(l = findlabel(subw)))
return error_asm("Invalid sublabel");
p.ptr += l->addr;
} else {
if(!(l = findlabel(w + 1)))
return error_asm("Invalid label", w);
return error_asm("Invalid label");
p.ptr += l->addr;
}
break;
case '@': /* label */
if(!makelabel(w + 1))
return error_asm("Invalid label", w);
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(!sublabel(subw, p.scope, w + 1) || !makelabel(subw))
return error_asm("Invalid sublabel", w);
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 writelitbyte(shex(w + 1));
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", w);
return error_asm("Invalid hex literal");
break;
case '_': /* raw byte relative */
return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
return addref(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
case ',': /* literal byte relative */
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
case '-': /* raw byte absolute */
return makereference(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
return addref(p.scope, w + 1, w[0], p.ptr) && writebyte(0xff);
case '.': /* literal byte zero-page */
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writelitbyte(0xff);
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 makereference(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0);
return addref(p.scope, w + 1, w[0], p.ptr) && writeshort(0xffff, 0);
case ';': /* literal short absolute */
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1);
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writeshort(0xffff, 1);
case '?': /* JCI */
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0);
return addref(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff, 0);
case '!': /* JMI */
return makereference(p.scope, w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff, 0);
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]))
@ -388,15 +377,15 @@ parse(char *w, FILE *f)
break;
case '}': /* lambda end */
if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])))
return error_asm("Invalid label", w);
return error_asm("Invalid label");
break;
case '[':
case ']':
if(slen(w) == 1) break; /* else fallthrough */
default:
/* opcode */
if(findopcode(w) || scmp(w, "BRK", 4))
return writeopcode(w);
if(isopcode(w))
return writebyte(findopcode(w));
/* raw byte */
else if(sihx(w) && slen(w) == 2)
return writebyte(shex(w));
@ -410,7 +399,7 @@ parse(char *w, FILE *f)
return 0;
return 1;
} else
return makereference(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0);
return addref(p.scope, w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff, 0);
}
return 1;
}
@ -430,7 +419,7 @@ resolve(void)
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_asm("Relative reference is too far", r->name);
return error_top("Relative reference is too far", r->name);
l->refs++;
break;
case '-':
@ -467,12 +456,11 @@ resolve(void)
static int
assemble(FILE *f)
{
char w[0x40];
p.ptr = 0x100;
scpy("on-reset", p.scope, 0x40);
while(fscanf(f, "%62s", w) == 1)
if(slen(w) > 0x3d || !parse(w, f))
return error_asm("Invalid token", w);
while(fscanf(f, "%62s", token) == 1)
if(slen(token) > 0x3d || !parse(token, f))
return error_asm("Invalid token");
return resolve();
}
@ -481,9 +469,7 @@ review(char *filename)
{
int i;
for(i = 0; i < p.label_len; i++)
if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
continue; /* Ignore capitalized labels(devices) */
else if(!p.labels[i].refs)
if(p.labels[i].name[0] - 'A' > 25 && !p.labels[i].refs)
fprintf(stdout, "-- Unused label: %s\n", p.labels[i].name);
fprintf(stdout,
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
@ -518,23 +504,14 @@ int
main(int argc, char *argv[])
{
FILE *src, *dst;
if(argc == 1)
return error_top("usage", "uxnasm [-v] input.tal output.rom");
if(argv[1][0] == '-' && argv[1][1] == 'v')
return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 7 Mar 2024.\n");
if(!(src = fopen(setlocation(argv[1]), "r")))
return !error_top("Invalid input", argv[1]);
p.entry = argv[1];
if(!assemble(src))
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.");
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(!(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]);
fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
if(!scmp(argv[2], "-", 2)) {
review(argv[2]);
writesym(argv[2]);
}
writesym(argv[2]);
return 0;
}