diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-11-03 21:12:15 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-11-03 21:15:30 +0000 |
commit | b8f6232bb266bc895267f8dde700aa67a91207d6 (patch) | |
tree | 126d98013c4713d9f6d8e526f28bce671c183d7c | |
parent | 92f4f9011d431f9c197aff56a3dbb8a3963b4197 (diff) | |
download | ovm-b8f6232bb266bc895267f8dde700aa67a91207d6.tar.gz ovm-b8f6232bb266bc895267f8dde700aa67a91207d6.tar.bz2 ovm-b8f6232bb266bc895267f8dde700aa67a91207d6.zip |
Refactor assembler to use prog_t structure
Set the program structure correctly with a header using the parsed
global instruction.
-rw-r--r-- | asm/main.c | 17 | ||||
-rw-r--r-- | asm/parser.c | 36 | ||||
-rw-r--r-- | asm/parser.h | 4 |
3 files changed, 31 insertions, 26 deletions
@@ -88,9 +88,8 @@ int main(int argc, char *argv[]) #if VERBOSE >= 2 printf("\t[%sPARSER%s]: Beginning parse...\n", TERM_YELLOW, TERM_RESET); #endif - size_t number = 0; - inst_t *instructions = NULL; - perr_t parse_error = parse_stream(&tokens, &instructions, &number); + prog_t *program = NULL; + perr_t parse_error = parse_stream(&tokens, &program); if (parse_error) { size_t column = 0; @@ -108,21 +107,21 @@ int main(int argc, char *argv[]) } #if VERBOSE >= 1 printf("\t[%sPARSER%s]: %lu tokens -> %lu instructions\n", TERM_GREEN, - TERM_RESET, tokens.available, number); + TERM_RESET, tokens.available, program->count); #endif #if VERBOSE >= 2 printf("\t[%sPARSER%s]: Instructions parsed:\n", TERM_GREEN, TERM_RESET); - for (size_t i = 0; i < number; ++i) + for (size_t i = 0; i < program->count; ++i) { printf("\t[%lu]: ", i); - inst_print(instructions[i], stdout); + inst_print(program->instructions[i], stdout); printf("\n"); } #endif fp = fopen(out_file, "wb"); - insts_write_bytecode_file(instructions, number, fp); + prog_write_file(program, fp); fclose(fp); #if VERBOSE >= 1 printf("[%sASSEMBLER%s]: Wrote bytecode to `%s`\n", TERM_GREEN, TERM_RESET, @@ -137,7 +136,7 @@ end: free(TOKEN_STREAM_AT(tokens.data, i).str); free(tokens.data); } - if (instructions) - free(instructions); + if (program) + free(program); return ret; } diff --git a/asm/parser.c b/asm/parser.c index 196c09c..4eff0a5 100644 --- a/asm/parser.c +++ b/asm/parser.c @@ -477,7 +477,7 @@ label_t search_labels(label_t *labels, size_t n, char *name) } perr_t process_presults(presult_t *results, size_t res_count, - inst_t **instructions, size_t *inst_count) + prog_t **program_ptr) { #if VERBOSE >= 2 printf("[%sprocess_presults%s]: Results found\n", TERM_YELLOW, TERM_RESET); @@ -515,7 +515,7 @@ perr_t process_presults(presult_t *results, size_t res_count, darr_t label_registry = {0}; darr_init(&label_registry, sizeof(label_t)); - *inst_count = 0; + word inst_count = 0; for (size_t i = 0; i < res_count; ++i) { presult_t res = results[i]; @@ -524,19 +524,19 @@ perr_t process_presults(presult_t *results, size_t res_count, case PRES_LABEL: { label_t label = {.name = res.label, .name_size = strlen(res.label), - .addr = (*inst_count) + 1}; + .addr = inst_count + 1}; darr_append_bytes(&label_registry, (byte *)&label, sizeof(label)); break; } case PRES_RELATIVE_ADDRESS: { s_word offset = res.address; - if (offset < 0 && ((word)(-offset)) > *inst_count) + if (offset < 0 && ((word)(-offset)) > inst_count) { free(label_registry.data); return PERR_INVALID_RELATIVE_ADDRESS; } - results[i].instruction.operand.as_word = ((s_word)*inst_count) + offset; - (*inst_count)++; + results[i].instruction.operand.as_word = ((s_word)inst_count) + offset; + inst_count++; break; } case PRES_GLOBAL_LABEL: { @@ -547,14 +547,15 @@ perr_t process_presults(presult_t *results, size_t res_count, case PRES_LABEL_ADDRESS: case PRES_COMPLETE_RESULT: default: - (*inst_count)++; + inst_count++; break; } } darr_t instr_darr = {0}; - darr_init(&instr_darr, sizeof(**instructions)); + darr_init(&instr_darr, sizeof(inst_t)); + prog_header_t header = {0}; if (global_start_defined) { label_t label = @@ -566,9 +567,7 @@ perr_t process_presults(presult_t *results, size_t res_count, free(label_registry.data); return PERR_UNKNOWN_LABEL; } - inst_t initial_jump = INST_JUMP_ABS(label.addr); - darr_append_bytes(&instr_darr, (byte *)&initial_jump, sizeof(initial_jump)); - (*inst_count)++; + header.start_address = label.addr; } for (size_t i = 0; i < res_count; ++i) @@ -606,11 +605,17 @@ perr_t process_presults(presult_t *results, size_t res_count, } free(label_registry.data); - *instructions = (inst_t *)instr_darr.data; + prog_t *program = + malloc(sizeof(**program_ptr) + (sizeof(inst_t) * inst_count)); + program->header = header; + program->count = inst_count; + memcpy(program->instructions, instr_darr.data, instr_darr.used); + free(instr_darr.data); + *program_ptr = program; return PERR_OK; } -perr_t parse_stream(token_stream_t *stream, inst_t **ret, size_t *size) +perr_t parse_stream(token_stream_t *stream, prog_t **program_ptr) { darr_t presults = {0}; darr_init(&presults, sizeof(presult_t)); @@ -634,8 +639,9 @@ perr_t parse_stream(token_stream_t *stream, inst_t **ret, size_t *size) ++stream->used; } - perr_t perr = process_presults((presult_t *)presults.data, - presults.used / sizeof(presult_t), ret, size); + perr_t perr = + process_presults((presult_t *)presults.data, + presults.used / sizeof(presult_t), program_ptr); for (size_t i = 0; i < (presults.used / sizeof(presult_t)); ++i) { presult_t res = ((presult_t *)presults.data)[i]; diff --git a/asm/parser.h b/asm/parser.h index b0091d2..7aea7a2 100644 --- a/asm/parser.h +++ b/asm/parser.h @@ -59,7 +59,7 @@ typedef struct label_t search_labels(label_t *, size_t, char *); perr_t parse_next(token_stream_t *, presult_t *); -perr_t process_presults(presult_t *, size_t, inst_t **, size_t *); -perr_t parse_stream(token_stream_t *, inst_t **, size_t *); +perr_t process_presults(presult_t *, size_t, prog_t **); +perr_t parse_stream(token_stream_t *, prog_t **); #endif |