split everything into multiple files

This commit is contained in:
XOR 2023-01-12 00:12:12 +01:00
parent 84b7ac25f8
commit 861225a3b8
9 changed files with 179 additions and 128 deletions

View file

@ -1,5 +1,5 @@
eipaasm:
clang main.c -Wall -Wextra -o eipaasm
clang src/* -Wall -Wextra -o eipaasm
all: eipaasm
@ -9,6 +9,6 @@ run:
buildrun: compile
./eipaasm
eipaasm_debug:
gcc main.c -g -o eipaasm_debug
gcc src/* -g -o eipaasm_debug
clean:
rm -f eipaasm eipaasm_debug

22
header/common.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#define MAX_MEMORY 1000000
#define MAX_TOKEN_SIZE 10
#define ALPHA 0
#define BETA 1
#define STABLE 2
#define VER_MAJOR 0
#define VER_MINOR 1
#define VER_PATCH 0
#define TAG ALPHA
#define ASCII_TAB 9
#define ASCII_SPACE 32
#define ASCII_NEWLINE 10
#define PREFGETC(file) *file->_IO_read_ptr
#endif

7
header/image_saver.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef _IMAGE_SAVER_H_
#define _IMAGE_SAVER_H_
// Save machine code from target_cde to file at out_path
int save_img(char *out_path, __uint32_t *target_code);
#endif

13
header/lexer.h Normal file
View file

@ -0,0 +1,13 @@
#include "../header/common.h"
#ifndef _LEXER_H_
#define _LEXER_H_
//Convert the input_file into tokens
void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]);
//Print out all the tokens in tokens
void print_tokens(char tokens[][MAX_TOKEN_SIZE]);
#endif

View file

@ -0,0 +1,15 @@
#include "../header/common.h"
#ifndef _TAGET_CODE_GENERATOR_H_
#define _TAGET_CODE_GENERATOR_H_
// Get a machine code instruction from opcode and and optional 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
void gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code);
//Print out the machine code in target_code
void print_target_code(__uint32_t *target_code);
#endif

17
src/image_saver.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdio.h>
int save_img(char *out_path, __uint32_t *target_code){
//get count of instructions
unsigned int count = 0;
while(target_code[count]!=0)
{
count++;
}
FILE *out_file = fopen(out_path, "wb");
if(out_file == NULL) return -1;
fwrite(target_code, sizeof(__uint32_t), count, out_file);
return 0;
}

71
src/lexer.c Normal file
View file

@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
#include "../header/common.h"
void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
{
char current_char = 0;
unsigned int token_index = 0;
short unsigned int is_pos_line_start = 1;
while ((current_char = fgetc(input_file)) != EOF)
{
if (is_pos_line_start)
{
is_pos_line_start = 0;
// Loop trough Spaces and Tabs, to make empty lines and Indentation work
while (current_char != EOF && (current_char == ASCII_SPACE || current_char == ASCII_TAB || current_char == ASCII_NEWLINE))
{
current_char = fgetc(input_file);
}
}
switch (current_char)
{
case EOF:
break;
case ASCII_TAB:
case ASCII_SPACE:
// 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) // The character which fgetc will read the next time when its called
{
current_char = fgetc(input_file);
}
// Between the Adress and the newline in an Instruction is usually no space.
// Therefore the token_index gets increased, whenever a newline is found.
// However, there can also be a space between the Adress and the newline.
// To not increase the token_index 2 times, we need to not increase it here if the next character is a \n
if (PREFGETC(input_file) != ';' && PREFGETC(input_file) != ASCII_NEWLINE)
{
token_index++;
}
break;
case ASCII_NEWLINE:
token_index++;
tokens[token_index][0] = ';';
token_index++;
is_pos_line_start = 1;
break;
case ';':
while (PREFGETC(input_file) != ASCII_NEWLINE) // The character which fgetc will read the next time when its called
{
current_char = fgetc(input_file);
}
break;
default:
strncat(tokens[token_index], &current_char, 1);
break;
}
}
tokens[token_index + 1][0] = ';';
tokens[token_index + 2][0] = EOF;
}
void print_tokens(char tokens[][MAX_TOKEN_SIZE])
{
int token_index = 0;
while (tokens[token_index][0] != EOF)
{
printf("%s\n", tokens[token_index]);
token_index++;
}
}

31
src/main.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../header/common.h"
#include "../header/lexer.h"
#include "../header/target_code_generator.h"
#include "../header/image_saver.h"
int main(int argc, char const *argv[])
{
printf("-----------------\nEIPA Assembler\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
char assembly_path[] = "test/test.eipa";
char image_path[] = "a.eipaimg";
FILE *assembly_file = fopen(assembly_path, "r");
if (assembly_file == NULL) return 0;
char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY);
lexer(assembly_file, tokens);
fclose(assembly_file);
print_tokens(tokens);
__uint32_t *target_code = malloc(MAX_MEMORY * sizeof(target_code));
gen_target_code(tokens, target_code);
print_target_code(target_code);
save_img(image_path, target_code);
return 0;
}

View file

@ -1,84 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MEMORY 1000000
#define MAX_TOKEN_SIZE 10
#define INSTRUCTION_OPERATION 0
#define INSTRUCTION_ADRESS 1
// #define INSTRUCTION_COMMENT 2
#define ALPHA 0
#define BETA 1
#define STABLE 2
#define VER_MAJOR 0
#define VER_MINOR 1
#define VER_PATCH 0
#define TAG ALPHA
#define ASCII_TAB 9
#define ASCII_SPACE 32
#define ASCII_NEWLINE 10
#define PREFGETC(file) *file->_IO_read_ptr
void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
{
char current_char = 0;
unsigned int token_index = 0;
short unsigned int is_pos_line_start = 1;
while ((current_char = fgetc(input_file)) != EOF)
{
if (is_pos_line_start)
{
is_pos_line_start = 0;
// Loop trough Spaces and Tabs, to make empty lines and Indentation work
while (current_char != EOF && (current_char == ASCII_SPACE || current_char == ASCII_TAB || current_char == ASCII_NEWLINE))
{
current_char = fgetc(input_file);
}
}
switch (current_char)
{
case EOF:
break;
case ASCII_TAB:
case ASCII_SPACE:
// 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) // The character which fgetc will read the next time when its called
{
current_char = fgetc(input_file);
}
// Between the Adress and the newline in an Instruction is usually no space.
// Therefore the token_index gets increased, whenever a newline is found.
// However, there can also be a space between the Adress and the newline.
// To not increase the token_index 2 times, we need to not increase it here if the next character is a \n
if (PREFGETC(input_file) != ';' && PREFGETC(input_file) != ASCII_NEWLINE)
{
token_index++;
}
break;
case ASCII_NEWLINE:
token_index++;
tokens[token_index][0] = ';';
token_index++;
is_pos_line_start = 1;
break;
case ';':
while (PREFGETC(input_file) != ASCII_NEWLINE) // The character which fgetc will read the next time when its called
{
current_char = fgetc(input_file);
}
break;
default:
strncat(tokens[token_index], &current_char, 1);
break;
}
}
tokens[token_index + 1][0] = ';';
tokens[token_index + 2][0] = EOF;
}
#include "../header/common.h"
__uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint32_t adress)
{
@ -96,16 +19,6 @@ __uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint
return instruction;
}
void print_tokens(char tokens[][MAX_TOKEN_SIZE])
{
int token_index = 0;
while (tokens[token_index][0] != EOF)
{
printf("%s\n", tokens[token_index]);
token_index++;
}
}
void gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
{
unsigned int token_index = 0;
@ -251,41 +164,3 @@ void print_target_code(__uint32_t *target_code)
instruction_index++;
}
}
int save_img(char *out_path, __uint32_t *target_code){
//get count of instructions
unsigned int count = 0;
while(target_code[count]!=0)
{
count++;
}
FILE *out_file = fopen(out_path, "w");
if(out_file == NULL) return -1;
fwrite(target_code, sizeof(__uint32_t), count, out_file);
}
int main(int argc, char const *argv[])
{
printf("-----------------\nEIPA Assembler\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
char assembly_path[] = "test/test.eipa";
char image_path[] = "a.eipaimg";
FILE *assembly_file = fopen(assembly_path, "r");
if (assembly_file == NULL) return 0;
char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY);
lexer(assembly_file, tokens);
fclose(assembly_file);
print_tokens(tokens);
__uint32_t *target_code = malloc(MAX_MEMORY * sizeof(target_code));
gen_target_code(tokens, target_code);
print_target_code(target_code);
save_img(image_path, target_code);
return 0;
}