-
Notifications
You must be signed in to change notification settings - Fork 9
/
scan.cc
3357 lines (2916 loc) · 92.2 KB
/
scan.cc
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
/*
* Copyright 2011 Leiden University. All rights reserved.
* Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
* Copyright 2015-2017 Sven Verdoolaege. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include "config.h"
#include <string.h>
#include <set>
#include <map>
#include <iostream>
#include <sstream>
#include <llvm/Support/raw_ostream.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/ASTDiagnostic.h>
#include <clang/AST/Attr.h>
#include <clang/AST/Expr.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <isl/id.h>
#include <isl/space.h>
#include <isl/aff.h>
#include <isl/set.h>
#include <isl/union_set.h>
#include "aff.h"
#include "array.h"
#include "clang_compatibility.h"
#include "clang.h"
#include "context.h"
#include "expr.h"
#include "expr_plus.h"
#include "id.h"
#include "inliner.h"
#include "inlined_calls.h"
#include "killed_locals.h"
#include "nest.h"
#include "options.h"
#include "scan.h"
#include "scop.h"
#include "scop_plus.h"
#include "substituter.h"
#include "tree.h"
#include "tree2scop.h"
using namespace std;
using namespace clang;
static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
{
switch (kind) {
case UO_Minus:
return pet_op_minus;
case UO_Not:
return pet_op_not;
case UO_LNot:
return pet_op_lnot;
case UO_PostInc:
return pet_op_post_inc;
case UO_PostDec:
return pet_op_post_dec;
case UO_PreInc:
return pet_op_pre_inc;
case UO_PreDec:
return pet_op_pre_dec;
default:
return pet_op_last;
}
}
static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
{
switch (kind) {
case BO_AddAssign:
return pet_op_add_assign;
case BO_SubAssign:
return pet_op_sub_assign;
case BO_MulAssign:
return pet_op_mul_assign;
case BO_DivAssign:
return pet_op_div_assign;
case BO_AndAssign:
return pet_op_and_assign;
case BO_XorAssign:
return pet_op_xor_assign;
case BO_OrAssign:
return pet_op_or_assign;
case BO_Assign:
return pet_op_assign;
case BO_Add:
return pet_op_add;
case BO_Sub:
return pet_op_sub;
case BO_Mul:
return pet_op_mul;
case BO_Div:
return pet_op_div;
case BO_Rem:
return pet_op_mod;
case BO_Shl:
return pet_op_shl;
case BO_Shr:
return pet_op_shr;
case BO_EQ:
return pet_op_eq;
case BO_NE:
return pet_op_ne;
case BO_LE:
return pet_op_le;
case BO_GE:
return pet_op_ge;
case BO_LT:
return pet_op_lt;
case BO_GT:
return pet_op_gt;
case BO_And:
return pet_op_and;
case BO_Xor:
return pet_op_xor;
case BO_Or:
return pet_op_or;
case BO_LAnd:
return pet_op_land;
case BO_LOr:
return pet_op_lor;
default:
return pet_op_last;
}
}
#ifdef GETTYPEINFORETURNSTYPEINFO
static int size_in_bytes(ASTContext &context, QualType type)
{
return context.getTypeInfo(type).Width / 8;
}
#else
static int size_in_bytes(ASTContext &context, QualType type)
{
return context.getTypeInfo(type).first / 8;
}
#endif
/* Check if the element type corresponding to the given array type
* has a const qualifier.
*/
static bool const_base(QualType qt)
{
const Type *type = qt.getTypePtr();
if (type->isPointerType())
return const_base(type->getPointeeType());
if (type->isArrayType()) {
const ArrayType *atype;
type = type->getCanonicalTypeInternal().getTypePtr();
atype = cast<ArrayType>(type);
return const_base(atype->getElementType());
}
return qt.isConstQualified();
}
PetScan::~PetScan()
{
std::map<const Type *, pet_expr *>::iterator it;
std::map<FunctionDecl *, pet_function_summary *>::iterator it_s;
for (it = type_size.begin(); it != type_size.end(); ++it)
pet_expr_free(it->second);
for (it_s = summary_cache.begin(); it_s != summary_cache.end(); ++it_s)
pet_function_summary_free(it_s->second);
isl_id_to_pet_expr_free(id_size);
isl_union_map_free(value_bounds);
}
/* Report a diagnostic on the range "range", unless autodetect is set.
*/
void PetScan::report(SourceRange range, unsigned id)
{
if (options->autodetect)
return;
SourceLocation loc = range.getBegin();
DiagnosticsEngine &diag = PP.getDiagnostics();
DiagnosticBuilder B = diag.Report(loc, id) << range;
}
/* Report a diagnostic on "stmt", unless autodetect is set.
*/
void PetScan::report(Stmt *stmt, unsigned id)
{
report(stmt->getSourceRange(), id);
}
/* Report a diagnostic on "decl", unless autodetect is set.
*/
void PetScan::report(Decl *decl, unsigned id)
{
report(decl->getSourceRange(), id);
}
/* Called if we found something we (currently) cannot handle.
* We'll provide more informative warnings later.
*
* We only actually complain if autodetect is false.
*/
void PetScan::unsupported(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"unsupported");
report(stmt, id);
}
/* Report an unsupported unary operator, unless autodetect is set.
*/
void PetScan::report_unsupported_unary_operator(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"this type of unary operator is not supported");
report(stmt, id);
}
/* Report an unsupported binary operator, unless autodetect is set.
*/
void PetScan::report_unsupported_binary_operator(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"this type of binary operator is not supported");
report(stmt, id);
}
/* Report an unsupported statement type, unless autodetect is set.
*/
void PetScan::report_unsupported_statement_type(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"this type of statement is not supported");
report(stmt, id);
}
/* Report a missing prototype, unless autodetect is set.
*/
void PetScan::report_prototype_required(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"prototype required");
report(stmt, id);
}
/* Report a missing increment, unless autodetect is set.
*/
void PetScan::report_missing_increment(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"missing increment");
report(stmt, id);
}
/* Report a missing summary function, unless autodetect is set.
*/
void PetScan::report_missing_summary_function(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"missing summary function");
report(stmt, id);
}
/* Report a missing summary function body, unless autodetect is set.
*/
void PetScan::report_missing_summary_function_body(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"missing summary function body");
report(stmt, id);
}
/* Report an unsupported argument in a call to an inlined function,
* unless autodetect is set.
*/
void PetScan::report_unsupported_inline_function_argument(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"unsupported inline function call argument");
report(stmt, id);
}
/* Report an unsupported type of declaration, unless autodetect is set.
*/
void PetScan::report_unsupported_declaration(Decl *decl)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"unsupported declaration");
report(decl, id);
}
/* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
*/
void PetScan::report_unbalanced_pragmas(SourceLocation scop,
SourceLocation endscop)
{
if (options->autodetect)
return;
DiagnosticsEngine &diag = PP.getDiagnostics();
{
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"unbalanced endscop pragma");
DiagnosticBuilder B2 = diag.Report(endscop, id);
}
{
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Note,
"corresponding scop pragma");
DiagnosticBuilder B = diag.Report(scop, id);
}
}
/* Report a return statement in an unsupported context,
* unless autodetect is set.
*/
void PetScan::report_unsupported_return(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"return statements not supported in this context");
report(stmt, id);
}
/* Report a return statement that does not appear at the end of a function,
* unless autodetect is set.
*/
void PetScan::report_return_not_at_end_of_function(Stmt *stmt)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"return statement must be final statement in function");
report(stmt, id);
}
/* Extract an integer from "val", which is assumed to be non-negative.
*/
static __isl_give isl_val *extract_unsigned(isl_ctx *ctx,
const llvm::APInt &val)
{
unsigned n;
const uint64_t *data;
data = val.getRawData();
n = val.getNumWords();
return isl_val_int_from_chunks(ctx, n, sizeof(uint64_t), data);
}
/* Extract an integer from "val". If "is_signed" is set, then "val"
* is signed. Otherwise it it unsigned.
*/
static __isl_give isl_val *extract_int(isl_ctx *ctx, bool is_signed,
llvm::APInt val)
{
int is_negative = is_signed && val.isNegative();
isl_val *v;
if (is_negative)
val = -val;
v = extract_unsigned(ctx, val);
if (is_negative)
v = isl_val_neg(v);
return v;
}
/* Extract an integer from "expr".
*/
__isl_give isl_val *PetScan::extract_int(isl_ctx *ctx, IntegerLiteral *expr)
{
const Type *type = expr->getType().getTypePtr();
bool is_signed = type->hasSignedIntegerRepresentation();
return ::extract_int(ctx, is_signed, expr->getValue());
}
/* Extract an integer from "expr".
* Return NULL if "expr" does not (obviously) represent an integer.
*/
__isl_give isl_val *PetScan::extract_int(clang::ParenExpr *expr)
{
return extract_int(expr->getSubExpr());
}
/* Extract an integer from "expr".
* Return NULL if "expr" does not (obviously) represent an integer.
*/
__isl_give isl_val *PetScan::extract_int(clang::Expr *expr)
{
if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
return extract_int(ctx, cast<IntegerLiteral>(expr));
if (expr->getStmtClass() == Stmt::ParenExprClass)
return extract_int(cast<ParenExpr>(expr));
unsupported(expr);
return NULL;
}
/* Extract a pet_expr from the APInt "val", which is assumed
* to be non-negative.
*/
__isl_give pet_expr *PetScan::extract_expr(const llvm::APInt &val)
{
return pet_expr_new_int(extract_unsigned(ctx, val));
}
/* Return the number of bits needed to represent the type of "decl",
* if it is an integer type. Otherwise return 0.
* If qt is signed then return the opposite of the number of bits.
*/
static int get_type_size(ValueDecl *decl)
{
return pet_clang_get_type_size(decl->getType(), decl->getASTContext());
}
/* Bound parameter "pos" of "set" to the possible values of "decl".
*/
static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
unsigned pos, ValueDecl *decl)
{
int type_size;
isl_ctx *ctx;
isl_val *bound;
ctx = isl_set_get_ctx(set);
type_size = get_type_size(decl);
if (type_size == 0)
isl_die(ctx, isl_error_invalid, "not an integer type",
return isl_set_free(set));
if (type_size > 0) {
set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
bound = isl_val_int_from_ui(ctx, type_size);
bound = isl_val_2exp(bound);
bound = isl_val_sub_ui(bound, 1);
set = isl_set_upper_bound_val(set, isl_dim_param, pos, bound);
} else {
bound = isl_val_int_from_ui(ctx, -type_size - 1);
bound = isl_val_2exp(bound);
bound = isl_val_sub_ui(bound, 1);
set = isl_set_upper_bound_val(set, isl_dim_param, pos,
isl_val_copy(bound));
bound = isl_val_neg(bound);
bound = isl_val_sub_ui(bound, 1);
set = isl_set_lower_bound_val(set, isl_dim_param, pos, bound);
}
return set;
}
__isl_give pet_expr *PetScan::extract_index_expr(ImplicitCastExpr *expr)
{
return extract_index_expr(expr->getSubExpr());
}
/* Construct a pet_expr representing an index expression for an access
* to the variable referenced by "expr".
*
* If "expr" references an enum constant, then return an integer expression
* instead, representing the value of the enum constant.
*/
__isl_give pet_expr *PetScan::extract_index_expr(DeclRefExpr *expr)
{
return extract_index_expr(expr->getDecl());
}
/* Construct a pet_expr representing an index expression for an access
* to the variable "decl".
*
* If "decl" is an enum constant, then we return an integer expression
* instead, representing the value of the enum constant.
*/
__isl_give pet_expr *PetScan::extract_index_expr(ValueDecl *decl)
{
isl_id *id;
if (isa<EnumConstantDecl>(decl))
return extract_expr(cast<EnumConstantDecl>(decl));
id = pet_id_from_decl(ctx, decl);
return pet_id_create_index_expr(id);
}
/* Construct a pet_expr representing the index expression "expr"
* Return NULL on error.
*
* If "expr" is a reference to an enum constant, then return
* an integer expression instead, representing the value of the enum constant.
*/
__isl_give pet_expr *PetScan::extract_index_expr(Expr *expr)
{
switch (expr->getStmtClass()) {
case Stmt::ImplicitCastExprClass:
return extract_index_expr(cast<ImplicitCastExpr>(expr));
case Stmt::DeclRefExprClass:
return extract_index_expr(cast<DeclRefExpr>(expr));
case Stmt::ArraySubscriptExprClass:
return extract_index_expr(cast<ArraySubscriptExpr>(expr));
case Stmt::IntegerLiteralClass:
return extract_expr(cast<IntegerLiteral>(expr));
case Stmt::MemberExprClass:
return extract_index_expr(cast<MemberExpr>(expr));
default:
unsupported(expr);
}
return NULL;
}
/* Extract an index expression from the given array subscript expression.
*
* We first extract an index expression from the base.
* This will result in an index expression with a range that corresponds
* to the earlier indices.
* We then extract the current index and let
* pet_expr_access_subscript combine the two.
*/
__isl_give pet_expr *PetScan::extract_index_expr(ArraySubscriptExpr *expr)
{
Expr *base = expr->getBase();
Expr *idx = expr->getIdx();
pet_expr *index;
pet_expr *base_expr;
base_expr = extract_index_expr(base);
index = extract_expr(idx);
base_expr = pet_expr_access_subscript(base_expr, index);
return base_expr;
}
/* Extract an index expression from a member expression.
*
* If the base access (to the structure containing the member)
* is of the form
*
* A[..]
*
* and the member is called "f", then the member access is of
* the form
*
* A_f[A[..] -> f[]]
*
* If the member access is to an anonymous struct, then simply return
*
* A[..]
*
* If the member access in the source code is of the form
*
* A->f
*
* then it is treated as
*
* A[0].f
*/
__isl_give pet_expr *PetScan::extract_index_expr(MemberExpr *expr)
{
Expr *base = expr->getBase();
FieldDecl *field = cast<FieldDecl>(expr->getMemberDecl());
pet_expr *base_index;
isl_id *id;
base_index = extract_index_expr(base);
if (expr->isArrow()) {
pet_expr *index = pet_expr_new_int(isl_val_zero(ctx));
base_index = pet_expr_access_subscript(base_index, index);
}
if (field->isAnonymousStructOrUnion())
return base_index;
id = pet_id_from_decl(ctx, field);
return pet_expr_access_member(base_index, id);
}
/* Mark the given access pet_expr as a write.
*/
static __isl_give pet_expr *mark_write(__isl_take pet_expr *access)
{
access = pet_expr_access_set_write(access, 1);
access = pet_expr_access_set_read(access, 0);
return access;
}
/* Mark the given (read) access pet_expr as also possibly being written.
* That is, initialize the may write access relation from the may read relation
* and initialize the must write access relation to the empty relation.
*/
static __isl_give pet_expr *mark_may_write(__isl_take pet_expr *expr)
{
isl_union_map *access;
isl_union_map *empty;
access = pet_expr_access_get_dependent_access(expr,
pet_expr_access_may_read);
empty = isl_union_map_empty(isl_union_map_get_space(access));
expr = pet_expr_access_set_access(expr, pet_expr_access_may_write,
access);
expr = pet_expr_access_set_access(expr, pet_expr_access_must_write,
empty);
return expr;
}
/* Construct a pet_expr representing a unary operator expression.
*/
__isl_give pet_expr *PetScan::extract_expr(UnaryOperator *expr)
{
int type_size;
pet_expr *arg;
enum pet_op_type op;
op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
if (op == pet_op_last) {
report_unsupported_unary_operator(expr);
return NULL;
}
arg = extract_expr(expr->getSubExpr());
if (expr->isIncrementDecrementOp() &&
pet_expr_get_type(arg) == pet_expr_access) {
arg = mark_write(arg);
arg = pet_expr_access_set_read(arg, 1);
}
type_size = pet_clang_get_type_size(expr->getType(), ast_context);
return pet_expr_new_unary(type_size, op, arg);
}
/* Construct a pet_expr representing a binary operator expression.
*
* If the top level operator is an assignment and the LHS is an access,
* then we mark that access as a write. If the operator is a compound
* assignment, the access is marked as both a read and a write.
*/
__isl_give pet_expr *PetScan::extract_expr(BinaryOperator *expr)
{
int type_size;
pet_expr *lhs, *rhs;
enum pet_op_type op;
op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
if (op == pet_op_last) {
report_unsupported_binary_operator(expr);
return NULL;
}
lhs = extract_expr(expr->getLHS());
rhs = extract_expr(expr->getRHS());
if (expr->isAssignmentOp() &&
pet_expr_get_type(lhs) == pet_expr_access) {
lhs = mark_write(lhs);
if (expr->isCompoundAssignmentOp())
lhs = pet_expr_access_set_read(lhs, 1);
}
type_size = pet_clang_get_type_size(expr->getType(), ast_context);
return pet_expr_new_binary(type_size, op, lhs, rhs);
}
/* Construct a pet_tree for a variable declaration and
* add the declaration to the list of declarations
* inside the current compound statement.
*/
__isl_give pet_tree *PetScan::extract(Decl *decl)
{
VarDecl *vd;
pet_expr *lhs, *rhs;
pet_tree *tree;
if (!isa<VarDecl>(decl)) {
report_unsupported_declaration(decl);
return NULL;
}
vd = cast<VarDecl>(decl);
declarations.push_back(vd);
lhs = extract_access_expr(vd);
lhs = mark_write(lhs);
if (!vd->getInit())
tree = pet_tree_new_decl(lhs);
else {
rhs = extract_expr(vd->getInit());
tree = pet_tree_new_decl_init(lhs, rhs);
}
return tree;
}
/* Construct a pet_tree for a variable declaration statement.
* If the declaration statement declares multiple variables,
* then return a group of pet_trees, one for each declared variable.
*/
__isl_give pet_tree *PetScan::extract(DeclStmt *stmt)
{
pet_tree *tree;
unsigned n;
if (!stmt->isSingleDecl()) {
const DeclGroup &group = stmt->getDeclGroup().getDeclGroup();
n = group.size();
tree = pet_tree_new_block(ctx, 0, n);
for (unsigned i = 0; i < n; ++i) {
pet_tree *tree_i;
pet_loc *loc;
tree_i = extract(group[i]);
loc = construct_pet_loc(group[i]->getSourceRange(),
false);
tree_i = pet_tree_set_loc(tree_i, loc);
tree = pet_tree_block_add_child(tree, tree_i);
}
return tree;
}
return extract(stmt->getSingleDecl());
}
/* Construct a pet_expr representing a conditional operation.
*/
__isl_give pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
{
pet_expr *cond, *lhs, *rhs;
cond = extract_expr(expr->getCond());
lhs = extract_expr(expr->getTrueExpr());
rhs = extract_expr(expr->getFalseExpr());
return pet_expr_new_ternary(cond, lhs, rhs);
}
__isl_give pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
{
return extract_expr(expr->getSubExpr());
}
/* Construct a pet_expr representing a floating point value.
*
* If the floating point literal does not appear in a macro,
* then we use the original representation in the source code
* as the string representation. Otherwise, we use the pretty
* printer to produce a string representation.
*/
__isl_give pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
{
double d;
string s;
const LangOptions &LO = PP.getLangOpts();
SourceLocation loc = expr->getLocation();
if (!loc.isMacroID()) {
SourceManager &SM = PP.getSourceManager();
unsigned len = Lexer::MeasureTokenLength(loc, SM, LO);
s = string(SM.getCharacterData(loc), len);
} else {
llvm::raw_string_ostream S(s);
expr->printPretty(S, 0, PrintingPolicy(LO));
S.str();
}
d = expr->getValueAsApproximateDouble();
return pet_expr_new_double(ctx, d, s.c_str());
}
/* Extract an index expression from "expr" and then convert it into
* an access pet_expr.
*
* If "expr" is a reference to an enum constant, then return
* an integer expression instead, representing the value of the enum constant.
*/
__isl_give pet_expr *PetScan::extract_access_expr(Expr *expr)
{
pet_expr *index;
index = extract_index_expr(expr);
if (pet_expr_get_type(index) == pet_expr_int)
return index;
return pet_expr_access_from_index(expr->getType(), index, ast_context);
}
/* Extract an index expression from "decl" and then convert it into
* an access pet_expr.
*/
__isl_give pet_expr *PetScan::extract_access_expr(ValueDecl *decl)
{
return pet_expr_access_from_index(decl->getType(),
extract_index_expr(decl), ast_context);
}
__isl_give pet_expr *PetScan::extract_expr(ParenExpr *expr)
{
return extract_expr(expr->getSubExpr());
}
/* Extract an assume statement from the argument "expr"
* of a __builtin_assume or __pencil_assume statement.
*/
__isl_give pet_expr *PetScan::extract_assume(Expr *expr)
{
return pet_expr_new_unary(0, pet_op_assume, extract_expr(expr));
}
/* If "expr" is an address-of operator, then return its argument.
* Otherwise, return NULL.
*/
static Expr *extract_addr_of_arg(Expr *expr)
{
UnaryOperator *op;
if (expr->getStmtClass() != Stmt::UnaryOperatorClass)
return NULL;
op = cast<UnaryOperator>(expr);
if (op->getOpcode() != UO_AddrOf)
return NULL;
return op->getSubExpr();
}
/* Construct a pet_expr corresponding to the function call argument "expr".
* The argument appears in position "pos" of a call to function "fd".
*
* If we are passing along a pointer to an array element
* or an entire row or even higher dimensional slice of an array,
* then the function being called may write into the array.
*
* We assume here that if the function is declared to take a pointer
* to a const type, then the function may only perform a read
* and that otherwise, it may either perform a read or a write (or both).
* We only perform this check if "detect_writes" is set.
*/
__isl_give pet_expr *PetScan::extract_argument(FunctionDecl *fd, int pos,
Expr *expr, bool detect_writes)
{
Expr *arg;
pet_expr *res;
int is_addr = 0, is_partial = 0;
expr = pet_clang_strip_casts(expr);
arg = extract_addr_of_arg(expr);
if (arg) {
is_addr = 1;
expr = arg;
}
res = extract_expr(expr);
if (!res)
return NULL;
if (pet_clang_array_depth(expr->getType()) > 0)
is_partial = 1;
if (detect_writes && (is_addr || is_partial) &&
pet_expr_get_type(res) == pet_expr_access) {
ParmVarDecl *parm;
if (!fd->hasPrototype()) {
report_prototype_required(expr);
return pet_expr_free(res);
}
parm = fd->getParamDecl(pos);
if (!const_base(parm->getType()))
res = mark_may_write(res);
}
if (is_addr)
res = pet_expr_new_unary(0, pet_op_address_of, res);
return res;
}
/* Find the first FunctionDecl with the given name.
* "call" is the corresponding call expression and is only used
* for reporting errors.
*
* Return NULL on error.
*/
FunctionDecl *PetScan::find_decl_from_name(CallExpr *call, string name)
{
TranslationUnitDecl *tu = ast_context.getTranslationUnitDecl();
DeclContext::decl_iterator begin = tu->decls_begin();
DeclContext::decl_iterator end = tu->decls_end();
for (DeclContext::decl_iterator i = begin; i != end; ++i) {
FunctionDecl *fd = dyn_cast<FunctionDecl>(*i);
if (!fd)
continue;
if (fd->getName().str().compare(name) != 0)
continue;
if (fd->hasBody())
return fd;
report_missing_summary_function_body(call);
return NULL;
}
report_missing_summary_function(call);
return NULL;
}
/* Return the FunctionDecl for the summary function associated to the
* function called by "call".
*
* In particular, if the pencil option is set, then
* search for an annotate attribute formatted as
* "pencil_access(name)", where "name" is the name of the summary function.
*
* If no summary function was specified, then return the FunctionDecl
* that is actually being called.
*
* Return NULL on error.
*/
FunctionDecl *PetScan::get_summary_function(CallExpr *call)
{
FunctionDecl *decl = call->getDirectCallee();
if (!decl)
return NULL;
if (!options->pencil)
return decl;
specific_attr_iterator<AnnotateAttr> begin, end, i;
begin = decl->specific_attr_begin<AnnotateAttr>();
end = decl->specific_attr_end<AnnotateAttr>();
for (i = begin; i != end; ++i) {
string attr = (*i)->getAnnotation().str();
const char prefix[] = "pencil_access(";
size_t start = attr.find(prefix);
if (start == string::npos)
continue;
start += strlen(prefix);
string name = attr.substr(start, attr.find(')') - start);
return find_decl_from_name(call, name);
}
return decl;
}
/* Is "name" the name of an assume statement?
* "pencil" indicates whether pencil builtins and pragmas should be supported.
* "__builtin_assume" is always accepted.
* If "pencil" is set, then "__pencil_assume" is also accepted.
*/
static bool is_assume(int pencil, const string &name)
{
if (name == "__builtin_assume")
return true;
return pencil && name == "__pencil_assume";
}
/* Construct a pet_expr representing a function call.
*