summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c269
1 files changed, 136 insertions, 133 deletions
diff --git a/lexer.c b/lexer.c
index 8b5b840..a7d50ec 100644
--- a/lexer.c
+++ b/lexer.c
@@ -43,7 +43,7 @@ uint16_t mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t useid, ui
}
}
}
- symbols[i] = malloc(sizeof(*symbols) + strlen(name));
+ symbols[i] = malloc(sizeof(**symbols) + strlen(name));
symbols[i]->def = def;
symbols[i]->val = val;
strcpy(symbols[i]->name, name);
@@ -119,7 +119,7 @@ char *get_symname(uint16_t id, uint8_t dbg) {
uint16_t fixup_cnt = 0;
uint16_t get_symid(const char *name, uint64_t val, uint16_t ln, uint8_t dbg) {
- uint16_t i = mksymbol(name, 0, 0, 0, 0, dbg);
+ uint16_t i = mksymbol(name, 0, 0, 0, ln, dbg);
if (dbg) {
printf("get_symid(): Symbol ID: $%X, i: $%X.\n", symbols[i]->id, i);
}
@@ -129,7 +129,7 @@ uint16_t get_symid(const char *name, uint64_t val, uint16_t ln, uint8_t dbg) {
if (dbg) {
printf("get_symid(): oof, symbol %s, does not exist, yet.\n", name);
}
- fixups[fixup_cnt] = malloc(sizeof(*fixups));
+ fixups[fixup_cnt] = malloc(sizeof(**fixups));
fixups[fixup_cnt]->adr = val;
fixups[fixup_cnt]->ln = ln;
fixups[fixup_cnt]->s = symbols[i];
@@ -204,14 +204,14 @@ uint16_t get_string(const char *str, uint8_t dbg) {
return i;
}
-uint16_t reslv_fixups(uint8_t dbg) {
+uint16_t reslv_fixups(struct line *l, uint8_t dbg) {
uint16_t i = 0, j = 0;
for (; fixups[j]; j++) {
if (fixups[j]->s->def) {
if (dbg) {
printf("reslv_fixups(): Symbol ID: $%X, Symbol Name: %s.\n", fixups[j]->s->id, fixups[j]->s->name);
}
- tokline[fixups[j]->ln].sym = fixups[j]->s->id;
+ l[fixups[j]->ln].sym = fixups[j]->s->id;
} else {
if (dbg) {
printf("reslv_fixups(): oof, undefined reference to '%s', at $%016"PRIX64".\n", fixups[j]->s->name, fixups[j]->adr);
@@ -223,25 +223,25 @@ uint16_t reslv_fixups(uint8_t dbg) {
}
-uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
+uint64_t update_addr(struct line *ln, uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
uint64_t value = 0;
uint16_t i = 0;
uint16_t j = 0;
uint16_t flags = 0;
uint8_t opsize = 0;
- uint16_t symid = tokline[l].sym;
- uint16_t str = tokline[l].str;
- uint16_t com = tokline[l].com;
- uint8_t islabel = tokline[l].islabel;
- uint8_t issym = tokline[l].issym;
- uint8_t opbase = tokline[l].opbase;
- uint8_t aopbase = tokline[l].aopbase;
- uint8_t dir = tokline[l].dir;
- uint8_t am = tokline[l].am;
- uint8_t cm = tokline[l].cm;
- uint8_t rs = tokline[l].rs;
- uint8_t mne = tokline[l].mne;
+ uint16_t symid = ln[l].sym;
+ uint16_t str = ln[l].str;
+ uint16_t com = ln[l].com;
+ uint8_t islabel = ln[l].islabel;
+ uint8_t issym = ln[l].issym;
+ uint8_t opbase = ln[l].opbase;
+ uint8_t aopbase = ln[l].aopbase;
+ uint8_t dir = ln[l].dir;
+ uint8_t am = ln[l].am;
+ uint8_t cm = ln[l].cm;
+ uint8_t rs = ln[l].rs;
+ uint8_t mne = ln[l].mne;
flags |= (dir != 0x00FF) << 0x00;
flags |= (mne != 0x00FF) << 0x01;
@@ -277,12 +277,12 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
value = use_symbol("", symid, address, 1, dbg);
}
} else {
- value = tokline[l].op;
+ value = ln[l].op;
}
if (flags & 0x220) {
switch (cm) {
- case 0: value += tokline[l].aop; break;
- case 1: value -= tokline[l].aop; break;
+ case 0: value += ln[l].aop; break;
+ case 1: value -= ln[l].aop; break;
}
}
if (dbg) {
@@ -324,7 +324,7 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
return address;
}
if ((flags & 0x15B) == 0x02 || (opcodes[mne][IMPL] != 0xFF && am == 0xFF && opbase == 0xFF && symid == 0xFFFF)) {
- tokline[l].am = IMPL;
+ ln[l].am = IMPL;
am = IMPL;
if (dbg) {
printf("update_addr(): ");
@@ -366,7 +366,7 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
case 3:
if (!(flags & 0x100)) {
am = ZM;
- tokline[l].am = am;
+ ln[l].am = am;
if (dbg) {
printf("update_addr(): ");
puts("Addressing Mode has been set to Zero Matrix.");
@@ -379,7 +379,7 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
case 7:
if (!(flags & 0x100)) {
am = ABS;
- tokline[l].am = am;
+ ln[l].am = am;
if (dbg) {
printf("update_addr(): ");
puts("Addressing Mode has been set to Absolute.");
@@ -402,21 +402,21 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) {
return address;
}
-uint16_t find_line(uint16_t ln, uint8_t dbg) {
+uint16_t find_line(struct line *l, uint16_t ln, uint8_t dbg) {
uint16_t i = 0;
- for (; i < lineidx && tokline[i].linenum != ln; i++);
- if (tokline[i].linenum == ln) {
+ for (; i < lineidx && l[i].linenum != ln; i++);
+ if (l[i].linenum == ln) {
if (dbg) {
printf("find_line(): Found line number %u, at line index %X.\n", ln, i);
}
}
if (dbg) {
- printf("find_line(): linenum: %u, i: %X\n", tokline[i].linenum, i);
+ printf("find_line(): linenum: %u, i: %X\n", l[i].linenum, i);
}
return i;
}
-uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
+uint64_t lex(char *str, struct line *l, uint64_t address, uint8_t dbg) {
char sym[0x100];
uint16_t i = 0;
uint16_t j = 0;
@@ -451,25 +451,25 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
ln = linenum;
}
for (; isspace(str[i]); i++);
- line = find_line(ln, dbg);
+ line = find_line(l, ln, dbg);
if (line != lineidx) {
- address = tokline[line].addr;
- }
- tokline[line].dir = 0xFF;
- tokline[line].mne = 0xFF;
- tokline[line].rs = 0xFF;
- tokline[line].am = 0xFF;
- tokline[line].cm = 0xFF;
- tokline[line].opbase = 0xFF;
- tokline[line].aopbase = 0xFF;
- tokline[line].islabel = 0;
- tokline[line].issym = 0;
- tokline[line].str = 0xFFFF;
- tokline[line].com = 0xFFFF;
- tokline[line].sym = 0xFFFF;
- tokline[line].op = 0;
- tokline[line].aop = 0;
- tokline[line].addr = address;
+ address = l[line].addr;
+ }
+ l[line].dir = 0xFF;
+ l[line].mne = 0xFF;
+ l[line].rs = 0xFF;
+ l[line].am = 0xFF;
+ l[line].cm = 0xFF;
+ l[line].opbase = 0xFF;
+ l[line].aopbase = 0xFF;
+ l[line].islabel = 0;
+ l[line].issym = 0;
+ l[line].str = 0xFFFF;
+ l[line].com = 0xFFFF;
+ l[line].sym = 0xFFFF;
+ l[line].op = 0;
+ l[line].aop = 0;
+ l[line].addr = address;
while (str[i] != '\0' && str[i] != '\n') {
space = 0;
tab = 0;
@@ -483,10 +483,10 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
printf("lex(): tab: %u, space: %u\n", tab, space);
}
if (isstart) {
- tokline[line].stab = tab;
- tokline[line].sspace = space;
+ l[line].stab = tab;
+ l[line].sspace = space;
if (dbg) {
- printf("lex(): starting tabs: %u, starting spaces: %u\n", tokline[line].stab, tokline[line].sspace);
+ printf("lex(): starting tabs: %u, starting spaces: %u\n", l[line].stab, l[line].sspace);
}
}
if (isspace(str[i])) {
@@ -502,7 +502,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
lexeme[j] = '\0';
if (!isop) {
- for (k = 0; k < 5; k++) {
+ for (k = 0; k < 6; k++) {
if (tolower(lexeme[0]) == dir_t[k][0]) {
if (!strcasecmp(lexeme, dir_t[k])) {
lex_type = TOK_DIR;
@@ -510,7 +510,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
}
}
- tokline[line].dir = k;
+ l[line].dir = k;
} else {
lex_type = TOK_RS;
switch (tolower(lexeme[j-1])) {
@@ -528,7 +528,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
break;
}
address++;
- tokline[line].rs = rs;
+ l[line].rs = rs;
isop = 0;
}
break;
@@ -539,31 +539,34 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
strid = get_string(lexeme, dbg);
if (strid == 0xFFFF) {
- if (line != lineidx && tokline[line].str != 0xFFFF) {
- strid = tokline[line].str;
+ if (line != lineidx && l[line].str != 0xFFFF) {
+ strid = l[line].str;
} else {
strid = stridx;
}
string[strid] = malloc(j+1);
memcpy(string[strid], lexeme, j+1);
- tokline[line].str = strid;
+ l[line].str = strid;
if (dbg) {
printf("lex(): str[0x%04X]: %s\n", strid, string[strid]);
}
stridx += (line == lineidx);
} else {
- tokline[line].str = strid;
+ l[line].str = strid;
if (dbg) {
printf("lex(): str[0x%04X]: %s\n", strid, string[strid]);
}
}
+ if (l[line].dir == DIR_INCLUDE) {
+ l[line].incl = strid;
+ }
lex_type = TOK_STRING;
break;
case '#':
lexeme[j] = '#';
lexeme[j+1] = '\0';
lexeme[j+2] = '\0';
- tokline[line].am = IMM;
+ l[line].am = IMM;
lex_type = TOK_IMM;
break;
case '$':
@@ -574,27 +577,27 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
switch (lex_type) {
case TOK_SYM:
- tokline[line].op = strtoull(lexeme, NULL, 16);
- mksymbol(sym, tokline[line].op, 1, 0, 0, dbg);
- tokline[line].sym = get_symid(sym, address, line, dbg);
- isfixup += (tokline[line].sym == 0xFFFF);
+ l[line].op = strtoull(lexeme, NULL, 16);
+ mksymbol(sym, l[line].op, 1, 0, 0, dbg);
+ l[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += (l[line].sym == 0xFFFF);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[line].opbase = BASE_HEX;
+ l[line].opbase = BASE_HEX;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[line].aop = strtoull(lexeme, NULL, 16);
- tokline[line].aopbase = BASE_HEX;
+ l[line].aop = strtoull(lexeme, NULL, 16);
+ l[line].aopbase = BASE_HEX;
break;
default:
- if (tokline[line].cm != 0xFF) {
- tokline[line].aop = strtoull(lexeme, NULL, 16);
- tokline[line].aopbase = BASE_HEX;
+ if (l[line].cm != 0xFF) {
+ l[line].aop = strtoull(lexeme, NULL, 16);
+ l[line].aopbase = BASE_HEX;
} else {
- tokline[line].op = strtoull(lexeme, NULL, 16);
- tokline[line].opbase = BASE_HEX;
+ l[line].op = strtoull(lexeme, NULL, 16);
+ l[line].opbase = BASE_HEX;
}
break;
@@ -609,27 +612,27 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
switch (lex_type) {
case TOK_SYM:
- tokline[line].op = strtoull(lexeme, NULL, 2);
- mksymbol(sym, tokline[line].op, 1, 0, 0, dbg);
- tokline[line].sym = get_symid(sym, address, line, dbg);
- isfixup += (tokline[line].sym == 0xFFFF);
+ l[line].op = strtoull(lexeme, NULL, 2);
+ mksymbol(sym, l[line].op, 1, 0, 0, dbg);
+ l[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += (l[line].sym == 0xFFFF);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[line].opbase = BASE_BIN;
+ l[line].opbase = BASE_BIN;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[line].aop = strtoull(lexeme, NULL, 2);
- tokline[line].aopbase = BASE_BIN;
+ l[line].aop = strtoull(lexeme, NULL, 2);
+ l[line].aopbase = BASE_BIN;
break;
default:
- if (tokline[lineidx].cm != 0xFF) {
- tokline[line].aop = strtoull(lexeme, NULL, 2);
- tokline[line].aopbase = BASE_BIN;
+ if (l[lineidx].cm != 0xFF) {
+ l[line].aop = strtoull(lexeme, NULL, 2);
+ l[line].aopbase = BASE_BIN;
} else {
- tokline[line].op = strtoull(lexeme, NULL, 2);
- tokline[line].opbase = BASE_BIN;
+ l[line].op = strtoull(lexeme, NULL, 2);
+ l[line].opbase = BASE_BIN;
}
break;
@@ -639,20 +642,20 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
case '+':
lexeme[j] = '+';
lexeme[j+1] = '\0';
- tokline[line].cm = 0;
+ l[line].cm = 0;
lex_type = TOK_PLUS;
break;
case '-':
lexeme[j] = '-';
lexeme[j+1] = '\0';
- tokline[line].cm = 1;
+ l[line].cm = 1;
lex_type = TOK_MINUS;
break;
case '(':
lexeme[j] = '(';
lexeme[j+1] = '\0';
lexeme[j+2] = '\0';
- tokline[line].am = IND;
+ l[line].am = IND;
break;
case ')':
i++;
@@ -661,9 +664,9 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
while (isspace(str[i])) {
lexeme[j++] = str[i++];
}
- if (tokline[line].am == IND && tolower(str[i]) == 'y') {
+ if (l[line].am == IND && tolower(str[i]) == 'y') {
lexeme[j++] = 'y';
- tokline[line].am = INDY;
+ l[line].am = INDY;
}
lexeme[j] = '\0';
} else {
@@ -677,18 +680,18 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
while (isspace(str[i])) {
lexeme[j++] = str[i++];
}
- if (tokline[line].am == IND && tolower(str[i]) == 'x') {
- tokline[line].am = INDX;
+ if (l[line].am == IND && tolower(str[i]) == 'x') {
+ l[line].am = INDX;
lexeme[j++] = 'x';
i++;
} else {
switch (tolower(str[i])) {
case 'x':
- tokline[line].am = ZMX;
+ l[line].am = ZMX;
lexeme[j++] = 'x';
break;
case 'y':
- tokline[line].am = ZMY;
+ l[line].am = ZMY;
lexeme[j++] = 'y';
break;
}
@@ -700,13 +703,13 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = ':';
lexeme[j+1] = '\0';
lex_type = TOK_LABEL;
- tokline[line].islabel = 1;
+ l[line].islabel = 1;
mksymbol(sym, address, 1, 0, 0, dbg);
if (isfixup) {
- isfixup = reslv_fixups(dbg);
+ isfixup = reslv_fixups(l, dbg);
}
- tokline[line].sym = get_symid(sym, address, line, dbg);
- isfixup += (tokline[line].sym == 0xFFFF);
+ l[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += (l[line].sym == 0xFFFF);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -715,7 +718,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
i++;
lexeme[j] = '=';
lexeme[j+1] = 0;
- tokline[line].issym = 1;
+ l[line].issym = 1;
lex_type = TOK_SYM;
break;
case ';':
@@ -726,20 +729,20 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
comid = get_comment(lexeme, dbg);
if (comid == 0xFFFF) {
- if (line != lineidx && tokline[line].com != 0xFFFF) {
- comid = tokline[line].com;
+ if (line != lineidx && l[line].com != 0xFFFF) {
+ comid = l[line].com;
} else {
comid = comidx;
}
comment[comid] = malloc(j+1);
memcpy(comment[comid], lexeme, j+1);
- tokline[line].com = comid;
+ l[line].com = comid;
if (dbg) {
printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]);
}
comidx += (line == lineidx);
} else {
- tokline[line].com = comid;
+ l[line].com = comid;
if (dbg) {
printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]);
}
@@ -780,7 +783,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
if (!strcasecmp(lexeme, mne[k])) {
lex_type = TOK_OPCODE;
isop = 1;
- tokline[line].mne = k;
+ l[line].mne = k;
address++;
break;
}
@@ -823,30 +826,30 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
if (num) {
switch (lex_type) {
case TOK_SYM:
- tokline[line].op = strtoull(lexeme, NULL, 10);
- mksymbol(sym, tokline[line].op, 1, 0, 0, dbg);
+ l[line].op = strtoull(lexeme, NULL, 10);
+ mksymbol(sym, l[line].op, 1, 0, 0, dbg);
if (isfixup) {
- isfixup = reslv_fixups(dbg);
+ isfixup = reslv_fixups(l, dbg);
}
- tokline[line].sym = get_symid(sym, address, line, dbg);
- isfixup += tokline[line].sym == 0xFFFF;
+ l[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += l[line].sym == 0xFFFF;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[line].opbase = BASE_DEC;
+ l[line].opbase = BASE_DEC;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[line].aop = strtoull(lexeme, NULL, 10);
- tokline[line].aopbase = BASE_DEC;
+ l[line].aop = strtoull(lexeme, NULL, 10);
+ l[line].aopbase = BASE_DEC;
break;
default:
- if (tokline[lineidx].cm != 0xFF) {
- tokline[line].aop = strtoull(lexeme, NULL, 10);
- tokline[line].aopbase = BASE_DEC;
+ if (l[lineidx].cm != 0xFF) {
+ l[line].aop = strtoull(lexeme, NULL, 10);
+ l[line].aopbase = BASE_DEC;
} else {
- tokline[line].op = strtoull(lexeme, NULL, 10);
- tokline[line].opbase = BASE_DEC;
+ l[line].op = strtoull(lexeme, NULL, 10);
+ l[line].opbase = BASE_DEC;
}
break;
@@ -861,8 +864,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
printf("lex(): spaces: %u\n", spaces);
}
if (str[i+spaces] != ':' && str[i+spaces] != '=') {
- tokline[line].sym = get_symid(lexeme, address, line, dbg);
- isfixup += tokline[line].sym == 0xFFFF;
+ l[line].sym = get_symid(lexeme, address, line, dbg);
+ isfixup += l[line].sym == 0xFFFF;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -906,10 +909,10 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
if (lex_type == TOK_COMMENT) {
if (!isstart) {
- tokline[line].etab = tab;
- tokline[line].espace = space;
+ l[line].etab = tab;
+ l[line].espace = space;
if (dbg) {
- printf("lex(): ending tabs: %u, ending spaces: %u\n", tokline[line].etab, tokline[line].espace);
+ printf("lex(): ending tabs: %u, ending spaces: %u\n", l[line].etab, l[line].espace);
}
}
}
@@ -919,7 +922,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
}
if (i) {
- address = update_addr(address, isfixup, line, dbg);
+ address = update_addr(l, address, isfixup, line, dbg);
if (dbg) {
printf("lex(): Next address: $%"PRIX64"\n", address);
printf(
@@ -936,26 +939,26 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
", op: $%016"PRIX64
", aop: $%016"PRIX64
", ln: %u\n"
- , tokline[line].addr
- , tokline[line].dir
- , tokline[line].mne
- , tokline[line].rs
- , tokline[line].am
- , tokline[line].cm
- , tokline[line].opbase
- , tokline[line].com
- , tokline[line].sym
- , tokline[line].op
- , tokline[line].aop
+ , l[line].addr
+ , l[line].dir
+ , l[line].mne
+ , l[line].rs
+ , l[line].am
+ , l[line].cm
+ , l[line].opbase
+ , l[line].com
+ , l[line].sym
+ , l[line].op
+ , l[line].aop
, line);
}
if (ln > linenum || islinenum) {
- tokline[line].linenum = ln;
+ l[line].linenum = ln;
if (ln > linenum) {
linenum+=(10+(ln & 10));
}
} else if (!islinenum) {
- tokline[line].linenum = linenum;
+ l[line].linenum = linenum;
linenum += 10;
}
lineidx += (line == lineidx);