Demo file choice

This commit is contained in:
Meyer Simon
2025-04-30 09:36:05 -04:00
parent 856e443181
commit 5b3de8cb02
3 changed files with 52 additions and 47 deletions

View File

@ -46,7 +46,7 @@ int generate(){
break;
case E_IF_X_TRUE:
generateIfTrue(i);
break;
break;
case E_IF_X_FALSE:
generateIfFalse(i);
break;
@ -118,7 +118,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;
@ -139,7 +139,7 @@ int generateAdd(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("generateAdd failed, NULL operand");
return -1;
@ -148,8 +148,8 @@ int generateAdd(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op1));
if (op1CG == NULL) {
@ -177,7 +177,7 @@ int generateSub(Instruction *instruction) {
TNodeOrConst *op1 = getOperand1(instruction);
TNodeOrConst *op2 = getOperand2(instruction);
CGNode *cg = findCG(getResult(instruction));
if (op1 == NULL || op2 == NULL) {
printdebug("generateSub failed, NULL operand");
return -1;
@ -186,7 +186,7 @@ int generateSub(Instruction *instruction) {
if (cg == NULL) {
cg = addCG(getResult(instruction), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -214,7 +214,7 @@ int generateMult(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("generateMult failed, NULL operand");
return -1;
@ -250,7 +250,7 @@ int generateDiv(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("generateDiv failed, NULL operand");
return -1;
@ -259,7 +259,7 @@ int generateDiv(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -275,7 +275,7 @@ int generateDiv(Instruction *inst) {
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#division start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#division end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#division end\n", getAddress(cg)); //stores result
return 0;
}
@ -288,7 +288,7 @@ int generateMod(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("generateMod failed, NULL operand");
return -1;
@ -303,12 +303,12 @@ int generateMod(Instruction *inst) {
printdebug("generateMod failed, op1 is not constant but not in CGlist");
return -1;
}
if (op2CG == NULL) {
printdebug("generateMod failed, %s is not initialized/in CG", getName(getTN(op2)));
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#mod start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
@ -325,7 +325,7 @@ int generateOr(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("generateOr failed, NULL operand");
return -1;
@ -334,7 +334,7 @@ int generateOr(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -349,22 +349,22 @@ int generateOr(Instruction *inst) {
int label = label_gen();
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#start or\n", getAddress(op1CG));
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#start or\n", getAddress(op1CG));
fprintf(cg_flag, "\tjne\t.L%dor2\n", label);
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\n", getAddress(op2CG));
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\n", getAddress(op2CG));
fprintf(cg_flag, "\tje\t.L%dor3\n", label);
fprintf(cg_flag, ".L%dor2:\n", label);
fprintf(cg_flag, "\tmovl\t$1, %%eax\n");
fprintf(cg_flag, "\tmovl\t$1, %%eax\n");
fprintf(cg_flag, "\tjmp\t.L%dor4\n", label);
fprintf(cg_flag, ".L%dor3:\n", label);
fprintf(cg_flag, "\tmovl\t$0, %%eax\n");
fprintf(cg_flag, "\tmovl\t$0, %%eax\n");
fprintf(cg_flag, ".L%dor4:\n", label);
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\n", getAddress(cg));
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#or end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#or end\n", getAddress(cg)); //stores result
return 0;
}
@ -377,7 +377,7 @@ int generateAnd(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("%sgenerateAnd failed, NULL operand", COLOR_RED);
return -1;
@ -386,7 +386,7 @@ int generateAnd(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -400,27 +400,27 @@ int generateAnd(Instruction *inst) {
}
int label = label_gen();
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#start and\n", getAddress(op1CG));
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#start and\n", getAddress(op1CG));
fprintf(cg_flag, "\tje\t.L%dor2\n", label);
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\n", getAddress(op2CG));
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\n", getAddress(op2CG));
fprintf(cg_flag, "\tje\t.L%dor2\n", label);
fprintf(cg_flag, "\tmovl\t$1, %%eax\n");
fprintf(cg_flag, "\tmovl\t$1, %%eax\n");
fprintf(cg_flag, "\tjmp\t.L%dor3\n", label);
fprintf(cg_flag, ".L%dor2:\n", label);
fprintf(cg_flag, "\tmovl\t$0, %%eax\n");
fprintf(cg_flag, "\tmovl\t$0, %%eax\n");
fprintf(cg_flag, ".L%dor3:\n", label);
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\n", getAddress(cg));
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#and end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#and end\n", getAddress(cg)); //stores result
return 0;
}
int generateNeg(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL) {
printdebug("generateNeg failed, NULL operand");
return -1;
@ -429,14 +429,14 @@ int generateNeg(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#negation start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#negation start\n", getAddress(op1CG));
fprintf(cg_flag, "\tnegl\t%%eax\n");
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#negation end\n", getAddress(cg));
return 0;
@ -444,7 +444,7 @@ int generateNeg(Instruction *inst) {
int generateNot(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL) {
printdebug("generateNot failed, NULL operand");
return -1;
@ -453,7 +453,7 @@ int generateNot(Instruction *inst) {
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateNot failed, op1 is not constant but not in CGlist");
@ -474,7 +474,7 @@ int generateAssign(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL) {
printdebug("generateAssign failed, NULL operand");
return -1;
@ -490,14 +490,14 @@ int generateAssign(Instruction *inst) {
fprintf(cg_flag, "\tmovl\t$%d, %d(%%rbp)\t#constant assign\n", getConst(op1), getAddress(cg));
return 0;
}
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateAssign failed, op1 is not constant but not in CGlist");
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#assign start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#assign start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#assign end\n", getAddress(cg));
return 0;
}
@ -528,7 +528,7 @@ int generateLessThan(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("%sgenerateLessThan failed, NULL operand", COLOR_RED);
return -1;
@ -537,7 +537,7 @@ int generateLessThan(Instruction *inst){
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -550,7 +550,7 @@ int generateLessThan(Instruction *inst){
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#less than start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#less than start\n", getAddress(op1CG));
fprintf(cg_flag, "\tcmpl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
fprintf(cg_flag, "\tsetl\t%%al\n");
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#less than end\n", getAddress(cg));
@ -566,7 +566,7 @@ int generateEqualTo(Instruction *inst){
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL || op2 == NULL) {
printdebug("%sgenerateLessThan failed, NULL operand", COLOR_RED);
return -1;
@ -575,7 +575,7 @@ int generateEqualTo(Instruction *inst){
if (cg == NULL) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -588,7 +588,7 @@ int generateEqualTo(Instruction *inst){
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#equal to start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#equal to start\n", getAddress(op1CG));
fprintf(cg_flag, "\tcmpl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
fprintf(cg_flag, "\tsete\t%%al\n");
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg));
@ -614,4 +614,4 @@ int generateAddressOf(Instruction *instruction){
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;
}
}

View File

@ -129,6 +129,10 @@ TNodeOrConst * tn_or_const(Discriminant d, void * tnc) {
}
static void emit_helper(void){
if (!ir_flag) {
begin = NULL;
return;
}
Instruction * inst = calloc(1, sizeof(*inst));
if(begin == NULL){
begin = current = inst;

View File

@ -121,6 +121,7 @@ extern Instruction * begin;
extern Instruction * current;
extern int label_count;
extern bool code_gen;
extern FILE * ir_flag;
TNodeOrConst * tn_or_const(Discriminant , void * );