Add resizing to replace_labels_with_adresses
This commit is contained in:
parent
7bc5189aef
commit
52c0e49073
3 changed files with 11 additions and 6 deletions
|
@ -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 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
|
||||
|
|
13
src/labels.c
13
src/labels.c
|
@ -50,13 +50,18 @@ void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], dynamic
|
|||
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.
|
||||
// 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);
|
||||
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;
|
||||
|
@ -74,7 +79,7 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI
|
|||
// 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);
|
||||
sprintf(((char (*)[10]) tokens->buffer)[i], "%d", *label_addr);
|
||||
|
||||
// skip ';'
|
||||
i++;
|
||||
|
@ -90,5 +95,5 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI
|
|||
i++;
|
||||
}
|
||||
}
|
||||
tokens[i][0] = EOF;
|
||||
((char (*)[10]) tokens->buffer)[i][0] = EOF;
|
||||
}
|
||||
|
|
|
@ -170,7 +170,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((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");
|
||||
free(no_label_definition_tokens.buffer);
|
||||
|
||||
|
|
Loading…
Reference in a new issue