trying to get annie's to work
This commit is contained in:
@ -647,8 +647,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_ADD,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of binary operation
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -662,8 +661,8 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_SUB,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of binary operation
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -677,8 +676,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_MUL,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of binary operation
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -692,8 +690,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_DIV,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of binary operation
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -707,8 +704,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
if((getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_MOD,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of binary operation
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -722,8 +718,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
|
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_AND,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of comparison
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -737,8 +732,7 @@ expression:
|
|||||||
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
|
if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_OR,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of comparison
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -752,8 +746,7 @@ expression:
|
|||||||
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
|
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_LESS_THAN,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of comparison
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
} else {
|
} else {
|
||||||
$$=undefined;
|
$$=undefined;
|
||||||
@ -767,8 +760,7 @@ expression:
|
|||||||
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
|
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
|
||||||
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK)
|
emit_binary_op(E_EQUAL_TO,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
|
||||||
//result of compariosn
|
|
||||||
$$ = node;
|
$$ = node;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
// check if start is NULL if it is assign it to the start globle variable
|
// check if start is NULL if it is assign it to the start globle variable
|
||||||
// otherwise make it next of current and set cur to your instruction.
|
// otherwise make it next of current and set cur to your instruction.
|
||||||
|
|
||||||
|
|
||||||
TNodeOrConst * tn_or_const(Op op, void * tnc) {
|
TNodeOrConst * tn_or_const(Op op, void * tnc) {
|
||||||
TNodeOrConst * count = calloc(1, sizeof(*count));
|
TNodeOrConst * count = calloc(1, sizeof(*count));
|
||||||
count->d = op;
|
count->d = op;
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
/* The Translators - Spring 2025 */
|
/* The Translators - Spring 2025 */
|
||||||
|
|
||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
|
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) {
|
||||||
|
@ -35,7 +35,8 @@
|
|||||||
#include "../tmp/grammar.tab.h"
|
#include "../tmp/grammar.tab.h"
|
||||||
#include "symbol_table.h"
|
#include "symbol_table.h"
|
||||||
|
|
||||||
extern int line_number, column_number;
|
extern int line_number;
|
||||||
|
extern int column_number;
|
||||||
extern char *yytext;
|
extern char *yytext;
|
||||||
extern FILE *yyin;
|
extern FILE *yyin;
|
||||||
extern bool DEBUG;
|
extern bool DEBUG;
|
||||||
@ -46,8 +47,6 @@ SymbolTable *cur;
|
|||||||
FILE *alpha_file;
|
FILE *alpha_file;
|
||||||
FILE *tok_flag = NULL;
|
FILE *tok_flag = NULL;
|
||||||
FILE *st_flag = NULL;
|
FILE *st_flag = NULL;
|
||||||
FILE *ir_flag = NULL;
|
|
||||||
FILE *cg_flag = NULL;
|
|
||||||
FILE *asc_flag = NULL;
|
FILE *asc_flag = NULL;
|
||||||
bool tc_flag = false;
|
bool tc_flag = false;
|
||||||
bool DEBUG = false;
|
bool DEBUG = false;
|
||||||
|
@ -10,6 +10,9 @@ char* temp = NULL;
|
|||||||
int label_count=0;
|
int label_count=0;
|
||||||
Instruction* begin = NULL;
|
Instruction* begin = NULL;
|
||||||
Instruction* current = NULL;
|
Instruction* current = NULL;
|
||||||
|
int offset = 0;
|
||||||
|
int currentsp = 0;
|
||||||
|
CGNode *cgList = NULL;
|
||||||
|
|
||||||
void printdebug_impl(char *file, int line, const char *format, ...) {
|
void printdebug_impl(char *file, int line, const char *format, ...) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -1574,7 +1577,7 @@ TNodeOrConst * getOperand2(Instruction * i){
|
|||||||
return i->operand2;
|
return i->operand2;
|
||||||
}
|
}
|
||||||
|
|
||||||
TableNode * get_result(Instruction * i){
|
TableNode * getResult(Instruction * i){
|
||||||
return i->result;
|
return i->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1672,12 +1675,10 @@ char * get_string(TNodeOrConst * tc){
|
|||||||
case STRING:
|
case STRING:
|
||||||
return tc->tnc_union->string;
|
return tc->tnc_union->string;
|
||||||
case INTEGER:
|
case INTEGER:
|
||||||
return getName(integ);
|
|
||||||
s = calloc(10, sizeof(char));
|
s = calloc(10, sizeof(char));
|
||||||
sprintf(s, "%d", tc->tnc_union->integer);
|
sprintf(s, "%d", tc->tnc_union->integer);
|
||||||
return s;
|
return s;
|
||||||
case CHARACTER:
|
case CHARACTER:
|
||||||
return getName(chara);
|
|
||||||
s = calloc(2, sizeof(char));
|
s = calloc(2, sizeof(char));
|
||||||
sprintf(s, "%c", tc->tnc_union->character);
|
sprintf(s, "%c", tc->tnc_union->character);
|
||||||
return s;
|
return s;
|
||||||
@ -1690,11 +1691,12 @@ char * get_string(TNodeOrConst * tc){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void emit_as_file(FILE * out_file, Instruction * i){
|
void emit_as_file(FILE * out_file, Instruction * i){
|
||||||
if(!i){
|
if(i == NULL){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch(i->opcode){
|
switch(i->opcode){
|
||||||
case E_LABEL:
|
case E_LABEL:
|
||||||
|
break;
|
||||||
// this is a terrible one to start with
|
// this is a terrible one to start with
|
||||||
// fprintf(out_file, "%04.d: %d ", i->index, i->label);
|
// fprintf(out_file, "%04.d: %d ", i->index, i->label);
|
||||||
case E_ADD:
|
case E_ADD:
|
||||||
@ -1752,7 +1754,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
|
|||||||
case E_ASSIGN:
|
case E_ASSIGN:
|
||||||
fprintf(out_file, "%4.d: %s = %s\n",
|
fprintf(out_file, "%4.d: %s = %s\n",
|
||||||
i->index, getName(i->result),
|
i->index, getName(i->result),
|
||||||
get_string(i->operand2));
|
get_string(i->operand1));
|
||||||
break;
|
break;
|
||||||
case E_GOTO:
|
case E_GOTO:
|
||||||
// are we ever going to use this?
|
// are we ever going to use this?
|
||||||
@ -1946,3 +1948,523 @@ char * label_gen(){
|
|||||||
label_count++;
|
label_count++;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
//-------------------------------------------------------------------------------------
|
||||||
|
int generate(){
|
||||||
|
offset = 0;
|
||||||
|
Instruction* i = begin;
|
||||||
|
while (i != NULL) {
|
||||||
|
switch(getOp(i)) {
|
||||||
|
case E_LABEL:
|
||||||
|
generateLabel(i);
|
||||||
|
break;
|
||||||
|
case E_ADD:
|
||||||
|
generateAdd(i);
|
||||||
|
break;
|
||||||
|
case E_SUB:
|
||||||
|
generateSub(i);
|
||||||
|
break;
|
||||||
|
case E_MUL:
|
||||||
|
generateMult(i);
|
||||||
|
break;
|
||||||
|
case E_DIV:
|
||||||
|
generateDiv(i);
|
||||||
|
break;
|
||||||
|
case E_MOD:
|
||||||
|
generateMod(i);
|
||||||
|
break;
|
||||||
|
case E_OR:
|
||||||
|
generateOr(i);
|
||||||
|
break;
|
||||||
|
case E_AND:
|
||||||
|
generateAnd(i);
|
||||||
|
break;
|
||||||
|
case E_NEG:
|
||||||
|
generateNeg(i);
|
||||||
|
break;
|
||||||
|
case E_NOT:
|
||||||
|
generateNot(i);
|
||||||
|
break;
|
||||||
|
case E_ASSIGN:
|
||||||
|
generateAssign(i);
|
||||||
|
break;
|
||||||
|
case E_GOTO:
|
||||||
|
generateGoto(i);
|
||||||
|
break;
|
||||||
|
case E_IF_X_TRUE:
|
||||||
|
generateIfTrue(i);
|
||||||
|
break;
|
||||||
|
case E_IF_X_FALSE:
|
||||||
|
generateIfFalse(i);
|
||||||
|
break;
|
||||||
|
case E_LESS_THAN:
|
||||||
|
generateLessThan(i);
|
||||||
|
break;
|
||||||
|
case E_EQUAL_TO:
|
||||||
|
generateEqualTo(i);
|
||||||
|
break;
|
||||||
|
case E_CALL:
|
||||||
|
generateCall(i);
|
||||||
|
break;
|
||||||
|
case E_PARAM:
|
||||||
|
generateParam(i);
|
||||||
|
break;
|
||||||
|
case E_RETURN:
|
||||||
|
generateReturn(i);
|
||||||
|
break;
|
||||||
|
case E_INDEX_COPY_RIGHT:
|
||||||
|
generateCopyRight(i);
|
||||||
|
break;
|
||||||
|
case E_INDEX_COPY_LEFT:
|
||||||
|
generateCopyLeft(i);
|
||||||
|
break;
|
||||||
|
case E_ADDRESS_OF:
|
||||||
|
generateAddressOf(i);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *getNextCG(CGNode *cg) {
|
||||||
|
if (cg == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return cg->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getAddress(CGNode *cg) {
|
||||||
|
if (cg == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return currentsp - cg->address;
|
||||||
|
}
|
||||||
|
|
||||||
|
TableNode *getTNofCG(CGNode *cg) {
|
||||||
|
if (cg == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return cg->tn;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
movl $1, -4(%rbp)
|
||||||
|
add -4(%rbp), $2
|
||||||
|
*/
|
||||||
|
CGNode *findCG(TableNode *tn) {
|
||||||
|
CGNode *cg = cgList;
|
||||||
|
while (cg != NULL) {
|
||||||
|
if (getTNofCG(cg) == tn) {
|
||||||
|
return cg;
|
||||||
|
}
|
||||||
|
cg = getNextCG(cg);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *addCG(TableNode *tn, int sp) {
|
||||||
|
CGNode *cg = calloc(1, sizeof(CGNode));
|
||||||
|
cg->tn = tn;
|
||||||
|
cg->address = sp;
|
||||||
|
offset += getPrimSize(tn); //not sure if this is the correct amount to add to the offset
|
||||||
|
cg->next = cgList;
|
||||||
|
cgList = cg;
|
||||||
|
return cg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int generateLabel(Instruction *inst) {
|
||||||
|
fprintf(cg_flag, ".L%d:\n", getLabel(inst));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int generateAdd(Instruction *inst) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateAdd failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateAdd failed, %s is not initialized/in CG", getName(getTN(op1)));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateAdd 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, "\taddl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateSub(Instruction *instruction) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateSub failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateSub failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateAdd 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, "\tsubl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateMult(Instruction *instruction){
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateMult failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateMult failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateMult 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, "\tsubl\t%d(\%rbp), \%eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateDiv(Instruction *instruction) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateDiv failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateDiv failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateDiv failed, %s is not initialized/in CG", getName(getTN(op2)));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, "\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
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateMod(Instruction *instruction) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateMod failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
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\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\%edx, %d(\%rbp)\n", getAddress(cg)); //stores result from edx (remainder)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateOr(Instruction *instruction) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateOr failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateOr failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateOr 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, "\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
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateAnd(Instruction *instruction) {
|
||||||
|
/*
|
||||||
|
Both immediate:
|
||||||
|
One immediate:
|
||||||
|
Neither immediate:
|
||||||
|
*/
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("%sgenerateAnd failed, NULL operand", COLOR_RED);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateNeg 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, "\tandl\t%d(\%rbp), %eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int generateNeg(Instruction *instruction) {
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateNeg failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateNeg failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateNeg 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, "\tnegl\t%d %eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int generateNot(Instruction *instruction) {
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
TNodeOrConst *op2 = getOperand2(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
if (op1 == NULL || op2 == NULL) {
|
||||||
|
printdebug("generateNot failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(op1);
|
||||||
|
CGNode *op2CG = findCG(op2);
|
||||||
|
if (op1CG == NULL) {
|
||||||
|
printdebug("generateNot failed, op1 is not constant but not in CGlist");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (op2CG == NULL) {
|
||||||
|
printdebug("generateNot 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, "\tnotl\t\%eax\n", getAddress(op2CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateAssign(Instruction *instruction) {
|
||||||
|
TNodeOrConst *op1 = getOperand1(inst);
|
||||||
|
CGNode *result = findCG(getResult(inst));
|
||||||
|
|
||||||
|
|
||||||
|
if (op1 == NULL) {
|
||||||
|
printdebug("generateAssign failed, NULL operand");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL) {
|
||||||
|
result = addCG(getResult(inst), offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//add option for constant assignment (should be easy)
|
||||||
|
|
||||||
|
|
||||||
|
CGNode *op1CG = findCG(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\n", getAddress(op1CG));
|
||||||
|
fprintf(cg_flag, "\tmovl\t\%eax, %d(\%rbp)\n", getAddress(cg));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateGoto(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateCondGoto(Instruciton *instruction) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateIfTrue(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateIfFalse(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int generateLessThan(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateEqualTo(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateCall(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateReturn(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateCopyRight(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateCopyLeft(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateAddressOf(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int generateParam(Instruction *instruction){
|
||||||
|
return -1;
|
||||||
|
}
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
struct TableNode;
|
struct TableNode;
|
||||||
typedef struct TFList TFList;
|
typedef struct TFList TFList;
|
||||||
|
typedef struct CGNode CGNode;
|
||||||
|
|
||||||
typedef struct Constant_Stack {
|
typedef struct Constant_Stack {
|
||||||
struct TableNode *theType;
|
struct TableNode *theType;
|
||||||
@ -328,3 +329,39 @@ Track 4: Memory Access & Integration
|
|||||||
* Build integration test suite covering all language features
|
* Build integration test suite covering all language features
|
||||||
* Implement row-major/column-major array layout calculation
|
* Implement row-major/column-major array layout calculation
|
||||||
*/
|
*/
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
extern FILE *cg_flag;
|
||||||
|
typedef struct CGNode {
|
||||||
|
TableNode *tn;
|
||||||
|
int address;
|
||||||
|
CGNode *next;
|
||||||
|
}CGNode;
|
||||||
|
|
||||||
|
int generate();
|
||||||
|
CGNode *getNextCG(CGNode *cg);
|
||||||
|
int getAddress(CGNode *cg);
|
||||||
|
TableNode *getTNofCG(CGNode *cg);
|
||||||
|
int generateLabel(Instruction *instruction);
|
||||||
|
int generateAdd(Instruction *instruction);
|
||||||
|
int generateSub(Instruction *instruction);
|
||||||
|
int generateMult(Instruction *instruction);
|
||||||
|
int generateDiv(Instruction *instruction);
|
||||||
|
int generateMod(Instruction *instruction);
|
||||||
|
int generateOr(Instruction *instruction);
|
||||||
|
int generateAnd(Instruction *instruction);
|
||||||
|
int generateNeg(Instruction *instruction);
|
||||||
|
int generateNot(Instruction *instruction);
|
||||||
|
int generateDiv(Instruction *instruction);
|
||||||
|
int generateAssign(Instruction *instruction); // is this for something like x = 1?
|
||||||
|
int generateGoto(Instruction *instruction);
|
||||||
|
int generateCondGoto(Instruction *instruction);
|
||||||
|
int generateIfTrue(Instruction *instruction);
|
||||||
|
int generateIfFalse(Instruction *instruction);
|
||||||
|
int generateLessThan(Instruction *instruction);
|
||||||
|
int generateEqualTo(Instruction *instruction);
|
||||||
|
int generateCall(Instruction *instruction);
|
||||||
|
int generateReturn(Instruction *instruction);
|
||||||
|
int generateCopyRight(Instruction *instruction);
|
||||||
|
int generateCopyLeft(Instruction *instruction);
|
||||||
|
int generateAddressOf(Instruction *instruction);
|
||||||
|
int generateParam(Instruction *instruction);
|
Reference in New Issue
Block a user