forked from clangupc/upc2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform.cpp
1922 lines (1824 loc) · 83.9 KB
/
Transform.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 <clang/Tooling/Tooling.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Sema/Scope.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/Decl.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/Arg.h>
#include <clang/Driver/Options.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Path.h>
#include <string>
#include <cctype>
#include "../../lib/Sema/TreeTransform.h"
using namespace clang;
using namespace clang::tooling;
using llvm::APInt;
namespace {
struct is_ident_char {
typedef bool result_type;
typedef char argument_type;
bool operator()(char arg) const {
return std::isalnum(arg) || arg == '_';
}
};
std::string get_file_id(const std::string& filename) {
uint32_t seed = 0;
for(std::string::const_iterator iter = filename.begin(), end = filename.end(); iter != end; ++iter) {
seed ^= uint32_t(*iter) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
std::string as_identifier(llvm::sys::path::stem(filename));
std::replace_if(as_identifier.begin(), as_identifier.end(), std::not1(is_ident_char()), '_');
return (as_identifier + "_" + llvm::Twine(seed)).str();
}
/* Copied from DeclPrinter.cpp */
static QualType GetBaseType(QualType T) {
// FIXME: This should be on the Type class!
QualType BaseType = T;
while (!BaseType->isSpecifierType()) {
if (isa<TypedefType>(BaseType))
break;
else if (const PointerType* PTy = BaseType->getAs<PointerType>())
BaseType = PTy->getPointeeType();
else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
BaseType = BPy->getPointeeType();
else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
BaseType = ATy->getElementType();
else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
BaseType = FTy->getResultType();
else if (const VectorType *VTy = BaseType->getAs<VectorType>())
BaseType = VTy->getElementType();
else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
BaseType = RTy->getPointeeType();
else
llvm_unreachable("Unknown declarator!");
}
return BaseType;
}
static QualType getDeclType(Decl* D) {
if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
return TDD->getUnderlyingType();
if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
return VD->getType();
return QualType();
}
struct UPCRDecls {
FunctionDecl * upcr_notify;
FunctionDecl * upcr_wait;
FunctionDecl * upcr_barrier;
FunctionDecl * upcr_poll;
FunctionDecl * upcr_mythread;
FunctionDecl * upcr_threads;
FunctionDecl * upcr_hasMyAffinity_pshared;
FunctionDecl * upcr_hasMyAffinity_shared;
FunctionDecl * UPCR_BEGIN_FUNCTION;
FunctionDecl * UPCRT_STARTUP_PSHALLOC;
FunctionDecl * UPCRT_STARTUP_SHALLOC;
FunctionDecl * upcr_startup_pshalloc;
FunctionDecl * upcr_startup_shalloc;
FunctionDecl * upcr_put_pshared;
FunctionDecl * upcr_put_shared;
FunctionDecl * UPCR_GET_PSHARED;
FunctionDecl * UPCR_PUT_PSHARED;
FunctionDecl * UPCR_GET_SHARED;
FunctionDecl * UPCR_PUT_SHARED;
FunctionDecl * UPCR_ADD_SHARED;
FunctionDecl * UPCR_INC_SHARED;
FunctionDecl * UPCR_GET_PSHARED_STRICT;
FunctionDecl * UPCR_PUT_PSHARED_STRICT;
FunctionDecl * UPCR_GET_SHARED_STRICT;
FunctionDecl * UPCR_PUT_SHARED_STRICT;
FunctionDecl * UPCR_ADD_PSHAREDI;
FunctionDecl * UPCR_ADD_PSHARED1;
FunctionDecl * UPCR_INC_PSHAREDI;
FunctionDecl * UPCR_INC_PSHARED1;
FunctionDecl * UPCR_SUB_SHARED;
FunctionDecl * UPCR_SUB_PSHAREDI;
FunctionDecl * UPCR_SUB_PSHARED1;
FunctionDecl * UPCR_ISEQUAL_SHARED_SHARED;
FunctionDecl * UPCR_ISEQUAL_SHARED_PSHARED;
FunctionDecl * UPCR_ISEQUAL_PSHARED_SHARED;
FunctionDecl * UPCR_ISEQUAL_PSHARED_PSHARED;
FunctionDecl * UPCR_PSHARED_TO_LOCAL;
FunctionDecl * UPCR_SHARED_TO_LOCAL;
FunctionDecl * UPCR_ISNULL_PSHARED;
FunctionDecl * UPCR_ISNULL_SHARED;
FunctionDecl * UPCR_SHARED_TO_PSHARED;
FunctionDecl * UPCR_PSHARED_TO_SHARED;
FunctionDecl * UPCR_SHARED_RESETPHASE;
VarDecl * upcrt_forall_control;
VarDecl * upcr_null_shared;
VarDecl * upcr_null_pshared;
QualType upcr_shared_ptr_t;
QualType upcr_pshared_ptr_t;
QualType upcr_startup_shalloc_t;
QualType upcr_startup_pshalloc_t;
SourceLocation FakeLocation;
explicit UPCRDecls(ASTContext& Context) {
SourceManager& SourceMgr = Context.getSourceManager();
FakeLocation = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
// types
// Make sure that the size and alignment are correct.
QualType SharedPtrTy = Context.getPointerType(Context.getSharedType(Context.VoidTy));
upcr_shared_ptr_t = CreateTypedefType(Context, "upcr_shared_ptr_t", SharedPtrTy);
upcr_pshared_ptr_t = CreateTypedefType(Context, "upcr_pshared_ptr_t", SharedPtrTy);
upcr_startup_shalloc_t = CreateTypedefType(Context, "upcr_startup_shalloc_t");
upcr_startup_pshalloc_t = CreateTypedefType(Context, "upcr_startup_pshalloc_t");
// upcr_notify
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_notify = CreateFunction(Context, "upcr_notify", Context.VoidTy, argTypes, 2);
}
// upcr_wait
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_wait = CreateFunction(Context, "upcr_wait", Context.VoidTy, argTypes, 2);
}
// upcr_barrier
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_barrier = CreateFunction(Context, "upcr_barrier", Context.VoidTy, argTypes, 2);
}
// upcr_poll
{
upcr_poll = CreateFunction(Context, "upcr_poll", Context.VoidTy, 0, 0);
}
// upcr_mythread
{
upcr_mythread = CreateFunction(Context, "upcr_mythread", Context.IntTy, 0, 0);
}
// upcr_threads
{
upcr_threads = CreateFunction(Context, "upcr_threads", Context.IntTy, 0, 0);
}
// upcr_hasMyAffinity_pshared
{
QualType argTypes[] = { upcr_pshared_ptr_t };
upcr_hasMyAffinity_pshared = CreateFunction(Context, "upcr_hasMyAffinity_pshared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_hasMyAffinity_shared
{
QualType argTypes[] = { upcr_shared_ptr_t };
upcr_hasMyAffinity_shared = CreateFunction(Context, "upcr_hasMyAffinity_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_put_pshared
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
upcr_put_pshared = CreateFunction(Context, "upcr_put_pshared", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_put_shared
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
upcr_put_shared = CreateFunction(Context, "upcr_put_shared", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_GET_PSHARED
{
QualType argTypes[] = { Context.VoidPtrTy, upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_PSHARED = CreateFunction(Context, "UPCR_GET_PSHARED", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PUT_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT_PSHARED = CreateFunction(Context, "UPCR_PUT_PSHARED", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_GET_SHARED
{
QualType argTypes[] = { Context.VoidPtrTy, upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_SHARED = CreateFunction(Context, "UPCR_GET_SHARED", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PUT_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT_SHARED = CreateFunction(Context, "UPCR_PUT_SHARED", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_GET_PSHARED_STRICT
{
QualType argTypes[] = { Context.VoidPtrTy, upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_PSHARED_STRICT = CreateFunction(Context, "UPCR_GET_PSHARED_STRICT", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PUT_PSHARED_STRICT
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT_PSHARED_STRICT = CreateFunction(Context, "UPCR_PUT_PSHARED_STRICT", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_GET_SHARED_STRICT
{
QualType argTypes[] = { Context.VoidPtrTy, upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_SHARED_STRICT = CreateFunction(Context, "UPCR_GET_SHARED_STRICT", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PUT_SHARED_STRICT
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT_SHARED_STRICT = CreateFunction(Context, "UPCR_PUT_SHARED_STRICT", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy };
UPCR_ADD_SHARED = CreateFunction(Context, "UPCR_ADD_SHARED", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_PSHAREDI
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_ADD_PSHAREDI = CreateFunction(Context, "UPCR_ADD_PSHAREDI", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_PSHARED1
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_ADD_PSHARED1 = CreateFunction(Context, "UPCR_ADD_PSHARED1", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_SHARED
{
QualType argTypes[] = { Context.getPointerType(upcr_shared_ptr_t), Context.IntTy, Context.IntTy, Context.IntTy };
UPCR_INC_SHARED = CreateFunction(Context, "upcr_inc_shared", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_PSHAREDI
{
QualType argTypes[] = { Context.getPointerType(upcr_pshared_ptr_t), Context.IntTy, Context.IntTy };
UPCR_INC_PSHAREDI = CreateFunction(Context, "upcr_inc_psharedI", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_PSHARED1
{
QualType argTypes[] = { Context.getPointerType(upcr_pshared_ptr_t), Context.IntTy, Context.IntTy };
UPCR_INC_PSHARED1 = CreateFunction(Context, "upcr_inc_pshared1", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_SUB_SHARED = CreateFunction(Context, "UPCR_SUB_SHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_PSHAREDI
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t, Context.IntTy };
UPCR_SUB_PSHAREDI = CreateFunction(Context, "UPCR_SUB_PSHAREDI", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_PSHARED1
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t, Context.IntTy };
UPCR_SUB_PSHARED1 = CreateFunction(Context, "UPCR_SUB_PSHARED1", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_SHARED_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_shared_ptr_t };
UPCR_ISEQUAL_SHARED_SHARED = CreateFunction(Context, "UPCR_ISEQUAL_SHARED_SHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_SHARED_PSHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_pshared_ptr_t };
UPCR_ISEQUAL_SHARED_PSHARED = CreateFunction(Context, "UPCR_ISEQUAL_SHARED_PSHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_PSHARED_SHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_shared_ptr_t };
UPCR_ISEQUAL_PSHARED_SHARED = CreateFunction(Context, "UPCR_ISEQUAL_PSHARED_SHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_PSHARED_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t };
UPCR_ISEQUAL_PSHARED_PSHARED = CreateFunction(Context, "UPCR_ISEQUAL_PSHARED_PSHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_TO_LOCAL
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_TO_LOCAL = CreateFunction(Context, "UPCR_SHARED_TO_LOCAL", Context.VoidPtrTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PSHARED_TO_LOCAL
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_PSHARED_TO_LOCAL = CreateFunction(Context, "UPCR_PSHARED_TO_LOCAL", Context.VoidPtrTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISNULL_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_ISNULL_SHARED = CreateFunction(Context, "UPCR_ISNULL_SHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISNULL_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_ISNULL_PSHARED = CreateFunction(Context, "UPCR_ISNULL_PSHARED", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_TO_PSHARED
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_TO_PSHARED = CreateFunction(Context, "UPCR_SHARED_TO_PSHARED", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PSHARED_TO_SHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_PSHARED_TO_SHARED = CreateFunction(Context, "UPCR_PSHARED_TO_SHARED", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_RESETPHASE
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_RESETPHASE = CreateFunction(Context, "UPCR_SHARED_RESETPHASE", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_BEGIN_FUNCTION
{
UPCR_BEGIN_FUNCTION = CreateFunction(Context, "UPCR_BEGIN_FUNCTION", Context.VoidTy, NULL, 0);
}
// UPCRT_STARTUP_PSHALLOC
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy, Context.IntTy, Context. getPointerType(Context.getConstType(Context.CharTy)) };
UPCRT_STARTUP_PSHALLOC = CreateFunction(Context, "UPCRT_STARTUP_PSHALLOC", upcr_startup_pshalloc_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCRT_STARTUP_SHALLOC
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy, Context.IntTy, Context. getPointerType(Context.getConstType(Context.CharTy)) };
UPCRT_STARTUP_SHALLOC = CreateFunction(Context, "UPCRT_STARTUP_SHALLOC", upcr_startup_shalloc_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_startup_pshalloc
{
QualType argTypes[] = { Context.getPointerType(upcr_startup_pshalloc_t), Context.IntTy };
upcr_startup_pshalloc = CreateFunction(Context, "upcr_startup_pshalloc", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_startup_shalloc
{
QualType argTypes[] = { Context.getPointerType(upcr_startup_shalloc_t), Context.IntTy };
upcr_startup_shalloc = CreateFunction(Context, "upcr_startup_shalloc", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcrt_forall_control
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcrt_forall_control = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcrt_forall_control"), Context.IntTy, Context.getTrivialTypeSourceInfo(Context.IntTy), SC_Extern);
}
// upcr_null_shared
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcr_null_shared = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcr_null_shared"), upcr_shared_ptr_t, Context.getTrivialTypeSourceInfo(upcr_shared_ptr_t), SC_Extern);
}
// upcr_null_pshared
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcr_null_pshared = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcr_null_pshared"), upcr_pshared_ptr_t, Context.getTrivialTypeSourceInfo(upcr_pshared_ptr_t), SC_Extern);
}
}
FunctionDecl *CreateFunction(ASTContext& Context, StringRef name, QualType RetType, QualType * argTypes, int numArgs) {
DeclContext *DC = Context.getTranslationUnitDecl();
QualType Ty = Context.getFunctionType(RetType, llvm::makeArrayRef(argTypes, numArgs), FunctionProtoType::ExtProtoInfo());
FunctionDecl *Result = FunctionDecl::Create(Context, DC, FakeLocation, FakeLocation, DeclarationName(&Context.Idents.get(name)), Ty, Context.getTrivialTypeSourceInfo(Ty), SC_Extern);
llvm::SmallVector<ParmVarDecl *, 4> Params;
for(int i = 0; i < numArgs; ++i) {
Params.push_back(ParmVarDecl::Create(Context, Result, SourceLocation(), SourceLocation(), 0, argTypes[i], 0, SC_None, 0));
Params[i]->setScopeInfo(0, i);
}
Result->setParams(Params);
return Result;
}
QualType CreateTypedefType(ASTContext& Context, StringRef name) {
return CreateTypedefType(Context, name, Context.IntTy);
}
QualType CreateTypedefType(ASTContext& Context, StringRef name, QualType BaseTy) {
DeclContext *DC = Context.getTranslationUnitDecl();
TypedefDecl *Typedef = TypedefDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get(name), Context.getTrivialTypeSourceInfo(BaseTy));
return Context.getTypedefType(Typedef);
}
};
class SubstituteType : public clang::TreeTransform<SubstituteType> {
typedef TreeTransform<SubstituteType> TreeTransformS;
public:
SubstituteType(Sema &S, QualType F, QualType T) : TreeTransformS(S), From(F), To(T) {}
TypeSourceInfo * TransformType(TypeSourceInfo *TI) {
if(SemaRef.Context.hasSameType(TI->getType(), From)) {
return SemaRef.Context.getTrivialTypeSourceInfo(To);
} else {
return TreeTransformS::TransformType(TI);
}
}
using TreeTransformS::TransformType;
private:
QualType From;
QualType To;
};
class RemoveUPCTransform : public clang::TreeTransform<RemoveUPCTransform> {
typedef TreeTransform<RemoveUPCTransform> TreeTransformUPC;
public:
RemoveUPCTransform(Sema& S, UPCRDecls* D, const std::string& fileid)
: TreeTransformUPC(S), AnonRecordID(0), Decls(D), FileString(fileid) {
UPCSystemHeaders.insert("upc.h");
UPCSystemHeaders.insert("upc_bits.h");
UPCSystemHeaders.insert("upc_castable.h");
UPCSystemHeaders.insert("upc_castable_bits.h");
UPCSystemHeaders.insert("upc_collective.h");
UPCSystemHeaders.insert("upc_collective_bits.h");
UPCSystemHeaders.insert("upc_io.h");
UPCSystemHeaders.insert("upc_io_bits.h");
UPCSystemHeaders.insert("upc_relaxed.h");
UPCSystemHeaders.insert("upc_strict.h");
UPCSystemHeaders.insert("upc_tick.h");
UPCSystemHeaders.insert("bupc_extensions.h");
UPCSystemHeaders.insert("bupc_atomics.h");
UPCSystemHeaders.insert("pupc.h");
UPCHeaderRenames["upc_types.h"] = "upcr_preinclude/upc_types.h";
}
bool AlwaysRebuild() { return true; }
ExprResult BuildParens(Expr * E) {
return SemaRef.ActOnParenExpr(SourceLocation(), SourceLocation(), E);
}
ExprResult BuildComma(Expr * LHS, Expr * RHS) {
return SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Comma, LHS, RHS);
}
// TreeTransform ignores AlwayRebuild for literals
ExprResult TransformIntegerLiteral(IntegerLiteral *E) {
return IntegerLiteral::Create(SemaRef.Context, E->getValue(), E->getType(), E->getLocation());
}
ExprResult BuildUPCRCall(FunctionDecl *FD, std::vector<Expr*>& args) {
ExprResult Fn = SemaRef.BuildDeclRefExpr(FD, FD->getType(), VK_LValue, SourceLocation());
return SemaRef.BuildResolvedCallExpr(Fn.get(), FD, SourceLocation(), args, SourceLocation());
}
ExprResult BuildUPCRDeclRef(VarDecl *VD) {
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation());
}
Expr * CreateSimpleDeclRef(VarDecl *VD) {
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation()).get();
}
int AnonRecordID;
IdentifierInfo *getRecordDeclName(IdentifierInfo * OrigName) {
return OrigName;
}
struct ArrayDimensionT {
ArrayDimensionT(ASTContext& Context) :
ArrayDimension(Context.getTypeSize(Context.getSizeType()), 1),
HasThread(false),
ElementSize(0),
E(NULL)
{}
llvm::APInt ArrayDimension;
bool HasThread;
int ElementSize;
Expr *E;
};
ArrayDimensionT GetArrayDimension(QualType Ty) {
ArrayDimensionT Result(SemaRef.Context);
QualType ElemTy = Ty.getCanonicalType();
while(const ArrayType *AT = dyn_cast<ArrayType>(ElemTy.getTypePtr())) {
if(const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Result.ArrayDimension *= CAT->getSize();
} else if(const UPCThreadArrayType *TAT = dyn_cast<UPCThreadArrayType>(AT)) {
if(TAT->getThread()) {
Result.HasThread = true;
}
Result.ArrayDimension *= TAT->getSize();
} else if(const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
Expr *Val = BuildParens(VAT->getSizeExpr()).get();
if(Result.E) {
Result.E = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Result.E, Val).get();
} else {
Result.E = Val;
}
} else if(isa<IncompleteArrayType>(AT)) {
Result.ArrayDimension = 0;
} else {
assert(!"Other array types should not syntax check");
}
ElemTy = AT->getElementType();
}
Result.ElementSize = SemaRef.Context.getTypeSizeInChars(ElemTy).getQuantity();
return Result;
}
Expr * MaybeAddParensForMultiply(Expr * E) {
if(isa<ParenExpr>(E) || isa<IntegerLiteral>(E) ||
isa<CallExpr>(E))
return E;
else
return BuildParens(E).get();
}
ExprResult MaybeAdjustForArray(const ArrayDimensionT & Dims, Expr * E, BinaryOperatorKind Op) {
if(Dims.ArrayDimension == 1 && !Dims.E && !Dims.HasThread) {
return SemaRef.Owned(E);
} else {
Expr *Dimension = IntegerLiteral::Create(SemaRef.Context, Dims.ArrayDimension, SemaRef.Context.getSizeType(), SourceLocation());
if(Dims.HasThread) {
std::vector<Expr*> args;
Expr *Threads = BuildUPCRCall(Decls->upcr_threads, args).get();
Dimension = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Dimension, Threads).get();
}
if(Dims.E) {
Dimension = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Dimension, Dims.E).get();
}
if(Dims.HasThread || Dims.E) {
Dimension = BuildParens(Dimension).get();
}
return BuildParens(SemaRef.CreateBuiltinBinOp(SourceLocation(), Op, MaybeAddParensForMultiply(E), Dimension).get());
}
}
StmtResult TransformUPCNotifyStmt(UPCNotifyStmt *S) {
Expr *ID = S->getIdValue();
std::vector<Expr*> args;
if(ID) {
args.push_back(TransformExpr(ID).get());
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
} else {
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 1), SemaRef.Context.IntTy, SourceLocation()));
}
Stmt *result = BuildUPCRCall(Decls->upcr_notify, args).get();
return SemaRef.Owned(result);
}
StmtResult TransformUPCWaitStmt(UPCWaitStmt *S) {
Expr *ID = S->getIdValue();
std::vector<Expr*> args;
if(ID) {
args.push_back(TransformExpr(ID).get());
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
} else {
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 1), SemaRef.Context.IntTy, SourceLocation()));
}
Stmt *result = BuildUPCRCall(Decls->upcr_wait, args).get();
return SemaRef.Owned(result);
}
StmtResult TransformUPCBarrierStmt(UPCBarrierStmt *S) {
Expr *ID = S->getIdValue();
std::vector<Expr*> args;
if(ID) {
args.push_back(TransformExpr(ID).get());
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
} else {
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 0), SemaRef.Context.IntTy, SourceLocation()));
args.push_back(IntegerLiteral::Create(
SemaRef.Context, APInt(32, 1), SemaRef.Context.IntTy, SourceLocation()));
}
Stmt *result = BuildUPCRCall(Decls->upcr_barrier, args).get();
return SemaRef.Owned(result);
}
StmtResult TransformUPCFenceStmt(UPCFenceStmt *S) {
std::vector<Expr*> args;
Stmt *result = BuildUPCRCall(Decls->upcr_poll, args).get();
return SemaRef.Owned(result);
}
ExprResult TransformInitializer(Expr *Init, bool CXXDirectInit) {
if(!Init)
return SemaRef.Owned(Init);
// Have to handle this separately, as TreeTransform
// strips off ImplicitCastExprs in TransformInitializer.
if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init)) {
if((ICE->getCastKind() == CK_LValueToRValue && ICE->getSubExpr()->getType().getQualifiers().hasShared()) ||
isPointerToShared(ICE->getSubExpr()->getType())) {
return TransformExpr(ICE);
} else {
ExprResult UPCCast = MaybeTransformUPCRCast(ICE);
if(!UPCCast.isInvalid())
return UPCCast;
return TransformInitializer(ICE->getSubExpr(), CXXDirectInit);
}
}
return TreeTransformUPC::TransformInitializer(Init, CXXDirectInit);
}
ExprResult TransformCStyleCastExpr(CStyleCastExpr *E) {
ExprResult UPCCast = MaybeTransformUPCRCast(E);
if(!UPCCast.isInvalid()) {
return UPCCast;
} else {
// The default transform strips off implicit casts
TypeSourceInfo *Type = TransformType(E->getTypeInfoAsWritten());
if (!Type)
return ExprError();
ExprResult SubExpr = TransformExpr(E->getSubExpr());
if (SubExpr.isInvalid())
return ExprError();
return RebuildCStyleCastExpr(E->getLParenLoc(),
Type,
E->getRParenLoc(),
SubExpr.get());
}
}
ExprResult TransformImplicitCastExpr(ImplicitCastExpr *E) {
if(E->getCastKind() == CK_LValueToRValue && E->getSubExpr()->getType().getQualifiers().hasShared()) {
return BuildUPCRLoad(TransformExpr(E->getSubExpr()).get(), E->getType().getUnqualifiedType(), E->getSubExpr()->getType());
} else {
ExprResult UPCCast = MaybeTransformUPCRCast(E);
if(!UPCCast.isInvalid()) {
return UPCCast;
}
// We can't use the default transform, because it
// strips off all implicit casts. We may need to
// process the subexpression
return TransformExpr(E->getSubExpr());
}
}
bool isPointerToShared(QualType Ty) {
if(const PointerType * PTy = Ty->getAs<PointerType>()) {
return PTy->getPointeeType().getQualifiers().hasShared();
} else {
return false;
}
}
IntegerLiteral *CreateInteger(QualType Ty, int Value) {
return IntegerLiteral::Create(SemaRef.Context, APInt(SemaRef.Context.getTypeSize(Ty), Value), Ty, SourceLocation());
}
ExprResult BuildUPCRLoad(Expr * E, QualType ResultType, QualType Ty) {
std::pair<Expr *, Expr *> LoadAndVar = BuildUPCRLoadParts(E, ResultType, Ty);
return BuildParens(BuildComma(LoadAndVar.first, LoadAndVar.second).get());
}
// Returns a pair containing the load stmt and a declrefexpr to the
// temporary variable created.
std::pair<Expr *, Expr *> BuildUPCRLoadParts(Expr * E, QualType ResultType, QualType Ty) {
int SizeTypeSize = SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType());
Qualifiers Quals = Ty.getQualifiers();
bool Phaseless = isPhaseless(Ty);
bool Strict = Quals.hasStrict();
// Select the correct function to call
FunctionDecl *Accessor;
if(Phaseless) {
if(Strict) {
Accessor = Decls->UPCR_GET_PSHARED_STRICT;
} else {
Accessor = Decls->UPCR_GET_PSHARED;
}
} else {
if(Strict) {
Accessor = Decls->UPCR_GET_SHARED_STRICT;
} else {
Accessor = Decls->UPCR_GET_SHARED;
}
}
VarDecl *TmpVar = CreateTmpVar(TransformType(ResultType));
// FIXME: Handle other layout qualifiers
std::vector<Expr*> args;
args.push_back(SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, CreateSimpleDeclRef(TmpVar)).get());
args.push_back(E);
// offset
args.push_back(IntegerLiteral::Create(SemaRef.Context, APInt(SizeTypeSize, 0), SemaRef.Context.getSizeType(), SourceLocation()));
// size
args.push_back(IntegerLiteral::Create(SemaRef.Context, APInt(SizeTypeSize, SemaRef.Context.getTypeSizeInChars(ResultType).getQuantity()), SemaRef.Context.getSizeType(), SourceLocation()));
Expr *Load = BuildUPCRCall(Accessor, args).get();
return std::make_pair(Load, CreateSimpleDeclRef(TmpVar));
}
ExprResult MaybeTransformUPCRCast(CastExpr *E) {
if(E->getCastKind() == CK_UPCSharedToLocal) {
bool Phaseless = isPhaseless(E->getSubExpr()->getType()->getAs<PointerType>()->getPointeeType());
FunctionDecl *Accessor = Phaseless? Decls->UPCR_PSHARED_TO_LOCAL : Decls->UPCR_SHARED_TO_LOCAL;
std::vector<Expr*> args;
args.push_back(TransformExpr(E->getSubExpr()).get());
ExprResult Result = BuildUPCRCall(Accessor, args);
TypeSourceInfo *Ty = SemaRef.Context.getTrivialTypeSourceInfo(TransformType(E->getType()));
return SemaRef.BuildCStyleCastExpr(SourceLocation(), Ty, SourceLocation(), Result.get());
} else if(E->getCastKind() == CK_NullToPointer && isPointerToShared(E->getType())) {
bool Phaseless = isPhaseless(E->getType()->getAs<PointerType>()->getPointeeType());
return BuildUPCRDeclRef(Phaseless? Decls->upcr_null_pshared : Decls->upcr_null_shared);
} else if((E->getCastKind() == CK_BitCast ||
E->getCastKind() == CK_UPCBitCastZeroPhase) &&
isPointerToShared(E->getType())) {
QualType DstPointee = E->getType()->getAs<PointerType>()->getPointeeType();
QualType SrcPointee = E->getSubExpr()->getType()->getAs<PointerType>()->getPointeeType();
FunctionDecl *CastFn = 0;
if(isPhaseless(DstPointee) && !isPhaseless(SrcPointee)) {
CastFn = Decls->UPCR_SHARED_TO_PSHARED;
} else if(!isPhaseless(DstPointee) && isPhaseless(SrcPointee)) {
CastFn = Decls->UPCR_PSHARED_TO_SHARED;
} else if(!isPhaseless(DstPointee) && !isPhaseless(SrcPointee) &&
E->getCastKind() == CK_UPCBitCastZeroPhase) {
CastFn = Decls->UPCR_SHARED_RESETPHASE;
}
if(CastFn) {
std::vector<Expr *> args;
args.push_back(TransformExpr(E->getSubExpr()).get());
return BuildUPCRCall(CastFn, args);
} else {
return TransformExpr(E->getSubExpr());
}
}
return ExprError();
}
ExprResult BuildUPCRStore(Expr * LHS, Expr * RHS, QualType Ty, bool ReturnValue = true) {
int SizeTypeSize = SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType());
Qualifiers Quals = Ty.getQualifiers();
bool Phaseless = isPhaseless(Ty);
bool Strict = Quals.hasStrict();
// Select the correct function to call
FunctionDecl *Accessor;
if(Phaseless) {
if(Strict) {
Accessor = Decls->UPCR_PUT_PSHARED_STRICT;
} else {
Accessor = Decls->UPCR_PUT_PSHARED;
}
} else {
if(Strict) {
Accessor = Decls->UPCR_PUT_SHARED_STRICT;
} else {
Accessor = Decls->UPCR_PUT_SHARED;
}
}
VarDecl *TmpVar = CreateTmpVar(TransformType(Ty).getUnqualifiedType());
Expr *SetTmp = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, CreateSimpleDeclRef(TmpVar), RHS).get();
std::vector<Expr*> args;
args.push_back(LHS);
// offset
args.push_back(IntegerLiteral::Create(SemaRef.Context, APInt(SizeTypeSize, 0), SemaRef.Context.getSizeType(), SourceLocation()));
args.push_back(SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, CreateSimpleDeclRef(TmpVar)).get());
// size
args.push_back(IntegerLiteral::Create(SemaRef.Context, APInt(SizeTypeSize, SemaRef.Context.getTypeSizeInChars(Ty).getQuantity()), SemaRef.Context.getSizeType(), SourceLocation()));
Expr *Store = BuildUPCRCall(Accessor, args).get();
Expr *CommaRHS = Store;
if(ReturnValue) {
CommaRHS = BuildComma(Store, CreateSimpleDeclRef(TmpVar)).get();
}
return BuildParens(BuildComma(SetTmp, CommaRHS).get());
}
ExprResult CreateUPCPointerArithmetic(Expr *Ptr, Expr *IntVal, QualType PtrTy) {
QualType PointeeType = PtrTy->getAs<PointerType>()->getPointeeType();
ArrayDimensionT Dims = GetArrayDimension(PointeeType);
int ElementSize = Dims.ElementSize;
IntVal = MaybeAdjustForArray(Dims, IntVal, BO_Mul).get();
std::vector<Expr*> args;
args.push_back(Ptr);
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), ElementSize));
args.push_back(IntVal);
int LayoutQualifier = PointeeType.getQualifiers().getLayoutQualifier();
if(LayoutQualifier == 0) {
return BuildUPCRCall(Decls->UPCR_ADD_PSHAREDI, args);
} else if(isPhaseless(PointeeType) && LayoutQualifier == 1) {
return BuildUPCRCall(Decls->UPCR_ADD_PSHARED1, args);
} else {
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), LayoutQualifier));
return BuildUPCRCall(Decls->UPCR_ADD_SHARED, args);
}
}
ExprResult CreateArithmeticExpr(Expr *LHS, Expr *RHS, QualType LHSTy, BinaryOperatorKind Op) {
if(isPointerToShared(LHSTy)) {
if(Op == BO_Sub) {
RHS = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_Minus, RHS).get();
}
return CreateUPCPointerArithmetic(LHS, RHS, LHSTy);
} else {
return SemaRef.CreateBuiltinBinOp(SourceLocation(), Op, LHS, RHS);
}
}
ExprResult TransformUnaryOperator(UnaryOperator *E) {
QualType ArgType = E->getSubExpr()->getType();
if((E->getOpcode() == UO_Deref && isPointerToShared(ArgType)) ||
(E->getOpcode() == UO_AddrOf && isPointerToShared(E->getType()))) {
// Strip off * and &. shared lvalues and pointers-to-shared
// have the same representation.
return TransformExpr(E->getSubExpr());
} else if(ArgType.getQualifiers().hasShared() && E->isIncrementDecrementOp()) {
bool Phaseless = isPhaseless(ArgType);
QualType PtrType = Phaseless? Decls->upcr_pshared_ptr_t : Decls->upcr_shared_ptr_t;
VarDecl * TmpPtrDecl = CreateTmpVar(PtrType);
Expr * TmpPtr = SemaRef.BuildDeclRefExpr(TmpPtrDecl, PtrType, VK_LValue, SourceLocation()).get();
Expr * SaveArg = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpPtr, BuildParens(TransformExpr(E->getSubExpr()).get()).get()).get();
std::pair<Expr *, Expr *> Load = BuildUPCRLoadParts(TmpPtr, ArgType.getUnqualifiedType(), ArgType);
Expr * LoadExpr = Load.first;
Expr * LoadVar = Load.second;
Expr * NewVal = CreateArithmeticExpr(LoadVar, CreateInteger(SemaRef.Context.IntTy, 1), ArgType, E->isIncrementOp()?BO_Add:BO_Sub).get();
if(E->isPrefix()) {
Expr * Result = BuildUPCRStore(TmpPtr, NewVal, ArgType).get();
return BuildParens(BuildComma(SaveArg, BuildComma(LoadExpr, Result).get()).get());
} else {
Expr * Result = BuildUPCRStore(TmpPtr, NewVal, ArgType, false).get();
return BuildParens(BuildComma(SaveArg, BuildComma(LoadExpr, BuildComma(Result, LoadVar).get()).get()).get());
}
} else if(isPointerToShared(ArgType) && E->isIncrementDecrementOp()) {
QualType TmpPtrType = SemaRef.Context.getPointerType(TransformType(ArgType));
VarDecl * TmpPtrDecl = CreateTmpVar(TmpPtrType);
Expr * TmpPtr = CreateSimpleDeclRef(TmpPtrDecl);
Expr * Setup = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpPtr, SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, TransformExpr(E->getSubExpr()).get()).get()).get();
Expr * Access = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_Deref, TmpPtr).get();
Expr * Saved;
Expr * TmpVal;
if(E->isPostfix()) {
// Save the old value
VarDecl * TmpValDecl = CreateTmpVar(TransformType(ArgType).getUnqualifiedType());
TmpVal = CreateSimpleDeclRef(TmpValDecl);
Saved = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpVal, Access).get();
}
Expr * NewVal = CreateArithmeticExpr(Access, CreateInteger(SemaRef.Context.IntTy, 1),
ArgType, E->isIncrementOp()?BO_Add:BO_Sub).get();
Expr * Operation = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, Access, NewVal).get();
if(E->isPrefix()) {
return BuildParens(BuildComma(Setup, BuildComma(Operation, Access).get()).get());
} else {
return BuildParens(BuildComma(Setup, BuildComma(Saved, BuildComma(Operation, TmpVal).get()).get()).get());
}
} else if(isPointerToShared(ArgType) && E->getOpcode() == UO_LNot) {
bool Phaseless = isPhaseless(ArgType->getAs<PointerType>()->getPointeeType());
std::vector<Expr*> args;
args.push_back(TransformExpr(E->getSubExpr()).get());
return BuildUPCRCall(Phaseless?Decls->UPCR_ISNULL_PSHARED:Decls->UPCR_ISNULL_SHARED, args);
} else {
return TreeTransformUPC::TransformUnaryOperator(E);
}
}
ExprResult TransformBinaryOperator(BinaryOperator *E) {
// Catch assignment to shared variables
if(E->getOpcode() == BO_Assign && E->getLHS()->getType().getQualifiers().hasShared()) {
Expr *LHS = TransformExpr(E->getLHS()).get();
Expr *RHS = TransformExpr(E->getRHS()).get();
return BuildUPCRStore(LHS, RHS, E->getLHS()->getType());
} else {
Expr *LHS = E->getLHS();
Expr *RHS = E->getRHS();
bool LHSIsShared = isPointerToShared(E->getLHS()->getType());
bool RHSIsShared = isPointerToShared(E->getRHS()->getType());
if(LHSIsShared && RHSIsShared && E->getOpcode() == BO_Sub) {
// Pointer - Pointer
ExprResult Result;
QualType PointeeType = LHS->getType()->getAs<PointerType>()->getPointeeType();
ArrayDimensionT Dims = GetArrayDimension(PointeeType);
int ElementSize = Dims.ElementSize;
std::vector<Expr*> args;
args.push_back(TransformExpr(LHS).get());
args.push_back(TransformExpr(RHS).get());
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), ElementSize));
int LayoutQualifier = PointeeType.getQualifiers().getLayoutQualifier();
if(LayoutQualifier == 0) {
Result = BuildUPCRCall(Decls->UPCR_SUB_PSHAREDI, args);
} else if(isPhaseless(PointeeType) && LayoutQualifier == 1) {
Result = BuildUPCRCall(Decls->UPCR_SUB_PSHARED1, args);
} else {
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), LayoutQualifier));
Result = BuildUPCRCall(Decls->UPCR_SUB_SHARED, args);
}
return MaybeAdjustForArray(Dims, Result.get(), BO_Div);
} else if((LHSIsShared || RHSIsShared) && (E->getOpcode() == BO_Add || E->getOpcode() == BO_Sub)) {
// Pointer +/- Integer
if(RHSIsShared) { std::swap(LHS, RHS); }
Expr *IntVal = TransformExpr(RHS).get();
if(E->getOpcode() == BO_Sub) {
IntVal = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_Minus, IntVal).get();
}
return CreateUPCPointerArithmetic(TransformExpr(LHS).get(), IntVal, LHS->getType());
} else if(LHSIsShared && RHSIsShared && (E->getOpcode() == BO_EQ || E->getOpcode() == BO_NE)) {
// Equality Comparison
std::vector<Expr*> args;
args.push_back(TransformExpr(LHS).get());
args.push_back(TransformExpr(RHS).get());
QualType LHSPointee = LHS->getType()->getAs<PointerType>()->getPointeeType();
QualType RHSPointee = RHS->getType()->getAs<PointerType>()->getPointeeType();
ExprResult Result;
if(isPhaseless(LHSPointee) && isPhaseless(RHSPointee)) {
Result = BuildUPCRCall(Decls->UPCR_ISEQUAL_PSHARED_PSHARED, args);
} else if(isPhaseless(LHSPointee) && !isPhaseless(RHSPointee)) {
Result = BuildUPCRCall(Decls->UPCR_ISEQUAL_PSHARED_SHARED, args);
} else if(!isPhaseless(LHSPointee) && isPhaseless(RHSPointee)) {
Result = BuildUPCRCall(Decls->UPCR_ISEQUAL_SHARED_PSHARED, args);
} else if(!isPhaseless(LHSPointee) && !isPhaseless(RHSPointee)) {
Result = BuildUPCRCall(Decls->UPCR_ISEQUAL_SHARED_SHARED, args);
}
if(E->getOpcode() == BO_NE) {
Result = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_LNot, Result.get());
}
return Result;
} else if(LHSIsShared && RHSIsShared && (E->getOpcode() == BO_LT || E->getOpcode() == BO_LE || E->getOpcode() == BO_GT || E->getOpcode() == BO_GE)) {
// Relational Comparison
QualType PointeeType = LHS->getType()->getAs<PointerType>()->getPointeeType();
int ElementSize = SemaRef.Context.getTypeSizeInChars(PointeeType).getQuantity();
std::vector<Expr*> args;
args.push_back(TransformExpr(LHS).get());
args.push_back(TransformExpr(RHS).get());
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), ElementSize));
int LayoutQualifier = PointeeType.getQualifiers().getLayoutQualifier();
Expr *Diff;
if(LayoutQualifier == 0) {
Diff = BuildUPCRCall(Decls->UPCR_SUB_PSHAREDI, args).get();
} else if(LayoutQualifier == 1) {
Diff = BuildUPCRCall(Decls->UPCR_SUB_PSHARED1, args).get();
} else {
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), LayoutQualifier));
Diff = BuildUPCRCall(Decls->UPCR_SUB_SHARED, args).get();
}
return SemaRef.CreateBuiltinBinOp(SourceLocation(), E->getOpcode(), Diff, CreateInteger(SemaRef.Context.IntTy, 0));
}
}
// Otherwise use the default transform
return TreeTransformUPC::TransformBinaryOperator(E);
}
ExprResult TransformCompoundAssignOperator(CompoundAssignOperator *E) {
if(E->getLHS()->getType().getQualifiers().hasShared()) {
QualType Ty = E->getLHS()->getType();
bool Phaseless = isPhaseless(Ty);
QualType PtrType = Phaseless? Decls->upcr_pshared_ptr_t : Decls->upcr_shared_ptr_t;
VarDecl * TmpPtrDecl = CreateTmpVar(PtrType);
BinaryOperatorKind Opc = BinaryOperator::getOpForCompoundAssignment(E->getOpcode());
Expr * TmpPtr = SemaRef.BuildDeclRefExpr(TmpPtrDecl, PtrType, VK_LValue, SourceLocation()).get();
Expr * SaveLHS = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpPtr, BuildParens(TransformExpr(E->getLHS()).get()).get()).get();
Expr * RHS = BuildParens(TransformExpr(E->getRHS()).get()).get();
Expr * LHSVal = BuildUPCRLoad(TmpPtr, Ty.getUnqualifiedType(), Ty).get();
Expr * OpResult = CreateArithmeticExpr(LHSVal, RHS, Ty, Opc).get();
Expr * Result = BuildUPCRStore(TmpPtr, OpResult, Ty).get();
return BuildParens(BuildComma(SaveLHS, Result).get());
} else if(isPointerToShared(E->getLHS()->getType())) {
QualType Ty = E->getLHS()->getType();
BinaryOperatorKind Opc = BinaryOperator::getOpForCompoundAssignment(E->getOpcode());
QualType PtrType = SemaRef.Context.getPointerType(TransformType(Ty));
VarDecl * TmpPtrDecl = CreateTmpVar(PtrType);
Expr * TmpPtr = CreateSimpleDeclRef(TmpPtrDecl);
Expr * LHSPtr = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, BuildParens(TransformExpr(E->getLHS()).get()).get()).get();
Expr * SetPtr = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpPtr,
LHSPtr).get();
Expr * TmpVar = SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_Deref, TmpPtr).get();
Expr * OpResult = CreateArithmeticExpr(TmpVar, TransformExpr(E->getRHS()).get(), Ty, Opc).get();
Expr * Result = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, TmpVar, OpResult).get();
return BuildParens(BuildComma(SetPtr, Result).get());
} else {
return TreeTransformUPC::TransformCompoundAssignOperator(E);
}
}
ExprResult TransformArraySubscriptExpr(ArraySubscriptExpr *E) {
if(isPointerToShared(E->getBase()->getType())) {
Expr *LHS = E->getBase();
Expr *RHS = E->getIdx();
QualType PointeeType = LHS->getType()->getAs<PointerType>()->getPointeeType();
ArrayDimensionT Dims = GetArrayDimension(PointeeType);
int ElementSize = Dims.ElementSize;
Expr *IntVal = TransformExpr(RHS).get();
IntVal = MaybeAdjustForArray(Dims, IntVal, BO_Mul).get();
std::vector<Expr*> args;
args.push_back(TransformExpr(LHS).get());
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), ElementSize));
args.push_back(IntVal);
int LayoutQualifier = PointeeType.getQualifiers().getLayoutQualifier();
if(LayoutQualifier == 0) {
return BuildUPCRCall(Decls->UPCR_ADD_PSHAREDI, args);
} else if(LayoutQualifier == 1) {
return BuildUPCRCall(Decls->UPCR_ADD_PSHARED1, args);
} else {
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), LayoutQualifier));
return BuildUPCRCall(Decls->UPCR_ADD_SHARED, args);
}
} else {
return TreeTransformUPC::TransformArraySubscriptExpr(E);
}
}
ExprResult TransformMemberExpr(MemberExpr *E) {
Expr *Base = E->getBase();
QualType BaseType = Base->getType();
if(const PointerType *PT = BaseType->getAs<PointerType>()) {
BaseType = PT->getPointeeType();
}
if(BaseType.getQualifiers().hasShared()) {
ValueDecl * FD = E->getMemberDecl();
Expr *NewBase = TransformExpr(Base).get();
if(!isPhaseless(BaseType)) {
std::vector<Expr*> args;
args.push_back(NewBase);
NewBase = BuildUPCRCall(Decls->UPCR_SHARED_TO_PSHARED, args).get();
}
CharUnits Offset = SemaRef.Context.toCharUnitsFromBits(SemaRef.Context.getFieldOffset(FD));
std::vector<Expr *> args;
args.push_back(NewBase);
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), 1));
args.push_back(CreateInteger(SemaRef.Context.getSizeType(), Offset.getQuantity()));
return BuildUPCRCall(Decls->UPCR_ADD_PSHAREDI, args);
} else {
return TreeTransformUPC::TransformMemberExpr(E);