summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c514
1 files changed, 307 insertions, 207 deletions
diff --git a/lexer.c b/lexer.c
index 47aea24..3e460d6 100644
--- a/lexer.c
+++ b/lexer.c
@@ -1,21 +1,28 @@
#include "asmmon.h"
-struct symbol *symbols = 0;
-struct fixup *fixups = 0;
+void init_symbol() {
+ uint16_t i = 0;
+ for (; i < 0x1000; i++) {
+ symbols[i] = 0;
+ fixups[i] = 0;
+ }
+}
-struct symbol *mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t useid, uint16_t id, uint8_t dbg) {
- struct symbol *s;
+uint16_t sym_count = 0;
+uint16_t mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t useid, uint16_t id, uint8_t dbg) {
uint16_t i = 0;
- uint8_t flag;
- for (s = symbols; s; s = s->nxt) {
+ uint8_t flag = 0;
+ for (; i < sym_count; i++) {
if (useid) {
- flag = id == s->id;
+ flag = id == symbols[i]->id;
} else {
- flag = !strcmp(name, s->name);
+ if (name[0] == symbols[i]->name[0]) {
+ flag = !strcmp(name, symbols[i]->name);
+ }
}
if (flag) {
if (def) {
- if (s->def) {
+ if (symbols[i]->def) {
if (dbg) {
printf("mksymbol(): oof, you cannot redefine the symbol: %s\n", name);
}
@@ -23,74 +30,86 @@ struct symbol *mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t use
} else {
defined = 0;
}
- s->def = def;
- s->val = val;
+ symbols[i]->def = def;
+ symbols[i]->val = val;
+ symbols[i]->id = i;
if (dbg) {
- printf("mksymbol(): def: %u, val: $%016llX, name: %s\n", def, val, name);
+ printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s\n", def, val, name);
+ printf("mksymbol(): i: $%X, id: $%04X\n", i, symbols[i]->id);
}
+ return symbols[i]->id;
+ } else {
+ return symbols[i]->id;
}
- return s;
}
- i++;
}
- s = malloc(sizeof(*s) + strlen(name));
- s->def = def;
- s->val = val;
- strcpy(s->name, name);
- s->nxt = symbols;
- s->id = i;
- symbols = s;
+ symbols[i] = malloc(sizeof(*symbols) + strlen(name));
+ symbols[i]->def = def;
+ symbols[i]->val = val;
+ strcpy(symbols[i]->name, name);
+ symbols[i]->id = sym_count++;
defined = 0;
if (dbg) {
- printf("mksymbol(): def: %u, val: $%016llX, name: %s, id: $%04X\n", def, val, name, i);
+ printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s, id: $%04X\n", def, val, name, sym_count-1);
}
- return s;
+ return sym_count-1;
}
uint64_t use_symbol(const char *name, uint16_t id, uint64_t val, uint8_t useid, uint8_t dbg) {
- struct symbol *s = mksymbol(name, 0, 0, useid, id, dbg);
+ uint16_t i;
+ i = mksymbol(name, 0, 0, useid, id, dbg);
+ uint8_t is_defined = (i != 0xFFFF);
val++;
- if (s->def) {
- return s->val;
- } else {
- if (dbg) {
- printf("use_symbol(): ");
- printf("oof, symbol ");
- if (useid) {
- printf("id $%04X, ", id);
- } else {
- printf("%s, ", name);
+ if (dbg) {
+ puts("use_symbol(): We also got here.");
+ printf("use_symbol(): i: $%X\n", i);
+ }
+ if (symbols[i] != NULL) {
+ if (symbols[i]->def) {
+ return symbols[i]->val;
+ } else {
+ if (dbg) {
+ printf("use_symbol(): ");
+ printf("oof, symbol ");
+ if (useid) {
+ printf("id $%04X, ", id);
+ } else {
+ printf("%s, ", name);
+ }
+ puts("does not exist, yet.");
}
- puts("does not exist, yet.");
+ return val-1;
}
- return val-1;
}
+ return val-1;
}
uint8_t set_symval(const char *name, uint16_t id, uint64_t val, uint8_t useid, uint8_t dbg) {
- struct symbol *s = mksymbol(name, 0, 0, useid, id, dbg);
- if (s->def) {
- s->val = val;
- return 1;
- } else {
- if (dbg) {
- printf("set_symval(): ");
- printf("oof, symbol ");
- if (useid) {
- printf("id $%04X, ", id);
- } else {
- printf("%s, ", name);
+ uint16_t i = mksymbol(name, 0, 0, useid, id, dbg);
+ if (symbols[i] != NULL) {
+ if (symbols[i]->def) {
+ symbols[i]->val = val;
+ return 1;
+ } else {
+ if (dbg) {
+ printf("set_symval(): ");
+ printf("oof, symbol ");
+ if (useid) {
+ printf("id $%04X, ", id);
+ } else {
+ printf("%s, ", name);
+ }
+ puts("does not exist, yet.");
}
- puts("does not exist, yet.");
+ return 0;
}
- return 0;
}
}
char *get_symname(uint16_t id, uint8_t dbg) {
- struct symbol *s = mksymbol("", 0, 0, 1, id, dbg);
- if (s->def) {
- return s->name;
+ /*struct symbol *s = mksymbol("", 0, 0, 1, id, dbg);*/
+ if (symbols[id]->def) {
+ return symbols[id]->name;
} else {
if (dbg) {
printf("get_symname(): oof, symbol id $%04X, has not been defined, yet.\n", id);
@@ -99,30 +118,35 @@ 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) {
- struct symbol *s = mksymbol(name, 0, 0, 0, 0, dbg);
- if (s->def) {
- return s->id;
+ uint16_t i = mksymbol(name, 0, 0, 0, 0, dbg);
+ if (dbg) {
+ printf("get_symid(): Symbol ID: $%X, i: $%X.\n", symbols[i]->id, i);
+ }
+ if (symbols[i]->def) {
+ return symbols[i]->id;
} else {
if (dbg) {
printf("get_symid(): oof, symbol %s, does not exist, yet.\n", name);
}
- struct fixup *f = malloc(sizeof(*f));
- f->nxt = fixups;
- f->adr = val;
- f->ln = ln;
- f->s = s;
- fixups = f;
+ fixups[fixup_cnt] = malloc(sizeof(*fixups));
+ fixups[fixup_cnt]->adr = val;
+ fixups[fixup_cnt]->ln = ln;
+ fixups[fixup_cnt]->s = symbols[i];
+ fixup_cnt++;
return 0xFFFF;
}
}
-uint16_t get_comment(const char *cmnt, uint8_t dbg) {
+uint16_t get_comment(const char *com, uint8_t dbg) {
uint16_t i = 0;
uint8_t iscom = 0;
for (; i < comidx; i++) {
if (comment[i] != NULL) {
- iscom = !strcmp(cmnt, comment[i]);
+ if (com[0] == comment[i][0]) {
+ iscom = !strcmp(com, comment[i]);
+ }
} else {
break;
}
@@ -138,24 +162,59 @@ uint16_t get_comment(const char *cmnt, uint8_t dbg) {
}
if (i == comidx) {
if (dbg) {
- printf("get_comment(): oof, the comment \"%s\", was not found in the comment table.\n", cmnt);
+ printf("get_comment(): oof, the comment \"%s\", was not found in the comment table.\n", com);
+ }
+ return 0xFFFF;
+ }
+ if (dbg) {
+ printf("get_comment(): Found comment \"%s\", in the table, at index $%04X.\n", com, i);
+ }
+ return i;
+}
+
+uint16_t get_string(const char *str, uint8_t dbg) {
+ uint16_t i = 0;
+ uint8_t isstr = 0;
+ for (; i < stridx; i++) {
+ if (string[i] != NULL) {
+ if (str[0] == string[i][0]) {
+ isstr = !strcmp(str, string[i]);
+ }
+ } else {
+ break;
+ }
+ if (isstr) {
+ break;
+ }
+ }
+ if (string[i] == NULL) {
+ if (dbg) {
+ printf("get_string(): oof, the index $%04X is NULL.\n", i);
+ }
+ return 0xFFFF;
+ }
+ if (i == stridx) {
+ if (dbg) {
+ printf("get_string(): oof, the string \"%s\", was not found in the string table.\n", str);
}
return 0xFFFF;
}
if (dbg) {
- printf("get_comment(): Found comment \"%s\", in the table, at index $%04X.\n", cmnt, i);
+ printf("get_string(): Found string \"%s\", in the table, at index $%04X.\n", str, i);
}
return i;
}
uint16_t reslv_fixups(uint8_t dbg) {
uint16_t i = 0, j = 0;
- struct fixup *f;
- f = fixups;
- for (; f;) {
- /*printf("f: $%016llX, f->nxt: $%016llX, f->s->name: %s, f->s->val: $%016llX\n", &f, &f->nxt, f->s->name, f->s->val);*/
- if (f->s->def) {
- if (f->ln == 0xFFFF) {
+ for (; fixups[j]; j++) {
+ /*printf("f: $%016"PRIX64", f->nxt: $%016"PRIX64", f->s->name: %s, f->s->val: $%016"PRIX64"\n", &f, &f->nxt, f->s->name, f->s->val);*/
+ 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;
+ /*if (f->ln == 0xFFFF) {
addr[f->adr] = f->s->val & 0xFF;
if (f->s->val & 0xFF00)
addr[f->adr+1] = f->s->val >> 8;
@@ -170,29 +229,25 @@ uint16_t reslv_fixups(uint8_t dbg) {
addr[f->adr+7] = f->s->val >> 56;
}
} else {
- tokline[f->ln].sym = f->s->id;
- }
+ }*/
} else {
if (dbg) {
- printf("reslv_fixups(): oof, undefined reference to '%s', at $%016llX.\n", f->s->name, f->adr);
+ printf("reslv_fixups(): oof, undefined reference to '%s', at $%016"PRIX64".\n", fixups[j]->s->name, fixups[j]->adr);
}
i++;
}
- f = f->nxt;
- j++;
}
return i;
}
-uint64_t update_addr(uint64_t address, uint8_t fixup, uint8_t dbg) {
+uint64_t update_addr(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 l = lineidx;
uint16_t symid = tokline[l].sym;
uint16_t str = tokline[l].str;
uint16_t com = tokline[l].com;
@@ -232,10 +287,13 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint8_t dbg) {
return address;
}
if (((flags & 0x53) == 0x42)) {
- if (isfixup && symid == 0xFFFF && (opcodes[mne][IMPL] == 0xFF)) {
+ if (fixup && symid == 0xFFFF && (opcodes[mne][IMPL] == 0xFF)) {
value = address;
+ } else if (opcodes[mne][IMPL] != 0xFF && symid == 0xFFFF) {
+ value = 0;
} else {
value = use_symbol("", symid, address, 1, dbg);
+ /*value = symbols[symid]->val;*/
}
} else {
value = tokline[l].op;
@@ -247,14 +305,14 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint8_t dbg) {
}
}
if (dbg) {
- printf("update_addr(): value: $%llX\n", value);
+ printf("update_addr(): value: $%"PRIX64"\n", value);
}
switch (dir) {
case DIR_ORG:
address = value;
if (dbg) {
printf("update_addr(): ");
- printf("Set the Program Counter's Origin to $%llX.\n", address);
+ printf("Set the Program Counter's Origin to $%"PRIX64".\n", address);
}
break;
case DIR_BYTE:
@@ -359,16 +417,33 @@ uint64_t update_addr(uint64_t address, uint8_t fixup, uint8_t dbg) {
}
if (dbg) {
printf("update_addr(): ");
- printf("Address: $%llX\n", address);
+ printf("Address: $%"PRIX64"\n", address);
}
return address;
}
+uint16_t find_line(uint16_t ln, uint8_t dbg) {
+ uint16_t i = 0;
+ for (; i < lineidx && tokline[i].linenum != ln; i++);
+ if (tokline[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);
+ }
+ return i;
+}
+
uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
char sym[0x100];
uint16_t i = 0;
uint16_t j = 0;
uint16_t comid = 0;
+ uint16_t strid = 0;
+ uint16_t symid = 0;
+ uint16_t line = 0;
lex_type = 0xFF;
uint8_t k = 0;
uint8_t rs = 0;
@@ -382,21 +457,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
uint8_t tab = 0;
uint8_t isstart = 1;
uint8_t fall = 0;
- tokline[lineidx].dir = 0xFF;
- tokline[lineidx].mne = 0xFF;
- tokline[lineidx].rs = 0xFF;
- tokline[lineidx].am = 0xFF;
- tokline[lineidx].cm = 0xFF;
- tokline[lineidx].opbase = 0xFF;
- tokline[lineidx].aopbase = 0xFF;
- tokline[lineidx].islabel = 0;
- tokline[lineidx].issym = 0;
- tokline[lineidx].str = 0xFFFF;
- tokline[lineidx].com = 0xFFFF;
- tokline[lineidx].sym = 0xFFFF;
- tokline[lineidx].op = 0;
- tokline[lineidx].aop = 0;
- tokline[lineidx].addr = address;
+ uint8_t done = 0;
while (isdigit(str[i]) && !isspace(str[i])) {
lnum[j++] = str[i++];
@@ -409,7 +470,26 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
} else {
ln = linenum;
}
- uint8_t done = 0;
+ for (; isspace(str[i]); i++);
+ line = find_line(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;
while (str[i] != '\0' && str[i] != '\n') {
space = 0;
tab = 0;
@@ -423,10 +503,10 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
printf("lex(): tab: %u, space: %u\n", tab, space);
}
if (isstart) {
- tokline[lineidx].stab = tab;
- tokline[lineidx].sspace = space;
+ tokline[line].stab = tab;
+ tokline[line].sspace = space;
if (dbg) {
- printf("lex(): starting tabs: %u, starting spaces: %u\n", tokline[lineidx].stab, tokline[lineidx].sspace);
+ printf("lex(): starting tabs: %u, starting spaces: %u\n", tokline[line].stab, tokline[line].sspace);
}
}
if (isspace(str[i])) {
@@ -443,12 +523,14 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
if (!isop) {
for (k = 0; k < 5; k++) {
- if (!strcasecmp(lexeme, dir_t[k])) {
- lex_type = TOK_DIR;
- break;
+ if (tolower(lexeme[0]) == dir_t[k][0]) {
+ if (!strcasecmp(lexeme, dir_t[k])) {
+ lex_type = TOK_DIR;
+ break;
+ }
}
}
- tokline[lineidx].dir = k;
+ tokline[line].dir = k;
} else {
lex_type = TOK_RS;
switch (tolower(lexeme[j-1])) {
@@ -466,7 +548,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
break;
}
address++;
- tokline[lineidx].rs = rs;
+ tokline[line].rs = rs;
isop = 0;
}
break;
@@ -475,21 +557,33 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
while (str[i] != '\"') {
lexeme[j++] = str[i++];
}
- lexeme[j] = '\0';
- string[stridx] = malloc(j+1);
- memcpy(string[stridx], lexeme, j+1);
- tokline[lineidx].str = stridx;
- if (dbg) {
- printf("lex(): str[0x%04X]: %s\n", stridx, string[stridx]);
+ strid = get_string(lexeme, dbg);
+ if (strid == 0xFFFF) {
+ if (line != lineidx && tokline[line].str != 0xFFFF) {
+ strid = tokline[line].str;
+ } else {
+ strid = stridx;
+ }
+ string[strid] = malloc(j+1);
+ memcpy(string[strid], lexeme, j+1);
+ tokline[line].str = strid;
+ if (dbg) {
+ printf("lex(): str[0x%04X]: %s\n", strid, string[strid]);
+ }
+ stridx += (line == lineidx);
+ } else {
+ tokline[line].str = strid;
+ if (dbg) {
+ printf("lex(): str[0x%04X]: %s\n", strid, string[strid]);
+ }
}
- stridx++;
lex_type = TOK_STRING;
break;
case '#':
lexeme[j] = '#';
lexeme[j+1] = '\0';
lexeme[j+2] = '\0';
- tokline[lineidx].am = IMM;
+ tokline[line].am = IMM;
lex_type = TOK_IMM;
break;
case '$':
@@ -500,27 +594,27 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
switch (lex_type) {
case TOK_SYM:
- tokline[lineidx].op = strtoull(lexeme, NULL, 16);
- mksymbol(sym, tokline[lineidx].op, 1, 0, 0, dbg);
- tokline[lineidx].sym = get_symid(sym, address, lineidx, dbg);
- isfixup += tokline[lineidx].sym == 0xFFFF;
+ 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);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[lineidx].opbase = BASE_HEX;
+ tokline[line].opbase = BASE_HEX;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[lineidx].aop = strtoull(lexeme, NULL, 16);
- tokline[lineidx].aopbase = BASE_HEX;
+ tokline[line].aop = strtoull(lexeme, NULL, 16);
+ tokline[line].aopbase = BASE_HEX;
break;
default:
- if (tokline[lineidx].cm != 0xFF) {
- tokline[lineidx].aop = strtoull(lexeme, NULL, 16);
- tokline[lineidx].aopbase = BASE_HEX;
+ if (tokline[line].cm != 0xFF) {
+ tokline[line].aop = strtoull(lexeme, NULL, 16);
+ tokline[line].aopbase = BASE_HEX;
} else {
- tokline[lineidx].op = strtoull(lexeme, NULL, 16);
- tokline[lineidx].opbase = BASE_HEX;
+ tokline[line].op = strtoull(lexeme, NULL, 16);
+ tokline[line].opbase = BASE_HEX;
}
break;
@@ -535,27 +629,27 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
switch (lex_type) {
case TOK_SYM:
- tokline[lineidx].op = strtoull(lexeme, NULL, 2);
- mksymbol(sym, tokline[lineidx].op, 1, 0, 0, dbg);
- tokline[lineidx].sym = get_symid(sym, address, lineidx, dbg);
- isfixup += tokline[lineidx].sym == 0xFFFF;
+ 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);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[lineidx].opbase = BASE_BIN;
+ tokline[line].opbase = BASE_BIN;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[lineidx].aop = strtoull(lexeme, NULL, 2);
- tokline[lineidx].aopbase = BASE_BIN;
+ tokline[line].aop = strtoull(lexeme, NULL, 2);
+ tokline[line].aopbase = BASE_BIN;
break;
default:
if (tokline[lineidx].cm != 0xFF) {
- tokline[lineidx].aop = strtoull(lexeme, NULL, 2);
- tokline[lineidx].aopbase = BASE_BIN;
+ tokline[line].aop = strtoull(lexeme, NULL, 2);
+ tokline[line].aopbase = BASE_BIN;
} else {
- tokline[lineidx].op = strtoull(lexeme, NULL, 2);
- tokline[lineidx].opbase = BASE_BIN;
+ tokline[line].op = strtoull(lexeme, NULL, 2);
+ tokline[line].opbase = BASE_BIN;
}
break;
@@ -565,20 +659,20 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
case '+':
lexeme[j] = '+';
lexeme[j+1] = '\0';
- tokline[lineidx].cm = 0;
+ tokline[line].cm = 0;
lex_type = TOK_PLUS;
break;
case '-':
lexeme[j] = '-';
lexeme[j+1] = '\0';
- tokline[lineidx].cm = 1;
+ tokline[line].cm = 1;
lex_type = TOK_MINUS;
break;
case '(':
lexeme[j] = '(';
lexeme[j+1] = '\0';
lexeme[j+2] = '\0';
- tokline[lineidx].am = IND;
+ tokline[line].am = IND;
break;
case ')':
i++;
@@ -587,9 +681,9 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
while (isspace(str[i])) {
lexeme[j++] = str[i++];
}
- if (tokline[lineidx].am == IND && tolower(str[i]) == 'y') {
+ if (tokline[line].am == IND && tolower(str[i]) == 'y') {
lexeme[j++] = 'y';
- tokline[lineidx].am = INDY;
+ tokline[line].am = INDY;
}
lexeme[j] = '\0';
} else {
@@ -603,18 +697,18 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
while (isspace(str[i])) {
lexeme[j++] = str[i++];
}
- if (tokline[lineidx].am == IND && tolower(str[i]) == 'x') {
- tokline[lineidx].am = INDX;
+ if (tokline[line].am == IND && tolower(str[i]) == 'x') {
+ tokline[line].am = INDX;
lexeme[j++] = 'x';
i++;
} else {
switch (tolower(str[i])) {
case 'x':
- tokline[lineidx].am = ZMX;
+ tokline[line].am = ZMX;
lexeme[j++] = 'x';
break;
case 'y':
- tokline[lineidx].am = ZMY;
+ tokline[line].am = ZMY;
lexeme[j++] = 'y';
break;
}
@@ -626,13 +720,13 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = ':';
lexeme[j+1] = '\0';
lex_type = TOK_LABEL;
- tokline[lineidx].islabel = 1;
+ tokline[line].islabel = 1;
mksymbol(sym, address, 1, 0, 0, dbg);
if (isfixup) {
isfixup = reslv_fixups(dbg);
}
- tokline[lineidx].sym = get_symid(sym, address, lineidx, dbg);
- isfixup += tokline[lineidx].sym == 0xFFFF;
+ tokline[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += (tokline[line].sym == 0xFFFF);
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -641,7 +735,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
i++;
lexeme[j] = '=';
lexeme[j+1] = 0;
- tokline[lineidx].issym = 1;
+ tokline[line].issym = 1;
lex_type = TOK_SYM;
break;
case ';':
@@ -652,15 +746,20 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
comid = get_comment(lexeme, dbg);
if (comid == 0xFFFF) {
- comment[comidx] = malloc(j+1);
- memcpy(comment[comidx], lexeme, j+1);
- tokline[lineidx].com = comidx;
+ if (line != lineidx && tokline[line].com != 0xFFFF) {
+ comid = tokline[line].com;
+ } else {
+ comid = comidx;
+ }
+ comment[comid] = malloc(j+1);
+ memcpy(comment[comid], lexeme, j+1);
+ tokline[line].com = comid;
if (dbg) {
- printf("lex(): com[0x%04X]: %s\n", comidx, comment[comidx]);
+ printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]);
}
- comidx++;
+ comidx += (line == lineidx);
} else {
- tokline[lineidx].com = comid;
+ tokline[line].com = comid;
if (dbg) {
printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]);
}
@@ -697,12 +796,14 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
isop = 0;
if (j == 3 && str[i] != ':') {
for (k = 0; k < OPNUM; k++) {
- if (!strcasecmp(lexeme, mne[k])) {
- lex_type = TOK_OPCODE;
- isop = 1;
- tokline[lineidx].mne = k;
- address++;
- break;
+ if (toupper(lexeme[0]) == mne[k][0]) {
+ if (!strcasecmp(lexeme, mne[k])) {
+ lex_type = TOK_OPCODE;
+ isop = 1;
+ tokline[line].mne = k;
+ address++;
+ break;
+ }
}
}
}
@@ -742,30 +843,30 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
if (num) {
switch (lex_type) {
case TOK_SYM:
- tokline[lineidx].op = strtoull(lexeme, NULL, 10);
- mksymbol(sym, tokline[lineidx].op, 1, 0, 0, dbg);
+ tokline[line].op = strtoull(lexeme, NULL, 10);
+ mksymbol(sym, tokline[line].op, 1, 0, 0, dbg);
if (isfixup) {
isfixup = reslv_fixups(dbg);
}
- tokline[lineidx].sym = get_symid(sym, address, lineidx, dbg);
- isfixup += tokline[lineidx].sym == 0xFFFF;
+ tokline[line].sym = get_symid(sym, address, line, dbg);
+ isfixup += tokline[line].sym == 0xFFFF;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
- tokline[lineidx].opbase = BASE_DEC;
+ tokline[line].opbase = BASE_DEC;
break;
case TOK_PLUS:
case TOK_MINUS:
- tokline[lineidx].aop = strtoull(lexeme, NULL, 10);
- tokline[lineidx].aopbase = BASE_DEC;
+ tokline[line].aop = strtoull(lexeme, NULL, 10);
+ tokline[line].aopbase = BASE_DEC;
break;
default:
if (tokline[lineidx].cm != 0xFF) {
- tokline[lineidx].aop = strtoull(lexeme, NULL, 10);
- tokline[lineidx].aopbase = BASE_DEC;
+ tokline[line].aop = strtoull(lexeme, NULL, 10);
+ tokline[line].aopbase = BASE_DEC;
} else {
- tokline[lineidx].op = strtoull(lexeme, NULL, 10);
- tokline[lineidx].opbase = BASE_DEC;
+ tokline[line].op = strtoull(lexeme, NULL, 10);
+ tokline[line].opbase = BASE_DEC;
}
break;
@@ -780,8 +881,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[lineidx].sym = get_symid(lexeme, address, lineidx, dbg);
- isfixup += tokline[lineidx].sym == 0xFFFF;
+ tokline[line].sym = get_symid(lexeme, address, line, dbg);
+ isfixup += tokline[line].sym == 0xFFFF;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -826,28 +927,25 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
if (lex_type == TOK_COMMENT) {
if (!isstart) {
- tokline[lineidx].etab = tab;
- tokline[lineidx].espace = space;
+ tokline[line].etab = tab;
+ tokline[line].espace = space;
if (dbg) {
- printf("lex(): ending tabs: %u, ending spaces: %u\n", tokline[lineidx].etab, tokline[lineidx].espace);
+ printf("lex(): ending tabs: %u, ending spaces: %u\n", tokline[line].etab, tokline[line].espace);
}
}
}
if (lex_type != TOK_SYM) {
- for (k = 0; lexeme[k] != '\0';) {
- lexeme[k] = 0;
- ++k;
- }
+ memset(lexeme, 0, strlen(lexeme)+1);
lex_type = 0xFF;
}
}
if (i) {
- address = update_addr(address, isfixup, dbg);
+ address = update_addr(address, isfixup, line, dbg);
if (dbg) {
- printf("lex(): Next address: $%llX\n", address);
+ printf("lex(): Next address: $%"PRIX64"\n", address);
printf(
"lex(): "
- "address: $%llX"
+ "address: $%"PRIX64
", dir: %u"
", mne: $%02X"
", rs: %u"
@@ -856,30 +954,32 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
", opbase: %u"
", com: $%04X"
", sym: $%04X"
- ", op: $%016X"
- ", aop: $%016X"
- ", ln: %i\n"
- , tokline[lineidx].addr
- , tokline[lineidx].dir
- , tokline[lineidx].mne
- , tokline[lineidx].rs
- , tokline[lineidx].am
- , tokline[lineidx].cm
- , tokline[lineidx].opbase
- , tokline[lineidx].com
- , tokline[lineidx].sym
- , tokline[lineidx].op
- , tokline[lineidx].aop
- , lineidx);
- }
- if (ln > linenum) {
- linenum+=(10+(ln & 10));
- tokline[lineidx].linenum = ln;
+ ", 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
+ , line);
+ }
+ if (ln > linenum || islinenum) {
+ tokline[line].linenum = ln;
+ if (ln > linenum) {
+ linenum+=(10+(ln & 10));
+ }
} else if (!islinenum) {
- tokline[lineidx].linenum = linenum;
+ tokline[line].linenum = linenum;
linenum += 10;
}
- lineidx++;
+ lineidx += (line == lineidx);
}
return address;
}