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>
// 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 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 current_instr_config;
instruction_information_config_t config[] = { unsigned int i = 0;
{.name = "INC", do
.requires_adress = 1, {
.supports_adress_label = 0, current_instr_config = config[i];
.opcode = 0b0001}, instruction_information_t *current_instruction_info = malloc(sizeof(instruction_information_t));
{.name = "OUT", current_instruction_info->requires_adress = current_instr_config.requires_adress;
.requires_adress = 1, current_instruction_info->supports_adress_label = current_instr_config.supports_adress_label;
.supports_adress_label = 0, current_instruction_info->opcode = current_instr_config.opcode;
.opcode = 0b0010},
{.name = "LDA", shash_set(current_instr_config.name, current_instr_config.name_length, current_instruction_info, &instruction_information_table);
.requires_adress = 1, i++;
.supports_adress_label = 0, } while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0);
.opcode = 0b0011},
{.name = "STA", return instruction_information_table;
.requires_adress = 1, }
.supports_adress_label = 0,
.opcode = 0b0100}, void destroy_instruction_information_hashtable(shash_hashtable_t *instruction_information_table)
{
{.name = "INC", //Destroy all the information structs stored in the hashtable
.requires_adress = 0, instruction_information_config_t current_instr_config;
.supports_adress_label = 0, unsigned int i = 0;
.opcode = 0b0101}, do
{
{.name = "DEC", current_instr_config = config[i];
.requires_adress = 0, free(shash_get(current_instr_config.name, current_instr_config.name_length, instruction_information_table));
.supports_adress_label = 0, i++;
.opcode = 0b0110}, } while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0);
{.name = "JPP", // Destroy the actual hashtable
.requires_adress = 1, shash_destroy_hashtable(instruction_information_table);
.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;
} }

View file

@ -133,8 +133,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");