-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.cpp
307 lines (260 loc) · 9.44 KB
/
variables.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <algorithm>
#include <iostream>
#include "functions.h"
#include "variables.h"
llvm::Type* astTypeToNativeType(BasicContext* ctx, VariableType type) {
switch (type) {
case Double:
return llvm::Type::getDoubleTy(ctx->context);
case String:
return llvm::Type::getInt8PtrTy(ctx->context);
case Integer:
return llvm::Type::getInt64Ty(ctx->context);
case Void:
return llvm::Type::getVoidTy(ctx->context);
}
}
struct AssignException : public std::exception {
public:
enum Reason {
NotString,
NoEquals,
};
AssignException(Reason reason) : _reason(reason) {}
const char* what() const noexcept override {
switch (_reason) {
case NotString:
return "First token wasn't a string";
case NoEquals:
return "Expected equals sign between variable and value in assignment";
}
}
Reason reason() const {
return _reason;
}
private:
Reason _reason;
};
// CreateEntryBlockAlloca - creates an alloca instruction in the entry block
// of the function. Used for mutable vars etc
llvm::AllocaInst* CreateEntryBlockAlloca(BasicContext* ctx,
const std::string& name,
llvm::Type* type) {
auto curFunction = ctx->getCurrentFunction();
auto& entryBlock = curFunction->getEntryBlock();
llvm::IRBuilder<> tmp(&entryBlock, entryBlock.begin());
return tmp.CreateAlloca(type, 0, name.c_str());
}
llvm::Value* VariableExprAST::codegenLookup() {
auto it = ctx()->namedVariables.find(_name);
if (it == ctx()->namedVariables.end()) {
std::cerr << "Could not find named value for " << _name << std::endl;
return nullptr;
}
return it->second->lookup(_dimensions);
}
llvm::Value* VariableExprAST::codegen() {
auto lookup = codegenLookup();
if (!lookup)
return nullptr;
return ctx()->builder.CreateLoad(lookup, _name);
}
std::unique_ptr<ExprAST> VariableExprAST::parse(const Token& tok, BasicContext* ctx) {
std::string variableName(ctx->makeVariableName(tok));
std::vector<std::unique_ptr<ExprAST>> dimensions;
auto nextTok = ctx->lexer.lex();
if (nextTok.tag == Token::LParens) {
dimensions = parseParensList<std::unique_ptr<ExprAST>>(
ctx, [ctx](Token tok) { return ExprAST::parse(tok, ctx); });
} else {
ctx->lexer.putBack(nextTok);
}
return std::make_unique<VariableExprAST>(tok, ctx, variableName, std::move(dimensions));
}
llvm::Value* VariableDeclartionAST::codegen() {
if (_alloca)
return _alloca;
if (ctx()->namedVariables.find(name()) != ctx()->namedVariables.end()) {
std::cerr << "Duplicate variable definition for " << name();
return nullptr;
}
llvm::Type* type = nativeType();
if (isGlobal()) {
auto globalPtr = ctx()->module->getOrInsertGlobal(name(), type);
auto global = ctx()->module->getGlobalVariable(name());
if (!global->hasInitializer()) {
llvm::Constant* initV;
if (type->isDoubleTy()) {
initV = ctx()->makeLiteralDouble(0.0);
} else if (type->isIntegerTy()) {
initV = ctx()->makeLiteralInteger(0);
} else if (type == ctx()->builder.getInt8PtrTy()) {
initV = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(ctx()->context));
} else if (!type->isArrayTy()) {
std::cerr << "Cannot find initial value for variable " << name() << std::endl;
return nullptr;
}
if (!dimensions().empty()) {
initV = llvm::ConstantAggregateZero::get(type);
}
global->setInitializer(initV);
global->setLinkage(llvm::GlobalValue::CommonLinkage);
}
ctx()->namedVariables[name()] = this;
_alloca = globalPtr;
return globalPtr;
} else {
ctx()->namedVariables[name()] = this;
_alloca = CreateEntryBlockAlloca(ctx(), name(), type);
return _alloca;
}
}
llvm::Value* VariableDeclartionAST::lookup() const {
return _alloca;
}
llvm::Value* VariableDeclartionAST::lookup(
const std::vector<std::unique_ptr<ExprAST>>& dimensions) const {
auto& builder = ctx()->builder;
if (dimensions.empty()) {
return lookup();
} else {
std::vector<llvm::Value*> generatedDimensions = {ctx()->makeLiteralInteger(0)};
for (const auto& dimension : dimensions) {
generatedDimensions.push_back(dimension->codegen());
}
return builder.CreateInBoundsGEP(nativeType(), lookup(), generatedDimensions);
}
}
std::unique_ptr<ExprAST> DimAST::parse(const Token& tok, BasicContext* ctx) {
Token nameTok = ctx->lexer.lex();
if (nameTok.tag != Token::String && nameTok.tag != Token::Variable) {
throw AssignException(AssignException::NotString);
}
auto nextTok = ctx->lexer.lex();
std::vector<int> dimensions;
VariableType type = Integer;
if (nextTok.tag == Token::LParens) {
dimensions = parseParensList<int>(ctx, [](Token tok) {
if (tok.tag != Token::Integer) {
throw ParseException(tok, "Expected integer list for array dimensions");
}
return mpark::get<int64_t>(*tok.value);
});
// Consume the RParens
nextTok = ctx->lexer.lex();
}
if (nextTok.tag == Token::As) {
nextTok = ctx->lexer.lex();
switch (nextTok.tag) {
case Token::IntegerType:
type = Integer;
break;
case Token::StringType:
type = String;
break;
case Token::DoubleType:
type = Double;
break;
default:
throw ParseException(nextTok, "Invalid token while parsing variable type");
}
} else {
ctx->lexer.putBack(nextTok);
}
bool global = (ctx->currentFunction == nullptr);
auto ret =
std::make_unique<DimAST>(tok, ctx, nameTok.strValue, type, std::move(dimensions), global);
ctx->namedVariables[nameTok.strValue] = ret.get();
return ret;
}
llvm::Type* DimAST::nativeType() const {
llvm::Type* type = astTypeToNativeType(ctx(), _type);
for (auto dimension : _dimensions) {
type = llvm::ArrayType::get(type, dimension);
}
return type;
}
llvm::Value* DimAST::codegen() {
if (!VariableDeclartionAST::codegen()) {
return nullptr;
}
return lookup();
}
const std::string& DimAST::name() const {
return _name;
}
std::unique_ptr<ExprAST> LetAST::parse(const Token& tok, BasicContext* ctx, bool maybeGlobal) {
Token nameTok = tok;
if (tok.tag == Token::Let) {
nameTok = ctx->lexer.lex();
}
if (nameTok.tag != Token::String && nameTok.tag != Token::Variable) {
throw AssignException(AssignException::NotString);
}
std::string name;
VariableType type = Void;
std::unique_ptr<VariableExprAST> variableExpr;
if (ctx->externFunctions.find(nameTok.strValue) == ctx->externFunctions.end()) {
variableExpr.reset(
static_cast<VariableExprAST*>(VariableExprAST::parse(nameTok, ctx).release()));
name = ctx->makeVariableName(variableExpr->token(), &type);
if (type == Void) {
type = Integer;
}
} else {
name = nameTok.strValue;
}
if (ctx->lexer.lex().tag != Token::Eq) {
throw AssignException(AssignException::NoEquals);
}
auto value = ExprAST::parse(ctx->lexer.lex(), ctx);
bool global = (ctx->currentFunction == nullptr && maybeGlobal);
bool isVariable = static_cast<bool>(variableExpr);
auto ret = std::make_unique<LetAST>(
tok, ctx, name, type, std::move(variableExpr), std::move(value), global);
if (isVariable)
ctx->namedVariables[name] = ret.get();
return ret;
}
llvm::Type* LetAST::nativeType() const {
return astTypeToNativeType(ctx(), _type);
}
llvm::Value* LetAST::codegen() {
auto& builder = ctx()->builder;
if (_name == ctx()->getCurrentFunction()->getName()) {
if (!_value) {
std::cerr << "Missing value in assignment used as return statement" << std::endl;
return nullptr;
}
return builder.CreateRet(_value->codegen());
}
llvm::Value* alloca;
if (ctx()->isNamedVariable(_name)) {
alloca = _variableExpr->codegenLookup();
} else {
alloca = VariableDeclartionAST::codegen();
}
auto type = alloca->getType();
if (type->isPointerTy()) {
auto ptr = static_cast<llvm::PointerType*>(type);
type = ptr->getPointerElementType();
}
auto value = _value->codegen();
if (!value) {
std::cerr << "Error generating value for assignment" << std::endl;
return nullptr;
}
if (type->isDoubleTy() && value->getType() != ctx()->builder.getDoubleTy()) {
value = ctx()->builder.CreateIntCast(value, ctx()->builder.getDoubleTy(), true);
} else if (type->isIntegerTy() && value->getType() != ctx()->builder.getInt64Ty()) {
value = ctx()->builder.CreateFPCast(value, ctx()->builder.getInt64Ty());
}
if (!value) {
std::cerr << "Error casting value for assignment" << std::endl;
return nullptr;
}
return builder.CreateStore(value, alloca);
}
llvm::Type* ArgumentAST::nativeType() const {
return astTypeToNativeType(ctx(), type);
}