-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
ByteCodeExprGen.cpp
3677 lines (3108 loc) · 105 KB
/
ByteCodeExprGen.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
//===--- ByteCodeExprGen.cpp - Code generator for expressions ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ByteCodeExprGen.h"
#include "ByteCodeEmitter.h"
#include "ByteCodeStmtGen.h"
#include "Context.h"
#include "Floating.h"
#include "Function.h"
#include "InterpShared.h"
#include "PrimType.h"
#include "Program.h"
#include "clang/AST/Attr.h"
using namespace clang;
using namespace clang::interp;
using APSInt = llvm::APSInt;
namespace clang {
namespace interp {
/// Scope used to handle temporaries in toplevel variable declarations.
template <class Emitter> class DeclScope final : public VariableScope<Emitter> {
public:
DeclScope(ByteCodeExprGen<Emitter> *Ctx, const ValueDecl *VD)
: VariableScope<Emitter>(Ctx), Scope(Ctx->P, VD),
OldGlobalDecl(Ctx->GlobalDecl) {
Ctx->GlobalDecl = Context::shouldBeGloballyIndexed(VD);
}
void addExtended(const Scope::Local &Local) override {
return this->addLocal(Local);
}
~DeclScope() { this->Ctx->GlobalDecl = OldGlobalDecl; }
private:
Program::DeclScope Scope;
bool OldGlobalDecl;
};
/// Scope used to handle initialization methods.
template <class Emitter> class OptionScope final {
public:
/// Root constructor, compiling or discarding primitives.
OptionScope(ByteCodeExprGen<Emitter> *Ctx, bool NewDiscardResult,
bool NewInitializing)
: Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
OldInitializing(Ctx->Initializing) {
Ctx->DiscardResult = NewDiscardResult;
Ctx->Initializing = NewInitializing;
}
~OptionScope() {
Ctx->DiscardResult = OldDiscardResult;
Ctx->Initializing = OldInitializing;
}
private:
/// Parent context.
ByteCodeExprGen<Emitter> *Ctx;
/// Old discard flag to restore.
bool OldDiscardResult;
bool OldInitializing;
};
} // namespace interp
} // namespace clang
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
const Expr *SubExpr = CE->getSubExpr();
switch (CE->getCastKind()) {
case CK_LValueToRValue: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> SubExprT = classify(SubExpr->getType());
// Prepare storage for the result.
if (!Initializing && !SubExprT) {
std::optional<unsigned> LocalIndex =
allocateLocal(SubExpr, /*IsExtended=*/false);
if (!LocalIndex)
return false;
if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
if (!this->visit(SubExpr))
return false;
if (SubExprT)
return this->emitLoadPop(*SubExprT, CE);
// If the subexpr type is not primitive, we need to perform a copy here.
// This happens for example in C when dereferencing a pointer of struct
// type.
return this->emitMemcpy(CE);
}
case CK_UncheckedDerivedToBase:
case CK_DerivedToBase: {
if (!this->visit(SubExpr))
return false;
unsigned DerivedOffset = collectBaseOffset(getRecordTy(CE->getType()),
getRecordTy(SubExpr->getType()));
return this->emitGetPtrBasePop(DerivedOffset, CE);
}
case CK_BaseToDerived: {
if (!this->visit(SubExpr))
return false;
unsigned DerivedOffset = collectBaseOffset(getRecordTy(SubExpr->getType()),
getRecordTy(CE->getType()));
return this->emitGetPtrDerivedPop(DerivedOffset, CE);
}
case CK_FloatingCast: {
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
}
case CK_IntegralToFloating: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> FromT = classify(SubExpr->getType());
if (!FromT)
return false;
if (!this->visit(SubExpr))
return false;
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
llvm::RoundingMode RM = getRoundingMode(CE);
return this->emitCastIntegralFloating(*FromT, TargetSemantics, RM, CE);
}
case CK_FloatingToBoolean:
case CK_FloatingToIntegral: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> ToT = classify(CE->getType());
if (!ToT)
return false;
if (!this->visit(SubExpr))
return false;
if (ToT == PT_IntAP)
return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
CE);
if (ToT == PT_IntAPS)
return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
CE);
return this->emitCastFloatingIntegral(*ToT, CE);
}
case CK_NullToPointer:
if (DiscardResult)
return true;
return this->emitNull(classifyPrim(CE->getType()), CE);
case CK_PointerToIntegral: {
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
PrimType T = classifyPrim(CE->getType());
return this->emitCastPointerIntegral(T, CE);
}
case CK_ArrayToPointerDecay: {
if (!this->visit(SubExpr))
return false;
if (!this->emitArrayDecay(CE))
return false;
if (DiscardResult)
return this->emitPopPtr(CE);
return true;
}
case CK_AtomicToNonAtomic:
case CK_ConstructorConversion:
case CK_FunctionToPointerDecay:
case CK_NonAtomicToAtomic:
case CK_NoOp:
case CK_UserDefinedConversion:
return this->delegate(SubExpr);
case CK_BitCast:
if (CE->getType()->isAtomicType()) {
if (!this->discard(SubExpr))
return false;
return this->emitInvalidCast(CastKind::Reinterpret, CE);
}
return this->delegate(SubExpr);
case CK_IntegralToBoolean:
case CK_IntegralCast: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> FromT = classify(SubExpr->getType());
std::optional<PrimType> ToT = classify(CE->getType());
if (!FromT || !ToT)
return false;
if (!this->visit(SubExpr))
return false;
if (ToT == PT_IntAP)
return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE);
if (ToT == PT_IntAPS)
return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE);
if (FromT == ToT)
return true;
return this->emitCast(*FromT, *ToT, CE);
}
case CK_PointerToBoolean: {
PrimType PtrT = classifyPrim(SubExpr->getType());
// Just emit p != nullptr for this.
if (!this->visit(SubExpr))
return false;
if (!this->emitNull(PtrT, CE))
return false;
return this->emitNE(PtrT, CE);
}
case CK_IntegralComplexToBoolean:
case CK_FloatingComplexToBoolean: {
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
return this->emitComplexBoolCast(SubExpr);
}
case CK_IntegralComplexToReal:
case CK_FloatingComplexToReal:
return this->emitComplexReal(SubExpr);
case CK_IntegralRealToComplex:
case CK_FloatingRealToComplex: {
// We're creating a complex value here, so we need to
// allocate storage for it.
if (!Initializing) {
std::optional<unsigned> LocalIndex =
allocateLocal(CE, /*IsExtended=*/true);
if (!LocalIndex)
return false;
if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
// Init the complex value to {SubExpr, 0}.
if (!this->visitArrayElemInit(0, SubExpr))
return false;
// Zero-init the second element.
PrimType T = classifyPrim(SubExpr->getType());
if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
return false;
return this->emitInitElem(T, 1, SubExpr);
}
case CK_IntegralComplexCast:
case CK_FloatingComplexCast:
case CK_IntegralComplexToFloatingComplex:
case CK_FloatingComplexToIntegralComplex: {
assert(CE->getType()->isAnyComplexType());
assert(SubExpr->getType()->isAnyComplexType());
if (DiscardResult)
return this->discard(SubExpr);
if (!Initializing) {
std::optional<unsigned> LocalIndex =
allocateLocal(CE, /*IsExtended=*/true);
if (!LocalIndex)
return false;
if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
// Location for the SubExpr.
// Since SubExpr is of complex type, visiting it results in a pointer
// anyway, so we just create a temporary pointer variable.
unsigned SubExprOffset = allocateLocalPrimitive(
SubExpr, PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
if (!this->visit(SubExpr))
return false;
if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
return false;
PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
QualType DestElemType =
CE->getType()->getAs<ComplexType>()->getElementType();
PrimType DestElemT = classifyPrim(DestElemType);
// Cast both elements individually.
for (unsigned I = 0; I != 2; ++I) {
if (!this->emitGetLocal(PT_Ptr, SubExprOffset, CE))
return false;
if (!this->emitArrayElemPop(SourceElemT, I, CE))
return false;
// Do the cast.
if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, CE))
return false;
// Save the value.
if (!this->emitInitElem(DestElemT, I, CE))
return false;
}
return true;
}
case CK_ToVoid:
return discard(SubExpr);
default:
return this->emitInvalid(CE);
}
llvm_unreachable("Unhandled clang::CastKind enum");
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
if (DiscardResult)
return true;
return this->emitConst(LE->getValue(), LE);
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
if (DiscardResult)
return true;
return this->emitConstFloat(E->getValue(), E);
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitImaginaryLiteral(
const ImaginaryLiteral *E) {
assert(E->getType()->isAnyComplexType());
if (DiscardResult)
return true;
if (!Initializing) {
std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/false);
if (!LocalIndex)
return false;
if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
const Expr *SubExpr = E->getSubExpr();
PrimType SubExprT = classifyPrim(SubExpr->getType());
if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
return false;
if (!this->emitInitElem(SubExprT, 0, SubExpr))
return false;
return this->visitArrayElemInit(1, SubExpr);
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitParenExpr(const ParenExpr *E) {
return this->delegate(E->getSubExpr());
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
// Need short-circuiting for these.
if (BO->isLogicalOp())
return this->VisitLogicalBinOp(BO);
const Expr *LHS = BO->getLHS();
const Expr *RHS = BO->getRHS();
// Handle comma operators. Just discard the LHS
// and delegate to RHS.
if (BO->isCommaOp()) {
if (!this->discard(LHS))
return false;
if (RHS->getType()->isVoidType())
return this->discard(RHS);
return this->delegate(RHS);
}
if (BO->getType()->isAnyComplexType())
return this->VisitComplexBinOp(BO);
if ((LHS->getType()->isAnyComplexType() ||
RHS->getType()->isAnyComplexType()) &&
BO->isComparisonOp())
return this->emitComplexComparison(LHS, RHS, BO);
if (BO->isPtrMemOp())
return this->visit(RHS);
// Typecheck the args.
std::optional<PrimType> LT = classify(LHS->getType());
std::optional<PrimType> RT = classify(RHS->getType());
std::optional<PrimType> T = classify(BO->getType());
// Special case for C++'s three-way/spaceship operator <=>, which
// returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
// have a PrimType).
if (!T && BO->getOpcode() == BO_Cmp) {
if (DiscardResult)
return true;
const ComparisonCategoryInfo *CmpInfo =
Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
assert(CmpInfo);
// We need a temporary variable holding our return value.
if (!Initializing) {
std::optional<unsigned> ResultIndex = this->allocateLocal(BO, false);
if (!this->emitGetPtrLocal(*ResultIndex, BO))
return false;
}
if (!visit(LHS) || !visit(RHS))
return false;
return this->emitCMP3(*LT, CmpInfo, BO);
}
if (!LT || !RT || !T)
return false;
// Pointer arithmetic special case.
if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
if (T == PT_Ptr || (LT == PT_Ptr && RT == PT_Ptr))
return this->VisitPointerArithBinOp(BO);
}
if (!visit(LHS) || !visit(RHS))
return false;
// For languages such as C, cast the result of one
// of our comparision opcodes to T (which is usually int).
auto MaybeCastToBool = [this, T, BO](bool Result) {
if (!Result)
return false;
if (DiscardResult)
return this->emitPop(*T, BO);
if (T != PT_Bool)
return this->emitCast(PT_Bool, *T, BO);
return true;
};
auto Discard = [this, T, BO](bool Result) {
if (!Result)
return false;
return DiscardResult ? this->emitPop(*T, BO) : true;
};
switch (BO->getOpcode()) {
case BO_EQ:
return MaybeCastToBool(this->emitEQ(*LT, BO));
case BO_NE:
return MaybeCastToBool(this->emitNE(*LT, BO));
case BO_LT:
return MaybeCastToBool(this->emitLT(*LT, BO));
case BO_LE:
return MaybeCastToBool(this->emitLE(*LT, BO));
case BO_GT:
return MaybeCastToBool(this->emitGT(*LT, BO));
case BO_GE:
return MaybeCastToBool(this->emitGE(*LT, BO));
case BO_Sub:
if (BO->getType()->isFloatingType())
return Discard(this->emitSubf(getRoundingMode(BO), BO));
return Discard(this->emitSub(*T, BO));
case BO_Add:
if (BO->getType()->isFloatingType())
return Discard(this->emitAddf(getRoundingMode(BO), BO));
return Discard(this->emitAdd(*T, BO));
case BO_Mul:
if (BO->getType()->isFloatingType())
return Discard(this->emitMulf(getRoundingMode(BO), BO));
return Discard(this->emitMul(*T, BO));
case BO_Rem:
return Discard(this->emitRem(*T, BO));
case BO_Div:
if (BO->getType()->isFloatingType())
return Discard(this->emitDivf(getRoundingMode(BO), BO));
return Discard(this->emitDiv(*T, BO));
case BO_Assign:
if (DiscardResult)
return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
: this->emitStorePop(*T, BO);
if (LHS->refersToBitField()) {
if (!this->emitStoreBitField(*T, BO))
return false;
} else {
if (!this->emitStore(*T, BO))
return false;
}
// Assignments aren't necessarily lvalues in C.
// Load from them in that case.
if (!BO->isLValue())
return this->emitLoadPop(*T, BO);
return true;
case BO_And:
return Discard(this->emitBitAnd(*T, BO));
case BO_Or:
return Discard(this->emitBitOr(*T, BO));
case BO_Shl:
return Discard(this->emitShl(*LT, *RT, BO));
case BO_Shr:
return Discard(this->emitShr(*LT, *RT, BO));
case BO_Xor:
return Discard(this->emitBitXor(*T, BO));
case BO_LOr:
case BO_LAnd:
llvm_unreachable("Already handled earlier");
default:
return false;
}
llvm_unreachable("Unhandled binary op");
}
/// Perform addition/subtraction of a pointer and an integer or
/// subtraction of two pointers.
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
BinaryOperatorKind Op = E->getOpcode();
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
if ((Op != BO_Add && Op != BO_Sub) ||
(!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
return false;
std::optional<PrimType> LT = classify(LHS);
std::optional<PrimType> RT = classify(RHS);
if (!LT || !RT)
return false;
if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
if (Op != BO_Sub)
return false;
assert(E->getType()->isIntegerType());
if (!visit(RHS) || !visit(LHS))
return false;
return this->emitSubPtr(classifyPrim(E->getType()), E);
}
PrimType OffsetType;
if (LHS->getType()->isIntegerType()) {
if (!visit(RHS) || !visit(LHS))
return false;
OffsetType = *LT;
} else if (RHS->getType()->isIntegerType()) {
if (!visit(LHS) || !visit(RHS))
return false;
OffsetType = *RT;
} else {
return false;
}
if (Op == BO_Add)
return this->emitAddOffset(OffsetType, E);
else if (Op == BO_Sub)
return this->emitSubOffset(OffsetType, E);
return false;
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
assert(E->isLogicalOp());
BinaryOperatorKind Op = E->getOpcode();
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
std::optional<PrimType> T = classify(E->getType());
if (Op == BO_LOr) {
// Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
LabelTy LabelTrue = this->getLabel();
LabelTy LabelEnd = this->getLabel();
if (!this->visitBool(LHS))
return false;
if (!this->jumpTrue(LabelTrue))
return false;
if (!this->visitBool(RHS))
return false;
if (!this->jump(LabelEnd))
return false;
this->emitLabel(LabelTrue);
this->emitConstBool(true, E);
this->fallthrough(LabelEnd);
this->emitLabel(LabelEnd);
} else {
assert(Op == BO_LAnd);
// Logical AND.
// Visit LHS. Only visit RHS if LHS was TRUE.
LabelTy LabelFalse = this->getLabel();
LabelTy LabelEnd = this->getLabel();
if (!this->visitBool(LHS))
return false;
if (!this->jumpFalse(LabelFalse))
return false;
if (!this->visitBool(RHS))
return false;
if (!this->jump(LabelEnd))
return false;
this->emitLabel(LabelFalse);
this->emitConstBool(false, E);
this->fallthrough(LabelEnd);
this->emitLabel(LabelEnd);
}
if (DiscardResult)
return this->emitPopBool(E);
// For C, cast back to integer type.
assert(T);
if (T != PT_Bool)
return this->emitCast(PT_Bool, *T, E);
return true;
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
// Prepare storage for result.
if (!Initializing) {
std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/false);
if (!LocalIndex)
return false;
if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
// Both LHS and RHS might _not_ be of complex type, but one of them
// needs to be.
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
PrimType ResultElemT = this->classifyComplexElementType(E->getType());
unsigned ResultOffset = ~0u;
if (!DiscardResult)
ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
// Save result pointer in ResultOffset
if (!this->DiscardResult) {
if (!this->emitDupPtr(E))
return false;
if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
return false;
}
QualType LHSType = LHS->getType();
if (const auto *AT = LHSType->getAs<AtomicType>())
LHSType = AT->getValueType();
QualType RHSType = RHS->getType();
if (const auto *AT = RHSType->getAs<AtomicType>())
RHSType = AT->getValueType();
// Evaluate LHS and save value to LHSOffset.
bool LHSIsComplex;
unsigned LHSOffset;
if (LHSType->isAnyComplexType()) {
LHSIsComplex = true;
LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
if (!this->visit(LHS))
return false;
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
return false;
} else {
LHSIsComplex = false;
PrimType LHST = classifyPrim(LHSType);
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
if (!this->visit(LHS))
return false;
if (!this->emitSetLocal(LHST, LHSOffset, E))
return false;
}
// Same with RHS.
bool RHSIsComplex;
unsigned RHSOffset;
if (RHSType->isAnyComplexType()) {
RHSIsComplex = true;
RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
if (!this->visit(RHS))
return false;
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
return false;
} else {
RHSIsComplex = false;
PrimType RHST = classifyPrim(RHSType);
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
if (!this->visit(RHS))
return false;
if (!this->emitSetLocal(RHST, RHSOffset, E))
return false;
}
// For both LHS and RHS, either load the value from the complex pointer, or
// directly from the local variable. For index 1 (i.e. the imaginary part),
// just load 0 and do the operation anyway.
auto loadComplexValue = [this](bool IsComplex, unsigned ElemIndex,
unsigned Offset, const Expr *E) -> bool {
if (IsComplex) {
if (!this->emitGetLocal(PT_Ptr, Offset, E))
return false;
return this->emitArrayElemPop(classifyComplexElementType(E->getType()),
ElemIndex, E);
}
if (ElemIndex == 0)
return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
E);
};
// Now we can get pointers to the LHS and RHS from the offsets above.
BinaryOperatorKind Op = E->getOpcode();
for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
// Result pointer for the store later.
if (!this->DiscardResult) {
if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
return false;
}
if (!loadComplexValue(LHSIsComplex, ElemIndex, LHSOffset, LHS))
return false;
if (!loadComplexValue(RHSIsComplex, ElemIndex, RHSOffset, RHS))
return false;
// The actual operation.
switch (Op) {
case BO_Add:
if (ResultElemT == PT_Float) {
if (!this->emitAddf(getRoundingMode(E), E))
return false;
} else {
if (!this->emitAdd(ResultElemT, E))
return false;
}
break;
case BO_Sub:
if (ResultElemT == PT_Float) {
if (!this->emitSubf(getRoundingMode(E), E))
return false;
} else {
if (!this->emitSub(ResultElemT, E))
return false;
}
break;
default:
return false;
}
if (!this->DiscardResult) {
// Initialize array element with the value we just computed.
if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
return false;
} else {
if (!this->emitPop(ResultElemT, E))
return false;
}
}
return true;
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
QualType QT = E->getType();
if (std::optional<PrimType> T = classify(QT))
return this->visitZeroInitializer(*T, QT, E);
if (QT->isRecordType())
return false;
if (QT->isIncompleteArrayType())
return true;
if (QT->isArrayType()) {
const ArrayType *AT = QT->getAsArrayTypeUnsafe();
assert(AT);
const auto *CAT = cast<ConstantArrayType>(AT);
size_t NumElems = CAT->getZExtSize();
PrimType ElemT = classifyPrim(CAT->getElementType());
for (size_t I = 0; I != NumElems; ++I) {
if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E))
return false;
if (!this->emitInitElem(ElemT, I, E))
return false;
}
return true;
}
if (QT->isAnyComplexType()) {
assert(Initializing);
QualType ElemQT = QT->getAs<ComplexType>()->getElementType();
PrimType ElemT = classifyPrim(ElemQT);
for (unsigned I = 0; I < 2; ++I) {
if (!this->visitZeroInitializer(ElemT, ElemQT, E))
return false;
if (!this->emitInitElem(ElemT, I, E))
return false;
}
return true;
}
return false;
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr(
const ArraySubscriptExpr *E) {
const Expr *Base = E->getBase();
const Expr *Index = E->getIdx();
if (DiscardResult)
return this->discard(Base) && this->discard(Index);
// Take pointer of LHS, add offset from RHS.
// What's left on the stack after this is a pointer.
if (!this->visit(Base))
return false;
if (!this->visit(Index))
return false;
PrimType IndexT = classifyPrim(Index->getType());
return this->emitArrayElemPtrPop(IndexT, E);
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
const Expr *E) {
assert(E->getType()->isRecordType());
const Record *R = getRecord(E->getType());
if (Inits.size() == 1 && E->getType() == Inits[0]->getType()) {
return this->visitInitializer(Inits[0]);
}
unsigned InitIndex = 0;
for (const Expr *Init : Inits) {
if (!this->emitDupPtr(E))
return false;
// guard relatively expensive base check behind an almost-always-false
// branch. this works because all bases must be initialized first before we
// initialize any direct fields
if (InitIndex == 0) {
// Initializer for a direct base class?
if (const Record::Base *B = R->getBase(Init->getType())) {
if (!this->emitGetPtrBasePop(B->Offset, Init))
return false;
if (!this->visitInitializer(Init))
return false;
if (!this->emitFinishInitPop(E))
return false;
// Base initializers don't increase InitIndex, since they don't count
// into the Record's fields.
continue;
}
} else {
assert(!R->getBase(Init->getType()));
}
// skip over padding-only fields
while (R->getField(InitIndex)->Decl->isUnnamedBitfield())
++InitIndex;
const Record::Field *FieldToInit = R->getField(InitIndex++);
if (std::optional<PrimType> T = classify(Init)) {
assert(classifyPrim(FieldToInit->Decl->getType()) == *T);
if (!this->visit(Init))
return false;
if (FieldToInit->isBitField()) {
if (!this->emitInitBitField(*T, FieldToInit, E))
return false;
} else {
if (!this->emitInitField(*T, FieldToInit->Offset, E))
return false;
}
if (!this->emitPopPtr(E))
return false;
} else {
// Non-primitive case. Get a pointer to the field-to-initialize
// on the stack and recurse into visitInitializer().
if (!this->emitGetPtrField(FieldToInit->Offset, Init))
return false;
if (!this->visitInitializer(Init))
return false;
if (!this->emitPopPtr(E))
return false;
}
}
return true;
}
/// Pointer to the array(not the element!) must be on the stack when calling
/// this.
template <class Emitter>
bool ByteCodeExprGen<Emitter>::visitArrayElemInit(unsigned ElemIndex,
const Expr *Init) {
if (std::optional<PrimType> T = classify(Init->getType())) {
// Visit the primitive element like normal.
if (!this->visit(Init))
return false;
return this->emitInitElem(*T, ElemIndex, Init);
}
// Advance the pointer currently on the stack to the given
// dimension.
if (!this->emitConstUint32(ElemIndex, Init))
return false;
if (!this->emitArrayElemPtrUint32(Init))
return false;
if (!this->visitInitializer(Init))
return false;
return this->emitFinishInitPop(Init);
}
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
// Handle discarding first.
if (DiscardResult) {
for (const Expr *Init : E->inits()) {
if (!this->discard(Init))
return false;
}
return true;
}
// Primitive values.
if (std::optional<PrimType> T = classify(E->getType())) {
assert(!DiscardResult);
if (E->getNumInits() == 0)
return this->visitZeroInitializer(*T, E->getType(), E);
assert(E->getNumInits() == 1);
return this->delegate(E->inits()[0]);
}
QualType T = E->getType();
if (T->isRecordType())
return this->visitInitList(E->inits(), E);
if (T->isArrayType()) {
unsigned ElementIndex = 0;
for (const Expr *Init : E->inits()) {
if (!this->visitArrayElemInit(ElementIndex, Init))
return false;
++ElementIndex;
}