/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. ADDR is given as the byte offset from the first instruction. The SymbolTable must be able to resize itself as more elements are added. Note that NAME may point to a temporary array, so it is not safe to simply store the NAME pointer. You must store a copy of the given string. If ADDR is not word-aligned, you should call addr_alignment_incorrect() and return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists in the table, you should call name_already_exists() and return -1. If memory allocation fails, you should call allocation_failed(). Otherwise, you should store the symbol name and address and return 0. */ int add_to_table(SymbolTable* table, const char* name, uint32_t addr) { /* YOUR CODE HERE */ if((addr % 4) != 0) { addr_alignment_incorrect(); return -1; } if(table->mode == SYMTBL_UNIQUE_NAME && get_addr_for_symbol(table, name) != -1) { name_already_exists(name); return -1; } if(table->len >= table->cap) { int new_cap = table->cap * 2; table->tbl = realloc(table->tbl, new_cap * sizeof(Symbol)); if(table->tbl == NULL) allocation_failed(); table->cap = new_cap; } table->tbl[table->len].name = strdup(name); table->tbl[table->len].addr = addr; table->len++; return 0; }
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. ADDR is given as the byte offset from the first instruction. The SymbolTable must be able to resize itself as more elements are added. Note that NAME may point to a temporary array, so it is not safe to simply store the NAME pointer. You must store a copy of the given string. If ADDR is not word-aligned, you should call addr_alignment_incorrect() and return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists in the table, you should call name_already_exists() and return -1. If memory allocation fails, you should call allocation_failed(). Otherwise, you should store the symbol name and address and return 0. */ int add_to_table(SymbolTable* table, const char* name, uint32_t addr) { if ((addr % 4) != 0) { addr_alignment_incorrect(); return -1; } if ((*table).len == (*table).cap) { int r = (4 * (*table).cap) * (sizeof(Symbol)); table->tbl = realloc(table->tbl, r); if (table->tbl == NULL) { allocation_failed(); return -1; } table->cap = table->cap * 4; } int i; Symbol* t = table->tbl; for (i = 0; i < table->len; i++) { if (strcmp(t[i].name, name) == 0 && table->mode) { name_already_exists(name); return -1; } } Symbol *temp = malloc(sizeof(Symbol)); temp->name = malloc(sizeof(name)*4 + 1); strcpy(temp->name, name); temp->addr = addr; (*table).len += 1; t[i] = *temp; free(temp); return 0; }
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. ADDR is given as the byte offset from the first instruction. The SymbolTable must be able to resize itself as more elements are added. Note that NAME may point to a temporary array, so it is not safe to simply store the NAME pointer. You must store a copy of the given string. If ADDR is not word-aligned, you should call addr_alignment_incorrect() and return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists in the table, you should call name_already_exists() and return -1. If memory allocation fails, you should call allocation_failed(). Otherwise, you should store the symbol name and address and return 0. */ int add_to_table(SymbolTable* table, const char* name, uint32_t addr) { /* YOUR CODE HERE */ if (addr % 4 != 0) { addr_alignment_incorrect(); return -1; } if (table->mode == SYMTBL_UNIQUE_NAME && get_addr_for_symbol(table, name) != -1) { name_already_exists(name); return -1; } if (!table) return -1; Symbol* to_add = (Symbol*) malloc(sizeof(Symbol)); if (!to_add) allocation_failed(); Symbol* sym = table->tbl; char* new_name = create_copy_of_str(name); to_add->name = new_name; to_add->addr = addr; if (table->len == table->cap) { sym = realloc(sym, table->len * SCALING_FACTOR * sizeof(Symbol)); table->len += 1; table->cap *= SCALING_FACTOR; sym[table->len - 1] = *to_add; table->tbl = sym; } else { sym[table->len] = *to_add; table->len++; } return 0; }