1 //===--- ByteCodeExprGen.cpp - Code generator for expressions ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ByteCodeExprGen.h"
10 #include "ByteCodeEmitter.h"
11 #include "ByteCodeGenError.h"
12 #include "ByteCodeStmtGen.h"
20 using namespace clang
;
21 using namespace clang::interp
;
23 using APSInt
= llvm::APSInt
;
28 /// Scope used to handle temporaries in toplevel variable declarations.
29 template <class Emitter
> class DeclScope final
: public VariableScope
<Emitter
> {
31 DeclScope(ByteCodeExprGen
<Emitter
> *Ctx
, const ValueDecl
*VD
)
32 : VariableScope
<Emitter
>(Ctx
), Scope(Ctx
->P
, VD
),
33 OldGlobalDecl(Ctx
->GlobalDecl
) {
34 Ctx
->GlobalDecl
= Context::shouldBeGloballyIndexed(VD
);
37 void addExtended(const Scope::Local
&Local
) override
{
38 return this->addLocal(Local
);
41 ~DeclScope() { this->Ctx
->GlobalDecl
= OldGlobalDecl
; }
44 Program::DeclScope Scope
;
48 /// Scope used to handle initialization methods.
49 template <class Emitter
> class OptionScope final
{
51 /// Root constructor, compiling or discarding primitives.
52 OptionScope(ByteCodeExprGen
<Emitter
> *Ctx
, bool NewDiscardResult
,
54 : Ctx(Ctx
), OldDiscardResult(Ctx
->DiscardResult
),
55 OldInitializing(Ctx
->Initializing
) {
56 Ctx
->DiscardResult
= NewDiscardResult
;
57 Ctx
->Initializing
= NewInitializing
;
61 Ctx
->DiscardResult
= OldDiscardResult
;
62 Ctx
->Initializing
= OldInitializing
;
67 ByteCodeExprGen
<Emitter
> *Ctx
;
68 /// Old discard flag to restore.
69 bool OldDiscardResult
;
76 template <class Emitter
>
77 bool ByteCodeExprGen
<Emitter
>::VisitCastExpr(const CastExpr
*CE
) {
78 auto *SubExpr
= CE
->getSubExpr();
79 switch (CE
->getCastKind()) {
81 case CK_LValueToRValue
: {
83 SubExpr
, DerefKind::Read
,
85 // Value loaded - nothing to do here.
88 [this, CE
](PrimType T
) {
89 // Pointer on stack - dereference it.
90 if (!this->emitLoadPop(T
, CE
))
92 return DiscardResult
? this->emitPop(T
, CE
) : true;
96 case CK_UncheckedDerivedToBase
:
97 case CK_DerivedToBase
: {
98 if (!this->visit(SubExpr
))
101 unsigned DerivedOffset
= collectBaseOffset(getRecordTy(CE
->getType()),
102 getRecordTy(SubExpr
->getType()));
104 return this->emitGetPtrBasePop(DerivedOffset
, CE
);
107 case CK_BaseToDerived
: {
108 if (!this->visit(SubExpr
))
111 unsigned DerivedOffset
= collectBaseOffset(getRecordTy(SubExpr
->getType()),
112 getRecordTy(CE
->getType()));
114 return this->emitGetPtrDerivedPop(DerivedOffset
, CE
);
117 case CK_FloatingCast
: {
118 if (!this->visit(SubExpr
))
120 const auto *TargetSemantics
= &Ctx
.getFloatSemantics(CE
->getType());
121 return this->emitCastFP(TargetSemantics
, getRoundingMode(CE
), CE
);
124 case CK_IntegralToFloating
: {
125 std::optional
<PrimType
> FromT
= classify(SubExpr
->getType());
129 if (!this->visit(SubExpr
))
132 const auto *TargetSemantics
= &Ctx
.getFloatSemantics(CE
->getType());
133 llvm::RoundingMode RM
= getRoundingMode(CE
);
134 return this->emitCastIntegralFloating(*FromT
, TargetSemantics
, RM
, CE
);
137 case CK_FloatingToBoolean
:
138 case CK_FloatingToIntegral
: {
139 std::optional
<PrimType
> ToT
= classify(CE
->getType());
144 if (!this->visit(SubExpr
))
148 return this->emitCastFloatingIntegralAP(Ctx
.getBitWidth(CE
->getType()),
150 if (ToT
== PT_IntAPS
)
151 return this->emitCastFloatingIntegralAPS(Ctx
.getBitWidth(CE
->getType()),
154 return this->emitCastFloatingIntegral(*ToT
, CE
);
157 case CK_NullToPointer
:
160 return this->emitNull(classifyPrim(CE
->getType()), CE
);
162 case CK_PointerToIntegral
: {
163 // TODO: Discard handling.
164 if (!this->visit(SubExpr
))
167 PrimType T
= classifyPrim(CE
->getType());
168 return this->emitCastPointerIntegral(T
, CE
);
171 case CK_ArrayToPointerDecay
: {
172 if (!this->visit(SubExpr
))
174 if (!this->emitArrayDecay(CE
))
177 return this->emitPopPtr(CE
);
181 case CK_AtomicToNonAtomic
:
182 case CK_ConstructorConversion
:
183 case CK_FunctionToPointerDecay
:
184 case CK_NonAtomicToAtomic
:
186 case CK_UserDefinedConversion
:
188 return this->delegate(SubExpr
);
190 case CK_IntegralToBoolean
:
191 case CK_IntegralCast
: {
193 return this->discard(SubExpr
);
194 std::optional
<PrimType
> FromT
= classify(SubExpr
->getType());
195 std::optional
<PrimType
> ToT
= classify(CE
->getType());
200 if (!this->visit(SubExpr
))
204 assert(ToT
!= PT_IntAP
&& ToT
!= PT_IntAPS
);
209 return this->emitCastAP(*FromT
, Ctx
.getBitWidth(CE
->getType()), CE
);
210 if (ToT
== PT_IntAPS
)
211 return this->emitCastAPS(*FromT
, Ctx
.getBitWidth(CE
->getType()), CE
);
213 return this->emitCast(*FromT
, *ToT
, CE
);
216 case CK_PointerToBoolean
: {
217 PrimType PtrT
= classifyPrim(SubExpr
->getType());
219 // Just emit p != nullptr for this.
220 if (!this->visit(SubExpr
))
223 if (!this->emitNull(PtrT
, CE
))
226 return this->emitNE(PtrT
, CE
);
230 return discard(SubExpr
);
233 assert(false && "Cast not implemented");
235 llvm_unreachable("Unhandled clang::CastKind enum");
238 template <class Emitter
>
239 bool ByteCodeExprGen
<Emitter
>::VisitIntegerLiteral(const IntegerLiteral
*LE
) {
243 return this->emitConst(LE
->getValue(), LE
);
246 template <class Emitter
>
247 bool ByteCodeExprGen
<Emitter
>::VisitFloatingLiteral(const FloatingLiteral
*E
) {
251 return this->emitConstFloat(E
->getValue(), E
);
254 template <class Emitter
>
255 bool ByteCodeExprGen
<Emitter
>::VisitParenExpr(const ParenExpr
*E
) {
256 return this->delegate(E
->getSubExpr());
259 template <class Emitter
>
260 bool ByteCodeExprGen
<Emitter
>::VisitBinaryOperator(const BinaryOperator
*BO
) {
261 // Need short-circuiting for these.
262 if (BO
->isLogicalOp())
263 return this->VisitLogicalBinOp(BO
);
265 const Expr
*LHS
= BO
->getLHS();
266 const Expr
*RHS
= BO
->getRHS();
268 if (BO
->isPtrMemOp())
269 return this->visit(RHS
);
271 // Typecheck the args.
272 std::optional
<PrimType
> LT
= classify(LHS
->getType());
273 std::optional
<PrimType
> RT
= classify(RHS
->getType());
274 std::optional
<PrimType
> T
= classify(BO
->getType());
276 // Deal with operations which have composite or void types.
277 if (BO
->isCommaOp()) {
278 if (!this->discard(LHS
))
280 if (RHS
->getType()->isVoidType())
281 return this->discard(RHS
);
283 return this->delegate(RHS
);
286 // Special case for C++'s three-way/spaceship operator <=>, which
287 // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
292 const ComparisonCategoryInfo
*CmpInfo
=
293 Ctx
.getASTContext().CompCategories
.lookupInfoForType(BO
->getType());
296 // We need a temporary variable holding our return value.
298 std::optional
<unsigned> ResultIndex
= this->allocateLocal(BO
, false);
299 if (!this->emitGetPtrLocal(*ResultIndex
, BO
))
303 if (!visit(LHS
) || !visit(RHS
))
306 return this->emitCMP3(*LT
, CmpInfo
, BO
);
309 if (!LT
|| !RT
|| !T
)
310 return this->bail(BO
);
312 // Pointer arithmetic special case.
313 if (BO
->getOpcode() == BO_Add
|| BO
->getOpcode() == BO_Sub
) {
314 if (T
== PT_Ptr
|| (LT
== PT_Ptr
&& RT
== PT_Ptr
))
315 return this->VisitPointerArithBinOp(BO
);
318 if (!visit(LHS
) || !visit(RHS
))
321 // For languages such as C, cast the result of one
322 // of our comparision opcodes to T (which is usually int).
323 auto MaybeCastToBool
= [this, T
, BO
](bool Result
) {
327 return this->emitPop(*T
, BO
);
329 return this->emitCast(PT_Bool
, *T
, BO
);
333 auto Discard
= [this, T
, BO
](bool Result
) {
336 return DiscardResult
? this->emitPop(*T
, BO
) : true;
339 switch (BO
->getOpcode()) {
341 return MaybeCastToBool(this->emitEQ(*LT
, BO
));
343 return MaybeCastToBool(this->emitNE(*LT
, BO
));
345 return MaybeCastToBool(this->emitLT(*LT
, BO
));
347 return MaybeCastToBool(this->emitLE(*LT
, BO
));
349 return MaybeCastToBool(this->emitGT(*LT
, BO
));
351 return MaybeCastToBool(this->emitGE(*LT
, BO
));
353 if (BO
->getType()->isFloatingType())
354 return Discard(this->emitSubf(getRoundingMode(BO
), BO
));
355 return Discard(this->emitSub(*T
, BO
));
357 if (BO
->getType()->isFloatingType())
358 return Discard(this->emitAddf(getRoundingMode(BO
), BO
));
359 return Discard(this->emitAdd(*T
, BO
));
361 if (BO
->getType()->isFloatingType())
362 return Discard(this->emitMulf(getRoundingMode(BO
), BO
));
363 return Discard(this->emitMul(*T
, BO
));
365 return Discard(this->emitRem(*T
, BO
));
367 if (BO
->getType()->isFloatingType())
368 return Discard(this->emitDivf(getRoundingMode(BO
), BO
));
369 return Discard(this->emitDiv(*T
, BO
));
372 return LHS
->refersToBitField() ? this->emitStoreBitFieldPop(*T
, BO
)
373 : this->emitStorePop(*T
, BO
);
374 return LHS
->refersToBitField() ? this->emitStoreBitField(*T
, BO
)
375 : this->emitStore(*T
, BO
);
377 return Discard(this->emitBitAnd(*T
, BO
));
379 return Discard(this->emitBitOr(*T
, BO
));
381 return Discard(this->emitShl(*LT
, *RT
, BO
));
383 return Discard(this->emitShr(*LT
, *RT
, BO
));
385 return Discard(this->emitBitXor(*T
, BO
));
388 llvm_unreachable("Already handled earlier");
390 return this->bail(BO
);
393 llvm_unreachable("Unhandled binary op");
396 /// Perform addition/subtraction of a pointer and an integer or
397 /// subtraction of two pointers.
398 template <class Emitter
>
399 bool ByteCodeExprGen
<Emitter
>::VisitPointerArithBinOp(const BinaryOperator
*E
) {
400 BinaryOperatorKind Op
= E
->getOpcode();
401 const Expr
*LHS
= E
->getLHS();
402 const Expr
*RHS
= E
->getRHS();
404 if ((Op
!= BO_Add
&& Op
!= BO_Sub
) ||
405 (!LHS
->getType()->isPointerType() && !RHS
->getType()->isPointerType()))
408 std::optional
<PrimType
> LT
= classify(LHS
);
409 std::optional
<PrimType
> RT
= classify(RHS
);
414 if (LHS
->getType()->isPointerType() && RHS
->getType()->isPointerType()) {
418 assert(E
->getType()->isIntegerType());
419 if (!visit(RHS
) || !visit(LHS
))
422 return this->emitSubPtr(classifyPrim(E
->getType()), E
);
426 if (LHS
->getType()->isIntegerType()) {
427 if (!visit(RHS
) || !visit(LHS
))
430 } else if (RHS
->getType()->isIntegerType()) {
431 if (!visit(LHS
) || !visit(RHS
))
439 return this->emitAddOffset(OffsetType
, E
);
440 else if (Op
== BO_Sub
)
441 return this->emitSubOffset(OffsetType
, E
);
443 return this->bail(E
);
446 template <class Emitter
>
447 bool ByteCodeExprGen
<Emitter
>::VisitLogicalBinOp(const BinaryOperator
*E
) {
448 assert(E
->isLogicalOp());
449 BinaryOperatorKind Op
= E
->getOpcode();
450 const Expr
*LHS
= E
->getLHS();
451 const Expr
*RHS
= E
->getRHS();
452 std::optional
<PrimType
> T
= classify(E
->getType());
455 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
456 LabelTy LabelTrue
= this->getLabel();
457 LabelTy LabelEnd
= this->getLabel();
459 if (!this->visitBool(LHS
))
461 if (!this->jumpTrue(LabelTrue
))
464 if (!this->visitBool(RHS
))
466 if (!this->jump(LabelEnd
))
469 this->emitLabel(LabelTrue
);
470 this->emitConstBool(true, E
);
471 this->fallthrough(LabelEnd
);
472 this->emitLabel(LabelEnd
);
475 assert(Op
== BO_LAnd
);
477 // Visit LHS. Only visit RHS if LHS was TRUE.
478 LabelTy LabelFalse
= this->getLabel();
479 LabelTy LabelEnd
= this->getLabel();
481 if (!this->visitBool(LHS
))
483 if (!this->jumpFalse(LabelFalse
))
486 if (!this->visitBool(RHS
))
488 if (!this->jump(LabelEnd
))
491 this->emitLabel(LabelFalse
);
492 this->emitConstBool(false, E
);
493 this->fallthrough(LabelEnd
);
494 this->emitLabel(LabelEnd
);
498 return this->emitPopBool(E
);
500 // For C, cast back to integer type.
503 return this->emitCast(PT_Bool
, *T
, E
);
507 template <class Emitter
>
508 bool ByteCodeExprGen
<Emitter
>::VisitImplicitValueInitExpr(const ImplicitValueInitExpr
*E
) {
509 QualType QT
= E
->getType();
511 if (std::optional
<PrimType
> T
= classify(QT
))
512 return this->visitZeroInitializer(*T
, QT
, E
);
514 if (QT
->isRecordType())
517 if (QT
->isIncompleteArrayType())
520 if (QT
->isArrayType()) {
521 const ArrayType
*AT
= QT
->getAsArrayTypeUnsafe();
523 const auto *CAT
= cast
<ConstantArrayType
>(AT
);
524 size_t NumElems
= CAT
->getSize().getZExtValue();
525 PrimType ElemT
= classifyPrim(CAT
->getElementType());
527 for (size_t I
= 0; I
!= NumElems
; ++I
) {
528 if (!this->visitZeroInitializer(ElemT
, CAT
->getElementType(), E
))
530 if (!this->emitInitElem(ElemT
, I
, E
))
540 template <class Emitter
>
541 bool ByteCodeExprGen
<Emitter
>::VisitArraySubscriptExpr(
542 const ArraySubscriptExpr
*E
) {
543 const Expr
*Base
= E
->getBase();
544 const Expr
*Index
= E
->getIdx();
547 return this->discard(Base
) && this->discard(Index
);
549 // Take pointer of LHS, add offset from RHS.
550 // What's left on the stack after this is a pointer.
551 if (!this->visit(Base
))
554 if (!this->visit(Index
))
557 PrimType IndexT
= classifyPrim(Index
->getType());
558 return this->emitArrayElemPtrPop(IndexT
, E
);
561 template <class Emitter
>
562 bool ByteCodeExprGen
<Emitter
>::visitInitList(ArrayRef
<const Expr
*> Inits
,
564 assert(E
->getType()->isRecordType());
565 const Record
*R
= getRecord(E
->getType());
567 unsigned InitIndex
= 0;
568 for (const Expr
*Init
: Inits
) {
569 if (!this->emitDupPtr(E
))
572 if (std::optional
<PrimType
> T
= classify(Init
)) {
573 const Record::Field
*FieldToInit
= R
->getField(InitIndex
);
574 if (!this->visit(Init
))
577 if (FieldToInit
->isBitField()) {
578 if (!this->emitInitBitField(*T
, FieldToInit
, E
))
581 if (!this->emitInitField(*T
, FieldToInit
->Offset
, E
))
585 if (!this->emitPopPtr(E
))
589 // Initializer for a direct base class.
590 if (const Record::Base
*B
= R
->getBase(Init
->getType())) {
591 if (!this->emitGetPtrBasePop(B
->Offset
, Init
))
594 if (!this->visitInitializer(Init
))
597 if (!this->emitInitPtrPop(E
))
599 // Base initializers don't increase InitIndex, since they don't count
600 // into the Record's fields.
602 const Record::Field
*FieldToInit
= R
->getField(InitIndex
);
603 // Non-primitive case. Get a pointer to the field-to-initialize
604 // on the stack and recurse into visitInitializer().
605 if (!this->emitGetPtrField(FieldToInit
->Offset
, Init
))
608 if (!this->visitInitializer(Init
))
611 if (!this->emitPopPtr(E
))
620 template <class Emitter
>
621 bool ByteCodeExprGen
<Emitter
>::VisitInitListExpr(const InitListExpr
*E
) {
622 // Handle discarding first.
624 for (const Expr
*Init
: E
->inits()) {
625 if (!this->discard(Init
))
632 if (std::optional
<PrimType
> T
= classify(E
->getType())) {
633 assert(!DiscardResult
);
634 if (E
->getNumInits() == 0)
635 return this->visitZeroInitializer(*T
, E
->getType(), E
);
636 assert(E
->getNumInits() == 1);
637 return this->delegate(E
->inits()[0]);
640 QualType T
= E
->getType();
641 if (T
->isRecordType())
642 return this->visitInitList(E
->inits(), E
);
644 if (T
->isArrayType()) {
645 // FIXME: Array fillers.
646 unsigned ElementIndex
= 0;
647 for (const Expr
*Init
: E
->inits()) {
648 if (std::optional
<PrimType
> T
= classify(Init
->getType())) {
649 // Visit the primitive element like normal.
650 if (!this->visit(Init
))
652 if (!this->emitInitElem(*T
, ElementIndex
, Init
))
655 // Advance the pointer currently on the stack to the given
657 if (!this->emitConstUint32(ElementIndex
, Init
))
659 if (!this->emitArrayElemPtrUint32(Init
))
661 if (!this->visitInitializer(Init
))
663 if (!this->emitPopPtr(Init
))
675 template <class Emitter
>
676 bool ByteCodeExprGen
<Emitter
>::VisitCXXParenListInitExpr(
677 const CXXParenListInitExpr
*E
) {
679 for (const Expr
*Init
: E
->getInitExprs()) {
680 if (!this->discard(Init
))
686 assert(E
->getType()->isRecordType());
687 return this->visitInitList(E
->getInitExprs(), E
);
690 template <class Emitter
>
691 bool ByteCodeExprGen
<Emitter
>::VisitSubstNonTypeTemplateParmExpr(
692 const SubstNonTypeTemplateParmExpr
*E
) {
693 return this->delegate(E
->getReplacement());
696 template <class Emitter
>
697 bool ByteCodeExprGen
<Emitter
>::VisitConstantExpr(const ConstantExpr
*E
) {
698 // Try to emit the APValue directly, without visiting the subexpr.
699 // This will only fail if we can't emit the APValue, so won't emit any
700 // diagnostics or any double values.
701 std::optional
<PrimType
> T
= classify(E
->getType());
702 if (T
&& E
->hasAPValueResult() &&
703 this->visitAPValue(E
->getAPValueResult(), *T
, E
))
706 return this->delegate(E
->getSubExpr());
709 static CharUnits
AlignOfType(QualType T
, const ASTContext
&ASTCtx
,
710 UnaryExprOrTypeTrait Kind
) {
711 bool AlignOfReturnsPreferred
=
712 ASTCtx
.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7
;
714 // C++ [expr.alignof]p3:
715 // When alignof is applied to a reference type, the result is the
716 // alignment of the referenced type.
717 if (const auto *Ref
= T
->getAs
<ReferenceType
>())
718 T
= Ref
->getPointeeType();
720 // __alignof is defined to return the preferred alignment.
721 // Before 8, clang returned the preferred alignment for alignof and
723 if (Kind
== UETT_PreferredAlignOf
|| AlignOfReturnsPreferred
)
724 return ASTCtx
.toCharUnitsFromBits(ASTCtx
.getPreferredTypeAlign(T
));
726 return ASTCtx
.getTypeAlignInChars(T
);
729 template <class Emitter
>
730 bool ByteCodeExprGen
<Emitter
>::VisitUnaryExprOrTypeTraitExpr(
731 const UnaryExprOrTypeTraitExpr
*E
) {
732 UnaryExprOrTypeTrait Kind
= E
->getKind();
733 ASTContext
&ASTCtx
= Ctx
.getASTContext();
735 if (Kind
== UETT_SizeOf
) {
736 QualType ArgType
= E
->getTypeOfArgument();
738 if (ArgType
->isVoidType() || ArgType
->isFunctionType())
739 Size
= CharUnits::One();
741 if (ArgType
->isDependentType() || !ArgType
->isConstantSizeType())
744 Size
= ASTCtx
.getTypeSizeInChars(ArgType
);
750 return this->emitConst(Size
.getQuantity(), E
);
753 if (Kind
== UETT_AlignOf
|| Kind
== UETT_PreferredAlignOf
) {
756 if (E
->isArgumentType()) {
757 QualType ArgType
= E
->getTypeOfArgument();
759 Size
= AlignOfType(ArgType
, ASTCtx
, Kind
);
761 // Argument is an expression, not a type.
762 const Expr
*Arg
= E
->getArgumentExpr()->IgnoreParens();
764 // The kinds of expressions that we have special-case logic here for
765 // should be kept up to date with the special checks for those
766 // expressions in Sema.
768 // alignof decl is always accepted, even if it doesn't make sense: we
769 // default to 1 in those cases.
770 if (const auto *DRE
= dyn_cast
<DeclRefExpr
>(Arg
))
771 Size
= ASTCtx
.getDeclAlign(DRE
->getDecl(),
772 /*RefAsPointee*/ true);
773 else if (const auto *ME
= dyn_cast
<MemberExpr
>(Arg
))
774 Size
= ASTCtx
.getDeclAlign(ME
->getMemberDecl(),
775 /*RefAsPointee*/ true);
777 Size
= AlignOfType(Arg
->getType(), ASTCtx
, Kind
);
783 return this->emitConst(Size
.getQuantity(), E
);
789 template <class Emitter
>
790 bool ByteCodeExprGen
<Emitter
>::VisitMemberExpr(const MemberExpr
*E
) {
792 const Expr
*Base
= E
->getBase();
795 return this->discard(Base
);
797 if (!this->visit(Base
))
800 // Base above gives us a pointer on the stack.
801 // TODO: Implement non-FieldDecl members.
802 const ValueDecl
*Member
= E
->getMemberDecl();
803 if (const auto *FD
= dyn_cast
<FieldDecl
>(Member
)) {
804 const RecordDecl
*RD
= FD
->getParent();
805 const Record
*R
= getRecord(RD
);
806 const Record::Field
*F
= R
->getField(FD
);
807 // Leave a pointer to the field on the stack.
808 if (F
->Decl
->getType()->isReferenceType())
809 return this->emitGetFieldPop(PT_Ptr
, F
->Offset
, E
);
810 return this->emitGetPtrField(F
->Offset
, E
);
816 template <class Emitter
>
817 bool ByteCodeExprGen
<Emitter
>::VisitArrayInitIndexExpr(
818 const ArrayInitIndexExpr
*E
) {
819 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
820 // stand-alone, e.g. via EvaluateAsInt().
823 return this->emitConst(*ArrayIndex
, E
);
826 template <class Emitter
>
827 bool ByteCodeExprGen
<Emitter
>::VisitArrayInitLoopExpr(
828 const ArrayInitLoopExpr
*E
) {
829 assert(Initializing
);
830 assert(!DiscardResult
);
831 // TODO: This compiles to quite a lot of bytecode if the array is larger.
832 // Investigate compiling this to a loop.
834 const Expr
*SubExpr
= E
->getSubExpr();
835 const Expr
*CommonExpr
= E
->getCommonExpr();
836 size_t Size
= E
->getArraySize().getZExtValue();
837 std::optional
<PrimType
> ElemT
= classify(SubExpr
->getType());
839 // If the common expression is an opaque expression, we visit it
840 // here once so we have its value cached.
841 // FIXME: This might be necessary (or useful) for all expressions.
842 if (isa
<OpaqueValueExpr
>(CommonExpr
)) {
843 if (!this->discard(CommonExpr
))
847 // So, every iteration, we execute an assignment here
848 // where the LHS is on the stack (the target array)
849 // and the RHS is our SubExpr.
850 for (size_t I
= 0; I
!= Size
; ++I
) {
851 ArrayIndexScope
<Emitter
> IndexScope(this, I
);
852 BlockScope
<Emitter
> BS(this);
855 if (!this->visit(SubExpr
))
857 if (!this->emitInitElem(*ElemT
, I
, E
))
860 // Get to our array element and recurse into visitInitializer()
861 if (!this->emitConstUint64(I
, SubExpr
))
863 if (!this->emitArrayElemPtrUint64(SubExpr
))
865 if (!visitInitializer(SubExpr
))
867 if (!this->emitPopPtr(E
))
874 template <class Emitter
>
875 bool ByteCodeExprGen
<Emitter
>::VisitOpaqueValueExpr(const OpaqueValueExpr
*E
) {
877 return this->visitInitializer(E
->getSourceExpr());
879 PrimType SubExprT
= classify(E
->getSourceExpr()).value_or(PT_Ptr
);
880 if (auto It
= OpaqueExprs
.find(E
); It
!= OpaqueExprs
.end())
881 return this->emitGetLocal(SubExprT
, It
->second
, E
);
883 if (!this->visit(E
->getSourceExpr()))
886 // At this point we either have the evaluated source expression or a pointer
887 // to an object on the stack. We want to create a local variable that stores
889 std::optional
<unsigned> LocalIndex
=
890 allocateLocalPrimitive(E
, SubExprT
, /*IsConst=*/true);
893 if (!this->emitSetLocal(SubExprT
, *LocalIndex
, E
))
896 // Here the local variable is created but the value is removed from the stack,
897 // so we put it back, because the caller might need it.
898 if (!DiscardResult
) {
899 if (!this->emitGetLocal(SubExprT
, *LocalIndex
, E
))
903 // FIXME: Ideally the cached value should be cleaned up later.
904 OpaqueExprs
.insert({E
, *LocalIndex
});
909 template <class Emitter
>
910 bool ByteCodeExprGen
<Emitter
>::VisitAbstractConditionalOperator(
911 const AbstractConditionalOperator
*E
) {
912 const Expr
*Condition
= E
->getCond();
913 const Expr
*TrueExpr
= E
->getTrueExpr();
914 const Expr
*FalseExpr
= E
->getFalseExpr();
916 LabelTy LabelEnd
= this->getLabel(); // Label after the operator.
917 LabelTy LabelFalse
= this->getLabel(); // Label for the false expr.
919 if (!this->visitBool(Condition
))
922 if (!this->jumpFalse(LabelFalse
))
925 if (!this->delegate(TrueExpr
))
927 if (!this->jump(LabelEnd
))
930 this->emitLabel(LabelFalse
);
932 if (!this->delegate(FalseExpr
))
935 this->fallthrough(LabelEnd
);
936 this->emitLabel(LabelEnd
);
941 template <class Emitter
>
942 bool ByteCodeExprGen
<Emitter
>::VisitStringLiteral(const StringLiteral
*E
) {
947 unsigned StringIndex
= P
.createGlobalString(E
);
948 return this->emitGetPtrGlobal(StringIndex
, E
);
951 // We are initializing an array on the stack.
952 const ConstantArrayType
*CAT
=
953 Ctx
.getASTContext().getAsConstantArrayType(E
->getType());
954 assert(CAT
&& "a string literal that's not a constant array?");
956 // If the initializer string is too long, a diagnostic has already been
957 // emitted. Read only the array length from the string literal.
958 unsigned ArraySize
= CAT
->getSize().getZExtValue();
959 unsigned N
= std::min(ArraySize
, E
->getLength());
960 size_t CharWidth
= E
->getCharByteWidth();
962 for (unsigned I
= 0; I
!= N
; ++I
) {
963 uint32_t CodeUnit
= E
->getCodeUnit(I
);
965 if (CharWidth
== 1) {
966 this->emitConstSint8(CodeUnit
, E
);
967 this->emitInitElemSint8(I
, E
);
968 } else if (CharWidth
== 2) {
969 this->emitConstUint16(CodeUnit
, E
);
970 this->emitInitElemUint16(I
, E
);
971 } else if (CharWidth
== 4) {
972 this->emitConstUint32(CodeUnit
, E
);
973 this->emitInitElemUint32(I
, E
);
975 llvm_unreachable("unsupported character width");
979 // Fill up the rest of the char array with NUL bytes.
980 for (unsigned I
= N
; I
!= ArraySize
; ++I
) {
981 if (CharWidth
== 1) {
982 this->emitConstSint8(0, E
);
983 this->emitInitElemSint8(I
, E
);
984 } else if (CharWidth
== 2) {
985 this->emitConstUint16(0, E
);
986 this->emitInitElemUint16(I
, E
);
987 } else if (CharWidth
== 4) {
988 this->emitConstUint32(0, E
);
989 this->emitInitElemUint32(I
, E
);
991 llvm_unreachable("unsupported character width");
998 template <class Emitter
>
999 bool ByteCodeExprGen
<Emitter
>::VisitCharacterLiteral(
1000 const CharacterLiteral
*E
) {
1003 return this->emitConst(E
->getValue(), E
);
1006 template <class Emitter
>
1007 bool ByteCodeExprGen
<Emitter
>::VisitFloatCompoundAssignOperator(
1008 const CompoundAssignOperator
*E
) {
1010 const Expr
*LHS
= E
->getLHS();
1011 const Expr
*RHS
= E
->getRHS();
1012 QualType LHSType
= LHS
->getType();
1013 QualType LHSComputationType
= E
->getComputationLHSType();
1014 QualType ResultType
= E
->getComputationResultType();
1015 std::optional
<PrimType
> LT
= classify(LHSComputationType
);
1016 std::optional
<PrimType
> RT
= classify(ResultType
);
1018 assert(ResultType
->isFloatingType());
1023 PrimType LHST
= classifyPrim(LHSType
);
1025 // C++17 onwards require that we evaluate the RHS first.
1026 // Compute RHS and save it in a temporary variable so we can
1027 // load it again later.
1031 unsigned TempOffset
= this->allocateLocalPrimitive(E
, *RT
, /*IsConst=*/true);
1032 if (!this->emitSetLocal(*RT
, TempOffset
, E
))
1035 // First, visit LHS.
1038 if (!this->emitLoad(LHST
, E
))
1041 // If necessary, convert LHS to its computation type.
1042 if (!this->emitPrimCast(LHST
, classifyPrim(LHSComputationType
),
1043 LHSComputationType
, E
))
1047 if (!this->emitGetLocal(*RT
, TempOffset
, E
))
1050 llvm::RoundingMode RM
= getRoundingMode(E
);
1051 switch (E
->getOpcode()) {
1053 if (!this->emitAddf(RM
, E
))
1057 if (!this->emitSubf(RM
, E
))
1061 if (!this->emitMulf(RM
, E
))
1065 if (!this->emitDivf(RM
, E
))
1072 if (!this->emitPrimCast(classifyPrim(ResultType
), LHST
, LHS
->getType(), E
))
1076 return this->emitStorePop(LHST
, E
);
1077 return this->emitStore(LHST
, E
);
1080 template <class Emitter
>
1081 bool ByteCodeExprGen
<Emitter
>::VisitPointerCompoundAssignOperator(
1082 const CompoundAssignOperator
*E
) {
1083 BinaryOperatorKind Op
= E
->getOpcode();
1084 const Expr
*LHS
= E
->getLHS();
1085 const Expr
*RHS
= E
->getRHS();
1086 std::optional
<PrimType
> LT
= classify(LHS
->getType());
1087 std::optional
<PrimType
> RT
= classify(RHS
->getType());
1089 if (Op
!= BO_AddAssign
&& Op
!= BO_SubAssign
)
1094 assert(*LT
== PT_Ptr
);
1099 if (!this->emitLoadPtr(LHS
))
1105 if (Op
== BO_AddAssign
)
1106 this->emitAddOffset(*RT
, E
);
1108 this->emitSubOffset(*RT
, E
);
1111 return this->emitStorePopPtr(E
);
1112 return this->emitStorePtr(E
);
1115 template <class Emitter
>
1116 bool ByteCodeExprGen
<Emitter
>::VisitCompoundAssignOperator(
1117 const CompoundAssignOperator
*E
) {
1119 const Expr
*LHS
= E
->getLHS();
1120 const Expr
*RHS
= E
->getRHS();
1121 std::optional
<PrimType
> LHSComputationT
=
1122 classify(E
->getComputationLHSType());
1123 std::optional
<PrimType
> LT
= classify(LHS
->getType());
1124 std::optional
<PrimType
> RT
= classify(E
->getComputationResultType());
1125 std::optional
<PrimType
> ResultT
= classify(E
->getType());
1127 if (!LT
|| !RT
|| !ResultT
|| !LHSComputationT
)
1130 // Handle floating point operations separately here, since they
1131 // require special care.
1133 if (ResultT
== PT_Float
|| RT
== PT_Float
)
1134 return VisitFloatCompoundAssignOperator(E
);
1136 if (E
->getType()->isPointerType())
1137 return VisitPointerCompoundAssignOperator(E
);
1139 assert(!E
->getType()->isPointerType() && "Handled above");
1140 assert(!E
->getType()->isFloatingType() && "Handled above");
1142 // C++17 onwards require that we evaluate the RHS first.
1143 // Compute RHS and save it in a temporary variable so we can
1144 // load it again later.
1145 // FIXME: Compound assignments are unsequenced in C, so we might
1146 // have to figure out how to reject them.
1150 unsigned TempOffset
= this->allocateLocalPrimitive(E
, *RT
, /*IsConst=*/true);
1152 if (!this->emitSetLocal(*RT
, TempOffset
, E
))
1155 // Get LHS pointer, load its value and cast it to the
1156 // computation type if necessary.
1159 if (!this->emitLoad(*LT
, E
))
1161 if (*LT
!= *LHSComputationT
) {
1162 if (!this->emitCast(*LT
, *LHSComputationT
, E
))
1166 // Get the RHS value on the stack.
1167 if (!this->emitGetLocal(*RT
, TempOffset
, E
))
1170 // Perform operation.
1171 switch (E
->getOpcode()) {
1173 if (!this->emitAdd(*LHSComputationT
, E
))
1177 if (!this->emitSub(*LHSComputationT
, E
))
1181 if (!this->emitMul(*LHSComputationT
, E
))
1185 if (!this->emitDiv(*LHSComputationT
, E
))
1189 if (!this->emitRem(*LHSComputationT
, E
))
1193 if (!this->emitShl(*LHSComputationT
, *RT
, E
))
1197 if (!this->emitShr(*LHSComputationT
, *RT
, E
))
1201 if (!this->emitBitAnd(*LHSComputationT
, E
))
1205 if (!this->emitBitXor(*LHSComputationT
, E
))
1209 if (!this->emitBitOr(*LHSComputationT
, E
))
1213 llvm_unreachable("Unimplemented compound assign operator");
1216 // And now cast from LHSComputationT to ResultT.
1217 if (*ResultT
!= *LHSComputationT
) {
1218 if (!this->emitCast(*LHSComputationT
, *ResultT
, E
))
1222 // And store the result in LHS.
1223 if (DiscardResult
) {
1224 if (LHS
->refersToBitField())
1225 return this->emitStoreBitFieldPop(*ResultT
, E
);
1226 return this->emitStorePop(*ResultT
, E
);
1228 if (LHS
->refersToBitField())
1229 return this->emitStoreBitField(*ResultT
, E
);
1230 return this->emitStore(*ResultT
, E
);
1233 template <class Emitter
>
1234 bool ByteCodeExprGen
<Emitter
>::VisitExprWithCleanups(
1235 const ExprWithCleanups
*E
) {
1236 const Expr
*SubExpr
= E
->getSubExpr();
1238 assert(E
->getNumObjects() == 0 && "TODO: Implement cleanups");
1240 return this->delegate(SubExpr
);
1243 template <class Emitter
>
1244 bool ByteCodeExprGen
<Emitter
>::VisitMaterializeTemporaryExpr(
1245 const MaterializeTemporaryExpr
*E
) {
1246 const Expr
*SubExpr
= E
->getSubExpr();
1249 // We already have a value, just initialize that.
1250 return this->visitInitializer(SubExpr
);
1252 // If we don't end up using the materialized temporary anyway, don't
1253 // bother creating it.
1255 return this->discard(SubExpr
);
1257 // When we're initializing a global variable *or* the storage duration of
1258 // the temporary is explicitly static, create a global variable.
1259 std::optional
<PrimType
> SubExprT
= classify(SubExpr
);
1260 bool IsStatic
= E
->getStorageDuration() == SD_Static
;
1261 if (GlobalDecl
|| IsStatic
) {
1262 std::optional
<unsigned> GlobalIndex
= P
.createGlobal(E
);
1266 const LifetimeExtendedTemporaryDecl
*TempDecl
=
1267 E
->getLifetimeExtendedTemporaryDecl();
1272 if (!this->visit(SubExpr
))
1275 if (!this->emitInitGlobalTemp(*SubExprT
, *GlobalIndex
, TempDecl
, E
))
1278 if (!this->emitInitGlobal(*SubExprT
, *GlobalIndex
, E
))
1281 return this->emitGetPtrGlobal(*GlobalIndex
, E
);
1284 // Non-primitive values.
1285 if (!this->emitGetPtrGlobal(*GlobalIndex
, E
))
1287 if (!this->visitInitializer(SubExpr
))
1290 return this->emitInitGlobalTempComp(TempDecl
, E
);
1294 // For everyhing else, use local variables.
1296 if (std::optional
<unsigned> LocalIndex
= allocateLocalPrimitive(
1297 SubExpr
, *SubExprT
, /*IsConst=*/true, /*IsExtended=*/true)) {
1298 if (!this->visit(SubExpr
))
1300 this->emitSetLocal(*SubExprT
, *LocalIndex
, E
);
1301 return this->emitGetPtrLocal(*LocalIndex
, E
);
1304 if (std::optional
<unsigned> LocalIndex
=
1305 allocateLocal(SubExpr
, /*IsExtended=*/true)) {
1306 if (!this->emitGetPtrLocal(*LocalIndex
, E
))
1308 return this->visitInitializer(SubExpr
);
1314 template <class Emitter
>
1315 bool ByteCodeExprGen
<Emitter
>::VisitCXXBindTemporaryExpr(
1316 const CXXBindTemporaryExpr
*E
) {
1317 return this->delegate(E
->getSubExpr());
1320 template <class Emitter
>
1321 bool ByteCodeExprGen
<Emitter
>::VisitCompoundLiteralExpr(
1322 const CompoundLiteralExpr
*E
) {
1323 const Expr
*Init
= E
->getInitializer();
1325 // We already have a value, just initialize that.
1326 return this->visitInitializer(Init
);
1329 std::optional
<PrimType
> T
= classify(E
->getType());
1330 if (E
->isFileScope()) {
1331 if (std::optional
<unsigned> GlobalIndex
= P
.createGlobal(E
)) {
1332 if (classify(E
->getType()))
1333 return this->visit(Init
);
1334 if (!this->emitGetPtrGlobal(*GlobalIndex
, E
))
1336 return this->visitInitializer(Init
);
1340 // Otherwise, use a local variable.
1342 // For primitive types, we just visit the initializer.
1343 return this->delegate(Init
);
1345 if (std::optional
<unsigned> LocalIndex
= allocateLocal(Init
)) {
1346 if (!this->emitGetPtrLocal(*LocalIndex
, E
))
1348 if (!this->visitInitializer(Init
))
1351 return this->emitPopPtr(E
);
1359 template <class Emitter
>
1360 bool ByteCodeExprGen
<Emitter
>::VisitTypeTraitExpr(const TypeTraitExpr
*E
) {
1363 return this->emitConstBool(E
->getValue(), E
);
1366 template <class Emitter
>
1367 bool ByteCodeExprGen
<Emitter
>::VisitLambdaExpr(const LambdaExpr
*E
) {
1368 assert(Initializing
);
1369 const Record
*R
= P
.getOrCreateRecord(E
->getLambdaClass());
1371 auto *CaptureInitIt
= E
->capture_init_begin();
1372 // Initialize all fields (which represent lambda captures) of the
1373 // record with their initializers.
1374 for (const Record::Field
&F
: R
->fields()) {
1375 const Expr
*Init
= *CaptureInitIt
;
1378 if (std::optional
<PrimType
> T
= classify(Init
)) {
1379 if (!this->visit(Init
))
1382 if (!this->emitSetField(*T
, F
.Offset
, E
))
1385 if (!this->emitDupPtr(E
))
1388 if (!this->emitGetPtrField(F
.Offset
, E
))
1391 if (!this->visitInitializer(Init
))
1394 if (!this->emitPopPtr(E
))
1402 template <class Emitter
>
1403 bool ByteCodeExprGen
<Emitter
>::VisitPredefinedExpr(const PredefinedExpr
*E
) {
1407 assert(!Initializing
);
1408 return this->visit(E
->getFunctionName());
1411 template <class Emitter
>
1412 bool ByteCodeExprGen
<Emitter
>::VisitCXXThrowExpr(const CXXThrowExpr
*E
) {
1413 if (E
->getSubExpr() && !this->discard(E
->getSubExpr()))
1416 return this->emitInvalid(E
);
1419 template <class Emitter
>
1420 bool ByteCodeExprGen
<Emitter
>::VisitCXXReinterpretCastExpr(
1421 const CXXReinterpretCastExpr
*E
) {
1422 if (!this->discard(E
->getSubExpr()))
1425 return this->emitInvalidCast(CastKind::Reinterpret
, E
);
1428 template <class Emitter
>
1429 bool ByteCodeExprGen
<Emitter
>::VisitCXXNoexceptExpr(const CXXNoexceptExpr
*E
) {
1430 assert(E
->getType()->isBooleanType());
1434 return this->emitConstBool(E
->getValue(), E
);
1437 template <class Emitter
>
1438 bool ByteCodeExprGen
<Emitter
>::VisitCXXConstructExpr(
1439 const CXXConstructExpr
*E
) {
1440 QualType T
= E
->getType();
1441 assert(!classify(T
));
1443 if (T
->isRecordType()) {
1444 const CXXConstructorDecl
*Ctor
= E
->getConstructor();
1446 // Trivial zero initialization.
1447 if (E
->requiresZeroInitialization() && Ctor
->isTrivial()) {
1448 const Record
*R
= getRecord(E
->getType());
1449 return this->visitZeroRecordInitializer(R
, E
);
1452 const Function
*Func
= getFunction(Ctor
);
1457 assert(Func
->hasThisPointer());
1458 assert(!Func
->hasRVO());
1460 // If we're discarding a construct expression, we still need
1461 // to allocate a variable and call the constructor and destructor.
1462 if (DiscardResult
) {
1463 assert(!Initializing
);
1464 std::optional
<unsigned> LocalIndex
=
1465 allocateLocal(E
, /*IsExtended=*/true);
1470 if (!this->emitGetPtrLocal(*LocalIndex
, E
))
1474 // The This pointer is already on the stack because this is an initializer,
1475 // but we need to dup() so the call() below has its own copy.
1476 if (!this->emitDupPtr(E
))
1479 // Constructor arguments.
1480 for (const auto *Arg
: E
->arguments()) {
1481 if (!this->visit(Arg
))
1485 if (!this->emitCall(Func
, E
))
1488 // Immediately call the destructor if we have to.
1489 if (DiscardResult
) {
1490 if (!this->emitPopPtr(E
))
1496 if (T
->isArrayType()) {
1497 const ConstantArrayType
*CAT
=
1498 Ctx
.getASTContext().getAsConstantArrayType(E
->getType());
1500 size_t NumElems
= CAT
->getSize().getZExtValue();
1501 const Function
*Func
= getFunction(E
->getConstructor());
1502 if (!Func
|| !Func
->isConstexpr())
1505 // FIXME(perf): We're calling the constructor once per array element here,
1506 // in the old intepreter we had a special-case for trivial constructors.
1507 for (size_t I
= 0; I
!= NumElems
; ++I
) {
1508 if (!this->emitConstUint64(I
, E
))
1510 if (!this->emitArrayElemPtrUint64(E
))
1513 // Constructor arguments.
1514 for (const auto *Arg
: E
->arguments()) {
1515 if (!this->visit(Arg
))
1519 if (!this->emitCall(Func
, E
))
1528 template <class Emitter
>
1529 bool ByteCodeExprGen
<Emitter
>::VisitSourceLocExpr(const SourceLocExpr
*E
) {
1534 E
->EvaluateInContext(Ctx
.getASTContext(), SourceLocDefaultExpr
);
1536 // Things like __builtin_LINE().
1537 if (E
->getType()->isIntegerType()) {
1538 assert(Val
.isInt());
1539 const APSInt
&I
= Val
.getInt();
1540 return this->emitConst(I
, E
);
1542 // Otherwise, the APValue is an LValue, with only one element.
1543 // Theoretically, we don't need the APValue at all of course.
1544 assert(E
->getType()->isPointerType());
1545 assert(Val
.isLValue());
1546 const APValue::LValueBase
&Base
= Val
.getLValueBase();
1547 if (const Expr
*LValueExpr
= Base
.dyn_cast
<const Expr
*>())
1548 return this->visit(LValueExpr
);
1550 // Otherwise, we have a decl (which is the case for
1551 // __builtin_source_location).
1552 assert(Base
.is
<const ValueDecl
*>());
1553 assert(Val
.getLValuePath().size() == 0);
1554 const auto *BaseDecl
= Base
.dyn_cast
<const ValueDecl
*>();
1557 auto *UGCD
= cast
<UnnamedGlobalConstantDecl
>(BaseDecl
);
1559 std::optional
<unsigned> GlobalIndex
= P
.getOrCreateGlobal(UGCD
);
1563 if (!this->emitGetPtrGlobal(*GlobalIndex
, E
))
1566 const Record
*R
= getRecord(E
->getType());
1567 const APValue
&V
= UGCD
->getValue();
1568 for (unsigned I
= 0, N
= R
->getNumFields(); I
!= N
; ++I
) {
1569 const Record::Field
*F
= R
->getField(I
);
1570 const APValue
&FieldValue
= V
.getStructField(I
);
1572 PrimType FieldT
= classifyPrim(F
->Decl
->getType());
1574 if (!this->visitAPValue(FieldValue
, FieldT
, E
))
1576 if (!this->emitInitField(FieldT
, F
->Offset
, E
))
1580 // Leave the pointer to the global on the stack.
1584 template <class Emitter
>
1585 bool ByteCodeExprGen
<Emitter
>::VisitOffsetOfExpr(const OffsetOfExpr
*E
) {
1586 unsigned N
= E
->getNumComponents();
1590 for (unsigned I
= 0; I
!= N
; ++I
) {
1591 const OffsetOfNode
&Node
= E
->getComponent(I
);
1592 if (Node
.getKind() == OffsetOfNode::Array
) {
1593 const Expr
*ArrayIndexExpr
= E
->getIndexExpr(Node
.getArrayExprIndex());
1594 PrimType IndexT
= classifyPrim(ArrayIndexExpr
->getType());
1596 if (DiscardResult
) {
1597 if (!this->discard(ArrayIndexExpr
))
1602 if (!this->visit(ArrayIndexExpr
))
1605 if (IndexT
!= PT_Sint64
) {
1606 if (!this->emitCast(IndexT
, PT_Sint64
, E
))
1615 PrimType T
= classifyPrim(E
->getType());
1616 return this->emitOffsetOf(T
, E
, E
);
1619 template <class Emitter
>
1620 bool ByteCodeExprGen
<Emitter
>::VisitCXXScalarValueInitExpr(
1621 const CXXScalarValueInitExpr
*E
) {
1622 QualType Ty
= E
->getType();
1624 if (Ty
->isVoidType())
1627 return this->visitZeroInitializer(classifyPrim(Ty
), Ty
, E
);
1630 template <class Emitter
> bool ByteCodeExprGen
<Emitter
>::discard(const Expr
*E
) {
1631 if (E
->containsErrors())
1634 OptionScope
<Emitter
> Scope(this, /*NewDiscardResult=*/true,
1635 /*NewInitializing=*/false);
1636 return this->Visit(E
);
1639 template <class Emitter
>
1640 bool ByteCodeExprGen
<Emitter
>::delegate(const Expr
*E
) {
1641 if (E
->containsErrors())
1644 // We're basically doing:
1645 // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
1646 // but that's unnecessary of course.
1647 return this->Visit(E
);
1650 template <class Emitter
> bool ByteCodeExprGen
<Emitter
>::visit(const Expr
*E
) {
1651 if (E
->containsErrors())
1654 if (E
->getType()->isVoidType())
1655 return this->discard(E
);
1657 // Create local variable to hold the return value.
1658 if (!E
->isGLValue() && !classify(E
->getType())) {
1659 std::optional
<unsigned> LocalIndex
= allocateLocal(E
, /*IsExtended=*/true);
1663 if (!this->emitGetPtrLocal(*LocalIndex
, E
))
1665 return this->visitInitializer(E
);
1668 // Otherwise,we have a primitive return value, produce the value directly
1669 // and push it on the stack.
1670 OptionScope
<Emitter
> Scope(this, /*NewDiscardResult=*/false,
1671 /*NewInitializing=*/false);
1672 return this->Visit(E
);
1675 template <class Emitter
>
1676 bool ByteCodeExprGen
<Emitter
>::visitInitializer(const Expr
*E
) {
1677 assert(!classify(E
->getType()));
1679 if (E
->containsErrors())
1682 OptionScope
<Emitter
> Scope(this, /*NewDiscardResult=*/false,
1683 /*NewInitializing=*/true);
1684 return this->Visit(E
);
1687 template <class Emitter
>
1688 bool ByteCodeExprGen
<Emitter
>::visitBool(const Expr
*E
) {
1689 std::optional
<PrimType
> T
= classify(E
->getType());
1693 if (!this->visit(E
))
1699 // Convert pointers to bool.
1700 if (T
== PT_Ptr
|| T
== PT_FnPtr
) {
1701 if (!this->emitNull(*T
, E
))
1703 return this->emitNE(*T
, E
);
1708 return this->emitCastFloatingIntegralBool(E
);
1710 // Or anything else we can.
1711 return this->emitCast(*T
, PT_Bool
, E
);
1714 template <class Emitter
>
1715 bool ByteCodeExprGen
<Emitter
>::visitZeroInitializer(PrimType T
, QualType QT
,
1719 return this->emitZeroBool(E
);
1721 return this->emitZeroSint8(E
);
1723 return this->emitZeroUint8(E
);
1725 return this->emitZeroSint16(E
);
1727 return this->emitZeroUint16(E
);
1729 return this->emitZeroSint32(E
);
1731 return this->emitZeroUint32(E
);
1733 return this->emitZeroSint64(E
);
1735 return this->emitZeroUint64(E
);
1737 return this->emitZeroIntAP(Ctx
.getBitWidth(QT
), E
);
1739 return this->emitZeroIntAPS(Ctx
.getBitWidth(QT
), E
);
1741 return this->emitNullPtr(E
);
1743 return this->emitNullFnPtr(E
);
1745 return this->emitConstFloat(APFloat::getZero(Ctx
.getFloatSemantics(QT
)), E
);
1748 llvm_unreachable("unknown primitive type");
1751 template <class Emitter
>
1752 bool ByteCodeExprGen
<Emitter
>::visitZeroRecordInitializer(const Record
*R
,
1757 for (const Record::Field
&Field
: R
->fields()) {
1758 const Descriptor
*D
= Field
.Desc
;
1759 if (D
->isPrimitive()) {
1760 QualType QT
= D
->getType();
1761 PrimType T
= classifyPrim(D
->getType());
1762 if (!this->visitZeroInitializer(T
, QT
, E
))
1764 if (!this->emitInitField(T
, Field
.Offset
, E
))
1769 // TODO: Add GetPtrFieldPop and get rid of this dup.
1770 if (!this->emitDupPtr(E
))
1772 if (!this->emitGetPtrField(Field
.Offset
, E
))
1775 if (D
->isPrimitiveArray()) {
1776 QualType ET
= D
->getElemQualType();
1777 PrimType T
= classifyPrim(ET
);
1778 for (uint32_t I
= 0, N
= D
->getNumElems(); I
!= N
; ++I
) {
1779 if (!this->visitZeroInitializer(T
, ET
, E
))
1781 if (!this->emitInitElem(T
, I
, E
))
1784 } else if (D
->isCompositeArray()) {
1785 const Record
*ElemRecord
= D
->ElemDesc
->ElemRecord
;
1786 assert(D
->ElemDesc
->ElemRecord
);
1787 for (uint32_t I
= 0, N
= D
->getNumElems(); I
!= N
; ++I
) {
1788 if (!this->emitConstUint32(I
, E
))
1790 if (!this->emitArrayElemPtr(PT_Uint32
, E
))
1792 if (!this->visitZeroRecordInitializer(ElemRecord
, E
))
1794 if (!this->emitPopPtr(E
))
1797 } else if (D
->isRecord()) {
1798 if (!this->visitZeroRecordInitializer(D
->ElemRecord
, E
))
1804 if (!this->emitPopPtr(E
))
1808 for (const Record::Base
&B
: R
->bases()) {
1809 if (!this->emitGetPtrBase(B
.Offset
, E
))
1811 if (!this->visitZeroRecordInitializer(B
.R
, E
))
1813 if (!this->emitInitPtrPop(E
))
1817 // FIXME: Virtual bases.
1822 template <class Emitter
>
1823 bool ByteCodeExprGen
<Emitter
>::dereference(
1824 const Expr
*LV
, DerefKind AK
, llvm::function_ref
<bool(PrimType
)> Direct
,
1825 llvm::function_ref
<bool(PrimType
)> Indirect
) {
1826 if (std::optional
<PrimType
> T
= classify(LV
->getType())) {
1827 if (!LV
->refersToBitField()) {
1828 // Only primitive, non bit-field types can be dereferenced directly.
1829 if (const auto *DE
= dyn_cast
<DeclRefExpr
>(LV
)) {
1830 if (!DE
->getDecl()->getType()->isReferenceType()) {
1831 if (const auto *PD
= dyn_cast
<ParmVarDecl
>(DE
->getDecl()))
1832 return dereferenceParam(LV
, *T
, PD
, AK
, Direct
, Indirect
);
1833 if (const auto *VD
= dyn_cast
<VarDecl
>(DE
->getDecl()))
1834 return dereferenceVar(LV
, *T
, VD
, AK
, Direct
, Indirect
);
1841 return Indirect(*T
);
1847 template <class Emitter
>
1848 bool ByteCodeExprGen
<Emitter
>::dereferenceParam(
1849 const Expr
*LV
, PrimType T
, const ParmVarDecl
*PD
, DerefKind AK
,
1850 llvm::function_ref
<bool(PrimType
)> Direct
,
1851 llvm::function_ref
<bool(PrimType
)> Indirect
) {
1852 auto It
= this->Params
.find(PD
);
1853 if (It
!= this->Params
.end()) {
1854 unsigned Idx
= It
->second
.Offset
;
1856 case DerefKind::Read
:
1857 return DiscardResult
? true : this->emitGetParam(T
, Idx
, LV
);
1859 case DerefKind::Write
:
1862 if (!this->emitSetParam(T
, Idx
, LV
))
1864 return DiscardResult
? true : this->emitGetPtrParam(Idx
, LV
);
1866 case DerefKind::ReadWrite
:
1867 if (!this->emitGetParam(T
, Idx
, LV
))
1871 if (!this->emitSetParam(T
, Idx
, LV
))
1873 return DiscardResult
? true : this->emitGetPtrParam(Idx
, LV
);
1878 // If the param is a pointer, we can dereference a dummy value.
1879 if (!DiscardResult
&& T
== PT_Ptr
&& AK
== DerefKind::Read
) {
1880 if (auto Idx
= P
.getOrCreateDummy(PD
))
1881 return this->emitGetPtrGlobal(*Idx
, PD
);
1885 // Value cannot be produced - try to emit pointer and do stuff with it.
1886 return visit(LV
) && Indirect(T
);
1889 template <class Emitter
>
1890 bool ByteCodeExprGen
<Emitter
>::dereferenceVar(
1891 const Expr
*LV
, PrimType T
, const VarDecl
*VD
, DerefKind AK
,
1892 llvm::function_ref
<bool(PrimType
)> Direct
,
1893 llvm::function_ref
<bool(PrimType
)> Indirect
) {
1894 auto It
= Locals
.find(VD
);
1895 if (It
!= Locals
.end()) {
1896 const auto &L
= It
->second
;
1898 case DerefKind::Read
:
1899 if (!this->emitGetLocal(T
, L
.Offset
, LV
))
1901 return DiscardResult
? this->emitPop(T
, LV
) : true;
1903 case DerefKind::Write
:
1906 if (!this->emitSetLocal(T
, L
.Offset
, LV
))
1908 return DiscardResult
? true : this->emitGetPtrLocal(L
.Offset
, LV
);
1910 case DerefKind::ReadWrite
:
1911 if (!this->emitGetLocal(T
, L
.Offset
, LV
))
1915 if (!this->emitSetLocal(T
, L
.Offset
, LV
))
1917 return DiscardResult
? true : this->emitGetPtrLocal(L
.Offset
, LV
);
1919 } else if (auto Idx
= P
.getGlobal(VD
)) {
1921 case DerefKind::Read
:
1922 if (!this->emitGetGlobal(T
, *Idx
, LV
))
1924 return DiscardResult
? this->emitPop(T
, LV
) : true;
1926 case DerefKind::Write
:
1929 if (!this->emitSetGlobal(T
, *Idx
, LV
))
1931 return DiscardResult
? true : this->emitGetPtrGlobal(*Idx
, LV
);
1933 case DerefKind::ReadWrite
:
1934 if (!this->emitGetGlobal(T
, *Idx
, LV
))
1938 if (!this->emitSetGlobal(T
, *Idx
, LV
))
1940 return DiscardResult
? true : this->emitGetPtrGlobal(*Idx
, LV
);
1944 // If the declaration is a constant value, emit it here even
1945 // though the declaration was not evaluated in the current scope.
1946 // The access mode can only be read in this case.
1947 if (!DiscardResult
&& AK
== DerefKind::Read
) {
1948 if (VD
->hasLocalStorage() && VD
->hasInit() && !VD
->isConstexpr()) {
1949 QualType VT
= VD
->getType();
1950 if (VT
.isConstQualified() && VT
->isFundamentalType())
1951 return this->visit(VD
->getInit());
1955 // Value cannot be produced - try to emit pointer.
1956 return visit(LV
) && Indirect(T
);
1959 template <class Emitter
>
1960 template <typename T
>
1961 bool ByteCodeExprGen
<Emitter
>::emitConst(T Value
, PrimType Ty
, const Expr
*E
) {
1964 return this->emitConstSint8(Value
, E
);
1966 return this->emitConstUint8(Value
, E
);
1968 return this->emitConstSint16(Value
, E
);
1970 return this->emitConstUint16(Value
, E
);
1972 return this->emitConstSint32(Value
, E
);
1974 return this->emitConstUint32(Value
, E
);
1976 return this->emitConstSint64(Value
, E
);
1978 return this->emitConstUint64(Value
, E
);
1984 return this->emitConstBool(Value
, E
);
1988 llvm_unreachable("Invalid integral type");
1991 llvm_unreachable("unknown primitive type");
1994 template <class Emitter
>
1995 template <typename T
>
1996 bool ByteCodeExprGen
<Emitter
>::emitConst(T Value
, const Expr
*E
) {
1997 return this->emitConst(Value
, classifyPrim(E
->getType()), E
);
2000 template <class Emitter
>
2001 bool ByteCodeExprGen
<Emitter
>::emitConst(const APSInt
&Value
, PrimType Ty
,
2003 if (Value
.isSigned())
2004 return this->emitConst(Value
.getSExtValue(), Ty
, E
);
2005 return this->emitConst(Value
.getZExtValue(), Ty
, E
);
2008 template <class Emitter
>
2009 bool ByteCodeExprGen
<Emitter
>::emitConst(const APSInt
&Value
, const Expr
*E
) {
2010 return this->emitConst(Value
, classifyPrim(E
->getType()), E
);
2013 template <class Emitter
>
2014 unsigned ByteCodeExprGen
<Emitter
>::allocateLocalPrimitive(DeclTy
&&Src
,
2018 // Make sure we don't accidentally register the same decl twice.
2019 if (const auto *VD
=
2020 dyn_cast_if_present
<ValueDecl
>(Src
.dyn_cast
<const Decl
*>())) {
2021 assert(!P
.getGlobal(VD
));
2022 assert(!Locals
.contains(VD
));
2025 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
2026 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
2027 // or isa<MaterializeTemporaryExpr>().
2028 Descriptor
*D
= P
.createDescriptor(Src
, Ty
, Descriptor::InlineDescMD
, IsConst
,
2029 Src
.is
<const Expr
*>());
2030 Scope::Local Local
= this->createLocal(D
);
2031 if (auto *VD
= dyn_cast_if_present
<ValueDecl
>(Src
.dyn_cast
<const Decl
*>()))
2032 Locals
.insert({VD
, Local
});
2033 VarScope
->add(Local
, IsExtended
);
2034 return Local
.Offset
;
2037 template <class Emitter
>
2038 std::optional
<unsigned>
2039 ByteCodeExprGen
<Emitter
>::allocateLocal(DeclTy
&&Src
, bool IsExtended
) {
2040 // Make sure we don't accidentally register the same decl twice.
2041 if ([[maybe_unused
]] const auto *VD
=
2042 dyn_cast_if_present
<ValueDecl
>(Src
.dyn_cast
<const Decl
*>())) {
2043 assert(!P
.getGlobal(VD
));
2044 assert(!Locals
.contains(VD
));
2048 const ValueDecl
*Key
= nullptr;
2049 const Expr
*Init
= nullptr;
2050 bool IsTemporary
= false;
2051 if (auto *VD
= dyn_cast_if_present
<ValueDecl
>(Src
.dyn_cast
<const Decl
*>())) {
2055 if (const auto *VarD
= dyn_cast
<VarDecl
>(VD
))
2056 Init
= VarD
->getInit();
2058 if (auto *E
= Src
.dyn_cast
<const Expr
*>()) {
2063 Descriptor
*D
= P
.createDescriptor(
2064 Src
, Ty
.getTypePtr(), Descriptor::InlineDescMD
, Ty
.isConstQualified(),
2065 IsTemporary
, /*IsMutable=*/false, Init
);
2069 Scope::Local Local
= this->createLocal(D
);
2071 Locals
.insert({Key
, Local
});
2072 VarScope
->add(Local
, IsExtended
);
2073 return Local
.Offset
;
2076 template <class Emitter
>
2077 const RecordType
*ByteCodeExprGen
<Emitter
>::getRecordTy(QualType Ty
) {
2078 if (const PointerType
*PT
= dyn_cast
<PointerType
>(Ty
))
2079 return PT
->getPointeeType()->getAs
<RecordType
>();
2080 return Ty
->getAs
<RecordType
>();
2083 template <class Emitter
>
2084 Record
*ByteCodeExprGen
<Emitter
>::getRecord(QualType Ty
) {
2085 if (const auto *RecordTy
= getRecordTy(Ty
))
2086 return getRecord(RecordTy
->getDecl());
2090 template <class Emitter
>
2091 Record
*ByteCodeExprGen
<Emitter
>::getRecord(const RecordDecl
*RD
) {
2092 return P
.getOrCreateRecord(RD
);
2095 template <class Emitter
>
2096 const Function
*ByteCodeExprGen
<Emitter
>::getFunction(const FunctionDecl
*FD
) {
2097 return Ctx
.getOrCreateFunction(FD
);
2100 template <class Emitter
>
2101 bool ByteCodeExprGen
<Emitter
>::visitExpr(const Expr
*E
) {
2102 ExprScope
<Emitter
> RootScope(this);
2106 if (E
->getType()->isVoidType())
2107 return this->emitRetVoid(E
);
2109 if (std::optional
<PrimType
> T
= classify(E
))
2110 return this->emitRet(*T
, E
);
2111 return this->emitRetValue(E
);
2114 /// Toplevel visitDecl().
2115 /// We get here from evaluateAsInitializer().
2116 /// We need to evaluate the initializer and return its value.
2117 template <class Emitter
>
2118 bool ByteCodeExprGen
<Emitter
>::visitDecl(const VarDecl
*VD
) {
2119 assert(!VD
->isInvalidDecl() && "Trying to constant evaluate an invalid decl");
2121 // Create and initialize the variable.
2122 if (!this->visitVarDecl(VD
))
2125 // Get a pointer to the variable
2126 if (Context::shouldBeGloballyIndexed(VD
)) {
2127 auto GlobalIndex
= P
.getGlobal(VD
);
2128 assert(GlobalIndex
); // visitVarDecl() didn't return false.
2129 if (!this->emitGetPtrGlobal(*GlobalIndex
, VD
))
2132 auto Local
= Locals
.find(VD
);
2133 assert(Local
!= Locals
.end()); // Same here.
2134 if (!this->emitGetPtrLocal(Local
->second
.Offset
, VD
))
2139 if (std::optional
<PrimType
> VarT
= classify(VD
->getType())) {
2140 if (!this->emitLoadPop(*VarT
, VD
))
2143 return this->emitRet(*VarT
, VD
);
2146 return this->emitRetValue(VD
);
2149 template <class Emitter
>
2150 bool ByteCodeExprGen
<Emitter
>::visitVarDecl(const VarDecl
*VD
) {
2151 // We don't know what to do with these, so just return false.
2152 if (VD
->getType().isNull())
2155 const Expr
*Init
= VD
->getInit();
2156 std::optional
<PrimType
> VarT
= classify(VD
->getType());
2158 if (Context::shouldBeGloballyIndexed(VD
)) {
2159 // We've already seen and initialized this global.
2160 if (P
.getGlobal(VD
))
2163 std::optional
<unsigned> GlobalIndex
= P
.createGlobal(VD
, Init
);
2166 return this->bail(VD
);
2170 DeclScope
<Emitter
> LocalScope(this, VD
);
2173 if (!this->visit(Init
))
2175 return this->emitInitGlobal(*VarT
, *GlobalIndex
, VD
);
2177 return this->visitGlobalInitializer(Init
, *GlobalIndex
);
2180 VariableScope
<Emitter
> LocalScope(this);
2182 unsigned Offset
= this->allocateLocalPrimitive(
2183 VD
, *VarT
, VD
->getType().isConstQualified());
2185 // Compile the initializer in its own scope.
2186 ExprScope
<Emitter
> Scope(this);
2187 if (!this->visit(Init
))
2190 return this->emitSetLocal(*VarT
, Offset
, VD
);
2193 if (std::optional
<unsigned> Offset
= this->allocateLocal(VD
)) {
2195 return this->visitLocalInitializer(Init
, *Offset
);
2204 template <class Emitter
>
2205 bool ByteCodeExprGen
<Emitter
>::visitAPValue(const APValue
&Val
,
2206 PrimType ValType
, const Expr
*E
) {
2207 assert(!DiscardResult
);
2209 return this->emitConst(Val
.getInt(), ValType
, E
);
2211 if (Val
.isLValue()) {
2212 APValue::LValueBase Base
= Val
.getLValueBase();
2213 if (const Expr
*BaseExpr
= Base
.dyn_cast
<const Expr
*>())
2214 return this->visit(BaseExpr
);
2220 template <class Emitter
>
2221 bool ByteCodeExprGen
<Emitter
>::VisitBuiltinCallExpr(const CallExpr
*E
) {
2222 const Function
*Func
= getFunction(E
->getDirectCallee());
2226 // Put arguments on the stack.
2227 for (const auto *Arg
: E
->arguments()) {
2228 if (!this->visit(Arg
))
2232 if (!this->emitCallBI(Func
, E
, E
))
2235 QualType ReturnType
= E
->getCallReturnType(Ctx
.getASTContext());
2236 if (DiscardResult
&& !ReturnType
->isVoidType()) {
2237 PrimType T
= classifyPrim(ReturnType
);
2238 return this->emitPop(T
, E
);
2244 template <class Emitter
>
2245 bool ByteCodeExprGen
<Emitter
>::VisitCallExpr(const CallExpr
*E
) {
2246 if (E
->getBuiltinCallee())
2247 return VisitBuiltinCallExpr(E
);
2249 QualType ReturnType
= E
->getCallReturnType(Ctx
.getASTContext());
2250 std::optional
<PrimType
> T
= classify(ReturnType
);
2251 bool HasRVO
= !ReturnType
->isVoidType() && !T
;
2254 if (DiscardResult
) {
2255 // If we need to discard the return value but the function returns its
2256 // value via an RVO pointer, we need to create one such pointer just
2258 if (std::optional
<unsigned> LocalIndex
= allocateLocal(E
)) {
2259 if (!this->emitGetPtrLocal(*LocalIndex
, E
))
2263 assert(Initializing
);
2264 if (!isa
<CXXMemberCallExpr
>(E
)) {
2265 if (!this->emitDupPtr(E
))
2271 // Put arguments on the stack.
2272 for (const auto *Arg
: E
->arguments()) {
2273 if (!this->visit(Arg
))
2277 if (const FunctionDecl
*FuncDecl
= E
->getDirectCallee()) {
2278 const Function
*Func
= getFunction(FuncDecl
);
2281 // If the function is being compiled right now, this is a recursive call.
2282 // In that case, the function can't be valid yet, even though it will be
2284 // If the function is already fully compiled but not constexpr, it was
2285 // found to be faulty earlier on, so bail out.
2286 if (Func
->isFullyCompiled() && !Func
->isConstexpr())
2289 assert(HasRVO
== Func
->hasRVO());
2291 bool HasQualifier
= false;
2292 if (const auto *ME
= dyn_cast
<MemberExpr
>(E
->getCallee()))
2293 HasQualifier
= ME
->hasQualifier();
2295 bool IsVirtual
= false;
2296 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(FuncDecl
))
2297 IsVirtual
= MD
->isVirtual();
2299 // In any case call the function. The return value will end up on the stack
2300 // and if the function has RVO, we already have the pointer on the stack to
2301 // write the result into.
2302 if (IsVirtual
&& !HasQualifier
) {
2303 if (!this->emitCallVirt(Func
, E
))
2306 if (!this->emitCall(Func
, E
))
2310 // Indirect call. Visit the callee, which will leave a FunctionPointer on
2311 // the stack. Cleanup of the returned value if necessary will be done after
2312 // the function call completed.
2313 if (!this->visit(E
->getCallee()))
2316 if (!this->emitCallPtr(E
))
2320 // Cleanup for discarded return values.
2321 if (DiscardResult
&& !ReturnType
->isVoidType() && T
)
2322 return this->emitPop(*T
, E
);
2327 template <class Emitter
>
2328 bool ByteCodeExprGen
<Emitter
>::VisitCXXMemberCallExpr(
2329 const CXXMemberCallExpr
*E
) {
2331 // If we're initializing, the current stack top is the pointer to
2332 // initialize, so dup that so this call has its own version.
2333 if (!this->emitDupPtr(E
))
2337 if (!this->visit(E
->getImplicitObjectArgument()))
2340 return VisitCallExpr(E
);
2343 template <class Emitter
>
2344 bool ByteCodeExprGen
<Emitter
>::VisitCXXDefaultInitExpr(
2345 const CXXDefaultInitExpr
*E
) {
2346 SourceLocScope
<Emitter
> SLS(this, E
);
2348 return this->visitInitializer(E
->getExpr());
2350 assert(classify(E
->getType()));
2351 return this->visit(E
->getExpr());
2354 template <class Emitter
>
2355 bool ByteCodeExprGen
<Emitter
>::VisitCXXDefaultArgExpr(
2356 const CXXDefaultArgExpr
*E
) {
2357 SourceLocScope
<Emitter
> SLS(this, E
);
2359 const Expr
*SubExpr
= E
->getExpr();
2360 if (std::optional
<PrimType
> T
= classify(E
->getExpr()))
2361 return this->visit(SubExpr
);
2363 assert(Initializing
);
2364 return this->visitInitializer(SubExpr
);
2367 template <class Emitter
>
2368 bool ByteCodeExprGen
<Emitter
>::VisitCXXBoolLiteralExpr(
2369 const CXXBoolLiteralExpr
*E
) {
2373 return this->emitConstBool(E
->getValue(), E
);
2376 template <class Emitter
>
2377 bool ByteCodeExprGen
<Emitter
>::VisitCXXNullPtrLiteralExpr(
2378 const CXXNullPtrLiteralExpr
*E
) {
2382 return this->emitNullPtr(E
);
2385 template <class Emitter
>
2386 bool ByteCodeExprGen
<Emitter
>::VisitGNUNullExpr(const GNUNullExpr
*E
) {
2390 assert(E
->getType()->isIntegerType());
2392 PrimType T
= classifyPrim(E
->getType());
2393 return this->emitZero(T
, E
);
2396 template <class Emitter
>
2397 bool ByteCodeExprGen
<Emitter
>::VisitCXXThisExpr(const CXXThisExpr
*E
) {
2401 if (this->LambdaThisCapture
> 0)
2402 return this->emitGetThisFieldPtr(this->LambdaThisCapture
, E
);
2404 return this->emitThis(E
);
2407 template <class Emitter
>
2408 bool ByteCodeExprGen
<Emitter
>::VisitUnaryOperator(const UnaryOperator
*E
) {
2409 const Expr
*SubExpr
= E
->getSubExpr();
2410 std::optional
<PrimType
> T
= classify(SubExpr
->getType());
2412 switch (E
->getOpcode()) {
2413 case UO_PostInc
: { // x++
2414 if (!this->visit(SubExpr
))
2418 if (!this->emitIncPtr(E
))
2421 return DiscardResult
? this->emitPopPtr(E
) : true;
2424 if (T
== PT_Float
) {
2425 return DiscardResult
? this->emitIncfPop(getRoundingMode(E
), E
)
2426 : this->emitIncf(getRoundingMode(E
), E
);
2429 return DiscardResult
? this->emitIncPop(*T
, E
) : this->emitInc(*T
, E
);
2431 case UO_PostDec
: { // x--
2432 if (!this->visit(SubExpr
))
2436 if (!this->emitDecPtr(E
))
2439 return DiscardResult
? this->emitPopPtr(E
) : true;
2442 if (T
== PT_Float
) {
2443 return DiscardResult
? this->emitDecfPop(getRoundingMode(E
), E
)
2444 : this->emitDecf(getRoundingMode(E
), E
);
2447 return DiscardResult
? this->emitDecPop(*T
, E
) : this->emitDec(*T
, E
);
2449 case UO_PreInc
: { // ++x
2450 if (!this->visit(SubExpr
))
2454 if (!this->emitLoadPtr(E
))
2456 if (!this->emitConstUint8(1, E
))
2458 if (!this->emitAddOffsetUint8(E
))
2460 return DiscardResult
? this->emitStorePopPtr(E
) : this->emitStorePtr(E
);
2463 // Post-inc and pre-inc are the same if the value is to be discarded.
2464 if (DiscardResult
) {
2466 return this->emitIncfPop(getRoundingMode(E
), E
);
2467 return this->emitIncPop(*T
, E
);
2470 if (T
== PT_Float
) {
2471 const auto &TargetSemantics
= Ctx
.getFloatSemantics(E
->getType());
2472 if (!this->emitLoadFloat(E
))
2474 if (!this->emitConstFloat(llvm::APFloat(TargetSemantics
, 1), E
))
2476 if (!this->emitAddf(getRoundingMode(E
), E
))
2478 return this->emitStoreFloat(E
);
2480 if (!this->emitLoad(*T
, E
))
2482 if (!this->emitConst(1, E
))
2484 if (!this->emitAdd(*T
, E
))
2486 return this->emitStore(*T
, E
);
2488 case UO_PreDec
: { // --x
2489 if (!this->visit(SubExpr
))
2493 if (!this->emitLoadPtr(E
))
2495 if (!this->emitConstUint8(1, E
))
2497 if (!this->emitSubOffsetUint8(E
))
2499 return DiscardResult
? this->emitStorePopPtr(E
) : this->emitStorePtr(E
);
2502 // Post-dec and pre-dec are the same if the value is to be discarded.
2503 if (DiscardResult
) {
2505 return this->emitDecfPop(getRoundingMode(E
), E
);
2506 return this->emitDecPop(*T
, E
);
2509 if (T
== PT_Float
) {
2510 const auto &TargetSemantics
= Ctx
.getFloatSemantics(E
->getType());
2511 if (!this->emitLoadFloat(E
))
2513 if (!this->emitConstFloat(llvm::APFloat(TargetSemantics
, 1), E
))
2515 if (!this->emitSubf(getRoundingMode(E
), E
))
2517 return this->emitStoreFloat(E
);
2519 if (!this->emitLoad(*T
, E
))
2521 if (!this->emitConst(1, E
))
2523 if (!this->emitSub(*T
, E
))
2525 return this->emitStore(*T
, E
);
2529 return this->discard(SubExpr
);
2531 if (!this->visitBool(SubExpr
))
2534 if (!this->emitInvBool(E
))
2537 if (PrimType ET
= classifyPrim(E
->getType()); ET
!= PT_Bool
)
2538 return this->emitCast(PT_Bool
, ET
, E
);
2540 case UO_Minus
: // -x
2541 if (!this->visit(SubExpr
))
2543 return DiscardResult
? this->emitPop(*T
, E
) : this->emitNeg(*T
, E
);
2545 if (!this->visit(SubExpr
)) // noop
2547 return DiscardResult
? this->emitPop(*T
, E
) : true;
2548 case UO_AddrOf
: // &x
2549 // We should already have a pointer when we get here.
2550 return this->delegate(SubExpr
);
2551 case UO_Deref
: // *x
2553 SubExpr
, DerefKind::Read
,
2555 llvm_unreachable("Dereferencing requires a pointer");
2558 [this, E
](PrimType T
) {
2559 return DiscardResult
? this->emitPop(T
, E
) : true;
2562 if (!this->visit(SubExpr
))
2564 return DiscardResult
? this->emitPop(*T
, E
) : this->emitComp(*T
, E
);
2565 case UO_Real
: // __real x
2566 case UO_Imag
: // __imag x
2568 return this->delegate(SubExpr
);
2570 assert(false && "Unhandled opcode");
2576 template <class Emitter
>
2577 bool ByteCodeExprGen
<Emitter
>::VisitDeclRefExpr(const DeclRefExpr
*E
) {
2581 const auto *D
= E
->getDecl();
2583 if (const auto *ECD
= dyn_cast
<EnumConstantDecl
>(D
)) {
2584 return this->emitConst(ECD
->getInitVal(), E
);
2585 } else if (const auto *BD
= dyn_cast
<BindingDecl
>(D
)) {
2586 return this->visit(BD
->getBinding());
2587 } else if (const auto *FuncDecl
= dyn_cast
<FunctionDecl
>(D
)) {
2588 const Function
*F
= getFunction(FuncDecl
);
2589 return F
&& this->emitGetFnPtr(F
, E
);
2592 // References are implemented via pointers, so when we see a DeclRefExpr
2593 // pointing to a reference, we need to get its value directly (i.e. the
2594 // pointer to the actual value) instead of a pointer to the pointer to the
2596 bool IsReference
= D
->getType()->isReferenceType();
2598 // Check for local/global variables and parameters.
2599 if (auto It
= Locals
.find(D
); It
!= Locals
.end()) {
2600 const unsigned Offset
= It
->second
.Offset
;
2603 return this->emitGetLocal(PT_Ptr
, Offset
, E
);
2604 return this->emitGetPtrLocal(Offset
, E
);
2605 } else if (auto GlobalIndex
= P
.getGlobal(D
)) {
2607 return this->emitGetGlobalPtr(*GlobalIndex
, E
);
2609 return this->emitGetPtrGlobal(*GlobalIndex
, E
);
2610 } else if (const auto *PVD
= dyn_cast
<ParmVarDecl
>(D
)) {
2611 if (auto It
= this->Params
.find(PVD
); It
!= this->Params
.end()) {
2612 if (IsReference
|| !It
->second
.IsPtr
)
2613 return this->emitGetParamPtr(It
->second
.Offset
, E
);
2615 return this->emitGetPtrParam(It
->second
.Offset
, E
);
2619 // Handle lambda captures.
2620 if (auto It
= this->LambdaCaptures
.find(D
);
2621 It
!= this->LambdaCaptures
.end()) {
2622 auto [Offset
, IsPtr
] = It
->second
;
2625 return this->emitGetThisFieldPtr(Offset
, E
);
2626 return this->emitGetPtrThisField(Offset
, E
);
2629 // Lazily visit global declarations we haven't seen yet.
2630 // This happens in C.
2631 if (!Ctx
.getLangOpts().CPlusPlus
) {
2632 if (const auto *VD
= dyn_cast
<VarDecl
>(D
);
2633 VD
&& VD
->hasGlobalStorage() && VD
->getAnyInitializer() &&
2634 VD
->getType().isConstQualified()) {
2635 if (!this->visitVarDecl(VD
))
2638 return this->VisitDeclRefExpr(E
);
2641 if (std::optional
<unsigned> I
= P
.getOrCreateDummy(D
))
2642 return this->emitGetPtrGlobal(*I
, E
);
2645 return this->emitInvalidDeclRef(E
, E
);
2648 template <class Emitter
>
2649 void ByteCodeExprGen
<Emitter
>::emitCleanup() {
2650 for (VariableScope
<Emitter
> *C
= VarScope
; C
; C
= C
->getParent())
2651 C
->emitDestruction();
2654 template <class Emitter
>
2656 ByteCodeExprGen
<Emitter
>::collectBaseOffset(const RecordType
*BaseType
,
2657 const RecordType
*DerivedType
) {
2658 const auto *FinalDecl
= cast
<CXXRecordDecl
>(BaseType
->getDecl());
2659 const RecordDecl
*CurDecl
= DerivedType
->getDecl();
2660 const Record
*CurRecord
= getRecord(CurDecl
);
2661 assert(CurDecl
&& FinalDecl
);
2663 unsigned OffsetSum
= 0;
2665 assert(CurRecord
->getNumBases() > 0);
2667 for (const Record::Base
&B
: CurRecord
->bases()) {
2668 const auto *BaseDecl
= cast
<CXXRecordDecl
>(B
.Decl
);
2670 if (BaseDecl
== FinalDecl
|| BaseDecl
->isDerivedFrom(FinalDecl
)) {
2671 OffsetSum
+= B
.Offset
;
2677 if (CurDecl
== FinalDecl
)
2681 assert(OffsetSum
> 0);
2685 /// Emit casts from a PrimType to another PrimType.
2686 template <class Emitter
>
2687 bool ByteCodeExprGen
<Emitter
>::emitPrimCast(PrimType FromT
, PrimType ToT
,
2688 QualType ToQT
, const Expr
*E
) {
2690 if (FromT
== PT_Float
) {
2691 // Floating to floating.
2692 if (ToT
== PT_Float
) {
2693 const llvm::fltSemantics
*ToSem
= &Ctx
.getFloatSemantics(ToQT
);
2694 return this->emitCastFP(ToSem
, getRoundingMode(E
), E
);
2697 // Float to integral.
2698 if (isIntegralType(ToT
) || ToT
== PT_Bool
)
2699 return this->emitCastFloatingIntegral(ToT
, E
);
2702 if (isIntegralType(FromT
) || FromT
== PT_Bool
) {
2703 // Integral to integral.
2704 if (isIntegralType(ToT
) || ToT
== PT_Bool
)
2705 return FromT
!= ToT
? this->emitCast(FromT
, ToT
, E
) : true;
2707 if (ToT
== PT_Float
) {
2708 // Integral to floating.
2709 const llvm::fltSemantics
*ToSem
= &Ctx
.getFloatSemantics(ToQT
);
2710 return this->emitCastIntegralFloating(FromT
, ToSem
, getRoundingMode(E
),
2718 /// When calling this, we have a pointer of the local-to-destroy
2720 /// Emit destruction of record types (or arrays of record types).
2721 /// FIXME: Handle virtual destructors.
2722 template <class Emitter
>
2723 bool ByteCodeExprGen
<Emitter
>::emitRecordDestruction(const Descriptor
*Desc
) {
2725 assert(!Desc
->isPrimitive());
2726 assert(!Desc
->isPrimitiveArray());
2729 if (Desc
->isArray()) {
2730 const Descriptor
*ElemDesc
= Desc
->ElemDesc
;
2733 // Don't need to do anything for these.
2734 if (ElemDesc
->isPrimitiveArray())
2735 return this->emitPopPtr(SourceInfo
{});
2737 // If this is an array of record types, check if we need
2738 // to call the element destructors at all. If not, try
2739 // to save the work.
2740 if (const Record
*ElemRecord
= ElemDesc
->ElemRecord
) {
2741 if (const CXXDestructorDecl
*Dtor
= ElemRecord
->getDestructor();
2742 !Dtor
|| Dtor
->isTrivial())
2743 return this->emitPopPtr(SourceInfo
{});
2746 for (ssize_t I
= Desc
->getNumElems() - 1; I
>= 0; --I
) {
2747 if (!this->emitConstUint64(I
, SourceInfo
{}))
2749 if (!this->emitArrayElemPtrUint64(SourceInfo
{}))
2751 if (!this->emitRecordDestruction(ElemDesc
))
2754 return this->emitPopPtr(SourceInfo
{});
2757 const Record
*R
= Desc
->ElemRecord
;
2759 // First, destroy all fields.
2760 for (const Record::Field
&Field
: llvm::reverse(R
->fields())) {
2761 const Descriptor
*D
= Field
.Desc
;
2762 if (!D
->isPrimitive() && !D
->isPrimitiveArray()) {
2763 if (!this->emitDupPtr(SourceInfo
{}))
2765 if (!this->emitGetPtrField(Field
.Offset
, SourceInfo
{}))
2767 if (!this->emitRecordDestruction(D
))
2772 // FIXME: Unions need to be handled differently here. We don't want to
2773 // call the destructor of its members.
2775 // Now emit the destructor and recurse into base classes.
2776 if (const CXXDestructorDecl
*Dtor
= R
->getDestructor();
2777 Dtor
&& !Dtor
->isTrivial()) {
2778 if (const Function
*DtorFunc
= getFunction(Dtor
)) {
2779 assert(DtorFunc
->hasThisPointer());
2780 assert(DtorFunc
->getNumParams() == 1);
2781 if (!this->emitDupPtr(SourceInfo
{}))
2783 if (!this->emitCall(DtorFunc
, SourceInfo
{}))
2788 for (const Record::Base
&Base
: llvm::reverse(R
->bases())) {
2789 if (!this->emitGetPtrBase(Base
.Offset
, SourceInfo
{}))
2791 if (!this->emitRecordDestruction(Base
.Desc
))
2794 // FIXME: Virtual bases.
2796 // Remove the instance pointer.
2797 return this->emitPopPtr(SourceInfo
{});
2803 template class ByteCodeExprGen
<ByteCodeEmitter
>;
2804 template class ByteCodeExprGen
<EvalEmitter
>;
2806 } // namespace interp
2807 } // namespace clang