Skip to content

Commit

Permalink
Merge pull request #48 from Quickfall/qasm/ptr
Browse files Browse the repository at this point in the history
[feat] removal of standart variables in QAsm in favor of Pointers
  • Loading branch information
Zffu authored Jan 18, 2025
2 parents c7ce318 + dc3fa5d commit 58c9d8d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "../ir/structs.h"
#include "../ir/ir.h"
#include "../ir/writer.h"

#include "../qasm/writer/writer.h"

Expand Down Expand Up @@ -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);
Expand Down
90 changes: 65 additions & 25 deletions src/ir/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,68 +38,61 @@ 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.
* @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,

/**
Expand Down Expand Up @@ -189,4 +182,51 @@ 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[24][3] = {
{INT},
{INT, VARIABLE},
{INT, INT, VARIABLE},
{INT, 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
26 changes: 26 additions & 0 deletions src/ir/writer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* The writer of IR. Allows to compile Quickfall files into a lower level format than Assembly for speed.
*/

#include <stdio.h>

#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 <
}
}
20 changes: 20 additions & 0 deletions src/ir/writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* The writer of IR. Allows to compile Quickfall files into a lower level format than Assembly for speed.
*/

#include <stdio.h>

#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

0 comments on commit 58c9d8d

Please sign in to comment.