Snapshot of non working code where I now forgot what I was doing...

This commit is contained in:
XOR 2023-08-17 21:43:38 +02:00
parent f22407ddd0
commit 7ffca91f95
5 changed files with 103 additions and 36 deletions

View file

@ -10,6 +10,9 @@ Here, common macros are specified
// Maximum Memory Adresses // Maximum Memory Adresses
#define MAX_MEMORY 268435456 #define MAX_MEMORY 268435456
// Size of the label_table
#define LABEL_TABLE_SIZE 268435456
// Maximum size (charachters) of one token // Maximum size (charachters) of one token
#define MAX_TOKEN_SIZE 10 #define MAX_TOKEN_SIZE 10

12
header/labels.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef _LABELS_H_
#define _LABELS_H_
#include "../header/common.h"
#include "../header/shash/shash.h"
void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *label_table);
void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], char no_label_definition_tokens[][MAX_TOKEN_SIZE]);
void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE]);
#endif

25
src/labels.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
#include "../header/labels.h"
void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *label_table)
{
unsigned int token_index = 0;
while (label_tokens[token_index][0] != EOF)
{
if(label_tokens[token_index][0] == '$')
{
printf("Found label: %s\n", label_tokens[token_index]+1);
}
token_index++;
}
}
void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], char no_label_definition_tokens[][MAX_TOKEN_SIZE])
{
}
void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE])
{
}

View file

@ -12,12 +12,12 @@ It also is responsible for Argument Parsing
#include "../header/argp_commons.h" #include "../header/argp_commons.h"
#include "../header/common.h" #include "../header/common.h"
#include "../header/error_analyzer.h"
#include "../header/image_saver.h" #include "../header/image_saver.h"
#include "../header/instruction_table.h"
#include "../header/labels.h"
#include "../header/lexer.h" #include "../header/lexer.h"
#include "../header/target_code_generator.h" #include "../header/target_code_generator.h"
#include "../header/error_analyzer.h"
#include "../header/instruction_table.h"
const char *argp_program_version = const char *argp_program_version =
VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG; VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG;
@ -104,24 +104,9 @@ static struct argp argument_parser =
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// Will store our input assembly file // Parse CLI Arguments
FILE *assembly_file;
// Stores the generated tokens
char(*tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*tokens));
// Stores the generated binary target code
__uint32_t *target_code = calloc(MAX_MEMORY, sizeof(target_code));
// stores the CLI options
struct cmd_arguments arguments;
// A variable for storing retrun values of functions that return errors on failure
int error;
shash_hashtable_t instruction_informations = create_instruction_information_hastable();
// set defaults // set defaults
struct cmd_arguments arguments;
arguments.output_file = DEFAULT_OUT_FILE; arguments.output_file = DEFAULT_OUT_FILE;
arguments.input_file = ""; arguments.input_file = "";
arguments.print_target_code = 0; arguments.print_target_code = 0;
@ -133,34 +118,70 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Tokenize the EIPA Assembly
FILE *assembly_file;
assembly_file = fopen(arguments.input_file, "r"); assembly_file = fopen(arguments.input_file, "r");
if (assembly_file == NULL) if (assembly_file == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
if(tokens == NULL){ char(*label_tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*label_tokens));
printf("Error: Could not allocate memory for the tokens array\n"); if (label_tokens == NULL)
return EXIT_FAILURE;
}
lexer(assembly_file, tokens);
fclose(assembly_file);
printf("Found %d errors\n", check_token_errors(tokens, instruction_informations));
if(target_code == NULL)
{ {
printf("Error: Could not allocate memory for the target code\n"); printf("Error: Could not allocate memory for the label_tokens array\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
error = gen_target_code(tokens, target_code); lexer(assembly_file, label_tokens);
fclose(assembly_file);
// Part I of processing labels
shash_hashtable_t label_table;
shash_init_hashtable(&label_table, LABEL_TABLE_SIZE);
build_label_table(label_tokens, &label_table);
printf("Built label table\n");
char(*no_label_definition_tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*no_label_definition_tokens));
if (no_label_definition_tokens == NULL)
{
printf("Error: Could not allocate memory for the no_label_defintion_tokens array\n");
return EXIT_FAILURE;
}
remove_label_definition_tokens(label_tokens, no_label_definition_tokens);
printf("Removed label defintions\n");
free(label_tokens);
// Check if the EIPA Assembly contains errors with the no_label_definition_tokens array
shash_hashtable_t instruction_informations = create_instruction_information_hastable();
printf("Found %d errors\n", check_token_errors(no_label_definition_tokens, instruction_informations));
// Part II of processing labels
char(*tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*tokens));
if (tokens == NULL)
{
printf("Error: Could not allocate memory for the tokens array\n");
return EXIT_FAILURE;
}
replace_labels_with_adresses(no_label_definition_tokens, tokens);
printf("Removed labels\n");
free(no_label_definition_tokens);
// Generate the target code
__uint32_t *target_code = calloc(MAX_MEMORY, sizeof(target_code));
if (target_code == NULL)
{
printf("Error: Could not allocate memory for the target code\n");
return EXIT_FAILURE;
}
int error = gen_target_code(tokens, target_code);
if (error != 0) if (error != 0)
{ {
printf("Error: Could not generate target code - Error %s\n", strerror(error)); printf("Error: Could not generate target code - Error %s\n", strerror(error));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Save the target code to a file
if (save_img(arguments.output_file, target_code) != 0) if (save_img(arguments.output_file, target_code) != 0)
{ {
printf("Couldn't save the output file\n"); printf("Couldn't save the output file\n");
@ -173,7 +194,9 @@ int main(int argc, char **argv)
if (arguments.print_target_code) if (arguments.print_target_code)
print_target_code(target_code); print_target_code(target_code);
// Cleanup
destroy_instruction_information_hashtable(&instruction_informations); destroy_instruction_information_hashtable(&instruction_informations);
shash_destroy_hashtable(&label_table);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

4
test/loopprint.eipaasm Normal file
View file

@ -0,0 +1,4 @@
INP 30
$print OUT 30
JPU print
EOJ