Repository navigation
Expand file tree
/
Copy pathVariableIrGen.cpp
More file actions
228 lines (192 loc) · 7.98 KB
/
Copy pathVariableIrGen.cpp
File metadata and controls
228 lines (192 loc) · 7.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "AST.hpp"
#include "llvm/Passes/PassBuilder.h"
#include <sstream>
#include <iomanip>
std::string escape_to_hex(const std::string& input_str) {
std::ostringstream hex_str;
bool isescapeChar = false;
for (char ch : input_str) {
if (ch == '\\' && !isescapeChar) isescapeChar = true;
else if (isescapeChar) {
switch (ch) {
case '\'': hex_str << "\x27"; break;
case '\"': hex_str << "\x22"; break;
case '?': hex_str << "\x3F"; break;
case '\\': hex_str << "\x5C"; break;
case 'a': hex_str << "\x07"; break;
case 'b': hex_str << "\x08"; break;
case 'f': hex_str << "\x0C"; break;
case 'n': hex_str << "\x0A"; break;
case 'r': hex_str << "\x0D"; break;
case 't': hex_str << "\x09"; break;
case 'v': hex_str << "\x0B"; break;
}
isescapeChar = false;
}
else hex_str << ch;
}
return hex_str.str();
}
// ********************************** TYPES **********************************
optional<Value *> IntExpr::codegen(CompilationContext &ctx)
{
return ConstantInt::get(*ctx.context, APInt(numBits, num));
}
optional<Value *> FloatExpr::codegen(CompilationContext &ctx)
{
return ConstantFP::get(*ctx.context, APFloat(decimal));
}
optional<Value *> DoubleExpr::codegen(CompilationContext &ctx)
{
return ConstantFP::get(*ctx.context, APFloat(decimal));
}
optional<Value *> StringExpr::codegen(CompilationContext &ctx)
{
// change text such that any escape characters will be changed
text = escape_to_hex(text);
auto st = llvm::ConstantDataArray::getString(*ctx.context, text);
llvm::GlobalVariable *formatStrVar = new llvm::GlobalVariable(*ctx.mod, st->getType(), true,
llvm::GlobalValue::PrivateLinkage, st, ".str");
llvm::Value* strPtr = ctx.builder->CreateBitCast(formatStrVar, ctx.convertToLLVMType("string"), "strPtr");
return strPtr;
}
optional<Value *> PointerExpr::codegen (CompilationContext &ctx) {
const auto result = ctx.findVarName(name); // find the original declaration of the variable itself
if (const VariableScope* v = get_if<VariableScope>(&result)) {
retType = v->type;
return v->value;
}
else if (const ClassScope* classSc = get_if<ClassScope>(&result)) {
// search through the class scope if we can get the var name
for (int i = 0; i < classSc->variables.size(); i++) {
if (classSc->variables[i].expr->name == name && ctx.runningClass != "") {
retType = classSc->variables[i].expr->return_type();
return classSc->variableValues[i];
}
}
}
throw invalid_argument("No variable found.");
}
optional<Value*> AccessorExpr::codegen (CompilationContext &ctx) {
auto val = *expr->codegen(ctx);
auto rt = expr->return_type();
string t = rt.substr(4, rt.size()-5);
Value* gepS = ctx.builder->CreateGEP(
ctx.convertToLLVMType(t),
val,
{*v[0]->codegen(ctx)}
);
Value* loadVal = ctx.builder->CreateLoad(ctx.convertToLLVMType(t), gepS);
for (int i = 1; i < v.size(); i++) {
t = t.substr(4, t.size()-5);
gepS = ctx.builder->CreateGEP(
ctx.convertToLLVMType(t),
loadVal,
{*v[i]->codegen(ctx)}
);
loadVal = ctx.builder->CreateLoad(ctx.convertToLLVMType(t), gepS);
}
return loadVal;
}
optional<Value*> CharExpr::codegen (CompilationContext &ctx) {
int x = (int)character;
return IntExpr(x, 8).codegen(ctx);
}
// ********************************** Variable uses/decl/assign **********************************
optional<Value *> VarUseExpr::codegen(CompilationContext &ctx)
{
auto result = ctx.findVarName(var);
if (const VariableScope* v = get_if<VariableScope>(&result)) {
retType = v->type;
return ctx.builder->CreateLoad(ctx.convertToLLVMType(v->type), v->value, var.c_str());
}
if (const ClassScope* classSc = get_if<ClassScope>(&result)) {
// search through the class scope if we can get the var name
for (int i = 0; i < classSc->variables.size(); i++) {
if (classSc->variables[i].expr->name == var && ctx.runningClass != "") {// check for public/private later
retType = *classSc->variables[i].expr->typeArg;
return ctx.builder->CreateLoad(ctx.convertToLLVMType(retType), classSc->variableValues[i], var.c_str());
}
}
throw invalid_argument("Unable to find variable in class scope!");
}
throw invalid_argument("Reached invalid subroutine!");
}
optional<Value*> VarDeclareExpr::codegen (CompilationContext &ctx) {
string ty;
llvm::Type* retType;
llvm::Value* val;
// get value & type
if (value) { // value defined
// codegen value
auto res = (*value)->codegen(ctx);
if (!res) throw invalid_argument("Value undefined");
// return type
val = *res;
auto typeExpr = (*value)->return_type();
// allow casting between bool and int
bool isIntBool = (typeArg && *typeArg == "bool" && typeExpr == "int");
if (typeArg && *typeArg != typeExpr && !isIntBool) {
string msg = "Not same type. First type: " + *typeArg + " and second type: " + typeExpr;
throw invalid_argument(msg);
}
if (isIntBool) typeExpr = "bool";
ty = typeExpr;
retType = ctx.convertToLLVMType(typeExpr);
} else if (!typeArg && !value) {
throw invalid_argument("Cannot declare auto with no assigment.");
}
else {
// we have a type defined, but no value; get from default value
ty = *typeArg;
retType = ctx.convertToLLVMType(ty);
auto res = ctx.getDefaultValue(ty);
if (!res) { // if its not one of the primitive types, then it may be a class
return make_unique<ClassVarDecl>(ty, name, move(params))->codegen(ctx);
}
val = *res;
}
AllocaInst *inst = ctx.builder->CreateAlloca(retType, 0, name.c_str());
ctx.createVarName(name, VariableScope { ty, inst, "" });
auto s = ctx.builder->CreateStore(val, inst);
// s->setVolatile(true);
return s;
};
optional<Value*> AssignExpr::codegen (CompilationContext &ctx) {
auto result = ctx.findVarName(varName);
Value* vlu = llvmValue != nullptr ? llvmValue : *(value->codegen(ctx));
Value* toAdd = nullptr;
string toAddRet = "";
if (const VariableScope* v = get_if<VariableScope>(&result)) {
toAdd = v->value;
toAddRet = v->type;
}
if (const ClassScope* classSc = get_if<ClassScope>(&result)) {
// search through the class scope if we can get the var name
for (int i = 0; i < classSc->variables.size(); i++) {
if (classSc->variables[i].expr->name == varName && ctx.runningClass != "") {
toAdd = classSc->variableValues[i];
toAddRet = *classSc->variables[i].expr->typeArg;
break;
}
}
}
if (toAdd == nullptr) throw invalid_argument("Unable to parse assignment.");
// parse var to access specific addresses
Value* gepS;
Value* loadVel;
for (int i = 0; i < v.size(); i++) {
toAdd = ctx.builder->CreateLoad(
ctx.convertToLLVMType(toAddRet),
toAdd
);
toAddRet = toAddRet.substr(4, toAddRet.size()-5);
toAdd = ctx.builder->CreateGEP(
ctx.convertToLLVMType(toAddRet),
toAdd,
{*v[i]->codegen(ctx)}
);
}
return ctx.builder->CreateStore(vlu, toAdd);
throw invalid_argument("Reached invalid subroutine!");
}