idk what i did anymore

This commit is contained in:
Annie
2025-05-02 16:12:35 -04:00
parent 18a29d7913
commit e598fad62a
9 changed files with 146 additions and 45 deletions

BIN
a.out Normal file

Binary file not shown.

0
integer Normal file
View File

View File

@ -5,6 +5,7 @@
int generate(){ int generate(){
offset = 0; offset = 0;
currentsp = 0;
Instruction *i = begin; Instruction *i = begin;
while (i != NULL) { while (i != NULL) {
switch(getOp(i)) { switch(getOp(i)) {
@ -74,6 +75,9 @@ int generate(){
case E_ADDRESS_OF: case E_ADDRESS_OF:
generateAddressOf(i); generateAddressOf(i);
break; break;
case E_FUNC_START:
generateFunctionStart(i);
break;
default: default:
; ;
} }
@ -118,7 +122,7 @@ CGNode *findCG(TableNode *tn) {
CGNode *addCG(TableNode *tn, int sp) { CGNode *addCG(TableNode *tn, int sp) {
CGNode *cg = calloc(1, sizeof(CGNode)); CGNode *cg = calloc(1, sizeof(CGNode));
cg->tn = tn; cg->tn = tn;
offset += 4; // <- quick fix getPrimSize(getTypeEntry(tn)) offset += getPrimSize(getTypeEntry(tn));
cg->address = offset; cg->address = offset;
cg->next = cgList; cg->next = cgList;
cgList = cg; cgList = cg;
@ -127,7 +131,12 @@ CGNode *addCG(TableNode *tn, int sp) {
int generateLabel(Instruction *inst) { int generateLabel(Instruction *inst) {
if (inst == NULL) {
return -1;
}
fprintf(cg_flag, ".L%d:\n", getLabel(inst)); fprintf(cg_flag, ".L%d:\n", getLabel(inst));
return 0; return 0;
} }
int generateAdd(Instruction *inst) { int generateAdd(Instruction *inst) {
@ -151,7 +160,7 @@ int generateAdd(Instruction *inst) {
CGNode *op1CG = findCG(getTN(op1)); CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op1)); CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) { if (op1CG == NULL) {
printdebug("generateAdd failed, %s is not initialized/in CG", getName(getTN(op1))); printdebug("generateAdd failed, %s is not initialized/in CG", getName(getTN(op1)));
return -1; return -1;
@ -503,20 +512,20 @@ int generateAssign(Instruction *inst) {
return 0; return 0;
} }
int generateGoto(Instruction *instruction){ int generateGoto(Instruction *inst){
return -1; return -1;
} }
int generateCondGoto(Instruction *instruction) { int generateCondGoto(Instruction *inst) {
return -1; return -1;
} }
int generateIfTrue(Instruction *instruction){ int generateIfTrue(Instruction *inst){
return -1; return -1;
// might just be a goto for where to go if something is true, or returning if something is true, or checking if true and writing goto if thats the case // might just be a goto for where to go if something is true, or returning if something is true, or checking if true and writing goto if thats the case
} }
int generateIfFalse(Instruction *instruction){ int generateIfFalse(Instruction *inst){
return -1; return -1;
} }
@ -595,24 +604,87 @@ int generateEqualTo(Instruction *inst){
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg)); fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg));
return 0; return 0;
} }
int generateCall(Instruction *instruction){ int generateCall(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
return -1; return -1;
//will want to store parameters and then update the offset by adding 8? for stack pointer stuff, can then print call subroutine name, followed by movl of the result into the result's cg
} }
int generateReturn(Instruction *instruction){
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
return -1;
}
if (getTN(op1) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode");
return -1;
}
if (getTN(op2) == NULL) {
printdebug("generateFunctionCall failed, NULL tablenode");
return -1;
}
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
fprintf(cg_flag, "\taddq\t$%d, %%rsp\n", 8 + getConst(op1));
return 0;
}
int generateReturn(Instruction *inst){
return -1; return -1;
//will movl the result into the appropriate register and move the stack pointer/offset stuff back to correct value //will movl the result into the appropriate register and move the stack pointer/offset stuff back to correct value
} }
int generateCopyRight(Instruction *instruction){ int generateCopyRight(Instruction *inst){
return -1; return -1;
} }
int generateCopyLeft(Instruction *instruction){ int generateCopyLeft(Instruction *inst){
return -1; return -1;
} }
int generateAddressOf(Instruction *instruction){ int generateAddressOf(Instruction *inst){
return -1; return -1;
} }
int generateParam(Instruction *instruction){ int generateParam(Instruction *inst){
//need to check if op1 is null, then add it to the appropriate register/cg node. need a way to keep track of this, maybe just have global count of params generated TNodeOrConst *op1 = getOperand1(inst);
if (op1 == NULL) {
printdebug("generateParam failed, NULL operand");
return -1; return -1;
} }
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateParam failed, op1 is not in CGlist");
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#adding param start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#adding param end\n", offset += 8);
return 0;
}
int generateFunctionStart(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
if (op1 == NULL) {
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
return -1;
}
if (getTN(op1) == NULL) {
printdebug("generateFunctionStart failed, NULL tablenode");
return -1;
}
fprintf(cg_flag, "%s:\n", getName(getTN(op1)));
fprintf(cg_flag, "\tpushq\t%%rbp\n");
fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n");
return 0;
}

View File

@ -50,6 +50,7 @@ int generateCopyRight(Instruction *instruction);
int generateCopyLeft(Instruction *instruction); int generateCopyLeft(Instruction *instruction);
int generateAddressOf(Instruction *instruction); int generateAddressOf(Instruction *instruction);
int generateParam(Instruction *instruction); int generateParam(Instruction *instruction);
int generateFunctionStart(Instruction *instruction);
extern int label_count; extern int label_count;
extern Instruction *begin; extern Instruction *begin;

2
src/codegen.h~ Normal file
View File

@ -0,0 +1,2 @@
/* Code Generation File - Contains functions to generate assembly code */

View File

@ -13,13 +13,14 @@
#include "symbol_table.h" #include "symbol_table.h"
// these are from page 364 // these are from page 364
typedef enum { typedef enum { // these are from page 364
E_LABEL = 10000, // this is not in the book E_LABEL = 10000, // this is not in the book
E_FUNC_START,
E_ADD, // 1 from the list E_ADD, // 1 from the list
E_SUB, // 1 E_SUB, // 1
E_MUL, // 1 E_MUL, // 1
E_DIV, // 1 E_DIV, // 1
E_MOD, // 1 E_MOD, // 1 TODO: Please change to REM
E_OR, // 1 E_OR, // 1
E_AND, // 1 E_AND, // 1
E_NEG, // 2 E_NEG, // 2

View File

@ -0,0 +1,8 @@
type main: string -> integer
function entry: main
entry(arg) := {
[integer:x]
x := 3 + 2 * 8;
return x;
}

View File

@ -0,0 +1,8 @@
type main: string -> integer
function entry: main
entry(arg) := {
[integer:x]
x := 3 + 2 * 8;
return 0;
}

View File

@ -0,0 +1,9 @@
type main: string -> integer
function entry: main
entry(arg) := {
[integer:x; Boolean b]
x := 3 + 2 * 8;
b := x < 1;
return 0;
}