//gets the value of a given symbol bool assembler_get_symbol_value(char *symbol, int *data, int ic) { symbol_table_t *correct_table = NULL; if(!symbol_table_symbol_exists(assembler_data_table, symbol)) { if (!symbol_table_symbol_exists(assembler_call_table, symbol)) { if(!symbol_table_symbol_exists(assembler_extern_table, symbol)) { return false; } // add value in extern table symbol_table_append(&assembler_extern_table_data, symbol, ic); *data = 1; return true; } else { correct_table = assembler_call_table; } } else { correct_table = assembler_data_table; } *data = symbol_table_get_value(correct_table, symbol); return true; }
// gets the value of a symbol from the symbol table int symbol_table_get_value(symbol_table_t *table, char *symbol) { if(0 == strcmp(table->symbol, symbol)) { return table->counter; } if (table->previous == NULL) { return -1; } return symbol_table_get_value(table->previous, symbol); }
struct vm_value * vm_state_get_value(struct vm_state *vm, const char *identifier) { return symbol_table_get_value(vm->symtab, identifier); }
//full proccess a code file, this is the assembler's "main" function void assembler_proccess_file(FILE *code_file, FILE *obj_file, FILE *entry_file, FILE *ext_file) { char buf[100]; int ic = 100; int dc = 0; char line[ASSEMBLER_LINE_SIZE] = {0, }; char current_data = 0; symbol_table_t *current_table = NULL; symbol_table_t *containing_table = NULL; if (NULL == code_file) { printf("Error, should be given a valid file.\n"); goto Exit; } /* First loop */ while (NULL != fgets(line, ASSEMBLER_LINE_SIZE, code_file)) { assembler_first_loop(line, &ic, &dc); } if (!should_compile) { printf("not compiling!\n"); return; } fseek(code_file, 0, SEEK_SET); /* adds ic to all data symbols */ symbol_table_add_to_all_symbols(assembler_data_table, ic); ic = 100; /* Second loop */ while (NULL != fgets(line, ASSEMBLER_LINE_SIZE, code_file)) { assembler_second_loop(line, obj_file, &ic); } //write data fclose(data_file); data_file = fopen("/tmp/data.section", "rb"); while(0!=fread(¤t_data,1,1,data_file)) { fprintf(obj_file, "%04d %06d\n", atoi(base4(buf, ic)),atoi(base4(buf, (current_data & 0xFFF)))); ic++; } //write extrnal current_table = assembler_extern_table_data->previous; while(current_table != NULL) { fprintf(ext_file, "%s %X\n", current_table->symbol, current_table->counter); current_table = current_table->previous; } //write entry current_table = assembler_entry_table->previous; while(current_table != NULL) { //if in: call, data then write if(symbol_table_symbol_exists(assembler_data_table, current_table->symbol)) { containing_table = assembler_data_table; } else if(symbol_table_symbol_exists(assembler_call_table, current_table->symbol)) { containing_table = assembler_call_table; } else { printf("No such entry: %s\n", current_table->symbol); should_compile = false; } fprintf(entry_file, "%s %X\n", current_table->symbol, symbol_table_get_value(containing_table, current_table->symbol)); current_table = current_table->previous; } if (!should_compile) { printf("not compiling!\n"); return; } Exit: return; }