mirror of
https://git.sr.ht/~rabbits/uxn
synced 2024-11-19 04:15:10 +00:00
Compare commits
No commits in common. "0098007b635d3d8f8d528e600ff731b09a19ab3b" and "c9f2eb0aad49528c056f55073c8bbd2039c05ccf" have entirely different histories.
0098007b63
...
c9f2eb0aad
2 changed files with 230 additions and 197 deletions
|
@ -1,7 +1,5 @@
|
||||||
( init )
|
( init )
|
||||||
|
|
||||||
|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|
|
||||||
|
|
||||||
%emit ( byte -- ) { #18 DEO }
|
%emit ( byte -- ) { #18 DEO }
|
||||||
|
|
||||||
|0100 @program
|
|0100 @program
|
||||||
|
@ -25,4 +23,4 @@ BRK
|
||||||
|
|
||||||
@program/extend BRK
|
@program/extend BRK
|
||||||
|
|
||||||
@hello-word "Hello 20 "World? 00
|
@hello-word "Hello 20 "World! 00
|
||||||
|
|
427
src/uxnasm.c
427
src/uxnasm.c
|
@ -11,28 +11,42 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
WITH REGARD TO THIS SOFTWARE.
|
WITH REGARD TO THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define PAGE 0x0100
|
#define TRIM 0x0100
|
||||||
|
#define LENGTH 0x10000
|
||||||
|
|
||||||
typedef unsigned char Uint8;
|
typedef unsigned char Uint8;
|
||||||
typedef signed char Sint8;
|
typedef signed char Sint8;
|
||||||
typedef unsigned short Uint16;
|
typedef unsigned short Uint16;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *name, rune, *content;
|
char *name, items[0x40][0x40];
|
||||||
Uint16 addr, refs;
|
Uint8 len;
|
||||||
} Item;
|
} Macro;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int line;
|
char *name;
|
||||||
char *path;
|
Uint16 addr, refs;
|
||||||
} Context;
|
} Label;
|
||||||
|
|
||||||
static int ptr, length;
|
typedef struct {
|
||||||
static char token[0x40], scope[0x40], sublabel[0x80], lambda[0x05];
|
char name[0x40], rune;
|
||||||
static char dict[0x10000], *dictnext = dict;
|
Uint16 addr;
|
||||||
static Uint8 data[0x10000], lambda_stack[0x100], lambda_ptr, lambda_len;
|
} Reference;
|
||||||
static Uint16 label_len, refs_len, macro_len;
|
|
||||||
static Item labels[0x400], refs[0x1000], macros[0x100];
|
typedef struct {
|
||||||
|
int ptr, length;
|
||||||
|
Uint8 data[LENGTH];
|
||||||
|
Uint8 lambda_stack[0x100], lambda_ptr, lambda_len;
|
||||||
|
Uint16 line, label_len, macro_len, refs_len;
|
||||||
|
Label labels[0x400];
|
||||||
|
Macro macros[0x100];
|
||||||
|
Reference refs[0x1000];
|
||||||
|
} Program;
|
||||||
|
|
||||||
|
char source[0x40], token[0x40], scope[0x40], sublabel[0x40], lambda[0x05];
|
||||||
|
char dict[0x10000], *storenext = dict;
|
||||||
|
|
||||||
|
Program p;
|
||||||
|
|
||||||
/* clang-format off */
|
/* clang-format off */
|
||||||
|
|
||||||
|
@ -51,30 +65,38 @@ static int shex(char *s) { int n = 0; char c; while((c = *s++)) { n = n << 4,
|
||||||
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 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 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 *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, char *src) { char *o = dst + slen(dst); while(*src) *o++ = *src++; *o = '\0'; return dst; } /* str concat */
|
static char *scat(char *dst, char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* str cat */
|
||||||
static char *push(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save str */
|
static char *push(char *s) { char *ptr = storenext; while((*storenext++ = *s++)); *storenext++ = 0; return ptr; } /* save str */
|
||||||
|
|
||||||
#define isopcode(x) (findopcode(x) || scmp(x, "BRK", 4))
|
#define isopcode(x) (findopcode(x) || scmp(x, "BRK", 4))
|
||||||
#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
|
#define writeshort(x) (writebyte(x >> 8) && writebyte(x & 0xff))
|
||||||
#define makesublabel(x) push(scat(scat(scpy(scope, sublabel, 0x40), "/"), x), 0)
|
#define error_top(name, msg) !!fprintf(stderr, "%s: %s\n", name, msg)
|
||||||
#define findlabel(x) finditem(x, labels, label_len)
|
#define error_asm(name) !!fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, source, p.line)
|
||||||
#define findmacro(x) finditem(x, macros, macro_len)
|
|
||||||
#define error_top(name, msg) !fprintf(stderr, "%s: %s\n", name, msg)
|
|
||||||
#define error_asm(name) !fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, ctx->path, ctx->line)
|
|
||||||
|
|
||||||
/* clang-format on */
|
/* clang-format on */
|
||||||
|
|
||||||
static int parse(char *w, FILE *f, Context *ctx);
|
static int parse(char *w, FILE *f);
|
||||||
|
static char *makesublabel(char *src, char *name);
|
||||||
|
|
||||||
static Item *
|
static Macro *
|
||||||
finditem(char *name, Item *list, int len)
|
findmacro(char *name)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < p.macro_len; i++)
|
||||||
|
if(scmp(p.macros[i].name, name, 0x40))
|
||||||
|
return &p.macros[i];
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Label *
|
||||||
|
findlabel(char *name)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
if(name[0] == '&')
|
if(name[0] == '&')
|
||||||
name = makesublabel(name + 1);
|
name = makesublabel(sublabel, name + 1);
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < p.label_len; i++)
|
||||||
if(scmp(list[i].name, name, 0x40))
|
if(scmp(p.labels[i].name, name, 0x40))
|
||||||
return &list[i];
|
return &p.labels[i];
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +106,10 @@ findopcode(char *s)
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < 0x20; i++) {
|
for(i = 0; i < 0x20; i++) {
|
||||||
int m = 3;
|
int m = 3;
|
||||||
if(!scmp(ops[i], s, 3)) continue;
|
if(!scmp(ops[i], s, 3))
|
||||||
if(!i) i |= (1 << 7);
|
continue;
|
||||||
|
if(!i)
|
||||||
|
i |= (1 << 7);
|
||||||
while(s[m]) {
|
while(s[m]) {
|
||||||
if(s[m] == '2')
|
if(s[m] == '2')
|
||||||
i |= (1 << 5);
|
i |= (1 << 5);
|
||||||
|
@ -103,97 +127,65 @@ findopcode(char *s)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
walkcomment(FILE *f, Context *ctx)
|
walkcomment(char *w, FILE *f)
|
||||||
{
|
{
|
||||||
char c;
|
|
||||||
int depth = 1;
|
int depth = 1;
|
||||||
while(f && fread(&c, 1, 1, f)) {
|
|
||||||
if(c == 0xa) ctx->line += 1;
|
|
||||||
if(c == '(') depth++;
|
|
||||||
if(c == ')' && --depth < 1) return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
walkmacro(Item *m, Context *ctx)
|
|
||||||
{
|
|
||||||
char c, *contentptr = m->content, *cptr = token;
|
|
||||||
while((c = *contentptr++)) {
|
|
||||||
if(c < 0x21) {
|
|
||||||
*cptr++ = 0x00;
|
|
||||||
if(token[0] && !parse(token, NULL, ctx)) return 0;
|
|
||||||
cptr = token;
|
|
||||||
} else
|
|
||||||
*cptr++ = c;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
walkfile(FILE *f, Context *ctx)
|
|
||||||
{
|
|
||||||
char c, *cptr = token;
|
|
||||||
while(f && fread(&c, 1, 1, f)) {
|
|
||||||
if(c == 0xa) ctx->line += 1;
|
|
||||||
if(c < 0x21) {
|
|
||||||
*cptr++ = 0x00;
|
|
||||||
if(token[0] && !parse(token, f, ctx))
|
|
||||||
return 0;
|
|
||||||
cptr = token;
|
|
||||||
} else if(cptr - token < 0x3f)
|
|
||||||
*cptr++ = c;
|
|
||||||
else
|
|
||||||
return error_asm("Token too long");
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
makemacro(char *name, FILE *f, Context *ctx)
|
|
||||||
{
|
|
||||||
char c;
|
char c;
|
||||||
Item *m;
|
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)
|
||||||
|
{
|
||||||
|
Macro *m;
|
||||||
|
char word[0x40];
|
||||||
if(!slen(name)) return error_asm("Macro is empty");
|
if(!slen(name)) return error_asm("Macro is empty");
|
||||||
if(findmacro(name)) return error_asm("Macro is duplicate");
|
if(findmacro(name)) return error_asm("Macro is duplicate");
|
||||||
if(sihx(name)) return error_asm("Macro is hex number");
|
if(sihx(name)) return error_asm("Macro is hex number");
|
||||||
if(isopcode(name)) return error_asm("Macro is opcode");
|
if(isopcode(name)) return error_asm("Macro is opcode");
|
||||||
if(macro_len == 0x100) return error_asm("Macros limit exceeded");
|
if(p.macro_len == 0x100) return error_asm("Macros limit exceeded");
|
||||||
m = ¯os[macro_len++];
|
m = &p.macros[p.macro_len++];
|
||||||
m->name = push(name, 0);
|
m->name = push(name);
|
||||||
m->content = dictnext;
|
while(fscanf(f, "%63s", word) == 1) {
|
||||||
while(f && fread(&c, 1, 1, f) && c != '{')
|
if(word[0] == '{') continue;
|
||||||
if(c == 0xa) ctx->line += 1;
|
if(word[0] == '}') break;
|
||||||
while(f && fread(&c, 1, 1, f) && c != '}') {
|
if(word[0] == '%') return error_asm("Macro error");
|
||||||
if(c == 0xa) ctx->line += 1;
|
if(m->len >= 0x40) return error_asm("Macro size exceeded");
|
||||||
if(c == '%') return 0;
|
if(word[0] == '(') {
|
||||||
if(c == '(') walkcomment(f, ctx);
|
walkcomment(word, f);
|
||||||
*dictnext++ = c;
|
continue;
|
||||||
|
}
|
||||||
|
scpy(word, m->items[m->len++], 0x40);
|
||||||
}
|
}
|
||||||
*dictnext++ = 0;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
makelabel(char *name, int setscope, Context *ctx)
|
makelabel(char *name, int setscope)
|
||||||
{
|
{
|
||||||
Item *l;
|
Label *l;
|
||||||
if(name[0] == '&')
|
if(name[0] == '&')
|
||||||
name = makesublabel(name + 1);
|
name = makesublabel(sublabel, name + 1);
|
||||||
if(!slen(name)) return error_asm("Label is empty");
|
if(!slen(name)) return error_asm("Label is empty");
|
||||||
if(findlabel(name)) return error_asm("Label is duplicate");
|
if(findlabel(name)) return error_asm("Label is duplicate");
|
||||||
if(sihx(name)) return error_asm("Label is hex number");
|
if(sihx(name)) return error_asm("Label is hex number");
|
||||||
if(isopcode(name)) return error_asm("Label is opcode");
|
if(isopcode(name)) return error_asm("Label is opcode");
|
||||||
if(cndx(runes, name[0]) >= 0) return error_asm("Label name is runic");
|
if(cndx(runes, name[0]) >= 0) return error_asm("Label name is runic");
|
||||||
if(label_len == 0x400) return error_asm("Labels limit exceeded");
|
if(p.label_len == 0x400) return error_asm("Labels limit exceeded");
|
||||||
l = &labels[label_len++];
|
l = &p.labels[p.label_len++];
|
||||||
l->name = push(name, 0);
|
l->addr = p.ptr;
|
||||||
l->addr = ptr;
|
|
||||||
l->refs = 0;
|
l->refs = 0;
|
||||||
|
l->name = push(name);
|
||||||
if(setscope) {
|
if(setscope) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(name[i] != '/' && i < 0x3e && (scope[i] = name[i]))
|
while(name[i] != '/' && i < 0x3e && (scope[i] = name[i])) i++;
|
||||||
i++;
|
|
||||||
scope[i] = '\0';
|
scope[i] = '\0';
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -209,161 +201,205 @@ makelambda(int id)
|
||||||
return lambda;
|
return lambda;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
makesublabel(char *buf, char *name)
|
||||||
|
{
|
||||||
|
if(slen(scope) + slen(name) >= 0x3f) {
|
||||||
|
(void)error_asm("Sublabel length too long");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return scat(scat(scpy(scope, buf, 0x40), "/"), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
makepad(char *w)
|
||||||
|
{
|
||||||
|
Label *l;
|
||||||
|
int rel = w[0] == '$' ? p.ptr : 0;
|
||||||
|
if(sihx(w + 1))
|
||||||
|
p.ptr = shex(w + 1) + rel;
|
||||||
|
else if((l = findlabel(w + 1)))
|
||||||
|
p.ptr = l->addr + rel;
|
||||||
|
else
|
||||||
|
return error_asm("Invalid padding");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
addref(char *label, char rune, Uint16 addr)
|
addref(char *label, char rune, Uint16 addr)
|
||||||
{
|
{
|
||||||
Item *r;
|
Reference *r;
|
||||||
if(refs_len >= 0x1000)
|
if(p.refs_len >= 0x1000)
|
||||||
return error_top("References limit exceeded", label);
|
return error_asm("References limit exceeded");
|
||||||
r = &refs[refs_len++];
|
r = &p.refs[p.refs_len++];
|
||||||
if(label[0] == '{') {
|
if(label[0] == '{') {
|
||||||
lambda_stack[lambda_ptr++] = lambda_len;
|
p.lambda_stack[p.lambda_ptr++] = p.lambda_len;
|
||||||
r->name = push(makelambda(lambda_len++), 0);
|
scpy(makelambda(p.lambda_len++), r->name, 0x40);
|
||||||
} else if(label[0] == '&' || label[0] == '/') {
|
} else if(label[0] == '&' || label[0] == '/') {
|
||||||
r->name = makesublabel(label + 1);
|
if(!makesublabel(r->name, label + 1))
|
||||||
|
return error_asm("Invalid sublabel");
|
||||||
} else
|
} else
|
||||||
r->name = push(label, 0);
|
scpy(label, r->name, 0x40);
|
||||||
r->rune = rune;
|
r->rune = rune;
|
||||||
r->addr = addr;
|
r->addr = addr;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
writepad(char *w)
|
writebyte(Uint8 b)
|
||||||
{
|
{
|
||||||
Item *l;
|
if(p.ptr < TRIM)
|
||||||
int rel = w[0] == '$' ? ptr : 0;
|
|
||||||
if(sihx(w + 1)) {
|
|
||||||
ptr = shex(w + 1) + rel;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if((l = findlabel(w + 1))) {
|
|
||||||
ptr = l->addr + rel;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
writebyte(Uint8 b, Context *ctx)
|
|
||||||
{
|
|
||||||
if(ptr < PAGE)
|
|
||||||
return error_asm("Writing in zero-page");
|
return error_asm("Writing in zero-page");
|
||||||
else if(ptr >= 0x10000)
|
else if(p.ptr >= 0x10000)
|
||||||
return error_asm("Writing outside memory");
|
return error_asm("Writing outside memory");
|
||||||
else if(ptr < length)
|
else if(p.ptr < p.length)
|
||||||
return error_asm("Writing rewind");
|
return error_asm("Writing rewind");
|
||||||
data[ptr++] = b;
|
p.data[p.ptr++] = b;
|
||||||
if(b)
|
p.length = p.ptr;
|
||||||
length = ptr;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
writehex(char *w, Context *ctx)
|
writehex(char *w)
|
||||||
{
|
{
|
||||||
if(*w == '#')
|
if(*w == '#')
|
||||||
writebyte(findopcode("LIT") | (slen(++w) > 2) << 5, ctx);
|
writebyte(findopcode("LIT") | (slen(++w) > 2) << 5);
|
||||||
if(slen(w) == 2)
|
if(slen(w) == 2)
|
||||||
return writebyte(shex(w), ctx);
|
return writebyte(shex(w));
|
||||||
else if(slen(w) == 4)
|
else if(slen(w) == 4)
|
||||||
return writeshort(shex(w));
|
return writeshort(shex(w));
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
tokenize(FILE *f)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
char *cptr = token;
|
||||||
|
while(fread(&c, 1, 1, f)) {
|
||||||
|
if(c < 0x21) {
|
||||||
|
*cptr++ = 0x00;
|
||||||
|
if(c == 0x0a)
|
||||||
|
p.line++;
|
||||||
|
if(token[0])
|
||||||
|
if(!parse(token, f))
|
||||||
|
return 0;
|
||||||
|
cptr = token;
|
||||||
|
} else if(cptr - token < 0x3f)
|
||||||
|
*cptr++ = c;
|
||||||
|
else
|
||||||
|
return error_asm("Token too long");
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
makeinclude(char *filename)
|
makeinclude(char *filename)
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
Context ctx;
|
|
||||||
ctx.line = 0;
|
|
||||||
ctx.path = push(filename, 0);
|
|
||||||
if(!(f = fopen(filename, "r")))
|
if(!(f = fopen(filename, "r")))
|
||||||
return error_top("Invalid source", filename);
|
return error_top("Invalid source", filename);
|
||||||
res = walkfile(f, &ctx);
|
scpy(filename, source, 0x40);
|
||||||
|
p.line = 0;
|
||||||
|
res = tokenize(f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
writestring(char *w, Context *ctx)
|
parse(char *w, FILE *f)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
char c;
|
char c;
|
||||||
while((c = *(w++)))
|
Macro *m;
|
||||||
if(!writebyte(c, ctx)) return 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
parse(char *w, FILE *f, Context *ctx)
|
|
||||||
{
|
|
||||||
Item *m;
|
|
||||||
switch(w[0]) {
|
switch(w[0]) {
|
||||||
case '(': return !walkcomment(f, ctx) ? error_asm("Invalid comment") : 1;
|
case '(': return !walkcomment(w, f) ? error_asm("Invalid comment") : 1;
|
||||||
case '~': return !makeinclude(w + 1) ? error_asm("Invalid include") : 1;
|
case '~': return !makeinclude(w + 1) ? error_asm("Invalid include") : 1;
|
||||||
case '%': return !makemacro(w + 1, f, ctx) ? error_asm("Invalid macro") : 1;
|
case '%': return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1;
|
||||||
case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1;
|
case '@': return !makelabel(w + 1, 1) ? error_asm("Invalid label") : 1;
|
||||||
case '&': return !makelabel(w, 0, ctx) ? error_asm("Invalid sublabel") : 1;
|
case '&': return !makelabel(w, 0) ? error_asm("Invalid sublabel") : 1;
|
||||||
case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx) ? error_asm("Invalid label") : 1;
|
case '#': return !sihx(w + 1) || !writehex(w) ? error_asm("Invalid hexadecimal") : 1;
|
||||||
case '#': return !sihx(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1;
|
case '_': return addref(w + 1, w[0], p.ptr) && writebyte(0xff);
|
||||||
case '_': return addref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
|
case ',': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
|
||||||
case ',': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
|
case '-': return addref(w + 1, w[0], p.ptr) && writebyte(0xff);
|
||||||
case '-': return addref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
|
case '.': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT")) && writebyte(0xff);
|
||||||
case '.': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
|
|
||||||
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
|
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
|
||||||
case '=': return addref(w + 1, w[0], ptr) && writeshort(0xffff);
|
case '=': return addref(w + 1, w[0], p.ptr) && writeshort(0xffff);
|
||||||
case ';': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
|
case ';': return addref(w + 1, w[0], p.ptr + 1) && writebyte(findopcode("LIT2")) && writeshort(0xffff);
|
||||||
case '?': return addref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
|
case '?': return addref(w + 1, w[0], p.ptr + 1) && writebyte(0x20) && writeshort(0xffff);
|
||||||
case '!': return addref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
|
case '!': return addref(w + 1, w[0], p.ptr + 1) && writebyte(0x40) && writeshort(0xffff);
|
||||||
case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1;
|
case '}': return !makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]), 0) ? error_asm("Invalid label") : 1;
|
||||||
case '$':
|
case '$':
|
||||||
case '|': return !writepad(w) ? error_asm("Invalid padding") : 1;
|
case '|': return !makepad(w) ? error_asm("Invalid padding") : 1;
|
||||||
case '[':
|
case '[':
|
||||||
case ']': return 1;
|
case ']':
|
||||||
}
|
if(slen(w) == 1) break; /* else fallthrough */
|
||||||
|
case '"': /* raw string */
|
||||||
|
while((c = *(++w)))
|
||||||
|
if(!writebyte(c)) return 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
if(sihx(w))
|
if(sihx(w))
|
||||||
return writehex(w, ctx);
|
return writehex(w);
|
||||||
else if(isopcode(w))
|
else if(isopcode(w))
|
||||||
return writebyte(findopcode(w), ctx);
|
return writebyte(findopcode(w));
|
||||||
else if((m = findmacro(w)))
|
else if((m = findmacro(w))) {
|
||||||
return walkmacro(m, ctx);
|
for(i = 0; i < m->len; i++)
|
||||||
return addref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff);
|
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
|
static int
|
||||||
resolve(void)
|
resolve(void)
|
||||||
{
|
{
|
||||||
int i, rel;
|
Label *l;
|
||||||
for(i = 0; i < refs_len; i++) {
|
int i;
|
||||||
Item *r = &refs[i], *l = findlabel(r->name);
|
Uint16 a;
|
||||||
Uint8 *rom = data + r->addr;
|
for(i = 0; i < p.refs_len; i++) {
|
||||||
if(!l) return 0;
|
Reference *r = &p.refs[i];
|
||||||
|
Uint8 *rom = p.data + r->addr;
|
||||||
switch(r->rune) {
|
switch(r->rune) {
|
||||||
case '_':
|
case '_':
|
||||||
case ',':
|
case ',':
|
||||||
*rom = rel = l->addr - r->addr - 2;
|
if(!(l = findlabel(r->name)))
|
||||||
if((Sint8)data[r->addr] != rel)
|
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);
|
return error_top("Relative reference is too far", r->name);
|
||||||
|
l->refs++;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
case '.':
|
case '.':
|
||||||
|
if(!(l = findlabel(r->name)))
|
||||||
|
return error_top("Unknown zero-page reference", r->name);
|
||||||
*rom = l->addr;
|
*rom = l->addr;
|
||||||
|
l->refs++;
|
||||||
break;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
case '=':
|
case '=':
|
||||||
case ';':
|
case ';':
|
||||||
|
if(!(l = findlabel(r->name)))
|
||||||
|
return error_top("Unknown absolute reference", r->name);
|
||||||
*rom++ = l->addr >> 8, *rom = l->addr;
|
*rom++ = l->addr >> 8, *rom = l->addr;
|
||||||
|
l->refs++;
|
||||||
break;
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
case '!':
|
case '!':
|
||||||
default:
|
default:
|
||||||
rel = l->addr - r->addr - 2;
|
if(!(l = findlabel(r->name)))
|
||||||
*rom++ = rel >> 8, *rom = rel;
|
return error_top("Unknown subroutine reference", r->name);
|
||||||
|
a = l->addr - r->addr - 2;
|
||||||
|
*rom++ = a >> 8, *rom = a;
|
||||||
|
l->refs++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
l->refs++;
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -372,16 +408,16 @@ static void
|
||||||
review(char *filename)
|
review(char *filename)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < label_len; i++)
|
for(i = 0; i < p.label_len; i++)
|
||||||
if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
|
if(p.labels[i].name[0] - 'A' > 25 && !p.labels[i].refs)
|
||||||
fprintf(stdout, "-- Unused label: %s\n", labels[i].name);
|
fprintf(stdout, "-- Unused label: %s\n", p.labels[i].name);
|
||||||
fprintf(stdout,
|
fprintf(stdout,
|
||||||
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
|
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
|
||||||
filename,
|
filename,
|
||||||
length - PAGE,
|
p.length - TRIM,
|
||||||
(length - PAGE) / 652.80,
|
(p.length - TRIM) / 652.80,
|
||||||
label_len,
|
p.label_len,
|
||||||
macro_len);
|
p.macro_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -394,11 +430,11 @@ writesym(char *filename)
|
||||||
return;
|
return;
|
||||||
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
|
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
|
||||||
if(fp != NULL) {
|
if(fp != NULL) {
|
||||||
for(i = 0; i < label_len; i++) {
|
for(i = 0; i < p.label_len; i++) {
|
||||||
Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
|
Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr;
|
||||||
fwrite(&hb, 1, 1, fp);
|
fwrite(&hb, 1, 1, fp);
|
||||||
fwrite(&lb, 1, 1, fp);
|
fwrite(&lb, 1, 1, fp);
|
||||||
fwrite(labels[i].name, slen(labels[i].name) + 1, 1, fp);
|
fwrite(p.labels[i].name, slen(p.labels[i].name) + 1, 1, fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -408,16 +444,15 @@ int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *dst;
|
FILE *dst;
|
||||||
ptr = PAGE;
|
p.ptr = 0x100;
|
||||||
scpy("on-reset", scope, 0x40);
|
scpy("on-reset", scope, 0x40);
|
||||||
if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom");
|
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, 27 Mar 2024.\n");
|
if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 26 Mar 2024.\n");
|
||||||
if(!makeinclude(argv[1])) return !error_top("Assembly", "Failed to assemble rom.");
|
if(!makeinclude(argv[1]) || !resolve()) return !error_top("Assembly", "Failed to assemble rom.");
|
||||||
if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols.");
|
|
||||||
if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]);
|
if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]);
|
||||||
if(length <= PAGE) return !error_top("Assembly", "Output rom is empty.");
|
if(p.length <= TRIM) return !error_top("Assembly", "Output rom is empty.");
|
||||||
review(argv[2]);
|
review(argv[2]);
|
||||||
fwrite(data + PAGE, length - PAGE, 1, dst);
|
fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
|
||||||
writesym(argv[2]);
|
writesym(argv[2]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue