[Custom Thumbnail]
All the Code of the series can be found at the Github repository:
https://github.com/drifter1/compiler
void generate_bool(FILE *fp, AST_Node_Bool *node){
int const_op = 0;
/* GP: 0, Constant: 1 */
int Operand1 = 0;
int Operand2 = 0;
AST_Node_Const *temp_const;
if(node->op != NOT){
if(node->left->type == CONST_NODE){
const_op = 1;
Operand1 = 1;
}
else if(node->right->type == CONST_NODE){
const_op = 1;
Operand2 = 1;
}
}
/* operation */
switch(node->op){
case OR:
if (const_op == 1){
if(Operand1 == 1){
temp_const = (AST_Node_Const*) node->left;
fprintf(fp,"ORI %s, %s, %d\n",
GetRegisterName(node->g_index, 0),
GetRegisterName(getGraphIndex(node->right), 0),
temp_const->val);
}
else{
temp_const = (AST_Node_Const*) node->right;
fprintf(fp,"ORI %s, %s, %d\n",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->left),0),
temp_const->val);
}
}
else{
fprintf(fp,"OR %s, %s, %s\n",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->left),0),
GetRegisterName(getGraphIndex(node->right),0));
}
break;
case AND:
if (const_op == 1){
if(Operand1 == 1){
temp_const = (AST_Node_Const*) node->left;
fprintf(fp,"ANDI %s, %s, %d\n",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->right),0),
temp_const->val);
}
else{
temp_const = (AST_Node_Const*) node->right;
fprintf(fp,"ANDI %s, %s, %d\n",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->left),0),
temp_const->val);
}
}
else{
fprintf(fp,"AND %s, %s, %s\n",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->left),0),
' GetRegisterName(getGraphIndex(node->right),0));
}
break;
case NOT:
fprintf(fp, "NOT %s, %s",
GetRegisterName(node->g_index ,0),
GetRegisterName(getGraphIndex(node->left),0));
break;
default:
fprintf(stderr, "Error in OP selection!\n");
exit(1);
}
}
int const_op = 0;
int float_op = 0;
/* GP: 0, FP: 1, Constant: 2, FP Constant: 3 */
int Operand1 = 0;
int Operand2 = 0;
/* check left operand */
if (expression_data_type(node->left) == REAL_TYPE){
float_op = 1;
if(node->left->type == CONST_NODE){
const_op = 1;
Operand1 = 3;
}
else{
Operand1 = 1;
}
}
else{
if(node->left->type == CONST_NODE){
const_op = 1;
Operand1 = 2;
}
else{
Operand1 = 0;
}
}
/* check right operand */
if(expression_data_type(node->right) == REAL_TYPE){
float_op = 1;
if(node->right->type == CONST_NODE){
const_op = 1;
Operand2 = 3;
}
else{
Operand2 = 1;
}
}
else{
if(node->right->type == CONST_NODE){
const_op = 1;
Operand2 = 2;
}
else{
Operand2 = 0;
}
}
* inverted logic */
int op;
if(invLogic == 1){
switch(node->op){
case GREATER:
op = LESS_EQUAL;
break;
case LESS:
op = GREATER_EQUAL;
break;
case GREATER_EQUAL:
op = LESS;
break;
case LESS_EQUAL:
op = GREATER;
break;
default:
fprintf(stderr, "Error in OP selection!\n");
exit(1);
}
}
else{
op = node->op;
}
if(float_op == 1){
if(const_op == 1){
if(Operand1 == 2 || Operand1 == 3){
temp_const = (AST_Node_Const *) node->left;
}
else{
temp_const = (AST_Node_Const *) node->right;
}
if(temp_const->const_type == REAL_TYPE){
fprintf(fp, "LI.D $f28, %.2f\n", temp_const->val);
}
else{
fprintf(fp, "LI.D $f28, %d.0\n", temp_const->val);
}
}
if(Operand1 == 0){
fprintf(fp, "MTC1.D %s, $f30\n",
GetRegisterName(getGraphIndex(node->left) , 0));
fprintf(fp, "CVT.D.W $f30, $f30\n");
}
else if(Operand2 == 0){
fprintf(fp, "MTC1.D %s, $f30\n",
GetRegisterName(getGraphIndex(node->right) , 0));
fprintf(fp, "CVT.D.W $f30, $f30\n");
}
fprintf(fp, "C.LE.D ");
switch(Operand1){
case 0:
fprintf(fp, "$f30 ");
break;
case 1:
fprintf(fp, "%s ", GetRegisterName(getGraphIndex(node->left) , 1));
break;
case 2:
case 3:
fprintf(fp, "$f28 ");
}
switch(Operand2){
case 0:
fprintf(fp, "$f30 ");
break;
case 1:
fprintf(fp, "%s ", GetRegisterName(getGraphIndex(node->right) , 1));
break;
case 2:
case 3:
fprintf(fp, "$f28 ");
}
fprintf(fp, "\n");
fprintf(fp, "BC1F %s\n", Label);
}
else if(const_op == 1){
if(Operand1 != 0){
temp_const = (AST_Node_Const *) node->left;
fprintf(fp, "BLE %s, %d, %s\n",
GetRegisterName(getGraphIndex(node->right), 0),
temp_const->val, Label);
}
else if(Operand2 != 0){
temp_const = (AST_Node_Const *) node->right;
fprintf(fp, "BGT %s, %d, %s\n",
GetRegisterName(getGraphIndex(node->left), 0),
temp_const->val, Label);
}
}
else{
fprintf(fp, "BGT %s, %s, %s\n",
GetRegisterName(getGraphIndex(node->left), 0),
GetRegisterName(getGraphIndex(node->right), 0), Label);
}int op;
if(invLogic == 1){
/* EQUAL NOT_EQUAL */
if(node->op == EQUAL){
op = NOT_EQUAL;
}
else{
op = EQUAL;
}
}
else{
op = node->op;
}
.data
# variables
i: .word 0
val: .double 2.500000
res: .space 80
# messages
.text
main:
LW $s0, i($0)
BGE $s0, 10, Temp_Label
L.D $f2, val
LW $s0, i($0)
LI.D $f6, 0.0
LW $s2, res($0)
LW $s2, res($0)
.data
# variables
c: .byte 'c'
i: .word 0
p: .word 0
val: .double 2.500000
res: .double 0.500000, 1.500000, 2.500000, 3.500000, 4.500000, 5.500000
# messages
msg1: .asciiz "\n"
msg2: .asciiz "\n"
msg3: .asciiz "iteration: 3\n"
msg4: .asciiz " "
msg5: .asciiz "\n"
.text
main:
LA $s3, res($0)
LW $s0, i($0)
BGE $s0, 10, Temp_Label
LW $s0, i($0)
BGT $s0, 5, Temp_Label
LW $s0, i($0)
BNE $s0, 5, Temp_Label
LW $s0, i($0)
MULI $s5, $s0, 2
LI $s6, 0
L.D $f4, val
LW $s0, i($0)
LI.D $f14, 0.0
LW $s3, res($0)
L.D $f4, val
LW $s0, i($0)
LI.D $f16, 0.0
LW $s3, res($0)
LW $s3, res($0)
LW $s4, p($0)
ADDI $t1, $s4, 1
LW $s0, i($0)
BNE $s0, 2, Temp_Label
L.D $f4, val
LI.D $f28, 4.50
C.EQ.D $f4 $f28
BC1F Temp_Label
AND $s0, $s0, $s0
LW $s0, i($0)
BGE $s0, 12, Temp_Label
LW $s0, i($0)
LW $s1, c($0)