Compare commits

...

13 Commits

Author SHA1 Message Date
Devine Lu Linvega fe515570a2 (uxnasm) Added ishex() macro 2024-03-28 17:22:05 -07:00
Devine Lu Linvega 5e593e7931 (uxnasm) Recover 2024-03-28 14:39:52 -07:00
Devine Lu Linvega f55a6a0b55 (uxnasm) Housekeeping 2024-03-28 14:29:34 -07:00
Devine Lu Linvega cf552bb50b (uxnasm) Removed scpy 2024-03-28 14:26:59 -07:00
Devine Lu Linvega e080125437 (uxnasm) Removed slen macro 2024-03-28 14:03:25 -07:00
Devine Lu Linvega 9af23e1090 (uxnasm) Generalized makesublabel 2024-03-28 13:24:41 -07:00
Devine Lu Linvega 84cc003dec (uxnasm) Catch buffer overflows 2024-03-28 12:08:10 -07:00
Devine Lu Linvega 8d06f3e1b9 (uxnasm) Fixed issue with comments inside macros 2024-03-28 12:04:25 -07:00
Devine Lu Linvega 6c3888f306 (uxnasm) Housekeeping 2024-03-28 11:57:54 -07:00
Devine Lu Linvega 19a06cfd17 Starting to report unused macros 2024-03-28 11:43:21 -07:00
Devine Lu Linvega 3dbdb19225 (uxnasm) Use save() to cat path 2024-03-28 10:47:46 -07:00
Devine Lu Linvega 4a028ed63b (uxnasm) Housekeeping 2024-03-28 10:37:16 -07:00
Devine Lu Linvega 389d6ce364 (uxnasm) Merge build functions 2024-03-28 10:07:37 -07:00
2 changed files with 79 additions and 103 deletions

View File

@ -2,7 +2,7 @@
|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 -- ) { ( hey ) #18 DEO }
|0100 @program

View File

@ -11,31 +11,23 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
/* clang-format off */
#define PAGE 0x0100
typedef unsigned char Uint8;
typedef signed char Sint8;
typedef unsigned short Uint16;
typedef struct {
char *name, rune, *content;
Uint16 addr, refs;
} Item;
typedef struct {
int line;
char *path;
} Context;
typedef struct { char *name, rune, *content; Uint16 addr, refs; } Item;
typedef struct { int line; char *path; } Context;
static int ptr, length;
static char token[0x40], scope[0x40], sublabel[0x80], lambda[0x05];
static char token[0x40], scope[0x40], lambda[0x05];
static char dict[0x4000], *dictnext = dict;
static Uint8 data[0x10000], lambda_stack[0x100], lambda_ptr, lambda_len;
static Uint16 label_len, refs_len, macro_len;
static Uint16 labels_len, refs_len, macro_len;
static Item labels[0x400], refs[0x1000], macros[0x100];
/* clang-format off */
static char *runes = "|$@&,_.-;=!?#\"%~";
static char *hexad = "0123456789abcdef";
static char ops[][4] = {
@ -45,19 +37,18 @@ static char ops[][4] = {
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
};
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 find(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 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 */
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, char *src) { char *o = dst + slen(dst); while(*src) *o++ = *src++; *o = '\0'; return dst; } /* str concat */
static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save to dict */
static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; } /* join two str */
#define isopcode(x) (findopcode(x) || scmp(x, "BRK", 4))
#define isinvalid(x) (!slen(x) || sihx(x) || isopcode(x) || cndx(runes, x[0]) >= 0)
#define ishex(x) (shex(x) >= 0)
#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
#define isinvalid(x) (!x[0] || ishex(x) || isopc(x) || find(runes, x[0]) >= 0)
#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
#define makesublabel(x) push(scat(scat(scpy(scope, sublabel, 0x40), "/"), x), 0)
#define findlabel(x) finditem(x, labels, label_len)
#define findlabel(x) finditem(x, labels, labels_len)
#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)
@ -69,18 +60,13 @@ static int parse(char *w, FILE *f, Context *ctx);
static char *
push(char *s, char c)
{
char *d = dict, *o = dictnext;
/* find */
char *d = dict;
for(d = dict; d < dictnext; d++) {
char *ss = s, *pp = d, a, b;
while((a = *pp++) == (b = *ss++))
char *ss = s, *dd = d, a, b;
while((a = *dd++) == (b = *ss++))
if(!a && !b) return d;
}
/* save */
while((*dictnext++ = *s++) && *s)
;
*dictnext++ = c;
return o;
return save(s, c);
}
static Item *
@ -88,7 +74,7 @@ finditem(char *name, Item *list, int len)
{
int i;
if(name[0] == '&')
name = makesublabel(name + 1);
name = join(scope, '/', name + 1);
for(i = 0; i < len; i++)
if(scmp(list[i].name, name, 0x40))
return &list[i];
@ -166,12 +152,22 @@ walkfile(FILE *f, Context *ctx)
return 1;
}
static char *
makelambda(int id)
{
lambda[0] = (char)0xce;
lambda[1] = (char)0xbb;
lambda[2] = hexad[id >> 0x4];
lambda[3] = hexad[id & 0xf];
return lambda;
}
static int
makemacro(char *name, FILE *f, Context *ctx)
{
char c;
Item *m;
if(macro_len == 0x100) return error_asm("Macros limit exceeded");
if(macro_len >= 0x100) return error_asm("Macros limit exceeded");
if(isinvalid(name)) return error_asm("Macro is invalid");
if(findmacro(name)) return error_asm("Macro is duplicate");
m = &macros[macro_len++];
@ -182,8 +178,10 @@ makemacro(char *name, FILE *f, Context *ctx)
while(f && fread(&c, 1, 1, f) && c != '}') {
if(c == 0xa) ctx->line += 1;
if(c == '%') return 0;
if(c == '(') walkcomment(f, ctx);
*dictnext++ = c;
if(c == '(')
walkcomment(f, ctx);
else
*dictnext++ = c;
}
*dictnext++ = 0;
return 1;
@ -194,11 +192,11 @@ makelabel(char *name, int setscope, Context *ctx)
{
Item *l;
if(name[0] == '&')
name = makesublabel(name + 1);
if(label_len == 0x400) return error_asm("Labels limit exceeded");
name = join(scope, '/', name + 1);
if(labels_len >= 0x400) return error_asm("Labels limit exceeded");
if(isinvalid(name)) return error_asm("Label is invalid");
if(findlabel(name)) return error_asm("Label is duplicate");
l = &labels[label_len++];
l = &labels[labels_len++];
l->name = push(name, 0);
l->addr = ptr;
l->refs = 0;
@ -211,28 +209,17 @@ makelabel(char *name, int setscope, Context *ctx)
return 1;
}
static char *
makelambda(int id)
{
lambda[0] = (char)0xce;
lambda[1] = (char)0xbb;
lambda[2] = hexad[id >> 0x4];
lambda[3] = hexad[id & 0xf];
return lambda;
}
static int
addref(char *label, char rune, Uint16 addr)
makeref(char *label, char rune, Uint16 addr)
{
Item *r;
if(refs_len >= 0x1000)
return error_top("References limit exceeded", label);
if(refs_len >= 0x1000) return error_top("References limit exceeded", label);
r = &refs[refs_len++];
if(label[0] == '{') {
lambda_stack[lambda_ptr++] = lambda_len;
r->name = push(makelambda(lambda_len++), 0);
} else if(label[0] == '&' || label[0] == '/') {
r->name = makesublabel(label + 1);
r->name = join(scope, '/', label + 1);
} else
r->name = push(label, 0);
r->rune = rune;
@ -245,7 +232,7 @@ writepad(char *w)
{
Item *l;
int rel = w[0] == '$' ? ptr : 0;
if(sihx(w + 1)) {
if(ishex(w + 1)) {
ptr = shex(w + 1) + rel;
return 1;
}
@ -275,10 +262,10 @@ static int
writehex(char *w, Context *ctx)
{
if(*w == '#')
writebyte(findopcode("LIT") | (slen(++w) > 2) << 5, ctx);
if(slen(w) == 2)
writebyte(findopcode("LIT") | !!(++w)[2] << 5, ctx);
if(!w[2])
return writebyte(shex(w), ctx);
else if(slen(w) == 4)
else if(!w[4])
return writeshort(shex(w));
else
return 0;
@ -318,16 +305,16 @@ parse(char *w, FILE *f, Context *ctx)
case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1;
case '&': return !makelabel(w, 0, ctx) ? 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, ctx) ? error_asm("Invalid hexadecimal") : 1;
case '_': return addref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
case ',': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
case '-': return addref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
case '.': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
case '#': return !ishex(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1;
case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
case '.': return makeref(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 '=': return addref(w + 1, w[0], 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], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
case '!': return addref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
case '=': return makeref(w + 1, w[0], ptr) && writeshort(0xffff);
case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1;
case '~': return !assemble(w + 1) ? error_asm("Invalid include") : 1;
case '$':
@ -335,13 +322,10 @@ parse(char *w, FILE *f, Context *ctx)
case '[':
case ']': return 1;
}
if(sihx(w))
return writehex(w, ctx);
else if(isopcode(w))
return writebyte(findopcode(w), ctx);
else if((m = findmacro(w)))
return walkmacro(m, ctx);
return addref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff);
if(ishex(w)) return writehex(w, ctx);
if(isopc(w)) return writebyte(findopcode(w), ctx);
if((m = findmacro(w))) return walkmacro(m, ctx);
return makeref(w, ' ', ptr + 1) && writebyte(0x60, ctx) && writeshort(0xffff);
}
static int
@ -381,46 +365,38 @@ resolve(void)
return 1;
}
static void
writesym(char *filename)
{
int i;
char symdst[0x60];
FILE *fp;
if(slen(filename) > 0x60 - 5)
return;
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
if(fp != NULL) {
for(i = 0; i < label_len; i++) {
Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
fwrite(&hb, 1, 1, fp);
fwrite(&lb, 1, 1, fp);
fwrite(labels[i].name, slen(labels[i].name) + 1, 1, fp);
}
}
fclose(fp);
}
static int
build(char *filename)
build(char *rompath)
{
int i;
FILE *dst;
if(!(dst = fopen(filename, "wb")))
return !error_top("Invalid output file", filename);
for(i = 0; i < label_len; i++)
FILE *dst, *dstsym;
char *sympath = join(rompath, '.', "sym");
/* rom */
if(!(dst = fopen(rompath, "wb")))
return !error_top("Invalid output file", rompath);
for(i = 0; i < labels_len; i++)
if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
fprintf(stdout, "-- Unused label: %s\n", labels[i].name);
fwrite(data + PAGE, length - PAGE, 1, dst);
fprintf(stdout,
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
filename,
rompath,
length - PAGE,
(length - PAGE) / 652.80,
label_len,
labels_len,
macro_len);
writesym(filename);
fclose(dst);
/* sym */
if(!(dstsym = fopen(sympath, "w")))
return !error_top("Invalid symbols file", sympath);
for(i = 0; i < labels_len; i++) {
Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
char c, d = 0, *name = labels[i].name;
fwrite(&hb, 1, 1, dstsym);
fwrite(&lb, 1, 1, dstsym);
while((c = *name++)) fwrite(&c, 1, 1, dstsym);
fwrite(&d, 1, 1, dstsym);
}
fclose(dst), fclose(dstsym);
return 1;
}