From 8f5c48d8e96c42dc188e54cc446317738417be72 Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 17 Jan 2025 22:19:48 +0100 Subject: [PATCH 1/3] feat: added writer header --- src/ir/writer.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/ir/writer.h diff --git a/src/ir/writer.h b/src/ir/writer.h new file mode 100644 index 0000000..c2cce0a --- /dev/null +++ b/src/ir/writer.h @@ -0,0 +1,20 @@ +/** + * The writer of IR. Allows to compile Quickfall files into a lower level format than Assembly for speed. + */ + +#include + +#include "./structs.h" +#include "./ir.h" + +#ifndef IR_WRITER_H +#define IR_WRITER_H + +/** + * Writes an IR block into the compiled Quickfall format. + * @param block the block. + * @param file the file stream. + */ +void writeIRBlock(IR_BASIC_BLOCK* block, FILE* file); + +#endif \ No newline at end of file From 6c9afab5891d3f5c2fdae4237ebed2c7a9c2886f Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 18 Jan 2025 01:53:20 +0100 Subject: [PATCH 2/3] feat: removed ptr_load instruction --- src/cli/main.c | 19 +++++++++++++++ src/ir/instructions.h | 55 +++++++++++++++++++++++++++++++++++++------ src/ir/writer.c | 26 ++++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/ir/writer.c diff --git a/src/cli/main.c b/src/cli/main.c index 3ad8067..150c31c 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -21,6 +21,7 @@ #include "../ir/structs.h" #include "../ir/ir.h" +#include "../ir/writer.h" #include "../qasm/writer/writer.h" @@ -130,6 +131,24 @@ int main(int argc, char* argv[]) { fptr = fopen(outputFile, "w"); + FILE* test = fopen("bob.txt", "w"); + + writeIRBlock(irOut->blocks[0], test); + + fclose(test); + + test = fopen("bob.txt", "r"); + fseek(test, 0, SEEK_END); + int sz = ftell(test); + rewind(test); + + unsigned char* b = malloc(sizeof(sz)); + fread(b, sz, 1, test); + + fclose(test); + + printf("%d\n", ((IR_BASIC_BLOCK*)b)->instructionCount); + BYTECODE_BUFFER* buffer = compile(irOut); compilePE(fptr, buffer); diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 6991056..abf2383 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -38,13 +38,6 @@ typedef enum IR_INSTRUCTION_CODE { */ S_ALLOC, - /** - * Loads the values of a specific address into a variable. - * @param var the output variable. - * @param ptr the pointer containing the target address. - */ - PTR_LOAD, - /** * Adds two 32 bit integers together. @@ -189,4 +182,52 @@ typedef enum IR_INSTRUCTION_CODE { } IR_INSTRUCTION_CODE; +/** + * The types for parameters in an IR instruction. + */ +typedef enum IR_PARAMETER_TYPE { + + VARIABLE, + INT, + STRING + +} IR_PARAMETER_TYPE; + +/** + * Holds all of the parameter types of the instructions. + */ +const unsigned char INSTRUCTION_PARAMETER_TYPES[25][3] = { + {INT}, + {INT, VARIABLE}, + {INT, INT, VARIABLE}, + {INT, VARIABLE}, + {VARIABLE, VARIABLE}, + + {VARIABLE, INT, INT}, + {VARIABLE, INT, INT}, + {VARIABLE, INT, INT}, + {VARIABLE, INT, INT}, + + {VARIABLE, INT, INT}, + {VARIABLE, INT, INT}, + {VARIABLE, INT, INT}, + + {VARIABLE, INT}, + {VARIABLE}, + {STRING}, + + {0}, + {0}, + {0}, + {0}, + + {VARIABLE, VARIABLE}, + {VARIABLE, VARIABLE}, + {VARIABLE, VARIABLE}, + {VARIABLE, VARIABLE}, + + {VARIABLE, INT}, + {VARIABLE, VARIABLE, VARIABLE} +}; + #endif \ No newline at end of file diff --git a/src/ir/writer.c b/src/ir/writer.c new file mode 100644 index 0000000..26a1768 --- /dev/null +++ b/src/ir/writer.c @@ -0,0 +1,26 @@ +/** + * The writer of IR. Allows to compile Quickfall files into a lower level format than Assembly for speed. + */ + +#include + +#include "./structs.h" +#include "./ir.h" + +/** + * Writes an IR block into the compiled Quickfall format. + * @param block the block. + * @param file the file stream. + */ +void writeIRBlock(IR_BASIC_BLOCK* block, FILE* file) { + fwrite(&block->instructionCount, sizeof(int), 1, file); + + for(int i = 0; i < block->instructionCount; ++i) { + IR_INSTRUCTION* instruction = block->instructions[i]; + + fwrite(instruction->opCode, 1, 1, file); + fwrite(instruction->paramCount, sizeof(int), 1, file); + + for(int i = 0; i < + } +} \ No newline at end of file From dc3fa5d916be99d87bcf6e24a32d22f272b54dce Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 18 Jan 2025 01:54:59 +0100 Subject: [PATCH 3/3] feat: changed variables to pointers in docs --- src/ir/instructions.h | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index abf2383..c6d4e40 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -42,57 +42,57 @@ typedef enum IR_INSTRUCTION_CODE { /** * Adds two 32 bit integers together. * @param output the output variable of the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ IADD, /** * Subtracts two 32 bit integers together. * @param output the output variable of the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ ISUB, /** * Multiplies two 32 bit integers together. * @param output the output variable of the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ IMUL, /** * Divides two 32 bit integers together. * @param output the output variable of the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ IDIV, /** * Compares two 32 bit integers to check if they are equal. - * @param out the output variable containing the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param output the output variable of the result. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ ICMP, /** * Compares two 32 bit integers to check if the first one is higher than the second one. - * @param out the output variable containing the result. - * @param i1 the first integer. - * @param i2 the second integer. + * @param output the output variable of the result. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. */ ICMP_H, /** * Compares two 32 bit integers to check if the first one is higher or equal to the second one. - * @param out the output variable containing the result. - * @param i1 the first integer. - * @param i2 the second integer. - */ + * @param output the output variable of the result. + * @param p1 the first integer pointer. + * @param p2 the second integer pointer. + */ ICMP_L, /** @@ -196,12 +196,11 @@ typedef enum IR_PARAMETER_TYPE { /** * Holds all of the parameter types of the instructions. */ -const unsigned char INSTRUCTION_PARAMETER_TYPES[25][3] = { +const unsigned char INSTRUCTION_PARAMETER_TYPES[24][3] = { {INT}, {INT, VARIABLE}, {INT, INT, VARIABLE}, {INT, VARIABLE}, - {VARIABLE, VARIABLE}, {VARIABLE, INT, INT}, {VARIABLE, INT, INT},