diff options
| author | Mistivia <i@mistivia.com> | 2025-03-24 20:37:41 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-03-24 20:37:41 +0800 |
| commit | 4188f791787be19f32226b8ac0f213f61cdd4666 (patch) | |
| tree | 54a38cbcaf67647c2e0e865f7f1dd537456817c1 /src/as_analyzer.c | |
| parent | 4f7f0aa49844756dbf430f35600d7c88e1a6a730 (diff) | |
finish analyzer
Diffstat (limited to 'src/as_analyzer.c')
| -rw-r--r-- | src/as_analyzer.c | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/src/as_analyzer.c b/src/as_analyzer.c index dbc1bd2..a3e7ea4 100644 --- a/src/as_analyzer.c +++ b/src/as_analyzer.c @@ -3,11 +3,6 @@ #include <stddef.h> #include <string.h> -struct sym_table_entry { - const char * name; - size_t offset; -}; - const char * compose_section_label(allocator_t alct, const char * section, const char * name) { size_t section_len = strlen(section); size_t name_len = strlen(name); @@ -36,18 +31,43 @@ void process_section_label(allocator_t alct, prog_t prog) { } size_t instr_size(instr_t instr) { - // TODO - return 0; + return op_size(instr->op); +} + +struct sym_table new_sym_table(allocator_t alct) { + struct sym_table tbl; + tbl.cap = 16; + tbl.size = 0; + tbl.buf = allocate(alct, sizeof(struct sym_table_entry) * 16); + return tbl; +} + +void sym_table_add(allocator_t alct, struct sym_table* tbl, const char* name, int pos) { + if (tbl->cap == tbl->size) { + void *old_buf = tbl->buf; + tbl->buf = allocate(alct, sizeof(struct sym_table_entry) * tbl->cap * 2); + memcpy(tbl->buf, old_buf, sizeof(struct sym_table_entry) * tbl->cap); + tbl->cap = tbl->cap * 2; + } + tbl->buf[tbl->size] = (struct sym_table_entry){.name = name, .offset = pos,}; + tbl->size += 1; } struct sym_table analyze_prog(allocator_t alct, prog_t prog) { process_section_label(alct, prog); stmt_t * stmts = prog->stmts->stmts; + struct sym_table tbl = new_sym_table(alct); + size_t cur_pos = 0; for (int i = 0; ; i++) { if (stmts[i] == NULL) break; + stmt_t stmt = stmts[i]; + if (stmt->label) { + sym_table_add(alct, &tbl, stmt->label->name, cur_pos); + } + if (stmt->instr) { + cur_pos += instr_size(stmt->instr); + } } - struct sym_table tbl; - // TODO return tbl; } |
