idk what i did anymore
This commit is contained in:
106
src/codegen.c
106
src/codegen.c
@ -5,6 +5,7 @@
|
||||
|
||||
int generate(){
|
||||
offset = 0;
|
||||
currentsp = 0;
|
||||
Instruction *i = begin;
|
||||
while (i != NULL) {
|
||||
switch(getOp(i)) {
|
||||
@ -74,6 +75,9 @@ int generate(){
|
||||
case E_ADDRESS_OF:
|
||||
generateAddressOf(i);
|
||||
break;
|
||||
case E_FUNC_START:
|
||||
generateFunctionStart(i);
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
@ -118,7 +122,7 @@ CGNode *findCG(TableNode *tn) {
|
||||
CGNode *addCG(TableNode *tn, int sp) {
|
||||
CGNode *cg = calloc(1, sizeof(CGNode));
|
||||
cg->tn = tn;
|
||||
offset += 4; // <- quick fix getPrimSize(getTypeEntry(tn))
|
||||
offset += getPrimSize(getTypeEntry(tn));
|
||||
cg->address = offset;
|
||||
cg->next = cgList;
|
||||
cgList = cg;
|
||||
@ -127,7 +131,12 @@ CGNode *addCG(TableNode *tn, int sp) {
|
||||
|
||||
|
||||
int generateLabel(Instruction *inst) {
|
||||
if (inst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(cg_flag, ".L%d:\n", getLabel(inst));
|
||||
|
||||
return 0;
|
||||
}
|
||||
int generateAdd(Instruction *inst) {
|
||||
@ -151,7 +160,7 @@ int generateAdd(Instruction *inst) {
|
||||
|
||||
|
||||
CGNode *op1CG = findCG(getTN(op1));
|
||||
CGNode *op2CG = findCG(getTN(op1));
|
||||
CGNode *op2CG = findCG(getTN(op2));
|
||||
if (op1CG == NULL) {
|
||||
printdebug("generateAdd failed, %s is not initialized/in CG", getName(getTN(op1)));
|
||||
return -1;
|
||||
@ -503,20 +512,20 @@ int generateAssign(Instruction *inst) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int generateGoto(Instruction *instruction){
|
||||
int generateGoto(Instruction *inst){
|
||||
return -1;
|
||||
}
|
||||
|
||||
int generateCondGoto(Instruction *instruction) {
|
||||
int generateCondGoto(Instruction *inst) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int generateIfTrue(Instruction *instruction){
|
||||
int generateIfTrue(Instruction *inst){
|
||||
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
|
||||
}
|
||||
|
||||
int generateIfFalse(Instruction *instruction){
|
||||
int generateIfFalse(Instruction *inst){
|
||||
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));
|
||||
return 0;
|
||||
}
|
||||
int generateCall(Instruction *instruction){
|
||||
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 generateCall(Instruction *inst){
|
||||
|
||||
TNodeOrConst *op1 = getOperand1(inst);
|
||||
TNodeOrConst *op2 = getOperand2(inst);
|
||||
if (op1 == NULL) {
|
||||
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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 *instruction){
|
||||
int generateReturn(Instruction *inst){
|
||||
return -1;
|
||||
//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;
|
||||
}
|
||||
int generateCopyLeft(Instruction *instruction){
|
||||
int generateCopyLeft(Instruction *inst){
|
||||
return -1;
|
||||
}
|
||||
int generateAddressOf(Instruction *instruction){
|
||||
int generateAddressOf(Instruction *inst){
|
||||
return -1;
|
||||
}
|
||||
int generateParam(Instruction *instruction){
|
||||
//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
|
||||
return -1;
|
||||
}
|
||||
int generateParam(Instruction *inst){
|
||||
TNodeOrConst *op1 = getOperand1(inst);
|
||||
|
||||
if (op1 == NULL) {
|
||||
printdebug("generateParam failed, NULL operand");
|
||||
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;
|
||||
|
||||
}
|
||||
|
@ -50,6 +50,7 @@ int generateCopyRight(Instruction *instruction);
|
||||
int generateCopyLeft(Instruction *instruction);
|
||||
int generateAddressOf(Instruction *instruction);
|
||||
int generateParam(Instruction *instruction);
|
||||
int generateFunctionStart(Instruction *instruction);
|
||||
|
||||
extern int label_count;
|
||||
extern Instruction *begin;
|
||||
@ -57,4 +58,4 @@ extern Instruction *current;
|
||||
|
||||
extern int offset;
|
||||
extern int currentsp;
|
||||
extern CGNode *cgList;
|
||||
extern CGNode *cgList;
|
||||
|
2
src/codegen.h~
Normal file
2
src/codegen.h~
Normal file
@ -0,0 +1,2 @@
|
||||
/* Code Generation File - Contains functions to generate assembly code */
|
||||
|
@ -13,32 +13,33 @@
|
||||
#include "symbol_table.h"
|
||||
|
||||
// these are from page 364
|
||||
typedef enum {
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_ADD, // 1 from the list
|
||||
E_SUB, // 1
|
||||
E_MUL, // 1
|
||||
E_DIV, // 1
|
||||
E_MOD, // 1
|
||||
E_OR, // 1
|
||||
E_AND, // 1
|
||||
E_NEG, // 2
|
||||
E_NOT, // 2
|
||||
E_ASSIGN, // 3
|
||||
E_GOTO, // 4
|
||||
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
|
||||
E_IF_X_TRUE, // 5
|
||||
E_IF_X_FALSE, // 5
|
||||
E_LESS_THAN, // 6 rule 1 + 5
|
||||
E_EQUAL_TO, // 6 rule 1 + 5
|
||||
E_CALL, // 7
|
||||
E_PARAM, // 7
|
||||
E_RETURN, // 7
|
||||
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
|
||||
E_INDEX_COPY_LEFT, // 8 x[i] = y
|
||||
E_ADDRESS_OF, // 9 x = &y
|
||||
E_DEREF_RIGHT, // 9 x = *y
|
||||
E_DEREF_LEFT // 9 x* = y
|
||||
typedef enum { // these are from page 364
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_FUNC_START,
|
||||
E_ADD, // 1 from the list
|
||||
E_SUB, // 1
|
||||
E_MUL, // 1
|
||||
E_DIV, // 1
|
||||
E_MOD, // 1 TODO: Please change to REM
|
||||
E_OR, // 1
|
||||
E_AND, // 1
|
||||
E_NEG, // 2
|
||||
E_NOT, // 2
|
||||
E_ASSIGN, // 3
|
||||
E_GOTO, // 4
|
||||
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
|
||||
E_IF_X_TRUE, // 5
|
||||
E_IF_X_FALSE, // 5
|
||||
E_LESS_THAN, // 6 rule 1 + 5
|
||||
E_EQUAL_TO, // 6 rule 1 + 5
|
||||
E_CALL, // 7
|
||||
E_PARAM, // 7
|
||||
E_RETURN, // 7
|
||||
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
|
||||
E_INDEX_COPY_LEFT, // 8 x[i] = y
|
||||
E_ADDRESS_OF, // 9 x = &y
|
||||
E_DEREF_RIGHT, // 9 x = *y
|
||||
E_DEREF_LEFT // 9 x* = y
|
||||
} Op;
|
||||
|
||||
typedef enum {
|
||||
@ -121,4 +122,4 @@ extern Instruction* current;
|
||||
|
||||
extern int offset;
|
||||
extern int currentsp;
|
||||
extern CGNode* cgList;
|
||||
extern CGNode* cgList;
|
||||
|
8
tests/sprint4/test/sp4_cg_demo.alpha
Normal file
8
tests/sprint4/test/sp4_cg_demo.alpha
Normal file
@ -0,0 +1,8 @@
|
||||
type main: string -> integer
|
||||
function entry: main
|
||||
|
||||
entry(arg) := {
|
||||
[integer:x]
|
||||
x := 3 + 2 * 8;
|
||||
return x;
|
||||
}
|
8
tests/sprint4/test/sp4_cg_demo.alpha~
Normal file
8
tests/sprint4/test/sp4_cg_demo.alpha~
Normal file
@ -0,0 +1,8 @@
|
||||
type main: string -> integer
|
||||
function entry: main
|
||||
|
||||
entry(arg) := {
|
||||
[integer:x]
|
||||
x := 3 + 2 * 8;
|
||||
return 0;
|
||||
}
|
9
tests/sprint4/test/sp4_cg_demo.cg
Normal file
9
tests/sprint4/test/sp4_cg_demo.cg
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user