-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.cpp
8772 lines (7587 loc) · 303 KB
/
expr.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
/*
Copyright (c) 2010-2014, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER
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.
*/
/** @file expr.cpp
@brief Implementations of expression classes
*/
#include "expr.h"
#include "ast.h"
#include "type.h"
#include "sym.h"
#include "ctx.h"
#include "module.h"
#include "util.h"
#include "llvmutil.h"
#ifndef _MSC_VER
#include <inttypes.h>
#endif
#ifndef PRId64
#define PRId64 "lld"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
#endif
#include <list>
#include <set>
#include <stdio.h>
#if defined(LLVM_3_1) || defined(LLVM_3_2)
#include <llvm/Module.h>
#include <llvm/Type.h>
#include <llvm/Instructions.h>
#include <llvm/Function.h>
#include <llvm/DerivedTypes.h>
#include <llvm/LLVMContext.h>
#include <llvm/CallingConv.h>
#else
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/CallingConv.h>
#endif
#include <llvm/ExecutionEngine/GenericValue.h>
#if defined(LLVM_3_5)
#include <llvm/IR/InstIterator.h>
#else
#include <llvm/Support/InstIterator.h>
#endif
/////////////////////////////////////////////////////////////////////////////////////
// Expr
llvm::Value *
Expr::GetLValue(FunctionEmitContext *ctx) const {
// Expressions that can't provide an lvalue can just return NULL
return NULL;
}
const Type *
Expr::GetLValueType() const {
// This also only needs to be overrided by Exprs that implement the
// GetLValue() method.
return NULL;
}
llvm::Constant *
Expr::GetConstant(const Type *type) const {
// The default is failure; just return NULL
return NULL;
}
Symbol *
Expr::GetBaseSymbol() const {
// Not all expressions can do this, so provide a generally-useful
// default implementation.
return NULL;
}
#if 0
/** If a conversion from 'fromAtomicType' to 'toAtomicType' may cause lost
precision, issue a warning. Don't warn for conversions to bool and
conversions between signed and unsigned integers of the same size.
*/
static void
lMaybeIssuePrecisionWarning(const AtomicType *toAtomicType,
const AtomicType *fromAtomicType,
SourcePos pos, const char *errorMsgBase) {
switch (toAtomicType->basicType) {
case AtomicType::TYPE_BOOL:
case AtomicType::TYPE_INT8:
case AtomicType::TYPE_UINT8:
case AtomicType::TYPE_INT16:
case AtomicType::TYPE_UINT16:
case AtomicType::TYPE_INT32:
case AtomicType::TYPE_UINT32:
case AtomicType::TYPE_FLOAT:
case AtomicType::TYPE_INT64:
case AtomicType::TYPE_UINT64:
case AtomicType::TYPE_DOUBLE:
if ((int)toAtomicType->basicType < (int)fromAtomicType->basicType &&
toAtomicType->basicType != AtomicType::TYPE_BOOL &&
!(toAtomicType->basicType == AtomicType::TYPE_INT8 &&
fromAtomicType->basicType == AtomicType::TYPE_UINT8) &&
!(toAtomicType->basicType == AtomicType::TYPE_INT16 &&
fromAtomicType->basicType == AtomicType::TYPE_UINT16) &&
!(toAtomicType->basicType == AtomicType::TYPE_INT32 &&
fromAtomicType->basicType == AtomicType::TYPE_UINT32) &&
!(toAtomicType->basicType == AtomicType::TYPE_INT64 &&
fromAtomicType->basicType == AtomicType::TYPE_UINT64))
Warning(pos, "Conversion from type \"%s\" to type \"%s\" for %s"
" may lose information.",
fromAtomicType->GetString().c_str(), toAtomicType->GetString().c_str(),
errorMsgBase);
break;
default:
FATAL("logic error in lMaybeIssuePrecisionWarning()");
}
}
#endif
///////////////////////////////////////////////////////////////////////////
static Expr *
lArrayToPointer(Expr *expr) {
AssertPos(expr->pos, expr && CastType<ArrayType>(expr->GetType()));
Expr *zero = new ConstExpr(AtomicType::UniformInt32, 0, expr->pos);
Expr *index = new IndexExpr(expr, zero, expr->pos);
Expr *addr = new AddressOfExpr(index, expr->pos);
addr = TypeCheck(addr);
Assert(addr != NULL);
addr = Optimize(addr);
Assert(addr != NULL);
return addr;
}
static bool
lIsAllIntZeros(Expr *expr) {
const Type *type = expr->GetType();
if (type == NULL || type->IsIntType() == false)
return false;
ConstExpr *ce = dynamic_cast<ConstExpr *>(expr);
if (ce == NULL)
return false;
uint64_t vals[ISPC_MAX_NVEC];
int count = ce->GetValues(vals);
if (count == 1)
return (vals[0] == 0);
else {
for (int i = 0; i < count; ++i)
if (vals[i] != 0)
return false;
}
return true;
}
static bool
lDoTypeConv(const Type *fromType, const Type *toType, Expr **expr,
bool failureOk, const char *errorMsgBase, SourcePos pos) {
/* This function is way too long and complex. Is type conversion stuff
always this messy, or can this be cleaned up somehow? */
AssertPos(pos, failureOk || errorMsgBase != NULL);
if (toType == NULL || fromType == NULL)
return false;
// The types are equal; there's nothing to do
if (Type::Equal(toType, fromType))
return true;
if (fromType->IsVoidType()) {
if (!failureOk)
Error(pos, "Can't convert from \"void\" to \"%s\" for %s.",
toType->GetString().c_str(), errorMsgBase);
return false;
}
if (toType->IsVoidType()) {
if (!failureOk)
Error(pos, "Can't convert type \"%s\" to \"void\" for %s.",
fromType->GetString().c_str(), errorMsgBase);
return false;
}
if (CastType<FunctionType>(fromType)) {
if (CastType<PointerType>(toType) != NULL) {
// Convert function type to pointer to function type
if (expr != NULL) {
Expr *aoe = new AddressOfExpr(*expr, (*expr)->pos);
if (lDoTypeConv(aoe->GetType(), toType, &aoe, failureOk,
errorMsgBase, pos)) {
*expr = aoe;
return true;
}
}
else
return lDoTypeConv(PointerType::GetUniform(fromType), toType, NULL,
failureOk, errorMsgBase, pos);
}
else {
if (!failureOk)
Error(pos, "Can't convert function type \"%s\" to \"%s\" for %s.",
fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
}
if (CastType<FunctionType>(toType)) {
if (!failureOk)
Error(pos, "Can't convert from type \"%s\" to function type \"%s\" "
"for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
if ((toType->GetSOAWidth() > 0 || fromType->GetSOAWidth() > 0) &&
Type::Equal(toType->GetAsUniformType(), fromType->GetAsUniformType()) &&
toType->GetSOAWidth() != fromType->GetSOAWidth()) {
if (!failureOk)
Error(pos, "Can't convert between types \"%s\" and \"%s\" with "
"different SOA widths for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
const ArrayType *toArrayType = CastType<ArrayType>(toType);
const ArrayType *fromArrayType = CastType<ArrayType>(fromType);
const VectorType *toVectorType = CastType<VectorType>(toType);
const VectorType *fromVectorType = CastType<VectorType>(fromType);
const StructType *toStructType = CastType<StructType>(toType);
const StructType *fromStructType = CastType<StructType>(fromType);
const EnumType *toEnumType = CastType<EnumType>(toType);
const EnumType *fromEnumType = CastType<EnumType>(fromType);
const AtomicType *toAtomicType = CastType<AtomicType>(toType);
const AtomicType *fromAtomicType = CastType<AtomicType>(fromType);
const PointerType *fromPointerType = CastType<PointerType>(fromType);
const PointerType *toPointerType = CastType<PointerType>(toType);
// Do this early, since for the case of a conversion like
// "float foo[10]" -> "float * uniform foo", we have what's seemingly
// a varying to uniform conversion (but not really)
if (fromArrayType != NULL && toPointerType != NULL) {
// can convert any array to a void pointer (both uniform and
// varying).
if (PointerType::IsVoidPointer(toPointerType))
goto typecast_ok;
// array to pointer to array element type
const Type *eltType = fromArrayType->GetElementType();
if (toPointerType->GetBaseType()->IsConstType())
eltType = eltType->GetAsConstType();
PointerType pt(eltType, toPointerType->GetVariability(),
toPointerType->IsConstType());
if (Type::Equal(toPointerType, &pt))
goto typecast_ok;
else {
if (!failureOk)
Error(pos, "Can't convert from incompatible array type \"%s\" "
"to pointer type \"%s\" for %s.",
fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
}
if (toType->IsUniformType() && fromType->IsVaryingType()) {
if (!failureOk)
Error(pos, "Can't convert from type \"%s\" to type \"%s\" for %s.",
fromType->GetString().c_str(), toType->GetString().c_str(),
errorMsgBase);
return false;
}
if (fromPointerType != NULL) {
if (CastType<AtomicType>(toType) != NULL &&
toType->IsBoolType())
// Allow implicit conversion of pointers to bools
goto typecast_ok;
if (toArrayType != NULL &&
Type::Equal(fromType->GetBaseType(), toArrayType->GetElementType())) {
// Can convert pointers to arrays of the same type
goto typecast_ok;
}
if (toPointerType == NULL) {
if (!failureOk)
Error(pos, "Can't convert between from pointer type "
"\"%s\" to non-pointer type \"%s\" for %s.",
fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
else if (fromPointerType->IsSlice() == true &&
toPointerType->IsSlice() == false) {
if (!failureOk)
Error(pos, "Can't convert from pointer to SOA type "
"\"%s\" to pointer to non-SOA type \"%s\" for %s.",
fromPointerType->GetAsNonSlice()->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
else if (PointerType::IsVoidPointer(toPointerType)) {
if (fromPointerType->GetBaseType()->IsConstType() &&
!(toPointerType->GetBaseType()->IsConstType())) {
if (!failureOk)
Error(pos, "Can't convert pointer to const \"%s\" to void pointer.",
fromPointerType->GetString().c_str());
return false;
}
// any pointer type can be converted to a void *
// ...almost. #731
goto typecast_ok;
}
else if (PointerType::IsVoidPointer(fromPointerType) &&
expr != NULL &&
dynamic_cast<NullPointerExpr *>(*expr) != NULL) {
// and a NULL convert to any other pointer type
goto typecast_ok;
}
else if (!Type::Equal(fromPointerType->GetBaseType(),
toPointerType->GetBaseType()) &&
!Type::Equal(fromPointerType->GetBaseType()->GetAsConstType(),
toPointerType->GetBaseType())) {
if (!failureOk)
Error(pos, "Can't convert from pointer type \"%s\" to "
"incompatible pointer type \"%s\" for %s.",
fromPointerType->GetString().c_str(),
toPointerType->GetString().c_str(), errorMsgBase);
return false;
}
if (toType->IsVaryingType() && fromType->IsUniformType())
goto typecast_ok;
if (toPointerType->IsSlice() == true &&
fromPointerType->IsSlice() == false)
goto typecast_ok;
// Otherwise there's nothing to do
return true;
}
if (toPointerType != NULL && fromAtomicType != NULL &&
fromAtomicType->IsIntType() && expr != NULL &&
lIsAllIntZeros(*expr)) {
// We have a zero-valued integer expression, which can also be
// treated as a NULL pointer that can be converted to any other
// pointer type.
Expr *npe = new NullPointerExpr(pos);
if (lDoTypeConv(PointerType::Void, toType, &npe,
failureOk, errorMsgBase, pos)) {
*expr = npe;
return true;
}
return false;
}
// Need to check this early, since otherwise the [sic] "unbound"
// variability of SOA struct types causes things to get messy if that
// hasn't been detected...
if (toStructType && fromStructType &&
(toStructType->GetSOAWidth() != fromStructType->GetSOAWidth())) {
if (!failureOk)
Error(pos, "Can't convert between incompatible struct types \"%s\" "
"and \"%s\" for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
// Convert from type T -> const T; just return a TypeCast expr, which
// can handle this
if (Type::EqualIgnoringConst(toType, fromType) &&
toType->IsConstType() == true &&
fromType->IsConstType() == false)
goto typecast_ok;
if (CastType<ReferenceType>(fromType)) {
if (CastType<ReferenceType>(toType)) {
// Convert from a reference to a type to a const reference to a type;
// this is handled by TypeCastExpr
if (Type::Equal(toType->GetReferenceTarget(),
fromType->GetReferenceTarget()->GetAsConstType()))
goto typecast_ok;
const ArrayType *atFrom =
CastType<ArrayType>(fromType->GetReferenceTarget());
const ArrayType *atTo =
CastType<ArrayType>(toType->GetReferenceTarget());
if (atFrom != NULL && atTo != NULL &&
Type::Equal(atFrom->GetElementType(), atTo->GetElementType())) {
goto typecast_ok;
}
else {
if (!failureOk)
Error(pos, "Can't convert between incompatible reference types \"%s\" "
"and \"%s\" for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
}
else {
// convert from a reference T -> T
if (expr != NULL) {
Expr *drExpr = new RefDerefExpr(*expr, pos);
if (lDoTypeConv(drExpr->GetType(), toType, &drExpr, failureOk,
errorMsgBase, pos) == true) {
*expr = drExpr;
return true;
}
return false;
}
else
return lDoTypeConv(fromType->GetReferenceTarget(), toType, NULL,
failureOk, errorMsgBase, pos);
}
}
else if (CastType<ReferenceType>(toType)) {
// T -> reference T
if (expr != NULL) {
Expr *rExpr = new ReferenceExpr(*expr, pos);
if (lDoTypeConv(rExpr->GetType(), toType, &rExpr, failureOk,
errorMsgBase, pos) == true) {
*expr = rExpr;
return true;
}
return false;
}
else {
ReferenceType rt(fromType);
return lDoTypeConv(&rt, toType, NULL, failureOk, errorMsgBase, pos);
}
}
else if (Type::Equal(toType, fromType->GetAsNonConstType()))
// convert: const T -> T (as long as T isn't a reference)
goto typecast_ok;
fromType = fromType->GetReferenceTarget();
toType = toType->GetReferenceTarget();
if (toArrayType && fromArrayType) {
if (Type::Equal(toArrayType->GetElementType(),
fromArrayType->GetElementType())) {
// the case of different element counts should have returned
// successfully earlier, yes??
AssertPos(pos, toArrayType->GetElementCount() != fromArrayType->GetElementCount());
goto typecast_ok;
}
else if (Type::Equal(toArrayType->GetElementType(),
fromArrayType->GetElementType()->GetAsConstType())) {
// T[x] -> const T[x]
goto typecast_ok;
}
else {
if (!failureOk)
Error(pos, "Array type \"%s\" can't be converted to type \"%s\" for %s.",
fromType->GetString().c_str(), toType->GetString().c_str(),
errorMsgBase);
return false;
}
}
if (toVectorType && fromVectorType) {
// converting e.g. int<n> -> float<n>
if (fromVectorType->GetElementCount() != toVectorType->GetElementCount()) {
if (!failureOk)
Error(pos, "Can't convert between differently sized vector types "
"\"%s\" -> \"%s\" for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
goto typecast_ok;
}
if (toStructType && fromStructType) {
if (!Type::Equal(toStructType->GetAsUniformType()->GetAsConstType(),
fromStructType->GetAsUniformType()->GetAsConstType())) {
if (!failureOk)
Error(pos, "Can't convert between different struct types "
"\"%s\" and \"%s\" for %s.", fromStructType->GetString().c_str(),
toStructType->GetString().c_str(), errorMsgBase);
return false;
}
goto typecast_ok;
}
if (toEnumType != NULL && fromEnumType != NULL) {
// No implicit conversions between different enum types
if (!Type::EqualIgnoringConst(toEnumType->GetAsUniformType(),
fromEnumType->GetAsUniformType())) {
if (!failureOk)
Error(pos, "Can't convert between different enum types "
"\"%s\" and \"%s\" for %s", fromEnumType->GetString().c_str(),
toEnumType->GetString().c_str(), errorMsgBase);
return false;
}
goto typecast_ok;
}
// enum -> atomic (integer, generally...) is always ok
if (fromEnumType != NULL) {
AssertPos(pos, toAtomicType != NULL || toVectorType != NULL);
goto typecast_ok;
}
// from here on out, the from type can only be atomic something or
// other...
if (fromAtomicType == NULL) {
if (!failureOk)
Error(pos, "Type conversion from \"%s\" to \"%s\" for %s is not "
"possible.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
// scalar -> short-vector conversions
if (toVectorType != NULL &&
(fromType->GetSOAWidth() == toType->GetSOAWidth()))
goto typecast_ok;
// ok, it better be a scalar->scalar conversion of some sort by now
if (toAtomicType == NULL) {
if (!failureOk)
Error(pos, "Type conversion from \"%s\" to \"%s\" for %s is "
"not possible", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
if (fromType->GetSOAWidth() != toType->GetSOAWidth()) {
if (!failureOk)
Error(pos, "Can't convert between types \"%s\" and \"%s\" with "
"different SOA widths for %s.", fromType->GetString().c_str(),
toType->GetString().c_str(), errorMsgBase);
return false;
}
typecast_ok:
if (expr != NULL)
*expr = new TypeCastExpr(toType, *expr, pos);
return true;
}
bool
CanConvertTypes(const Type *fromType, const Type *toType,
const char *errorMsgBase, SourcePos pos) {
return lDoTypeConv(fromType, toType, NULL, errorMsgBase == NULL,
errorMsgBase, pos);
}
Expr *
TypeConvertExpr(Expr *expr, const Type *toType, const char *errorMsgBase) {
if (expr == NULL)
return NULL;
#if 0
Debug(expr->pos, "type convert %s -> %s.", expr->GetType()->GetString().c_str(),
toType->GetString().c_str());
#endif
const Type *fromType = expr->GetType();
Expr *e = expr;
if (lDoTypeConv(fromType, toType, &e, false, errorMsgBase,
expr->pos))
return e;
else
return NULL;
}
bool
PossiblyResolveFunctionOverloads(Expr *expr, const Type *type) {
FunctionSymbolExpr *fse = NULL;
const FunctionType *funcType = NULL;
if (CastType<PointerType>(type) != NULL &&
(funcType = CastType<FunctionType>(type->GetBaseType())) &&
(fse = dynamic_cast<FunctionSymbolExpr *>(expr)) != NULL) {
// We're initializing a function pointer with a function symbol,
// which in turn may represent an overloaded function. So we need
// to try to resolve the overload based on the type of the symbol
// we're initializing here.
std::vector<const Type *> paramTypes;
for (int i = 0; i < funcType->GetNumParameters(); ++i)
paramTypes.push_back(funcType->GetParameterType(i));
if (fse->ResolveOverloads(expr->pos, paramTypes) == false)
return false;
}
return true;
}
/** Utility routine that emits code to initialize a symbol given an
initializer expression.
@param ptr Memory location of storage for the symbol's data
@param symName Name of symbol (used in error messages)
@param symType Type of variable being initialized
@param initExpr Expression for the initializer
@param ctx FunctionEmitContext to use for generating instructions
@param pos Source file position of the variable being initialized
*/
void
InitSymbol(llvm::Value *ptr, const Type *symType, Expr *initExpr,
FunctionEmitContext *ctx, SourcePos pos) {
if (initExpr == NULL)
// leave it uninitialized
return;
// See if we have a constant initializer a this point
llvm::Constant *constValue = initExpr->GetConstant(symType);
if (constValue != NULL) {
// It'd be nice if we could just do a StoreInst(constValue, ptr)
// at this point, but unfortunately that doesn't generate great
// code (e.g. a bunch of scalar moves for a constant array.) So
// instead we'll make a constant static global that holds the
// constant value and emit a memcpy to put its value into the
// pointer we have.
llvm::Type *llvmType = symType->LLVMType(g->ctx);
if (llvmType == NULL) {
AssertPos(pos, m->errorCount > 0);
return;
}
if (Type::IsBasicType(symType))
ctx->StoreInst(constValue, ptr);
else {
llvm::Value *constPtr =
new llvm::GlobalVariable(*m->module, llvmType, true /* const */,
llvm::GlobalValue::InternalLinkage,
constValue, "const_initializer");
llvm::Value *size = g->target->SizeOf(llvmType,
ctx->GetCurrentBasicBlock());
ctx->MemcpyInst(ptr, constPtr, size);
}
return;
}
// If the initializer is a straight up expression that isn't an
// ExprList, then we'll see if we can type convert it to the type of
// the variable.
if (dynamic_cast<ExprList *>(initExpr) == NULL) {
if (PossiblyResolveFunctionOverloads(initExpr, symType) == false)
return;
initExpr = TypeConvertExpr(initExpr, symType, "initializer");
if (initExpr == NULL)
return;
llvm::Value *initializerValue = initExpr->GetValue(ctx);
if (initializerValue != NULL)
// Bingo; store the value in the variable's storage
ctx->StoreInst(initializerValue, ptr);
return;
}
// Atomic types and enums can be initialized with { ... } initializer
// expressions if they have a single element (except for SOA types,
// which are handled below).
if (symType->IsSOAType() == false && Type::IsBasicType(symType)) {
ExprList *elist = dynamic_cast<ExprList *>(initExpr);
if (elist != NULL) {
if (elist->exprs.size() == 1)
InitSymbol(ptr, symType, elist->exprs[0], ctx, pos);
else
Error(initExpr->pos, "Expression list initializers with "
"multiple values can't be used with type \"%s\".",
symType->GetString().c_str());
}
return;
}
const ReferenceType *rt = CastType<ReferenceType>(symType);
if (rt) {
if (!Type::Equal(initExpr->GetType(), rt)) {
Error(initExpr->pos, "Initializer for reference type \"%s\" must have same "
"reference type itself. \"%s\" is incompatible.",
rt->GetString().c_str(), initExpr->GetType()->GetString().c_str());
return;
}
llvm::Value *initializerValue = initExpr->GetValue(ctx);
if (initializerValue)
ctx->StoreInst(initializerValue, ptr);
return;
}
// Handle initiailizers for SOA types as well as for structs, arrays,
// and vectors.
const CollectionType *collectionType = CastType<CollectionType>(symType);
if (collectionType != NULL || symType->IsSOAType()) {
int nElements = collectionType ? collectionType->GetElementCount() :
symType->GetSOAWidth();
std::string name;
if (CastType<StructType>(symType) != NULL)
name = "struct";
else if (CastType<ArrayType>(symType) != NULL)
name = "array";
else if (CastType<VectorType>(symType) != NULL)
name = "vector";
else if (symType->IsSOAType())
name = symType->GetVariability().GetString();
else
FATAL("Unexpected CollectionType in InitSymbol()");
// There are two cases for initializing these types; either a
// single initializer may be provided (float foo[3] = 0;), in which
// case all of the elements are initialized to the given value, or
// an initializer list may be provided (float foo[3] = { 1,2,3 }),
// in which case the elements are initialized with the
// corresponding values.
ExprList *exprList = dynamic_cast<ExprList *>(initExpr);
if (exprList != NULL) {
// The { ... } case; make sure we have the no more expressions
// in the ExprList as we have struct members
int nInits = exprList->exprs.size();
if (nInits > nElements) {
Error(initExpr->pos, "Initializer for %s type \"%s\" requires "
"no more than %d values; %d provided.", name.c_str(),
symType->GetString().c_str(), nElements, nInits);
return;
}
// Initialize each element with the corresponding value from
// the ExprList
for (int i = 0; i < nElements; ++i) {
// For SOA types, the element type is the uniform variant
// of the underlying type
const Type *elementType =
collectionType ? collectionType->GetElementType(i) :
symType->GetAsUniformType();
if (elementType == NULL) {
AssertPos(pos, m->errorCount > 0);
return;
}
llvm::Value *ep;
if (CastType<StructType>(symType) != NULL)
ep = ctx->AddElementOffset(ptr, i, NULL, "element");
else
ep = ctx->GetElementPtrInst(ptr, LLVMInt32(0), LLVMInt32(i),
PointerType::GetUniform(elementType),
"gep");
if (i < nInits)
InitSymbol(ep, elementType, exprList->exprs[i], ctx, pos);
else {
// If we don't have enough initializer values, initialize the
// rest as zero.
llvm::Type *llvmType = elementType->LLVMType(g->ctx);
if (llvmType == NULL) {
AssertPos(pos, m->errorCount > 0);
return;
}
llvm::Constant *zeroInit = llvm::Constant::getNullValue(llvmType);
ctx->StoreInst(zeroInit, ep);
}
}
}
else
Error(initExpr->pos, "Can't assign type \"%s\" to \"%s\".",
initExpr->GetType()->GetString().c_str(),
collectionType->GetString().c_str());
return;
}
FATAL("Unexpected Type in InitSymbol()");
}
///////////////////////////////////////////////////////////////////////////
/** Given an atomic or vector type, this returns a boolean type with the
same "shape". In other words, if the given type is a vector type of
three uniform ints, the returned type is a vector type of three uniform
bools. */
static const Type *
lMatchingBoolType(const Type *type) {
bool uniformTest = type->IsUniformType();
const AtomicType *boolBase = uniformTest ? AtomicType::UniformBool :
AtomicType::VaryingBool;
const VectorType *vt = CastType<VectorType>(type);
if (vt != NULL)
return new VectorType(boolBase, vt->GetElementCount());
else {
Assert(Type::IsBasicType(type));
return boolBase;
}
}
///////////////////////////////////////////////////////////////////////////
// UnaryExpr
static llvm::Constant *
lLLVMConstantValue(const Type *type, llvm::LLVMContext *ctx, double value) {
const AtomicType *atomicType = CastType<AtomicType>(type);
const EnumType *enumType = CastType<EnumType>(type);
const VectorType *vectorType = CastType<VectorType>(type);
const PointerType *pointerType = CastType<PointerType>(type);
// This function is only called with, and only works for atomic, enum,
// and vector types.
Assert(atomicType != NULL || enumType != NULL || vectorType != NULL ||
pointerType != NULL);
if (atomicType != NULL || enumType != NULL) {
// If it's an atomic or enuemrator type, then figure out which of
// the llvmutil.h functions to call to get the corresponding
// constant and then call it...
bool isUniform = type->IsUniformType();
AtomicType::BasicType basicType = (enumType != NULL) ?
AtomicType::TYPE_UINT32 : atomicType->basicType;
switch (basicType) {
case AtomicType::TYPE_VOID:
FATAL("can't get constant value for void type");
return NULL;
case AtomicType::TYPE_BOOL:
if (isUniform)
return (value != 0.) ? LLVMTrue : LLVMFalse;
else
return LLVMBoolVector(value != 0.);
case AtomicType::TYPE_INT8: {
int i = (int)value;
Assert((double)i == value);
return isUniform ? LLVMInt8(i) : LLVMInt8Vector(i);
}
case AtomicType::TYPE_UINT8: {
unsigned int i = (unsigned int)value;
return isUniform ? LLVMUInt8(i) : LLVMUInt8Vector(i);
}
case AtomicType::TYPE_INT16: {
int i = (int)value;
Assert((double)i == value);
return isUniform ? LLVMInt16(i) : LLVMInt16Vector(i);
}
case AtomicType::TYPE_UINT16: {
unsigned int i = (unsigned int)value;
return isUniform ? LLVMUInt16(i) : LLVMUInt16Vector(i);
}
case AtomicType::TYPE_INT32: {
int i = (int)value;
Assert((double)i == value);
return isUniform ? LLVMInt32(i) : LLVMInt32Vector(i);
}
case AtomicType::TYPE_UINT32: {
unsigned int i = (unsigned int)value;
return isUniform ? LLVMUInt32(i) : LLVMUInt32Vector(i);
}
case AtomicType::TYPE_FLOAT:
return isUniform ? LLVMFloat((float)value) :
LLVMFloatVector((float)value);
case AtomicType::TYPE_UINT64: {
uint64_t i = (uint64_t)value;
Assert(value == (int64_t)i);
return isUniform ? LLVMUInt64(i) : LLVMUInt64Vector(i);
}
case AtomicType::TYPE_INT64: {
int64_t i = (int64_t)value;
Assert((double)i == value);
return isUniform ? LLVMInt64(i) : LLVMInt64Vector(i);
}
case AtomicType::TYPE_DOUBLE:
return isUniform ? LLVMDouble(value) : LLVMDoubleVector(value);
default:
FATAL("logic error in lLLVMConstantValue");
return NULL;
}
}
else if (pointerType != NULL) {
Assert(value == 0);
if (pointerType->IsUniformType())
return llvm::Constant::getNullValue(LLVMTypes::VoidPointerType);
else
return llvm::Constant::getNullValue(LLVMTypes::VoidPointerVectorType);
}
else {
// For vector types, first get the LLVM constant for the basetype with
// a recursive call to lLLVMConstantValue().
const Type *baseType = vectorType->GetBaseType();
llvm::Constant *constElement = lLLVMConstantValue(baseType, ctx, value);
llvm::Type *llvmVectorType = vectorType->LLVMType(ctx);
// Now create a constant version of the corresponding LLVM type that we
// use to represent the VectorType.
// FIXME: this is a little ugly in that the fact that ispc represents
// uniform VectorTypes as LLVM VectorTypes and varying VectorTypes as
// LLVM ArrayTypes leaks into the code here; it feels like this detail
// should be better encapsulated?
if (baseType->IsUniformType()) {
llvm::VectorType *lvt =
llvm::dyn_cast<llvm::VectorType>(llvmVectorType);
Assert(lvt != NULL);
std::vector<llvm::Constant *> vals;
for (unsigned int i = 0; i < lvt->getNumElements(); ++i)
vals.push_back(constElement);
return llvm::ConstantVector::get(vals);
}
else {
llvm::ArrayType *lat =
llvm::dyn_cast<llvm::ArrayType>(llvmVectorType);
Assert(lat != NULL);
std::vector<llvm::Constant *> vals;
for (unsigned int i = 0; i < lat->getNumElements(); ++i)
vals.push_back(constElement);
return llvm::ConstantArray::get(lat, vals);
}
}
}
static llvm::Value *
lMaskForSymbol(Symbol *baseSym, FunctionEmitContext *ctx) {
if (baseSym == NULL)
return ctx->GetFullMask();
if (CastType<PointerType>(baseSym->type) != NULL ||
CastType<ReferenceType>(baseSym->type) != NULL)
// FIXME: for pointers, we really only want to do this for
// dereferencing the pointer, not for things like pointer
// arithmetic, when we may be able to use the internal mask,
// depending on context...
return ctx->GetFullMask();
llvm::Value *mask = (baseSym->parentFunction == ctx->GetFunction() &&
baseSym->storageClass != SC_STATIC) ?
ctx->GetInternalMask() : ctx->GetFullMask();
return mask;
}
/** Store the result of an assignment to the given location.
*/
static void
lStoreAssignResult(llvm::Value *value, llvm::Value *ptr, const Type *valueType,
const Type *ptrType, FunctionEmitContext *ctx,
Symbol *baseSym) {
Assert(baseSym == NULL ||
baseSym->varyingCFDepth <= ctx->VaryingCFDepth());
if (!g->opt.disableMaskedStoreToStore &&
!g->opt.disableMaskAllOnOptimizations &&
baseSym != NULL &&
baseSym->varyingCFDepth == ctx->VaryingCFDepth() &&
baseSym->storageClass != SC_STATIC &&
CastType<ReferenceType>(baseSym->type) == NULL &&
CastType<PointerType>(baseSym->type) == NULL) {
// If the variable is declared at the same varying control flow
// depth as where it's being assigned, then we don't need to do any
// masking but can just do the assignment as if all the lanes were
// known to be on. While this may lead to random/garbage values
// written into the lanes that are off, by definition they will
// never be accessed, since those lanes aren't executing, and won't
// be executing at this scope or any other one before the variable