got compilation to work
This commit is contained in:
44
cg.s
Normal file
44
cg.s
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
movl $3, 0(%rbp)
|
||||||
|
movl $2, 1(%rbp)
|
||||||
|
movl $8, 2(%rbp)
|
||||||
|
movl 1(%rbp), %eax
|
||||||
|
subl 2(%rbp), %eax
|
||||||
|
movl %eax, 3(%rbp)
|
||||||
|
movl 0(%rbp), %eax
|
||||||
|
addl 0(%rbp), %eax
|
||||||
|
movl %eax, 4(%rbp)
|
||||||
|
movl $3, 5(%rbp)
|
||||||
|
movl $2, 6(%rbp)
|
||||||
|
movl $8, 7(%rbp)
|
||||||
|
movl 6(%rbp), %eax
|
||||||
|
cltd
|
||||||
|
idivl 7(%rbp)
|
||||||
|
movl %eax, 8(%rbp)
|
||||||
|
movl 5(%rbp), %eax
|
||||||
|
subl 8(%rbp), %eax
|
||||||
|
movl %eax, 9(%rbp)
|
||||||
|
movl $2, 10(%rbp)
|
||||||
|
movl $8, 11(%rbp)
|
||||||
|
movl $3, 12(%rbp)
|
||||||
|
movl $2, 13(%rbp)
|
||||||
|
movl 12(%rbp), %eax
|
||||||
|
subl 13(%rbp), %eax
|
||||||
|
movl %eax, 14(%rbp)
|
||||||
|
movl $8, 15(%rbp)
|
||||||
|
movl 14(%rbp), %eax
|
||||||
|
cltd
|
||||||
|
idivl 15(%rbp)
|
||||||
|
movl %edx, 16(%rbp)
|
||||||
|
movl $3, 17(%rbp)
|
||||||
|
movl $2, 18(%rbp)
|
||||||
|
movl 17(%rbp), %eax
|
||||||
|
cltd
|
||||||
|
idivl 18(%rbp)
|
||||||
|
movl %edx, 19(%rbp)
|
||||||
|
movl $8, 20(%rbp)
|
||||||
|
movl 19(%rbp), %eax
|
||||||
|
subl 20(%rbp), %eax
|
||||||
|
movl %eax, 21(%rbp)
|
||||||
|
movl $3, 22(%rbp)
|
||||||
|
movl $8, 23(%rbp)
|
||||||
|
movl $0, 24(%rbp)
|
12
src/runner.c
12
src/runner.c
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
FILE *ir_flag = NULL;
|
FILE *ir_flag = NULL;
|
||||||
FILE *cg_flag = NULL;
|
|
||||||
//Constant_Stack *head = NULL;
|
//Constant_Stack *head = NULL;
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
@ -38,6 +37,7 @@ int main(int argc, char *argv[]) {
|
|||||||
alpha_file = fopen(argv[argc - 1], "r");
|
alpha_file = fopen(argv[argc - 1], "r");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cg_flag = fopen("cg.s", "w");
|
||||||
return run(alpha_file);
|
return run(alpha_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +125,8 @@ int run(FILE *alpha) {
|
|||||||
print_symbol_table(top, st_flag);
|
print_symbol_table(top, st_flag);
|
||||||
emit_as_file(stdout, begin);
|
emit_as_file(stdout, begin);
|
||||||
fclose(st_flag);
|
fclose(st_flag);
|
||||||
|
generate();
|
||||||
|
fclose(cg_flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (asc_flag != NULL) {
|
if (asc_flag != NULL) {
|
||||||
@ -141,10 +143,10 @@ int run(FILE *alpha) {
|
|||||||
fclose(ir_flag);
|
fclose(ir_flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cg_flag != NULL) {
|
//if (cg_flag != NULL) {
|
||||||
printf("Flag -cg is not implemented yet\n");
|
// printf("Flag -cg is not implemented yet\n");
|
||||||
fclose(cg_flag);
|
//fclose(cg_flag);
|
||||||
}
|
//}
|
||||||
|
|
||||||
if (yyin != NULL) {
|
if (yyin != NULL) {
|
||||||
fclose(yyin);
|
fclose(yyin);
|
||||||
|
@ -48,6 +48,7 @@ FILE *alpha_file;
|
|||||||
FILE *tok_flag = NULL;
|
FILE *tok_flag = NULL;
|
||||||
FILE *st_flag = NULL;
|
FILE *st_flag = NULL;
|
||||||
FILE *asc_flag = NULL;
|
FILE *asc_flag = NULL;
|
||||||
|
FILE *cg_flag = NULL;
|
||||||
bool tc_flag = false;
|
bool tc_flag = false;
|
||||||
bool DEBUG = false;
|
bool DEBUG = false;
|
||||||
int no_flag = 0;
|
int no_flag = 0;
|
||||||
|
@ -1948,10 +1948,24 @@ char * label_gen(){
|
|||||||
label_count++;
|
label_count++;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TableNode* getTN(TNodeOrConst * tnc){
|
||||||
|
if(tnc->d == NODE){
|
||||||
|
return tnc->tnc_union->node;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
//we must fix this
|
||||||
|
int getConst(TNodeOrConst * tnc){
|
||||||
|
if(tnc->d == INTEGER){
|
||||||
|
return tnc->tnc_union->integer;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
//-------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------
|
||||||
int generate(){
|
int generate(){
|
||||||
offset = 0;
|
offset = 0;
|
||||||
Instruction* i = begin;
|
Instruction *i = begin;
|
||||||
while (i != NULL) {
|
while (i != NULL) {
|
||||||
switch(getOp(i)) {
|
switch(getOp(i)) {
|
||||||
case E_LABEL:
|
case E_LABEL:
|
||||||
@ -2023,6 +2037,7 @@ int generate(){
|
|||||||
default:
|
default:
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
i = i->next;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -2038,6 +2053,7 @@ int getAddress(CGNode *cg) {
|
|||||||
if (cg == NULL) {
|
if (cg == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return currentsp - cg->address;
|
return currentsp - cg->address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2047,10 +2063,7 @@ TableNode *getTNofCG(CGNode *cg) {
|
|||||||
}
|
}
|
||||||
return cg->tn;
|
return cg->tn;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
movl $1, -4(%rbp)
|
|
||||||
add -4(%rbp), $2
|
|
||||||
*/
|
|
||||||
CGNode *findCG(TableNode *tn) {
|
CGNode *findCG(TableNode *tn) {
|
||||||
CGNode *cg = cgList;
|
CGNode *cg = cgList;
|
||||||
while (cg != NULL) {
|
while (cg != NULL) {
|
||||||
@ -2085,20 +2098,20 @@ int generateAdd(Instruction *inst) {
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateAdd failed, NULL operand");
|
printdebug("generateAdd failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op1));
|
||||||
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;
|
||||||
@ -2109,9 +2122,9 @@ int generateAdd(Instruction *inst) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\taddl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\taddl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2121,21 +2134,21 @@ int generateSub(Instruction *instruction) {
|
|||||||
One immediate:
|
One immediate:
|
||||||
Neither immediate:
|
Neither immediate:
|
||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(instruction);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(instruction);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(instruction));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateSub failed, NULL operand");
|
printdebug("generateSub failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(instruction), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateSub failed, op1 is not constant but not in CGlist");
|
printdebug("generateSub failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2146,13 +2159,13 @@ int generateSub(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tsubl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\tsubl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateMult(Instruction *instruction){
|
int generateMult(Instruction *inst){
|
||||||
/*
|
/*
|
||||||
Both immediate:
|
Both immediate:
|
||||||
One immediate:
|
One immediate:
|
||||||
@ -2160,18 +2173,18 @@ int generateMult(Instruction *instruction){
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateMult failed, NULL operand");
|
printdebug("generateMult failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateMult failed, op1 is not constant but not in CGlist");
|
printdebug("generateMult failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2182,13 +2195,13 @@ int generateMult(Instruction *instruction){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tsubl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\tsubl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateDiv(Instruction *instruction) {
|
int generateDiv(Instruction *inst) {
|
||||||
/*
|
/*
|
||||||
Both immediate:
|
Both immediate:
|
||||||
One immediate:
|
One immediate:
|
||||||
@ -2196,19 +2209,19 @@ int generateDiv(Instruction *instruction) {
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateDiv failed, NULL operand");
|
printdebug("generateDiv failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateDiv failed, op1 is not constant but not in CGlist");
|
printdebug("generateDiv failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2219,14 +2232,14 @@ int generateDiv(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG)); //moves dividend into eax
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG)); //moves dividend into eax
|
||||||
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in 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, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg)); //stores result
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg)); //stores result
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateMod(Instruction *instruction) {
|
int generateMod(Instruction *inst) {
|
||||||
/*
|
/*
|
||||||
Both immediate:
|
Both immediate:
|
||||||
One immediate:
|
One immediate:
|
||||||
@ -2234,18 +2247,18 @@ int generateMod(Instruction *instruction) {
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateMod failed, NULL operand");
|
printdebug("generateMod failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateMod failed, op1 is not constant but not in CGlist");
|
printdebug("generateMod failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2256,14 +2269,14 @@ int generateMod(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG)); //moves dividend into eax
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG)); //moves dividend into eax
|
||||||
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in 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, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
|
||||||
fprintf(cg_flag, "\tmovl\t\%edx, %d(\%rbp)\n", getAddress(cg)); //stores result from edx (remainder)
|
fprintf(cg_flag, "\tmovl\t%%edx, %d(%%rbp)\n", getAddress(cg)); //stores result from edx (remainder)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateOr(Instruction *instruction) {
|
int generateOr(Instruction *inst) {
|
||||||
/*
|
/*
|
||||||
Both immediate:
|
Both immediate:
|
||||||
One immediate:
|
One immediate:
|
||||||
@ -2271,19 +2284,19 @@ int generateOr(Instruction *instruction) {
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateOr failed, NULL operand");
|
printdebug("generateOr failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateOr failed, op1 is not constant but not in CGlist");
|
printdebug("generateOr failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2294,13 +2307,13 @@ int generateOr(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\torll\t%d(\%rbp), %eax\n", getAddress(op2CG));//divides edx by value accessed from stack
|
fprintf(cg_flag, "\torll\t%d(%%rbp), %%eax\n", getAddress(op2CG));//divides edx by value accessed from stack
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg)); //stores result
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg)); //stores result
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateAnd(Instruction *instruction) {
|
int generateAnd(Instruction *inst) {
|
||||||
/*
|
/*
|
||||||
Both immediate:
|
Both immediate:
|
||||||
One immediate:
|
One immediate:
|
||||||
@ -2308,50 +2321,50 @@ int generateAnd(Instruction *instruction) {
|
|||||||
*/
|
*/
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("%sgenerateAnd failed, NULL operand", COLOR_RED);
|
printdebug("%sgenerateAnd failed, NULL operand", COLOR_RED);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
|
printdebug("generateAnd failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (op2CG == NULL) {
|
if (op2CG == NULL) {
|
||||||
printdebug("generateNeg failed, %s is not initialized/in CG", getName(getTN(op2)));
|
printdebug("generateAnd failed, %s is not initialized/in CG", getName(getTN(op2)));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tandl\t%d(\%rbp), %eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\tandl\t%d(%%rbp), %%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int generateNeg(Instruction *instruction) {
|
int generateNeg(Instruction *inst) {
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateNeg failed, NULL operand");
|
printdebug("generateNeg failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
|
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2362,27 +2375,27 @@ int generateNeg(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tnegl\t%d %eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\tnegl\t%d %%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int generateNot(Instruction *instruction) {
|
int generateNot(Instruction *inst) {
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
TNodeOrConst *op2 = getOperand2(inst);
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
if (op1 == NULL || op2 == NULL) {
|
if (op1 == NULL || op2 == NULL) {
|
||||||
printdebug("generateNot failed, NULL operand");
|
printdebug("generateNot failed, NULL operand");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
CGNode *op2CG = findCG(op2);
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateNot failed, op1 is not constant but not in CGlist");
|
printdebug("generateNot failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
@ -2393,15 +2406,15 @@ int generateNot(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tnotl\t\%eax\n", getAddress(op2CG));
|
fprintf(cg_flag, "\tnotl\t%%eax\n", getAddress(op2CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateAssign(Instruction *instruction) {
|
int generateAssign(Instruction *inst) {
|
||||||
TNodeOrConst *op1 = getOperand1(inst);
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
CGNode *result = findCG(getResult(inst));
|
CGNode *cg = findCG(getResult(inst));
|
||||||
|
|
||||||
|
|
||||||
if (op1 == NULL) {
|
if (op1 == NULL) {
|
||||||
@ -2409,22 +2422,25 @@ int generateAssign(Instruction *instruction) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL) {
|
if (cg == NULL) {
|
||||||
result = addCG(getResult(inst), offset);
|
cg = addCG(getResult(inst), offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//add option for constant assignment (should be easy)
|
//add option for constant assignment (should be easy)
|
||||||
|
if (isConst(op1) == true) {
|
||||||
|
fprintf(cg_flag, "\tmovl\t$%d, %d(%%rbp)\n", getConst(op1), getAddress(cg));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
CGNode *op1CG = findCG(op1);
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
if (op1CG == NULL) {
|
if (op1CG == NULL) {
|
||||||
printdebug("generateAssign failed, op1 is not constant but not in CGlist");
|
printdebug("generateAssign failed, op1 is not constant but not in CGlist");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(cg_flag, "\tmovl\t%d(\%rbp), \%eax\n", getAddress(op1CG));
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\n", getAddress(op1CG));
|
||||||
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\n", getAddress(cg));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2432,29 +2448,101 @@ int generateGoto(Instruction *instruction){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateCondGoto(Instruciton *instruction) {
|
int generateCondGoto(Instruction *instruction) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateIfTrue(Instruction *instruction){
|
int generateIfTrue(Instruction *instruction){
|
||||||
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 *instruction){
|
int generateIfFalse(Instruction *instruction){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int generateLessThan(Instruction *instruction){
|
int generateLessThan(Instruction *inst){
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
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;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cg == NULL) {
|
||||||
|
cg = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateLessThan failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateLessThan failed, %s is not initialized/in CG", getName(getTN(op2)));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\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)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
int generateEqualTo(Instruction *instruction){
|
|
||||||
|
int generateEqualTo(Instruction *inst){
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
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;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cg == NULL) {
|
||||||
|
cg = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(getTN(op1));
|
||||||
|
CGNode *op2CG = findCG(getTN(op2));
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateLessThan failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateLessThan failed, %s is not initialized/in CG", getName(getTN(op2)));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\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)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
int generateCall(Instruction *instruction){
|
int generateCall(Instruction *instruction){
|
||||||
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){
|
int generateReturn(Instruction *instruction){
|
||||||
return -1;
|
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 *instruction){
|
||||||
return -1;
|
return -1;
|
||||||
@ -2466,5 +2554,6 @@ int generateAddressOf(Instruction *instruction){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int generateParam(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;
|
return -1;
|
||||||
}
|
}
|
Reference in New Issue
Block a user