-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBD.cpp
522 lines (486 loc) · 16.4 KB
/
DBD.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/Decl.h"
#include "clang/AST/ASTContext.h"
#include <llvm/Support/InitLLVM.h>
#include <sstream>
#include "blocks/block.h"
#include "blocks/var.h"
#include "blocks/stmt.h"
#include "blocks/expr.h"
#include "blocks/c_code_generator.h"
static llvm::cl::OptionCategory DynamicByDefaultCategory("dynamic-by-default options");
using namespace clang;
using namespace llvm;
/* Resuable "block"s */
block::scalar_type::Ptr int_type = nullptr;
block::scalar_type::Ptr char_type = nullptr;
block::scalar_type::Ptr float_type = nullptr;
block::scalar_type::Ptr void_type = nullptr;
block::int_const::Ptr const_one = nullptr;
block::int_const::Ptr const_zero = nullptr;
/*--- */
class DynamicByDefaultVisitor: public RecursiveASTVisitor<DynamicByDefaultVisitor> {
private:
ASTContext* context;
std::vector<block::block::Ptr> &new_decls;
public:
DynamicByDefaultVisitor(ASTContext* context, std::vector<block::block::Ptr>& new_decls):
context(context), new_decls(new_decls) {}
block::stmt_block::Ptr promote_to_body(Stmt* s) {
auto bs = std::make_shared<block::stmt_block>();
std::vector<block::stmt::Ptr> stmts;
lower_stmt_into(s, stmts);
if (stmts.size() != 1) {
bs->stmts = stmts;
return bs;
}
if (block::isa<block::stmt_block>(stmts.back()))
return block::to<block::stmt_block>(stmts.back());
bs->stmts = stmts;
return bs;
}
block::type::Ptr lower_type(QualType type) {
auto t = type.getTypePtr();
if (type->isVoidType()) return void_type;
else if (auto bit = dyn_cast<BuiltinType>(t)) {
switch(bit->getKind()) {
case BuiltinType::Char_U:
case BuiltinType::Char_S:
return char_type;
case BuiltinType::UInt:
case BuiltinType::Int:
return int_type;
case BuiltinType::Float:
return float_type;
default:
bit->dump();
llvm_unreachable("Cannot handle builtin type");
}
if (bit->isInteger()) return int_type;
if (bit->isFloatingPoint()) return float_type;
} else if (const clang::PointerType* pt = dyn_cast<clang::PointerType>(t)) {
auto bpt = std::make_shared<block::pointer_type>();
bpt->pointee_type = lower_type(pt->getPointeeType());
return bpt;
} else if (const ConstantArrayType* cat = dyn_cast<const ConstantArrayType>(t)) {
auto bat = std::make_shared<block::array_type>();
bat->element_type = lower_type(cat->getElementType());
bat->size = cat->getSize().getSExtValue();
return bat;
} else if (const DecayedType *dt = dyn_cast<const DecayedType>(t)) {
return lower_type(dt->getDecayedType());
}
type.dump();
llvm_unreachable("Can't resolve type");
}
block::type::Ptr wrap_type(block::type::Ptr type) {
auto bt = std::make_shared<block::builder_var_type>();
bt->closure_type = type;
bt->builder_var_type_id = block::builder_var_type::DYN_VAR;
return bt;
}
block::var::Ptr lower_function_arg(ParmVarDecl* p) {
block::var::Ptr v = std::make_shared<block::var>();
v->var_name = p->getNameAsString();
v->var_type = lower_type(p->getType());
return v;
}
block::expr::Ptr create_compound_expr(BinaryOperator *bo, block::binary_expr::Ptr bbe) {
// Lowered as a = a <bbe> b
auto e1 = lower_expr(bo->getLHS());
auto m = bbe;
m->expr1 = e1;
m->expr2 = lower_expr(bo->getRHS());
auto a = std::make_shared<block::assign_expr>();
a->var1 = e1;
a->expr1 = m;
return a;
}
block::expr::Ptr lower_expr(Expr* e) {
if (e == nullptr) {
llvm_unreachable("Expr cannot be null?");
}
if (ParenExpr* pe = dyn_cast<ParenExpr>(e)) {
return lower_expr(pe->getSubExpr());
} else if (IntegerLiteral* il = dyn_cast<IntegerLiteral>(e)) {
auto ic = std::make_shared<block::int_const>();
ic->value = il->getValue().getSExtValue();
ic->is_64bit = false;
return ic;
} else if (CharacterLiteral* cl = dyn_cast<CharacterLiteral>(e)) {
auto ic = std::make_shared<block::int_const>();
ic->value = cl->getValue();
ic->is_64bit = false;
return ic;
} else if (BinaryOperator *bo = dyn_cast<BinaryOperator>(e)) {
if (bo->getOpcode() == BO_Assign) {
auto a = std::make_shared<block::assign_expr>();
a->var1 = lower_expr(bo->getLHS());
a->expr1 = lower_expr(bo->getRHS());
return a;
} else {
// Handle the regular binary operators
block::binary_expr::Ptr be = nullptr;
switch (bo->getOpcode()) {
case BO_Add:
be = std::make_shared<block::plus_expr>();
break;
case BO_LT:
be = std::make_shared<block::lt_expr>();
break;
case BO_GT:
be = std::make_shared<block::gt_expr>();
break;
case BO_LE:
be = std::make_shared<block::lte_expr>();
break;
case BO_GE:
be = std::make_shared<block::gte_expr>();
break;
case BO_LAnd:
be = std::make_shared<block::and_expr>();
break;
case BO_LOr:
be = std::make_shared<block::or_expr>();
break;
case BO_NE:
be = std::make_shared<block::ne_expr>();
break;
case BO_EQ:
be = std::make_shared<block::equals_expr>();
break;
case BO_Rem:
be = std::make_shared<block::mod_expr>();
break;
case BO_Mul:
be = std::make_shared<block::mul_expr>();
break;
case BO_Div:
be = std::make_shared<block::div_expr>();
break;
case BO_Sub:
be = std::make_shared<block::minus_expr>();
break;
case BO_Shl:
be = std::make_shared<block::lshift_expr>();
break;
case BO_Shr:
be = std::make_shared<block::rshift_expr>();
break;
case BO_And:
be = std::make_shared<block::bitwise_and_expr>();
break;
case BO_Xor:
be = std::make_shared<block::bitwise_xor_expr>();
break;
case BO_Or:
be = std::make_shared<block::bitwise_or_expr>();
break;
// Operations like MulAssign need to be handled separately
case BO_MulAssign:
return create_compound_expr(bo, std::make_shared<block::mul_expr>());
break;
case BO_DivAssign:
return create_compound_expr(bo, std::make_shared<block::div_expr>());
break;
case BO_RemAssign:
return create_compound_expr(bo, std::make_shared<block::mod_expr>());
break;
case BO_AddAssign:
return create_compound_expr(bo, std::make_shared<block::plus_expr>());
break;
case BO_SubAssign:
return create_compound_expr(bo, std::make_shared<block::minus_expr>());
break;
case BO_ShlAssign:
return create_compound_expr(bo, std::make_shared<block::lshift_expr>());
break;
case BO_ShrAssign:
return create_compound_expr(bo, std::make_shared<block::rshift_expr>());
break;
case BO_AndAssign:
return create_compound_expr(bo, std::make_shared<block::bitwise_and_expr>());
break;
case BO_XorAssign:
return create_compound_expr(bo, std::make_shared<block::bitwise_xor_expr>());
break;
case BO_OrAssign:
return create_compound_expr(bo, std::make_shared<block::bitwise_or_expr>());
break;
default:
bo->dump();
llvm_unreachable("Cannot handle binary operator");
break;
}
be->expr1 = lower_expr(bo->getLHS());
be->expr2 = lower_expr(bo->getRHS());
return be;
}
} else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
// Most unary operators can be handled as unary operators, some need special
// handling as binary operators and expressions
if (uo->getOpcode() == UO_PostInc) {
// Post inc is realized as (a = a + 1) - 1
// TODO: consider adding a postfix ++ in BuildIt
auto e1 = lower_expr(uo->getSubExpr());
auto p = std::make_shared<block::plus_expr>();
p->expr1 = e1;
p->expr2 = const_one;
auto a = std::make_shared<block::assign_expr>();
a->var1 = e1;
a->expr1 = p;
auto m = std::make_shared<block::minus_expr>();
m->expr1 = a;
m->expr2 = const_one;
return m;
} else if (uo->getOpcode() == UO_PostDec) {
auto e1 = lower_expr(uo->getSubExpr());
auto p = std::make_shared<block::minus_expr>();
p->expr1 = e1;
p->expr2 = const_one;
auto a = std::make_shared<block::assign_expr>();
a->var1 = e1;
a->expr1 = p;
auto m = std::make_shared<block::plus_expr>();
m->expr1 = a;
m->expr2 = const_one;
return m;
} else if (uo->getOpcode() == UO_Deref) {
auto sq = std::make_shared<block::sq_bkt_expr>();
sq->var_expr = lower_expr(uo->getSubExpr());
sq->index = const_zero;
return sq;
} else {
// General unary operators
block::unary_expr::Ptr ue;
switch(uo->getOpcode()) {
case UO_AddrOf:
ue = std::make_shared<block::addr_of_expr>();
break;
default:
uo->dump();
llvm_unreachable("Cannot handle unary operator");
break;
}
ue->expr1 = lower_expr(uo->getSubExpr());
return ue;
}
} else if (CastExpr *ice = dyn_cast<CastExpr>(e)) {
if (ImplicitCastExpr* ice = dyn_cast<ImplicitCastExpr>(e)) {
// All implicit casts can actually be lowered as it is,
// There is special cast with void* ofcourse due to how C and C++ treat them
// TODO: handle void* to T* casts
return lower_expr(ice->getSubExpr());
} if (ice->getCastKind() == CK_LValueToRValue) {
return lower_expr(ice->getSubExpr());
} else {
ice->dump();
llvm_unreachable("Cannot handle cast operation");
}
} else if (DeclRefExpr *dr = dyn_cast<DeclRefExpr>(e)) {
ValueDecl *vd = dr->getDecl();
auto v = std::make_shared<block::var>();
v->var_name = vd->getNameAsString();
// type here doesn't matter, but cannot be left null
v->var_type = int_type;
auto ve = std::make_shared<block::var_expr>();
ve->var1 = v;
return ve;
} else if (ArraySubscriptExpr* ase = dyn_cast<ArraySubscriptExpr>(e)) {
auto sbe = std::make_shared<block::sq_bkt_expr>();
sbe->var_expr = lower_expr(ase->getLHS());
sbe->index = lower_expr(ase->getRHS());
return sbe;
} else if (CallExpr *ce = dyn_cast<CallExpr>(e)) {
auto bce = std::make_shared<block::function_call_expr>();
bce->expr1 = lower_expr(ce->getCallee());
for (auto arg: ce->arguments()) {
bce->args.push_back(lower_expr(arg));
}
return bce;
} else if (InitListExpr* ile = dyn_cast<InitListExpr>(e)) {
auto bile = std::make_shared<block::initializer_list_expr>();
for (auto in: ile->inits()) {
bile->elems.push_back(lower_expr(in));
}
return bile;
} else if (clang::StringLiteral *sl = dyn_cast<clang::StringLiteral>(e)) {
auto bse = std::make_shared<block::string_const>();
bse->value = sl->getString().str();
return bse;
} else {
e->dump();
llvm_unreachable("Cannot handle expr type");
}
return nullptr;
}
void lower_stmt_into(Stmt* s, std::vector<block::stmt::Ptr> &out) {
if (CompoundStmt *cs = dyn_cast<CompoundStmt>(s)) {
block::stmt_block::Ptr nb = std::make_shared<block::stmt_block>();
for (auto s: cs->body()) {
lower_stmt_into(s, nb->stmts);
}
out.push_back(nb);
return;
} else if (DeclStmt *ds = dyn_cast<DeclStmt>(s)) {
for (auto de: ds->decls()) {
if (VarDecl* vd = dyn_cast<VarDecl>(de)) {
// TODO figure out if we need to reuse vars or is it okay for them
// to be separate
block::var::Ptr v = std::make_shared<block::var>();
v->var_name = vd->getName().str();
v->var_type = lower_type(vd->getType());
block::decl_stmt::Ptr d = std::make_shared<block::decl_stmt>();
d->decl_var = v;
if (vd->hasInit())
d->init_expr = lower_expr(vd->getInit());
else
d->init_expr = nullptr;
out.push_back(d);
} else {
de->dump();
llvm_unreachable("Cannot handle decl in decl_stmt");
}
}
return;
} else if (ForStmt *fs = dyn_cast<ForStmt>(s)) {
block::for_stmt::Ptr bfs = std::make_shared<block::for_stmt>();
std::vector<block::stmt::Ptr> is;
lower_stmt_into(fs->getInit(), is);
if (is.size() != 1) {
llvm_unreachable("For loop can have only one stmt in initialization");
}
bfs->decl_stmt = is.back();
bfs->cond = lower_expr(fs->getCond());
bfs->update = lower_expr(fs->getInc());
bfs->body = promote_to_body(fs->getBody());
out.push_back(bfs);
return;
} else if (WhileStmt *ws = dyn_cast<WhileStmt>(s)) {
auto bws = std::make_shared<block::while_stmt>();
bws->cond = lower_expr(ws->getCond());
bws->body = promote_to_body(ws->getBody());
out.push_back(bws);
return;
} else if (IfStmt *is = dyn_cast<IfStmt>(s)) {
auto bis = std::make_shared<block::if_stmt>();
bis->cond = lower_expr(is->getCond());
if (is->getThen()) {
bis->then_stmt = promote_to_body(is->getThen());
} else {
bis->then_stmt = std::make_shared<block::stmt_block>();
}
if (is->getElse()) {
bis->else_stmt = promote_to_body(is->getElse());
} else {
bis->else_stmt = std::make_shared<block::stmt_block>();
}
out.push_back(bis);
return;
} else if (ValueStmt *vs = dyn_cast<ValueStmt>(s)) {
if (vs->getExprStmt() == nullptr) return;
auto es = std::make_shared<block::expr_stmt>();
es->expr1 = lower_expr(vs->getExprStmt());
out.push_back(es);
return;
} else if (ReturnStmt *rs = dyn_cast<ReturnStmt>(s)) {
auto brs = std::make_shared<block::return_stmt>();
if (rs->getRetValue()) {
brs->return_val = lower_expr(rs->getRetValue());
} else {
// This is a special case for return statements in
// a void returning function
brs->return_val = nullptr;
}
out.push_back(brs);
return;
} else {
s->dump();
llvm_unreachable("Cannot handle statement");
}
return;
}
bool VisitFunctionDecl(FunctionDecl* fd) {
auto func_decl = std::make_shared<block::func_decl>();
func_decl->func_name = fd->getName();
func_decl->return_type = lower_type(fd->getReturnType());
for (auto pi: fd->parameters()) {
func_decl->args.push_back(lower_function_arg(pi));
}
if (fd->hasBody()) {
func_decl->body = promote_to_body(fd->getBody());
} else {
func_decl->body = std::make_shared<block::stmt_block>();
func_decl->setMetadata<bool>("is_decl_only", true);
}
if (fd->isVariadic()) {
func_decl->setMetadata<bool>("is_variadic", true);
}
new_decls.push_back(func_decl);
return true;
}
};
class DynamicByDefaultConsumer: public ASTConsumer {
private:
DynamicByDefaultVisitor Visitor;
std::vector<block::block::Ptr> new_decls;
public:
explicit DynamicByDefaultConsumer(ASTContext *context): Visitor(context, new_decls) {}
virtual void HandleTranslationUnit(ASTContext& context) override {
Visitor.TraverseDecl(context.getTranslationUnitDecl());
// Dump all the generated decls
std::stringstream ss;
for (auto decl: new_decls) {
block::c_code_generator::generate_code(decl, ss, 0);
}
llvm::errs() << ss.str();
}
};
class DynamicByDefaultAction: public ASTFrontendAction {
public:
virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef infile) override {
return std::unique_ptr<ASTConsumer>(new DynamicByDefaultConsumer(&CI.getASTContext()));
}
};
class DynamicByDefaultFrontendActionFactory : public tooling::FrontendActionFactory {
public:
DynamicByDefaultFrontendActionFactory() {}
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<DynamicByDefaultAction>();
}
};
int main(int argc, const char* argv[]) {
llvm::InitLLVM X(argc, argv);
// Initialize all reusable blocks
int_type = std::make_shared<block::scalar_type>();
int_type->scalar_type_id = block::scalar_type::INT_TYPE;
char_type = std::make_shared<block::scalar_type>();
char_type->scalar_type_id = block::scalar_type::CHAR_TYPE;
float_type = std::make_shared<block::scalar_type>();
float_type->scalar_type_id = block::scalar_type::FLOAT_TYPE;
void_type = std::make_shared<block::scalar_type>();
void_type->scalar_type_id = block::scalar_type::VOID_TYPE;
const_one = std::make_shared<block::int_const>();
const_one->value = 1;
const_one->is_64bit = false;
const_zero = std::make_shared<block::int_const>();
const_zero->value = 0;
const_zero->is_64bit = false;
if (argc > 1) {
auto OptionsParser = tooling::CommonOptionsParser::create(argc, argv, DynamicByDefaultCategory);
if (!OptionsParser) return -1;
tooling::ClangTool Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList());
DynamicByDefaultFrontendActionFactory Factory;
Tool.run(&Factory);
return 0;
}
return -1;
}