Compare commits

...

3 Commits

Author SHA1 Message Date
Devine Lu Linvega c5a5eaff0e (uxnasm) Quieted warning 2024-03-26 21:56:50 -07:00
Devine Lu Linvega 9d6587ffa5 (uxnasm) Converted functions to macros 2024-03-26 21:46:41 -07:00
Devine Lu Linvega 2f3eb460d6 (uxnasm) Removed unnecessary masking 2024-03-26 21:11:31 -07:00
1 changed files with 13 additions and 35 deletions

View File

@ -49,6 +49,8 @@ Program p;
/* clang-format off */
static char *runes = "|$@&,_.-;=!?#\"%~";
static char *hexad = "0123456789abcdef";
static char ops[][4] = {
"LIT", "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR",
"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
@ -56,36 +58,24 @@ static char ops[][4] = {
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
};
static char *runes = "|$@&,_.-;=!?#\"%~";
static char *hexad = "0123456789abcdef";
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 */
static char *scat(char *dst, char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* str cat */
#define isopcode(x) (findopcode(x) || scmp(x, "BRK", 4))
#define writeshort(x) (writebyte(x >> 8) && writebyte(x & 0xff))
#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, source, p.line)
/* clang-format on */
static int parse(char *w, FILE *f);
static char *makesublabel(char *src, char *name);
static int
error_top(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s\n", name, msg);
return 0;
}
static int
error_asm(const char *name)
{
fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, source, p.line);
return 0;
}
static Macro *
findmacro(char *name)
{
@ -134,12 +124,6 @@ findopcode(char *s)
return 0;
}
static int
isopcode(char *s)
{
return findopcode(s) || scmp(s, "BRK", 4);
}
static int
walkcomment(char *w, FILE *f)
{
@ -219,7 +203,7 @@ static char *
makesublabel(char *buf, char *name)
{
if(slen(scope) + slen(name) >= 0x3f) {
error_asm("Sublabel length too long");
(void)error_asm("Sublabel length too long");
return NULL;
}
return scat(scat(scpy(scope, buf, 0x40), "/"), name);
@ -273,12 +257,6 @@ writebyte(Uint8 b)
return 1;
}
static int
writeshort(Uint16 s)
{
return writebyte(s >> 8) && writebyte(s & 0xff);
}
static int
writehex(char *w)
{
@ -399,7 +377,7 @@ resolve(void)
case '.':
if(!(l = findlabel(r->name)))
return error_top("Unknown zero-page reference", r->name);
*rom = l->addr & 0xff;
*rom = l->addr;
l->refs++;
break;
case ':':
@ -407,7 +385,7 @@ resolve(void)
case ';':
if(!(l = findlabel(r->name)))
return error_top("Unknown absolute reference", r->name);
*rom++ = l->addr >> 0x8, *rom = l->addr & 0xff;
*rom++ = l->addr >> 8, *rom = l->addr;
l->refs++;
break;
case '?':
@ -416,7 +394,7 @@ resolve(void)
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;
*rom++ = a >> 8, *rom = a;
l->refs++;
break;
}
@ -451,7 +429,7 @@ writesym(char *filename)
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
if(fp != NULL) {
for(i = 0; i < p.label_len; i++) {
Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff;
Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr;
fwrite(&hb, 1, 1, fp);
fwrite(&lb, 1, 1, fp);
fwrite(p.labels[i].name, slen(p.labels[i].name) + 1, 1, fp);