From 0d2d613bc8b0f92b82254e4c79640012aab8a9a0 Mon Sep 17 00:00:00 2001 From: XOR Date: Tue, 5 Sep 2023 00:42:36 +0200 Subject: [PATCH] Add label support to error detection --- design.txt | 10 ++++++++++ header/error_analyzer.h | 2 +- src/error_analyzer.c | 40 ++++++++++++++++++++++++++++++++++++---- src/labels.c | 11 +++++++++++ src/main.c | 2 +- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/design.txt b/design.txt index e065bf6..2deb684 100755 --- a/design.txt +++ b/design.txt @@ -24,3 +24,13 @@ Instruction Layout: Y: Memory Adress (28bit) Labels will get resolved by a preprocessor and transformed to adresses on assembling time +A label can be defined at any position in a instruction (except in the comment) by writing $label_name. +So you could e.g. write + +$label OUT 35 + +but also + +OUT $label 35 + +Note that the first variant is recommendet for readability diff --git a/header/error_analyzer.h b/header/error_analyzer.h index 328769d..63d87b8 100644 --- a/header/error_analyzer.h +++ b/header/error_analyzer.h @@ -7,6 +7,6 @@ /* Check if there exist errors in the Assembly using the tokens array Returns amount of errors found, and prints out the error's to stdout */ -unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table); +unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table, shash_hashtable_t label_table); #endif diff --git a/src/error_analyzer.c b/src/error_analyzer.c index 0d6ea00..525141e 100644 --- a/src/error_analyzer.c +++ b/src/error_analyzer.c @@ -5,7 +5,7 @@ #include #include -unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table) +unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table, shash_hashtable_t label_table) { printf("=====================\nErrors found:\n"); unsigned int error_count = 0; @@ -33,9 +33,41 @@ unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t switch (current_token_info->supports_adress_label) { case 1: - /* Labels are not implemented yet - When they are, we need to check wether we got a label, - And if yes, we need to make sure the label exists */ + { + printf("Got instruction '%s', which supports labels\n", tokens[i]); + + // Check label at next token + i++; + if(atoi(tokens[i]) == 0) + { + unsigned int *label_addr = shash_get(tokens[i], strlen(tokens[i]), &label_table); + if(label_addr != NULL) + { + printf("Label %s found. It corrosponds to adress %d\n", tokens[i], *label_addr); + } + else + { + printf("Label %s does not exist\n", tokens[i]); + } + + // Check that the next token is a ; + i++; + if (tokens[i][0] != ';') + { + printf("ERROR: Expected end of instruction at %d but found \"%s\"\n", i, tokens[i]); + error_count++; + + // Loop to the next ; + for (; tokens[i][0] != ';'; i++) + { + if (tokens[i][0] == EOF) + break; + } + } + break; + } + + } case 0: { // Check the adress stored at the next token diff --git a/src/labels.c b/src/labels.c index c6cb3a8..094e8d8 100644 --- a/src/labels.c +++ b/src/labels.c @@ -1,15 +1,26 @@ #include +#include #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; + unsigned int instruction_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); + + //Store the adress we are at on the heap, as the hashtable only stores pointers to data + unsigned int *heap_pointer_to_adress = malloc(sizeof(instruction_index)); + *heap_pointer_to_adress = instruction_index; + shash_set(label_tokens[token_index]+1, strlen(label_tokens[token_index]+1), heap_pointer_to_adress, label_table); + } + if(label_tokens[token_index][0] == ';') + { + instruction_index++; } token_index++; } diff --git a/src/main.c b/src/main.c index 483cbef..40ffd4f 100755 --- a/src/main.c +++ b/src/main.c @@ -156,7 +156,7 @@ int main(int argc, char **argv) // 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)); + printf("Found %d errors\n", check_token_errors(no_label_definition_tokens, instruction_informations, label_table)); exit(0); // Part II of processing labels