Finish label processing

This commit is contained in:
XOR 2023-09-07 18:41:46 +02:00
parent 0d2d613bc8
commit 640e8bcdaf
4 changed files with 48 additions and 14 deletions

View file

@ -8,5 +8,5 @@ void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *l
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]);
void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t label_table, shash_hashtable_t instruction_information_table);
#endif

View file

@ -34,20 +34,16 @@ unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t
{
case 1:
{
printf("Got instruction '%s', which supports labels\n", tokens[i]);
// Check label at next token
i++;
if(atoi(tokens[i]) == 0)
// Only execute the label recognition if the adress part is not a number/is a label
if(atoi(tokens[i]) == 0)
{
unsigned int *label_addr = shash_get(tokens[i], strlen(tokens[i]), &label_table);
if(label_addr != NULL)
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]);
printf("ERROR: Label %s has not been defined (Used at token %d)\n", tokens[i], i);
}
// Check that the next token is a ;

View file

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "../header/labels.h"
#include "../header/instruction_table.h"
void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *label_table)
{
@ -38,7 +39,45 @@ void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], char no
no_label_definition_tokens[new_index][0] = EOF;
}
void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE])
void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t label_table, shash_hashtable_t instruction_information_table)
{
// We first copy everything and then modify the tokens array.
// This requires a few more cpu cycles but keeps the code simpler
for(int i = 0; no_label_definition_tokens[i][0] != EOF; i++)
{
strncpy(tokens[i], no_label_definition_tokens[i], MAX_TOKEN_SIZE);
}
int i;
for(i = 0; no_label_definition_tokens[i][0] != EOF; i++)
{
instruction_information_t *current_token_info = shash_get(no_label_definition_tokens[i], (unsigned int)strlen(no_label_definition_tokens[i]), &instruction_information_table);
if(current_token_info == NULL)
{
printf("ERROR: Instruction %s not found at label processing stage. \n This error should not occur. Please contact %s\n", no_label_definition_tokens[i], EIPA_BUG_ADRESS);
exit(EXIT_FAILURE);
}
if(current_token_info->supports_adress_label)
{
// go to label at next token
i++;
unsigned int *label_addr = shash_get(no_label_definition_tokens[i], strlen(no_label_definition_tokens[i]), &label_table);
sprintf(tokens[i], "%d", *label_addr);
// skip ';'
i++;
}
else if(current_token_info->requires_adress)
{
// skip over adress and ';'
i += 2;
}
else
{
// skip ';'
i++;
}
}
tokens[i][0] = EOF;
}

View file

@ -157,7 +157,6 @@ 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, label_table));
exit(0);
// Part II of processing labels
char(*tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*tokens));
@ -166,7 +165,7 @@ int main(int argc, char **argv)
printf("Error: Could not allocate memory for the tokens array\n");
return EXIT_FAILURE;
}
replace_labels_with_adresses(no_label_definition_tokens, tokens);
replace_labels_with_adresses(no_label_definition_tokens, tokens, label_table, instruction_informations);
printf("Removed labels\n");
free(no_label_definition_tokens);