Merge branch 'FunctionCalls' into DontBreakDev
This commit is contained in:
@ -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:
|
||||
;
|
||||
}
|
||||
@ -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;
|
||||
@ -502,20 +511,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;
|
||||
}
|
||||
|
||||
@ -594,24 +603,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){
|
||||
int generateCall(Instruction *inst){
|
||||
|
||||
TNodeOrConst *op1 = getOperand1(inst);
|
||||
TNodeOrConst *op2 = getOperand2(inst);
|
||||
if (op1 == NULL) {
|
||||
printdebug("%sgenerateFunctionStart failed, NULL operand", COLOR_RED);
|
||||
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;
|
||||
//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
|
||||
|
||||
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;
|
||||
|
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 */
|
||||
|
@ -38,6 +38,9 @@ typedef union TNConstUnion TNConstUnion;
|
||||
typedef struct Instruction Instruction;
|
||||
typedef struct TNodeOrConst TNodeOrConst;
|
||||
|
||||
|
||||
// these are from page 364
|
||||
|
||||
typedef enum { // these are from page 364
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_FUNC_START,
|
||||
@ -166,3 +169,6 @@ bool isConst(TNodeOrConst * tnc);
|
||||
int label_gen();
|
||||
void backpatch(Stack *s, int l);
|
||||
void emit_backpatch(Stack *s, int l);
|
||||
extern int offset;
|
||||
extern int currentsp;
|
||||
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