diff options
author | Freya Murphy <freya@freyacat.org> | 2024-09-09 12:41:49 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-09-09 12:41:49 -0400 |
commit | 2ed275821676a0d5baea6c7fd843d71c72c2342c (patch) | |
tree | 480297f28e5c42d02a47b3b94027a7abe507d010 /masm/symtbl.c | |
download | mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.gz mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.bz2 mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.zip |
initial mips32 (r2000ish mips32r6) assembler
Diffstat (limited to 'masm/symtbl.c')
-rw-r--r-- | masm/symtbl.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/masm/symtbl.c b/masm/symtbl.c new file mode 100644 index 0000000..b75c752 --- /dev/null +++ b/masm/symtbl.c @@ -0,0 +1,57 @@ +#include <merror.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "parse.h" + +#define SYMTBL_INIT_LEN 24 + +int symtbl_init(struct symbol_table *sym_tbl) +{ + sym_tbl->len = SYMTBL_INIT_LEN; + sym_tbl->count = 0; + sym_tbl->symbols = malloc(sizeof(struct symbol) * SYMTBL_INIT_LEN); + + if (sym_tbl->symbols == NULL) { + ERROR("cannot alloc"); + return M_ERROR; + } + + return M_SUCCESS; +} + +void symtbl_free(struct symbol_table *sym_tbl) +{ + free(sym_tbl->symbols); +} + +int symtbl_push(struct symbol_table *sym_tbl, struct symbol sym) +{ + if (sym_tbl->count >= sym_tbl->len) { + sym_tbl->len *= 2; + sym_tbl->symbols = realloc(sym_tbl->symbols, + sizeof(struct symbol) * sym_tbl->len); + if (sym_tbl->symbols == NULL) { + ERROR("cannot relloc"); + return M_ERROR; + } + } + + sym_tbl->symbols[sym_tbl->count++] = sym; + return M_SUCCESS; +} + +int symtbl_find(struct symbol_table *sym_tbl, struct symbol **ptr, + const char name[MAX_LEX_LENGTH]) +{ + for (uint32_t i = 0; i < sym_tbl->count; i++) { + struct symbol *sym = &sym_tbl->symbols[i]; + if (strcmp(sym->name, name) == 0) { + if (ptr != NULL) + *ptr = sym; + return M_SUCCESS; + } + } + return M_ERROR; +} |