1 //===--- ByteCodeExprGen.cpp - Code generator for expressions ---*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ByteCodeExprGen.h"
10 #include "ByteCodeEmitter.h"
11 #include "ByteCodeGenError.h"
12 #include "ByteCodeStmtGen.h"
13 #include "Context.h"
14 #include "Floating.h"
15 #include "Function.h"
16 #include "PrimType.h"
17 #include "Program.h"
18 
19 using namespace clang;
20 using namespace clang::interp;
21 
22 using APSInt = llvm::APSInt;
23 
24 namespace clang {
25 namespace interp {
26 
27 /// Scope used to handle temporaries in toplevel variable declarations.
28 template <class Emitter> class DeclScope final : public VariableScope<Emitter> {
29 public:
30   DeclScope(ByteCodeExprGen<Emitter> *Ctx, const ValueDecl *VD)
31       : VariableScope<Emitter>(Ctx), Scope(Ctx->P, VD),
32         OldGlobalDecl(Ctx->GlobalDecl) {
33     Ctx->GlobalDecl = Context::shouldBeGloballyIndexed(VD);
34   }
35 
36   void addExtended(const Scope::Local &Local) override {
37     return this->addLocal(Local);
38   }
39 
40   ~DeclScope() { this->Ctx->GlobalDecl = OldGlobalDecl; }
41 
42 private:
43   Program::DeclScope Scope;
44   bool OldGlobalDecl;
45 };
46 
47 /// Scope used to handle initialization methods.
48 template <class Emitter> class OptionScope final {
49 public:
50   /// Root constructor, compiling or discarding primitives.
51   OptionScope(ByteCodeExprGen<Emitter> *Ctx, bool NewDiscardResult,
52               bool NewInitializing)
53       : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
54         OldInitializing(Ctx->Initializing) {
55     Ctx->DiscardResult = NewDiscardResult;
56     Ctx->Initializing = NewInitializing;
57   }
58 
59   ~OptionScope() {
60     Ctx->DiscardResult = OldDiscardResult;
61     Ctx->Initializing = OldInitializing;
62   }
63 
64 private:
65   /// Parent context.
66   ByteCodeExprGen<Emitter> *Ctx;
67   /// Old discard flag to restore.
68   bool OldDiscardResult;
69   bool OldInitializing;
70 };
71 
72 } // namespace interp
73 } // namespace clang
74 
75 template <class Emitter>
76 bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
77   const Expr *SubExpr = CE->getSubExpr();
78   switch (CE->getCastKind()) {
79 
80   case CK_LValueToRValue: {
81     return dereference(
82         SubExpr, DerefKind::Read,
83         [](PrimType) {
84           // Value loaded - nothing to do here.
85           return true;
86         },
87         [this, CE](PrimType T) {
88           // Pointer on stack - dereference it.
89           if (!this->emitLoadPop(T, CE))
90             return false;
91           return DiscardResult ? this->emitPop(T, CE) : true;
92         });
93   }
94 
95   case CK_UncheckedDerivedToBase:
96   case CK_DerivedToBase: {
97     if (!this->visit(SubExpr))
98       return false;
99 
100     unsigned DerivedOffset = collectBaseOffset(getRecordTy(CE->getType()),
101                                                getRecordTy(SubExpr->getType()));
102 
103     return this->emitGetPtrBasePop(DerivedOffset, CE);
104   }
105 
106   case CK_BaseToDerived: {
107     if (!this->visit(SubExpr))
108       return false;
109 
110     unsigned DerivedOffset = collectBaseOffset(getRecordTy(SubExpr->getType()),
111                                                getRecordTy(CE->getType()));
112 
113     return this->emitGetPtrDerivedPop(DerivedOffset, CE);
114   }
115 
116   case CK_FloatingCast: {
117     if (DiscardResult)
118       return this->discard(SubExpr);
119     if (!this->visit(SubExpr))
120       return false;
121     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
122     return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
123   }
124 
125   case CK_IntegralToFloating: {
126     if (DiscardResult)
127       return this->discard(SubExpr);
128     std::optional<PrimType> FromT = classify(SubExpr->getType());
129     if (!FromT)
130       return false;
131 
132     if (!this->visit(SubExpr))
133       return false;
134 
135     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
136     llvm::RoundingMode RM = getRoundingMode(CE);
137     return this->emitCastIntegralFloating(*FromT, TargetSemantics, RM, CE);
138   }
139 
140   case CK_FloatingToBoolean:
141   case CK_FloatingToIntegral: {
142     if (DiscardResult)
143       return this->discard(SubExpr);
144 
145     std::optional<PrimType> ToT = classify(CE->getType());
146 
147     if (!ToT)
148       return false;
149 
150     if (!this->visit(SubExpr))
151       return false;
152 
153     if (ToT == PT_IntAP)
154       return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
155                                               CE);
156     if (ToT == PT_IntAPS)
157       return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
158                                                CE);
159 
160     return this->emitCastFloatingIntegral(*ToT, CE);
161   }
162 
163   case CK_NullToPointer:
164     if (DiscardResult)
165       return true;
166     return this->emitNull(classifyPrim(CE->getType()), CE);
167 
168   case CK_PointerToIntegral: {
169     // TODO: Discard handling.
170     if (!this->visit(SubExpr))
171       return false;
172 
173     PrimType T = classifyPrim(CE->getType());
174     return this->emitCastPointerIntegral(T, CE);
175   }
176 
177   case CK_ArrayToPointerDecay: {
178     if (!this->visit(SubExpr))
179       return false;
180     if (!this->emitArrayDecay(CE))
181       return false;
182     if (DiscardResult)
183       return this->emitPopPtr(CE);
184     return true;
185   }
186 
187   case CK_AtomicToNonAtomic:
188   case CK_ConstructorConversion:
189   case CK_FunctionToPointerDecay:
190   case CK_NonAtomicToAtomic:
191   case CK_NoOp:
192   case CK_UserDefinedConversion:
193   case CK_BitCast:
194     return this->delegate(SubExpr);
195 
196   case CK_IntegralToBoolean:
197   case CK_IntegralCast: {
198     if (DiscardResult)
199       return this->discard(SubExpr);
200     std::optional<PrimType> FromT = classify(SubExpr->getType());
201     std::optional<PrimType> ToT = classify(CE->getType());
202 
203     if (!FromT || !ToT)
204       return false;
205 
206     if (!this->visit(SubExpr))
207       return false;
208 
209     if (ToT == PT_IntAP)
210       return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE);
211     if (ToT == PT_IntAPS)
212       return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE);
213 
214     if (FromT == ToT)
215       return true;
216     return this->emitCast(*FromT, *ToT, CE);
217   }
218 
219   case CK_PointerToBoolean: {
220     PrimType PtrT = classifyPrim(SubExpr->getType());
221 
222     // Just emit p != nullptr for this.
223     if (!this->visit(SubExpr))
224       return false;
225 
226     if (!this->emitNull(PtrT, CE))
227       return false;
228 
229     return this->emitNE(PtrT, CE);
230   }
231 
232   case CK_IntegralComplexToBoolean:
233   case CK_FloatingComplexToBoolean: {
234     std::optional<PrimType> ElemT =
235         classifyComplexElementType(SubExpr->getType());
236     if (!ElemT)
237       return false;
238     // We emit the expression (__real(E) != 0 || __imag(E) != 0)
239     // for us, that means (bool)E[0] || (bool)E[1]
240     if (!this->visit(SubExpr))
241       return false;
242     if (!this->emitConstUint8(0, CE))
243       return false;
244     if (!this->emitArrayElemPtrUint8(CE))
245       return false;
246     if (!this->emitLoadPop(*ElemT, CE))
247       return false;
248     if (*ElemT == PT_Float) {
249       if (!this->emitCastFloatingIntegral(PT_Bool, CE))
250         return false;
251     } else {
252       if (!this->emitCast(*ElemT, PT_Bool, CE))
253         return false;
254     }
255 
256     // We now have the bool value of E[0] on the stack.
257     LabelTy LabelTrue = this->getLabel();
258     if (!this->jumpTrue(LabelTrue))
259       return false;
260 
261     if (!this->emitConstUint8(1, CE))
262       return false;
263     if (!this->emitArrayElemPtrPopUint8(CE))
264       return false;
265     if (!this->emitLoadPop(*ElemT, CE))
266       return false;
267     if (*ElemT == PT_Float) {
268       if (!this->emitCastFloatingIntegral(PT_Bool, CE))
269         return false;
270     } else {
271       if (!this->emitCast(*ElemT, PT_Bool, CE))
272         return false;
273     }
274     // Leave the boolean value of E[1] on the stack.
275     LabelTy EndLabel = this->getLabel();
276     this->jump(EndLabel);
277 
278     this->emitLabel(LabelTrue);
279     if (!this->emitPopPtr(CE))
280       return false;
281     if (!this->emitConstBool(true, CE))
282       return false;
283 
284     this->fallthrough(EndLabel);
285     this->emitLabel(EndLabel);
286 
287     return true;
288   }
289 
290   case CK_ToVoid:
291     return discard(SubExpr);
292 
293   default:
294     assert(false && "Cast not implemented");
295   }
296   llvm_unreachable("Unhandled clang::CastKind enum");
297 }
298 
299 template <class Emitter>
300 bool ByteCodeExprGen<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
301   if (DiscardResult)
302     return true;
303 
304   return this->emitConst(LE->getValue(), LE);
305 }
306 
307 template <class Emitter>
308 bool ByteCodeExprGen<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
309   if (DiscardResult)
310     return true;
311 
312   return this->emitConstFloat(E->getValue(), E);
313 }
314 
315 template <class Emitter>
316 bool ByteCodeExprGen<Emitter>::VisitParenExpr(const ParenExpr *E) {
317   return this->delegate(E->getSubExpr());
318 }
319 
320 template <class Emitter>
321 bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
322   // Need short-circuiting for these.
323   if (BO->isLogicalOp())
324     return this->VisitLogicalBinOp(BO);
325 
326   if (BO->getType()->isAnyComplexType())
327     return this->VisitComplexBinOp(BO);
328 
329   const Expr *LHS = BO->getLHS();
330   const Expr *RHS = BO->getRHS();
331 
332   if (BO->isPtrMemOp())
333     return this->visit(RHS);
334 
335   // Typecheck the args.
336   std::optional<PrimType> LT = classify(LHS->getType());
337   std::optional<PrimType> RT = classify(RHS->getType());
338   std::optional<PrimType> T = classify(BO->getType());
339 
340   // Deal with operations which have composite or void types.
341   if (BO->isCommaOp()) {
342     if (!this->discard(LHS))
343       return false;
344     if (RHS->getType()->isVoidType())
345       return this->discard(RHS);
346 
347     return this->delegate(RHS);
348   }
349 
350   // Special case for C++'s three-way/spaceship operator <=>, which
351   // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
352   // have a PrimType).
353   if (!T) {
354     if (DiscardResult)
355       return true;
356     const ComparisonCategoryInfo *CmpInfo =
357         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
358     assert(CmpInfo);
359 
360     // We need a temporary variable holding our return value.
361     if (!Initializing) {
362       std::optional<unsigned> ResultIndex = this->allocateLocal(BO, false);
363       if (!this->emitGetPtrLocal(*ResultIndex, BO))
364         return false;
365     }
366 
367     if (!visit(LHS) || !visit(RHS))
368       return false;
369 
370     return this->emitCMP3(*LT, CmpInfo, BO);
371   }
372 
373   if (!LT || !RT || !T)
374     return this->bail(BO);
375 
376   // Pointer arithmetic special case.
377   if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
378     if (T == PT_Ptr || (LT == PT_Ptr && RT == PT_Ptr))
379       return this->VisitPointerArithBinOp(BO);
380   }
381 
382   if (!visit(LHS) || !visit(RHS))
383     return false;
384 
385   // For languages such as C, cast the result of one
386   // of our comparision opcodes to T (which is usually int).
387   auto MaybeCastToBool = [this, T, BO](bool Result) {
388     if (!Result)
389       return false;
390     if (DiscardResult)
391       return this->emitPop(*T, BO);
392     if (T != PT_Bool)
393       return this->emitCast(PT_Bool, *T, BO);
394     return true;
395   };
396 
397   auto Discard = [this, T, BO](bool Result) {
398     if (!Result)
399       return false;
400     return DiscardResult ? this->emitPop(*T, BO) : true;
401   };
402 
403   switch (BO->getOpcode()) {
404   case BO_EQ:
405     return MaybeCastToBool(this->emitEQ(*LT, BO));
406   case BO_NE:
407     return MaybeCastToBool(this->emitNE(*LT, BO));
408   case BO_LT:
409     return MaybeCastToBool(this->emitLT(*LT, BO));
410   case BO_LE:
411     return MaybeCastToBool(this->emitLE(*LT, BO));
412   case BO_GT:
413     return MaybeCastToBool(this->emitGT(*LT, BO));
414   case BO_GE:
415     return MaybeCastToBool(this->emitGE(*LT, BO));
416   case BO_Sub:
417     if (BO->getType()->isFloatingType())
418       return Discard(this->emitSubf(getRoundingMode(BO), BO));
419     return Discard(this->emitSub(*T, BO));
420   case BO_Add:
421     if (BO->getType()->isFloatingType())
422       return Discard(this->emitAddf(getRoundingMode(BO), BO));
423     return Discard(this->emitAdd(*T, BO));
424   case BO_Mul:
425     if (BO->getType()->isFloatingType())
426       return Discard(this->emitMulf(getRoundingMode(BO), BO));
427     return Discard(this->emitMul(*T, BO));
428   case BO_Rem:
429     return Discard(this->emitRem(*T, BO));
430   case BO_Div:
431     if (BO->getType()->isFloatingType())
432       return Discard(this->emitDivf(getRoundingMode(BO), BO));
433     return Discard(this->emitDiv(*T, BO));
434   case BO_Assign:
435     if (DiscardResult)
436       return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
437                                      : this->emitStorePop(*T, BO);
438     return LHS->refersToBitField() ? this->emitStoreBitField(*T, BO)
439                                    : this->emitStore(*T, BO);
440   case BO_And:
441     return Discard(this->emitBitAnd(*T, BO));
442   case BO_Or:
443     return Discard(this->emitBitOr(*T, BO));
444   case BO_Shl:
445     return Discard(this->emitShl(*LT, *RT, BO));
446   case BO_Shr:
447     return Discard(this->emitShr(*LT, *RT, BO));
448   case BO_Xor:
449     return Discard(this->emitBitXor(*T, BO));
450   case BO_LOr:
451   case BO_LAnd:
452     llvm_unreachable("Already handled earlier");
453   default:
454     return this->bail(BO);
455   }
456 
457   llvm_unreachable("Unhandled binary op");
458 }
459 
460 /// Perform addition/subtraction of a pointer and an integer or
461 /// subtraction of two pointers.
462 template <class Emitter>
463 bool ByteCodeExprGen<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
464   BinaryOperatorKind Op = E->getOpcode();
465   const Expr *LHS = E->getLHS();
466   const Expr *RHS = E->getRHS();
467 
468   if ((Op != BO_Add && Op != BO_Sub) ||
469       (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
470     return false;
471 
472   std::optional<PrimType> LT = classify(LHS);
473   std::optional<PrimType> RT = classify(RHS);
474 
475   if (!LT || !RT)
476     return false;
477 
478   if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
479     if (Op != BO_Sub)
480       return false;
481 
482     assert(E->getType()->isIntegerType());
483     if (!visit(RHS) || !visit(LHS))
484       return false;
485 
486     return this->emitSubPtr(classifyPrim(E->getType()), E);
487   }
488 
489   PrimType OffsetType;
490   if (LHS->getType()->isIntegerType()) {
491     if (!visit(RHS) || !visit(LHS))
492       return false;
493     OffsetType = *LT;
494   } else if (RHS->getType()->isIntegerType()) {
495     if (!visit(LHS) || !visit(RHS))
496       return false;
497     OffsetType = *RT;
498   } else {
499     return false;
500   }
501 
502   if (Op == BO_Add)
503     return this->emitAddOffset(OffsetType, E);
504   else if (Op == BO_Sub)
505     return this->emitSubOffset(OffsetType, E);
506 
507   return this->bail(E);
508 }
509 
510 template <class Emitter>
511 bool ByteCodeExprGen<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
512   assert(E->isLogicalOp());
513   BinaryOperatorKind Op = E->getOpcode();
514   const Expr *LHS = E->getLHS();
515   const Expr *RHS = E->getRHS();
516   std::optional<PrimType> T = classify(E->getType());
517 
518   if (Op == BO_LOr) {
519     // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
520     LabelTy LabelTrue = this->getLabel();
521     LabelTy LabelEnd = this->getLabel();
522 
523     if (!this->visitBool(LHS))
524       return false;
525     if (!this->jumpTrue(LabelTrue))
526       return false;
527 
528     if (!this->visitBool(RHS))
529       return false;
530     if (!this->jump(LabelEnd))
531       return false;
532 
533     this->emitLabel(LabelTrue);
534     this->emitConstBool(true, E);
535     this->fallthrough(LabelEnd);
536     this->emitLabel(LabelEnd);
537 
538   } else {
539     assert(Op == BO_LAnd);
540     // Logical AND.
541     // Visit LHS. Only visit RHS if LHS was TRUE.
542     LabelTy LabelFalse = this->getLabel();
543     LabelTy LabelEnd = this->getLabel();
544 
545     if (!this->visitBool(LHS))
546       return false;
547     if (!this->jumpFalse(LabelFalse))
548       return false;
549 
550     if (!this->visitBool(RHS))
551       return false;
552     if (!this->jump(LabelEnd))
553       return false;
554 
555     this->emitLabel(LabelFalse);
556     this->emitConstBool(false, E);
557     this->fallthrough(LabelEnd);
558     this->emitLabel(LabelEnd);
559   }
560 
561   if (DiscardResult)
562     return this->emitPopBool(E);
563 
564   // For C, cast back to integer type.
565   assert(T);
566   if (T != PT_Bool)
567     return this->emitCast(PT_Bool, *T, E);
568   return true;
569 }
570 
571 template <class Emitter>
572 bool ByteCodeExprGen<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
573   assert(Initializing);
574 
575   const Expr *LHS = E->getLHS();
576   const Expr *RHS = E->getRHS();
577   PrimType LHSElemT = *this->classifyComplexElementType(LHS->getType());
578   PrimType RHSElemT = *this->classifyComplexElementType(RHS->getType());
579 
580   unsigned LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
581   unsigned RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
582   unsigned ResultOffset = ~0u;
583   if (!this->DiscardResult)
584     ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
585 
586   assert(LHSElemT == RHSElemT);
587 
588   // Save result pointer in ResultOffset
589   if (!this->DiscardResult) {
590     if (!this->emitDupPtr(E))
591       return false;
592     if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
593       return false;
594   }
595 
596   // Evaluate LHS and save value to LHSOffset.
597   if (!this->visit(LHS))
598     return false;
599   if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
600     return false;
601 
602   // Same with RHS.
603   if (!this->visit(RHS))
604     return false;
605   if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
606     return false;
607 
608   // Now we can get pointers to the LHS and RHS from the offsets above.
609   BinaryOperatorKind Op = E->getOpcode();
610   for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
611     // Result pointer for the store later.
612     if (!this->DiscardResult) {
613       if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
614         return false;
615     }
616 
617     if (!this->emitGetLocal(PT_Ptr, LHSOffset, E))
618       return false;
619     if (!this->emitConstUint8(ElemIndex, E))
620       return false;
621     if (!this->emitArrayElemPtrPopUint8(E))
622       return false;
623     if (!this->emitLoadPop(LHSElemT, E))
624       return false;
625 
626     if (!this->emitGetLocal(PT_Ptr, RHSOffset, E))
627       return false;
628     if (!this->emitConstUint8(ElemIndex, E))
629       return false;
630     if (!this->emitArrayElemPtrPopUint8(E))
631       return false;
632     if (!this->emitLoadPop(RHSElemT, E))
633       return false;
634 
635     // The actual operation.
636     switch (Op) {
637     case BO_Add:
638       if (LHSElemT == PT_Float) {
639         if (!this->emitAddf(getRoundingMode(E), E))
640           return false;
641       } else {
642         if (!this->emitAdd(LHSElemT, E))
643           return false;
644       }
645       break;
646     case BO_Sub:
647       if (LHSElemT == PT_Float) {
648         if (!this->emitSubf(getRoundingMode(E), E))
649           return false;
650       } else {
651         if (!this->emitSub(LHSElemT, E))
652           return false;
653       }
654       break;
655 
656     default:
657       return false;
658     }
659 
660     if (!this->DiscardResult) {
661       // Initialize array element with the value we just computed.
662       if (!this->emitInitElemPop(LHSElemT, ElemIndex, E))
663         return false;
664     } else {
665       if (!this->emitPop(LHSElemT, E))
666         return false;
667     }
668   }
669   return true;
670 }
671 
672 template <class Emitter>
673 bool ByteCodeExprGen<Emitter>::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
674   QualType QT = E->getType();
675 
676   if (std::optional<PrimType> T = classify(QT))
677     return this->visitZeroInitializer(*T, QT, E);
678 
679   if (QT->isRecordType())
680     return false;
681 
682   if (QT->isIncompleteArrayType())
683     return true;
684 
685   if (QT->isArrayType()) {
686     const ArrayType *AT = QT->getAsArrayTypeUnsafe();
687     assert(AT);
688     const auto *CAT = cast<ConstantArrayType>(AT);
689     size_t NumElems = CAT->getSize().getZExtValue();
690     PrimType ElemT = classifyPrim(CAT->getElementType());
691 
692     for (size_t I = 0; I != NumElems; ++I) {
693       if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E))
694         return false;
695       if (!this->emitInitElem(ElemT, I, E))
696         return false;
697     }
698 
699     return true;
700   }
701 
702   return false;
703 }
704 
705 template <class Emitter>
706 bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr(
707     const ArraySubscriptExpr *E) {
708   const Expr *Base = E->getBase();
709   const Expr *Index = E->getIdx();
710 
711   if (DiscardResult)
712     return this->discard(Base) && this->discard(Index);
713 
714   // Take pointer of LHS, add offset from RHS.
715   // What's left on the stack after this is a pointer.
716   if (!this->visit(Base))
717     return false;
718 
719   if (!this->visit(Index))
720     return false;
721 
722   PrimType IndexT = classifyPrim(Index->getType());
723   return this->emitArrayElemPtrPop(IndexT, E);
724 }
725 
726 template <class Emitter>
727 bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
728                                              const Expr *E) {
729   assert(E->getType()->isRecordType());
730   const Record *R = getRecord(E->getType());
731 
732   unsigned InitIndex = 0;
733   for (const Expr *Init : Inits) {
734     if (!this->emitDupPtr(E))
735       return false;
736 
737     if (std::optional<PrimType> T = classify(Init)) {
738       const Record::Field *FieldToInit = R->getField(InitIndex);
739       if (!this->visit(Init))
740         return false;
741 
742       if (FieldToInit->isBitField()) {
743         if (!this->emitInitBitField(*T, FieldToInit, E))
744           return false;
745       } else {
746         if (!this->emitInitField(*T, FieldToInit->Offset, E))
747           return false;
748       }
749 
750       if (!this->emitPopPtr(E))
751         return false;
752       ++InitIndex;
753     } else {
754       // Initializer for a direct base class.
755       if (const Record::Base *B = R->getBase(Init->getType())) {
756         if (!this->emitGetPtrBasePop(B->Offset, Init))
757           return false;
758 
759         if (!this->visitInitializer(Init))
760           return false;
761 
762         if (!this->emitInitPtrPop(E))
763           return false;
764         // Base initializers don't increase InitIndex, since they don't count
765         // into the Record's fields.
766       } else {
767         const Record::Field *FieldToInit = R->getField(InitIndex);
768         // Non-primitive case. Get a pointer to the field-to-initialize
769         // on the stack and recurse into visitInitializer().
770         if (!this->emitGetPtrField(FieldToInit->Offset, Init))
771           return false;
772 
773         if (!this->visitInitializer(Init))
774           return false;
775 
776         if (!this->emitPopPtr(E))
777           return false;
778         ++InitIndex;
779       }
780     }
781   }
782   return true;
783 }
784 
785 /// Pointer to the array(not the element!) must be on the stack when calling
786 /// this.
787 template <class Emitter>
788 bool ByteCodeExprGen<Emitter>::visitArrayElemInit(unsigned ElemIndex,
789                                                   const Expr *Init) {
790   if (std::optional<PrimType> T = classify(Init->getType())) {
791     // Visit the primitive element like normal.
792     if (!this->visit(Init))
793       return false;
794     return this->emitInitElem(*T, ElemIndex, Init);
795   }
796 
797   // Advance the pointer currently on the stack to the given
798   // dimension.
799   if (!this->emitConstUint32(ElemIndex, Init))
800     return false;
801   if (!this->emitArrayElemPtrUint32(Init))
802     return false;
803   if (!this->visitInitializer(Init))
804     return false;
805   return this->emitPopPtr(Init);
806 }
807 
808 template <class Emitter>
809 bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
810   // Handle discarding first.
811   if (DiscardResult) {
812     for (const Expr *Init : E->inits()) {
813       if (!this->discard(Init))
814         return false;
815     }
816     return true;
817   }
818 
819   // Primitive values.
820   if (std::optional<PrimType> T = classify(E->getType())) {
821     assert(!DiscardResult);
822     if (E->getNumInits() == 0)
823       return this->visitZeroInitializer(*T, E->getType(), E);
824     assert(E->getNumInits() == 1);
825     return this->delegate(E->inits()[0]);
826   }
827 
828   QualType T = E->getType();
829   if (T->isRecordType())
830     return this->visitInitList(E->inits(), E);
831 
832   if (T->isArrayType()) {
833     // FIXME: Array fillers.
834     unsigned ElementIndex = 0;
835     for (const Expr *Init : E->inits()) {
836       if (!this->visitArrayElemInit(ElementIndex, Init))
837         return false;
838       ++ElementIndex;
839     }
840     return true;
841   }
842 
843   if (T->isAnyComplexType()) {
844     unsigned NumInits = E->getNumInits();
845     QualType ElemQT = E->getType()->getAs<ComplexType>()->getElementType();
846     PrimType ElemT = classifyPrim(ElemQT);
847     if (NumInits == 0) {
848       // Zero-initialize both elements.
849       for (unsigned I = 0; I < 2; ++I) {
850         if (!this->visitZeroInitializer(ElemT, ElemQT, E))
851           return false;
852         if (!this->emitInitElem(ElemT, I, E))
853           return false;
854       }
855     } else if (NumInits == 2) {
856       unsigned InitIndex = 0;
857       for (const Expr *Init : E->inits()) {
858         if (!this->visit(Init))
859           return false;
860 
861         if (!this->emitInitElem(ElemT, InitIndex, E))
862           return false;
863         ++InitIndex;
864       }
865     }
866     return true;
867   }
868 
869   return false;
870 }
871 
872 template <class Emitter>
873 bool ByteCodeExprGen<Emitter>::VisitCXXParenListInitExpr(
874     const CXXParenListInitExpr *E) {
875   if (DiscardResult) {
876     for (const Expr *Init : E->getInitExprs()) {
877       if (!this->discard(Init))
878         return false;
879     }
880     return true;
881   }
882 
883   assert(E->getType()->isRecordType());
884   return this->visitInitList(E->getInitExprs(), E);
885 }
886 
887 template <class Emitter>
888 bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(
889     const SubstNonTypeTemplateParmExpr *E) {
890   return this->delegate(E->getReplacement());
891 }
892 
893 template <class Emitter>
894 bool ByteCodeExprGen<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
895   // Try to emit the APValue directly, without visiting the subexpr.
896   // This will only fail if we can't emit the APValue, so won't emit any
897   // diagnostics or any double values.
898   std::optional<PrimType> T = classify(E->getType());
899   if (T && E->hasAPValueResult() &&
900       this->visitAPValue(E->getAPValueResult(), *T, E))
901     return true;
902 
903   return this->delegate(E->getSubExpr());
904 }
905 
906 static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx,
907                              UnaryExprOrTypeTrait Kind) {
908   bool AlignOfReturnsPreferred =
909       ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
910 
911   // C++ [expr.alignof]p3:
912   //     When alignof is applied to a reference type, the result is the
913   //     alignment of the referenced type.
914   if (const auto *Ref = T->getAs<ReferenceType>())
915     T = Ref->getPointeeType();
916 
917   // __alignof is defined to return the preferred alignment.
918   // Before 8, clang returned the preferred alignment for alignof and
919   // _Alignof as well.
920   if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
921     return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
922 
923   return ASTCtx.getTypeAlignInChars(T);
924 }
925 
926 template <class Emitter>
927 bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
928     const UnaryExprOrTypeTraitExpr *E) {
929   UnaryExprOrTypeTrait Kind = E->getKind();
930   ASTContext &ASTCtx = Ctx.getASTContext();
931 
932   if (Kind == UETT_SizeOf) {
933     QualType ArgType = E->getTypeOfArgument();
934     CharUnits Size;
935     if (ArgType->isVoidType() || ArgType->isFunctionType())
936       Size = CharUnits::One();
937     else {
938       if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
939         return false;
940 
941       Size = ASTCtx.getTypeSizeInChars(ArgType);
942     }
943 
944     if (DiscardResult)
945       return true;
946 
947     return this->emitConst(Size.getQuantity(), E);
948   }
949 
950   if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
951     CharUnits Size;
952 
953     if (E->isArgumentType()) {
954       QualType ArgType = E->getTypeOfArgument();
955 
956       Size = AlignOfType(ArgType, ASTCtx, Kind);
957     } else {
958       // Argument is an expression, not a type.
959       const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
960 
961       // The kinds of expressions that we have special-case logic here for
962       // should be kept up to date with the special checks for those
963       // expressions in Sema.
964 
965       // alignof decl is always accepted, even if it doesn't make sense: we
966       // default to 1 in those cases.
967       if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
968         Size = ASTCtx.getDeclAlign(DRE->getDecl(),
969                                    /*RefAsPointee*/ true);
970       else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
971         Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
972                                    /*RefAsPointee*/ true);
973       else
974         Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
975     }
976 
977     if (DiscardResult)
978       return true;
979 
980     return this->emitConst(Size.getQuantity(), E);
981   }
982 
983   return false;
984 }
985 
986 template <class Emitter>
987 bool ByteCodeExprGen<Emitter>::VisitMemberExpr(const MemberExpr *E) {
988   // 'Base.Member'
989   const Expr *Base = E->getBase();
990 
991   if (DiscardResult)
992     return this->discard(Base);
993 
994   if (!this->visit(Base))
995     return false;
996 
997   // Base above gives us a pointer on the stack.
998   // TODO: Implement non-FieldDecl members.
999   const ValueDecl *Member = E->getMemberDecl();
1000   if (const auto *FD = dyn_cast<FieldDecl>(Member)) {
1001     const RecordDecl *RD = FD->getParent();
1002     const Record *R = getRecord(RD);
1003     const Record::Field *F = R->getField(FD);
1004     // Leave a pointer to the field on the stack.
1005     if (F->Decl->getType()->isReferenceType())
1006       return this->emitGetFieldPop(PT_Ptr, F->Offset, E);
1007     return this->emitGetPtrField(F->Offset, E);
1008   }
1009 
1010   return false;
1011 }
1012 
1013 template <class Emitter>
1014 bool ByteCodeExprGen<Emitter>::VisitArrayInitIndexExpr(
1015     const ArrayInitIndexExpr *E) {
1016   // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
1017   // stand-alone, e.g. via EvaluateAsInt().
1018   if (!ArrayIndex)
1019     return false;
1020   return this->emitConst(*ArrayIndex, E);
1021 }
1022 
1023 template <class Emitter>
1024 bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
1025     const ArrayInitLoopExpr *E) {
1026   assert(Initializing);
1027   assert(!DiscardResult);
1028   // TODO: This compiles to quite a lot of bytecode if the array is larger.
1029   //   Investigate compiling this to a loop.
1030 
1031   const Expr *SubExpr = E->getSubExpr();
1032   const Expr *CommonExpr = E->getCommonExpr();
1033   size_t Size = E->getArraySize().getZExtValue();
1034 
1035   // If the common expression is an opaque expression, we visit it
1036   // here once so we have its value cached.
1037   // FIXME: This might be necessary (or useful) for all expressions.
1038   if (isa<OpaqueValueExpr>(CommonExpr)) {
1039     if (!this->discard(CommonExpr))
1040       return false;
1041   }
1042 
1043   // So, every iteration, we execute an assignment here
1044   // where the LHS is on the stack (the target array)
1045   // and the RHS is our SubExpr.
1046   for (size_t I = 0; I != Size; ++I) {
1047     ArrayIndexScope<Emitter> IndexScope(this, I);
1048     BlockScope<Emitter> BS(this);
1049 
1050     if (!this->visitArrayElemInit(I, SubExpr))
1051       return false;
1052   }
1053   return true;
1054 }
1055 
1056 template <class Emitter>
1057 bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
1058   if (Initializing)
1059     return this->visitInitializer(E->getSourceExpr());
1060 
1061   PrimType SubExprT = classify(E->getSourceExpr()).value_or(PT_Ptr);
1062   if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
1063     return this->emitGetLocal(SubExprT, It->second, E);
1064 
1065   if (!this->visit(E->getSourceExpr()))
1066     return false;
1067 
1068   // At this point we either have the evaluated source expression or a pointer
1069   // to an object on the stack. We want to create a local variable that stores
1070   // this value.
1071   std::optional<unsigned> LocalIndex =
1072       allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
1073   if (!LocalIndex)
1074     return false;
1075   if (!this->emitSetLocal(SubExprT, *LocalIndex, E))
1076     return false;
1077 
1078   // Here the local variable is created but the value is removed from the stack,
1079   // so we put it back, because the caller might need it.
1080   if (!DiscardResult) {
1081     if (!this->emitGetLocal(SubExprT, *LocalIndex, E))
1082       return false;
1083   }
1084 
1085   // FIXME: Ideally the cached value should be cleaned up later.
1086   OpaqueExprs.insert({E, *LocalIndex});
1087 
1088   return true;
1089 }
1090 
1091 template <class Emitter>
1092 bool ByteCodeExprGen<Emitter>::VisitAbstractConditionalOperator(
1093     const AbstractConditionalOperator *E) {
1094   const Expr *Condition = E->getCond();
1095   const Expr *TrueExpr = E->getTrueExpr();
1096   const Expr *FalseExpr = E->getFalseExpr();
1097 
1098   LabelTy LabelEnd = this->getLabel();   // Label after the operator.
1099   LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
1100 
1101   if (!this->visitBool(Condition))
1102     return false;
1103 
1104   if (!this->jumpFalse(LabelFalse))
1105     return false;
1106 
1107   if (!this->delegate(TrueExpr))
1108     return false;
1109   if (!this->jump(LabelEnd))
1110     return false;
1111 
1112   this->emitLabel(LabelFalse);
1113 
1114   if (!this->delegate(FalseExpr))
1115     return false;
1116 
1117   this->fallthrough(LabelEnd);
1118   this->emitLabel(LabelEnd);
1119 
1120   return true;
1121 }
1122 
1123 template <class Emitter>
1124 bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {
1125   if (DiscardResult)
1126     return true;
1127 
1128   if (!Initializing) {
1129     unsigned StringIndex = P.createGlobalString(E);
1130     return this->emitGetPtrGlobal(StringIndex, E);
1131   }
1132 
1133   // We are initializing an array on the stack.
1134   const ConstantArrayType *CAT =
1135       Ctx.getASTContext().getAsConstantArrayType(E->getType());
1136   assert(CAT && "a string literal that's not a constant array?");
1137 
1138   // If the initializer string is too long, a diagnostic has already been
1139   // emitted. Read only the array length from the string literal.
1140   unsigned ArraySize = CAT->getSize().getZExtValue();
1141   unsigned N = std::min(ArraySize, E->getLength());
1142   size_t CharWidth = E->getCharByteWidth();
1143 
1144   for (unsigned I = 0; I != N; ++I) {
1145     uint32_t CodeUnit = E->getCodeUnit(I);
1146 
1147     if (CharWidth == 1) {
1148       this->emitConstSint8(CodeUnit, E);
1149       this->emitInitElemSint8(I, E);
1150     } else if (CharWidth == 2) {
1151       this->emitConstUint16(CodeUnit, E);
1152       this->emitInitElemUint16(I, E);
1153     } else if (CharWidth == 4) {
1154       this->emitConstUint32(CodeUnit, E);
1155       this->emitInitElemUint32(I, E);
1156     } else {
1157       llvm_unreachable("unsupported character width");
1158     }
1159   }
1160 
1161   // Fill up the rest of the char array with NUL bytes.
1162   for (unsigned I = N; I != ArraySize; ++I) {
1163     if (CharWidth == 1) {
1164       this->emitConstSint8(0, E);
1165       this->emitInitElemSint8(I, E);
1166     } else if (CharWidth == 2) {
1167       this->emitConstUint16(0, E);
1168       this->emitInitElemUint16(I, E);
1169     } else if (CharWidth == 4) {
1170       this->emitConstUint32(0, E);
1171       this->emitInitElemUint32(I, E);
1172     } else {
1173       llvm_unreachable("unsupported character width");
1174     }
1175   }
1176 
1177   return true;
1178 }
1179 
1180 template <class Emitter>
1181 bool ByteCodeExprGen<Emitter>::VisitCharacterLiteral(
1182     const CharacterLiteral *E) {
1183   if (DiscardResult)
1184     return true;
1185   return this->emitConst(E->getValue(), E);
1186 }
1187 
1188 template <class Emitter>
1189 bool ByteCodeExprGen<Emitter>::VisitFloatCompoundAssignOperator(
1190     const CompoundAssignOperator *E) {
1191 
1192   const Expr *LHS = E->getLHS();
1193   const Expr *RHS = E->getRHS();
1194   QualType LHSType = LHS->getType();
1195   QualType LHSComputationType = E->getComputationLHSType();
1196   QualType ResultType = E->getComputationResultType();
1197   std::optional<PrimType> LT = classify(LHSComputationType);
1198   std::optional<PrimType> RT = classify(ResultType);
1199 
1200   assert(ResultType->isFloatingType());
1201 
1202   if (!LT || !RT)
1203     return false;
1204 
1205   PrimType LHST = classifyPrim(LHSType);
1206 
1207   // C++17 onwards require that we evaluate the RHS first.
1208   // Compute RHS and save it in a temporary variable so we can
1209   // load it again later.
1210   if (!visit(RHS))
1211     return false;
1212 
1213   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
1214   if (!this->emitSetLocal(*RT, TempOffset, E))
1215     return false;
1216 
1217   // First, visit LHS.
1218   if (!visit(LHS))
1219     return false;
1220   if (!this->emitLoad(LHST, E))
1221     return false;
1222 
1223   // If necessary, convert LHS to its computation type.
1224   if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
1225                           LHSComputationType, E))
1226     return false;
1227 
1228   // Now load RHS.
1229   if (!this->emitGetLocal(*RT, TempOffset, E))
1230     return false;
1231 
1232   llvm::RoundingMode RM = getRoundingMode(E);
1233   switch (E->getOpcode()) {
1234   case BO_AddAssign:
1235     if (!this->emitAddf(RM, E))
1236       return false;
1237     break;
1238   case BO_SubAssign:
1239     if (!this->emitSubf(RM, E))
1240       return false;
1241     break;
1242   case BO_MulAssign:
1243     if (!this->emitMulf(RM, E))
1244       return false;
1245     break;
1246   case BO_DivAssign:
1247     if (!this->emitDivf(RM, E))
1248       return false;
1249     break;
1250   default:
1251     return false;
1252   }
1253 
1254   if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
1255     return false;
1256 
1257   if (DiscardResult)
1258     return this->emitStorePop(LHST, E);
1259   return this->emitStore(LHST, E);
1260 }
1261 
1262 template <class Emitter>
1263 bool ByteCodeExprGen<Emitter>::VisitPointerCompoundAssignOperator(
1264     const CompoundAssignOperator *E) {
1265   BinaryOperatorKind Op = E->getOpcode();
1266   const Expr *LHS = E->getLHS();
1267   const Expr *RHS = E->getRHS();
1268   std::optional<PrimType> LT = classify(LHS->getType());
1269   std::optional<PrimType> RT = classify(RHS->getType());
1270 
1271   if (Op != BO_AddAssign && Op != BO_SubAssign)
1272     return false;
1273 
1274   if (!LT || !RT)
1275     return false;
1276   assert(*LT == PT_Ptr);
1277 
1278   if (!visit(LHS))
1279     return false;
1280 
1281   if (!this->emitLoadPtr(LHS))
1282     return false;
1283 
1284   if (!visit(RHS))
1285     return false;
1286 
1287   if (Op == BO_AddAssign)
1288     this->emitAddOffset(*RT, E);
1289   else
1290     this->emitSubOffset(*RT, E);
1291 
1292   if (DiscardResult)
1293     return this->emitStorePopPtr(E);
1294   return this->emitStorePtr(E);
1295 }
1296 
1297 template <class Emitter>
1298 bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator(
1299     const CompoundAssignOperator *E) {
1300 
1301   const Expr *LHS = E->getLHS();
1302   const Expr *RHS = E->getRHS();
1303   std::optional<PrimType> LHSComputationT =
1304       classify(E->getComputationLHSType());
1305   std::optional<PrimType> LT = classify(LHS->getType());
1306   std::optional<PrimType> RT = classify(E->getComputationResultType());
1307   std::optional<PrimType> ResultT = classify(E->getType());
1308 
1309   if (!LT || !RT || !ResultT || !LHSComputationT)
1310     return false;
1311 
1312   // Handle floating point operations separately here, since they
1313   // require special care.
1314 
1315   if (ResultT == PT_Float || RT == PT_Float)
1316     return VisitFloatCompoundAssignOperator(E);
1317 
1318   if (E->getType()->isPointerType())
1319     return VisitPointerCompoundAssignOperator(E);
1320 
1321   assert(!E->getType()->isPointerType() && "Handled above");
1322   assert(!E->getType()->isFloatingType() && "Handled above");
1323 
1324   // C++17 onwards require that we evaluate the RHS first.
1325   // Compute RHS and save it in a temporary variable so we can
1326   // load it again later.
1327   // FIXME: Compound assignments are unsequenced in C, so we might
1328   //   have to figure out how to reject them.
1329   if (!visit(RHS))
1330     return false;
1331 
1332   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
1333 
1334   if (!this->emitSetLocal(*RT, TempOffset, E))
1335     return false;
1336 
1337   // Get LHS pointer, load its value and cast it to the
1338   // computation type if necessary.
1339   if (!visit(LHS))
1340     return false;
1341   if (!this->emitLoad(*LT, E))
1342     return false;
1343   if (*LT != *LHSComputationT) {
1344     if (!this->emitCast(*LT, *LHSComputationT, E))
1345       return false;
1346   }
1347 
1348   // Get the RHS value on the stack.
1349   if (!this->emitGetLocal(*RT, TempOffset, E))
1350     return false;
1351 
1352   // Perform operation.
1353   switch (E->getOpcode()) {
1354   case BO_AddAssign:
1355     if (!this->emitAdd(*LHSComputationT, E))
1356       return false;
1357     break;
1358   case BO_SubAssign:
1359     if (!this->emitSub(*LHSComputationT, E))
1360       return false;
1361     break;
1362   case BO_MulAssign:
1363     if (!this->emitMul(*LHSComputationT, E))
1364       return false;
1365     break;
1366   case BO_DivAssign:
1367     if (!this->emitDiv(*LHSComputationT, E))
1368       return false;
1369     break;
1370   case BO_RemAssign:
1371     if (!this->emitRem(*LHSComputationT, E))
1372       return false;
1373     break;
1374   case BO_ShlAssign:
1375     if (!this->emitShl(*LHSComputationT, *RT, E))
1376       return false;
1377     break;
1378   case BO_ShrAssign:
1379     if (!this->emitShr(*LHSComputationT, *RT, E))
1380       return false;
1381     break;
1382   case BO_AndAssign:
1383     if (!this->emitBitAnd(*LHSComputationT, E))
1384       return false;
1385     break;
1386   case BO_XorAssign:
1387     if (!this->emitBitXor(*LHSComputationT, E))
1388       return false;
1389     break;
1390   case BO_OrAssign:
1391     if (!this->emitBitOr(*LHSComputationT, E))
1392       return false;
1393     break;
1394   default:
1395     llvm_unreachable("Unimplemented compound assign operator");
1396   }
1397 
1398   // And now cast from LHSComputationT to ResultT.
1399   if (*ResultT != *LHSComputationT) {
1400     if (!this->emitCast(*LHSComputationT, *ResultT, E))
1401       return false;
1402   }
1403 
1404   // And store the result in LHS.
1405   if (DiscardResult) {
1406     if (LHS->refersToBitField())
1407       return this->emitStoreBitFieldPop(*ResultT, E);
1408     return this->emitStorePop(*ResultT, E);
1409   }
1410   if (LHS->refersToBitField())
1411     return this->emitStoreBitField(*ResultT, E);
1412   return this->emitStore(*ResultT, E);
1413 }
1414 
1415 template <class Emitter>
1416 bool ByteCodeExprGen<Emitter>::VisitExprWithCleanups(
1417     const ExprWithCleanups *E) {
1418   const Expr *SubExpr = E->getSubExpr();
1419 
1420   assert(E->getNumObjects() == 0 && "TODO: Implement cleanups");
1421 
1422   return this->delegate(SubExpr);
1423 }
1424 
1425 template <class Emitter>
1426 bool ByteCodeExprGen<Emitter>::VisitMaterializeTemporaryExpr(
1427     const MaterializeTemporaryExpr *E) {
1428   const Expr *SubExpr = E->getSubExpr();
1429 
1430   if (Initializing) {
1431     // We already have a value, just initialize that.
1432     return this->visitInitializer(SubExpr);
1433   }
1434   // If we don't end up using the materialized temporary anyway, don't
1435   // bother creating it.
1436   if (DiscardResult)
1437     return this->discard(SubExpr);
1438 
1439   // When we're initializing a global variable *or* the storage duration of
1440   // the temporary is explicitly static, create a global variable.
1441   std::optional<PrimType> SubExprT = classify(SubExpr);
1442   bool IsStatic = E->getStorageDuration() == SD_Static;
1443   if (GlobalDecl || IsStatic) {
1444     std::optional<unsigned> GlobalIndex = P.createGlobal(E);
1445     if (!GlobalIndex)
1446       return false;
1447 
1448     const LifetimeExtendedTemporaryDecl *TempDecl =
1449         E->getLifetimeExtendedTemporaryDecl();
1450     if (IsStatic)
1451       assert(TempDecl);
1452 
1453     if (SubExprT) {
1454       if (!this->visit(SubExpr))
1455         return false;
1456       if (IsStatic) {
1457         if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
1458           return false;
1459       } else {
1460         if (!this->emitInitGlobal(*SubExprT, *GlobalIndex, E))
1461           return false;
1462       }
1463       return this->emitGetPtrGlobal(*GlobalIndex, E);
1464     }
1465 
1466     // Non-primitive values.
1467     if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1468       return false;
1469     if (!this->visitInitializer(SubExpr))
1470       return false;
1471     if (IsStatic)
1472       return this->emitInitGlobalTempComp(TempDecl, E);
1473     return true;
1474   }
1475 
1476   // For everyhing else, use local variables.
1477   if (SubExprT) {
1478     if (std::optional<unsigned> LocalIndex = allocateLocalPrimitive(
1479             SubExpr, *SubExprT, /*IsConst=*/true, /*IsExtended=*/true)) {
1480       if (!this->visit(SubExpr))
1481         return false;
1482       this->emitSetLocal(*SubExprT, *LocalIndex, E);
1483       return this->emitGetPtrLocal(*LocalIndex, E);
1484     }
1485   } else {
1486     if (std::optional<unsigned> LocalIndex =
1487             allocateLocal(SubExpr, /*IsExtended=*/true)) {
1488       if (!this->emitGetPtrLocal(*LocalIndex, E))
1489         return false;
1490       return this->visitInitializer(SubExpr);
1491     }
1492   }
1493   return false;
1494 }
1495 
1496 template <class Emitter>
1497 bool ByteCodeExprGen<Emitter>::VisitCXXBindTemporaryExpr(
1498     const CXXBindTemporaryExpr *E) {
1499   return this->delegate(E->getSubExpr());
1500 }
1501 
1502 template <class Emitter>
1503 bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(
1504     const CompoundLiteralExpr *E) {
1505   const Expr *Init = E->getInitializer();
1506   if (Initializing) {
1507     // We already have a value, just initialize that.
1508     return this->visitInitializer(Init);
1509   }
1510 
1511   std::optional<PrimType> T = classify(E->getType());
1512   if (E->isFileScope()) {
1513     if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
1514       if (classify(E->getType()))
1515         return this->visit(Init);
1516       if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1517         return false;
1518       return this->visitInitializer(Init);
1519     }
1520   }
1521 
1522   // Otherwise, use a local variable.
1523   if (T) {
1524     // For primitive types, we just visit the initializer.
1525     return this->delegate(Init);
1526   } else {
1527     if (std::optional<unsigned> LocalIndex = allocateLocal(Init)) {
1528       if (!this->emitGetPtrLocal(*LocalIndex, E))
1529         return false;
1530       if (!this->visitInitializer(Init))
1531         return false;
1532       if (DiscardResult)
1533         return this->emitPopPtr(E);
1534       return true;
1535     }
1536   }
1537 
1538   return false;
1539 }
1540 
1541 template <class Emitter>
1542 bool ByteCodeExprGen<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) {
1543   if (DiscardResult)
1544     return true;
1545   return this->emitConstBool(E->getValue(), E);
1546 }
1547 
1548 template <class Emitter>
1549 bool ByteCodeExprGen<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
1550   assert(Initializing);
1551   const Record *R = P.getOrCreateRecord(E->getLambdaClass());
1552 
1553   auto *CaptureInitIt = E->capture_init_begin();
1554   // Initialize all fields (which represent lambda captures) of the
1555   // record with their initializers.
1556   for (const Record::Field &F : R->fields()) {
1557     const Expr *Init = *CaptureInitIt;
1558     ++CaptureInitIt;
1559 
1560     if (std::optional<PrimType> T = classify(Init)) {
1561       if (!this->visit(Init))
1562         return false;
1563 
1564       if (!this->emitSetField(*T, F.Offset, E))
1565         return false;
1566     } else {
1567       if (!this->emitDupPtr(E))
1568         return false;
1569 
1570       if (!this->emitGetPtrField(F.Offset, E))
1571         return false;
1572 
1573       if (!this->visitInitializer(Init))
1574         return false;
1575 
1576       if (!this->emitPopPtr(E))
1577         return false;
1578     }
1579   }
1580 
1581   return true;
1582 }
1583 
1584 template <class Emitter>
1585 bool ByteCodeExprGen<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
1586   if (DiscardResult)
1587     return true;
1588 
1589   assert(!Initializing);
1590   return this->visit(E->getFunctionName());
1591 }
1592 
1593 template <class Emitter>
1594 bool ByteCodeExprGen<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
1595   if (E->getSubExpr() && !this->discard(E->getSubExpr()))
1596     return false;
1597 
1598   return this->emitInvalid(E);
1599 }
1600 
1601 template <class Emitter>
1602 bool ByteCodeExprGen<Emitter>::VisitCXXReinterpretCastExpr(
1603     const CXXReinterpretCastExpr *E) {
1604   if (!this->discard(E->getSubExpr()))
1605     return false;
1606 
1607   return this->emitInvalidCast(CastKind::Reinterpret, E);
1608 }
1609 
1610 template <class Emitter>
1611 bool ByteCodeExprGen<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1612   assert(E->getType()->isBooleanType());
1613 
1614   if (DiscardResult)
1615     return true;
1616   return this->emitConstBool(E->getValue(), E);
1617 }
1618 
1619 template <class Emitter>
1620 bool ByteCodeExprGen<Emitter>::VisitCXXConstructExpr(
1621     const CXXConstructExpr *E) {
1622   QualType T = E->getType();
1623   assert(!classify(T));
1624 
1625   if (T->isRecordType()) {
1626     const CXXConstructorDecl *Ctor = E->getConstructor();
1627 
1628     // Trivial zero initialization.
1629     if (E->requiresZeroInitialization() && Ctor->isTrivial()) {
1630       const Record *R = getRecord(E->getType());
1631       return this->visitZeroRecordInitializer(R, E);
1632     }
1633 
1634     const Function *Func = getFunction(Ctor);
1635 
1636     if (!Func)
1637       return false;
1638 
1639     assert(Func->hasThisPointer());
1640     assert(!Func->hasRVO());
1641 
1642     // If we're discarding a construct expression, we still need
1643     // to allocate a variable and call the constructor and destructor.
1644     if (DiscardResult) {
1645       assert(!Initializing);
1646       std::optional<unsigned> LocalIndex =
1647           allocateLocal(E, /*IsExtended=*/true);
1648 
1649       if (!LocalIndex)
1650         return false;
1651 
1652       if (!this->emitGetPtrLocal(*LocalIndex, E))
1653         return false;
1654     }
1655 
1656     //  The This pointer is already on the stack because this is an initializer,
1657     //  but we need to dup() so the call() below has its own copy.
1658     if (!this->emitDupPtr(E))
1659       return false;
1660 
1661     // Constructor arguments.
1662     for (const auto *Arg : E->arguments()) {
1663       if (!this->visit(Arg))
1664         return false;
1665     }
1666 
1667     if (!this->emitCall(Func, E))
1668       return false;
1669 
1670     // Immediately call the destructor if we have to.
1671     if (DiscardResult) {
1672       if (!this->emitPopPtr(E))
1673         return false;
1674     }
1675     return true;
1676   }
1677 
1678   if (T->isArrayType()) {
1679     const ConstantArrayType *CAT =
1680         Ctx.getASTContext().getAsConstantArrayType(E->getType());
1681     assert(CAT);
1682     size_t NumElems = CAT->getSize().getZExtValue();
1683     const Function *Func = getFunction(E->getConstructor());
1684     if (!Func || !Func->isConstexpr())
1685       return false;
1686 
1687     // FIXME(perf): We're calling the constructor once per array element here,
1688     //   in the old intepreter we had a special-case for trivial constructors.
1689     for (size_t I = 0; I != NumElems; ++I) {
1690       if (!this->emitConstUint64(I, E))
1691         return false;
1692       if (!this->emitArrayElemPtrUint64(E))
1693         return false;
1694 
1695       // Constructor arguments.
1696       for (const auto *Arg : E->arguments()) {
1697         if (!this->visit(Arg))
1698           return false;
1699       }
1700 
1701       if (!this->emitCall(Func, E))
1702         return false;
1703     }
1704     return true;
1705   }
1706 
1707   return false;
1708 }
1709 
1710 template <class Emitter>
1711 bool ByteCodeExprGen<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
1712   if (DiscardResult)
1713     return true;
1714 
1715   const APValue Val =
1716       E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
1717 
1718   // Things like __builtin_LINE().
1719   if (E->getType()->isIntegerType()) {
1720     assert(Val.isInt());
1721     const APSInt &I = Val.getInt();
1722     return this->emitConst(I, E);
1723   }
1724   // Otherwise, the APValue is an LValue, with only one element.
1725   // Theoretically, we don't need the APValue at all of course.
1726   assert(E->getType()->isPointerType());
1727   assert(Val.isLValue());
1728   const APValue::LValueBase &Base = Val.getLValueBase();
1729   if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
1730     return this->visit(LValueExpr);
1731 
1732   // Otherwise, we have a decl (which is the case for
1733   // __builtin_source_location).
1734   assert(Base.is<const ValueDecl *>());
1735   assert(Val.getLValuePath().size() == 0);
1736   const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
1737   assert(BaseDecl);
1738 
1739   auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
1740 
1741   std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(UGCD);
1742   if (!GlobalIndex)
1743     return false;
1744 
1745   if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1746     return false;
1747 
1748   const Record *R = getRecord(E->getType());
1749   const APValue &V = UGCD->getValue();
1750   for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
1751     const Record::Field *F = R->getField(I);
1752     const APValue &FieldValue = V.getStructField(I);
1753 
1754     PrimType FieldT = classifyPrim(F->Decl->getType());
1755 
1756     if (!this->visitAPValue(FieldValue, FieldT, E))
1757       return false;
1758     if (!this->emitInitField(FieldT, F->Offset, E))
1759       return false;
1760   }
1761 
1762   // Leave the pointer to the global on the stack.
1763   return true;
1764 }
1765 
1766 template <class Emitter>
1767 bool ByteCodeExprGen<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1768   unsigned N = E->getNumComponents();
1769   if (N == 0)
1770     return false;
1771 
1772   for (unsigned I = 0; I != N; ++I) {
1773     const OffsetOfNode &Node = E->getComponent(I);
1774     if (Node.getKind() == OffsetOfNode::Array) {
1775       const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
1776       PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
1777 
1778       if (DiscardResult) {
1779         if (!this->discard(ArrayIndexExpr))
1780           return false;
1781         continue;
1782       }
1783 
1784       if (!this->visit(ArrayIndexExpr))
1785         return false;
1786       // Cast to Sint64.
1787       if (IndexT != PT_Sint64) {
1788         if (!this->emitCast(IndexT, PT_Sint64, E))
1789           return false;
1790       }
1791     }
1792   }
1793 
1794   if (DiscardResult)
1795     return true;
1796 
1797   PrimType T = classifyPrim(E->getType());
1798   return this->emitOffsetOf(T, E, E);
1799 }
1800 
1801 template <class Emitter>
1802 bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
1803     const CXXScalarValueInitExpr *E) {
1804   QualType Ty = E->getType();
1805 
1806   if (Ty->isVoidType())
1807     return true;
1808 
1809   return this->visitZeroInitializer(classifyPrim(Ty), Ty, E);
1810 }
1811 
1812 template <class Emitter>
1813 bool ByteCodeExprGen<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1814   return this->emitConst(E->getPackLength(), E);
1815 }
1816 
1817 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
1818   if (E->containsErrors())
1819     return false;
1820 
1821   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
1822                              /*NewInitializing=*/false);
1823   return this->Visit(E);
1824 }
1825 
1826 template <class Emitter>
1827 bool ByteCodeExprGen<Emitter>::delegate(const Expr *E) {
1828   if (E->containsErrors())
1829     return false;
1830 
1831   // We're basically doing:
1832   // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
1833   // but that's unnecessary of course.
1834   return this->Visit(E);
1835 }
1836 
1837 template <class Emitter> bool ByteCodeExprGen<Emitter>::visit(const Expr *E) {
1838   if (E->containsErrors())
1839     return false;
1840 
1841   if (E->getType()->isVoidType())
1842     return this->discard(E);
1843 
1844   // Create local variable to hold the return value.
1845   if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
1846       !classify(E->getType())) {
1847     std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/true);
1848     if (!LocalIndex)
1849       return false;
1850 
1851     if (!this->emitGetPtrLocal(*LocalIndex, E))
1852       return false;
1853     return this->visitInitializer(E);
1854   }
1855 
1856   //  Otherwise,we have a primitive return value, produce the value directly
1857   //  and push it on the stack.
1858   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
1859                              /*NewInitializing=*/false);
1860   return this->Visit(E);
1861 }
1862 
1863 template <class Emitter>
1864 bool ByteCodeExprGen<Emitter>::visitInitializer(const Expr *E) {
1865   assert(!classify(E->getType()));
1866 
1867   if (E->containsErrors())
1868     return false;
1869 
1870   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
1871                              /*NewInitializing=*/true);
1872   return this->Visit(E);
1873 }
1874 
1875 template <class Emitter>
1876 bool ByteCodeExprGen<Emitter>::visitBool(const Expr *E) {
1877   std::optional<PrimType> T = classify(E->getType());
1878   if (!T)
1879     return false;
1880 
1881   if (!this->visit(E))
1882     return false;
1883 
1884   if (T == PT_Bool)
1885     return true;
1886 
1887   // Convert pointers to bool.
1888   if (T == PT_Ptr || T == PT_FnPtr) {
1889     if (!this->emitNull(*T, E))
1890       return false;
1891     return this->emitNE(*T, E);
1892   }
1893 
1894   // Or Floats.
1895   if (T == PT_Float)
1896     return this->emitCastFloatingIntegralBool(E);
1897 
1898   // Or anything else we can.
1899   return this->emitCast(*T, PT_Bool, E);
1900 }
1901 
1902 template <class Emitter>
1903 bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
1904                                                     const Expr *E) {
1905   switch (T) {
1906   case PT_Bool:
1907     return this->emitZeroBool(E);
1908   case PT_Sint8:
1909     return this->emitZeroSint8(E);
1910   case PT_Uint8:
1911     return this->emitZeroUint8(E);
1912   case PT_Sint16:
1913     return this->emitZeroSint16(E);
1914   case PT_Uint16:
1915     return this->emitZeroUint16(E);
1916   case PT_Sint32:
1917     return this->emitZeroSint32(E);
1918   case PT_Uint32:
1919     return this->emitZeroUint32(E);
1920   case PT_Sint64:
1921     return this->emitZeroSint64(E);
1922   case PT_Uint64:
1923     return this->emitZeroUint64(E);
1924   case PT_IntAP:
1925     return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
1926   case PT_IntAPS:
1927     return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
1928   case PT_Ptr:
1929     return this->emitNullPtr(E);
1930   case PT_FnPtr:
1931     return this->emitNullFnPtr(E);
1932   case PT_Float: {
1933     return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
1934   }
1935   }
1936   llvm_unreachable("unknown primitive type");
1937 }
1938 
1939 template <class Emitter>
1940 bool ByteCodeExprGen<Emitter>::visitZeroRecordInitializer(const Record *R,
1941                                                           const Expr *E) {
1942   assert(E);
1943   assert(R);
1944   // Fields
1945   for (const Record::Field &Field : R->fields()) {
1946     const Descriptor *D = Field.Desc;
1947     if (D->isPrimitive()) {
1948       QualType QT = D->getType();
1949       PrimType T = classifyPrim(D->getType());
1950       if (!this->visitZeroInitializer(T, QT, E))
1951         return false;
1952       if (!this->emitInitField(T, Field.Offset, E))
1953         return false;
1954       continue;
1955     }
1956 
1957     // TODO: Add GetPtrFieldPop and get rid of this dup.
1958     if (!this->emitDupPtr(E))
1959       return false;
1960     if (!this->emitGetPtrField(Field.Offset, E))
1961       return false;
1962 
1963     if (D->isPrimitiveArray()) {
1964       QualType ET = D->getElemQualType();
1965       PrimType T = classifyPrim(ET);
1966       for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
1967         if (!this->visitZeroInitializer(T, ET, E))
1968           return false;
1969         if (!this->emitInitElem(T, I, E))
1970           return false;
1971       }
1972     } else if (D->isCompositeArray()) {
1973       const Record *ElemRecord = D->ElemDesc->ElemRecord;
1974       assert(D->ElemDesc->ElemRecord);
1975       for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
1976         if (!this->emitConstUint32(I, E))
1977           return false;
1978         if (!this->emitArrayElemPtr(PT_Uint32, E))
1979           return false;
1980         if (!this->visitZeroRecordInitializer(ElemRecord, E))
1981           return false;
1982         if (!this->emitPopPtr(E))
1983           return false;
1984       }
1985     } else if (D->isRecord()) {
1986       if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
1987         return false;
1988     } else {
1989       assert(false);
1990     }
1991 
1992     if (!this->emitPopPtr(E))
1993       return false;
1994   }
1995 
1996   for (const Record::Base &B : R->bases()) {
1997     if (!this->emitGetPtrBase(B.Offset, E))
1998       return false;
1999     if (!this->visitZeroRecordInitializer(B.R, E))
2000       return false;
2001     if (!this->emitInitPtrPop(E))
2002       return false;
2003   }
2004 
2005   // FIXME: Virtual bases.
2006 
2007   return true;
2008 }
2009 
2010 template <class Emitter>
2011 bool ByteCodeExprGen<Emitter>::dereference(
2012     const Expr *LV, DerefKind AK, llvm::function_ref<bool(PrimType)> Direct,
2013     llvm::function_ref<bool(PrimType)> Indirect) {
2014   if (std::optional<PrimType> T = classify(LV->getType())) {
2015     if (!LV->refersToBitField()) {
2016       // Only primitive, non bit-field types can be dereferenced directly.
2017       if (const auto *DE = dyn_cast<DeclRefExpr>(LV)) {
2018         if (!DE->getDecl()->getType()->isReferenceType()) {
2019           if (const auto *PD = dyn_cast<ParmVarDecl>(DE->getDecl()))
2020             return dereferenceParam(LV, *T, PD, AK, Direct, Indirect);
2021           if (const auto *VD = dyn_cast<VarDecl>(DE->getDecl()))
2022             return dereferenceVar(LV, *T, VD, AK, Direct, Indirect);
2023         }
2024       }
2025     }
2026 
2027     if (!visit(LV))
2028       return false;
2029     return Indirect(*T);
2030   }
2031 
2032   if (LV->getType()->isAnyComplexType())
2033     return visit(LV);
2034 
2035   return false;
2036 }
2037 
2038 template <class Emitter>
2039 bool ByteCodeExprGen<Emitter>::dereferenceParam(
2040     const Expr *LV, PrimType T, const ParmVarDecl *PD, DerefKind AK,
2041     llvm::function_ref<bool(PrimType)> Direct,
2042     llvm::function_ref<bool(PrimType)> Indirect) {
2043   auto It = this->Params.find(PD);
2044   if (It != this->Params.end()) {
2045     unsigned Idx = It->second.Offset;
2046     switch (AK) {
2047     case DerefKind::Read:
2048       return DiscardResult ? true : this->emitGetParam(T, Idx, LV);
2049 
2050     case DerefKind::Write:
2051       if (!Direct(T))
2052         return false;
2053       if (!this->emitSetParam(T, Idx, LV))
2054         return false;
2055       return DiscardResult ? true : this->emitGetPtrParam(Idx, LV);
2056 
2057     case DerefKind::ReadWrite:
2058       if (!this->emitGetParam(T, Idx, LV))
2059         return false;
2060       if (!Direct(T))
2061         return false;
2062       if (!this->emitSetParam(T, Idx, LV))
2063         return false;
2064       return DiscardResult ? true : this->emitGetPtrParam(Idx, LV);
2065     }
2066     return true;
2067   }
2068 
2069   // If the param is a pointer, we can dereference a dummy value.
2070   if (!DiscardResult && T == PT_Ptr && AK == DerefKind::Read) {
2071     if (auto Idx = P.getOrCreateDummy(PD))
2072       return this->emitGetPtrGlobal(*Idx, PD);
2073     return false;
2074   }
2075 
2076   // Value cannot be produced - try to emit pointer and do stuff with it.
2077   return visit(LV) && Indirect(T);
2078 }
2079 
2080 template <class Emitter>
2081 bool ByteCodeExprGen<Emitter>::dereferenceVar(
2082     const Expr *LV, PrimType T, const VarDecl *VD, DerefKind AK,
2083     llvm::function_ref<bool(PrimType)> Direct,
2084     llvm::function_ref<bool(PrimType)> Indirect) {
2085   auto It = Locals.find(VD);
2086   if (It != Locals.end()) {
2087     const auto &L = It->second;
2088     switch (AK) {
2089     case DerefKind::Read:
2090       if (!this->emitGetLocal(T, L.Offset, LV))
2091         return false;
2092       return DiscardResult ? this->emitPop(T, LV) : true;
2093 
2094     case DerefKind::Write:
2095       if (!Direct(T))
2096         return false;
2097       if (!this->emitSetLocal(T, L.Offset, LV))
2098         return false;
2099       return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV);
2100 
2101     case DerefKind::ReadWrite:
2102       if (!this->emitGetLocal(T, L.Offset, LV))
2103         return false;
2104       if (!Direct(T))
2105         return false;
2106       if (!this->emitSetLocal(T, L.Offset, LV))
2107         return false;
2108       return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV);
2109     }
2110   } else if (auto Idx = P.getGlobal(VD)) {
2111     switch (AK) {
2112     case DerefKind::Read:
2113       if (!this->emitGetGlobal(T, *Idx, LV))
2114         return false;
2115       return DiscardResult ? this->emitPop(T, LV) : true;
2116 
2117     case DerefKind::Write:
2118       if (!Direct(T))
2119         return false;
2120       if (!this->emitSetGlobal(T, *Idx, LV))
2121         return false;
2122       return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV);
2123 
2124     case DerefKind::ReadWrite:
2125       if (!this->emitGetGlobal(T, *Idx, LV))
2126         return false;
2127       if (!Direct(T))
2128         return false;
2129       if (!this->emitSetGlobal(T, *Idx, LV))
2130         return false;
2131       return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV);
2132     }
2133   }
2134 
2135   // If the declaration is a constant value, emit it here even
2136   // though the declaration was not evaluated in the current scope.
2137   // The access mode can only be read in this case.
2138   if (!DiscardResult && AK == DerefKind::Read) {
2139     if (VD->hasLocalStorage() && VD->hasInit() && !VD->isConstexpr()) {
2140       QualType VT = VD->getType();
2141       if (VT.isConstQualified() && VT->isFundamentalType())
2142         return this->visit(VD->getInit());
2143     }
2144   }
2145 
2146   // Value cannot be produced - try to emit pointer.
2147   return visit(LV) && Indirect(T);
2148 }
2149 
2150 template <class Emitter>
2151 template <typename T>
2152 bool ByteCodeExprGen<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
2153   switch (Ty) {
2154   case PT_Sint8:
2155     return this->emitConstSint8(Value, E);
2156   case PT_Uint8:
2157     return this->emitConstUint8(Value, E);
2158   case PT_Sint16:
2159     return this->emitConstSint16(Value, E);
2160   case PT_Uint16:
2161     return this->emitConstUint16(Value, E);
2162   case PT_Sint32:
2163     return this->emitConstSint32(Value, E);
2164   case PT_Uint32:
2165     return this->emitConstUint32(Value, E);
2166   case PT_Sint64:
2167     return this->emitConstSint64(Value, E);
2168   case PT_Uint64:
2169     return this->emitConstUint64(Value, E);
2170   case PT_IntAP:
2171   case PT_IntAPS:
2172     assert(false);
2173     return false;
2174   case PT_Bool:
2175     return this->emitConstBool(Value, E);
2176   case PT_Ptr:
2177   case PT_FnPtr:
2178   case PT_Float:
2179     llvm_unreachable("Invalid integral type");
2180     break;
2181   }
2182   llvm_unreachable("unknown primitive type");
2183 }
2184 
2185 template <class Emitter>
2186 template <typename T>
2187 bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
2188   return this->emitConst(Value, classifyPrim(E->getType()), E);
2189 }
2190 
2191 template <class Emitter>
2192 bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
2193                                          const Expr *E) {
2194   if (Value.isSigned())
2195     return this->emitConst(Value.getSExtValue(), Ty, E);
2196   return this->emitConst(Value.getZExtValue(), Ty, E);
2197 }
2198 
2199 template <class Emitter>
2200 bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
2201   return this->emitConst(Value, classifyPrim(E->getType()), E);
2202 }
2203 
2204 template <class Emitter>
2205 unsigned ByteCodeExprGen<Emitter>::allocateLocalPrimitive(DeclTy &&Src,
2206                                                           PrimType Ty,
2207                                                           bool IsConst,
2208                                                           bool IsExtended) {
2209   // Make sure we don't accidentally register the same decl twice.
2210   if (const auto *VD =
2211           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2212     assert(!P.getGlobal(VD));
2213     assert(!Locals.contains(VD));
2214   }
2215 
2216   // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
2217   //   (int){12} in C. Consider using Expr::isTemporaryObject() instead
2218   //   or isa<MaterializeTemporaryExpr>().
2219   Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst,
2220                                      Src.is<const Expr *>());
2221   Scope::Local Local = this->createLocal(D);
2222   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
2223     Locals.insert({VD, Local});
2224   VarScope->add(Local, IsExtended);
2225   return Local.Offset;
2226 }
2227 
2228 template <class Emitter>
2229 std::optional<unsigned>
2230 ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src, bool IsExtended) {
2231   // Make sure we don't accidentally register the same decl twice.
2232   if ([[maybe_unused]]  const auto *VD =
2233           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2234     assert(!P.getGlobal(VD));
2235     assert(!Locals.contains(VD));
2236   }
2237 
2238   QualType Ty;
2239   const ValueDecl *Key = nullptr;
2240   const Expr *Init = nullptr;
2241   bool IsTemporary = false;
2242   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2243     Key = VD;
2244     Ty = VD->getType();
2245 
2246     if (const auto *VarD = dyn_cast<VarDecl>(VD))
2247       Init = VarD->getInit();
2248   }
2249   if (auto *E = Src.dyn_cast<const Expr *>()) {
2250     IsTemporary = true;
2251     Ty = E->getType();
2252   }
2253 
2254   Descriptor *D = P.createDescriptor(
2255       Src, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
2256       IsTemporary, /*IsMutable=*/false, Init);
2257   if (!D)
2258     return {};
2259 
2260   Scope::Local Local = this->createLocal(D);
2261   if (Key)
2262     Locals.insert({Key, Local});
2263   VarScope->add(Local, IsExtended);
2264   return Local.Offset;
2265 }
2266 
2267 template <class Emitter>
2268 const RecordType *ByteCodeExprGen<Emitter>::getRecordTy(QualType Ty) {
2269   if (const PointerType *PT = dyn_cast<PointerType>(Ty))
2270     return PT->getPointeeType()->getAs<RecordType>();
2271   return Ty->getAs<RecordType>();
2272 }
2273 
2274 template <class Emitter>
2275 Record *ByteCodeExprGen<Emitter>::getRecord(QualType Ty) {
2276   if (const auto *RecordTy = getRecordTy(Ty))
2277     return getRecord(RecordTy->getDecl());
2278   return nullptr;
2279 }
2280 
2281 template <class Emitter>
2282 Record *ByteCodeExprGen<Emitter>::getRecord(const RecordDecl *RD) {
2283   return P.getOrCreateRecord(RD);
2284 }
2285 
2286 template <class Emitter>
2287 const Function *ByteCodeExprGen<Emitter>::getFunction(const FunctionDecl *FD) {
2288   return Ctx.getOrCreateFunction(FD);
2289 }
2290 
2291 template <class Emitter>
2292 bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *E) {
2293   ExprScope<Emitter> RootScope(this);
2294   // Void expressions.
2295   if (E->getType()->isVoidType()) {
2296     if (!visit(E))
2297       return false;
2298     return this->emitRetVoid(E);
2299   }
2300 
2301   // Expressions with a primitive return type.
2302   if (std::optional<PrimType> T = classify(E)) {
2303     if (!visit(E))
2304       return false;
2305     return this->emitRet(*T, E);
2306   }
2307 
2308   // Expressions with a composite return type.
2309   // For us, that means everything we don't
2310   // have a PrimType for.
2311   if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
2312     if (!this->visitLocalInitializer(E, *LocalOffset))
2313       return false;
2314 
2315     if (!this->emitGetPtrLocal(*LocalOffset, E))
2316       return false;
2317     return this->emitRetValue(E);
2318   }
2319 
2320   return false;
2321 }
2322 
2323 /// Toplevel visitDecl().
2324 /// We get here from evaluateAsInitializer().
2325 /// We need to evaluate the initializer and return its value.
2326 template <class Emitter>
2327 bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) {
2328   assert(!VD->isInvalidDecl() && "Trying to constant evaluate an invalid decl");
2329 
2330   // Create and initialize the variable.
2331   if (!this->visitVarDecl(VD))
2332     return false;
2333 
2334   std::optional<PrimType> VarT = classify(VD->getType());
2335   // Get a pointer to the variable
2336   if (Context::shouldBeGloballyIndexed(VD)) {
2337     auto GlobalIndex = P.getGlobal(VD);
2338     assert(GlobalIndex); // visitVarDecl() didn't return false.
2339     if (VarT) {
2340       if (!this->emitGetGlobal(*VarT, *GlobalIndex, VD))
2341         return false;
2342     } else {
2343       if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
2344         return false;
2345     }
2346   } else {
2347     auto Local = Locals.find(VD);
2348     assert(Local != Locals.end()); // Same here.
2349     if (VarT) {
2350       if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
2351         return false;
2352     } else {
2353       if (!this->emitGetPtrLocal(Local->second.Offset, VD))
2354         return false;
2355     }
2356   }
2357 
2358   // Return the value
2359   if (VarT)
2360     return this->emitRet(*VarT, VD);
2361   return this->emitRetValue(VD);
2362 }
2363 
2364 template <class Emitter>
2365 bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
2366   // We don't know what to do with these, so just return false.
2367   if (VD->getType().isNull())
2368     return false;
2369 
2370   const Expr *Init = VD->getInit();
2371   std::optional<PrimType> VarT = classify(VD->getType());
2372 
2373   if (Context::shouldBeGloballyIndexed(VD)) {
2374     // We've already seen and initialized this global.
2375     if (P.getGlobal(VD))
2376       return true;
2377 
2378     std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
2379 
2380     if (!GlobalIndex)
2381       return this->bail(VD);
2382 
2383     assert(Init);
2384     {
2385       DeclScope<Emitter> LocalScope(this, VD);
2386 
2387       if (VarT) {
2388         if (!this->visit(Init))
2389           return false;
2390         return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
2391       }
2392       return this->visitGlobalInitializer(Init, *GlobalIndex);
2393     }
2394   } else {
2395     VariableScope<Emitter> LocalScope(this);
2396     if (VarT) {
2397       unsigned Offset = this->allocateLocalPrimitive(
2398           VD, *VarT, VD->getType().isConstQualified());
2399       if (Init) {
2400         // Compile the initializer in its own scope.
2401         ExprScope<Emitter> Scope(this);
2402         if (!this->visit(Init))
2403           return false;
2404 
2405         return this->emitSetLocal(*VarT, Offset, VD);
2406       }
2407     } else {
2408       if (std::optional<unsigned> Offset = this->allocateLocal(VD)) {
2409         if (Init)
2410           return this->visitLocalInitializer(Init, *Offset);
2411       }
2412     }
2413     return true;
2414   }
2415 
2416   return false;
2417 }
2418 
2419 template <class Emitter>
2420 bool ByteCodeExprGen<Emitter>::visitAPValue(const APValue &Val,
2421                                             PrimType ValType, const Expr *E) {
2422   assert(!DiscardResult);
2423   if (Val.isInt())
2424     return this->emitConst(Val.getInt(), ValType, E);
2425 
2426   if (Val.isLValue()) {
2427     APValue::LValueBase Base = Val.getLValueBase();
2428     if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
2429       return this->visit(BaseExpr);
2430   }
2431 
2432   return false;
2433 }
2434 
2435 template <class Emitter>
2436 bool ByteCodeExprGen<Emitter>::VisitBuiltinCallExpr(const CallExpr *E) {
2437   const Function *Func = getFunction(E->getDirectCallee());
2438   if (!Func)
2439     return false;
2440 
2441   if (!Func->isUnevaluatedBuiltin()) {
2442     // Put arguments on the stack.
2443     for (const auto *Arg : E->arguments()) {
2444       if (!this->visit(Arg))
2445         return false;
2446     }
2447   }
2448 
2449   if (!this->emitCallBI(Func, E, E))
2450     return false;
2451 
2452   QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
2453   if (DiscardResult && !ReturnType->isVoidType()) {
2454     PrimType T = classifyPrim(ReturnType);
2455     return this->emitPop(T, E);
2456   }
2457 
2458   return true;
2459 }
2460 
2461 template <class Emitter>
2462 bool ByteCodeExprGen<Emitter>::VisitCallExpr(const CallExpr *E) {
2463   if (E->getBuiltinCallee())
2464     return VisitBuiltinCallExpr(E);
2465 
2466   QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
2467   std::optional<PrimType> T = classify(ReturnType);
2468   bool HasRVO = !ReturnType->isVoidType() && !T;
2469 
2470   if (HasRVO) {
2471     if (DiscardResult) {
2472       // If we need to discard the return value but the function returns its
2473       // value via an RVO pointer, we need to create one such pointer just
2474       // for this call.
2475       if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
2476         if (!this->emitGetPtrLocal(*LocalIndex, E))
2477           return false;
2478       }
2479     } else {
2480       assert(Initializing);
2481       if (!this->emitDupPtr(E))
2482         return false;
2483     }
2484   }
2485 
2486   // Add the (optional, implicit) This pointer.
2487   if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
2488     if (!this->visit(MC->getImplicitObjectArgument()))
2489       return false;
2490   }
2491 
2492   // Put arguments on the stack.
2493   for (const auto *Arg : E->arguments()) {
2494     if (!this->visit(Arg))
2495       return false;
2496   }
2497 
2498   if (const FunctionDecl *FuncDecl = E->getDirectCallee()) {
2499     const Function *Func = getFunction(FuncDecl);
2500     if (!Func)
2501       return false;
2502     // If the function is being compiled right now, this is a recursive call.
2503     // In that case, the function can't be valid yet, even though it will be
2504     // later.
2505     // If the function is already fully compiled but not constexpr, it was
2506     // found to be faulty earlier on, so bail out.
2507     if (Func->isFullyCompiled() && !Func->isConstexpr())
2508       return false;
2509 
2510     assert(HasRVO == Func->hasRVO());
2511 
2512     bool HasQualifier = false;
2513     if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
2514       HasQualifier = ME->hasQualifier();
2515 
2516     bool IsVirtual = false;
2517     if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
2518       IsVirtual = MD->isVirtual();
2519 
2520     // In any case call the function. The return value will end up on the stack
2521     // and if the function has RVO, we already have the pointer on the stack to
2522     // write the result into.
2523     if (IsVirtual && !HasQualifier) {
2524       if (!this->emitCallVirt(Func, E))
2525         return false;
2526     } else {
2527       if (!this->emitCall(Func, E))
2528         return false;
2529     }
2530   } else {
2531     // Indirect call. Visit the callee, which will leave a FunctionPointer on
2532     // the stack. Cleanup of the returned value if necessary will be done after
2533     // the function call completed.
2534     if (!this->visit(E->getCallee()))
2535       return false;
2536 
2537     if (!this->emitCallPtr(E))
2538       return false;
2539   }
2540 
2541   // Cleanup for discarded return values.
2542   if (DiscardResult && !ReturnType->isVoidType() && T)
2543     return this->emitPop(*T, E);
2544 
2545   return true;
2546 }
2547 
2548 template <class Emitter>
2549 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultInitExpr(
2550     const CXXDefaultInitExpr *E) {
2551   SourceLocScope<Emitter> SLS(this, E);
2552   if (Initializing)
2553     return this->visitInitializer(E->getExpr());
2554 
2555   assert(classify(E->getType()));
2556   return this->visit(E->getExpr());
2557 }
2558 
2559 template <class Emitter>
2560 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultArgExpr(
2561     const CXXDefaultArgExpr *E) {
2562   SourceLocScope<Emitter> SLS(this, E);
2563 
2564   const Expr *SubExpr = E->getExpr();
2565   if (std::optional<PrimType> T = classify(E->getExpr()))
2566     return this->visit(SubExpr);
2567 
2568   assert(Initializing);
2569   return this->visitInitializer(SubExpr);
2570 }
2571 
2572 template <class Emitter>
2573 bool ByteCodeExprGen<Emitter>::VisitCXXBoolLiteralExpr(
2574     const CXXBoolLiteralExpr *E) {
2575   if (DiscardResult)
2576     return true;
2577 
2578   return this->emitConstBool(E->getValue(), E);
2579 }
2580 
2581 template <class Emitter>
2582 bool ByteCodeExprGen<Emitter>::VisitCXXNullPtrLiteralExpr(
2583     const CXXNullPtrLiteralExpr *E) {
2584   if (DiscardResult)
2585     return true;
2586 
2587   return this->emitNullPtr(E);
2588 }
2589 
2590 template <class Emitter>
2591 bool ByteCodeExprGen<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) {
2592   if (DiscardResult)
2593     return true;
2594 
2595   assert(E->getType()->isIntegerType());
2596 
2597   PrimType T = classifyPrim(E->getType());
2598   return this->emitZero(T, E);
2599 }
2600 
2601 template <class Emitter>
2602 bool ByteCodeExprGen<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
2603   if (DiscardResult)
2604     return true;
2605 
2606   if (this->LambdaThisCapture > 0)
2607     return this->emitGetThisFieldPtr(this->LambdaThisCapture, E);
2608 
2609   return this->emitThis(E);
2610 }
2611 
2612 template <class Emitter>
2613 bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
2614   const Expr *SubExpr = E->getSubExpr();
2615   std::optional<PrimType> T = classify(SubExpr->getType());
2616 
2617   switch (E->getOpcode()) {
2618   case UO_PostInc: { // x++
2619     if (!this->visit(SubExpr))
2620       return false;
2621 
2622     if (T == PT_Ptr) {
2623       if (!this->emitIncPtr(E))
2624         return false;
2625 
2626       return DiscardResult ? this->emitPopPtr(E) : true;
2627     }
2628 
2629     if (T == PT_Float) {
2630       return DiscardResult ? this->emitIncfPop(getRoundingMode(E), E)
2631                            : this->emitIncf(getRoundingMode(E), E);
2632     }
2633 
2634     return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
2635   }
2636   case UO_PostDec: { // x--
2637     if (!this->visit(SubExpr))
2638       return false;
2639 
2640     if (T == PT_Ptr) {
2641       if (!this->emitDecPtr(E))
2642         return false;
2643 
2644       return DiscardResult ? this->emitPopPtr(E) : true;
2645     }
2646 
2647     if (T == PT_Float) {
2648       return DiscardResult ? this->emitDecfPop(getRoundingMode(E), E)
2649                            : this->emitDecf(getRoundingMode(E), E);
2650     }
2651 
2652     return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
2653   }
2654   case UO_PreInc: { // ++x
2655     if (!this->visit(SubExpr))
2656       return false;
2657 
2658     if (T == PT_Ptr) {
2659       if (!this->emitLoadPtr(E))
2660         return false;
2661       if (!this->emitConstUint8(1, E))
2662         return false;
2663       if (!this->emitAddOffsetUint8(E))
2664         return false;
2665       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
2666     }
2667 
2668     // Post-inc and pre-inc are the same if the value is to be discarded.
2669     if (DiscardResult) {
2670       if (T == PT_Float)
2671         return this->emitIncfPop(getRoundingMode(E), E);
2672       return this->emitIncPop(*T, E);
2673     }
2674 
2675     if (T == PT_Float) {
2676       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
2677       if (!this->emitLoadFloat(E))
2678         return false;
2679       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
2680         return false;
2681       if (!this->emitAddf(getRoundingMode(E), E))
2682         return false;
2683       return this->emitStoreFloat(E);
2684     }
2685     if (!this->emitLoad(*T, E))
2686       return false;
2687     if (!this->emitConst(1, E))
2688       return false;
2689     if (!this->emitAdd(*T, E))
2690       return false;
2691     return this->emitStore(*T, E);
2692   }
2693   case UO_PreDec: { // --x
2694     if (!this->visit(SubExpr))
2695       return false;
2696 
2697     if (T == PT_Ptr) {
2698       if (!this->emitLoadPtr(E))
2699         return false;
2700       if (!this->emitConstUint8(1, E))
2701         return false;
2702       if (!this->emitSubOffsetUint8(E))
2703         return false;
2704       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
2705     }
2706 
2707     // Post-dec and pre-dec are the same if the value is to be discarded.
2708     if (DiscardResult) {
2709       if (T == PT_Float)
2710         return this->emitDecfPop(getRoundingMode(E), E);
2711       return this->emitDecPop(*T, E);
2712     }
2713 
2714     if (T == PT_Float) {
2715       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
2716       if (!this->emitLoadFloat(E))
2717         return false;
2718       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
2719         return false;
2720       if (!this->emitSubf(getRoundingMode(E), E))
2721         return false;
2722       return this->emitStoreFloat(E);
2723     }
2724     if (!this->emitLoad(*T, E))
2725       return false;
2726     if (!this->emitConst(1, E))
2727       return false;
2728     if (!this->emitSub(*T, E))
2729       return false;
2730     return this->emitStore(*T, E);
2731   }
2732   case UO_LNot: // !x
2733     if (DiscardResult)
2734       return this->discard(SubExpr);
2735 
2736     if (!this->visitBool(SubExpr))
2737       return false;
2738 
2739     if (!this->emitInvBool(E))
2740       return false;
2741 
2742     if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
2743       return this->emitCast(PT_Bool, ET, E);
2744     return true;
2745   case UO_Minus: // -x
2746     if (!this->visit(SubExpr))
2747       return false;
2748     return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
2749   case UO_Plus:  // +x
2750     if (!this->visit(SubExpr)) // noop
2751       return false;
2752     return DiscardResult ? this->emitPop(*T, E) : true;
2753   case UO_AddrOf: // &x
2754     // We should already have a pointer when we get here.
2755     return this->delegate(SubExpr);
2756   case UO_Deref:  // *x
2757     return dereference(
2758         SubExpr, DerefKind::Read,
2759         [](PrimType) {
2760           llvm_unreachable("Dereferencing requires a pointer");
2761           return false;
2762         },
2763         [this, E](PrimType T) {
2764           return DiscardResult ? this->emitPop(T, E) : true;
2765         });
2766   case UO_Not:    // ~x
2767     if (!this->visit(SubExpr))
2768       return false;
2769     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
2770   case UO_Real: { // __real x
2771     assert(!T);
2772     if (!this->visit(SubExpr))
2773       return false;
2774     if (!this->emitConstUint8(0, E))
2775       return false;
2776     if (!this->emitArrayElemPtrPopUint8(E))
2777       return false;
2778 
2779     // Since our _Complex implementation does not map to a primitive type,
2780     // we sometimes have to do the lvalue-to-rvalue conversion here manually.
2781     if (!SubExpr->isLValue())
2782       return this->emitLoadPop(classifyPrim(E->getType()), E);
2783     return true;
2784   }
2785   case UO_Imag: { // __imag x
2786     assert(!T);
2787     if (!this->visit(SubExpr))
2788       return false;
2789     if (!this->emitConstUint8(1, E))
2790       return false;
2791     if (!this->emitArrayElemPtrPopUint8(E))
2792       return false;
2793 
2794     // Since our _Complex implementation does not map to a primitive type,
2795     // we sometimes have to do the lvalue-to-rvalue conversion here manually.
2796     if (!SubExpr->isLValue())
2797       return this->emitLoadPop(classifyPrim(E->getType()), E);
2798     return true;
2799   }
2800   case UO_Extension:
2801     return this->delegate(SubExpr);
2802   case UO_Coawait:
2803     assert(false && "Unhandled opcode");
2804   }
2805 
2806   return false;
2807 }
2808 
2809 template <class Emitter>
2810 bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
2811   if (DiscardResult)
2812     return true;
2813 
2814   const auto *D = E->getDecl();
2815 
2816   if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
2817     return this->emitConst(ECD->getInitVal(), E);
2818   } else if (const auto *BD = dyn_cast<BindingDecl>(D)) {
2819     return this->visit(BD->getBinding());
2820   } else if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
2821     const Function *F = getFunction(FuncDecl);
2822     return F && this->emitGetFnPtr(F, E);
2823   }
2824 
2825   // References are implemented via pointers, so when we see a DeclRefExpr
2826   // pointing to a reference, we need to get its value directly (i.e. the
2827   // pointer to the actual value) instead of a pointer to the pointer to the
2828   // value.
2829   bool IsReference = D->getType()->isReferenceType();
2830 
2831   // Check for local/global variables and parameters.
2832   if (auto It = Locals.find(D); It != Locals.end()) {
2833     const unsigned Offset = It->second.Offset;
2834 
2835     if (IsReference)
2836       return this->emitGetLocal(PT_Ptr, Offset, E);
2837     return this->emitGetPtrLocal(Offset, E);
2838   } else if (auto GlobalIndex = P.getGlobal(D)) {
2839     if (IsReference)
2840       return this->emitGetGlobalPtr(*GlobalIndex, E);
2841 
2842     return this->emitGetPtrGlobal(*GlobalIndex, E);
2843   } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
2844     if (auto It = this->Params.find(PVD); It != this->Params.end()) {
2845       if (IsReference || !It->second.IsPtr)
2846         return this->emitGetParamPtr(It->second.Offset, E);
2847 
2848       return this->emitGetPtrParam(It->second.Offset, E);
2849     }
2850   }
2851 
2852   // Handle lambda captures.
2853   if (auto It = this->LambdaCaptures.find(D);
2854       It != this->LambdaCaptures.end()) {
2855     auto [Offset, IsPtr] = It->second;
2856 
2857     if (IsPtr)
2858       return this->emitGetThisFieldPtr(Offset, E);
2859     return this->emitGetPtrThisField(Offset, E);
2860   }
2861 
2862   // Lazily visit global declarations we haven't seen yet.
2863   // This happens in C.
2864   if (!Ctx.getLangOpts().CPlusPlus) {
2865     if (const auto *VD = dyn_cast<VarDecl>(D);
2866         VD && VD->hasGlobalStorage() && VD->getAnyInitializer() &&
2867         VD->getType().isConstQualified()) {
2868       if (!this->visitVarDecl(VD))
2869         return false;
2870       // Retry.
2871       return this->VisitDeclRefExpr(E);
2872     }
2873 
2874     if (std::optional<unsigned> I = P.getOrCreateDummy(D))
2875       return this->emitGetPtrGlobal(*I, E);
2876   }
2877 
2878   return this->emitInvalidDeclRef(E, E);
2879 }
2880 
2881 template <class Emitter>
2882 void ByteCodeExprGen<Emitter>::emitCleanup() {
2883   for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent())
2884     C->emitDestruction();
2885 }
2886 
2887 template <class Emitter>
2888 unsigned
2889 ByteCodeExprGen<Emitter>::collectBaseOffset(const RecordType *BaseType,
2890                                             const RecordType *DerivedType) {
2891   const auto *FinalDecl = cast<CXXRecordDecl>(BaseType->getDecl());
2892   const RecordDecl *CurDecl = DerivedType->getDecl();
2893   const Record *CurRecord = getRecord(CurDecl);
2894   assert(CurDecl && FinalDecl);
2895 
2896   unsigned OffsetSum = 0;
2897   for (;;) {
2898     assert(CurRecord->getNumBases() > 0);
2899     // One level up
2900     for (const Record::Base &B : CurRecord->bases()) {
2901       const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
2902 
2903       if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
2904         OffsetSum += B.Offset;
2905         CurRecord = B.R;
2906         CurDecl = BaseDecl;
2907         break;
2908       }
2909     }
2910     if (CurDecl == FinalDecl)
2911       break;
2912   }
2913 
2914   assert(OffsetSum > 0);
2915   return OffsetSum;
2916 }
2917 
2918 /// Emit casts from a PrimType to another PrimType.
2919 template <class Emitter>
2920 bool ByteCodeExprGen<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
2921                                             QualType ToQT, const Expr *E) {
2922 
2923   if (FromT == PT_Float) {
2924     // Floating to floating.
2925     if (ToT == PT_Float) {
2926       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
2927       return this->emitCastFP(ToSem, getRoundingMode(E), E);
2928     }
2929 
2930     // Float to integral.
2931     if (isIntegralType(ToT) || ToT == PT_Bool)
2932       return this->emitCastFloatingIntegral(ToT, E);
2933   }
2934 
2935   if (isIntegralType(FromT) || FromT == PT_Bool) {
2936     // Integral to integral.
2937     if (isIntegralType(ToT) || ToT == PT_Bool)
2938       return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
2939 
2940     if (ToT == PT_Float) {
2941       // Integral to floating.
2942       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
2943       return this->emitCastIntegralFloating(FromT, ToSem, getRoundingMode(E),
2944                                             E);
2945     }
2946   }
2947 
2948   return false;
2949 }
2950 
2951 /// When calling this, we have a pointer of the local-to-destroy
2952 /// on the stack.
2953 /// Emit destruction of record types (or arrays of record types).
2954 /// FIXME: Handle virtual destructors.
2955 template <class Emitter>
2956 bool ByteCodeExprGen<Emitter>::emitRecordDestruction(const Descriptor *Desc) {
2957   assert(Desc);
2958   assert(!Desc->isPrimitive());
2959   assert(!Desc->isPrimitiveArray());
2960 
2961   // Arrays.
2962   if (Desc->isArray()) {
2963     const Descriptor *ElemDesc = Desc->ElemDesc;
2964     assert(ElemDesc);
2965 
2966     // Don't need to do anything for these.
2967     if (ElemDesc->isPrimitiveArray())
2968       return this->emitPopPtr(SourceInfo{});
2969 
2970     // If this is an array of record types, check if we need
2971     // to call the element destructors at all. If not, try
2972     // to save the work.
2973     if (const Record *ElemRecord = ElemDesc->ElemRecord) {
2974       if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor();
2975           !Dtor || Dtor->isTrivial())
2976         return this->emitPopPtr(SourceInfo{});
2977     }
2978 
2979     for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) {
2980       if (!this->emitConstUint64(I, SourceInfo{}))
2981         return false;
2982       if (!this->emitArrayElemPtrUint64(SourceInfo{}))
2983         return false;
2984       if (!this->emitRecordDestruction(ElemDesc))
2985         return false;
2986     }
2987     return this->emitPopPtr(SourceInfo{});
2988   }
2989 
2990   const Record *R = Desc->ElemRecord;
2991   assert(R);
2992   // First, destroy all fields.
2993   for (const Record::Field &Field : llvm::reverse(R->fields())) {
2994     const Descriptor *D = Field.Desc;
2995     if (!D->isPrimitive() && !D->isPrimitiveArray()) {
2996       if (!this->emitDupPtr(SourceInfo{}))
2997         return false;
2998       if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
2999         return false;
3000       if (!this->emitRecordDestruction(D))
3001         return false;
3002     }
3003   }
3004 
3005   // FIXME: Unions need to be handled differently here. We don't want to
3006   //   call the destructor of its members.
3007 
3008   // Now emit the destructor and recurse into base classes.
3009   if (const CXXDestructorDecl *Dtor = R->getDestructor();
3010       Dtor && !Dtor->isTrivial()) {
3011     if (const Function *DtorFunc = getFunction(Dtor)) {
3012       assert(DtorFunc->hasThisPointer());
3013       assert(DtorFunc->getNumParams() == 1);
3014       if (!this->emitDupPtr(SourceInfo{}))
3015         return false;
3016       if (!this->emitCall(DtorFunc, SourceInfo{}))
3017         return false;
3018     }
3019   }
3020 
3021   for (const Record::Base &Base : llvm::reverse(R->bases())) {
3022     if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
3023       return false;
3024     if (!this->emitRecordDestruction(Base.Desc))
3025       return false;
3026   }
3027   // FIXME: Virtual bases.
3028 
3029   // Remove the instance pointer.
3030   return this->emitPopPtr(SourceInfo{});
3031 }
3032 
3033 namespace clang {
3034 namespace interp {
3035 
3036 template class ByteCodeExprGen<ByteCodeEmitter>;
3037 template class ByteCodeExprGen<EvalEmitter>;
3038 
3039 } // namespace interp
3040 } // namespace clang
3041