diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2020-06-01 15:15:17 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2020-06-01 15:15:17 -0400 |
commit | df81112b8369eeca5788a6f28c6b6b85ca911a95 (patch) | |
tree | fed73a1fb94485d4bc9dd25966d626c9338ccb05 /assemble.c | |
parent | f600bf84c453649f625b06ba232a7464e7093b32 (diff) |
Did some more refactoring to the assembler.
- Refactored the symbol, and fixup table to now use a
linked list
- Added support for local symbols to the assembler.
- Rewrote SuBEditor, and SuBAsm to use local symbols.
Diffstat (limited to 'assemble.c')
-rw-r--r-- | assemble.c | 85 |
1 files changed, 69 insertions, 16 deletions
@@ -42,7 +42,7 @@ uint64_t get_val(token *t, uint64_t addr, uint8_t size, uint8_t dbg) { break; case TOK_SYM: case TOK_LABEL: - tmp_val = use_symbol("", t->word, addr, 1, dbg); + tmp_val = (t->sym) ? t->sym->val : addr; t = t->next; break; } @@ -201,7 +201,7 @@ uint64_t handle_opcode(token *t, bytecount *bc, uint8_t isasm, uint64_t address, val.u64 = get_val(t, address, (rs != 0xFF) ? rs : 0, dbg); } opcode = opcodes[inst][type]; - if (inst == 80) { + if (inst == 78) { if (type == IMM) { rs = 1; } else { @@ -333,15 +333,17 @@ uint64_t parse_tokens(token *t, bytecount *bc, uint8_t isasm, uint64_t address, return address; } -token *make_token(uint8_t id, uint8_t type, uint64_t value, char *str) { +token *make_token(uint8_t id, uint8_t type, uint64_t value, char *str, symbol *s) { token *new_tok = malloc(sizeof(token)); (last_tok) ? (last_tok->next = new_tok) : (tokens = new_tok); new_tok->id = id; new_tok->type = type; - if (!str[0]) { - new_tok->qword = value; - } else { + if (s) { + new_tok->sym = s; + } else if (str[0]) { new_tok->str = str; + } else { + new_tok->qword = value; } new_tok->next = NULL; last_tok = new_tok; @@ -355,27 +357,78 @@ void assemble(line *ln, bytecount *bc, uint8_t dbg) { } } -void free_tokens(token *t, uint16_t count) { +static inline void free_tokens(token *t, uint16_t count) { token *tok; - while (t != NULL) { - if (count--) { - break; - } + for (; t != NULL && t >= tok; t = t->next) { tok = t; free(tok); - t = t->next; } } void free_lines() { line *l = lines; line *ln; - for (; l != NULL; l = l->next) { - if (l < ln) { - break; - } + for (; l != NULL && l >= ln; l = l->next) { free_tokens(l->tok, l->count); ln = l; free(ln); } } + +void free_locals(symbol *s) { + symbol *sym; + for (; s != NULL && s >= sym; s = s->next) { + sym = s; + if (sym->name != NULL) { + free(sym->name); + } + free(sym); + } +} + +void free_symbols() { + symbol *s = symbols; + symbol *sym = symbols; + for (; s != NULL && s >= sym; s = s->next) { + if (s->local) { + free_locals(s->local); + } + sym = s; + if (sym->name != NULL) { + free(sym->name); + } + free(sym); + } +} + +static inline void free_fixups() { + fixup *f = fixups; + fixup *fix; + for (; f != NULL && f >= fix; f = f->next) { + /*free_tokens(l->tok, l->count);*/ + fix = f; + free(fix); + } +} + +void cleanup() { + uint16_t i; + if (lines) { + free_lines(); + } + if (symbols) { + free_symbols(); + } + if (fixups) { + free_fixups(); + } + while (i < stridx || i < comidx) { + if (i < stridx) { + free(string[i]); + } + if (i < comidx) { + free(comment[i]); + } + i++; + } +} |