Add resizing to target_code_generator

This commit is contained in:
XOR 2023-09-21 22:51:01 +02:00
parent 52c0e49073
commit e9a3dc6ec7
3 changed files with 31 additions and 25 deletions

View file

@ -1,4 +1,5 @@
#include "../header/common.h" #include "../header/common.h"
#include "dyn_buf.h"
#ifndef _TAGET_CODE_GENERATOR_H_ #ifndef _TAGET_CODE_GENERATOR_H_
#define _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); __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 // 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 //Print out the machine code in target_code
void print_target_code(__uint32_t *target_code); void print_target_code(__uint32_t *target_code);

View file

@ -183,7 +183,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE; 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) if (error != 0)
{ {
printf("Error: Could not generate target code - Error %s\n", strerror(error)); printf("Error: Could not generate target code - Error %s\n", strerror(error));

View file

@ -34,7 +34,7 @@ __uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint
return instruction; 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 // stores which token we are currently looking at
unsigned int token_index = 0; 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 // indice of a new instruction
instruction_index++; 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) 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++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_INP, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_INP, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -74,8 +79,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_OUT, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_OUT, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -90,8 +95,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_LDA, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_LDA, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -106,8 +111,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_STA, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_STA, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; 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) else if (strcmp(tokens[token_index], "INC") == 0)
{ {
target_code[instruction_index] = get_target_instruction(INSTR_INC, 0, 0); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_INC, 0, 0);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; 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) else if (strcmp(tokens[token_index], "DEC") == 0)
{ {
target_code[instruction_index] = get_target_instruction(INSTR_DEC, 0, 0); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_DEC, 0, 0);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -141,8 +146,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPP, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPP, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -157,8 +162,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPZ, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPZ, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -173,8 +178,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPN, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPN, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;
@ -189,8 +194,8 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
token_index++; token_index++;
sscanf(tokens[token_index], "%d", &adress); sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPU, 1, adress); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_JPU, 1, adress);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; 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) else if (strcmp(tokens[token_index], "EOJ") == 0)
{ {
target_code[instruction_index] = get_target_instruction(INSTR_EOJ, 0, 0); ((__uint32_t *) target_code->buffer)[instruction_index] = get_target_instruction(INSTR_EOJ, 0, 0);
if (target_code[instruction_index] == 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); printf("Error: Received andress bigger than %d\nThis error shoudn't occur! Please report at %s\n", MAX_MEMORY, EIPA_BUG_ADRESS);
return EOVERFLOW; return EOVERFLOW;