Add resizing to replace_labels_with_adresses

This commit is contained in:
XOR 2023-09-21 22:32:18 +02:00
parent 7bc5189aef
commit 52c0e49073
3 changed files with 11 additions and 6 deletions

View file

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

View file

@ -50,13 +50,18 @@ void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], dynamic
no_label_definition_tokens->used += MAX_TOKEN_SIZE; no_label_definition_tokens->used += 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) void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *tokens, shash_hashtable_t label_table, shash_hashtable_t instruction_information_table)
{ {
// We first copy everything and then modify the tokens array. // We first copy everything and then modify the tokens array.
// This requires a few more cpu cycles but keeps the code simpler // This requires a few more cpu cycles but keeps the code simpler
for(int i = 0; no_label_definition_tokens[i][0] != EOF; i++) for(int i = 0; no_label_definition_tokens[i][0] != EOF; i++)
{ {
strncpy(tokens[i], no_label_definition_tokens[i], MAX_TOKEN_SIZE); if(tokens->size <= tokens->used)
{
resize_dynamic_buffer(tokens);
}
strncpy(((char (*)[10]) tokens->buffer)[i], no_label_definition_tokens[i], MAX_TOKEN_SIZE);
tokens->used += MAX_TOKEN_SIZE;
} }
int i; int i;
@ -74,7 +79,7 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI
// go to label at next token // go to label at next token
i++; i++;
unsigned int *label_addr = shash_get(no_label_definition_tokens[i], strlen(no_label_definition_tokens[i]), &label_table); 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); sprintf(((char (*)[10]) tokens->buffer)[i], "%d", *label_addr);
// skip ';' // skip ';'
i++; i++;
@ -90,5 +95,5 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI
i++; i++;
} }
} }
tokens[i][0] = EOF; ((char (*)[10]) tokens->buffer)[i][0] = EOF;
} }

View file

@ -170,7 +170,7 @@ int main(int argc, char **argv)
printf("Error: Could not allocate memory for the tokens array\n"); printf("Error: Could not allocate memory for the tokens array\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
replace_labels_with_adresses((char (*)[MAX_TOKEN_SIZE]) no_label_definition_tokens.buffer, (char (*)[MAX_TOKEN_SIZE])tokens.buffer, label_table, instruction_informations); replace_labels_with_adresses((char (*)[MAX_TOKEN_SIZE]) no_label_definition_tokens.buffer, &tokens, label_table, instruction_informations);
printf("Removed labels\n"); printf("Removed labels\n");
free(no_label_definition_tokens.buffer); free(no_label_definition_tokens.buffer);