-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathassignment.cpp
51 lines (44 loc) · 1.69 KB
/
assignment.cpp
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
/**
* Implementation of \ref Assignment\ref class
*/
#include <utility>
#include "assignment.h"
#include "utilities.h"
/**
* Default constructor
* @param location location to which the assignment is done
* @param assignmentOperator name of the operator with which assignment is done can be "=" "+=" or "-="
* @param expression expression which has been assigned
*/
Assignment::Assignment(class Location *location, string assignmentOperator, class Expression *expression) {
this->stype = stmtType::NonReturn;
this->loc = location;
this->opr = std::move(assignmentOperator);
this->expr = expression;
}
Value *Assignment::generateCode(Constructs *compilerConstructs) {
Value *cur = compilerConstructs->NamedValues[loc->getVar()];
if (cur == nullptr) {
cur = compilerConstructs->TheModule->getGlobalVariable(loc->getVar());
}
if (cur == nullptr) {
compilerConstructs->errors++;
return reportError("Unknown Variable Name " + loc->getVar());
}
Value *val = expr->generateCode(compilerConstructs);
if (expr->getEtype() == exprType::location) {
val = compilerConstructs->Builder->CreateLoad(val);
}
Value *lhs = loc->generateCode(compilerConstructs);
cur = compilerConstructs->Builder->CreateLoad(lhs);
if (val == nullptr) {
compilerConstructs->errors++;
return reportError("Error in right hand side of the Assignment");
}
if (opr == "+=") {
val = compilerConstructs->Builder->CreateAdd(cur, val, "addEqualToTmp");
} else if (opr == "-=") {
val = compilerConstructs->Builder->CreateSub(cur, val, "subEqualToTmp");
}
return compilerConstructs->Builder->CreateStore(val, lhs);
}