Competle init/destroy functions of instruction information table

This commit is contained in:
XOR 2023-04-15 23:16:35 +02:00
parent e81e0a6e94
commit d1748f65b1
3 changed files with 110 additions and 60 deletions

View file

@ -4,9 +4,14 @@
#include "../header/shash/shash.h" #include "../header/shash/shash.h"
#include <sys/types.h> #include <sys/types.h>
// 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); 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 typedef struct
{ {
unsigned int requires_adress; unsigned int requires_adress;
@ -17,6 +22,7 @@ typedef struct
typedef struct typedef struct
{ {
char *name; char *name;
unsigned int name_length;
unsigned int requires_adress; unsigned int requires_adress;
unsigned int supports_adress_label; unsigned int supports_adress_label;
u_int8_t opcode; u_int8_t opcode;

View file

@ -1,68 +1,114 @@
#include "../header/instruction_table.h" #include "../header/instruction_table.h"
#include "../header/common.h"
#include "../header/shash/shash.h" #include "../header/shash/shash.h"
#include <stdio.h> #include <stdio.h>
shash_hashtable_t create_instruction_information_hastable(void) // Change this code if you want to configure changes to the instruction format
{ instruction_information_config_t config[] = {
shash_hashtable_t instruction_table;
// Change this code if you want to configure changes to the instruction format
instruction_information_config_t config[] = {
{.name = "INC", {.name = "INC",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0001}, .opcode = 0b0001},
{.name = "OUT", {.name = "OUT",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0010}, .opcode = 0b0010},
{.name = "LDA", {.name = "LDA",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0011}, .opcode = 0b0011},
{.name = "STA", {.name = "STA",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0100}, .opcode = 0b0100},
{.name = "INC", {.name = "INC",
.name_length = 3,
.requires_adress = 0, .requires_adress = 0,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0101}, .opcode = 0b0101},
{.name = "DEC", {.name = "DEC",
.name_length = 3,
.requires_adress = 0, .requires_adress = 0,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b0110}, .opcode = 0b0110},
{.name = "JPP", {.name = "JPP",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 1, .supports_adress_label = 1,
.opcode = 0b0111}, .opcode = 0b0111},
{.name = "JPZ", {.name = "JPZ",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 1, .supports_adress_label = 1,
.opcode = 0b1000}, .opcode = 0b1000},
{.name = "JPN", {.name = "JPN",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 1, .supports_adress_label = 1,
.opcode = 0b1001}, .opcode = 0b1001},
{.name = "JPU", {.name = "JPU",
.name_length = 3,
.requires_adress = 1, .requires_adress = 1,
.supports_adress_label = 1, .supports_adress_label = 1,
.opcode = 0b1010}, .opcode = 0b1010},
{.name = "EOJ", {.name = "EOJ",
.name_length = 3,
.requires_adress = 0, .requires_adress = 0,
.supports_adress_label = 0, .supports_adress_label = 0,
.opcode = 0b1011}, .opcode = 0b1011},
{0} {0}
}; };
return instruction_table;
shash_hashtable_t create_instruction_information_hastable(void)
{
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);
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));
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;
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);
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);
} }

View file

@ -134,8 +134,6 @@ int main(int argc, char **argv)
if (assembly_file == NULL) if (assembly_file == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
if(tokens == NULL){ if(tokens == NULL){
printf("Error: Could not allocate memory for the tokens array\n"); printf("Error: Could not allocate memory for the tokens array\n");
return EXIT_FAILURE; return EXIT_FAILURE;