-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.cpp
468 lines (418 loc) · 15.5 KB
/
AST.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/raw_ostream.h"
#include "AST.h"
namespace AST {
Type string_to_type(std::string type) {
if (type == "integer") {
return INT;
}
if (type == "boolean") {
return BOOL;
}
return NA;
}
std::string type_to_string(Type type) {
switch (type) {
case INT:
return "integer";
case BOOL:
return "boolean";
case UNIT:
return "unit";
default:
return "N/A";
}
}
std::string op_to_string(BinOp op) {
switch (op) {
case PLUS:
return "+";
case MINUS:
return "-";
case TIMES:
return "*";
case DIV:
return "div";
case AND:
return "and";
case OR:
return "or";
case LT:
return "<";
case GT:
return ">";
case LEQ:
return "<=";
case GEQ:
return ">=";
case EQ:
return "=";
case NEQ:
return "<>";
}
}
ASTNode::ASTNode(int line_no, int col_no) : line_no(line_no), col_no(col_no) {};
VarDeclNode::VarDeclNode(IdentifierName variable, Type type, int line_no, int col_no) :
ASTNode(line_no, col_no), variable(variable), type(type) {};
Type VarDeclNode::typeCheck(TypeEnv& typeEnv, bool& error) {
if (typeEnv.find(variable) != typeEnv.end()) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " variable " << variable << " already declared" << std::endl;
error = true;
}
typeEnv[variable] = type;
return NA;
}
llvm::Value *VarDeclNode::codegen(Compiler::Components &comp) {
if (type == INT) {
comp.symbols[variable] = comp.builder.CreateAlloca(llvm::IntegerType::getInt32Ty(comp.llvmCtx), 0,
variable.c_str());
} else if (type == BOOL) {
comp.symbols[variable] = comp.builder.CreateAlloca(llvm::IntegerType::getInt1Ty(comp.llvmCtx), 0,
variable.c_str());
}
return nullptr;
}
VarBlockNode::VarBlockNode(std::vector<VarDeclNode*>& varDecls, int line_no, int col_no) :
ASTNode(line_no, col_no), varDecls(varDecls) {};
Type VarBlockNode::typeCheck(TypeEnv& typeEnv, bool &error) {
for (auto decl : varDecls) {
decl->typeCheck(typeEnv, error);
}
return NA;
}
llvm::Value *VarBlockNode::codegen(Compiler::Components &comp) {
for (auto decl : varDecls) {
decl->codegen(comp);
}
return nullptr;
}
StatementBlockNode::StatementBlockNode(std::vector<StatementNode*>& statements, int line_no, int col_no) :
StatementNode(line_no, col_no), statements(statements) {};
Type StatementBlockNode::typeCheck(TypeEnv& typeEnv, bool &error) {
for (auto statement : statements) {
if (statement->typeCheck(typeEnv, error) != UNIT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " only statements can be sequenced" << std::endl;
error = true;
}
}
return UNIT;
}
llvm::Value *StatementBlockNode::codegen(Compiler::Components &comp) {
for (auto statement : statements) {
statement->codegen(comp);
}
return nullptr;
}
ProgNode::ProgNode(VarBlockNode *varBlock, StatementNode *statBlock, int line_no, int col_no) :
ASTNode(line_no, col_no), varBlock(varBlock), statBlock(statBlock) {};
Type ProgNode::typeCheck(TypeEnv& typeEnv, bool &error) {
varBlock->typeCheck(typeEnv, error);
statBlock->typeCheck(typeEnv, error);
return NA;
}
llvm::Value *ProgNode::codegen(Compiler::Components &comp) {
comp.builder.SetInsertPoint(llvm::BasicBlock::Create(comp.llvmCtx, "entry", comp.fun));
varBlock->codegen(comp);
statBlock->codegen(comp);
comp.builder.CreateRet(llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(comp.llvmCtx), 0));
return nullptr;
}
AssignmentNode::AssignmentNode(IdentifierName variable, ExpressionNode *expressionNode, int line_no, int col_no) :
StatementNode(line_no, col_no), variable(variable), expressionNode(expressionNode) {};
Type AssignmentNode::typeCheck(TypeEnv &typeEnv, bool &error) {
if (typeEnv.find(variable) == typeEnv.end()) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " undeclared variable " << variable << std::endl;
error = true;
}
Type var_type = typeEnv[variable];
Type expr_type = expressionNode->typeCheck(typeEnv, error);
if (var_type != expr_type) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " cannot assign " << type_to_string(expr_type)
<< " to a variable of type " << type_to_string(var_type) << std::endl;
error = true;
}
return UNIT;
}
llvm::Value *AssignmentNode::codegen(Compiler::Components &comp) {
llvm::Value *rhs = expressionNode->codegen(comp);
llvm::Value *var = comp.symbols[variable];
comp.builder.CreateStore(rhs, var);
return nullptr;
}
IfNode::IfNode(ExpressionNode *condNode, StatementNode *thenNode, int line_no, int col_no) :
StatementNode(line_no, col_no), condition(condNode), thenBranch(thenNode) {}
Type IfNode::typeCheck(TypeEnv &typeEnv, bool &error) {
if (condition->typeCheck(typeEnv, error) != BOOL) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " condition must be a boolean expression" << std::endl;
error = true;
}
if (thenBranch->typeCheck(typeEnv, error) != UNIT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " body of a conditional must be a statement" << std::endl;
error = true;
}
return UNIT;
}
llvm::Value *IfNode::codegen(Compiler::Components &comp) {
llvm::Value *condV = condition->codegen(comp);
llvm::BasicBlock *cont = llvm::BasicBlock::Create(comp.llvmCtx, "if.cont", comp.fun);
llvm::BasicBlock *ifBody = llvm::BasicBlock::Create(comp.llvmCtx, "if.then", comp.fun);
condV = comp.builder.CreateIsNotNull(condV);
comp.builder.CreateCondBr(condV, ifBody, cont);
comp.builder.SetInsertPoint(ifBody);
thenBranch->codegen(comp);
if (comp.builder.GetInsertBlock() != nullptr){
comp.builder.CreateBr(cont);
}
comp.builder.SetInsertPoint(cont);
return nullptr;
}
IfElseNode::IfElseNode(ExpressionNode *condNode, StatementNode *thenNode, StatementNode *elseNode, int line_no, int col_no) :
StatementNode(line_no, col_no), condition(condNode), thenBranch(thenNode), elseBranch(elseNode) {};
Type IfElseNode::typeCheck(TypeEnv &typeEnv, bool &error) {
if (condition->typeCheck(typeEnv, error) != BOOL) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " condition must be a boolean expression" << std::endl;
error = true;
}
if (thenBranch->typeCheck(typeEnv, error) != UNIT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " then branch of a conditional must be a statement" << std::endl;
error = true;
}
if (elseBranch->typeCheck(typeEnv, error) != UNIT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " else branch of a conditional must be a statement" << std::endl;
error = true;
}
return UNIT;
}
llvm::Value *IfElseNode::codegen(Compiler::Components &comp) {
llvm::Value *condV = condition->codegen(comp);
llvm::BasicBlock *cont = llvm::BasicBlock::Create(comp.llvmCtx, "if.cont", comp.fun);
llvm::BasicBlock *ifThen = llvm::BasicBlock::Create(comp.llvmCtx, "if.then", comp.fun);
llvm::BasicBlock *ifElse = llvm::BasicBlock::Create(comp.llvmCtx, "if.else", comp.fun);
condV = comp.builder.CreateIsNotNull(condV);
comp.builder.CreateCondBr(condV, ifThen, ifElse);
comp.builder.SetInsertPoint(ifThen);
thenBranch->codegen(comp);
if (comp.builder.GetInsertBlock() != nullptr){
comp.builder.CreateBr(cont);
}
comp.builder.SetInsertPoint(ifElse);
elseBranch->codegen(comp);
if (comp.builder.GetInsertBlock() != nullptr){
comp.builder.CreateBr(cont);
}
comp.builder.SetInsertPoint(cont);
return nullptr;
}
WhileNode::WhileNode(ExpressionNode *condition, StatementNode *body, int line_no, int col_no):
StatementNode(line_no, col_no), condition(condition), body(body) {}
Type WhileNode::typeCheck(TypeEnv& typeEnv, bool &error) {
if (condition->typeCheck(typeEnv, error) != BOOL) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " loop condition must be a boolean" << std::endl;
error = true;
}
if (body->typeCheck(typeEnv, error) != UNIT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " loop body must be a statement" << std::endl;
error = true;
}
return UNIT;
}
llvm::Value *WhileNode::codegen(Compiler::Components &comp) {
llvm::BasicBlock *whileCond = llvm::BasicBlock::Create(comp.llvmCtx, "while.cond", comp.fun);
comp.builder.CreateBr(whileCond);
comp.builder.SetInsertPoint(whileCond);
llvm::Value *condV = condition->codegen(comp);
llvm::BasicBlock *cont = llvm::BasicBlock::Create(comp.llvmCtx, "while.cont", comp.fun);
llvm::BasicBlock *whileBody = llvm::BasicBlock::Create(comp.llvmCtx, "while.body", comp.fun);
condV = comp.builder.CreateIsNotNull(condV);
comp.builder.CreateCondBr(condV, whileBody, cont);
comp.builder.SetInsertPoint(whileBody);
body->codegen(comp);
if (comp.builder.GetInsertBlock() != nullptr){
comp.builder.CreateBr(whileCond);
}
comp.builder.SetInsertPoint(cont);
return nullptr;
}
PrintNode::PrintNode(ExpressionNode *to_print, int line_no, int col_no) :
StatementNode(line_no, col_no), printed_exp(to_print) {};
Type PrintNode::typeCheck(TypeEnv &typeEnv, bool &error) {
if (printed_exp->typeCheck(typeEnv, error) != INT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " only integer-valued expressions can be printed" << std::endl;
error = true;
}
return UNIT;
}
llvm::Value *PrintNode::codegen(Compiler::Components &comp) {
auto v = printed_exp->codegen(comp);
std::vector<llvm::Value*> print_args;
llvm::Value *format_str = comp.builder.CreateGlobalStringPtr("%d\n");
print_args.push_back(format_str);
print_args.push_back(v);
return comp.builder.CreateCall(comp.llvmModule->getFunction("printf"), print_args);
}
BinopExpressionNode::BinopExpressionNode(ExpressionNode *left, ExpressionNode *right, BinOp op, int line_no, int col_no):
ExpressionNode(line_no, col_no), left_exp(left), right_exp(right), op(op) {}
Type BinopExpressionNode::typeCheck(TypeEnv &typeEnv, bool &error) {
Type typeLeft = left_exp->typeCheck(typeEnv, error);
Type typeRight = right_exp->typeCheck(typeEnv, error);
if (typeLeft != typeRight) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operand types do not match ("
<< type_to_string(typeLeft) << " and " << type_to_string(typeRight)
<< ")" << std::endl;
error = true;
}
switch (op)
{
case PLUS:
case MINUS:
case TIMES:
case DIV:
if (typeLeft != INT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operands of operator " << op_to_string(op)
<< " must be of type integer" << std::endl;
error = true;
}
return INT;
case GT:
case LT:
case EQ:
case NEQ:
case GEQ:
case LEQ:
if (typeLeft != INT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operands of operator " << op_to_string(op)
<< " must be of type integer" << std::endl;
error = true;
}
return BOOL;
case AND:
case OR:
if (typeLeft != BOOL) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operands of operator " << op_to_string(op)
<< " must be of type boolean" << std::endl;
error = true;
}
return BOOL;
default:
return NA;
}
}
llvm::Value *BinopExpressionNode::codegen(Compiler::Components &comp) {
llvm::Value *left = left_exp->codegen(comp);
llvm::Value *right = right_exp->codegen(comp);
switch (op) {
case PLUS:
return comp.builder.CreateAdd(left, right, "addtmp");
case MINUS:
return comp.builder.CreateSub(left, right, "subtmp");
case TIMES:
return comp.builder.CreateMul(left, right, "multmp");
case DIV:
return comp.builder.CreateSDiv(left, right, "divtmp");
case AND:
return comp.builder.CreateAnd(left, right, "addtmp");
case OR:
return comp.builder.CreateOr(left, right, "ortmp");
case EQ:
return comp.builder.CreateICmpEQ(left, right, "eqtmp");
case NEQ:
return comp.builder.CreateICmpNE(left, right, "neqtmp");
case GT:
return comp.builder.CreateICmpSGT(left, right, "gttmp");
case GEQ:
return comp.builder.CreateICmpSGE(left, right, "geqtmp");
case LT:
return comp.builder.CreateICmpSLT(left, right, "ltttmp");
case LEQ:
return comp.builder.CreateICmpSLE(left, right, "leqtmp");
}
return nullptr;
}
UnopExpressionNode::UnopExpressionNode(ExpressionNode *exp, UnOp op, int line_no, int col_no):
ExpressionNode(line_no, col_no), exp(exp), op(op) {}
Type UnopExpressionNode::typeCheck(TypeEnv &typeEnv, bool &error) {
Type type = exp->typeCheck(typeEnv, error);
switch (op)
{
case NEG:
if (type != INT) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operand of unary operator - must be an integer expression" << std::endl;
error = true;
}
return INT;
case NOT:
if (type != BOOL) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " operand of unary operator - must be a boolean expression" << std::endl;
error = true;
}
return BOOL;
default:
return NA;
}
}
llvm::Value *UnopExpressionNode::codegen(Compiler::Components &comp) {
// FORGOT ABOUT TODO!!!!
llvm::Value *arg = exp->codegen(comp);
switch (op) {
case NEG:
return comp.builder.CreateNeg(arg, "negtmp");
case NOT:
return comp.builder.CreateNot(arg, "nottmp");
}
return nullptr;
}
IntConstantNode::IntConstantNode(int number, int line_no, int col_no) :
ExpressionNode(line_no, col_no), underlying_int(number) {};
Type IntConstantNode::typeCheck(TypeEnv &typeEnv, bool &error) {
return INT;
}
llvm::Value *IntConstantNode::codegen(Compiler::Components &comp) {
return llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(comp.llvmCtx),
underlying_int, true);
}
BoolNode::BoolNode(bool truthy, int line_no, int col_no) :
ExpressionNode(line_no, col_no), underlying_bool(truthy) {};
Type BoolNode::typeCheck(TypeEnv &typeEnv, bool &error) {
return BOOL;
}
llvm::Value *BoolNode::codegen(Compiler::Components &comp) {
return llvm::ConstantInt::get(llvm::IntegerType::getInt1Ty(comp.llvmCtx),
underlying_bool);
}
IdentifierNode::IdentifierNode(IdentifierName id_name, int line_no, int col_no) :
ExpressionNode(line_no, col_no), id_name(id_name) {};
Type IdentifierNode::typeCheck(TypeEnv &typeEnv, bool &error) {
if (typeEnv.find(id_name) == typeEnv.end()) {
std::cerr << "Error on line " << line_no << ", column " << col_no << ":" << std::endl
<< " undeclared variable " << id_name << std::endl;
error = true;
}
return typeEnv[id_name];
}
llvm::Value *IdentifierNode::codegen(Compiler::Components &comp) {
return comp.builder.CreateLoad(comp.symbols[id_name], id_name.c_str());
}
}