diff --git a/header/instruction_table.h b/header/instruction_table.h index 0c6bca7..f8cfff5 100644 --- a/header/instruction_table.h +++ b/header/instruction_table.h @@ -4,9 +4,14 @@ #include "../header/shash/shash.h" #include -// Create a hashtable that contains +#define MAX_INSTRUCTION_COUNT 16 + +// Create a hashtable that contains informations about the instructions. shash_hashtable_t create_instruction_information_hastable(void); +// Destroy the information table +void destroy_instruction_information_hashtable(shash_hashtable_t *instruction_information_table) + typedef struct { unsigned int requires_adress; @@ -17,6 +22,7 @@ typedef struct typedef struct { char *name; + unsigned int name_length; unsigned int requires_adress; unsigned int supports_adress_label; u_int8_t opcode; diff --git a/src/instruction_table.c b/src/instruction_table.c index 94171ba..3a0f182 100644 --- a/src/instruction_table.c +++ b/src/instruction_table.c @@ -1,68 +1,114 @@ #include "../header/instruction_table.h" +#include "../header/common.h" #include "../header/shash/shash.h" #include +// Change this code if you want to configure changes to the instruction format +instruction_information_config_t config[] = { + {.name = "INC", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 0, + .opcode = 0b0001}, + + {.name = "OUT", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 0, + .opcode = 0b0010}, + + {.name = "LDA", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 0, + .opcode = 0b0011}, + + {.name = "STA", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 0, + .opcode = 0b0100}, + + {.name = "INC", + .name_length = 3, + .requires_adress = 0, + .supports_adress_label = 0, + .opcode = 0b0101}, + + {.name = "DEC", + .name_length = 3, + .requires_adress = 0, + .supports_adress_label = 0, + .opcode = 0b0110}, + + {.name = "JPP", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 1, + .opcode = 0b0111}, + + {.name = "JPZ", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 1, + .opcode = 0b1000}, + + {.name = "JPN", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 1, + .opcode = 0b1001}, + + {.name = "JPU", + .name_length = 3, + .requires_adress = 1, + .supports_adress_label = 1, + .opcode = 0b1010}, + + {.name = "EOJ", + .name_length = 3, + .requires_adress = 0, + .supports_adress_label = 0, + .opcode = 0b1011}, + {0} +}; + shash_hashtable_t create_instruction_information_hastable(void) { - shash_hashtable_t instruction_table; + shash_hashtable_t instruction_information_table; + // We make the hashtable bigger than needed to avoid collosions + shash_init_hashtable(&instruction_information_table, MAX_INSTRUCTION_COUNT * 10); - // Change this code if you want to configure changes to the instruction format - instruction_information_config_t config[] = { - {.name = "INC", - .requires_adress = 1, - .supports_adress_label = 0, - .opcode = 0b0001}, + instruction_information_config_t current_instr_config; + unsigned int i = 0; + do + { + current_instr_config = config[i]; + instruction_information_t *current_instruction_info = malloc(sizeof(instruction_information_t)); - {.name = "OUT", - .requires_adress = 1, - .supports_adress_label = 0, - .opcode = 0b0010}, + current_instruction_info->requires_adress = current_instr_config.requires_adress; + current_instruction_info->supports_adress_label = current_instr_config.supports_adress_label; + current_instruction_info->opcode = current_instr_config.opcode; - {.name = "LDA", - .requires_adress = 1, - .supports_adress_label = 0, - .opcode = 0b0011}, + shash_set(current_instr_config.name, current_instr_config.name_length, current_instruction_info, &instruction_information_table); + i++; + } while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0); - {.name = "STA", - .requires_adress = 1, - .supports_adress_label = 0, - .opcode = 0b0100}, - - {.name = "INC", - .requires_adress = 0, - .supports_adress_label = 0, - .opcode = 0b0101}, - - {.name = "DEC", - .requires_adress = 0, - .supports_adress_label = 0, - .opcode = 0b0110}, - - {.name = "JPP", - .requires_adress = 1, - .supports_adress_label = 1, - .opcode = 0b0111}, - - {.name = "JPZ", - .requires_adress = 1, - .supports_adress_label = 1, - .opcode = 0b1000}, - - {.name = "JPN", - .requires_adress = 1, - .supports_adress_label = 1, - .opcode = 0b1001}, - - {.name = "JPU", - .requires_adress = 1, - .supports_adress_label = 1, - .opcode = 0b1010}, - - {.name = "EOJ", - .requires_adress = 0, - .supports_adress_label = 0, - .opcode = 0b1011}, - {0} - }; - return instruction_table; + return instruction_information_table; +} + +void destroy_instruction_information_hashtable(shash_hashtable_t *instruction_information_table) +{ + //Destroy all the information structs stored in the hashtable + instruction_information_config_t current_instr_config; + unsigned int i = 0; + do + { + current_instr_config = config[i]; + free(shash_get(current_instr_config.name, current_instr_config.name_length, instruction_information_table)); + i++; + } while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0); + + // Destroy the actual hashtable + shash_destroy_hashtable(instruction_information_table); } diff --git a/src/main.c b/src/main.c index fa44aa1..763a0f5 100755 --- a/src/main.c +++ b/src/main.c @@ -133,8 +133,6 @@ int main(int argc, char **argv) if (assembly_file == NULL) return EXIT_FAILURE; - - if(tokens == NULL){ printf("Error: Could not allocate memory for the tokens array\n");