-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAST_node.cpp
More file actions
488 lines (457 loc) · 18.4 KB
/
AST_node.cpp
File metadata and controls
488 lines (457 loc) · 18.4 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "AST_node.h"
#include "C_syntax.hpp"
extern int yyparse();
extern BlockExprNode* root;
bool error = false;
static Value* cast(Value* value, Type* type, GenContext& context);
Function* printfFunction(GenContext& context) {
vector<Type*> printfArgs;
printfArgs.push_back(Type::getInt8PtrTy(getGlobalContext()));
FunctionType* printfType = FunctionType::get(Type::getInt32Ty(getGlobalContext()), printfArgs, true);
Function *printfFunc = Function::Create(printfType, Function::ExternalLinkage, Twine("printf"), context.module);
printfFunc->setCallingConv(CallingConv::C);
return printfFunc;
}
Function* strlenFunction(GenContext& context) {
vector<Type*> strlenArgs;
strlenArgs.push_back(Type::getInt8PtrTy(getGlobalContext()));
FunctionType* strlenType = FunctionType::get(Type::getInt64Ty(getGlobalContext()), strlenArgs, false);
Function *strlenFunc = Function::Create(strlenType, Function::ExternalLinkage, Twine("strlen"), context.module);
strlenFunc->setCallingConv(CallingConv::C);
return strlenFunc;
}
Function* isdigitFunction(GenContext& context) {
vector<Type*> isdigitArgs;
isdigitArgs.push_back(Type::getInt8Ty(getGlobalContext()));
FunctionType* isdigitType = FunctionType::get(Type::getInt1Ty(getGlobalContext()), isdigitArgs, false);
Function *isdigitFunc = Function::Create(isdigitType, Function::ExternalLinkage, Twine("isdigit"), context.module);
isdigitFunc->setCallingConv(CallingConv::C);
return isdigitFunc;
}
Function* atoiFunction(GenContext& context) {
vector<Type*> atoiArgs;
atoiArgs.push_back(Type::getInt8PtrTy(getGlobalContext()));
FunctionType* atoiType =FunctionType::get(Type::getInt64Ty(getGlobalContext()), atoiArgs, false);
Function *atoiFunc = Function::Create(atoiType, Function::ExternalLinkage, Twine("atoi"), context.module);
atoiFunc->setCallingConv(CallingConv::C);
return atoiFunc;
}
void linkExternalFunctions(GenContext& context) {
printfFunction(context);
strlenFunction(context);
isdigitFunction(context);
atoiFunction(context);
}
void GenContext::CodeGen(BlockExprNode& root) {
vector<Type*> arg_types;
FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(arg_types), false);
SmallVector<AttributeSet, 4> attrs;
AttrBuilder builder;
builder.addAttribute(Attribute::NoUnwind);
attrs.push_back(AttributeSet::get(getGlobalContext(), ~0U, builder));
AttributeSet funcFunc = AttributeSet::get(getGlobalContext(), attrs);
root.CodeGen(*this);
PassManager<Module> pm;
raw_string_ostream *out = new raw_string_ostream(code);
pm.addPass(PrintModulePass(*out));
pm.run(*module);
}
void GenContext::OutputCode(ostream &out) {
out << code;
}
GenericValue GenContext::run() {
ExecutionEngine *ee = EngineBuilder(unique_ptr<Module>(module)).create();
vector<GenericValue> noargs;
GenericValue v;
ee->finalizeObject();
mainFunction = module->getFunction("main");
v = ee->runFunction(mainFunction, noargs);
return v;
}
static Type *typeOf(VariableExprNode *var) {
Type *type;
if (var->name == "int")
type = Type::getInt64Ty(getGlobalContext());
else if (var->name == "char")
type = Type::getInt8Ty(getGlobalContext());
else if (var->name == "double")
type = Type::getDoubleTy(getGlobalContext());
else if (var->name == "void")
type = Type::getVoidTy(getGlobalContext());
return type;
}
Value* VariableExprNode::CodeGen(GenContext& context) {
Value* val;
if (!context.funcDeclaring || context.locals().find(name) == context.locals().end()) {
if (context.globals().find(name) == context.globals().end()) {
cerr << "Undeclared Variable " << name << endl;
return NULL;
} else
val = context.globals()[name];
} else
val = context.locals()[name];
if (((AllocaInst*)val)->getAllocatedType()->isArrayTy()) {
ConstantInt* constInt = ConstantInt::get(getGlobalContext(), APInt(32, StringRef("0"), 10));
vector<Value*> args;
args.push_back(constInt);
args.push_back(constInt);
Type* type;
type = ((AllocaInst*)val)->getAllocatedType();
val = GetElementPtrInst::Create(type, val, args, "", context.context());
return val;
} else
return new LoadInst(val, "", false, context.context());
}
Value* CharExprNode::CodeGen(GenContext& context) {
return ConstantInt::get(Type::getInt8Ty(getGlobalContext()), val, true);
}
Value* IntExprNode::CodeGen(GenContext& context) {
return ConstantInt::get(Type::getInt64Ty(getGlobalContext()), val, true);
}
Value* DoubleExprNode::CodeGen(GenContext& context) {
return ConstantFP::get(Type::getDoubleTy(getGlobalContext()), val);
}
Value* BlockExprNode::CodeGen(GenContext& context) {
Value *returnValue = NULL;
for (auto it = statements->begin(); it != statements->end(); it++)
returnValue = (*it)->CodeGen(context);
return returnValue;
}
Value* OperatorExprNode::CodeGen(GenContext& context) {
Instruction::BinaryOps instr;
ICmpInst::Predicate pred;
bool floatOp = false;
bool mathOP = false;
Value* leftVal = left->CodeGen(context);
Value* rightVal = right->CodeGen(context);
if (leftVal->getType()->isDoubleTy() || rightVal->getType()->isDoubleTy()) {
leftVal = cast(leftVal, Type::getDoubleTy(getGlobalContext()), context);
rightVal = cast(rightVal, Type::getDoubleTy(getGlobalContext()), context);
floatOp = true;
} else if (leftVal->getType() == rightVal->getType()) {
} else {
leftVal = cast(leftVal, Type::getInt64Ty(getGlobalContext()), context);
rightVal = cast(rightVal, Type::getInt64Ty(getGlobalContext()), context);
}
if (!floatOp) {
switch (op) {
case EQ:
pred = ICmpInst::ICMP_EQ;
break;
case NE:
pred = ICmpInst::ICMP_NE;
break;
case GR:
pred = ICmpInst::ICMP_SGT;
break;
case LW:
pred = ICmpInst::ICMP_SLT;
break;
case GE:
pred = ICmpInst::ICMP_SGE;
break;
case LE:
pred = ICmpInst::ICMP_SLE;
break;
case ADD:
case SADD:
instr = Instruction::Add;
mathOP=true;
break;
case SUB:
case SSUB:
instr = Instruction::Sub;
mathOP=true;
break;
case MUL:
case SMUL:
instr = Instruction::Mul;
mathOP=true;
break;
case DIV:
case SDIV:
instr = Instruction::SDiv;
mathOP=true;
break;
case OR:
instr = Instruction::Or;
mathOP=true;
break;
case AND:
instr = Instruction::And;
mathOP=true;
break;
}
} else {
switch (op) {
case EQ:
pred = ICmpInst::FCMP_OEQ;
break;
case NE:
pred = ICmpInst::FCMP_ONE;
break;
case GR:
pred = ICmpInst::FCMP_OGT;
break;
case LW:
pred = ICmpInst::FCMP_OLT;
break;
case GE:
pred = ICmpInst::FCMP_OGE;
break;
case LE:
pred = ICmpInst::FCMP_OLE;
break;
case ADD:
case SADD:
instr = Instruction::FAdd;
mathOP=true;
break;
case SUB:
case SSUB:
instr = Instruction::FSub;
mathOP=true;
break;
case MUL:
case SMUL:
instr = Instruction::FMul;
mathOP=true;
break;
case DIV:
case SDIV:
instr = Instruction::FDiv;
mathOP=true;
break;
}
}
if (mathOP)
return BinaryOperator::Create(instr, leftVal, rightVal, "", context.context());
else
return new ICmpInst(*context.context(), pred, leftVal, rightVal, "");
}
Value* AssignExprNode::CodeGen(GenContext& context) {
Value* rightVal;
Value* leftVal;
if (!context.funcDeclaring || context.locals().find(left->name) == context.locals().end()) {
if (context.globals().find(left->name) == context.globals().end())
return NULL;
else
leftVal = context.globals()[left->name];
}
else
leftVal = context.locals()[left->name];
rightVal = right->CodeGen(context);
if (leftVal->getType() == Type::getInt64PtrTy(getGlobalContext()))
rightVal = cast(rightVal, Type::getInt64Ty(getGlobalContext()), context);
else if (leftVal->getType() == Type::getDoublePtrTy(getGlobalContext()))
rightVal = cast(rightVal, Type::getDoubleTy(getGlobalContext()), context);
else if (leftVal->getType() == Type::getInt8PtrTy(getGlobalContext()))
rightVal = cast(rightVal, Type::getInt8Ty(getGlobalContext()), context);
return new StoreInst(rightVal, leftVal, false, context.context());
}
Value* FuncExprNode::CodeGen(GenContext& context) {
// Get functor
Function *function = context.module->getFunction(functor->name.c_str());
if (function == NULL)
cerr << "No such function " << functor->name << endl;
vector<Value*> argsRef;
for (auto it = args->begin(); it != args->end(); it++)
argsRef.push_back((*it)->CodeGen(context));
CallInst *call = CallInst::Create(function, makeArrayRef(argsRef), "", context.context());
return call;
}
Value* CastExprNode::CodeGen(GenContext& context) {
Value* value = expr->CodeGen(context);
Type* castType = typeOf(type);
value = cast(value, castType, context);
return value;
}
Value* IndexExprNode::CodeGen(GenContext& context) {
Value* array = name->CodeGen(context);
Value* num = cast(expr->CodeGen(context), Type::getInt64Ty(getGlobalContext()), context);
num = new TruncInst(num, Type::getInt32Ty(getGlobalContext()), "", context.context());
Type* arrayType = cast<PointerType>(array->getType()->getScalarType())->getElementType();
Instruction* instr;
Value* retInst;
instr = GetElementPtrInst::Create(arrayType, array, num, "", context.context());
// whether read or write
if (assign == NULL)
retInst = new LoadInst(instr, "", false, context.context());
else
retInst = new StoreInst(assign->CodeGen(context), instr, false, context.context());
return retInst;
}
Value* ExprStatementNode::CodeGen(GenContext& context) {
return expr->CodeGen(context);
}
Value* VarDecStatementNode::CodeGen(GenContext& context) {
Value* newVar;
newVar = new AllocaInst(typeOf(type), name->name.c_str(), context.context());
context.locals()[name->name] = newVar;
if (expr != NULL) {
AssignExprNode assign(name, expr);
assign.CodeGen(context);
}
return newVar;
}
Value* ArrayDecStatementNode::CodeGen(GenContext& context) {
ArrayType* arrayType = ArrayType::get(typeOf(type), size);
AllocaInst *alloc = new AllocaInst(arrayType, name->name.c_str(), context.context());
context.locals()[name->name] = alloc;
if (init->size() != 0) {
for (auto it = init->begin(); it != init->end(); ++it) {
ExprNode* num = new IntExprNode(it - init->begin());
IndexExprNode a(name, num, (*it));
a.CodeGen(context);
delete num;
}
}
return alloc;
}
Value* ReturnStatementNode::CodeGen(GenContext& context) {
return expr->CodeGen(context);
}
Value* FuncDecStatementNode::CodeGen(GenContext& context) {
// Function type
vector<Type*> argTypeRef;
for (auto it = args->begin(); it != args->end(); it++)
argTypeRef.push_back(typeOf((*it)->type));
FunctionType *funcType = FunctionType::get(typeOf(type), ArrayRef<Type*>(argTypeRef), false);
Function *function = Function::Create(funcType, GlobalValue::ExternalLinkage, name->name.c_str(), context.module);
function->setCallingConv(CallingConv::C);
SmallVector<AttributeSet, 4> Attrs;
AttrBuilder Builder;
Builder.addAttribute(Attribute::NoUnwind);
Attrs.push_back(AttributeSet::get(getGlobalContext(), ~0U, Builder));
AttributeSet funcFuncAttrSet = AttributeSet::get(getGlobalContext(), Attrs);
function->setAttributes(funcFuncAttrSet);
context.currentFunction(function);
context.funcDeclaring = true;
// Start function
BasicBlock *funcBlock = BasicBlock::Create(getGlobalContext(), "", function, 0);
context.push(funcBlock, false);
Function::arg_iterator argsValues = function->arg_begin();
for (auto it = args->begin(); it != args->end(); it++, argsValues++) {
(*it)->CodeGen(context);
Value *argumentValue = &(*argsValues);
argumentValue->setName((*it)->name->name.c_str());
StoreInst *inst = new StoreInst(argumentValue, context.locals()[(*it)->name->name], false, funcBlock);
}
// Get return value
Value* returnValue = block->CodeGen(context);
context.funcDeclaring = false;
// Return
BasicBlock *returnBlock = BasicBlock::Create(getGlobalContext(), "", function, 0);
BranchInst::Create(returnBlock, context.context());
ReturnInst::Create(getGlobalContext(), returnValue, returnBlock);
context.pop();
return function;
}
Value* ExternFuncDecStatementNode::CodeGen(GenContext& context) {
vector<Type*> argTypes;
FunctionType *ftype;
Function *function;
for (auto it = args->begin(); it != args->end(); it++)
argTypes.push_back(typeOf((*it)->type));
ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false);
function = Function::Create(ftype, GlobalValue::ExternalLinkage, name->name.c_str(), context.module);
return function;
}
Value* IfStatementNode::CodeGen(GenContext& context) {
BasicBlock* ifTrue = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* ifFalse = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* ifEnd = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BranchInst::Create(ifTrue, ifFalse, condExpr->CodeGen(context), context.context());
// Entering IF
context.push(ifTrue);
trueBlock->CodeGen(context);
// JMP to END
BranchInst::Create(ifEnd, context.context());
context.pop();
// Entering ELSE
context.push(ifFalse);
falseBlock->CodeGen(context);
// JMP to END
BranchInst::Create(ifEnd, context.context());
context.pop();
// Return END
context.ret(ifEnd);
return ifEnd;
}
Value* ForStatementNode::CodeGen(GenContext& context) {
// Initialize
initExpr->CodeGen(context);
BasicBlock* forIter = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* forEnd = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* forCheck = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
// Check condition satisfaction
BranchInst::Create(forCheck, context.context());
context.push(forCheck);
// Whether break the loop
BranchInst::Create(forIter, forEnd, condExpr->CodeGen(context), forCheck);
context.pop();
// Entering loop block
context.push(forIter);
block->CodeGen(context);
// Iteration
loopExpr->CodeGen(context);
// Jump back to condition checking
BranchInst::Create(forCheck, context.context());
context.pop();
// Return END
context.ret(forEnd);
return forEnd;
}
Value* WhileStatementNode::CodeGen(GenContext& context) {
BasicBlock* whileIter = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* whileEnd = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
BasicBlock* whileCheck = BasicBlock::Create(getGlobalContext(), "", context.currentFunction(), 0);
// Check condition satisfaction
BranchInst::Create(whileCheck, context.context());
context.push(whileCheck);
// Whether break the loop
BranchInst::Create(whileIter, whileEnd, whileExpr->CodeGen(context), context.context());
context.pop();
// Entering loop block
context.push(whileIter);
block->CodeGen(context);
// Jump back to condition checking
BranchInst::Create(whileCheck, context.context());
context.pop();
// Return END
context.ret(whileEnd);
return whileEnd;
}
static Value* cast(Value* value, Type* type, GenContext& context) {
if (type == value->getType())
return value;
if (type == Type::getDoubleTy(getGlobalContext())) {
if (value->getType() == Type::getInt64Ty(getGlobalContext()) || value->getType() == Type::getInt8Ty(getGlobalContext()))
value = new SIToFPInst(value, type, "", context.context());
else
cout << "Cannot cast this value.\n";
}
else if (type == Type::getInt64Ty(getGlobalContext())) {
if (value->getType() == Type::getDoubleTy(getGlobalContext()))
value = new FPToSIInst(value, type, "", context.context());
else if (value->getType() == Type::getInt8Ty(getGlobalContext()))
value = new SExtInst(value, type, "", context.context());
else if (value->getType() == Type::getInt32Ty(getGlobalContext()))
value = new ZExtInst(value, type, "", context.context());
else if (value->getType() == Type::getInt8PtrTy(getGlobalContext()))
value = new PtrToIntInst(value, type, "", context.context());
else if (value->getType() == Type::getInt64PtrTy(getGlobalContext()))
value = new PtrToIntInst(value, type, "", context.context());
else
cout << "Cannot cast this value.\n";
} else if (type == Type::getInt8Ty(getGlobalContext())) {
if (value->getType() == Type::getDoubleTy(getGlobalContext()))
value = new FPToSIInst(value, type, "", context.context());
else if (value->getType() == Type::getInt64Ty(getGlobalContext()))
value = new TruncInst(value, type, "", context.context());
else
cout << "Cannot cast this value.\n";
} else
cout << "Cannot cast this value.\n";
return value;
}