/
cfg_builder.cpp
1975 lines (1707 loc) · 64.7 KB
/
cfg_builder.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cfg_builder.hpp"
#include "reg.hpp"
#include <boost/assert.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/utility.hpp>
#include <boost/ref.hpp>
#include <clang/AST/CharUnits.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/StmtCXX.h>
#include <clang/AST/Expr.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/Decl.h>
#include <clang/AST/DeclCXX.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Basic/FileManager.h>
#include <list>
namespace {
// FIXME: Assumes that sizeof(wchar_t) == 2
std::vector<sir_int_t> string_literal_to_value_array(clang::StringLiteral const * sl)
{
std::vector<sir_int_t> res;
llvm::StringRef str = sl->getString();
if (sl->isWide())
{
for (std::size_t i = 0; i < str.size(); i += 2)
res.push_back(str[i+1] * 256 + (unsigned char)str[i]);
}
else
{
for (std::size_t i = 0; i < str.size(); ++i)
res.push_back(str[i]);
}
res.push_back(0);
return res;
}
struct context
{
context(program & p, cfg & c, name_mangler & nm, clang::FunctionDecl const * fn, clang::SourceManager const & sm,
filename_store & fnames, detail::build_cfg_visitor_base & visitor, std::string const & static_prefix)
: m_program(p), m_name_mangler(nm), m_static_prefix(static_prefix), m_sm(sm), m_fnames(fnames), m_visitor(visitor), g(c), m_fn(fn),
m_head(add_vertex(g))
{
g.entry(m_head);
this->build();
}
program & m_program;
name_mangler & m_name_mangler;
std::string m_static_prefix;
clang::SourceManager const & m_sm;
filename_store & m_fnames;
detail::build_cfg_visitor_base & m_visitor;
void register_decl_ref(clang::FunctionDecl const * fn)
{
// TODO: Will we ever need this function?
}
cfg & g;
clang::FunctionDecl const * m_fn;
context_registry m_context_registry;
typedef context_registry::node_descriptor context_node;
typedef context_registry::context_type execution_context;
typedef std::vector<context_node> lifetime_context_t;
std::vector<lifetime_context_t> m_fullexpr_lifetimes;
std::vector<lifetime_context_t> m_block_lifetimes;
std::vector<clang::Type const *> m_temporaries;
cfg::vertex_descriptor m_head;
jump_registry m_exc_registry;
jump_registry m_return_registry;
std::vector<jump_registry> m_break_registries;
std::vector<jump_registry> m_continue_registries;
std::map<clang::GotoStmt const *, cfg::vertex_descriptor> m_gotos;
std::map<clang::LabelDecl const *, cfg::vertex_descriptor> m_labels;
typedef std::pair<cfg::vertex_descriptor, std::map<sir_int_t, cfg::vertex_descriptor> > case_context_t;
std::vector<case_context_t> m_case_contexts;
std::map<clang::NamedDecl const *, std::string> m_registered_names;
eop join_jump_registry(std::vector<jump_registry> & registries, execution_context ec, cfg::vertex_descriptor target)
{
BOOST_ASSERT(!registries.empty());
jump_sentinel s = this->generate_return_paths(registries.back(), ec);
this->join_nodes(s.sentinel, target);
registries.pop_back();
return s.value;
}
void connect_to_exc(cfg::vertex_descriptor v, execution_context ctx)
{
cfg::vertex_descriptor sentinel = add_vertex(g);
cfg::edge_descriptor e = add_edge(v, sentinel, g).first;
g[e].id = 1;
m_exc_registry[ctx].push_back(sentinel);
}
void connect_to_exc(cfg::vertex_descriptor v)
{
this->connect_to_exc(v, m_context_registry.current_context());
}
cfg::vertex_descriptor duplicate_vertex(cfg::vertex_descriptor src)
{
BOOST_ASSERT(out_degree(src, g) == 0);
cfg::vertex_descriptor res = add_vertex(g);
g[res] = g[src];
std::pair<cfg::in_edge_iterator, cfg::in_edge_iterator> in_edge_range = in_edges(src, g);
for (; in_edge_range.first != in_edge_range.second; ++in_edge_range.first)
{
cfg::edge_descriptor e = add_edge(source(*in_edge_range.first, g), res, g).first;
g[e] = g[*in_edge_range.first];
}
return res;
}
void join_nodes(cfg::vertex_descriptor src, cfg::vertex_descriptor dest)
{
if (src != g.null_vertex())
{
g.redirect_vertex(src, dest);
remove_vertex(src, g);
}
}
std::string get_name(clang::NamedDecl const * decl) const
{
std::map<clang::NamedDecl const *, std::string>::const_iterator it = m_registered_names.find(decl);
if (it == m_registered_names.end())
return m_name_mangler.make_decl_name(decl, m_static_prefix);
else
return it->second;
}
void register_locals(clang::FunctionDecl const * fn)
{
if (fn->getResultType()->isStructureOrClassType())
g.add_param("p:return");
if (clang::CXXMethodDecl const * d = llvm::dyn_cast<clang::CXXMethodDecl>(fn))
{
if (!d->isStatic())
g.add_param("p:this");
}
for (clang::FunctionDecl::param_const_iterator it = fn->param_begin(); it != fn->param_end(); ++it)
{
clang::ParmVarDecl const * d = *it;
std::string name = "p:" + d->getQualifiedNameAsString();
m_registered_names[d] = name;
g.add_param(name);
}
std::set<std::string> used_names;
for (clang::FunctionDecl::decl_iterator it = fn->decls_begin(); it != fn->decls_end(); ++it)
{
clang::Decl const * decl = *it;
if (clang::VarDecl const * d = llvm::dyn_cast<clang::VarDecl>(decl))
{
std::string name_base;
if (d->isStaticLocal() || decl->getKind() == clang::Decl::ParmVar)
continue;
name_base = "l:" + d->getQualifiedNameAsString();
name_base += ':';
std::string name;
for (std::size_t i = 0;; ++i)
{
std::ostringstream ss;
ss << name_base << i;
if (used_names.find(ss.str()) == used_names.end())
{
name = ss.str();
used_names.insert(name);
m_registered_names[d] = name;
break;
}
}
g.add_local(name);
}
}
}
eop make_temporary(clang::Type const * type)
{
std::ostringstream ss;
ss << "t:" << m_temporaries.size();
m_temporaries.push_back(type);
g.add_local(ss.str());
return eop(eot_var, ss.str());
}
cfg::operand make_rvalue(eop const & op)
{
BOOST_ASSERT(op.type < eot_nodetgt);
return cfg::operand(static_cast<cfg::op_type>(op.type), op.id);
}
cfg::operand make_rvalue(cfg::vertex_descriptor & head, eop const & op)
{
if (op.type == eot_nodetgt)
{
cfg::vertex_descriptor res = head;
this->add_node(head, enode(cfg::nt_deref)
(eot_node, op.id));
return cfg::operand(cfg::ot_node, res);
}
else if (op.type == eot_vartgt)
{
cfg::vertex_descriptor res = head;
this->add_node(head, enode(cfg::nt_deref)
(eot_var, op.id));
return cfg::operand(cfg::ot_node, res);
}
return this->make_rvalue(op);
}
eop make_param(cfg::vertex_descriptor & head, eop const & op, clang::Type const * type)
{
if (type->isReferenceType() && !this->is_lvalue(op))
{
eop temp = this->make_temporary(type);
this->add_node(head, enode(cfg::nt_assign)
(this->make_address(temp))
(op));
return this->make_address(temp);
}
if (type->isReferenceType() || type->isStructureOrClassType())
return this->make_address(op);
else
return op;
}
template <typename ParamIter, typename ArgIter>
void append_args(cfg::vertex_descriptor & head, enode & node, ParamIter param_first, ParamIter param_last, ArgIter arg_first, ArgIter arg_last)
{
for (; param_first != param_last; ++param_first, ++arg_first)
node(this->make_param(head, this->build_expr(head, *arg_first), (*param_first)->getType().getTypePtr()));
BOOST_ASSERT(arg_first == arg_last);
}
bool is_lvalue(eop const & op)
{
return op.type == eot_func || op.type == eot_nodetgt || op.type == eot_vartgt || op.type == eot_var;
}
eop make_address(eop op)
{
switch (op.type)
{
case eot_func:
break;
case eot_nodetgt:
op.type = eot_node;
break;
case eot_vartgt:
op.type = eot_var;
break;
case eot_var:
op.type = eot_varptr;
break;
default:
BOOST_ASSERT(0 && "lvalue is required to form an address");
}
return op;
}
eop make_deref(cfg::vertex_descriptor & head, eop op)
{
switch (op.type)
{
case eot_func:
break;
case eot_node:
op.type = eot_nodetgt;
break;
case eot_nodetgt:
op = this->make_rvalue(head, op);
BOOST_ASSERT(op.type == eot_node);
op.type = eot_nodetgt;
break;
case eot_varptr:
op.type = eot_var;
break;
case eot_var:
op.type = eot_vartgt;
break;
case eot_vartgt:
op = this->make_rvalue(head, op);
BOOST_ASSERT(op.type == eot_node);
op.type = eot_nodetgt;
break;
default:
BOOST_ASSERT(0 && "lvalue is required to form an address");
}
return op;
}
/**
* \brief Convert an eop to an op of type ot_node and of the same value.
*/
cfg::vertex_descriptor make_node(cfg::vertex_descriptor & head, eop op)
{
op = this->make_rvalue(head, op);
if (op.type != eot_node)
return this->add_node(head, enode(cfg::nt_value)(op));
return boost::get<cfg::vertex_descriptor>(op.id);
}
cfg::vertex_descriptor make_cond_node(cfg::vertex_descriptor & head, eop op)
{
op = this->make_rvalue(head, op);
if (op.type != eot_node || in_degree(head, g) != 1)
return this->add_node(head, enode(cfg::nt_value)(op));
cfg::vertex_descriptor res = boost::get<cfg::vertex_descriptor>(op.id);
if (source(*in_edges(head, g).first, g) != res)
return this->add_node(head, enode(cfg::nt_value)(op));
return res;
}
void attach_range_tag(cfg::node & n, clang::SourceRange const & sr)
{
range_tag r = {};
clang::PresumedLoc pl = m_sm.getPresumedLoc(sr.getBegin());
r.fname = m_fnames.add(pl.getFilename());
r.start_line = pl.getLine();
r.start_col = pl.getColumn();
pl = m_sm.getPresumedLoc(sr.getEnd());
r.end_line = pl.getLine();
r.end_col = pl.getColumn();
n.tags.insert(g.add_tag(r));
}
cfg::node convert_node(cfg::vertex_descriptor & head, enode const & node)
{
cfg::node n;
n.type = node.type;
for (std::size_t i = 0; i < node.ops.size(); ++i)
n.ops.push_back(this->make_rvalue(head, node.ops[i]));
if (node.data)
this->attach_range_tag(n, node.data->getSourceRange());
return n;
}
cfg::vertex_descriptor add_node(cfg::vertex_descriptor & head, enode const & node)
{
BOOST_ASSERT(g[head].type == cfg::nt_none);
BOOST_ASSERT(g[head].ops.empty());
cfg::node n = this->convert_node(head, node);
g[head] = n;
cfg::vertex_descriptor new_head = add_vertex(g);
add_edge(head, new_head, g);
using std::swap;
swap(head, new_head);
return new_head;
}
void set_cond(cfg::vertex_descriptor node, std::size_t index, constant const & cond)
{
BOOST_ASSERT(in_degree(node, g) == 1);
cfg::edge_descriptor edge = *in_edges(node, g).first;
g[edge].id = index;
g[edge].cond = cond;
}
void cleanup_lifetime_node(cfg::vertex_descriptor & head, context_node n)
{
execution_context ec = m_context_registry.context(n);
m_context_registry.remove(n);
return_path_generator g(*this, m_context_registry.current_context(), head);
head = m_context_registry.value(ec).apply_visitor(g).sentinel;
}
void begin_lifetime_context(std::vector<lifetime_context_t> & ctx)
{
ctx.push_back(lifetime_context_t());
}
void end_lifetime_context(cfg::vertex_descriptor & head, std::vector<lifetime_context_t> & ctx)
{
BOOST_ASSERT(!ctx.empty());
for (std::size_t i = ctx.back().size(); i != 0; --i)
this->cleanup_lifetime_node(head, ctx.back()[i-1]);
ctx.pop_back();
}
context_node register_destructible_var(clang::CXXDestructorDecl const * destructor, eop const & varptr)
{
var_regrec reg = { destructor, varptr };
return m_context_registry.add(reg);
}
void build_construct_expr(cfg::vertex_descriptor & head, eop const & varptr, clang::CXXConstructExpr const * e)
{
BOOST_ASSERT(varptr.type != eot_none);
enode node(cfg::nt_call, e);
this->register_decl_ref(e->getConstructor());
node(eot_func, this->get_name(e->getConstructor()));
clang::FunctionProtoType const * fntype = llvm::dyn_cast<clang::FunctionProtoType>(e->getConstructor()->getType().getTypePtr());
node(varptr);
for (std::size_t i = 0; i < e->getNumArgs(); ++i)
node(this->make_param(head, this->build_expr(head, e->getArg(i)), fntype->getArgType(i).getTypePtr()));
cfg::vertex_descriptor call_node = this->add_node(head, node);
if (!llvm::cast<clang::FunctionProtoType>(e->getConstructor()->getType()->getUnqualifiedDesugaredType())->hasEmptyExceptionSpec())
this->connect_to_exc(call_node);
}
eop make_phi(cfg::vertex_descriptor & head, cfg::vertex_descriptor branch, eop headop, eop branchop, bool lvalue, clang::Expr const * data = 0)
{
if (lvalue)
{
headop = this->make_address(headop);
branchop = this->make_address(branchop);
}
cfg::vertex_descriptor head_value_node = this->make_node(head, headop);
cfg::vertex_descriptor branch_value_node = this->make_node(branch, branchop);
this->join_nodes(branch, head);
cfg::vertex_descriptor res_node = this->add_node(head, enode(cfg::nt_phi, data)
(eot_node, head_value_node)
(eot_node, branch_value_node));
if (lvalue)
return eop(eot_nodetgt, res_node);
else
return eop(eot_node, res_node);
}
eop build_expr(cfg::vertex_descriptor & head, clang::Expr const * expr)
{
BOOST_ASSERT(expr != 0);
if (clang::BinaryOperator const * e = llvm::dyn_cast<clang::BinaryOperator>(expr))
{
// FIXME: pointer arithmetics should produce adjust/dist operands.
// These should only appear as a part of CallExpr and should be handler right there.
BOOST_ASSERT(e->getOpcode() != clang::BO_PtrMemD && e->getOpcode() != clang::BO_PtrMemI);
eop const & lhs = this->build_expr(head, e->getLHS());
if (e->getOpcode() == clang::BO_Assign)
{
// Treat assignment specially (takes a pointer to the assignee).
eop const & rhs = this->build_expr(head, e->getRHS());
this->add_node(head, enode(cfg::nt_assign, expr)
(this->make_address(lhs))
(rhs));
return lhs;
}
else if (e->isCompoundAssignmentOp())
{
// Break compound assignments into separate instructions,
// i.e. model a += b as an add followed by assign.
eop const & rhs = this->build_expr(head, e->getRHS());
cfg::node_type type;
switch (e->getOpcode())
{
case clang::BO_MulAssign: type = cfg::nt_mul; break;
case clang::BO_DivAssign: type = cfg::nt_div; break;
case clang::BO_RemAssign: type = cfg::nt_rem; break;
case clang::BO_AddAssign: type = cfg::nt_add; break;
case clang::BO_SubAssign: type = cfg::nt_sub; break;
case clang::BO_ShlAssign: type = cfg::nt_shl; break;
case clang::BO_ShrAssign: type = cfg::nt_shr; break;
case clang::BO_AndAssign: type = cfg::nt_and; break;
case clang::BO_XorAssign: type = cfg::nt_xor; break;
case clang::BO_OrAssign: type = cfg::nt_or; break;
default:
BOOST_ASSERT(0 && "unknown compound assignment encountered");
}
cfg::vertex_descriptor v = this->add_node(head, enode(type, expr)(lhs)(rhs));
this->add_node(head, enode(cfg::nt_assign, expr)(this->make_address(lhs))(eot_node, v));
return lhs;
}
else if (e->getOpcode() == clang::BO_Comma)
{
this->build_expr(head, e->getLHS());
return this->build_expr(head, e->getRHS());
}
else if (e->getOpcode() == clang::BO_LOr || e->getOpcode() == clang::BO_LAnd)
{
// These two operators are shot-circuiting, i.e. the right-hand side
// operand is not evaluated, unless the result of the operation is still unknown
// even after the left-hand side evaluation is finished.
cfg::vertex_descriptor cond_node = this->make_node(head, lhs);
cfg::vertex_descriptor cont_head = this->duplicate_vertex(head);
if (e->getOpcode() == clang::BO_LAnd)
g[*in_edges(head, g).first].cond = sir_int_t(0);
else
g[*in_edges(cont_head, g).first].cond = sir_int_t(0);
eop const & rhs = this->build_expr(cont_head, e->getRHS());
cfg::vertex_descriptor rhs_value_node = this->make_node(cont_head, rhs);
this->join_nodes(cont_head, head);
return eop(eot_node, this->add_node(head, enode(cfg::nt_phi, e)
(eot_node, rhs_value_node)
(eot_node, cond_node)));
}
else
{
eop const & rhs = this->build_expr(head, e->getRHS());
bool negate = false;
cfg::node_type type;
switch (e->getOpcode())
{
case clang::BO_Mul: type = cfg::nt_mul; break;
case clang::BO_Div: type = cfg::nt_div; break;
case clang::BO_Rem: type = cfg::nt_rem; break;
case clang::BO_Add: type = cfg::nt_add; break;
case clang::BO_Sub: type = cfg::nt_sub; break;
case clang::BO_Shl: type = cfg::nt_shl; break;
case clang::BO_Shr: type = cfg::nt_shr; break;
case clang::BO_And: type = cfg::nt_and; break;
case clang::BO_Xor: type = cfg::nt_xor; break;
case clang::BO_Or: type = cfg::nt_or; break;
case clang::BO_EQ: type = cfg::nt_eq; break;
case clang::BO_NE: type = cfg::nt_eq; negate = true; break;
case clang::BO_LT: type = cfg::nt_less; break;
case clang::BO_LE: type = cfg::nt_leq; break;
case clang::BO_GE: type = cfg::nt_less; negate = true; break;
case clang::BO_GT: type = cfg::nt_leq; negate = true; break;
default:
BOOST_ASSERT(0 && "unknown binary operator encountered");
}
cfg::vertex_descriptor v = this->add_node(head, enode(type, expr)(lhs)(rhs));
if (negate)
v = this->add_node(head, enode(cfg::nt_not, expr)(eot_node, v));
return eop(eot_node, v);
}
}
else if (clang::UnaryOperator const * e = llvm::dyn_cast<clang::UnaryOperator>(expr))
{
if (e->getOpcode() == clang::UO_AddrOf)
{
return this->make_address(this->build_expr(head, e->getSubExpr()));
}
else if (e->getOpcode() == clang::UO_Deref)
{
return this->make_deref(head, this->build_expr(head, e->getSubExpr()));
}
else if (e->getOpcode() == clang::UO_PreInc || e->getOpcode() == clang::UO_PreDec)
{
eop expr = this->build_expr(head, e->getSubExpr());
cfg::vertex_descriptor node = this->add_node(head, enode(e)
(e->getOpcode() == clang::UO_PreInc? cfg::nt_add: cfg::nt_sub)
(expr)
(eot_const, sir_int_t(1)));
this->add_node(head, enode(cfg::nt_assign, e)
(this->make_address(expr))
(eot_node, node));
return expr;
}
else if (e->getOpcode() == clang::UO_PostInc || e->getOpcode() == clang::UO_PostDec)
{
eop expr = this->build_expr(head, e->getSubExpr());
cfg::vertex_descriptor node = this->add_node(head, enode(e)
(e->getOpcode() == clang::UO_PostInc? cfg::nt_add: cfg::nt_sub)
(expr)
(eot_const, sir_int_t(1)));
this->add_node(head, enode(cfg::nt_assign, e)
(this->make_address(expr))
(eot_node, node));
return eop(eot_node, node);
}
else if (e->getOpcode() == clang::UO_Plus)
{
// Ignore the unary plus operator.
return this->build_expr(head, e->getSubExpr());
}
else if (e->getOpcode() == clang::UO_Not)
{
// Model negation as a xor between the value and the maximum value of the value's type.
uint64_t xor_val = (1 << m_fn->getASTContext().getTypeSize(e->getType())) - 1;
return eop(eot_node, this->add_node(head, enode(cfg::nt_xor, expr)
(this->build_expr(head, e->getSubExpr()))
(eot_const, sir_int_t(xor_val))));
}
else
{
cfg::node_type type;
switch (e->getOpcode())
{
case clang::UO_Minus: type = cfg::nt_neg; break;
case clang::UO_LNot: type = cfg::nt_not; break;
default:
BOOST_ASSERT(0 && "unknown unary operator encountered");
}
return eop(eot_node, this->add_node(head, enode(type, e)
(this->build_expr(head, e->getSubExpr()))));
}
}
else if (clang::CXXBoolLiteralExpr const * e = llvm::dyn_cast<clang::CXXBoolLiteralExpr>(expr))
{
return eop(eot_const, e->getValue()? sir_int_t(1): sir_int_t(0));
}
else if (clang::IntegerLiteral const * e = llvm::dyn_cast<clang::IntegerLiteral>(expr))
{
return eop(eot_const, (sir_int_t)e->getValue().getLimitedValue());
}
else if (clang::FloatingLiteral const * e = llvm::dyn_cast<clang::FloatingLiteral>(expr))
{
return eop(eot_const, e->getValueAsApproximateDouble());
}
else if (clang::CharacterLiteral const * e = llvm::dyn_cast<clang::CharacterLiteral>(expr))
{
return eop(eot_const, sir_int_t(e->getValue()));
}
else if (clang::StringLiteral const * e = llvm::dyn_cast<clang::StringLiteral>(expr))
{
std::vector<sir_int_t> values = string_literal_to_value_array(e);
std::string lit_symbol = m_program.get_string_literal_symbol(values, m_static_prefix);
return eop(eot_var, lit_symbol);
}
else if (clang::DeclRefExpr const * e = llvm::dyn_cast<clang::DeclRefExpr>(expr))
{
clang::Decl const * decl = e->getDecl();
if (clang::ValueDecl const * nd = llvm::dyn_cast<clang::ValueDecl>(decl))
{
if (clang::EnumConstantDecl const * ecd = llvm::dyn_cast<clang::EnumConstantDecl>(nd))
return eop(eot_const, sir_int_t(ecd->getInitVal().getLimitedValue()));
if (clang::FunctionDecl const * fd = llvm::dyn_cast<clang::FunctionDecl>(nd))
{
this->register_decl_ref(fd);
return eop(eot_func, this->get_name(fd));
}
if (nd->getType()->isReferenceType())
return eop(eot_vartgt, this->get_name(nd));
return eop(eot_var, this->get_name(nd));
}
else
{
BOOST_ASSERT(0 && "encountered a declref to a non-value decl");
return eop();
}
}
else if (clang::CXXThisExpr const * e = llvm::dyn_cast<clang::CXXThisExpr>(expr))
{
return eop(eot_var, "p:this");
}
else if (clang::CallExpr const * e = llvm::dyn_cast<clang::CallExpr>(expr))
{
// Deal with pseudo-destructor calls.
if (clang::CXXPseudoDestructorExpr const * de = llvm::dyn_cast<clang::CXXPseudoDestructorExpr>(e->getCallee()))
{
return this->build_expr(head, de->getBase());
}
// There are several possibilities.
// 1. The call is a call to an overloaded operator.
// 2. The expression type is clang::CXXMemberCallExpr. Then the callee is either
// a. clang::MemberExpr and the type of the function can be determined from the
// member declaration (remember there is an implicit `this` parameter), or
// b. clang::BinaryOperator, with either PtrMemD or PtrMemI; the type of
// the parameters can be extracted from the rhs operand.
// 3. This is a normal invocation, in which case the type of the callee is a pointer to function,
// the types of parameters can be extracted from there.
//
// Additionally, there can be an implicit parameter representing the return value
// (if the value is of a class type).
eop callee_op;
std::vector<eop> params;
std::vector<clang::Type const *> param_types;
clang::FunctionProtoType const * fntype;
std::size_t arg_index = 0;
if (llvm::isa<clang::CXXOperatorCallExpr>(e))
{
BOOST_ASSERT(e->getDirectCallee() != 0);
if (clang::CXXMethodDecl const * md = llvm::dyn_cast<clang::CXXMethodDecl>(e->getDirectCallee()))
{
// C++03: 13.5/6: overloaded operators can't be static member functions
eop this_op = this->build_expr(head, e->getArg(arg_index++));
this_op = this->make_address(this_op);
params.push_back(this_op);
param_types.push_back(
md->getThisType(md->getASTContext()).getTypePtr());
this->register_decl_ref(md);
callee_op = eop(eot_func, this->get_name(md));
fntype = llvm::dyn_cast<clang::FunctionProtoType>(md->getType().getTypePtr());
}
else
{
callee_op = this->build_expr(head, e->getCallee());
fntype = llvm::dyn_cast<clang::FunctionProtoType>(e->getCallee()->getType()->getPointeeType());
}
}
else if (llvm::isa<clang::CXXMemberCallExpr>(e))
{
if (clang::MemberExpr const * mcallee = llvm::dyn_cast<clang::MemberExpr>(e->getCallee()))
{
clang::CXXMethodDecl const * mdecl = llvm::dyn_cast<clang::CXXMethodDecl>(mcallee->getMemberDecl());
eop this_op = this->build_expr(head, mcallee->getBase());
if (!mdecl->isStatic())
{
if (!mcallee->isArrow())
this_op = this->make_address(this_op);
params.push_back(this_op);
param_types.push_back(
mdecl->getThisType(mdecl->getASTContext()).getTypePtr());
}
else
{
this->make_node(head, this_op);
}
this->register_decl_ref(mdecl);
std::string fnname = this->get_name(mdecl);
if (mdecl->isVirtual() && !mcallee->hasQualifier())
fnname = "v:" + fnname;
callee_op = eop(eot_func, fnname);
fntype = llvm::dyn_cast<clang::FunctionProtoType>(mdecl->getType().getTypePtr());
}
else if (clang::BinaryOperator const * callee = llvm::dyn_cast<clang::BinaryOperator>(e->getCallee()))
{
BOOST_ASSERT(callee->getOpcode() == clang::BO_PtrMemD || callee->getOpcode() == clang::BO_PtrMemI);
callee_op = this->build_expr(head, callee->getRHS());
clang::MemberPointerType const * calleePtrType = llvm::dyn_cast<clang::MemberPointerType>(callee->getRHS()->getType().getTypePtr());
BOOST_ASSERT(calleePtrType);
eop this_op = this->build_expr(head, callee->getLHS());
if (callee->getOpcode() == clang::BO_PtrMemD)
this_op = this->make_address(this_op);
params.push_back(this_op);
// FIXME: this should be a pointer to
param_types.push_back(calleePtrType->getClass()->getCanonicalTypeInternal().getTypePtr());
fntype = llvm::dyn_cast<clang::FunctionProtoType>(calleePtrType->getPointeeType().getTypePtr());
}
else
BOOST_ASSERT(0);
}
else
{
callee_op = this->build_expr(head, e->getCallee());
fntype = llvm::dyn_cast<clang::FunctionProtoType>(e->getCallee()->getType()->getUnqualifiedDesugaredType()->getPointeeType()->getUnqualifiedDesugaredType());
}
BOOST_ASSERT(fntype);
eop result_op;
clang::Type const * restype = fntype->getResultType().getTypePtr();
if (restype->isStructureOrClassType())
{
eop temp = this->make_temporary(restype);
params.insert(params.begin(), temp);
param_types.insert(param_types.begin(), restype);
result_op = temp;
}
BOOST_ASSERT(fntype->isVariadic() || fntype->getNumArgs() + arg_index == e->getNumArgs());
for (std::size_t i = 0; i < fntype->getNumArgs(); ++i)
{
params.push_back(this->build_expr(head, e->getArg(i + arg_index)));
param_types.push_back(fntype->getArgType(i).getTypePtr());
}
for (std::size_t i = fntype->getNumArgs() + arg_index; i < e->getNumArgs(); ++i)
{
params.push_back(this->build_expr(head, e->getArg(i)));
param_types.push_back(e->getArg(i)->getType().getTypePtr());
}
// TODO: handle classes with conversion to pointer to fn.
enode node(cfg::nt_call, e);
node(callee_op);
for (std::size_t i = 0; i < params.size(); ++i)
node(this->make_param(head, params[i], param_types[i]));
cfg::vertex_descriptor call_node = this->add_node(head, node);
if (!fntype->hasEmptyExceptionSpec())
this->connect_to_exc(call_node);
if (result_op.type != eot_none)
return result_op;
if (restype->isReferenceType())
return eop(eot_nodetgt, call_node);
else
return eop(eot_node, call_node);
}
else if (clang::ConditionalOperator const * e = llvm::dyn_cast<clang::ConditionalOperator>(expr))
{
eop cond_op = this->build_expr(head, e->getCond());
cfg::vertex_descriptor branch_node = this->make_node(head, cond_op);
cfg::vertex_descriptor false_head = this->duplicate_vertex(head);
g[*in_edges(false_head, g).first].cond = sir_int_t(0);
eop true_res = this->build_expr(head, e->getTrueExpr());
eop false_res = this->build_expr(false_head, e->getFalseExpr());
if (true_res.type != eot_none && false_res.type != eot_none)
{
return this->make_phi(head, false_head, true_res, false_res, e->isLValue(), e);
}
else
{
this->join_nodes(false_head, head);
if (true_res.type == eot_none && false_res.type == eot_none)
return eop();
if (true_res.type != eot_none)
return true_res;
else
return false_res;
}
}
else if (clang::SizeOfAlignOfExpr const * e = llvm::dyn_cast<clang::SizeOfAlignOfExpr>(expr))
{
// TODO: is there a better way?
BOOST_ASSERT(e->isSizeOf() && e->isIntegerConstantExpr(m_fn->getASTContext()));
return eop(eot_const, sir_int_t(e->EvaluateAsInt(m_fn->getASTContext()).getLimitedValue()));
}
else if (clang::MemberExpr const * e = llvm::dyn_cast<clang::MemberExpr>(expr))
{
// TODO: lvalue/rvalue
eop base = this->build_expr(head, e->getBase());
if (!e->isArrow())
base = this->make_address(base);
return eop(eot_nodetgt, this->add_node(head, enode(cfg::nt_member, e)
(base)
(eot_const, this->get_name(e->getMemberDecl()))
));
}
else if (clang::ArraySubscriptExpr const * e = llvm::dyn_cast<clang::ArraySubscriptExpr>(expr))
{
return this->get_array_element(head, this->build_expr(head, e->getLHS()), this->build_expr(head, e->getRHS()), e);
}
else if (clang::ParenExpr const * e = llvm::dyn_cast<clang::ParenExpr>(expr))
{
return this->build_expr(head, e->getSubExpr());
}
else if (clang::CXXDefaultArgExpr const * e = llvm::dyn_cast<clang::CXXDefaultArgExpr>(expr))
{
return this->build_expr(head, e->getExpr());
}
else if (clang::CXXConstructExpr const * e = llvm::dyn_cast<clang::CXXConstructExpr>(expr))
{
eop temp = this->make_temporary(e->getType().getTypePtr());
this->build_construct_expr(head, this->make_address(temp), e);
return temp;
}
else if (clang::ExprWithCleanups const * e = llvm::dyn_cast<clang::ExprWithCleanups>(expr))
{
return this->build_expr(head, e->getSubExpr());
}
else if (clang::CXXBindTemporaryExpr const * e = llvm::dyn_cast<clang::CXXBindTemporaryExpr>(expr))
{
// TODO: deal with extended lifetime of temporaries bound to a reference.
eop res = this->build_expr(head, e->getSubExpr());
context_node reg = this->register_destructible_var(e->getTemporary()->getDestructor(), this->make_address(res));
m_fullexpr_lifetimes.back().push_back(reg);
return res;
}
else if (clang::CastExpr const * e = llvm::dyn_cast<clang::CastExpr>(expr))
{
// TODO: deal with the casts correctly (notably with dynamic_cast)
switch (e->getCastKind())
{
case clang::CK_ArrayToPointerDecay:
return this->decay_array_to_pointer(head, this->build_expr(head, e->getSubExpr()));
default:
return this->build_expr(head, e->getSubExpr());
}
}
else if (clang::CXXPseudoDestructorExpr const * e = llvm::dyn_cast<clang::CXXPseudoDestructorExpr>(expr))
{
return this->build_expr(head, e->getBase());
}
else if (clang::CXXNewExpr const * e = llvm::dyn_cast<clang::CXXNewExpr>(expr))
{
// TODO: Model the two operators directly.
if (e->isArray())
{
enode node(cfg::nt_call, e);
node(eot_func, "__sir_cpp_new_array");
node(eot_func, this->get_name(e->getOperatorNew()));
node(eot_const, sir_int_t(m_fn->getASTContext().getTypeSizeInChars(e->getAllocatedType()).getQuantity()));
if (e->getConstructor())
node(eot_func, this->get_name(e->getConstructor()));
else
node(eot_const, sir_int_t(0));
node(eot_func, this->get_name(e->getOperatorDelete()));
node(eot_const, e->hasInitializer()? sir_int_t(1): sir_int_t(0));
this->append_args(
head,
node,
e->getOperatorNew()->param_begin() + 1, e->getOperatorNew()->param_end(),
e->placement_arg_begin(), e->placement_arg_end());
return eop(eot_node, this->add_node(head, node));
}
else
{
enode opnew_node(cfg::nt_call, e);
opnew_node(eot_func, this->get_name(e->getOperatorNew()));
opnew_node(eot_const, sir_int_t(m_fn->getASTContext().getTypeSizeInChars(e->getAllocatedType()).getQuantity()));
this->append_args(
head,
opnew_node,
e->getOperatorNew()->param_begin() + 1, e->getOperatorNew()->param_end(),
e->placement_arg_begin(), e->placement_arg_end());
cfg::vertex_descriptor v = this->add_node(head, opnew_node);
this->connect_to_exc(v);
eop ptr_op = eop(eot_node, v);
if (e->getConstructor() != 0)
{
enode construct_node(cfg::nt_call, e);
construct_node(eot_func, this->get_name(e->getConstructor()));
construct_node(ptr_op);
this->append_args(head, construct_node, e->getConstructor()->param_begin(), e->getConstructor()->param_end(),
e->constructor_arg_begin(), e->constructor_arg_end());
this->add_node(head, construct_node);
cfg::vertex_descriptor exc_head = this->duplicate_vertex(head);
this->set_cond(exc_head, 1, boost::none);
this->add_node(exc_head, enode(cfg::nt_call, e)
(eot_func, this->get_name(e->getOperatorDelete()))
(ptr_op)
);
m_exc_registry[m_context_registry.current_context()].push_back(exc_head);
}
else if (e->raw_arg_begin() != e->raw_arg_end())
{
// Non-class type, but with an initializer
BOOST_ASSERT(e->raw_arg_end() - e->raw_arg_begin() == 1);
this->add_node(head, enode(cfg::nt_assign, e)
(ptr_op)
(this->build_expr(head, *e->raw_arg_begin())));
}
return ptr_op;
}
}
else if (clang::CXXDeleteExpr const * e = llvm::dyn_cast<clang::CXXDeleteExpr>(expr))
{
// TODO: Model directly by calling the destructor followed by calling the operator delete function.