diff --git a/header/common.h b/header/common.h index 8caa831..8b3bf20 100755 --- a/header/common.h +++ b/header/common.h @@ -10,6 +10,9 @@ Here, common macros are specified // Maximum Memory Adresses #define MAX_MEMORY 268435456 +// Size of the label_table +#define LABEL_TABLE_SIZE 268435456 + // Maximum size (charachters) of one token #define MAX_TOKEN_SIZE 10 diff --git a/header/labels.h b/header/labels.h new file mode 100644 index 0000000..fc8b3d2 --- /dev/null +++ b/header/labels.h @@ -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 diff --git a/src/labels.c b/src/labels.c new file mode 100644 index 0000000..08953c8 --- /dev/null +++ b/src/labels.c @@ -0,0 +1,25 @@ +#include +#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]) +{ + +} diff --git a/src/main.c b/src/main.c index 9d7098d..4928232 100755 --- a/src/main.c +++ b/src/main.c @@ -12,12 +12,12 @@ It also is responsible for Argument Parsing #include "../header/argp_commons.h" #include "../header/common.h" +#include "../header/error_analyzer.h" #include "../header/image_saver.h" +#include "../header/instruction_table.h" +#include "../header/labels.h" #include "../header/lexer.h" #include "../header/target_code_generator.h" -#include "../header/error_analyzer.h" -#include "../header/instruction_table.h" - const char *argp_program_version = VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG; @@ -104,24 +104,9 @@ static struct argp argument_parser = int main(int argc, char **argv) { - // Will store our input assembly file - 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(); - + // Parse CLI Arguments // set defaults + struct cmd_arguments arguments; arguments.output_file = DEFAULT_OUT_FILE; arguments.input_file = ""; arguments.print_target_code = 0; @@ -133,34 +118,70 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + // Tokenize the EIPA Assembly + FILE *assembly_file; assembly_file = fopen(arguments.input_file, "r"); - if (assembly_file == NULL) return EXIT_FAILURE; - if(tokens == NULL){ - printf("Error: Could not allocate memory for the tokens array\n"); - 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) + char(*label_tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*label_tokens)); + if (label_tokens == NULL) { - printf("Error: Could not allocate memory for the target code\n"); - return EXIT_FAILURE; + printf("Error: Could not allocate memory for the label_tokens array\n"); + 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) { printf("Error: Could not generate target code - Error %s\n", strerror(error)); return EXIT_FAILURE; } + // Save the target code to a file if (save_img(arguments.output_file, target_code) != 0) { printf("Couldn't save the output file\n"); @@ -173,7 +194,9 @@ int main(int argc, char **argv) if (arguments.print_target_code) print_target_code(target_code); + // Cleanup destroy_instruction_information_hashtable(&instruction_informations); - + shash_destroy_hashtable(&label_table); + return EXIT_SUCCESS; } diff --git a/test/loopprint.eipaasm b/test/loopprint.eipaasm new file mode 100644 index 0000000..b38d8f6 --- /dev/null +++ b/test/loopprint.eipaasm @@ -0,0 +1,4 @@ + INP 30 +$print OUT 30 + JPU print + EOJ \ No newline at end of file