hopefully fixed merge
This commit is contained in:
@@ -3,11 +3,12 @@
|
|||||||
|
|
||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
int paramCount = 0;
|
int paramCount = 0;
|
||||||
|
int funDec = 0;
|
||||||
int generate() {
|
int generate() {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
currentsp = 0;
|
currentsp = 0;
|
||||||
Instruction *i = begin;
|
Instruction *i = begin;
|
||||||
|
funList = NULL;
|
||||||
// temporary
|
// temporary
|
||||||
fprintf(cg_flag, ".globl entry\n");
|
fprintf(cg_flag, ".globl entry\n");
|
||||||
|
|
||||||
@@ -82,6 +83,8 @@ int generate() {
|
|||||||
case E_FUNC_START:
|
case E_FUNC_START:
|
||||||
generateFunctionStart(i);
|
generateFunctionStart(i);
|
||||||
break;
|
break;
|
||||||
|
case E_FUNC_DEC:
|
||||||
|
generateFunctionDec(i);
|
||||||
default:;
|
default:;
|
||||||
}
|
}
|
||||||
i = i->next;
|
i = i->next;
|
||||||
@@ -89,6 +92,10 @@ int generate() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void align(TableNode *tn) {
|
||||||
|
int size = getPrimSize(getTypeEntry(tn));
|
||||||
|
offset += offset % size;
|
||||||
|
}
|
||||||
CGNode *getNextCG(CGNode *cg) {
|
CGNode *getNextCG(CGNode *cg) {
|
||||||
if (cg == NULL) {
|
if (cg == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -125,6 +132,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;
|
||||||
|
align(tn);
|
||||||
offset += getPrimSize(getTypeEntry(tn));
|
offset += getPrimSize(getTypeEntry(tn));
|
||||||
cg->address = offset;
|
cg->address = offset;
|
||||||
cg->next = cgList;
|
cg->next = cgList;
|
||||||
@@ -132,6 +140,13 @@ CGNode *addCG(TableNode *tn, int sp) {
|
|||||||
return cg;
|
return cg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int generateFunctionDec(Instruction *i) {
|
||||||
|
FunDecList * fd = calloc(1, sizeof(FunDecList));
|
||||||
|
fd->tn = getResult(i);
|
||||||
|
fd->next = funList;
|
||||||
|
funList = fd;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
int generateLabel(Instruction *inst) {
|
int generateLabel(Instruction *inst) {
|
||||||
if (inst == NULL) {
|
if (inst == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -511,7 +526,8 @@ int generateAssign(Instruction *inst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int generateGoto(Instruction *inst) {
|
int generateGoto(Instruction *inst) {
|
||||||
return -1;
|
fprintf(cg_flag, "\tjmp\t.L%d\n", getLabel(inst));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateCondGoto(Instruction *inst) {
|
int generateCondGoto(Instruction *inst) {
|
||||||
@@ -520,7 +536,6 @@ int generateCondGoto(Instruction *inst) {
|
|||||||
|
|
||||||
int generateIfTrue(Instruction *inst) {
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateIfFalse(Instruction *inst) {
|
int generateIfFalse(Instruction *inst) {
|
||||||
@@ -621,13 +636,18 @@ int generateCall(Instruction *inst) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if (findCG(getTN(op1)) == NULL) {
|
||||||
|
// printdebug("generateFunctionCall failed, function not in stack");
|
||||||
|
// return -1;
|
||||||
|
//}
|
||||||
// if (getTN(op2) == NULL) {
|
// if (getTN(op2) == NULL) {
|
||||||
// printdebug("generateFunctionCall failed, NULL tablenode2");
|
// printdebug("generateFunctionCall failed, NULL tablenode2");
|
||||||
// return -1;
|
// return -1;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
|
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
|
||||||
|
//fprintf(cg_flag, "\tmovq\t%d(%%rbp), %%rax\t#call %s!\n", getAddress(findCG(getTN(op1))), getName(getTN(op1)));
|
||||||
|
//fprintf(cg_flag, "\tcall *%%rax\n");
|
||||||
//now for the return
|
//now for the return
|
||||||
CGNode *cg = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
@@ -703,6 +723,7 @@ int generateParam(Instruction *inst) {
|
|||||||
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG6);
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %s\t#adding param start\n", getAddress(op1CG), REG6);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
align(getTN(op1));
|
||||||
offset += getPrimSize(getTypeEntry(getTN(op1)));
|
offset += getPrimSize(getTypeEntry(getTN(op1)));
|
||||||
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#adding param start\n", getAddress(op1CG));
|
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 - getPrimSize(getTypeEntry(getTN(op1))));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#adding param end\n", offset - getPrimSize(getTypeEntry(getTN(op1))));
|
||||||
@@ -737,7 +758,8 @@ int generateFunctionStart(Instruction *inst) {
|
|||||||
TableNode *tnToAdd = getFirstEntry(st);
|
TableNode *tnToAdd = getFirstEntry(st);
|
||||||
if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) {
|
if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) {
|
||||||
CGNode *paramCG = addCG(tnToAdd, offset);
|
CGNode *paramCG = addCG(tnToAdd, offset);
|
||||||
paramOffset += getPrimSize(tnToAdd);
|
int size = getPrimSize(getTypeEntry(tnToAdd));
|
||||||
|
paramOffset += getPrimSize(getTypeEntry(tnToAdd));
|
||||||
fprintf(cg_flag, "\tmovl\t%%edi, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
|
fprintf(cg_flag, "\tmovl\t%%edi, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
|
||||||
} else {
|
} else {
|
||||||
int numParams = getRecLength(paramTN);
|
int numParams = getRecLength(paramTN);
|
||||||
@@ -763,7 +785,8 @@ int generateFunctionStart(Instruction *inst) {
|
|||||||
fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG6, getAddress(paramCG));
|
fprintf(cg_flag, "\tmovl\t%s, %d(%%rbp)\t#FunctionStart1param end\n", REG6, getAddress(paramCG));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
paramOffset += getPrimSize(getTypeEntry(tnToAdd));
|
int size = getPrimSize(getTypeEntry(tnToAdd));
|
||||||
|
paramOffset += size;
|
||||||
offset += getPrimSize(getTypeEntry(tnToAdd));
|
offset += getPrimSize(getTypeEntry(tnToAdd));
|
||||||
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
|
||||||
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
|
||||||
@@ -774,5 +797,18 @@ int generateFunctionStart(Instruction *inst) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp(getName(funDecTN), "entry") == 0) {
|
||||||
|
if (funList == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "here\n");
|
||||||
|
TableNode *funDec;
|
||||||
|
while (funList != NULL) {
|
||||||
|
funDec = funList->tn;
|
||||||
|
CGNode *cg = addCG(getResult(inst), offset);
|
||||||
|
fprintf(cg_flag, "\tmovq\t$%s, %d(%%rbp)\t#storing function declaration\n", getName(funDec), getAddress(cg));
|
||||||
|
funList = funList->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,15 +25,21 @@ extern FILE *cg_flag;
|
|||||||
typedef struct CGNode {
|
typedef struct CGNode {
|
||||||
TableNode *tn;
|
TableNode *tn;
|
||||||
int address;
|
int address;
|
||||||
CGNode *next;
|
struct CGNode *next;
|
||||||
} CGNode;
|
} CGNode;
|
||||||
|
|
||||||
|
typedef struct FunDecList {
|
||||||
|
TableNode *tn;
|
||||||
|
struct FunDecList *next;
|
||||||
|
} FunDecList;
|
||||||
|
|
||||||
int generate();
|
int generate();
|
||||||
CGNode *getNextCG(CGNode *cg);
|
CGNode *getNextCG(CGNode *cg);
|
||||||
int getAddress(CGNode *cg);
|
int getAddress(CGNode *cg);
|
||||||
TableNode *getTNofCG(CGNode *cg);
|
TableNode *getTNofCG(CGNode *cg);
|
||||||
CGNode *findCG(TableNode *tn);
|
CGNode *findCG(TableNode *tn);
|
||||||
CGNode *addCG(TableNode *tn, int sp);
|
CGNode *addCG(TableNode *tn, int sp);
|
||||||
|
int generateFunctionDec(Instruction *inst);
|
||||||
int generateLabel(Instruction *inst);
|
int generateLabel(Instruction *inst);
|
||||||
int generateAdd(Instruction *inst);
|
int generateAdd(Instruction *inst);
|
||||||
int generateSub(Instruction *instruction);
|
int generateSub(Instruction *instruction);
|
||||||
@@ -66,3 +72,4 @@ extern Instruction *current;
|
|||||||
extern int offset;
|
extern int offset;
|
||||||
extern int currentsp;
|
extern int currentsp;
|
||||||
extern CGNode *cgList;
|
extern CGNode *cgList;
|
||||||
|
extern FunDecList *funList;
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ Instruction *current;
|
|||||||
int offset;
|
int offset;
|
||||||
int currentsp;
|
int currentsp;
|
||||||
CGNode *cgList;
|
CGNode *cgList;
|
||||||
|
FunDecList *funList;
|
||||||
Stack *stack;
|
Stack *stack;
|
||||||
Stack *TrueList;
|
Stack *TrueList;
|
||||||
Stack *FalseList;
|
Stack *FalseList;
|
||||||
|
|||||||
@@ -615,6 +615,10 @@ TableNode *getTN(TNodeOrConst *tnc) {
|
|||||||
int getConst(TNodeOrConst *tnc) {
|
int getConst(TNodeOrConst *tnc) {
|
||||||
if (tnc->d == INTEGER) {
|
if (tnc->d == INTEGER) {
|
||||||
return tnc->tnc_union->integer;
|
return tnc->tnc_union->integer;
|
||||||
|
} else if (tnc->d == CHARACTER) {
|
||||||
|
return tnc->tnc_union->character;
|
||||||
|
} else if (tnc->d == BOOLEAN) {
|
||||||
|
return tnc->tnc_union->Boolean;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ function d: iic2b
|
|||||||
|
|
||||||
d(x,y,z) := {
|
d(x,y,z) := {
|
||||||
[string: s]
|
[string: s]
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function entry: string2int
|
function entry: string2int
|
||||||
@@ -38,6 +38,7 @@ function entry: string2int
|
|||||||
a(x) := {
|
a(x) := {
|
||||||
[string : s]
|
[string : s]
|
||||||
s := x;
|
s := x;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ b(x) := {
|
|||||||
c(x) := {
|
c(x) := {
|
||||||
[string: s]
|
[string: s]
|
||||||
s := "Hi!";
|
s := "Hi!";
|
||||||
return x(s);
|
return a(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -61,6 +62,7 @@ entry(arg) := {
|
|||||||
[integer: result; string2int: f; integer: temp]
|
[integer: result; string2int: f; integer: temp]
|
||||||
temp := a("Hello");
|
temp := a("Hello");
|
||||||
f := b(temp);
|
f := b(temp);
|
||||||
|
result := f("ahhh");
|
||||||
result := c(f);
|
result := c(f);
|
||||||
if (d(1,2,'c'))
|
if (d(1,2,'c'))
|
||||||
then {
|
then {
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ external function inC: integer2character
|
|||||||
|
|
||||||
function entry: string2integer
|
function entry: string2integer
|
||||||
|
|
||||||
|
|
||||||
type rec: [integer: x; integer: y; integer: z; integer: a; integer: b; integer: c; integer: d]
|
type rec: [integer: x; integer: y; integer: z; integer: a; integer: b; integer: c; integer: d]
|
||||||
type T2: rec -> integer
|
type T2: rec -> integer
|
||||||
type T: integer -> integer
|
type T: integer -> integer
|
||||||
@@ -54,8 +53,9 @@ bar (a, b, c,d,e,f,g) := {
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry (arg) := {
|
entry (arg) := {
|
||||||
[integer:x; integer:y; integer: result]
|
[integer:x; integer:y; integer: result; character: a]
|
||||||
|
a := 'a';
|
||||||
|
x := printCharacter(a);
|
||||||
result := bar(1,2,3,4,5,6,7);
|
result := bar(1,2,3,4,5,6,7);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user