Repository navigation
Expand file tree
/
Copy pathbinaryExpression.cpp
More file actions
68 lines (64 loc) · 2.73 KB
/
Copy pathbinaryExpression.cpp
File metadata and controls
68 lines (64 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Implementation of \ref binaryExpression class */
#include <utility>
#include "binaryExpression.h"
#include "location.h"
#include "utilities.h"
/**
* Constructor for binaryExpression class
* @param lhs left hand side of the expression
* @param opr operator present in the expression can be
* arithmetic operators like "+", "-", "*", "/", "%" or
* comparing operators like "<", ">", "<=", ">=", "==", "!="
* @param rhs right hand side of the expression
*/
binaryExpression::binaryExpression(class Expression *lhs, string opr, class Expression *rhs) {
this->lhs = lhs;
this->rhs = rhs;
this->opr = std::move(opr);
this->etype = exprType::binary;
}
Value *binaryExpression::generateCode(Constructs *compilerConstructs) {
Value *left = lhs->generateCode(compilerConstructs);
Value *right = rhs->generateCode(compilerConstructs);
if (lhs->getEtype() == exprType::location) {
Location *loc = static_cast<Location *>(lhs);
left = compilerConstructs->Builder->CreateLoad(loc->getValueType(left), left);
}
if (rhs->getEtype() == exprType::location) {
Location *loc = static_cast<Location *>(rhs);
right = compilerConstructs->Builder->CreateLoad(loc->getValueType(right), right);
}
if (left == 0) {
compilerConstructs->errors++;
return reportError("Error in left operand of " + opr);
} else if (right == 0) {
compilerConstructs->errors++;
return reportError("Error in right operand of " + opr);
}
Value *v = nullptr;
if (opr == "+") {
v = compilerConstructs->Builder->CreateAdd(left, right, "addtmp");
} else if (opr == "-") {
v = compilerConstructs->Builder->CreateSub(left, right, "subtmp");
} else if (opr == "*") {
v = compilerConstructs->Builder->CreateMul(left, right, "multmp");
} else if (opr == "/") {
v = compilerConstructs->Builder->CreateSDiv(left, right, "divtmp");
} else if (opr == "%") {
v = compilerConstructs->Builder->CreateSRem(left, right, "modtmp");
} else if (opr == "<") {
v = compilerConstructs->Builder->CreateICmpSLT(left, right, "ltcomparetmp");
} else if (opr == ">") {
v = compilerConstructs->Builder->CreateICmpSGT(left, right, "gtcomparetmp");
} else if (opr == "<=") {
v = compilerConstructs->Builder->CreateICmpSLE(left, right, "lecomparetmp");
} else if (opr == ">=") {
v = compilerConstructs->Builder->CreateICmpSGE(left, right, "gecomparetmp");
} else if (opr == "==") {
v = compilerConstructs->Builder->CreateICmpEQ(left, right, "equalcomparetmp");
} else if (opr == "!=") {
v = compilerConstructs->Builder->CreateICmpNE(left, right, "notequalcomparetmp");
}
return v;
}