Fix some compiler warnings

This commit is contained in:
XOR 2023-03-04 23:55:51 +01:00
parent ab328bd2f5
commit 1ffd59b615
6 changed files with 108 additions and 54 deletions

View file

@ -5,5 +5,7 @@ Check for Errors in the EIPA Assembly (using the tokens)
#COMMENTS!
function return errors + handling of those
Consider what happens when the input file contains too many instructions or one token is too long
Do some "skip over" loops in lexer need a check for EOF?
Consider using a hashtable for the token array to eliminate the if else ladder in get_target_code

View file

@ -7,10 +7,4 @@
#define LONG_DOC "Argp test"
#define DEFAULT_OUT_FILE "a.eipaimg"
// Gets declared in the main function dynamically
const char *argp_program_version =
VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG;
const char *argp_program_bug_address = EIPA_BUG_ADRESS;
#endif
#endif

View file

@ -6,17 +6,30 @@ This code contains the implementations of all functions related to saving EIPA t
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
int save_img(char *out_path, __uint32_t *target_code){
FILE *out_file = fopen(out_path, "wb");
if(out_file == NULL) return EIO;
#include "../header/image_saver.h"
// Count instructions
// Count how many instruction a target_code array contains
static unsigned int get_instruction_count(__uint32_t *target_code)
{
unsigned int count = 0;
while(target_code[count]!=0)
{
count++;
}
return count;
}
int save_img(char *out_path, __uint32_t *target_code){
unsigned int count = get_instruction_count(target_code);
FILE *out_file = fopen(out_path, "wb");
if(out_file == NULL) return EIO;
// Count instructions
if (fwrite(target_code, sizeof(target_code), count, out_file) != count)
{
@ -25,4 +38,4 @@ int save_img(char *out_path, __uint32_t *target_code){
}
return 0;
}
}

View file

@ -8,26 +8,34 @@ This code contains the implementations of all functions related to the lexical a
#include <string.h>
#include "../header/common.h"
#include "../header/lexer.h"
void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
{
// Stores the current character we are examining
char current_char = 0;
// Stores the return value of fgetc
int fgetc_return = 0;
// Stores at which token current_char is
unsigned int token_index = 0;
// Stores wether current_char is the beginning of a line, so that we can skip any indentation at that point
short unsigned int is_pos_line_start = 1;
// Loop trough all characters in the file
while ((current_char = fgetc(input_file)) != EOF)
while ((fgetc_return = fgetc(input_file)) != EOF)
{
// We do this as fgetc can also retrun EOF which isn't inside the char range
current_char = (char)fgetc_return;
if (is_pos_line_start)
{
// Loop trough Spaces and Tabs, to make empty lines and Indentation work
is_pos_line_start = 0;
while (current_char != EOF && (current_char == ASCII_SPACE || current_char == ASCII_TAB || current_char == ASCII_NEWLINE))
{
current_char = fgetc(input_file);
current_char = (char)fgetc(input_file);
}
}
@ -42,7 +50,7 @@ void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
// Loop to the characters until the next character fgetc() would read is not space or tab
while (PREFGETC(input_file) == ASCII_SPACE || PREFGETC(input_file) == ASCII_TAB)
{
current_char = fgetc(input_file);
current_char = (char)fgetc(input_file);
}
/*
Between the Adress and the newline in an Instruction is usually no space.
@ -72,7 +80,7 @@ void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
// Loop over the comment
while (PREFGETC(input_file) != ASCII_NEWLINE && PREFGETC(input_file) != '\0')
{
current_char = fgetc(input_file);
current_char = (char)fgetc(input_file);
}
break;
default:

View file

@ -16,6 +16,12 @@ It also is responsible for Argument Parsing
#include "../header/lexer.h"
#include "../header/target_code_generator.h"
const char *argp_program_version =
VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG;
const char *argp_program_bug_address = EIPA_BUG_ADRESS;
// Here, the argumets get stored by the parser, so acess them from here
struct cmd_arguments
{
@ -26,7 +32,7 @@ struct cmd_arguments
};
// Parses the command line options & arguments
error_t parser_function(int key, char *arg, struct argp_state *state)
static error_t parser_function(int key, char *arg, struct argp_state *state)
{
struct cmd_arguments *save_arguments = state->input;
@ -63,7 +69,7 @@ error_t parser_function(int key, char *arg, struct argp_state *state)
}
// This array stores structs containing information about the cli options
struct argp_option argp_options[] =
static struct argp_option argp_options[] =
{
{.name = "output",
.key = 'o',
@ -87,7 +93,7 @@ struct argp_option argp_options[] =
};
// Contains the neccesary stuff for argp
struct argp argument_parser =
static struct argp argument_parser =
{
.options = argp_options,
.parser = parser_function,
@ -96,9 +102,21 @@ struct argp argument_parser =
int main(int argc, char **argv)
{
// Will store our input assembly file
FILE *assembly_file;
// Stores the generated tokens
char(*tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*tokens));
// Stores the generated binary target code
__uint32_t *target_code = calloc(MAX_MEMORY, sizeof(target_code));
// stores the CLI options
struct cmd_arguments arguments;
// A variable for storing retrun values of functions that return errors on failure
int error;
// set defaults
arguments.output_file = DEFAULT_OUT_FILE;
arguments.input_file = "";
@ -111,14 +129,12 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
FILE *assembly_file;
assembly_file = fopen(arguments.input_file, "r");
if (assembly_file == NULL)
return EXIT_FAILURE;
// Stores the generated tokens
char(*tokens)[MAX_TOKEN_SIZE] = calloc(MAX_MEMORY, sizeof(*tokens));
if(tokens == NULL){
printf("Error: Could not allocate memory for the tokens array\n");
@ -128,16 +144,13 @@ int main(int argc, char **argv)
lexer(assembly_file, tokens);
fclose(assembly_file);
// Stores the generated binary target code
__uint32_t *target_code = calloc(MAX_MEMORY, sizeof(target_code));
if(target_code == NULL)
{
printf("Error: Could not allocate memory for the target code\n");
return EXIT_FAILURE;
}
int error = gen_target_code(tokens, target_code);
error = gen_target_code(tokens, target_code);
if (error != 0)
{
printf("Error: Could not generate target code - Error %s\n", strerror(error));

View file

@ -4,12 +4,13 @@ This code is part of the EIPA Platform
This code contains the implementations for all the functions related to the target code (binary) generation
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../header/common.h"
#include "../header/target_code_generator.h"
__uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint32_t adress)
{
@ -17,8 +18,7 @@ __uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint
__uint32_t instruction = 0;
// move the opcode bytes to the left of the instruction
instruction = instruction | opcode << (32-4);
instruction = instruction | (unsigned)opcode << (28);
if (!use_adress)
{
@ -38,11 +38,10 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
{
// stores which token we are currently looking at
unsigned int token_index = 0;
// stores which instruction we are currently looking at
unsigned int instruction_index = 0;
while (tokens[token_index][0] != EOF)
{
if (strcmp(tokens[token_index], ";") == 0)
@ -52,52 +51,64 @@ int gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
}
else if (strcmp(tokens[token_index], "INP") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_INP, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "OUT") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_OUT, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "LDA") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_LDA, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "STA") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_STA, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
@ -105,69 +116,82 @@ 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){
if (target_code[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;
}
}
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){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "JPP") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPP, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "JPZ") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPZ, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "JPN") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPN, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
}
else if (strcmp(tokens[token_index], "JPU") == 0)
{
// Stores the Adress accociated with this operation
__uint32_t adress;
// The address of the instructon is stored at the next token. Read that
token_index++;
__uint32_t adress;
sscanf(tokens[token_index], "%d", &adress);
target_code[instruction_index] = get_target_instruction(INSTR_JPU, 1, adress);
if(target_code[instruction_index] == 0){
if (target_code[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;
}
@ -175,11 +199,11 @@ 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){
if (target_code[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;
}
}
token_index++;