diff --git a/header/target_code_generator.h b/header/target_code_generator.h index e4a6dee..84bb873 100755 --- a/header/target_code_generator.h +++ b/header/target_code_generator.h @@ -1,4 +1,5 @@ #include "../header/common.h" +#include "dyn_buf.h" #ifndef _TAGET_CODE_GENERATOR_H_ #define _TAGET_CODE_GENERATOR_H_ @@ -9,7 +10,7 @@ __uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint32_t adress); // Generate the machine code from tokens into target_code. Returns 0 on success, errno integer on failure -int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code); +int gen_target_code(char tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *target_code); //Print out the machine code in target_code void print_target_code(__uint32_t *target_code); diff --git a/src/main.c b/src/main.c index fcdcc32..f066cb8 100755 --- a/src/main.c +++ b/src/main.c @@ -183,7 +183,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - int error = gen_target_code((char (*)[MAX_TOKEN_SIZE])tokens.buffer, (__uint32_t *)target_code.buffer); + int error = gen_target_code((char (*)[MAX_TOKEN_SIZE])tokens.buffer, &target_code); if (error != 0) { printf("Error: Could not generate target code - Error %s\n", strerror(error)); diff --git a/src/target_code_generator.c b/src/target_code_generator.c index 7f3c4f0..4ff6374 100755 --- a/src/target_code_generator.c +++ b/src/target_code_generator.c @@ -34,7 +34,7 @@ __uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint return instruction; } -int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) +int gen_target_code(char tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *target_code) { // stores which token we are currently looking at unsigned int token_index = 0; @@ -48,6 +48,11 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) { // indice of a new instruction instruction_index++; + target_code->used++; + if(target_code->size <= target_code->used) + { + resize_dynamic_buffer(target_code); + } } else if (strcmp(tokens[token_index], "INP") == 0) { @@ -58,8 +63,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_INP, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_INP, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -74,8 +79,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_OUT, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_OUT, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -90,8 +95,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_LDA, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_LDA, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -106,8 +111,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_STA, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_STA, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -115,8 +120,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) } else if (strcmp(tokens[token_index], "INC") == 0) { - target_code[instruction_index] = get_target_instruction(INSTR_INC, 0, 0); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_INC, 0, 0); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -124,8 +129,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) } else if (strcmp(tokens[token_index], "DEC") == 0) { - target_code[instruction_index] = get_target_instruction(INSTR_DEC, 0, 0); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_DEC, 0, 0); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -141,8 +146,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_JPP, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPP, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -157,8 +162,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_JPZ, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPZ, 1, adress); + if (((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -173,8 +178,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_JPN, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPN, 1, adress); + if ( ((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -189,8 +194,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) token_index++; sscanf(tokens[token_index], "%d", &adress); - target_code[instruction_index] = get_target_instruction(INSTR_JPU, 1, adress); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPU, 1, adress); + if ( ((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW; @@ -198,8 +203,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code) } else if (strcmp(tokens[token_index], "EOJ") == 0) { - target_code[instruction_index] = get_target_instruction(INSTR_EOJ, 0, 0); - if (target_code[instruction_index] == 0) + ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_EOJ, 0, 0); + if ( ((__uint32_t *) target_code->buffer)[instruction_index] == 0) { printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS); return EOVERFLOW;