1 //===--- Compiler.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 "Compiler.h"
10 #include "ByteCodeEmitter.h"
11 #include "Context.h"
12 #include "FixedPoint.h"
13 #include "Floating.h"
14 #include "Function.h"
15 #include "InterpShared.h"
16 #include "PrimType.h"
17 #include "Program.h"
18 #include "clang/AST/Attr.h"
19
20 using namespace clang;
21 using namespace clang::interp;
22
23 using APSInt = llvm::APSInt;
24
25 namespace clang {
26 namespace interp {
27
getBoolValue(const Expr * E)28 static std::optional<bool> getBoolValue(const Expr *E) {
29 if (const auto *CE = dyn_cast_if_present<ConstantExpr>(E);
30 CE && CE->hasAPValueResult() &&
31 CE->getResultAPValueKind() == APValue::ValueKind::Int) {
32 return CE->getResultAsAPSInt().getBoolValue();
33 }
34
35 return std::nullopt;
36 }
37
38 /// Scope used to handle temporaries in toplevel variable declarations.
39 template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
40 public:
DeclScope(Compiler<Emitter> * Ctx,const ValueDecl * VD)41 DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
42 : LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P),
43 OldInitializingDecl(Ctx->InitializingDecl) {
44 Ctx->InitializingDecl = VD;
45 Ctx->InitStack.push_back(InitLink::Decl(VD));
46 }
47
~DeclScope()48 ~DeclScope() {
49 this->Ctx->InitializingDecl = OldInitializingDecl;
50 this->Ctx->InitStack.pop_back();
51 }
52
53 private:
54 Program::DeclScope Scope;
55 const ValueDecl *OldInitializingDecl;
56 };
57
58 /// Scope used to handle initialization methods.
59 template <class Emitter> class OptionScope final {
60 public:
61 /// Root constructor, compiling or discarding primitives.
OptionScope(Compiler<Emitter> * Ctx,bool NewDiscardResult,bool NewInitializing)62 OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult,
63 bool NewInitializing)
64 : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
65 OldInitializing(Ctx->Initializing) {
66 Ctx->DiscardResult = NewDiscardResult;
67 Ctx->Initializing = NewInitializing;
68 }
69
~OptionScope()70 ~OptionScope() {
71 Ctx->DiscardResult = OldDiscardResult;
72 Ctx->Initializing = OldInitializing;
73 }
74
75 private:
76 /// Parent context.
77 Compiler<Emitter> *Ctx;
78 /// Old discard flag to restore.
79 bool OldDiscardResult;
80 bool OldInitializing;
81 };
82
83 template <class Emitter>
emit(Compiler<Emitter> * Ctx,const Expr * E) const84 bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
85 switch (Kind) {
86 case K_This:
87 return Ctx->emitThis(E);
88 case K_Field:
89 // We're assuming there's a base pointer on the stack already.
90 return Ctx->emitGetPtrFieldPop(Offset, E);
91 case K_Temp:
92 return Ctx->emitGetPtrLocal(Offset, E);
93 case K_Decl:
94 return Ctx->visitDeclRef(D, E);
95 case K_Elem:
96 if (!Ctx->emitConstUint32(Offset, E))
97 return false;
98 return Ctx->emitArrayElemPtrPopUint32(E);
99 case K_RVO:
100 return Ctx->emitRVOPtr(E);
101 case K_InitList:
102 return true;
103 default:
104 llvm_unreachable("Unhandled InitLink kind");
105 }
106 return true;
107 }
108
109 /// Scope managing label targets.
110 template <class Emitter> class LabelScope {
111 public:
~LabelScope()112 virtual ~LabelScope() {}
113
114 protected:
LabelScope(Compiler<Emitter> * Ctx)115 LabelScope(Compiler<Emitter> *Ctx) : Ctx(Ctx) {}
116 /// Compiler instance.
117 Compiler<Emitter> *Ctx;
118 };
119
120 /// Sets the context for break/continue statements.
121 template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
122 public:
123 using LabelTy = typename Compiler<Emitter>::LabelTy;
124 using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
125
LoopScope(Compiler<Emitter> * Ctx,LabelTy BreakLabel,LabelTy ContinueLabel)126 LoopScope(Compiler<Emitter> *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
127 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
128 OldContinueLabel(Ctx->ContinueLabel),
129 OldBreakVarScope(Ctx->BreakVarScope),
130 OldContinueVarScope(Ctx->ContinueVarScope) {
131 this->Ctx->BreakLabel = BreakLabel;
132 this->Ctx->ContinueLabel = ContinueLabel;
133 this->Ctx->BreakVarScope = this->Ctx->VarScope;
134 this->Ctx->ContinueVarScope = this->Ctx->VarScope;
135 }
136
~LoopScope()137 ~LoopScope() {
138 this->Ctx->BreakLabel = OldBreakLabel;
139 this->Ctx->ContinueLabel = OldContinueLabel;
140 this->Ctx->ContinueVarScope = OldContinueVarScope;
141 this->Ctx->BreakVarScope = OldBreakVarScope;
142 }
143
144 private:
145 OptLabelTy OldBreakLabel;
146 OptLabelTy OldContinueLabel;
147 VariableScope<Emitter> *OldBreakVarScope;
148 VariableScope<Emitter> *OldContinueVarScope;
149 };
150
151 // Sets the context for a switch scope, mapping labels.
152 template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
153 public:
154 using LabelTy = typename Compiler<Emitter>::LabelTy;
155 using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
156 using CaseMap = typename Compiler<Emitter>::CaseMap;
157
SwitchScope(Compiler<Emitter> * Ctx,CaseMap && CaseLabels,LabelTy BreakLabel,OptLabelTy DefaultLabel)158 SwitchScope(Compiler<Emitter> *Ctx, CaseMap &&CaseLabels, LabelTy BreakLabel,
159 OptLabelTy DefaultLabel)
160 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
161 OldDefaultLabel(this->Ctx->DefaultLabel),
162 OldCaseLabels(std::move(this->Ctx->CaseLabels)),
163 OldLabelVarScope(Ctx->BreakVarScope) {
164 this->Ctx->BreakLabel = BreakLabel;
165 this->Ctx->DefaultLabel = DefaultLabel;
166 this->Ctx->CaseLabels = std::move(CaseLabels);
167 this->Ctx->BreakVarScope = this->Ctx->VarScope;
168 }
169
~SwitchScope()170 ~SwitchScope() {
171 this->Ctx->BreakLabel = OldBreakLabel;
172 this->Ctx->DefaultLabel = OldDefaultLabel;
173 this->Ctx->CaseLabels = std::move(OldCaseLabels);
174 this->Ctx->BreakVarScope = OldLabelVarScope;
175 }
176
177 private:
178 OptLabelTy OldBreakLabel;
179 OptLabelTy OldDefaultLabel;
180 CaseMap OldCaseLabels;
181 VariableScope<Emitter> *OldLabelVarScope;
182 };
183
184 template <class Emitter> class StmtExprScope final {
185 public:
StmtExprScope(Compiler<Emitter> * Ctx)186 StmtExprScope(Compiler<Emitter> *Ctx) : Ctx(Ctx), OldFlag(Ctx->InStmtExpr) {
187 Ctx->InStmtExpr = true;
188 }
189
~StmtExprScope()190 ~StmtExprScope() { Ctx->InStmtExpr = OldFlag; }
191
192 private:
193 Compiler<Emitter> *Ctx;
194 bool OldFlag;
195 };
196
197 } // namespace interp
198 } // namespace clang
199
200 template <class Emitter>
VisitCastExpr(const CastExpr * CE)201 bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
202 const Expr *SubExpr = CE->getSubExpr();
203
204 if (DiscardResult)
205 return this->delegate(SubExpr);
206
207 switch (CE->getCastKind()) {
208 case CK_LValueToRValue: {
209 if (SubExpr->getType().isVolatileQualified())
210 return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, CE);
211
212 std::optional<PrimType> SubExprT = classify(SubExpr->getType());
213 // Prepare storage for the result.
214 if (!Initializing && !SubExprT) {
215 std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
216 if (!LocalIndex)
217 return false;
218 if (!this->emitGetPtrLocal(*LocalIndex, CE))
219 return false;
220 }
221
222 if (!this->visit(SubExpr))
223 return false;
224
225 if (SubExprT)
226 return this->emitLoadPop(*SubExprT, CE);
227
228 // If the subexpr type is not primitive, we need to perform a copy here.
229 // This happens for example in C when dereferencing a pointer of struct
230 // type.
231 return this->emitMemcpy(CE);
232 }
233
234 case CK_DerivedToBaseMemberPointer: {
235 assert(classifyPrim(CE->getType()) == PT_MemberPtr);
236 assert(classifyPrim(SubExpr->getType()) == PT_MemberPtr);
237 const auto *FromMP = SubExpr->getType()->castAs<MemberPointerType>();
238 const auto *ToMP = CE->getType()->castAs<MemberPointerType>();
239
240 unsigned DerivedOffset =
241 Ctx.collectBaseOffset(ToMP->getMostRecentCXXRecordDecl(),
242 FromMP->getMostRecentCXXRecordDecl());
243
244 if (!this->delegate(SubExpr))
245 return false;
246
247 return this->emitGetMemberPtrBasePop(DerivedOffset, CE);
248 }
249
250 case CK_BaseToDerivedMemberPointer: {
251 assert(classifyPrim(CE) == PT_MemberPtr);
252 assert(classifyPrim(SubExpr) == PT_MemberPtr);
253 const auto *FromMP = SubExpr->getType()->castAs<MemberPointerType>();
254 const auto *ToMP = CE->getType()->castAs<MemberPointerType>();
255
256 unsigned DerivedOffset =
257 Ctx.collectBaseOffset(FromMP->getMostRecentCXXRecordDecl(),
258 ToMP->getMostRecentCXXRecordDecl());
259
260 if (!this->delegate(SubExpr))
261 return false;
262 return this->emitGetMemberPtrBasePop(-DerivedOffset, CE);
263 }
264
265 case CK_UncheckedDerivedToBase:
266 case CK_DerivedToBase: {
267 if (!this->delegate(SubExpr))
268 return false;
269
270 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
271 if (const auto *PT = dyn_cast<PointerType>(Ty))
272 return PT->getPointeeType()->getAsCXXRecordDecl();
273 return Ty->getAsCXXRecordDecl();
274 };
275
276 // FIXME: We can express a series of non-virtual casts as a single
277 // GetPtrBasePop op.
278 QualType CurType = SubExpr->getType();
279 for (const CXXBaseSpecifier *B : CE->path()) {
280 if (B->isVirtual()) {
281 if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), CE))
282 return false;
283 CurType = B->getType();
284 } else {
285 unsigned DerivedOffset = collectBaseOffset(B->getType(), CurType);
286 if (!this->emitGetPtrBasePop(
287 DerivedOffset, /*NullOK=*/CE->getType()->isPointerType(), CE))
288 return false;
289 CurType = B->getType();
290 }
291 }
292
293 return true;
294 }
295
296 case CK_BaseToDerived: {
297 if (!this->delegate(SubExpr))
298 return false;
299 unsigned DerivedOffset =
300 collectBaseOffset(SubExpr->getType(), CE->getType());
301
302 const Type *TargetType = CE->getType().getTypePtr();
303 if (TargetType->isPointerOrReferenceType())
304 TargetType = TargetType->getPointeeType().getTypePtr();
305 return this->emitGetPtrDerivedPop(DerivedOffset,
306 /*NullOK=*/CE->getType()->isPointerType(),
307 TargetType, CE);
308 }
309
310 case CK_FloatingCast: {
311 // HLSL uses CK_FloatingCast to cast between vectors.
312 if (!SubExpr->getType()->isFloatingType() ||
313 !CE->getType()->isFloatingType())
314 return false;
315 if (!this->visit(SubExpr))
316 return false;
317 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
318 return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
319 }
320
321 case CK_IntegralToFloating: {
322 if (!CE->getType()->isRealFloatingType())
323 return false;
324 if (!this->visit(SubExpr))
325 return false;
326 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
327 return this->emitCastIntegralFloating(
328 classifyPrim(SubExpr), TargetSemantics, getFPOptions(CE), CE);
329 }
330
331 case CK_FloatingToBoolean: {
332 if (!SubExpr->getType()->isRealFloatingType() ||
333 !CE->getType()->isBooleanType())
334 return false;
335 if (const auto *FL = dyn_cast<FloatingLiteral>(SubExpr))
336 return this->emitConstBool(FL->getValue().isNonZero(), CE);
337 if (!this->visit(SubExpr))
338 return false;
339 return this->emitCastFloatingIntegralBool(getFPOptions(CE), CE);
340 }
341
342 case CK_FloatingToIntegral: {
343 if (!this->visit(SubExpr))
344 return false;
345 PrimType ToT = classifyPrim(CE);
346 if (ToT == PT_IntAP)
347 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
348 getFPOptions(CE), CE);
349 if (ToT == PT_IntAPS)
350 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
351 getFPOptions(CE), CE);
352
353 return this->emitCastFloatingIntegral(ToT, getFPOptions(CE), CE);
354 }
355
356 case CK_NullToPointer:
357 case CK_NullToMemberPointer: {
358 if (!this->discard(SubExpr))
359 return false;
360 const Descriptor *Desc = nullptr;
361 const QualType PointeeType = CE->getType()->getPointeeType();
362 if (!PointeeType.isNull()) {
363 if (std::optional<PrimType> T = classify(PointeeType))
364 Desc = P.createDescriptor(SubExpr, *T);
365 else
366 Desc = P.createDescriptor(SubExpr, PointeeType.getTypePtr(),
367 std::nullopt, /*IsConst=*/true);
368 }
369
370 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(CE->getType());
371 return this->emitNull(classifyPrim(CE->getType()), Val, Desc, CE);
372 }
373
374 case CK_PointerToIntegral: {
375 if (!this->visit(SubExpr))
376 return false;
377
378 // If SubExpr doesn't result in a pointer, make it one.
379 if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
380 assert(isPtrType(FromT));
381 if (!this->emitDecayPtr(FromT, PT_Ptr, CE))
382 return false;
383 }
384
385 PrimType T = classifyPrim(CE->getType());
386 if (T == PT_IntAP)
387 return this->emitCastPointerIntegralAP(Ctx.getBitWidth(CE->getType()),
388 CE);
389 if (T == PT_IntAPS)
390 return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(CE->getType()),
391 CE);
392 return this->emitCastPointerIntegral(T, CE);
393 }
394
395 case CK_ArrayToPointerDecay: {
396 if (!this->visit(SubExpr))
397 return false;
398 return this->emitArrayDecay(CE);
399 }
400
401 case CK_IntegralToPointer: {
402 QualType IntType = SubExpr->getType();
403 assert(IntType->isIntegralOrEnumerationType());
404 if (!this->visit(SubExpr))
405 return false;
406 // FIXME: I think the discard is wrong since the int->ptr cast might cause a
407 // diagnostic.
408 PrimType T = classifyPrim(IntType);
409 QualType PtrType = CE->getType();
410 const Descriptor *Desc;
411 if (std::optional<PrimType> T = classify(PtrType->getPointeeType()))
412 Desc = P.createDescriptor(SubExpr, *T);
413 else if (PtrType->getPointeeType()->isVoidType())
414 Desc = nullptr;
415 else
416 Desc = P.createDescriptor(CE, PtrType->getPointeeType().getTypePtr(),
417 Descriptor::InlineDescMD, /*IsConst=*/true);
418
419 if (!this->emitGetIntPtr(T, Desc, CE))
420 return false;
421
422 PrimType DestPtrT = classifyPrim(PtrType);
423 if (DestPtrT == PT_Ptr)
424 return true;
425
426 // In case we're converting the integer to a non-Pointer.
427 return this->emitDecayPtr(PT_Ptr, DestPtrT, CE);
428 }
429
430 case CK_AtomicToNonAtomic:
431 case CK_ConstructorConversion:
432 case CK_FunctionToPointerDecay:
433 case CK_NonAtomicToAtomic:
434 case CK_NoOp:
435 case CK_UserDefinedConversion:
436 case CK_AddressSpaceConversion:
437 case CK_CPointerToObjCPointerCast:
438 return this->delegate(SubExpr);
439
440 case CK_BitCast: {
441 // Reject bitcasts to atomic types.
442 if (CE->getType()->isAtomicType()) {
443 if (!this->discard(SubExpr))
444 return false;
445 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, CE);
446 }
447 QualType SubExprTy = SubExpr->getType();
448 std::optional<PrimType> FromT = classify(SubExprTy);
449 // Casts from integer/vector to vector.
450 if (CE->getType()->isVectorType())
451 return this->emitBuiltinBitCast(CE);
452
453 std::optional<PrimType> ToT = classify(CE->getType());
454 if (!FromT || !ToT)
455 return false;
456
457 assert(isPtrType(*FromT));
458 assert(isPtrType(*ToT));
459 if (FromT == ToT) {
460 if (CE->getType()->isVoidPointerType())
461 return this->delegate(SubExpr);
462
463 if (!this->visit(SubExpr))
464 return false;
465 if (CE->getType()->isFunctionPointerType())
466 return true;
467 if (FromT == PT_Ptr)
468 return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), CE);
469 return true;
470 }
471
472 if (!this->visit(SubExpr))
473 return false;
474 return this->emitDecayPtr(*FromT, *ToT, CE);
475 }
476 case CK_IntegralToBoolean:
477 case CK_FixedPointToBoolean: {
478 // HLSL uses this to cast to one-element vectors.
479 std::optional<PrimType> FromT = classify(SubExpr->getType());
480 if (!FromT)
481 return false;
482
483 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr))
484 return this->emitConst(IL->getValue(), CE);
485 if (!this->visit(SubExpr))
486 return false;
487 return this->emitCast(*FromT, classifyPrim(CE), CE);
488 }
489
490 case CK_BooleanToSignedIntegral:
491 case CK_IntegralCast: {
492 std::optional<PrimType> FromT = classify(SubExpr->getType());
493 std::optional<PrimType> ToT = classify(CE->getType());
494 if (!FromT || !ToT)
495 return false;
496
497 // Try to emit a casted known constant value directly.
498 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
499 if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
500 FromT != PT_IntAPS && !CE->getType()->isEnumeralType())
501 return this->emitConst(IL->getValue(), CE);
502 if (!this->emitConst(IL->getValue(), SubExpr))
503 return false;
504 } else {
505 if (!this->visit(SubExpr))
506 return false;
507 }
508
509 // Possibly diagnose casts to enum types if the target type does not
510 // have a fixed size.
511 if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
512 if (const auto *ET = CE->getType().getCanonicalType()->castAs<EnumType>();
513 !ET->getDecl()->isFixed()) {
514 if (!this->emitCheckEnumValue(*FromT, ET->getDecl(), CE))
515 return false;
516 }
517 }
518
519 if (ToT == PT_IntAP) {
520 if (!this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE))
521 return false;
522 } else if (ToT == PT_IntAPS) {
523 if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE))
524 return false;
525 } else {
526 if (FromT == ToT)
527 return true;
528 if (!this->emitCast(*FromT, *ToT, CE))
529 return false;
530 }
531 if (CE->getCastKind() == CK_BooleanToSignedIntegral)
532 return this->emitNeg(*ToT, CE);
533 return true;
534 }
535
536 case CK_PointerToBoolean:
537 case CK_MemberPointerToBoolean: {
538 PrimType PtrT = classifyPrim(SubExpr->getType());
539
540 if (!this->visit(SubExpr))
541 return false;
542 return this->emitIsNonNull(PtrT, CE);
543 }
544
545 case CK_IntegralComplexToBoolean:
546 case CK_FloatingComplexToBoolean: {
547 if (!this->visit(SubExpr))
548 return false;
549 return this->emitComplexBoolCast(SubExpr);
550 }
551
552 case CK_IntegralComplexToReal:
553 case CK_FloatingComplexToReal:
554 return this->emitComplexReal(SubExpr);
555
556 case CK_IntegralRealToComplex:
557 case CK_FloatingRealToComplex: {
558 // We're creating a complex value here, so we need to
559 // allocate storage for it.
560 if (!Initializing) {
561 std::optional<unsigned> LocalIndex = allocateTemporary(CE);
562 if (!LocalIndex)
563 return false;
564 if (!this->emitGetPtrLocal(*LocalIndex, CE))
565 return false;
566 }
567
568 PrimType T = classifyPrim(SubExpr->getType());
569 // Init the complex value to {SubExpr, 0}.
570 if (!this->visitArrayElemInit(0, SubExpr, T))
571 return false;
572 // Zero-init the second element.
573 if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
574 return false;
575 return this->emitInitElem(T, 1, SubExpr);
576 }
577
578 case CK_IntegralComplexCast:
579 case CK_FloatingComplexCast:
580 case CK_IntegralComplexToFloatingComplex:
581 case CK_FloatingComplexToIntegralComplex: {
582 assert(CE->getType()->isAnyComplexType());
583 assert(SubExpr->getType()->isAnyComplexType());
584 if (!Initializing) {
585 std::optional<unsigned> LocalIndex = allocateLocal(CE);
586 if (!LocalIndex)
587 return false;
588 if (!this->emitGetPtrLocal(*LocalIndex, CE))
589 return false;
590 }
591
592 // Location for the SubExpr.
593 // Since SubExpr is of complex type, visiting it results in a pointer
594 // anyway, so we just create a temporary pointer variable.
595 unsigned SubExprOffset =
596 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
597 if (!this->visit(SubExpr))
598 return false;
599 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
600 return false;
601
602 PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
603 QualType DestElemType =
604 CE->getType()->getAs<ComplexType>()->getElementType();
605 PrimType DestElemT = classifyPrim(DestElemType);
606 // Cast both elements individually.
607 for (unsigned I = 0; I != 2; ++I) {
608 if (!this->emitGetLocal(PT_Ptr, SubExprOffset, CE))
609 return false;
610 if (!this->emitArrayElemPop(SourceElemT, I, CE))
611 return false;
612
613 // Do the cast.
614 if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, CE))
615 return false;
616
617 // Save the value.
618 if (!this->emitInitElem(DestElemT, I, CE))
619 return false;
620 }
621 return true;
622 }
623
624 case CK_VectorSplat: {
625 assert(!classify(CE->getType()));
626 assert(classify(SubExpr->getType()));
627 assert(CE->getType()->isVectorType());
628
629 if (!Initializing) {
630 std::optional<unsigned> LocalIndex = allocateLocal(CE);
631 if (!LocalIndex)
632 return false;
633 if (!this->emitGetPtrLocal(*LocalIndex, CE))
634 return false;
635 }
636
637 const auto *VT = CE->getType()->getAs<VectorType>();
638 PrimType ElemT = classifyPrim(SubExpr->getType());
639 unsigned ElemOffset =
640 allocateLocalPrimitive(SubExpr, ElemT, /*IsConst=*/true);
641
642 // Prepare a local variable for the scalar value.
643 if (!this->visit(SubExpr))
644 return false;
645 if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, CE))
646 return false;
647
648 if (!this->emitSetLocal(ElemT, ElemOffset, CE))
649 return false;
650
651 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
652 if (!this->emitGetLocal(ElemT, ElemOffset, CE))
653 return false;
654 if (!this->emitInitElem(ElemT, I, CE))
655 return false;
656 }
657
658 return true;
659 }
660
661 case CK_HLSLVectorTruncation: {
662 assert(SubExpr->getType()->isVectorType());
663 if (std::optional<PrimType> ResultT = classify(CE)) {
664 assert(!DiscardResult);
665 // Result must be either a float or integer. Take the first element.
666 if (!this->visit(SubExpr))
667 return false;
668 return this->emitArrayElemPop(*ResultT, 0, CE);
669 }
670 // Otherwise, this truncates from one vector type to another.
671 assert(CE->getType()->isVectorType());
672
673 if (!Initializing) {
674 std::optional<unsigned> LocalIndex = allocateTemporary(CE);
675 if (!LocalIndex)
676 return false;
677 if (!this->emitGetPtrLocal(*LocalIndex, CE))
678 return false;
679 }
680 unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
681 assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
682 if (!this->visit(SubExpr))
683 return false;
684 return this->emitCopyArray(classifyVectorElementType(CE->getType()), 0, 0,
685 ToSize, CE);
686 };
687
688 case CK_IntegralToFixedPoint: {
689 if (!this->visit(SubExpr))
690 return false;
691
692 auto Sem =
693 Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
694 return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()),
695 Sem, CE);
696 }
697 case CK_FloatingToFixedPoint: {
698 if (!this->visit(SubExpr))
699 return false;
700
701 auto Sem =
702 Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
703 return this->emitCastFloatingFixedPoint(Sem, CE);
704 }
705 case CK_FixedPointToFloating: {
706 if (!this->visit(SubExpr))
707 return false;
708 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
709 return this->emitCastFixedPointFloating(TargetSemantics, CE);
710 }
711 case CK_FixedPointToIntegral: {
712 if (!this->visit(SubExpr))
713 return false;
714 return this->emitCastFixedPointIntegral(classifyPrim(CE->getType()), CE);
715 }
716 case CK_FixedPointCast: {
717 if (!this->visit(SubExpr))
718 return false;
719 auto Sem =
720 Ctx.getASTContext().getFixedPointSemantics(CE->getType()).toOpaqueInt();
721 return this->emitCastFixedPoint(Sem, CE);
722 }
723
724 case CK_ToVoid:
725 return discard(SubExpr);
726
727 default:
728 return this->emitInvalid(CE);
729 }
730 llvm_unreachable("Unhandled clang::CastKind enum");
731 }
732
733 template <class Emitter>
VisitBuiltinBitCastExpr(const BuiltinBitCastExpr * E)734 bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
735 return this->emitBuiltinBitCast(E);
736 }
737
738 template <class Emitter>
VisitIntegerLiteral(const IntegerLiteral * LE)739 bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
740 if (DiscardResult)
741 return true;
742
743 return this->emitConst(LE->getValue(), LE);
744 }
745
746 template <class Emitter>
VisitFloatingLiteral(const FloatingLiteral * E)747 bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
748 if (DiscardResult)
749 return true;
750
751 APFloat F = E->getValue();
752 return this->emitFloat(F, E);
753 }
754
755 template <class Emitter>
VisitImaginaryLiteral(const ImaginaryLiteral * E)756 bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
757 assert(E->getType()->isAnyComplexType());
758 if (DiscardResult)
759 return true;
760
761 if (!Initializing) {
762 std::optional<unsigned> LocalIndex = allocateTemporary(E);
763 if (!LocalIndex)
764 return false;
765 if (!this->emitGetPtrLocal(*LocalIndex, E))
766 return false;
767 }
768
769 const Expr *SubExpr = E->getSubExpr();
770 PrimType SubExprT = classifyPrim(SubExpr->getType());
771
772 if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
773 return false;
774 if (!this->emitInitElem(SubExprT, 0, SubExpr))
775 return false;
776 return this->visitArrayElemInit(1, SubExpr, SubExprT);
777 }
778
779 template <class Emitter>
VisitFixedPointLiteral(const FixedPointLiteral * E)780 bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) {
781 assert(E->getType()->isFixedPointType());
782 assert(classifyPrim(E) == PT_FixedPoint);
783
784 if (DiscardResult)
785 return true;
786
787 auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
788 APInt Value = E->getValue();
789 return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
790 }
791
792 template <class Emitter>
VisitParenExpr(const ParenExpr * E)793 bool Compiler<Emitter>::VisitParenExpr(const ParenExpr *E) {
794 return this->delegate(E->getSubExpr());
795 }
796
797 template <class Emitter>
VisitBinaryOperator(const BinaryOperator * BO)798 bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
799 // Need short-circuiting for these.
800 if (BO->isLogicalOp() && !BO->getType()->isVectorType())
801 return this->VisitLogicalBinOp(BO);
802
803 const Expr *LHS = BO->getLHS();
804 const Expr *RHS = BO->getRHS();
805
806 // Handle comma operators. Just discard the LHS
807 // and delegate to RHS.
808 if (BO->isCommaOp()) {
809 if (!this->discard(LHS))
810 return false;
811 if (RHS->getType()->isVoidType())
812 return this->discard(RHS);
813
814 return this->delegate(RHS);
815 }
816
817 if (BO->getType()->isAnyComplexType())
818 return this->VisitComplexBinOp(BO);
819 if (BO->getType()->isVectorType())
820 return this->VisitVectorBinOp(BO);
821 if ((LHS->getType()->isAnyComplexType() ||
822 RHS->getType()->isAnyComplexType()) &&
823 BO->isComparisonOp())
824 return this->emitComplexComparison(LHS, RHS, BO);
825 if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType())
826 return this->VisitFixedPointBinOp(BO);
827
828 if (BO->isPtrMemOp()) {
829 if (!this->visit(LHS))
830 return false;
831
832 if (!this->visit(RHS))
833 return false;
834
835 if (!this->emitToMemberPtr(BO))
836 return false;
837
838 if (classifyPrim(BO) == PT_MemberPtr)
839 return true;
840
841 if (!this->emitCastMemberPtrPtr(BO))
842 return false;
843 return DiscardResult ? this->emitPopPtr(BO) : true;
844 }
845
846 // Typecheck the args.
847 std::optional<PrimType> LT = classify(LHS);
848 std::optional<PrimType> RT = classify(RHS);
849 std::optional<PrimType> T = classify(BO->getType());
850
851 // Special case for C++'s three-way/spaceship operator <=>, which
852 // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
853 // have a PrimType).
854 if (!T && BO->getOpcode() == BO_Cmp) {
855 if (DiscardResult)
856 return true;
857 const ComparisonCategoryInfo *CmpInfo =
858 Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
859 assert(CmpInfo);
860
861 // We need a temporary variable holding our return value.
862 if (!Initializing) {
863 std::optional<unsigned> ResultIndex = this->allocateLocal(BO);
864 if (!this->emitGetPtrLocal(*ResultIndex, BO))
865 return false;
866 }
867
868 if (!visit(LHS) || !visit(RHS))
869 return false;
870
871 return this->emitCMP3(*LT, CmpInfo, BO);
872 }
873
874 if (!LT || !RT || !T)
875 return false;
876
877 // Pointer arithmetic special case.
878 if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
879 if (isPtrType(*T) || (isPtrType(*LT) && isPtrType(*RT)))
880 return this->VisitPointerArithBinOp(BO);
881 }
882
883 // Assignments require us to evalute the RHS first.
884 if (BO->getOpcode() == BO_Assign) {
885
886 if (!visit(RHS) || !visit(LHS))
887 return false;
888
889 // We don't support assignments in C.
890 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(BO))
891 return false;
892
893 if (!this->emitFlip(*LT, *RT, BO))
894 return false;
895 } else {
896 if (!visit(LHS) || !visit(RHS))
897 return false;
898 }
899
900 // For languages such as C, cast the result of one
901 // of our comparision opcodes to T (which is usually int).
902 auto MaybeCastToBool = [this, T, BO](bool Result) {
903 if (!Result)
904 return false;
905 if (DiscardResult)
906 return this->emitPop(*T, BO);
907 if (T != PT_Bool)
908 return this->emitCast(PT_Bool, *T, BO);
909 return true;
910 };
911
912 auto Discard = [this, T, BO](bool Result) {
913 if (!Result)
914 return false;
915 return DiscardResult ? this->emitPop(*T, BO) : true;
916 };
917
918 switch (BO->getOpcode()) {
919 case BO_EQ:
920 return MaybeCastToBool(this->emitEQ(*LT, BO));
921 case BO_NE:
922 return MaybeCastToBool(this->emitNE(*LT, BO));
923 case BO_LT:
924 return MaybeCastToBool(this->emitLT(*LT, BO));
925 case BO_LE:
926 return MaybeCastToBool(this->emitLE(*LT, BO));
927 case BO_GT:
928 return MaybeCastToBool(this->emitGT(*LT, BO));
929 case BO_GE:
930 return MaybeCastToBool(this->emitGE(*LT, BO));
931 case BO_Sub:
932 if (BO->getType()->isFloatingType())
933 return Discard(this->emitSubf(getFPOptions(BO), BO));
934 return Discard(this->emitSub(*T, BO));
935 case BO_Add:
936 if (BO->getType()->isFloatingType())
937 return Discard(this->emitAddf(getFPOptions(BO), BO));
938 return Discard(this->emitAdd(*T, BO));
939 case BO_Mul:
940 if (BO->getType()->isFloatingType())
941 return Discard(this->emitMulf(getFPOptions(BO), BO));
942 return Discard(this->emitMul(*T, BO));
943 case BO_Rem:
944 return Discard(this->emitRem(*T, BO));
945 case BO_Div:
946 if (BO->getType()->isFloatingType())
947 return Discard(this->emitDivf(getFPOptions(BO), BO));
948 return Discard(this->emitDiv(*T, BO));
949 case BO_Assign:
950 if (DiscardResult)
951 return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
952 : this->emitStorePop(*T, BO);
953 if (LHS->refersToBitField()) {
954 if (!this->emitStoreBitField(*T, BO))
955 return false;
956 } else {
957 if (!this->emitStore(*T, BO))
958 return false;
959 }
960 // Assignments aren't necessarily lvalues in C.
961 // Load from them in that case.
962 if (!BO->isLValue())
963 return this->emitLoadPop(*T, BO);
964 return true;
965 case BO_And:
966 return Discard(this->emitBitAnd(*T, BO));
967 case BO_Or:
968 return Discard(this->emitBitOr(*T, BO));
969 case BO_Shl:
970 return Discard(this->emitShl(*LT, *RT, BO));
971 case BO_Shr:
972 return Discard(this->emitShr(*LT, *RT, BO));
973 case BO_Xor:
974 return Discard(this->emitBitXor(*T, BO));
975 case BO_LOr:
976 case BO_LAnd:
977 llvm_unreachable("Already handled earlier");
978 default:
979 return false;
980 }
981
982 llvm_unreachable("Unhandled binary op");
983 }
984
985 /// Perform addition/subtraction of a pointer and an integer or
986 /// subtraction of two pointers.
987 template <class Emitter>
VisitPointerArithBinOp(const BinaryOperator * E)988 bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
989 BinaryOperatorKind Op = E->getOpcode();
990 const Expr *LHS = E->getLHS();
991 const Expr *RHS = E->getRHS();
992
993 if ((Op != BO_Add && Op != BO_Sub) ||
994 (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
995 return false;
996
997 std::optional<PrimType> LT = classify(LHS);
998 std::optional<PrimType> RT = classify(RHS);
999
1000 if (!LT || !RT)
1001 return false;
1002
1003 // Visit the given pointer expression and optionally convert to a PT_Ptr.
1004 auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool {
1005 if (!this->visit(E))
1006 return false;
1007 if (T != PT_Ptr)
1008 return this->emitDecayPtr(T, PT_Ptr, E);
1009 return true;
1010 };
1011
1012 if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
1013 if (Op != BO_Sub)
1014 return false;
1015
1016 assert(E->getType()->isIntegerType());
1017 if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
1018 return false;
1019
1020 PrimType IntT = classifyPrim(E->getType());
1021 if (!this->emitSubPtr(IntT, E))
1022 return false;
1023 return DiscardResult ? this->emitPop(IntT, E) : true;
1024 }
1025
1026 PrimType OffsetType;
1027 if (LHS->getType()->isIntegerType()) {
1028 if (!visitAsPointer(RHS, *RT))
1029 return false;
1030 if (!this->visit(LHS))
1031 return false;
1032 OffsetType = *LT;
1033 } else if (RHS->getType()->isIntegerType()) {
1034 if (!visitAsPointer(LHS, *LT))
1035 return false;
1036 if (!this->visit(RHS))
1037 return false;
1038 OffsetType = *RT;
1039 } else {
1040 return false;
1041 }
1042
1043 // Do the operation and optionally transform to
1044 // result pointer type.
1045 if (Op == BO_Add) {
1046 if (!this->emitAddOffset(OffsetType, E))
1047 return false;
1048
1049 if (classifyPrim(E) != PT_Ptr)
1050 return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
1051 return true;
1052 } else if (Op == BO_Sub) {
1053 if (!this->emitSubOffset(OffsetType, E))
1054 return false;
1055
1056 if (classifyPrim(E) != PT_Ptr)
1057 return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
1058 return true;
1059 }
1060
1061 return false;
1062 }
1063
1064 template <class Emitter>
VisitLogicalBinOp(const BinaryOperator * E)1065 bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
1066 assert(E->isLogicalOp());
1067 BinaryOperatorKind Op = E->getOpcode();
1068 const Expr *LHS = E->getLHS();
1069 const Expr *RHS = E->getRHS();
1070 std::optional<PrimType> T = classify(E->getType());
1071
1072 if (Op == BO_LOr) {
1073 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
1074 LabelTy LabelTrue = this->getLabel();
1075 LabelTy LabelEnd = this->getLabel();
1076
1077 if (!this->visitBool(LHS))
1078 return false;
1079 if (!this->jumpTrue(LabelTrue))
1080 return false;
1081
1082 if (!this->visitBool(RHS))
1083 return false;
1084 if (!this->jump(LabelEnd))
1085 return false;
1086
1087 this->emitLabel(LabelTrue);
1088 this->emitConstBool(true, E);
1089 this->fallthrough(LabelEnd);
1090 this->emitLabel(LabelEnd);
1091
1092 } else {
1093 assert(Op == BO_LAnd);
1094 // Logical AND.
1095 // Visit LHS. Only visit RHS if LHS was TRUE.
1096 LabelTy LabelFalse = this->getLabel();
1097 LabelTy LabelEnd = this->getLabel();
1098
1099 if (!this->visitBool(LHS))
1100 return false;
1101 if (!this->jumpFalse(LabelFalse))
1102 return false;
1103
1104 if (!this->visitBool(RHS))
1105 return false;
1106 if (!this->jump(LabelEnd))
1107 return false;
1108
1109 this->emitLabel(LabelFalse);
1110 this->emitConstBool(false, E);
1111 this->fallthrough(LabelEnd);
1112 this->emitLabel(LabelEnd);
1113 }
1114
1115 if (DiscardResult)
1116 return this->emitPopBool(E);
1117
1118 // For C, cast back to integer type.
1119 assert(T);
1120 if (T != PT_Bool)
1121 return this->emitCast(PT_Bool, *T, E);
1122 return true;
1123 }
1124
1125 template <class Emitter>
VisitComplexBinOp(const BinaryOperator * E)1126 bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
1127 // Prepare storage for result.
1128 if (!Initializing) {
1129 std::optional<unsigned> LocalIndex = allocateTemporary(E);
1130 if (!LocalIndex)
1131 return false;
1132 if (!this->emitGetPtrLocal(*LocalIndex, E))
1133 return false;
1134 }
1135
1136 // Both LHS and RHS might _not_ be of complex type, but one of them
1137 // needs to be.
1138 const Expr *LHS = E->getLHS();
1139 const Expr *RHS = E->getRHS();
1140
1141 PrimType ResultElemT = this->classifyComplexElementType(E->getType());
1142 unsigned ResultOffset = ~0u;
1143 if (!DiscardResult)
1144 ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
1145
1146 // Save result pointer in ResultOffset
1147 if (!this->DiscardResult) {
1148 if (!this->emitDupPtr(E))
1149 return false;
1150 if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
1151 return false;
1152 }
1153 QualType LHSType = LHS->getType();
1154 if (const auto *AT = LHSType->getAs<AtomicType>())
1155 LHSType = AT->getValueType();
1156 QualType RHSType = RHS->getType();
1157 if (const auto *AT = RHSType->getAs<AtomicType>())
1158 RHSType = AT->getValueType();
1159
1160 bool LHSIsComplex = LHSType->isAnyComplexType();
1161 unsigned LHSOffset;
1162 bool RHSIsComplex = RHSType->isAnyComplexType();
1163
1164 // For ComplexComplex Mul, we have special ops to make their implementation
1165 // easier.
1166 BinaryOperatorKind Op = E->getOpcode();
1167 if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) {
1168 assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) ==
1169 classifyPrim(RHSType->getAs<ComplexType>()->getElementType()));
1170 PrimType ElemT =
1171 classifyPrim(LHSType->getAs<ComplexType>()->getElementType());
1172 if (!this->visit(LHS))
1173 return false;
1174 if (!this->visit(RHS))
1175 return false;
1176 return this->emitMulc(ElemT, E);
1177 }
1178
1179 if (Op == BO_Div && RHSIsComplex) {
1180 QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType();
1181 PrimType ElemT = classifyPrim(ElemQT);
1182 // If the LHS is not complex, we still need to do the full complex
1183 // division, so just stub create a complex value and stub it out with
1184 // the LHS and a zero.
1185
1186 if (!LHSIsComplex) {
1187 // This is using the RHS type for the fake-complex LHS.
1188 std::optional<unsigned> LocalIndex = allocateTemporary(RHS);
1189 if (!LocalIndex)
1190 return false;
1191 LHSOffset = *LocalIndex;
1192
1193 if (!this->emitGetPtrLocal(LHSOffset, E))
1194 return false;
1195
1196 if (!this->visit(LHS))
1197 return false;
1198 // real is LHS
1199 if (!this->emitInitElem(ElemT, 0, E))
1200 return false;
1201 // imag is zero
1202 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1203 return false;
1204 if (!this->emitInitElem(ElemT, 1, E))
1205 return false;
1206 } else {
1207 if (!this->visit(LHS))
1208 return false;
1209 }
1210
1211 if (!this->visit(RHS))
1212 return false;
1213 return this->emitDivc(ElemT, E);
1214 }
1215
1216 // Evaluate LHS and save value to LHSOffset.
1217 if (LHSType->isAnyComplexType()) {
1218 LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1219 if (!this->visit(LHS))
1220 return false;
1221 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1222 return false;
1223 } else {
1224 PrimType LHST = classifyPrim(LHSType);
1225 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
1226 if (!this->visit(LHS))
1227 return false;
1228 if (!this->emitSetLocal(LHST, LHSOffset, E))
1229 return false;
1230 }
1231
1232 // Same with RHS.
1233 unsigned RHSOffset;
1234 if (RHSType->isAnyComplexType()) {
1235 RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1236 if (!this->visit(RHS))
1237 return false;
1238 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1239 return false;
1240 } else {
1241 PrimType RHST = classifyPrim(RHSType);
1242 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
1243 if (!this->visit(RHS))
1244 return false;
1245 if (!this->emitSetLocal(RHST, RHSOffset, E))
1246 return false;
1247 }
1248
1249 // For both LHS and RHS, either load the value from the complex pointer, or
1250 // directly from the local variable. For index 1 (i.e. the imaginary part),
1251 // just load 0 and do the operation anyway.
1252 auto loadComplexValue = [this](bool IsComplex, bool LoadZero,
1253 unsigned ElemIndex, unsigned Offset,
1254 const Expr *E) -> bool {
1255 if (IsComplex) {
1256 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1257 return false;
1258 return this->emitArrayElemPop(classifyComplexElementType(E->getType()),
1259 ElemIndex, E);
1260 }
1261 if (ElemIndex == 0 || !LoadZero)
1262 return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
1263 return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
1264 E);
1265 };
1266
1267 // Now we can get pointers to the LHS and RHS from the offsets above.
1268 for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
1269 // Result pointer for the store later.
1270 if (!this->DiscardResult) {
1271 if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
1272 return false;
1273 }
1274
1275 // The actual operation.
1276 switch (Op) {
1277 case BO_Add:
1278 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1279 return false;
1280
1281 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1282 return false;
1283 if (ResultElemT == PT_Float) {
1284 if (!this->emitAddf(getFPOptions(E), E))
1285 return false;
1286 } else {
1287 if (!this->emitAdd(ResultElemT, E))
1288 return false;
1289 }
1290 break;
1291 case BO_Sub:
1292 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1293 return false;
1294
1295 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1296 return false;
1297 if (ResultElemT == PT_Float) {
1298 if (!this->emitSubf(getFPOptions(E), E))
1299 return false;
1300 } else {
1301 if (!this->emitSub(ResultElemT, E))
1302 return false;
1303 }
1304 break;
1305 case BO_Mul:
1306 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1307 return false;
1308
1309 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1310 return false;
1311
1312 if (ResultElemT == PT_Float) {
1313 if (!this->emitMulf(getFPOptions(E), E))
1314 return false;
1315 } else {
1316 if (!this->emitMul(ResultElemT, E))
1317 return false;
1318 }
1319 break;
1320 case BO_Div:
1321 assert(!RHSIsComplex);
1322 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1323 return false;
1324
1325 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1326 return false;
1327
1328 if (ResultElemT == PT_Float) {
1329 if (!this->emitDivf(getFPOptions(E), E))
1330 return false;
1331 } else {
1332 if (!this->emitDiv(ResultElemT, E))
1333 return false;
1334 }
1335 break;
1336
1337 default:
1338 return false;
1339 }
1340
1341 if (!this->DiscardResult) {
1342 // Initialize array element with the value we just computed.
1343 if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
1344 return false;
1345 } else {
1346 if (!this->emitPop(ResultElemT, E))
1347 return false;
1348 }
1349 }
1350 return true;
1351 }
1352
1353 template <class Emitter>
VisitVectorBinOp(const BinaryOperator * E)1354 bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
1355 assert(!E->isCommaOp() &&
1356 "Comma op should be handled in VisitBinaryOperator");
1357 assert(E->getType()->isVectorType());
1358 assert(E->getLHS()->getType()->isVectorType());
1359 assert(E->getRHS()->getType()->isVectorType());
1360
1361 // Prepare storage for result.
1362 if (!Initializing && !E->isCompoundAssignmentOp()) {
1363 std::optional<unsigned> LocalIndex = allocateTemporary(E);
1364 if (!LocalIndex)
1365 return false;
1366 if (!this->emitGetPtrLocal(*LocalIndex, E))
1367 return false;
1368 }
1369
1370 const Expr *LHS = E->getLHS();
1371 const Expr *RHS = E->getRHS();
1372 const auto *VecTy = E->getType()->getAs<VectorType>();
1373 auto Op = E->isCompoundAssignmentOp()
1374 ? BinaryOperator::getOpForCompoundAssignment(E->getOpcode())
1375 : E->getOpcode();
1376
1377 PrimType ElemT = this->classifyVectorElementType(LHS->getType());
1378 PrimType RHSElemT = this->classifyVectorElementType(RHS->getType());
1379 PrimType ResultElemT = this->classifyVectorElementType(E->getType());
1380
1381 // Evaluate LHS and save value to LHSOffset.
1382 unsigned LHSOffset =
1383 this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1384 if (!this->visit(LHS))
1385 return false;
1386 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1387 return false;
1388
1389 // Evaluate RHS and save value to RHSOffset.
1390 unsigned RHSOffset =
1391 this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1392 if (!this->visit(RHS))
1393 return false;
1394 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1395 return false;
1396
1397 if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E))
1398 return false;
1399
1400 // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the
1401 // integer promotion.
1402 bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp());
1403 QualType PromotTy =
1404 Ctx.getASTContext().getPromotedIntegerType(Ctx.getASTContext().BoolTy);
1405 PrimType PromotT = classifyPrim(PromotTy);
1406 PrimType OpT = NeedIntPromot ? PromotT : ElemT;
1407
1408 auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) {
1409 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1410 return false;
1411 if (!this->emitArrayElemPop(ElemT, Index, E))
1412 return false;
1413 if (E->isLogicalOp()) {
1414 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
1415 return false;
1416 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1417 return false;
1418 } else if (NeedIntPromot) {
1419 if (!this->emitPrimCast(ElemT, PromotT, PromotTy, E))
1420 return false;
1421 }
1422 return true;
1423 };
1424
1425 #define EMIT_ARITH_OP(OP) \
1426 { \
1427 if (ElemT == PT_Float) { \
1428 if (!this->emit##OP##f(getFPOptions(E), E)) \
1429 return false; \
1430 } else { \
1431 if (!this->emit##OP(ElemT, E)) \
1432 return false; \
1433 } \
1434 break; \
1435 }
1436
1437 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
1438 if (!getElem(LHSOffset, ElemT, I))
1439 return false;
1440 if (!getElem(RHSOffset, RHSElemT, I))
1441 return false;
1442 switch (Op) {
1443 case BO_Add:
1444 EMIT_ARITH_OP(Add)
1445 case BO_Sub:
1446 EMIT_ARITH_OP(Sub)
1447 case BO_Mul:
1448 EMIT_ARITH_OP(Mul)
1449 case BO_Div:
1450 EMIT_ARITH_OP(Div)
1451 case BO_Rem:
1452 if (!this->emitRem(ElemT, E))
1453 return false;
1454 break;
1455 case BO_And:
1456 if (!this->emitBitAnd(OpT, E))
1457 return false;
1458 break;
1459 case BO_Or:
1460 if (!this->emitBitOr(OpT, E))
1461 return false;
1462 break;
1463 case BO_Xor:
1464 if (!this->emitBitXor(OpT, E))
1465 return false;
1466 break;
1467 case BO_Shl:
1468 if (!this->emitShl(OpT, RHSElemT, E))
1469 return false;
1470 break;
1471 case BO_Shr:
1472 if (!this->emitShr(OpT, RHSElemT, E))
1473 return false;
1474 break;
1475 case BO_EQ:
1476 if (!this->emitEQ(ElemT, E))
1477 return false;
1478 break;
1479 case BO_NE:
1480 if (!this->emitNE(ElemT, E))
1481 return false;
1482 break;
1483 case BO_LE:
1484 if (!this->emitLE(ElemT, E))
1485 return false;
1486 break;
1487 case BO_LT:
1488 if (!this->emitLT(ElemT, E))
1489 return false;
1490 break;
1491 case BO_GE:
1492 if (!this->emitGE(ElemT, E))
1493 return false;
1494 break;
1495 case BO_GT:
1496 if (!this->emitGT(ElemT, E))
1497 return false;
1498 break;
1499 case BO_LAnd:
1500 // a && b is equivalent to a!=0 & b!=0
1501 if (!this->emitBitAnd(ResultElemT, E))
1502 return false;
1503 break;
1504 case BO_LOr:
1505 // a || b is equivalent to a!=0 | b!=0
1506 if (!this->emitBitOr(ResultElemT, E))
1507 return false;
1508 break;
1509 default:
1510 return this->emitInvalid(E);
1511 }
1512
1513 // The result of the comparison is a vector of the same width and number
1514 // of elements as the comparison operands with a signed integral element
1515 // type.
1516 //
1517 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
1518 if (E->isComparisonOp()) {
1519 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1520 return false;
1521 if (!this->emitNeg(ResultElemT, E))
1522 return false;
1523 }
1524
1525 // If we performed an integer promotion, we need to cast the compute result
1526 // into result vector element type.
1527 if (NeedIntPromot &&
1528 !this->emitPrimCast(PromotT, ResultElemT, VecTy->getElementType(), E))
1529 return false;
1530
1531 // Initialize array element with the value we just computed.
1532 if (!this->emitInitElem(ResultElemT, I, E))
1533 return false;
1534 }
1535
1536 if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E))
1537 return false;
1538 return true;
1539 }
1540
1541 template <class Emitter>
VisitFixedPointBinOp(const BinaryOperator * E)1542 bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
1543 const Expr *LHS = E->getLHS();
1544 const Expr *RHS = E->getRHS();
1545 const ASTContext &ASTCtx = Ctx.getASTContext();
1546
1547 assert(LHS->getType()->isFixedPointType() ||
1548 RHS->getType()->isFixedPointType());
1549
1550 auto LHSSema = ASTCtx.getFixedPointSemantics(LHS->getType());
1551 auto LHSSemaInt = LHSSema.toOpaqueInt();
1552 auto RHSSema = ASTCtx.getFixedPointSemantics(RHS->getType());
1553 auto RHSSemaInt = RHSSema.toOpaqueInt();
1554
1555 if (!this->visit(LHS))
1556 return false;
1557 if (!LHS->getType()->isFixedPointType()) {
1558 if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()),
1559 LHSSemaInt, E))
1560 return false;
1561 }
1562
1563 if (!this->visit(RHS))
1564 return false;
1565 if (!RHS->getType()->isFixedPointType()) {
1566 if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()),
1567 RHSSemaInt, E))
1568 return false;
1569 }
1570
1571 // Convert the result to the target semantics.
1572 auto ConvertResult = [&](bool R) -> bool {
1573 if (!R)
1574 return false;
1575 auto ResultSema = ASTCtx.getFixedPointSemantics(E->getType()).toOpaqueInt();
1576 auto CommonSema = LHSSema.getCommonSemantics(RHSSema).toOpaqueInt();
1577 if (ResultSema != CommonSema)
1578 return this->emitCastFixedPoint(ResultSema, E);
1579 return true;
1580 };
1581
1582 auto MaybeCastToBool = [&](bool Result) {
1583 if (!Result)
1584 return false;
1585 PrimType T = classifyPrim(E);
1586 if (DiscardResult)
1587 return this->emitPop(T, E);
1588 if (T != PT_Bool)
1589 return this->emitCast(PT_Bool, T, E);
1590 return true;
1591 };
1592
1593 switch (E->getOpcode()) {
1594 case BO_EQ:
1595 return MaybeCastToBool(this->emitEQFixedPoint(E));
1596 case BO_NE:
1597 return MaybeCastToBool(this->emitNEFixedPoint(E));
1598 case BO_LT:
1599 return MaybeCastToBool(this->emitLTFixedPoint(E));
1600 case BO_LE:
1601 return MaybeCastToBool(this->emitLEFixedPoint(E));
1602 case BO_GT:
1603 return MaybeCastToBool(this->emitGTFixedPoint(E));
1604 case BO_GE:
1605 return MaybeCastToBool(this->emitGEFixedPoint(E));
1606 case BO_Add:
1607 return ConvertResult(this->emitAddFixedPoint(E));
1608 case BO_Sub:
1609 return ConvertResult(this->emitSubFixedPoint(E));
1610 case BO_Mul:
1611 return ConvertResult(this->emitMulFixedPoint(E));
1612 case BO_Div:
1613 return ConvertResult(this->emitDivFixedPoint(E));
1614 case BO_Shl:
1615 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E));
1616 case BO_Shr:
1617 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E));
1618
1619 default:
1620 return this->emitInvalid(E);
1621 }
1622
1623 llvm_unreachable("unhandled binop opcode");
1624 }
1625
1626 template <class Emitter>
VisitFixedPointUnaryOperator(const UnaryOperator * E)1627 bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) {
1628 const Expr *SubExpr = E->getSubExpr();
1629 assert(SubExpr->getType()->isFixedPointType());
1630
1631 switch (E->getOpcode()) {
1632 case UO_Plus:
1633 return this->delegate(SubExpr);
1634 case UO_Minus:
1635 if (!this->visit(SubExpr))
1636 return false;
1637 return this->emitNegFixedPoint(E);
1638 default:
1639 return false;
1640 }
1641
1642 llvm_unreachable("Unhandled unary opcode");
1643 }
1644
1645 template <class Emitter>
VisitImplicitValueInitExpr(const ImplicitValueInitExpr * E)1646 bool Compiler<Emitter>::VisitImplicitValueInitExpr(
1647 const ImplicitValueInitExpr *E) {
1648 QualType QT = E->getType();
1649
1650 if (std::optional<PrimType> T = classify(QT))
1651 return this->visitZeroInitializer(*T, QT, E);
1652
1653 if (QT->isRecordType()) {
1654 const RecordDecl *RD = QT->getAsRecordDecl();
1655 assert(RD);
1656 if (RD->isInvalidDecl())
1657 return false;
1658
1659 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1660 CXXRD && CXXRD->getNumVBases() > 0) {
1661 // TODO: Diagnose.
1662 return false;
1663 }
1664
1665 const Record *R = getRecord(QT);
1666 if (!R)
1667 return false;
1668
1669 assert(Initializing);
1670 return this->visitZeroRecordInitializer(R, E);
1671 }
1672
1673 if (QT->isIncompleteArrayType())
1674 return true;
1675
1676 if (QT->isArrayType())
1677 return this->visitZeroArrayInitializer(QT, E);
1678
1679 if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
1680 assert(Initializing);
1681 QualType ElemQT = ComplexTy->getElementType();
1682 PrimType ElemT = classifyPrim(ElemQT);
1683 for (unsigned I = 0; I < 2; ++I) {
1684 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1685 return false;
1686 if (!this->emitInitElem(ElemT, I, E))
1687 return false;
1688 }
1689 return true;
1690 }
1691
1692 if (const auto *VecT = E->getType()->getAs<VectorType>()) {
1693 unsigned NumVecElements = VecT->getNumElements();
1694 QualType ElemQT = VecT->getElementType();
1695 PrimType ElemT = classifyPrim(ElemQT);
1696
1697 for (unsigned I = 0; I < NumVecElements; ++I) {
1698 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1699 return false;
1700 if (!this->emitInitElem(ElemT, I, E))
1701 return false;
1702 }
1703 return true;
1704 }
1705
1706 return false;
1707 }
1708
1709 template <class Emitter>
VisitArraySubscriptExpr(const ArraySubscriptExpr * E)1710 bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1711 const Expr *LHS = E->getLHS();
1712 const Expr *RHS = E->getRHS();
1713 const Expr *Index = E->getIdx();
1714 const Expr *Base = E->getBase();
1715
1716 // C++17's rules require us to evaluate the LHS first, regardless of which
1717 // side is the base.
1718 bool Success = true;
1719 for (const Expr *SubExpr : {LHS, RHS}) {
1720 if (!this->visit(SubExpr)) {
1721 Success = false;
1722 continue;
1723 }
1724
1725 // Expand the base if this is a subscript on a
1726 // pointer expression.
1727 if (SubExpr == Base && Base->getType()->isPointerType()) {
1728 if (!this->emitExpandPtr(E))
1729 Success = false;
1730 }
1731 }
1732
1733 if (!Success)
1734 return false;
1735
1736 std::optional<PrimType> IndexT = classify(Index->getType());
1737 // In error-recovery cases, the index expression has a dependent type.
1738 if (!IndexT)
1739 return this->emitError(E);
1740 // If the index is first, we need to change that.
1741 if (LHS == Index) {
1742 if (!this->emitFlip(PT_Ptr, *IndexT, E))
1743 return false;
1744 }
1745
1746 if (!this->emitArrayElemPtrPop(*IndexT, E))
1747 return false;
1748 if (DiscardResult)
1749 return this->emitPopPtr(E);
1750 return true;
1751 }
1752
1753 template <class Emitter>
visitInitList(ArrayRef<const Expr * > Inits,const Expr * ArrayFiller,const Expr * E)1754 bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
1755 const Expr *ArrayFiller, const Expr *E) {
1756 InitLinkScope<Emitter> ILS(this, InitLink::InitList());
1757
1758 QualType QT = E->getType();
1759 if (const auto *AT = QT->getAs<AtomicType>())
1760 QT = AT->getValueType();
1761
1762 if (QT->isVoidType()) {
1763 if (Inits.size() == 0)
1764 return true;
1765 return this->emitInvalid(E);
1766 }
1767
1768 // Handle discarding first.
1769 if (DiscardResult) {
1770 for (const Expr *Init : Inits) {
1771 if (!this->discard(Init))
1772 return false;
1773 }
1774 return true;
1775 }
1776
1777 // Primitive values.
1778 if (std::optional<PrimType> T = classify(QT)) {
1779 assert(!DiscardResult);
1780 if (Inits.size() == 0)
1781 return this->visitZeroInitializer(*T, QT, E);
1782 assert(Inits.size() == 1);
1783 return this->delegate(Inits[0]);
1784 }
1785
1786 if (QT->isRecordType()) {
1787 const Record *R = getRecord(QT);
1788
1789 if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
1790 return this->delegate(Inits[0]);
1791
1792 auto initPrimitiveField = [=](const Record::Field *FieldToInit,
1793 const Expr *Init, PrimType T) -> bool {
1794 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1795 InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
1796 if (!this->visit(Init))
1797 return false;
1798
1799 if (FieldToInit->isBitField())
1800 return this->emitInitBitField(T, FieldToInit, E);
1801 return this->emitInitField(T, FieldToInit->Offset, E);
1802 };
1803
1804 auto initCompositeField = [=](const Record::Field *FieldToInit,
1805 const Expr *Init) -> bool {
1806 InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1807 InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
1808
1809 // Non-primitive case. Get a pointer to the field-to-initialize
1810 // on the stack and recurse into visitInitializer().
1811 if (!this->emitGetPtrField(FieldToInit->Offset, Init))
1812 return false;
1813 if (!this->visitInitializer(Init))
1814 return false;
1815 return this->emitPopPtr(E);
1816 };
1817
1818 if (R->isUnion()) {
1819 if (Inits.size() == 0) {
1820 if (!this->visitZeroRecordInitializer(R, E))
1821 return false;
1822 } else {
1823 const Expr *Init = Inits[0];
1824 const FieldDecl *FToInit = nullptr;
1825 if (const auto *ILE = dyn_cast<InitListExpr>(E))
1826 FToInit = ILE->getInitializedFieldInUnion();
1827 else
1828 FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion();
1829
1830 const Record::Field *FieldToInit = R->getField(FToInit);
1831 if (std::optional<PrimType> T = classify(Init)) {
1832 if (!initPrimitiveField(FieldToInit, Init, *T))
1833 return false;
1834 } else {
1835 if (!initCompositeField(FieldToInit, Init))
1836 return false;
1837 }
1838 }
1839 return this->emitFinishInit(E);
1840 }
1841
1842 assert(!R->isUnion());
1843 unsigned InitIndex = 0;
1844 for (const Expr *Init : Inits) {
1845 // Skip unnamed bitfields.
1846 while (InitIndex < R->getNumFields() &&
1847 R->getField(InitIndex)->isUnnamedBitField())
1848 ++InitIndex;
1849
1850 if (std::optional<PrimType> T = classify(Init)) {
1851 const Record::Field *FieldToInit = R->getField(InitIndex);
1852 if (!initPrimitiveField(FieldToInit, Init, *T))
1853 return false;
1854 ++InitIndex;
1855 } else {
1856 // Initializer for a direct base class.
1857 if (const Record::Base *B = R->getBase(Init->getType())) {
1858 if (!this->emitGetPtrBase(B->Offset, Init))
1859 return false;
1860
1861 if (!this->visitInitializer(Init))
1862 return false;
1863
1864 if (!this->emitFinishInitPop(E))
1865 return false;
1866 // Base initializers don't increase InitIndex, since they don't count
1867 // into the Record's fields.
1868 } else {
1869 const Record::Field *FieldToInit = R->getField(InitIndex);
1870 if (!initCompositeField(FieldToInit, Init))
1871 return false;
1872 ++InitIndex;
1873 }
1874 }
1875 }
1876 return this->emitFinishInit(E);
1877 }
1878
1879 if (QT->isArrayType()) {
1880 if (Inits.size() == 1 && QT == Inits[0]->getType())
1881 return this->delegate(Inits[0]);
1882
1883 const ConstantArrayType *CAT =
1884 Ctx.getASTContext().getAsConstantArrayType(QT);
1885 uint64_t NumElems = CAT->getZExtSize();
1886
1887 if (!this->emitCheckArraySize(NumElems, E))
1888 return false;
1889
1890 std::optional<PrimType> InitT = classify(CAT->getElementType());
1891 unsigned ElementIndex = 0;
1892 for (const Expr *Init : Inits) {
1893 if (const auto *EmbedS =
1894 dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1895 PrimType TargetT = classifyPrim(Init->getType());
1896
1897 auto Eval = [&](const Expr *Init, unsigned ElemIndex) {
1898 PrimType InitT = classifyPrim(Init->getType());
1899 if (!this->visit(Init))
1900 return false;
1901 if (InitT != TargetT) {
1902 if (!this->emitCast(InitT, TargetT, E))
1903 return false;
1904 }
1905 return this->emitInitElem(TargetT, ElemIndex, Init);
1906 };
1907 if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
1908 return false;
1909 } else {
1910 if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
1911 return false;
1912 ++ElementIndex;
1913 }
1914 }
1915
1916 // Expand the filler expression.
1917 // FIXME: This should go away.
1918 if (ArrayFiller) {
1919 for (; ElementIndex != NumElems; ++ElementIndex) {
1920 if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
1921 return false;
1922 }
1923 }
1924
1925 return this->emitFinishInit(E);
1926 }
1927
1928 if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
1929 unsigned NumInits = Inits.size();
1930
1931 if (NumInits == 1)
1932 return this->delegate(Inits[0]);
1933
1934 QualType ElemQT = ComplexTy->getElementType();
1935 PrimType ElemT = classifyPrim(ElemQT);
1936 if (NumInits == 0) {
1937 // Zero-initialize both elements.
1938 for (unsigned I = 0; I < 2; ++I) {
1939 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1940 return false;
1941 if (!this->emitInitElem(ElemT, I, E))
1942 return false;
1943 }
1944 } else if (NumInits == 2) {
1945 unsigned InitIndex = 0;
1946 for (const Expr *Init : Inits) {
1947 if (!this->visit(Init))
1948 return false;
1949
1950 if (!this->emitInitElem(ElemT, InitIndex, E))
1951 return false;
1952 ++InitIndex;
1953 }
1954 }
1955 return true;
1956 }
1957
1958 if (const auto *VecT = QT->getAs<VectorType>()) {
1959 unsigned NumVecElements = VecT->getNumElements();
1960 assert(NumVecElements >= Inits.size());
1961
1962 QualType ElemQT = VecT->getElementType();
1963 PrimType ElemT = classifyPrim(ElemQT);
1964
1965 // All initializer elements.
1966 unsigned InitIndex = 0;
1967 for (const Expr *Init : Inits) {
1968 if (!this->visit(Init))
1969 return false;
1970
1971 // If the initializer is of vector type itself, we have to deconstruct
1972 // that and initialize all the target fields from the initializer fields.
1973 if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
1974 if (!this->emitCopyArray(ElemT, 0, InitIndex,
1975 InitVecT->getNumElements(), E))
1976 return false;
1977 InitIndex += InitVecT->getNumElements();
1978 } else {
1979 if (!this->emitInitElem(ElemT, InitIndex, E))
1980 return false;
1981 ++InitIndex;
1982 }
1983 }
1984
1985 assert(InitIndex <= NumVecElements);
1986
1987 // Fill the rest with zeroes.
1988 for (; InitIndex != NumVecElements; ++InitIndex) {
1989 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1990 return false;
1991 if (!this->emitInitElem(ElemT, InitIndex, E))
1992 return false;
1993 }
1994 return true;
1995 }
1996
1997 return false;
1998 }
1999
2000 /// Pointer to the array(not the element!) must be on the stack when calling
2001 /// this.
2002 template <class Emitter>
visitArrayElemInit(unsigned ElemIndex,const Expr * Init,std::optional<PrimType> InitT)2003 bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
2004 std::optional<PrimType> InitT) {
2005 if (InitT) {
2006 // Visit the primitive element like normal.
2007 if (!this->visit(Init))
2008 return false;
2009 return this->emitInitElem(*InitT, ElemIndex, Init);
2010 }
2011
2012 InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
2013 // Advance the pointer currently on the stack to the given
2014 // dimension.
2015 if (!this->emitConstUint32(ElemIndex, Init))
2016 return false;
2017 if (!this->emitArrayElemPtrUint32(Init))
2018 return false;
2019 if (!this->visitInitializer(Init))
2020 return false;
2021 return this->emitFinishInitPop(Init);
2022 }
2023
2024 template <class Emitter>
visitCallArgs(ArrayRef<const Expr * > Args,const FunctionDecl * FuncDecl)2025 bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
2026 const FunctionDecl *FuncDecl) {
2027 assert(VarScope->getKind() == ScopeKind::Call);
2028 llvm::BitVector NonNullArgs = collectNonNullArgs(FuncDecl, Args);
2029
2030 unsigned ArgIndex = 0;
2031 for (const Expr *Arg : Args) {
2032 if (std::optional<PrimType> T = classify(Arg)) {
2033 if (!this->visit(Arg))
2034 return false;
2035 } else {
2036
2037 std::optional<unsigned> LocalIndex = allocateLocal(
2038 Arg, Arg->getType(), /*ExtendingDecl=*/nullptr, ScopeKind::Call);
2039 if (!LocalIndex)
2040 return false;
2041
2042 if (!this->emitGetPtrLocal(*LocalIndex, Arg))
2043 return false;
2044 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2045 if (!this->visitInitializer(Arg))
2046 return false;
2047 }
2048
2049 if (FuncDecl && NonNullArgs[ArgIndex]) {
2050 PrimType ArgT = classify(Arg).value_or(PT_Ptr);
2051 if (ArgT == PT_Ptr) {
2052 if (!this->emitCheckNonNullArg(ArgT, Arg))
2053 return false;
2054 }
2055 }
2056
2057 ++ArgIndex;
2058 }
2059
2060 return true;
2061 }
2062
2063 template <class Emitter>
VisitInitListExpr(const InitListExpr * E)2064 bool Compiler<Emitter>::VisitInitListExpr(const InitListExpr *E) {
2065 return this->visitInitList(E->inits(), E->getArrayFiller(), E);
2066 }
2067
2068 template <class Emitter>
VisitCXXParenListInitExpr(const CXXParenListInitExpr * E)2069 bool Compiler<Emitter>::VisitCXXParenListInitExpr(
2070 const CXXParenListInitExpr *E) {
2071 return this->visitInitList(E->getInitExprs(), E->getArrayFiller(), E);
2072 }
2073
2074 template <class Emitter>
VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr * E)2075 bool Compiler<Emitter>::VisitSubstNonTypeTemplateParmExpr(
2076 const SubstNonTypeTemplateParmExpr *E) {
2077 return this->delegate(E->getReplacement());
2078 }
2079
2080 template <class Emitter>
VisitConstantExpr(const ConstantExpr * E)2081 bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
2082 std::optional<PrimType> T = classify(E->getType());
2083 if (T && E->hasAPValueResult()) {
2084 // Try to emit the APValue directly, without visiting the subexpr.
2085 // This will only fail if we can't emit the APValue, so won't emit any
2086 // diagnostics or any double values.
2087 if (DiscardResult)
2088 return true;
2089
2090 if (this->visitAPValue(E->getAPValueResult(), *T, E))
2091 return true;
2092 }
2093 return this->delegate(E->getSubExpr());
2094 }
2095
2096 template <class Emitter>
VisitEmbedExpr(const EmbedExpr * E)2097 bool Compiler<Emitter>::VisitEmbedExpr(const EmbedExpr *E) {
2098 auto It = E->begin();
2099 return this->visit(*It);
2100 }
2101
AlignOfType(QualType T,const ASTContext & ASTCtx,UnaryExprOrTypeTrait Kind)2102 static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx,
2103 UnaryExprOrTypeTrait Kind) {
2104 bool AlignOfReturnsPreferred =
2105 ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
2106
2107 // C++ [expr.alignof]p3:
2108 // When alignof is applied to a reference type, the result is the
2109 // alignment of the referenced type.
2110 if (const auto *Ref = T->getAs<ReferenceType>())
2111 T = Ref->getPointeeType();
2112
2113 if (T.getQualifiers().hasUnaligned())
2114 return CharUnits::One();
2115
2116 // __alignof is defined to return the preferred alignment.
2117 // Before 8, clang returned the preferred alignment for alignof and
2118 // _Alignof as well.
2119 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
2120 return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
2121
2122 return ASTCtx.getTypeAlignInChars(T);
2123 }
2124
2125 template <class Emitter>
VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr * E)2126 bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
2127 const UnaryExprOrTypeTraitExpr *E) {
2128 UnaryExprOrTypeTrait Kind = E->getKind();
2129 const ASTContext &ASTCtx = Ctx.getASTContext();
2130
2131 if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
2132 QualType ArgType = E->getTypeOfArgument();
2133
2134 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
2135 // the result is the size of the referenced type."
2136 if (const auto *Ref = ArgType->getAs<ReferenceType>())
2137 ArgType = Ref->getPointeeType();
2138
2139 CharUnits Size;
2140 if (ArgType->isVoidType() || ArgType->isFunctionType())
2141 Size = CharUnits::One();
2142 else {
2143 if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2144 return this->emitInvalid(E);
2145
2146 if (Kind == UETT_SizeOf)
2147 Size = ASTCtx.getTypeSizeInChars(ArgType);
2148 else
2149 Size = ASTCtx.getTypeInfoDataSizeInChars(ArgType).Width;
2150 }
2151
2152 if (DiscardResult)
2153 return true;
2154
2155 return this->emitConst(Size.getQuantity(), E);
2156 }
2157
2158 if (Kind == UETT_CountOf) {
2159 QualType Ty = E->getTypeOfArgument();
2160 assert(Ty->isArrayType());
2161
2162 // We don't need to worry about array element qualifiers, so getting the
2163 // unsafe array type is fine.
2164 if (const auto *CAT =
2165 dyn_cast<ConstantArrayType>(Ty->getAsArrayTypeUnsafe())) {
2166 if (DiscardResult)
2167 return true;
2168 return this->emitConst(CAT->getSize(), E);
2169 }
2170
2171 assert(!Ty->isConstantSizeType());
2172
2173 // If it's a variable-length array type, we need to check whether it is a
2174 // multidimensional array. If so, we need to check the size expression of
2175 // the VLA to see if it's a constant size. If so, we can return that value.
2176 const auto *VAT = ASTCtx.getAsVariableArrayType(Ty);
2177 assert(VAT);
2178 if (VAT->getElementType()->isArrayType()) {
2179 std::optional<APSInt> Res =
2180 VAT->getSizeExpr()->getIntegerConstantExpr(ASTCtx);
2181 if (Res) {
2182 if (DiscardResult)
2183 return true;
2184 return this->emitConst(*Res, E);
2185 }
2186 }
2187 }
2188
2189 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
2190 CharUnits Size;
2191
2192 if (E->isArgumentType()) {
2193 QualType ArgType = E->getTypeOfArgument();
2194
2195 Size = AlignOfType(ArgType, ASTCtx, Kind);
2196 } else {
2197 // Argument is an expression, not a type.
2198 const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
2199
2200 // The kinds of expressions that we have special-case logic here for
2201 // should be kept up to date with the special checks for those
2202 // expressions in Sema.
2203
2204 // alignof decl is always accepted, even if it doesn't make sense: we
2205 // default to 1 in those cases.
2206 if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
2207 Size = ASTCtx.getDeclAlign(DRE->getDecl(),
2208 /*RefAsPointee*/ true);
2209 else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
2210 Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
2211 /*RefAsPointee*/ true);
2212 else
2213 Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
2214 }
2215
2216 if (DiscardResult)
2217 return true;
2218
2219 return this->emitConst(Size.getQuantity(), E);
2220 }
2221
2222 if (Kind == UETT_VectorElements) {
2223 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
2224 return this->emitConst(VT->getNumElements(), E);
2225 assert(E->getTypeOfArgument()->isSizelessVectorType());
2226 return this->emitSizelessVectorElementSize(E);
2227 }
2228
2229 if (Kind == UETT_VecStep) {
2230 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
2231 unsigned N = VT->getNumElements();
2232
2233 // The vec_step built-in functions that take a 3-component
2234 // vector return 4. (OpenCL 1.1 spec 6.11.12)
2235 if (N == 3)
2236 N = 4;
2237
2238 return this->emitConst(N, E);
2239 }
2240 return this->emitConst(1, E);
2241 }
2242
2243 if (Kind == UETT_OpenMPRequiredSimdAlign) {
2244 assert(E->isArgumentType());
2245 unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(E->getArgumentType());
2246
2247 return this->emitConst(ASTCtx.toCharUnitsFromBits(Bits).getQuantity(), E);
2248 }
2249
2250 if (Kind == UETT_PtrAuthTypeDiscriminator) {
2251 if (E->getArgumentType()->isDependentType())
2252 return this->emitInvalid(E);
2253
2254 return this->emitConst(
2255 const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator(
2256 E->getArgumentType()),
2257 E);
2258 }
2259
2260 return false;
2261 }
2262
2263 template <class Emitter>
VisitMemberExpr(const MemberExpr * E)2264 bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) {
2265 // 'Base.Member'
2266 const Expr *Base = E->getBase();
2267 const ValueDecl *Member = E->getMemberDecl();
2268
2269 if (DiscardResult)
2270 return this->discard(Base);
2271
2272 // MemberExprs are almost always lvalues, in which case we don't need to
2273 // do the load. But sometimes they aren't.
2274 const auto maybeLoadValue = [&]() -> bool {
2275 if (E->isGLValue())
2276 return true;
2277 if (std::optional<PrimType> T = classify(E))
2278 return this->emitLoadPop(*T, E);
2279 return false;
2280 };
2281
2282 if (const auto *VD = dyn_cast<VarDecl>(Member)) {
2283 // I am almost confident in saying that a var decl must be static
2284 // and therefore registered as a global variable. But this will probably
2285 // turn out to be wrong some time in the future, as always.
2286 if (auto GlobalIndex = P.getGlobal(VD))
2287 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybeLoadValue();
2288 return false;
2289 }
2290
2291 if (!isa<FieldDecl>(Member)) {
2292 if (!this->discard(Base) && !this->emitSideEffect(E))
2293 return false;
2294
2295 return this->visitDeclRef(Member, E);
2296 }
2297
2298 if (Initializing) {
2299 if (!this->delegate(Base))
2300 return false;
2301 } else {
2302 if (!this->visit(Base))
2303 return false;
2304 }
2305
2306 // Base above gives us a pointer on the stack.
2307 const auto *FD = cast<FieldDecl>(Member);
2308 const RecordDecl *RD = FD->getParent();
2309 const Record *R = getRecord(RD);
2310 if (!R)
2311 return false;
2312 const Record::Field *F = R->getField(FD);
2313 // Leave a pointer to the field on the stack.
2314 if (F->Decl->getType()->isReferenceType())
2315 return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
2316 return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
2317 }
2318
2319 template <class Emitter>
VisitArrayInitIndexExpr(const ArrayInitIndexExpr * E)2320 bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2321 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
2322 // stand-alone, e.g. via EvaluateAsInt().
2323 if (!ArrayIndex)
2324 return false;
2325 return this->emitConst(*ArrayIndex, E);
2326 }
2327
2328 template <class Emitter>
VisitArrayInitLoopExpr(const ArrayInitLoopExpr * E)2329 bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2330 assert(Initializing);
2331 assert(!DiscardResult);
2332
2333 // We visit the common opaque expression here once so we have its value
2334 // cached.
2335 if (!this->discard(E->getCommonExpr()))
2336 return false;
2337
2338 // TODO: This compiles to quite a lot of bytecode if the array is larger.
2339 // Investigate compiling this to a loop.
2340 const Expr *SubExpr = E->getSubExpr();
2341 size_t Size = E->getArraySize().getZExtValue();
2342 std::optional<PrimType> SubExprT = classify(SubExpr);
2343
2344 // So, every iteration, we execute an assignment here
2345 // where the LHS is on the stack (the target array)
2346 // and the RHS is our SubExpr.
2347 for (size_t I = 0; I != Size; ++I) {
2348 ArrayIndexScope<Emitter> IndexScope(this, I);
2349 BlockScope<Emitter> BS(this);
2350
2351 if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
2352 return false;
2353 if (!BS.destroyLocals())
2354 return false;
2355 }
2356 return true;
2357 }
2358
2359 template <class Emitter>
VisitOpaqueValueExpr(const OpaqueValueExpr * E)2360 bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2361 const Expr *SourceExpr = E->getSourceExpr();
2362 if (!SourceExpr)
2363 return false;
2364
2365 if (Initializing)
2366 return this->visitInitializer(SourceExpr);
2367
2368 PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
2369 if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
2370 return this->emitGetLocal(SubExprT, It->second, E);
2371
2372 if (!this->visit(SourceExpr))
2373 return false;
2374
2375 // At this point we either have the evaluated source expression or a pointer
2376 // to an object on the stack. We want to create a local variable that stores
2377 // this value.
2378 unsigned LocalIndex = allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
2379 if (!this->emitSetLocal(SubExprT, LocalIndex, E))
2380 return false;
2381
2382 // Here the local variable is created but the value is removed from the stack,
2383 // so we put it back if the caller needs it.
2384 if (!DiscardResult) {
2385 if (!this->emitGetLocal(SubExprT, LocalIndex, E))
2386 return false;
2387 }
2388
2389 // This is cleaned up when the local variable is destroyed.
2390 OpaqueExprs.insert({E, LocalIndex});
2391
2392 return true;
2393 }
2394
2395 template <class Emitter>
VisitAbstractConditionalOperator(const AbstractConditionalOperator * E)2396 bool Compiler<Emitter>::VisitAbstractConditionalOperator(
2397 const AbstractConditionalOperator *E) {
2398 const Expr *Condition = E->getCond();
2399 const Expr *TrueExpr = E->getTrueExpr();
2400 const Expr *FalseExpr = E->getFalseExpr();
2401
2402 auto visitChildExpr = [&](const Expr *E) -> bool {
2403 LocalScope<Emitter> S(this);
2404 if (!this->delegate(E))
2405 return false;
2406 return S.destroyLocals();
2407 };
2408
2409 if (std::optional<bool> BoolValue = getBoolValue(Condition)) {
2410 if (BoolValue)
2411 return visitChildExpr(TrueExpr);
2412 return visitChildExpr(FalseExpr);
2413 }
2414
2415 bool IsBcpCall = false;
2416 if (const auto *CE = dyn_cast<CallExpr>(Condition->IgnoreParenCasts());
2417 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) {
2418 IsBcpCall = true;
2419 }
2420
2421 LabelTy LabelEnd = this->getLabel(); // Label after the operator.
2422 LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
2423
2424 if (IsBcpCall) {
2425 if (!this->emitStartSpeculation(E))
2426 return false;
2427 }
2428
2429 if (!this->visitBool(Condition)) {
2430 // If the condition failed and we're checking for undefined behavior
2431 // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr
2432 // as well.
2433 if (this->checkingForUndefinedBehavior()) {
2434 if (!this->discard(TrueExpr))
2435 return false;
2436 if (!this->discard(FalseExpr))
2437 return false;
2438 }
2439 return false;
2440 }
2441
2442 if (!this->jumpFalse(LabelFalse))
2443 return false;
2444 if (!visitChildExpr(TrueExpr))
2445 return false;
2446 if (!this->jump(LabelEnd))
2447 return false;
2448 this->emitLabel(LabelFalse);
2449 if (!visitChildExpr(FalseExpr))
2450 return false;
2451 this->fallthrough(LabelEnd);
2452 this->emitLabel(LabelEnd);
2453
2454 if (IsBcpCall)
2455 return this->emitEndSpeculation(E);
2456 return true;
2457 }
2458
2459 template <class Emitter>
VisitStringLiteral(const StringLiteral * E)2460 bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) {
2461 if (DiscardResult)
2462 return true;
2463
2464 if (!Initializing) {
2465 unsigned StringIndex = P.createGlobalString(E);
2466 return this->emitGetPtrGlobal(StringIndex, E);
2467 }
2468
2469 // We are initializing an array on the stack.
2470 const ConstantArrayType *CAT =
2471 Ctx.getASTContext().getAsConstantArrayType(E->getType());
2472 assert(CAT && "a string literal that's not a constant array?");
2473
2474 // If the initializer string is too long, a diagnostic has already been
2475 // emitted. Read only the array length from the string literal.
2476 unsigned ArraySize = CAT->getZExtSize();
2477 unsigned N = std::min(ArraySize, E->getLength());
2478 unsigned CharWidth = E->getCharByteWidth();
2479
2480 for (unsigned I = 0; I != N; ++I) {
2481 uint32_t CodeUnit = E->getCodeUnit(I);
2482
2483 if (CharWidth == 1) {
2484 this->emitConstSint8(CodeUnit, E);
2485 this->emitInitElemSint8(I, E);
2486 } else if (CharWidth == 2) {
2487 this->emitConstUint16(CodeUnit, E);
2488 this->emitInitElemUint16(I, E);
2489 } else if (CharWidth == 4) {
2490 this->emitConstUint32(CodeUnit, E);
2491 this->emitInitElemUint32(I, E);
2492 } else {
2493 llvm_unreachable("unsupported character width");
2494 }
2495 }
2496
2497 // Fill up the rest of the char array with NUL bytes.
2498 for (unsigned I = N; I != ArraySize; ++I) {
2499 if (CharWidth == 1) {
2500 this->emitConstSint8(0, E);
2501 this->emitInitElemSint8(I, E);
2502 } else if (CharWidth == 2) {
2503 this->emitConstUint16(0, E);
2504 this->emitInitElemUint16(I, E);
2505 } else if (CharWidth == 4) {
2506 this->emitConstUint32(0, E);
2507 this->emitInitElemUint32(I, E);
2508 } else {
2509 llvm_unreachable("unsupported character width");
2510 }
2511 }
2512
2513 return true;
2514 }
2515
2516 template <class Emitter>
VisitObjCStringLiteral(const ObjCStringLiteral * E)2517 bool Compiler<Emitter>::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
2518 if (DiscardResult)
2519 return true;
2520 return this->emitDummyPtr(E, E);
2521 }
2522
2523 template <class Emitter>
VisitObjCEncodeExpr(const ObjCEncodeExpr * E)2524 bool Compiler<Emitter>::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2525 auto &A = Ctx.getASTContext();
2526 std::string Str;
2527 A.getObjCEncodingForType(E->getEncodedType(), Str);
2528 StringLiteral *SL =
2529 StringLiteral::Create(A, Str, StringLiteralKind::Ordinary,
2530 /*Pascal=*/false, E->getType(), E->getAtLoc());
2531 return this->delegate(SL);
2532 }
2533
2534 template <class Emitter>
VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr * E)2535 bool Compiler<Emitter>::VisitSYCLUniqueStableNameExpr(
2536 const SYCLUniqueStableNameExpr *E) {
2537 if (DiscardResult)
2538 return true;
2539
2540 assert(!Initializing);
2541
2542 auto &A = Ctx.getASTContext();
2543 std::string ResultStr = E->ComputeName(A);
2544
2545 QualType CharTy = A.CharTy.withConst();
2546 APInt Size(A.getTypeSize(A.getSizeType()), ResultStr.size() + 1);
2547 QualType ArrayTy = A.getConstantArrayType(CharTy, Size, nullptr,
2548 ArraySizeModifier::Normal, 0);
2549
2550 StringLiteral *SL =
2551 StringLiteral::Create(A, ResultStr, StringLiteralKind::Ordinary,
2552 /*Pascal=*/false, ArrayTy, E->getLocation());
2553
2554 unsigned StringIndex = P.createGlobalString(SL);
2555 return this->emitGetPtrGlobal(StringIndex, E);
2556 }
2557
2558 template <class Emitter>
VisitCharacterLiteral(const CharacterLiteral * E)2559 bool Compiler<Emitter>::VisitCharacterLiteral(const CharacterLiteral *E) {
2560 if (DiscardResult)
2561 return true;
2562 return this->emitConst(E->getValue(), E);
2563 }
2564
2565 template <class Emitter>
VisitFloatCompoundAssignOperator(const CompoundAssignOperator * E)2566 bool Compiler<Emitter>::VisitFloatCompoundAssignOperator(
2567 const CompoundAssignOperator *E) {
2568
2569 const Expr *LHS = E->getLHS();
2570 const Expr *RHS = E->getRHS();
2571 QualType LHSType = LHS->getType();
2572 QualType LHSComputationType = E->getComputationLHSType();
2573 QualType ResultType = E->getComputationResultType();
2574 std::optional<PrimType> LT = classify(LHSComputationType);
2575 std::optional<PrimType> RT = classify(ResultType);
2576
2577 assert(ResultType->isFloatingType());
2578
2579 if (!LT || !RT)
2580 return false;
2581
2582 PrimType LHST = classifyPrim(LHSType);
2583
2584 // C++17 onwards require that we evaluate the RHS first.
2585 // Compute RHS and save it in a temporary variable so we can
2586 // load it again later.
2587 if (!visit(RHS))
2588 return false;
2589
2590 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2591 if (!this->emitSetLocal(*RT, TempOffset, E))
2592 return false;
2593
2594 // First, visit LHS.
2595 if (!visit(LHS))
2596 return false;
2597 if (!this->emitLoad(LHST, E))
2598 return false;
2599
2600 // If necessary, convert LHS to its computation type.
2601 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
2602 LHSComputationType, E))
2603 return false;
2604
2605 // Now load RHS.
2606 if (!this->emitGetLocal(*RT, TempOffset, E))
2607 return false;
2608
2609 switch (E->getOpcode()) {
2610 case BO_AddAssign:
2611 if (!this->emitAddf(getFPOptions(E), E))
2612 return false;
2613 break;
2614 case BO_SubAssign:
2615 if (!this->emitSubf(getFPOptions(E), E))
2616 return false;
2617 break;
2618 case BO_MulAssign:
2619 if (!this->emitMulf(getFPOptions(E), E))
2620 return false;
2621 break;
2622 case BO_DivAssign:
2623 if (!this->emitDivf(getFPOptions(E), E))
2624 return false;
2625 break;
2626 default:
2627 return false;
2628 }
2629
2630 if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
2631 return false;
2632
2633 if (DiscardResult)
2634 return this->emitStorePop(LHST, E);
2635 return this->emitStore(LHST, E);
2636 }
2637
2638 template <class Emitter>
VisitPointerCompoundAssignOperator(const CompoundAssignOperator * E)2639 bool Compiler<Emitter>::VisitPointerCompoundAssignOperator(
2640 const CompoundAssignOperator *E) {
2641 BinaryOperatorKind Op = E->getOpcode();
2642 const Expr *LHS = E->getLHS();
2643 const Expr *RHS = E->getRHS();
2644 std::optional<PrimType> LT = classify(LHS->getType());
2645 std::optional<PrimType> RT = classify(RHS->getType());
2646
2647 if (Op != BO_AddAssign && Op != BO_SubAssign)
2648 return false;
2649
2650 if (!LT || !RT)
2651 return false;
2652
2653 if (!visit(LHS))
2654 return false;
2655
2656 if (!this->emitLoad(*LT, LHS))
2657 return false;
2658
2659 if (!visit(RHS))
2660 return false;
2661
2662 if (Op == BO_AddAssign) {
2663 if (!this->emitAddOffset(*RT, E))
2664 return false;
2665 } else {
2666 if (!this->emitSubOffset(*RT, E))
2667 return false;
2668 }
2669
2670 if (DiscardResult)
2671 return this->emitStorePopPtr(E);
2672 return this->emitStorePtr(E);
2673 }
2674
2675 template <class Emitter>
VisitCompoundAssignOperator(const CompoundAssignOperator * E)2676 bool Compiler<Emitter>::VisitCompoundAssignOperator(
2677 const CompoundAssignOperator *E) {
2678 if (E->getType()->isVectorType())
2679 return VisitVectorBinOp(E);
2680
2681 const Expr *LHS = E->getLHS();
2682 const Expr *RHS = E->getRHS();
2683 std::optional<PrimType> LHSComputationT =
2684 classify(E->getComputationLHSType());
2685 std::optional<PrimType> LT = classify(LHS->getType());
2686 std::optional<PrimType> RT = classify(RHS->getType());
2687 std::optional<PrimType> ResultT = classify(E->getType());
2688
2689 if (!Ctx.getLangOpts().CPlusPlus14)
2690 return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
2691
2692 if (!LT || !RT || !ResultT || !LHSComputationT)
2693 return false;
2694
2695 // Handle floating point operations separately here, since they
2696 // require special care.
2697
2698 if (ResultT == PT_Float || RT == PT_Float)
2699 return VisitFloatCompoundAssignOperator(E);
2700
2701 if (E->getType()->isPointerType())
2702 return VisitPointerCompoundAssignOperator(E);
2703
2704 assert(!E->getType()->isPointerType() && "Handled above");
2705 assert(!E->getType()->isFloatingType() && "Handled above");
2706
2707 // C++17 onwards require that we evaluate the RHS first.
2708 // Compute RHS and save it in a temporary variable so we can
2709 // load it again later.
2710 // FIXME: Compound assignments are unsequenced in C, so we might
2711 // have to figure out how to reject them.
2712 if (!visit(RHS))
2713 return false;
2714
2715 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2716
2717 if (!this->emitSetLocal(*RT, TempOffset, E))
2718 return false;
2719
2720 // Get LHS pointer, load its value and cast it to the
2721 // computation type if necessary.
2722 if (!visit(LHS))
2723 return false;
2724 if (!this->emitLoad(*LT, E))
2725 return false;
2726 if (LT != LHSComputationT) {
2727 if (!this->emitCast(*LT, *LHSComputationT, E))
2728 return false;
2729 }
2730
2731 // Get the RHS value on the stack.
2732 if (!this->emitGetLocal(*RT, TempOffset, E))
2733 return false;
2734
2735 // Perform operation.
2736 switch (E->getOpcode()) {
2737 case BO_AddAssign:
2738 if (!this->emitAdd(*LHSComputationT, E))
2739 return false;
2740 break;
2741 case BO_SubAssign:
2742 if (!this->emitSub(*LHSComputationT, E))
2743 return false;
2744 break;
2745 case BO_MulAssign:
2746 if (!this->emitMul(*LHSComputationT, E))
2747 return false;
2748 break;
2749 case BO_DivAssign:
2750 if (!this->emitDiv(*LHSComputationT, E))
2751 return false;
2752 break;
2753 case BO_RemAssign:
2754 if (!this->emitRem(*LHSComputationT, E))
2755 return false;
2756 break;
2757 case BO_ShlAssign:
2758 if (!this->emitShl(*LHSComputationT, *RT, E))
2759 return false;
2760 break;
2761 case BO_ShrAssign:
2762 if (!this->emitShr(*LHSComputationT, *RT, E))
2763 return false;
2764 break;
2765 case BO_AndAssign:
2766 if (!this->emitBitAnd(*LHSComputationT, E))
2767 return false;
2768 break;
2769 case BO_XorAssign:
2770 if (!this->emitBitXor(*LHSComputationT, E))
2771 return false;
2772 break;
2773 case BO_OrAssign:
2774 if (!this->emitBitOr(*LHSComputationT, E))
2775 return false;
2776 break;
2777 default:
2778 llvm_unreachable("Unimplemented compound assign operator");
2779 }
2780
2781 // And now cast from LHSComputationT to ResultT.
2782 if (ResultT != LHSComputationT) {
2783 if (!this->emitCast(*LHSComputationT, *ResultT, E))
2784 return false;
2785 }
2786
2787 // And store the result in LHS.
2788 if (DiscardResult) {
2789 if (LHS->refersToBitField())
2790 return this->emitStoreBitFieldPop(*ResultT, E);
2791 return this->emitStorePop(*ResultT, E);
2792 }
2793 if (LHS->refersToBitField())
2794 return this->emitStoreBitField(*ResultT, E);
2795 return this->emitStore(*ResultT, E);
2796 }
2797
2798 template <class Emitter>
VisitExprWithCleanups(const ExprWithCleanups * E)2799 bool Compiler<Emitter>::VisitExprWithCleanups(const ExprWithCleanups *E) {
2800 LocalScope<Emitter> ES(this);
2801 const Expr *SubExpr = E->getSubExpr();
2802
2803 return this->delegate(SubExpr) && ES.destroyLocals(E);
2804 }
2805
2806 template <class Emitter>
VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * E)2807 bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
2808 const MaterializeTemporaryExpr *E) {
2809 const Expr *SubExpr = E->getSubExpr();
2810
2811 if (Initializing) {
2812 // We already have a value, just initialize that.
2813 return this->delegate(SubExpr);
2814 }
2815 // If we don't end up using the materialized temporary anyway, don't
2816 // bother creating it.
2817 if (DiscardResult)
2818 return this->discard(SubExpr);
2819
2820 // When we're initializing a global variable *or* the storage duration of
2821 // the temporary is explicitly static, create a global variable.
2822 std::optional<PrimType> SubExprT = classify(SubExpr);
2823 bool IsStatic = E->getStorageDuration() == SD_Static;
2824 if (IsStatic) {
2825 std::optional<unsigned> GlobalIndex = P.createGlobal(E);
2826 if (!GlobalIndex)
2827 return false;
2828
2829 const LifetimeExtendedTemporaryDecl *TempDecl =
2830 E->getLifetimeExtendedTemporaryDecl();
2831 if (IsStatic)
2832 assert(TempDecl);
2833
2834 if (SubExprT) {
2835 if (!this->visit(SubExpr))
2836 return false;
2837 if (IsStatic) {
2838 if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
2839 return false;
2840 } else {
2841 if (!this->emitInitGlobal(*SubExprT, *GlobalIndex, E))
2842 return false;
2843 }
2844 return this->emitGetPtrGlobal(*GlobalIndex, E);
2845 }
2846
2847 if (!this->checkLiteralType(SubExpr))
2848 return false;
2849 // Non-primitive values.
2850 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2851 return false;
2852 if (!this->visitInitializer(SubExpr))
2853 return false;
2854 if (IsStatic)
2855 return this->emitInitGlobalTempComp(TempDecl, E);
2856 return true;
2857 }
2858
2859 // For everyhing else, use local variables.
2860 if (SubExprT) {
2861 bool IsConst = SubExpr->getType().isConstQualified();
2862 unsigned LocalIndex =
2863 allocateLocalPrimitive(E, *SubExprT, IsConst, E->getExtendingDecl());
2864 if (!this->visit(SubExpr))
2865 return false;
2866 if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
2867 return false;
2868 return this->emitGetPtrLocal(LocalIndex, E);
2869 } else {
2870
2871 if (!this->checkLiteralType(SubExpr))
2872 return false;
2873
2874 const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
2875 if (std::optional<unsigned> LocalIndex =
2876 allocateLocal(E, Inner->getType(), E->getExtendingDecl())) {
2877 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2878 if (!this->emitGetPtrLocal(*LocalIndex, E))
2879 return false;
2880 return this->visitInitializer(SubExpr) && this->emitFinishInit(E);
2881 }
2882 }
2883 return false;
2884 }
2885
2886 template <class Emitter>
VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr * E)2887 bool Compiler<Emitter>::VisitCXXBindTemporaryExpr(
2888 const CXXBindTemporaryExpr *E) {
2889 const Expr *SubExpr = E->getSubExpr();
2890
2891 if (Initializing)
2892 return this->delegate(SubExpr);
2893
2894 // Make sure we create a temporary even if we're discarding, since that will
2895 // make sure we will also call the destructor.
2896
2897 if (!this->visit(SubExpr))
2898 return false;
2899
2900 if (DiscardResult)
2901 return this->emitPopPtr(E);
2902 return true;
2903 }
2904
2905 template <class Emitter>
VisitCompoundLiteralExpr(const CompoundLiteralExpr * E)2906 bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2907 const Expr *Init = E->getInitializer();
2908 if (DiscardResult)
2909 return this->discard(Init);
2910
2911 if (Initializing) {
2912 // We already have a value, just initialize that.
2913 return this->visitInitializer(Init) && this->emitFinishInit(E);
2914 }
2915
2916 std::optional<PrimType> T = classify(E->getType());
2917 if (E->isFileScope()) {
2918 // Avoid creating a variable if this is a primitive RValue anyway.
2919 if (T && !E->isLValue())
2920 return this->delegate(Init);
2921
2922 if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
2923 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2924 return false;
2925
2926 if (T) {
2927 if (!this->visit(Init))
2928 return false;
2929 return this->emitInitGlobal(*T, *GlobalIndex, E);
2930 }
2931
2932 return this->visitInitializer(Init) && this->emitFinishInit(E);
2933 }
2934
2935 return false;
2936 }
2937
2938 // Otherwise, use a local variable.
2939 if (T && !E->isLValue()) {
2940 // For primitive types, we just visit the initializer.
2941 return this->delegate(Init);
2942 }
2943
2944 unsigned LocalIndex;
2945 if (T)
2946 LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
2947 else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
2948 LocalIndex = *MaybeIndex;
2949 else
2950 return false;
2951
2952 if (!this->emitGetPtrLocal(LocalIndex, E))
2953 return false;
2954
2955 if (T)
2956 return this->visit(Init) && this->emitInit(*T, E);
2957 return this->visitInitializer(Init) && this->emitFinishInit(E);
2958 }
2959
2960 template <class Emitter>
VisitTypeTraitExpr(const TypeTraitExpr * E)2961 bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) {
2962 if (DiscardResult)
2963 return true;
2964 if (E->isStoredAsBoolean()) {
2965 if (E->getType()->isBooleanType())
2966 return this->emitConstBool(E->getBoolValue(), E);
2967 return this->emitConst(E->getBoolValue(), E);
2968 }
2969 PrimType T = classifyPrim(E->getType());
2970 return this->visitAPValue(E->getAPValue(), T, E);
2971 }
2972
2973 template <class Emitter>
VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr * E)2974 bool Compiler<Emitter>::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
2975 if (DiscardResult)
2976 return true;
2977 return this->emitConst(E->getValue(), E);
2978 }
2979
2980 template <class Emitter>
VisitLambdaExpr(const LambdaExpr * E)2981 bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
2982 if (DiscardResult)
2983 return true;
2984
2985 assert(Initializing);
2986 const Record *R = P.getOrCreateRecord(E->getLambdaClass());
2987 if (!R)
2988 return false;
2989
2990 auto *CaptureInitIt = E->capture_init_begin();
2991 // Initialize all fields (which represent lambda captures) of the
2992 // record with their initializers.
2993 for (const Record::Field &F : R->fields()) {
2994 const Expr *Init = *CaptureInitIt;
2995 if (!Init || Init->containsErrors())
2996 continue;
2997 ++CaptureInitIt;
2998
2999 if (std::optional<PrimType> T = classify(Init)) {
3000 if (!this->visit(Init))
3001 return false;
3002
3003 if (!this->emitInitField(*T, F.Offset, E))
3004 return false;
3005 } else {
3006 if (!this->emitGetPtrField(F.Offset, E))
3007 return false;
3008
3009 if (!this->visitInitializer(Init))
3010 return false;
3011
3012 if (!this->emitPopPtr(E))
3013 return false;
3014 }
3015 }
3016
3017 return true;
3018 }
3019
3020 template <class Emitter>
VisitPredefinedExpr(const PredefinedExpr * E)3021 bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
3022 if (DiscardResult)
3023 return true;
3024
3025 if (!Initializing) {
3026 unsigned StringIndex = P.createGlobalString(E->getFunctionName(), E);
3027 return this->emitGetPtrGlobal(StringIndex, E);
3028 }
3029
3030 return this->delegate(E->getFunctionName());
3031 }
3032
3033 template <class Emitter>
VisitCXXThrowExpr(const CXXThrowExpr * E)3034 bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
3035 if (E->getSubExpr() && !this->discard(E->getSubExpr()))
3036 return false;
3037
3038 return this->emitInvalid(E);
3039 }
3040
3041 template <class Emitter>
VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr * E)3042 bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
3043 const CXXReinterpretCastExpr *E) {
3044 const Expr *SubExpr = E->getSubExpr();
3045
3046 std::optional<PrimType> FromT = classify(SubExpr);
3047 std::optional<PrimType> ToT = classify(E);
3048
3049 if (!FromT || !ToT)
3050 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
3051
3052 if (FromT == PT_Ptr || ToT == PT_Ptr) {
3053 // Both types could be PT_Ptr because their expressions are glvalues.
3054 std::optional<PrimType> PointeeFromT;
3055 if (SubExpr->getType()->isPointerOrReferenceType())
3056 PointeeFromT = classify(SubExpr->getType()->getPointeeType());
3057 else
3058 PointeeFromT = classify(SubExpr->getType());
3059
3060 std::optional<PrimType> PointeeToT;
3061 if (E->getType()->isPointerOrReferenceType())
3062 PointeeToT = classify(E->getType()->getPointeeType());
3063 else
3064 PointeeToT = classify(E->getType());
3065
3066 bool Fatal = true;
3067 if (PointeeToT && PointeeFromT) {
3068 if (isIntegralType(*PointeeFromT) && isIntegralType(*PointeeToT))
3069 Fatal = false;
3070 } else {
3071 Fatal = SubExpr->getType().getTypePtr() != E->getType().getTypePtr();
3072 }
3073
3074 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3075 return false;
3076
3077 if (E->getCastKind() == CK_LValueBitCast)
3078 return this->delegate(SubExpr);
3079 return this->VisitCastExpr(E);
3080 }
3081
3082 // Try to actually do the cast.
3083 bool Fatal = (ToT != FromT);
3084 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3085 return false;
3086
3087 return this->VisitCastExpr(E);
3088 }
3089
3090 template <class Emitter>
VisitCXXDynamicCastExpr(const CXXDynamicCastExpr * E)3091 bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
3092
3093 if (!Ctx.getLangOpts().CPlusPlus20) {
3094 if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E))
3095 return false;
3096 }
3097
3098 return this->VisitCastExpr(E);
3099 }
3100
3101 template <class Emitter>
VisitCXXNoexceptExpr(const CXXNoexceptExpr * E)3102 bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
3103 assert(E->getType()->isBooleanType());
3104
3105 if (DiscardResult)
3106 return true;
3107 return this->emitConstBool(E->getValue(), E);
3108 }
3109
3110 template <class Emitter>
VisitCXXConstructExpr(const CXXConstructExpr * E)3111 bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3112 QualType T = E->getType();
3113 assert(!classify(T));
3114
3115 if (T->isRecordType()) {
3116 const CXXConstructorDecl *Ctor = E->getConstructor();
3117
3118 // Trivial copy/move constructor. Avoid copy.
3119 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
3120 Ctor->isTrivial() &&
3121 E->getArg(0)->isTemporaryObject(Ctx.getASTContext(),
3122 T->getAsCXXRecordDecl()))
3123 return this->visitInitializer(E->getArg(0));
3124
3125 // If we're discarding a construct expression, we still need
3126 // to allocate a variable and call the constructor and destructor.
3127 if (DiscardResult) {
3128 if (Ctor->isTrivial())
3129 return true;
3130 assert(!Initializing);
3131 std::optional<unsigned> LocalIndex = allocateLocal(E);
3132
3133 if (!LocalIndex)
3134 return false;
3135
3136 if (!this->emitGetPtrLocal(*LocalIndex, E))
3137 return false;
3138 }
3139
3140 // Zero initialization.
3141 if (E->requiresZeroInitialization()) {
3142 const Record *R = getRecord(E->getType());
3143
3144 if (!this->visitZeroRecordInitializer(R, E))
3145 return false;
3146
3147 // If the constructor is trivial anyway, we're done.
3148 if (Ctor->isTrivial())
3149 return true;
3150 }
3151
3152 const Function *Func = getFunction(Ctor);
3153
3154 if (!Func)
3155 return false;
3156
3157 assert(Func->hasThisPointer());
3158 assert(!Func->hasRVO());
3159
3160 // The This pointer is already on the stack because this is an initializer,
3161 // but we need to dup() so the call() below has its own copy.
3162 if (!this->emitDupPtr(E))
3163 return false;
3164
3165 // Constructor arguments.
3166 for (const auto *Arg : E->arguments()) {
3167 if (!this->visit(Arg))
3168 return false;
3169 }
3170
3171 if (Func->isVariadic()) {
3172 uint32_t VarArgSize = 0;
3173 unsigned NumParams = Func->getNumWrittenParams();
3174 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
3175 VarArgSize +=
3176 align(primSize(classify(E->getArg(I)->getType()).value_or(PT_Ptr)));
3177 }
3178 if (!this->emitCallVar(Func, VarArgSize, E))
3179 return false;
3180 } else {
3181 if (!this->emitCall(Func, 0, E)) {
3182 // When discarding, we don't need the result anyway, so clean up
3183 // the instance dup we did earlier in case surrounding code wants
3184 // to keep evaluating.
3185 if (DiscardResult)
3186 (void)this->emitPopPtr(E);
3187 return false;
3188 }
3189 }
3190
3191 if (DiscardResult)
3192 return this->emitPopPtr(E);
3193 return this->emitFinishInit(E);
3194 }
3195
3196 if (T->isArrayType()) {
3197 const ConstantArrayType *CAT =
3198 Ctx.getASTContext().getAsConstantArrayType(E->getType());
3199 if (!CAT)
3200 return false;
3201
3202 size_t NumElems = CAT->getZExtSize();
3203 const Function *Func = getFunction(E->getConstructor());
3204 if (!Func)
3205 return false;
3206
3207 // FIXME(perf): We're calling the constructor once per array element here,
3208 // in the old intepreter we had a special-case for trivial constructors.
3209 for (size_t I = 0; I != NumElems; ++I) {
3210 if (!this->emitConstUint64(I, E))
3211 return false;
3212 if (!this->emitArrayElemPtrUint64(E))
3213 return false;
3214
3215 // Constructor arguments.
3216 for (const auto *Arg : E->arguments()) {
3217 if (!this->visit(Arg))
3218 return false;
3219 }
3220
3221 if (!this->emitCall(Func, 0, E))
3222 return false;
3223 }
3224 return true;
3225 }
3226
3227 return false;
3228 }
3229
3230 template <class Emitter>
VisitSourceLocExpr(const SourceLocExpr * E)3231 bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
3232 if (DiscardResult)
3233 return true;
3234
3235 const APValue Val =
3236 E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
3237
3238 // Things like __builtin_LINE().
3239 if (E->getType()->isIntegerType()) {
3240 assert(Val.isInt());
3241 const APSInt &I = Val.getInt();
3242 return this->emitConst(I, E);
3243 }
3244 // Otherwise, the APValue is an LValue, with only one element.
3245 // Theoretically, we don't need the APValue at all of course.
3246 assert(E->getType()->isPointerType());
3247 assert(Val.isLValue());
3248 const APValue::LValueBase &Base = Val.getLValueBase();
3249 if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
3250 return this->visit(LValueExpr);
3251
3252 // Otherwise, we have a decl (which is the case for
3253 // __builtin_source_location).
3254 assert(Base.is<const ValueDecl *>());
3255 assert(Val.getLValuePath().size() == 0);
3256 const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
3257 assert(BaseDecl);
3258
3259 auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
3260
3261 std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(UGCD);
3262 if (!GlobalIndex)
3263 return false;
3264
3265 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3266 return false;
3267
3268 const Record *R = getRecord(E->getType());
3269 const APValue &V = UGCD->getValue();
3270 for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
3271 const Record::Field *F = R->getField(I);
3272 const APValue &FieldValue = V.getStructField(I);
3273
3274 PrimType FieldT = classifyPrim(F->Decl->getType());
3275
3276 if (!this->visitAPValue(FieldValue, FieldT, E))
3277 return false;
3278 if (!this->emitInitField(FieldT, F->Offset, E))
3279 return false;
3280 }
3281
3282 // Leave the pointer to the global on the stack.
3283 return true;
3284 }
3285
3286 template <class Emitter>
VisitOffsetOfExpr(const OffsetOfExpr * E)3287 bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
3288 unsigned N = E->getNumComponents();
3289 if (N == 0)
3290 return false;
3291
3292 for (unsigned I = 0; I != N; ++I) {
3293 const OffsetOfNode &Node = E->getComponent(I);
3294 if (Node.getKind() == OffsetOfNode::Array) {
3295 const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
3296 PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
3297
3298 if (DiscardResult) {
3299 if (!this->discard(ArrayIndexExpr))
3300 return false;
3301 continue;
3302 }
3303
3304 if (!this->visit(ArrayIndexExpr))
3305 return false;
3306 // Cast to Sint64.
3307 if (IndexT != PT_Sint64) {
3308 if (!this->emitCast(IndexT, PT_Sint64, E))
3309 return false;
3310 }
3311 }
3312 }
3313
3314 if (DiscardResult)
3315 return true;
3316
3317 PrimType T = classifyPrim(E->getType());
3318 return this->emitOffsetOf(T, E, E);
3319 }
3320
3321 template <class Emitter>
VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr * E)3322 bool Compiler<Emitter>::VisitCXXScalarValueInitExpr(
3323 const CXXScalarValueInitExpr *E) {
3324 QualType Ty = E->getType();
3325
3326 if (DiscardResult || Ty->isVoidType())
3327 return true;
3328
3329 if (std::optional<PrimType> T = classify(Ty))
3330 return this->visitZeroInitializer(*T, Ty, E);
3331
3332 if (const auto *CT = Ty->getAs<ComplexType>()) {
3333 if (!Initializing) {
3334 std::optional<unsigned> LocalIndex = allocateLocal(E);
3335 if (!LocalIndex)
3336 return false;
3337 if (!this->emitGetPtrLocal(*LocalIndex, E))
3338 return false;
3339 }
3340
3341 // Initialize both fields to 0.
3342 QualType ElemQT = CT->getElementType();
3343 PrimType ElemT = classifyPrim(ElemQT);
3344
3345 for (unsigned I = 0; I != 2; ++I) {
3346 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3347 return false;
3348 if (!this->emitInitElem(ElemT, I, E))
3349 return false;
3350 }
3351 return true;
3352 }
3353
3354 if (const auto *VT = Ty->getAs<VectorType>()) {
3355 // FIXME: Code duplication with the _Complex case above.
3356 if (!Initializing) {
3357 std::optional<unsigned> LocalIndex = allocateLocal(E);
3358 if (!LocalIndex)
3359 return false;
3360 if (!this->emitGetPtrLocal(*LocalIndex, E))
3361 return false;
3362 }
3363
3364 // Initialize all fields to 0.
3365 QualType ElemQT = VT->getElementType();
3366 PrimType ElemT = classifyPrim(ElemQT);
3367
3368 for (unsigned I = 0, N = VT->getNumElements(); I != N; ++I) {
3369 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3370 return false;
3371 if (!this->emitInitElem(ElemT, I, E))
3372 return false;
3373 }
3374 return true;
3375 }
3376
3377 return false;
3378 }
3379
3380 template <class Emitter>
VisitSizeOfPackExpr(const SizeOfPackExpr * E)3381 bool Compiler<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
3382 return this->emitConst(E->getPackLength(), E);
3383 }
3384
3385 template <class Emitter>
VisitGenericSelectionExpr(const GenericSelectionExpr * E)3386 bool Compiler<Emitter>::VisitGenericSelectionExpr(
3387 const GenericSelectionExpr *E) {
3388 return this->delegate(E->getResultExpr());
3389 }
3390
3391 template <class Emitter>
VisitChooseExpr(const ChooseExpr * E)3392 bool Compiler<Emitter>::VisitChooseExpr(const ChooseExpr *E) {
3393 return this->delegate(E->getChosenSubExpr());
3394 }
3395
3396 template <class Emitter>
VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr * E)3397 bool Compiler<Emitter>::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
3398 if (DiscardResult)
3399 return true;
3400
3401 return this->emitConst(E->getValue(), E);
3402 }
3403
3404 template <class Emitter>
VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr * E)3405 bool Compiler<Emitter>::VisitCXXInheritedCtorInitExpr(
3406 const CXXInheritedCtorInitExpr *E) {
3407 const CXXConstructorDecl *Ctor = E->getConstructor();
3408 assert(!Ctor->isTrivial() &&
3409 "Trivial CXXInheritedCtorInitExpr, implement. (possible?)");
3410 const Function *F = this->getFunction(Ctor);
3411 assert(F);
3412 assert(!F->hasRVO());
3413 assert(F->hasThisPointer());
3414
3415 if (!this->emitDupPtr(SourceInfo{}))
3416 return false;
3417
3418 // Forward all arguments of the current function (which should be a
3419 // constructor itself) to the inherited ctor.
3420 // This is necessary because the calling code has pushed the pointer
3421 // of the correct base for us already, but the arguments need
3422 // to come after.
3423 unsigned Offset = align(primSize(PT_Ptr)); // instance pointer.
3424 for (const ParmVarDecl *PD : Ctor->parameters()) {
3425 PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
3426
3427 if (!this->emitGetParam(PT, Offset, E))
3428 return false;
3429 Offset += align(primSize(PT));
3430 }
3431
3432 return this->emitCall(F, 0, E);
3433 }
3434
3435 // FIXME: This function has become rather unwieldy, especially
3436 // the part where we initialize an array allocation of dynamic size.
3437 template <class Emitter>
VisitCXXNewExpr(const CXXNewExpr * E)3438 bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
3439 assert(classifyPrim(E->getType()) == PT_Ptr);
3440 const Expr *Init = E->getInitializer();
3441 QualType ElementType = E->getAllocatedType();
3442 std::optional<PrimType> ElemT = classify(ElementType);
3443 unsigned PlacementArgs = E->getNumPlacementArgs();
3444 const FunctionDecl *OperatorNew = E->getOperatorNew();
3445 const Expr *PlacementDest = nullptr;
3446 bool IsNoThrow = false;
3447
3448 if (PlacementArgs != 0) {
3449 // FIXME: There is no restriction on this, but it's not clear that any
3450 // other form makes any sense. We get here for cases such as:
3451 //
3452 // new (std::align_val_t{N}) X(int)
3453 //
3454 // (which should presumably be valid only if N is a multiple of
3455 // alignof(int), and in any case can't be deallocated unless N is
3456 // alignof(X) and X has new-extended alignment).
3457 if (PlacementArgs == 1) {
3458 const Expr *Arg1 = E->getPlacementArg(0);
3459 if (OperatorNew->isReservedGlobalPlacementOperator()) {
3460 if (!this->emitCheckPlacementNew(E, E))
3461 return false;
3462 PlacementDest = Arg1;
3463 } else if (
3464 Arg1->getType()->isNothrowT() &&
3465 OperatorNew
3466 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
3467 if (!this->discard(Arg1))
3468 return false;
3469 IsNoThrow = true;
3470 } else {
3471 // Any other placement list is invalid. This includes a user-declared
3472 // allocation function taking std::nothrow_t, e.g. by value.
3473 return this->emitInvalidNewDeleteExpr(E, E);
3474 }
3475 } else {
3476 // Always invalid.
3477 return this->emitInvalid(E);
3478 }
3479 } else if (!OperatorNew
3480 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
3481 return this->emitInvalidNewDeleteExpr(E, E);
3482
3483 const Descriptor *Desc;
3484 if (!PlacementDest) {
3485 if (ElemT) {
3486 if (E->isArray())
3487 Desc = nullptr; // We're not going to use it in this case.
3488 else
3489 Desc = P.createDescriptor(E, *ElemT, /*SourceTy=*/nullptr,
3490 Descriptor::InlineDescMD);
3491 } else {
3492 Desc = P.createDescriptor(
3493 E, ElementType.getTypePtr(),
3494 E->isArray() ? std::nullopt : Descriptor::InlineDescMD,
3495 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false,
3496 /*IsVolatile=*/false, Init);
3497 }
3498 }
3499
3500 if (E->isArray()) {
3501 std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
3502 if (!ArraySizeExpr)
3503 return false;
3504
3505 const Expr *Stripped = *ArraySizeExpr;
3506 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
3507 Stripped = ICE->getSubExpr())
3508 if (ICE->getCastKind() != CK_NoOp &&
3509 ICE->getCastKind() != CK_IntegralCast)
3510 break;
3511
3512 PrimType SizeT = classifyPrim(Stripped->getType());
3513
3514 // Save evaluated array size to a variable.
3515 unsigned ArrayLen =
3516 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
3517 if (!this->visit(Stripped))
3518 return false;
3519 if (!this->emitSetLocal(SizeT, ArrayLen, E))
3520 return false;
3521
3522 if (PlacementDest) {
3523 if (!this->visit(PlacementDest))
3524 return false;
3525 if (!this->emitStartLifetime(E))
3526 return false;
3527 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3528 return false;
3529 if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E))
3530 return false;
3531 } else {
3532 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3533 return false;
3534
3535 if (ElemT) {
3536 // N primitive elements.
3537 if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
3538 return false;
3539 } else {
3540 // N Composite elements.
3541 if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
3542 return false;
3543 }
3544 }
3545
3546 if (Init) {
3547 QualType InitType = Init->getType();
3548 size_t StaticInitElems = 0;
3549 const Expr *DynamicInit = nullptr;
3550 if (const ConstantArrayType *CAT =
3551 Ctx.getASTContext().getAsConstantArrayType(InitType)) {
3552 StaticInitElems = CAT->getZExtSize();
3553 if (!this->visitInitializer(Init))
3554 return false;
3555
3556 if (const auto *ILE = dyn_cast<InitListExpr>(Init);
3557 ILE && ILE->hasArrayFiller())
3558 DynamicInit = ILE->getArrayFiller();
3559 }
3560
3561 // The initializer initializes a certain number of elements, S.
3562 // However, the complete number of elements, N, might be larger than that.
3563 // In this case, we need to get an initializer for the remaining elements.
3564 // There are to cases:
3565 // 1) For the form 'new Struct[n];', the initializer is a
3566 // CXXConstructExpr and its type is an IncompleteArrayType.
3567 // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an
3568 // InitListExpr and the initializer for the remaining elements
3569 // is the array filler.
3570
3571 if (DynamicInit || InitType->isIncompleteArrayType()) {
3572 const Function *CtorFunc = nullptr;
3573 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
3574 CtorFunc = getFunction(CE->getConstructor());
3575 if (!CtorFunc)
3576 return false;
3577 } else if (!DynamicInit)
3578 DynamicInit = Init;
3579
3580 LabelTy EndLabel = this->getLabel();
3581 LabelTy StartLabel = this->getLabel();
3582
3583 // In the nothrow case, the alloc above might have returned nullptr.
3584 // Don't call any constructors that case.
3585 if (IsNoThrow) {
3586 if (!this->emitDupPtr(E))
3587 return false;
3588 if (!this->emitNullPtr(0, nullptr, E))
3589 return false;
3590 if (!this->emitEQPtr(E))
3591 return false;
3592 if (!this->jumpTrue(EndLabel))
3593 return false;
3594 }
3595
3596 // Create loop variables.
3597 unsigned Iter =
3598 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
3599 if (!this->emitConst(StaticInitElems, SizeT, E))
3600 return false;
3601 if (!this->emitSetLocal(SizeT, Iter, E))
3602 return false;
3603
3604 this->fallthrough(StartLabel);
3605 this->emitLabel(StartLabel);
3606 // Condition. Iter < ArrayLen?
3607 if (!this->emitGetLocal(SizeT, Iter, E))
3608 return false;
3609 if (!this->emitGetLocal(SizeT, ArrayLen, E))
3610 return false;
3611 if (!this->emitLT(SizeT, E))
3612 return false;
3613 if (!this->jumpFalse(EndLabel))
3614 return false;
3615
3616 // Pointer to the allocated array is already on the stack.
3617 if (!this->emitGetLocal(SizeT, Iter, E))
3618 return false;
3619 if (!this->emitArrayElemPtr(SizeT, E))
3620 return false;
3621
3622 if (isa_and_nonnull<ImplicitValueInitExpr>(DynamicInit) &&
3623 DynamicInit->getType()->isArrayType()) {
3624 QualType ElemType =
3625 DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType();
3626 PrimType InitT = classifyPrim(ElemType);
3627 if (!this->visitZeroInitializer(InitT, ElemType, E))
3628 return false;
3629 if (!this->emitStorePop(InitT, E))
3630 return false;
3631 } else if (DynamicInit) {
3632 if (std::optional<PrimType> InitT = classify(DynamicInit)) {
3633 if (!this->visit(DynamicInit))
3634 return false;
3635 if (!this->emitStorePop(*InitT, E))
3636 return false;
3637 } else {
3638 if (!this->visitInitializer(DynamicInit))
3639 return false;
3640 if (!this->emitPopPtr(E))
3641 return false;
3642 }
3643 } else {
3644 assert(CtorFunc);
3645 if (!this->emitCall(CtorFunc, 0, E))
3646 return false;
3647 }
3648
3649 // ++Iter;
3650 if (!this->emitGetPtrLocal(Iter, E))
3651 return false;
3652 if (!this->emitIncPop(SizeT, false, E))
3653 return false;
3654
3655 if (!this->jump(StartLabel))
3656 return false;
3657
3658 this->fallthrough(EndLabel);
3659 this->emitLabel(EndLabel);
3660 }
3661 }
3662 } else { // Non-array.
3663 if (PlacementDest) {
3664 if (!this->visit(PlacementDest))
3665 return false;
3666 if (!this->emitStartLifetime(E))
3667 return false;
3668 if (!this->emitCheckNewTypeMismatch(E, E))
3669 return false;
3670 } else {
3671 // Allocate just one element.
3672 if (!this->emitAlloc(Desc, E))
3673 return false;
3674 }
3675
3676 if (Init) {
3677 if (ElemT) {
3678 if (!this->visit(Init))
3679 return false;
3680
3681 if (!this->emitInit(*ElemT, E))
3682 return false;
3683 } else {
3684 // Composite.
3685 if (!this->visitInitializer(Init))
3686 return false;
3687 }
3688 }
3689 }
3690
3691 if (DiscardResult)
3692 return this->emitPopPtr(E);
3693
3694 return true;
3695 }
3696
3697 template <class Emitter>
VisitCXXDeleteExpr(const CXXDeleteExpr * E)3698 bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
3699 const Expr *Arg = E->getArgument();
3700
3701 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
3702
3703 if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
3704 return this->emitInvalidNewDeleteExpr(E, E);
3705
3706 // Arg must be an lvalue.
3707 if (!this->visit(Arg))
3708 return false;
3709
3710 return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
3711 }
3712
3713 template <class Emitter>
VisitBlockExpr(const BlockExpr * E)3714 bool Compiler<Emitter>::VisitBlockExpr(const BlockExpr *E) {
3715 if (DiscardResult)
3716 return true;
3717
3718 const Function *Func = nullptr;
3719 if (auto F = Ctx.getOrCreateObjCBlock(E))
3720 Func = F;
3721
3722 if (!Func)
3723 return false;
3724 return this->emitGetFnPtr(Func, E);
3725 }
3726
3727 template <class Emitter>
VisitCXXTypeidExpr(const CXXTypeidExpr * E)3728 bool Compiler<Emitter>::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
3729 const Type *TypeInfoType = E->getType().getTypePtr();
3730
3731 auto canonType = [](const Type *T) {
3732 return T->getCanonicalTypeUnqualified().getTypePtr();
3733 };
3734
3735 if (!E->isPotentiallyEvaluated()) {
3736 if (DiscardResult)
3737 return true;
3738
3739 if (E->isTypeOperand())
3740 return this->emitGetTypeid(
3741 canonType(E->getTypeOperand(Ctx.getASTContext()).getTypePtr()),
3742 TypeInfoType, E);
3743
3744 return this->emitGetTypeid(
3745 canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType,
3746 E);
3747 }
3748
3749 // Otherwise, we need to evaluate the expression operand.
3750 assert(E->getExprOperand());
3751 assert(E->getExprOperand()->isLValue());
3752
3753 if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
3754 return false;
3755
3756 if (!this->visit(E->getExprOperand()))
3757 return false;
3758
3759 if (!this->emitGetTypeidPtr(TypeInfoType, E))
3760 return false;
3761 if (DiscardResult)
3762 return this->emitPopPtr(E);
3763 return true;
3764 }
3765
3766 template <class Emitter>
VisitExpressionTraitExpr(const ExpressionTraitExpr * E)3767 bool Compiler<Emitter>::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
3768 assert(Ctx.getLangOpts().CPlusPlus);
3769 return this->emitConstBool(E->getValue(), E);
3770 }
3771
3772 template <class Emitter>
VisitCXXUuidofExpr(const CXXUuidofExpr * E)3773 bool Compiler<Emitter>::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
3774 if (DiscardResult)
3775 return true;
3776 assert(!Initializing);
3777
3778 const MSGuidDecl *GuidDecl = E->getGuidDecl();
3779 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
3780 assert(RD);
3781 // If the definiton of the result type is incomplete, just return a dummy.
3782 // If (and when) that is read from, we will fail, but not now.
3783 if (!RD->isCompleteDefinition())
3784 return this->emitDummyPtr(GuidDecl, E);
3785
3786 std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(GuidDecl);
3787 if (!GlobalIndex)
3788 return false;
3789 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3790 return false;
3791
3792 assert(this->getRecord(E->getType()));
3793
3794 const APValue &V = GuidDecl->getAsAPValue();
3795 if (V.getKind() == APValue::None)
3796 return true;
3797
3798 assert(V.isStruct());
3799 assert(V.getStructNumBases() == 0);
3800 if (!this->visitAPValueInitializer(V, E, E->getType()))
3801 return false;
3802
3803 return this->emitFinishInit(E);
3804 }
3805
3806 template <class Emitter>
VisitRequiresExpr(const RequiresExpr * E)3807 bool Compiler<Emitter>::VisitRequiresExpr(const RequiresExpr *E) {
3808 assert(classifyPrim(E->getType()) == PT_Bool);
3809 if (DiscardResult)
3810 return true;
3811 return this->emitConstBool(E->isSatisfied(), E);
3812 }
3813
3814 template <class Emitter>
VisitConceptSpecializationExpr(const ConceptSpecializationExpr * E)3815 bool Compiler<Emitter>::VisitConceptSpecializationExpr(
3816 const ConceptSpecializationExpr *E) {
3817 assert(classifyPrim(E->getType()) == PT_Bool);
3818 if (DiscardResult)
3819 return true;
3820 return this->emitConstBool(E->isSatisfied(), E);
3821 }
3822
3823 template <class Emitter>
VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator * E)3824 bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator(
3825 const CXXRewrittenBinaryOperator *E) {
3826 return this->delegate(E->getSemanticForm());
3827 }
3828
3829 template <class Emitter>
VisitPseudoObjectExpr(const PseudoObjectExpr * E)3830 bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
3831
3832 for (const Expr *SemE : E->semantics()) {
3833 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
3834 if (SemE == E->getResultExpr())
3835 return false;
3836
3837 if (OVE->isUnique())
3838 continue;
3839
3840 if (!this->discard(OVE))
3841 return false;
3842 } else if (SemE == E->getResultExpr()) {
3843 if (!this->delegate(SemE))
3844 return false;
3845 } else {
3846 if (!this->discard(SemE))
3847 return false;
3848 }
3849 }
3850 return true;
3851 }
3852
3853 template <class Emitter>
VisitPackIndexingExpr(const PackIndexingExpr * E)3854 bool Compiler<Emitter>::VisitPackIndexingExpr(const PackIndexingExpr *E) {
3855 return this->delegate(E->getSelectedExpr());
3856 }
3857
3858 template <class Emitter>
VisitRecoveryExpr(const RecoveryExpr * E)3859 bool Compiler<Emitter>::VisitRecoveryExpr(const RecoveryExpr *E) {
3860 return this->emitError(E);
3861 }
3862
3863 template <class Emitter>
VisitAddrLabelExpr(const AddrLabelExpr * E)3864 bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) {
3865 assert(E->getType()->isVoidPointerType());
3866
3867 unsigned Offset =
3868 allocateLocalPrimitive(E->getLabel(), PT_Ptr, /*IsConst=*/true);
3869
3870 return this->emitGetLocal(PT_Ptr, Offset, E);
3871 }
3872
3873 template <class Emitter>
VisitConvertVectorExpr(const ConvertVectorExpr * E)3874 bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
3875 assert(Initializing);
3876 const auto *VT = E->getType()->castAs<VectorType>();
3877 QualType ElemType = VT->getElementType();
3878 PrimType ElemT = classifyPrim(ElemType);
3879 const Expr *Src = E->getSrcExpr();
3880 QualType SrcType = Src->getType();
3881 PrimType SrcElemT = classifyVectorElementType(SrcType);
3882
3883 unsigned SrcOffset =
3884 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
3885 if (!this->visit(Src))
3886 return false;
3887 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
3888 return false;
3889
3890 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
3891 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
3892 return false;
3893 if (!this->emitArrayElemPop(SrcElemT, I, E))
3894 return false;
3895
3896 // Cast to the desired result element type.
3897 if (SrcElemT != ElemT) {
3898 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
3899 return false;
3900 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
3901 const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
3902 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
3903 return false;
3904 }
3905 if (!this->emitInitElem(ElemT, I, E))
3906 return false;
3907 }
3908
3909 return true;
3910 }
3911
3912 template <class Emitter>
VisitShuffleVectorExpr(const ShuffleVectorExpr * E)3913 bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
3914 assert(Initializing);
3915 assert(E->getNumSubExprs() > 2);
3916
3917 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
3918 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
3919 PrimType ElemT = classifyPrim(VT->getElementType());
3920 unsigned NumInputElems = VT->getNumElements();
3921 unsigned NumOutputElems = E->getNumSubExprs() - 2;
3922 assert(NumOutputElems > 0);
3923
3924 // Save both input vectors to a local variable.
3925 unsigned VectorOffsets[2];
3926 for (unsigned I = 0; I != 2; ++I) {
3927 VectorOffsets[I] =
3928 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
3929 if (!this->visit(Vecs[I]))
3930 return false;
3931 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
3932 return false;
3933 }
3934 for (unsigned I = 0; I != NumOutputElems; ++I) {
3935 APSInt ShuffleIndex = E->getShuffleMaskIdx(I);
3936 assert(ShuffleIndex >= -1);
3937 if (ShuffleIndex == -1)
3938 return this->emitInvalidShuffleVectorIndex(I, E);
3939
3940 assert(ShuffleIndex < (NumInputElems * 2));
3941 if (!this->emitGetLocal(PT_Ptr,
3942 VectorOffsets[ShuffleIndex >= NumInputElems], E))
3943 return false;
3944 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
3945 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
3946 return false;
3947
3948 if (!this->emitInitElem(ElemT, I, E))
3949 return false;
3950 }
3951
3952 return true;
3953 }
3954
3955 template <class Emitter>
VisitExtVectorElementExpr(const ExtVectorElementExpr * E)3956 bool Compiler<Emitter>::VisitExtVectorElementExpr(
3957 const ExtVectorElementExpr *E) {
3958 const Expr *Base = E->getBase();
3959 assert(
3960 Base->getType()->isVectorType() ||
3961 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
3962
3963 SmallVector<uint32_t, 4> Indices;
3964 E->getEncodedElementAccess(Indices);
3965
3966 if (Indices.size() == 1) {
3967 if (!this->visit(Base))
3968 return false;
3969
3970 if (E->isGLValue()) {
3971 if (!this->emitConstUint32(Indices[0], E))
3972 return false;
3973 return this->emitArrayElemPtrPop(PT_Uint32, E);
3974 }
3975 // Else, also load the value.
3976 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
3977 }
3978
3979 // Create a local variable for the base.
3980 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
3981 if (!this->visit(Base))
3982 return false;
3983 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
3984 return false;
3985
3986 // Now the vector variable for the return value.
3987 if (!Initializing) {
3988 std::optional<unsigned> ResultIndex;
3989 ResultIndex = allocateLocal(E);
3990 if (!ResultIndex)
3991 return false;
3992 if (!this->emitGetPtrLocal(*ResultIndex, E))
3993 return false;
3994 }
3995
3996 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
3997
3998 PrimType ElemT =
3999 classifyPrim(E->getType()->getAs<VectorType>()->getElementType());
4000 uint32_t DstIndex = 0;
4001 for (uint32_t I : Indices) {
4002 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4003 return false;
4004 if (!this->emitArrayElemPop(ElemT, I, E))
4005 return false;
4006 if (!this->emitInitElem(ElemT, DstIndex, E))
4007 return false;
4008 ++DstIndex;
4009 }
4010
4011 // Leave the result pointer on the stack.
4012 assert(!DiscardResult);
4013 return true;
4014 }
4015
4016 template <class Emitter>
VisitObjCBoxedExpr(const ObjCBoxedExpr * E)4017 bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
4018 const Expr *SubExpr = E->getSubExpr();
4019 if (!E->isExpressibleAsConstantInitializer())
4020 return this->discard(SubExpr) && this->emitInvalid(E);
4021
4022 if (DiscardResult)
4023 return true;
4024
4025 assert(classifyPrim(E) == PT_Ptr);
4026 return this->emitDummyPtr(E, E);
4027 }
4028
4029 template <class Emitter>
VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr * E)4030 bool Compiler<Emitter>::VisitCXXStdInitializerListExpr(
4031 const CXXStdInitializerListExpr *E) {
4032 const Expr *SubExpr = E->getSubExpr();
4033 const ConstantArrayType *ArrayType =
4034 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
4035 const Record *R = getRecord(E->getType());
4036 assert(Initializing);
4037 assert(SubExpr->isGLValue());
4038
4039 if (!this->visit(SubExpr))
4040 return false;
4041 if (!this->emitConstUint8(0, E))
4042 return false;
4043 if (!this->emitArrayElemPtrPopUint8(E))
4044 return false;
4045 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
4046 return false;
4047
4048 PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
4049 if (isIntegralType(SecondFieldT)) {
4050 if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()),
4051 SecondFieldT, E))
4052 return false;
4053 return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
4054 }
4055 assert(SecondFieldT == PT_Ptr);
4056
4057 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
4058 return false;
4059 if (!this->emitExpandPtr(E))
4060 return false;
4061 if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()), PT_Uint64, E))
4062 return false;
4063 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
4064 return false;
4065 return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
4066 }
4067
4068 template <class Emitter>
VisitStmtExpr(const StmtExpr * E)4069 bool Compiler<Emitter>::VisitStmtExpr(const StmtExpr *E) {
4070 BlockScope<Emitter> BS(this);
4071 StmtExprScope<Emitter> SS(this);
4072
4073 const CompoundStmt *CS = E->getSubStmt();
4074 const Stmt *Result = CS->getStmtExprResult();
4075 for (const Stmt *S : CS->body()) {
4076 if (S != Result) {
4077 if (!this->visitStmt(S))
4078 return false;
4079 continue;
4080 }
4081
4082 assert(S == Result);
4083 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
4084 return this->delegate(ResultExpr);
4085 return this->emitUnsupported(E);
4086 }
4087
4088 return BS.destroyLocals();
4089 }
4090
discard(const Expr * E)4091 template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
4092 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
4093 /*NewInitializing=*/false);
4094 return this->Visit(E);
4095 }
4096
delegate(const Expr * E)4097 template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
4098 // We're basically doing:
4099 // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
4100 // but that's unnecessary of course.
4101 return this->Visit(E);
4102 }
4103
visit(const Expr * E)4104 template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
4105 if (E->getType().isNull())
4106 return false;
4107
4108 if (E->getType()->isVoidType())
4109 return this->discard(E);
4110
4111 // Create local variable to hold the return value.
4112 if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
4113 !classify(E->getType())) {
4114 std::optional<unsigned> LocalIndex = allocateLocal(E);
4115 if (!LocalIndex)
4116 return false;
4117
4118 if (!this->emitGetPtrLocal(*LocalIndex, E))
4119 return false;
4120 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
4121 return this->visitInitializer(E);
4122 }
4123
4124 // Otherwise,we have a primitive return value, produce the value directly
4125 // and push it on the stack.
4126 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4127 /*NewInitializing=*/false);
4128 return this->Visit(E);
4129 }
4130
4131 template <class Emitter>
visitInitializer(const Expr * E)4132 bool Compiler<Emitter>::visitInitializer(const Expr *E) {
4133 assert(!classify(E->getType()));
4134
4135 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
4136 /*NewInitializing=*/true);
4137 return this->Visit(E);
4138 }
4139
visitBool(const Expr * E)4140 template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
4141 std::optional<PrimType> T = classify(E->getType());
4142 if (!T) {
4143 // Convert complex values to bool.
4144 if (E->getType()->isAnyComplexType()) {
4145 if (!this->visit(E))
4146 return false;
4147 return this->emitComplexBoolCast(E);
4148 }
4149 return false;
4150 }
4151
4152 if (!this->visit(E))
4153 return false;
4154
4155 if (T == PT_Bool)
4156 return true;
4157
4158 // Convert pointers to bool.
4159 if (T == PT_Ptr)
4160 return this->emitIsNonNullPtr(E);
4161
4162 // Or Floats.
4163 if (T == PT_Float)
4164 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
4165
4166 // Or anything else we can.
4167 return this->emitCast(*T, PT_Bool, E);
4168 }
4169
4170 template <class Emitter>
visitZeroInitializer(PrimType T,QualType QT,const Expr * E)4171 bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
4172 const Expr *E) {
4173 if (const auto *AT = QT->getAs<AtomicType>())
4174 QT = AT->getValueType();
4175
4176 switch (T) {
4177 case PT_Bool:
4178 return this->emitZeroBool(E);
4179 case PT_Sint8:
4180 return this->emitZeroSint8(E);
4181 case PT_Uint8:
4182 return this->emitZeroUint8(E);
4183 case PT_Sint16:
4184 return this->emitZeroSint16(E);
4185 case PT_Uint16:
4186 return this->emitZeroUint16(E);
4187 case PT_Sint32:
4188 return this->emitZeroSint32(E);
4189 case PT_Uint32:
4190 return this->emitZeroUint32(E);
4191 case PT_Sint64:
4192 return this->emitZeroSint64(E);
4193 case PT_Uint64:
4194 return this->emitZeroUint64(E);
4195 case PT_IntAP:
4196 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
4197 case PT_IntAPS:
4198 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
4199 case PT_Ptr:
4200 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
4201 nullptr, E);
4202 case PT_MemberPtr:
4203 return this->emitNullMemberPtr(0, nullptr, E);
4204 case PT_Float: {
4205 APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
4206 return this->emitFloat(F, E);
4207 }
4208 case PT_FixedPoint: {
4209 auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
4210 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
4211 }
4212 }
4213 llvm_unreachable("unknown primitive type");
4214 }
4215
4216 template <class Emitter>
visitZeroRecordInitializer(const Record * R,const Expr * E)4217 bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
4218 const Expr *E) {
4219 assert(E);
4220 assert(R);
4221 // Fields
4222 for (const Record::Field &Field : R->fields()) {
4223 if (Field.isUnnamedBitField())
4224 continue;
4225
4226 const Descriptor *D = Field.Desc;
4227 if (D->isPrimitive()) {
4228 QualType QT = D->getType();
4229 PrimType T = classifyPrim(D->getType());
4230 if (!this->visitZeroInitializer(T, QT, E))
4231 return false;
4232 if (!this->emitInitField(T, Field.Offset, E))
4233 return false;
4234 if (R->isUnion())
4235 break;
4236 continue;
4237 }
4238
4239 if (!this->emitGetPtrField(Field.Offset, E))
4240 return false;
4241
4242 if (D->isPrimitiveArray()) {
4243 QualType ET = D->getElemQualType();
4244 PrimType T = classifyPrim(ET);
4245 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
4246 if (!this->visitZeroInitializer(T, ET, E))
4247 return false;
4248 if (!this->emitInitElem(T, I, E))
4249 return false;
4250 }
4251 } else if (D->isCompositeArray()) {
4252 // Can't be a vector or complex field.
4253 if (!this->visitZeroArrayInitializer(D->getType(), E))
4254 return false;
4255 } else if (D->isRecord()) {
4256 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
4257 return false;
4258 } else
4259 return false;
4260
4261 if (!this->emitFinishInitPop(E))
4262 return false;
4263
4264 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
4265 // object's first non-static named data member is zero-initialized
4266 if (R->isUnion())
4267 break;
4268 }
4269
4270 for (const Record::Base &B : R->bases()) {
4271 if (!this->emitGetPtrBase(B.Offset, E))
4272 return false;
4273 if (!this->visitZeroRecordInitializer(B.R, E))
4274 return false;
4275 if (!this->emitFinishInitPop(E))
4276 return false;
4277 }
4278
4279 // FIXME: Virtual bases.
4280
4281 return true;
4282 }
4283
4284 template <class Emitter>
visitZeroArrayInitializer(QualType T,const Expr * E)4285 bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
4286 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
4287 const ArrayType *AT = T->getAsArrayTypeUnsafe();
4288 QualType ElemType = AT->getElementType();
4289 size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
4290
4291 if (std::optional<PrimType> ElemT = classify(ElemType)) {
4292 for (size_t I = 0; I != NumElems; ++I) {
4293 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
4294 return false;
4295 if (!this->emitInitElem(*ElemT, I, E))
4296 return false;
4297 }
4298 return true;
4299 } else if (ElemType->isRecordType()) {
4300 const Record *R = getRecord(ElemType);
4301
4302 for (size_t I = 0; I != NumElems; ++I) {
4303 if (!this->emitConstUint32(I, E))
4304 return false;
4305 if (!this->emitArrayElemPtr(PT_Uint32, E))
4306 return false;
4307 if (!this->visitZeroRecordInitializer(R, E))
4308 return false;
4309 if (!this->emitPopPtr(E))
4310 return false;
4311 }
4312 return true;
4313 } else if (ElemType->isArrayType()) {
4314 for (size_t I = 0; I != NumElems; ++I) {
4315 if (!this->emitConstUint32(I, E))
4316 return false;
4317 if (!this->emitArrayElemPtr(PT_Uint32, E))
4318 return false;
4319 if (!this->visitZeroArrayInitializer(ElemType, E))
4320 return false;
4321 if (!this->emitPopPtr(E))
4322 return false;
4323 }
4324 return true;
4325 }
4326
4327 return false;
4328 }
4329
4330 template <class Emitter>
4331 template <typename T>
emitConst(T Value,PrimType Ty,const Expr * E)4332 bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
4333 switch (Ty) {
4334 case PT_Sint8:
4335 return this->emitConstSint8(Value, E);
4336 case PT_Uint8:
4337 return this->emitConstUint8(Value, E);
4338 case PT_Sint16:
4339 return this->emitConstSint16(Value, E);
4340 case PT_Uint16:
4341 return this->emitConstUint16(Value, E);
4342 case PT_Sint32:
4343 return this->emitConstSint32(Value, E);
4344 case PT_Uint32:
4345 return this->emitConstUint32(Value, E);
4346 case PT_Sint64:
4347 return this->emitConstSint64(Value, E);
4348 case PT_Uint64:
4349 return this->emitConstUint64(Value, E);
4350 case PT_Bool:
4351 return this->emitConstBool(Value, E);
4352 case PT_Ptr:
4353 case PT_MemberPtr:
4354 case PT_Float:
4355 case PT_IntAP:
4356 case PT_IntAPS:
4357 case PT_FixedPoint:
4358 llvm_unreachable("Invalid integral type");
4359 break;
4360 }
4361 llvm_unreachable("unknown primitive type");
4362 }
4363
4364 template <class Emitter>
4365 template <typename T>
emitConst(T Value,const Expr * E)4366 bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
4367 return this->emitConst(Value, classifyPrim(E->getType()), E);
4368 }
4369
4370 template <class Emitter>
emitConst(const APSInt & Value,PrimType Ty,const Expr * E)4371 bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
4372 const Expr *E) {
4373 if (Ty == PT_IntAPS)
4374 return this->emitConstIntAPS(Value, E);
4375 if (Ty == PT_IntAP)
4376 return this->emitConstIntAP(Value, E);
4377
4378 if (Value.isSigned())
4379 return this->emitConst(Value.getSExtValue(), Ty, E);
4380 return this->emitConst(Value.getZExtValue(), Ty, E);
4381 }
4382
4383 template <class Emitter>
emitConst(const APSInt & Value,const Expr * E)4384 bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
4385 return this->emitConst(Value, classifyPrim(E->getType()), E);
4386 }
4387
4388 template <class Emitter>
allocateLocalPrimitive(DeclTy && Src,PrimType Ty,bool IsConst,const ValueDecl * ExtendingDecl,ScopeKind SC,bool IsConstexprUnknown)4389 unsigned Compiler<Emitter>::allocateLocalPrimitive(
4390 DeclTy &&Src, PrimType Ty, bool IsConst, const ValueDecl *ExtendingDecl,
4391 ScopeKind SC, bool IsConstexprUnknown) {
4392 // Make sure we don't accidentally register the same decl twice.
4393 if (const auto *VD =
4394 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4395 assert(!P.getGlobal(VD));
4396 assert(!Locals.contains(VD));
4397 (void)VD;
4398 }
4399
4400 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
4401 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
4402 // or isa<MaterializeTemporaryExpr>().
4403 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
4404 IsConst, isa<const Expr *>(Src));
4405 D->IsConstexprUnknown = IsConstexprUnknown;
4406 Scope::Local Local = this->createLocal(D);
4407 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
4408 Locals.insert({VD, Local});
4409 if (ExtendingDecl)
4410 VarScope->addExtended(Local, ExtendingDecl);
4411 else
4412 VarScope->addForScopeKind(Local, SC);
4413 return Local.Offset;
4414 }
4415
4416 template <class Emitter>
4417 std::optional<unsigned>
allocateLocal(DeclTy && Src,QualType Ty,const ValueDecl * ExtendingDecl,ScopeKind SC,bool IsConstexprUnknown)4418 Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
4419 const ValueDecl *ExtendingDecl, ScopeKind SC,
4420 bool IsConstexprUnknown) {
4421 // Make sure we don't accidentally register the same decl twice.
4422 if ([[maybe_unused]] const auto *VD =
4423 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4424 assert(!P.getGlobal(VD));
4425 assert(!Locals.contains(VD));
4426 }
4427
4428 const ValueDecl *Key = nullptr;
4429 const Expr *Init = nullptr;
4430 bool IsTemporary = false;
4431 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4432 Key = VD;
4433 Ty = VD->getType();
4434
4435 if (const auto *VarD = dyn_cast<VarDecl>(VD))
4436 Init = VarD->getInit();
4437 }
4438 if (auto *E = Src.dyn_cast<const Expr *>()) {
4439 IsTemporary = true;
4440 if (Ty.isNull())
4441 Ty = E->getType();
4442 }
4443
4444 Descriptor *D = P.createDescriptor(
4445 Src, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
4446 IsTemporary, /*IsMutable=*/false, /*IsVolatile=*/false, Init);
4447 if (!D)
4448 return std::nullopt;
4449 D->IsConstexprUnknown = IsConstexprUnknown;
4450
4451 Scope::Local Local = this->createLocal(D);
4452 if (Key)
4453 Locals.insert({Key, Local});
4454 if (ExtendingDecl)
4455 VarScope->addExtended(Local, ExtendingDecl);
4456 else
4457 VarScope->addForScopeKind(Local, SC);
4458 return Local.Offset;
4459 }
4460
4461 template <class Emitter>
allocateTemporary(const Expr * E)4462 std::optional<unsigned> Compiler<Emitter>::allocateTemporary(const Expr *E) {
4463 QualType Ty = E->getType();
4464 assert(!Ty->isRecordType());
4465
4466 Descriptor *D = P.createDescriptor(
4467 E, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
4468 /*IsTemporary=*/true);
4469
4470 if (!D)
4471 return std::nullopt;
4472
4473 Scope::Local Local = this->createLocal(D);
4474 VariableScope<Emitter> *S = VarScope;
4475 assert(S);
4476 // Attach to topmost scope.
4477 while (S->getParent())
4478 S = S->getParent();
4479 assert(S && !S->getParent());
4480 S->addLocal(Local);
4481 return Local.Offset;
4482 }
4483
4484 template <class Emitter>
getRecordTy(QualType Ty)4485 const RecordType *Compiler<Emitter>::getRecordTy(QualType Ty) {
4486 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
4487 return PT->getPointeeType()->getAs<RecordType>();
4488 return Ty->getAs<RecordType>();
4489 }
4490
getRecord(QualType Ty)4491 template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
4492 if (const auto *RecordTy = getRecordTy(Ty))
4493 return getRecord(RecordTy->getDecl());
4494 return nullptr;
4495 }
4496
4497 template <class Emitter>
getRecord(const RecordDecl * RD)4498 Record *Compiler<Emitter>::getRecord(const RecordDecl *RD) {
4499 return P.getOrCreateRecord(RD);
4500 }
4501
4502 template <class Emitter>
getFunction(const FunctionDecl * FD)4503 const Function *Compiler<Emitter>::getFunction(const FunctionDecl *FD) {
4504 return Ctx.getOrCreateFunction(FD);
4505 }
4506
4507 template <class Emitter>
visitExpr(const Expr * E,bool DestroyToplevelScope)4508 bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
4509 LocalScope<Emitter> RootScope(this);
4510
4511 // If we won't destroy the toplevel scope, check for memory leaks first.
4512 if (!DestroyToplevelScope) {
4513 if (!this->emitCheckAllocations(E))
4514 return false;
4515 }
4516
4517 auto maybeDestroyLocals = [&]() -> bool {
4518 if (DestroyToplevelScope)
4519 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
4520 return this->emitCheckAllocations(E);
4521 };
4522
4523 // Void expressions.
4524 if (E->getType()->isVoidType()) {
4525 if (!visit(E))
4526 return false;
4527 return this->emitRetVoid(E) && maybeDestroyLocals();
4528 }
4529
4530 // Expressions with a primitive return type.
4531 if (std::optional<PrimType> T = classify(E)) {
4532 if (!visit(E))
4533 return false;
4534
4535 return this->emitRet(*T, E) && maybeDestroyLocals();
4536 }
4537
4538 // Expressions with a composite return type.
4539 // For us, that means everything we don't
4540 // have a PrimType for.
4541 if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
4542 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
4543 if (!this->emitGetPtrLocal(*LocalOffset, E))
4544 return false;
4545
4546 if (!visitInitializer(E))
4547 return false;
4548
4549 if (!this->emitFinishInit(E))
4550 return false;
4551 // We are destroying the locals AFTER the Ret op.
4552 // The Ret op needs to copy the (alive) values, but the
4553 // destructors may still turn the entire expression invalid.
4554 return this->emitRetValue(E) && maybeDestroyLocals();
4555 }
4556
4557 return maybeDestroyLocals() && this->emitCheckAllocations(E) && false;
4558 }
4559
4560 template <class Emitter>
visitDecl(const VarDecl * VD,bool IsConstexprUnknown)4561 VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
4562 bool IsConstexprUnknown) {
4563
4564 auto R = this->visitVarDecl(VD, /*Toplevel=*/true, IsConstexprUnknown);
4565
4566 if (R.notCreated())
4567 return R;
4568
4569 if (R)
4570 return true;
4571
4572 if (!R && Context::shouldBeGloballyIndexed(VD)) {
4573 if (auto GlobalIndex = P.getGlobal(VD)) {
4574 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4575 GlobalInlineDescriptor &GD =
4576 *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4577
4578 GD.InitState = GlobalInitState::InitializerFailed;
4579 GlobalBlock->invokeDtor();
4580 }
4581 }
4582
4583 return R;
4584 }
4585
4586 /// Toplevel visitDeclAndReturn().
4587 /// We get here from evaluateAsInitializer().
4588 /// We need to evaluate the initializer and return its value.
4589 template <class Emitter>
visitDeclAndReturn(const VarDecl * VD,bool ConstantContext)4590 bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD,
4591 bool ConstantContext) {
4592
4593 // We only create variables if we're evaluating in a constant context.
4594 // Otherwise, just evaluate the initializer and return it.
4595 if (!ConstantContext) {
4596 DeclScope<Emitter> LS(this, VD);
4597 const Expr *Init = VD->getInit();
4598 if (!this->visit(Init))
4599 return false;
4600 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
4601 LS.destroyLocals() && this->emitCheckAllocations(VD);
4602 }
4603
4604 LocalScope<Emitter> VDScope(this, VD);
4605 if (!this->visitVarDecl(VD, /*Toplevel=*/true))
4606 return false;
4607
4608 std::optional<PrimType> VarT = classify(VD->getType());
4609 if (Context::shouldBeGloballyIndexed(VD)) {
4610 auto GlobalIndex = P.getGlobal(VD);
4611 assert(GlobalIndex); // visitVarDecl() didn't return false.
4612 if (VarT) {
4613 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
4614 return false;
4615 } else {
4616 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
4617 return false;
4618 }
4619 } else {
4620 auto Local = Locals.find(VD);
4621 assert(Local != Locals.end()); // Same here.
4622 if (VarT) {
4623 if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
4624 return false;
4625 } else {
4626 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
4627 return false;
4628 }
4629 }
4630
4631 // Return the value.
4632 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
4633 // If the Ret above failed and this is a global variable, mark it as
4634 // uninitialized, even everything else succeeded.
4635 if (Context::shouldBeGloballyIndexed(VD)) {
4636 auto GlobalIndex = P.getGlobal(VD);
4637 assert(GlobalIndex);
4638 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4639 GlobalInlineDescriptor &GD =
4640 *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4641
4642 GD.InitState = GlobalInitState::InitializerFailed;
4643 GlobalBlock->invokeDtor();
4644 }
4645 return false;
4646 }
4647
4648 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
4649 }
4650
4651 template <class Emitter>
visitVarDecl(const VarDecl * VD,bool Toplevel,bool IsConstexprUnknown)4652 VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
4653 bool Toplevel,
4654 bool IsConstexprUnknown) {
4655 // We don't know what to do with these, so just return false.
4656 if (VD->getType().isNull())
4657 return false;
4658
4659 // This case is EvalEmitter-only. If we won't create any instructions for the
4660 // initializer anyway, don't bother creating the variable in the first place.
4661 if (!this->isActive())
4662 return VarCreationState::NotCreated();
4663
4664 const Expr *Init = VD->getInit();
4665 std::optional<PrimType> VarT = classify(VD->getType());
4666
4667 if (Init && Init->isValueDependent())
4668 return false;
4669
4670 if (Context::shouldBeGloballyIndexed(VD)) {
4671 auto checkDecl = [&]() -> bool {
4672 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
4673 return !NeedsOp || this->emitCheckDecl(VD, VD);
4674 };
4675
4676 auto initGlobal = [&](unsigned GlobalIndex) -> bool {
4677 assert(Init);
4678
4679 if (VarT) {
4680 if (!this->visit(Init))
4681 return checkDecl() && false;
4682
4683 return checkDecl() && this->emitInitGlobal(*VarT, GlobalIndex, VD);
4684 }
4685
4686 if (!checkDecl())
4687 return false;
4688
4689 if (!this->emitGetPtrGlobal(GlobalIndex, Init))
4690 return false;
4691
4692 if (!visitInitializer(Init))
4693 return false;
4694
4695 return this->emitFinishInitGlobal(Init);
4696 };
4697
4698 DeclScope<Emitter> LocalScope(this, VD);
4699
4700 // We've already seen and initialized this global.
4701 if (std::optional<unsigned> GlobalIndex = P.getGlobal(VD)) {
4702 if (P.getPtrGlobal(*GlobalIndex).isInitialized())
4703 return checkDecl();
4704
4705 // The previous attempt at initialization might've been unsuccessful,
4706 // so let's try this one.
4707 return Init && checkDecl() && initGlobal(*GlobalIndex);
4708 }
4709
4710 std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
4711
4712 if (!GlobalIndex)
4713 return false;
4714
4715 return !Init || (checkDecl() && initGlobal(*GlobalIndex));
4716 }
4717 // Local variables.
4718 InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
4719
4720 if (VarT) {
4721 unsigned Offset = this->allocateLocalPrimitive(
4722 VD, *VarT, VD->getType().isConstQualified(), nullptr, ScopeKind::Block,
4723 IsConstexprUnknown);
4724 if (Init) {
4725 // If this is a toplevel declaration, create a scope for the
4726 // initializer.
4727 if (Toplevel) {
4728 LocalScope<Emitter> Scope(this);
4729 if (!this->visit(Init))
4730 return false;
4731 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
4732 } else {
4733 if (!this->visit(Init))
4734 return false;
4735 return this->emitSetLocal(*VarT, Offset, VD);
4736 }
4737 }
4738 } else {
4739 if (std::optional<unsigned> Offset = this->allocateLocal(
4740 VD, VD->getType(), nullptr, ScopeKind::Block, IsConstexprUnknown)) {
4741 if (!Init)
4742 return true;
4743
4744 if (!this->emitGetPtrLocal(*Offset, Init))
4745 return false;
4746
4747 if (!visitInitializer(Init))
4748 return false;
4749
4750 return this->emitFinishInitPop(Init);
4751 }
4752 return false;
4753 }
4754 return true;
4755 }
4756
4757 template <class Emitter>
visitAPValue(const APValue & Val,PrimType ValType,const Expr * E)4758 bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType,
4759 const Expr *E) {
4760 assert(!DiscardResult);
4761 if (Val.isInt())
4762 return this->emitConst(Val.getInt(), ValType, E);
4763 else if (Val.isFloat()) {
4764 APFloat F = Val.getFloat();
4765 return this->emitFloat(F, E);
4766 }
4767
4768 if (Val.isLValue()) {
4769 if (Val.isNullPointer())
4770 return this->emitNull(ValType, 0, nullptr, E);
4771 APValue::LValueBase Base = Val.getLValueBase();
4772 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
4773 return this->visit(BaseExpr);
4774 else if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
4775 return this->visitDeclRef(VD, E);
4776 }
4777 } else if (Val.isMemberPointer()) {
4778 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
4779 return this->emitGetMemberPtr(MemberDecl, E);
4780 return this->emitNullMemberPtr(0, nullptr, E);
4781 }
4782
4783 return false;
4784 }
4785
4786 template <class Emitter>
visitAPValueInitializer(const APValue & Val,const Expr * E,QualType T)4787 bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val,
4788 const Expr *E, QualType T) {
4789 if (Val.isStruct()) {
4790 const Record *R = this->getRecord(T);
4791 assert(R);
4792 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
4793 const APValue &F = Val.getStructField(I);
4794 const Record::Field *RF = R->getField(I);
4795 QualType FieldType = RF->Decl->getType();
4796
4797 if (std::optional<PrimType> PT = classify(FieldType)) {
4798 if (!this->visitAPValue(F, *PT, E))
4799 return false;
4800 if (!this->emitInitField(*PT, RF->Offset, E))
4801 return false;
4802 } else {
4803 if (!this->emitGetPtrField(RF->Offset, E))
4804 return false;
4805 if (!this->visitAPValueInitializer(F, E, FieldType))
4806 return false;
4807 if (!this->emitPopPtr(E))
4808 return false;
4809 }
4810 }
4811 return true;
4812 } else if (Val.isUnion()) {
4813 const FieldDecl *UnionField = Val.getUnionField();
4814 const Record *R = this->getRecord(UnionField->getParent());
4815 assert(R);
4816 const APValue &F = Val.getUnionValue();
4817 const Record::Field *RF = R->getField(UnionField);
4818 PrimType T = classifyPrim(RF->Decl->getType());
4819 if (!this->visitAPValue(F, T, E))
4820 return false;
4821 return this->emitInitField(T, RF->Offset, E);
4822 } else if (Val.isArray()) {
4823 const auto *ArrType = T->getAsArrayTypeUnsafe();
4824 QualType ElemType = ArrType->getElementType();
4825 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
4826 const APValue &Elem = Val.getArrayInitializedElt(A);
4827 if (std::optional<PrimType> ElemT = classify(ElemType)) {
4828 if (!this->visitAPValue(Elem, *ElemT, E))
4829 return false;
4830 if (!this->emitInitElem(*ElemT, A, E))
4831 return false;
4832 } else {
4833 if (!this->emitConstUint32(A, E))
4834 return false;
4835 if (!this->emitArrayElemPtrUint32(E))
4836 return false;
4837 if (!this->visitAPValueInitializer(Elem, E, ElemType))
4838 return false;
4839 if (!this->emitPopPtr(E))
4840 return false;
4841 }
4842 }
4843 return true;
4844 }
4845 // TODO: Other types.
4846
4847 return false;
4848 }
4849
4850 template <class Emitter>
VisitBuiltinCallExpr(const CallExpr * E,unsigned BuiltinID)4851 bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
4852 unsigned BuiltinID) {
4853
4854 if (BuiltinID == Builtin::BI__builtin_constant_p) {
4855 // Void argument is always invalid and harder to handle later.
4856 if (E->getArg(0)->getType()->isVoidType()) {
4857 if (DiscardResult)
4858 return true;
4859 return this->emitConst(0, E);
4860 }
4861
4862 if (!this->emitStartSpeculation(E))
4863 return false;
4864 LabelTy EndLabel = this->getLabel();
4865 if (!this->speculate(E, EndLabel))
4866 return false;
4867 this->fallthrough(EndLabel);
4868 if (!this->emitEndSpeculation(E))
4869 return false;
4870 if (DiscardResult)
4871 return this->emitPop(classifyPrim(E), E);
4872 return true;
4873 }
4874
4875 // For these, we're expected to ultimately return an APValue pointing
4876 // to the CallExpr. This is needed to get the correct codegen.
4877 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
4878 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
4879 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
4880 BuiltinID == Builtin::BI__builtin_function_start) {
4881 if (DiscardResult)
4882 return true;
4883 return this->emitDummyPtr(E, E);
4884 }
4885
4886 QualType ReturnType = E->getType();
4887 std::optional<PrimType> ReturnT = classify(E);
4888
4889 // Non-primitive return type. Prepare storage.
4890 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
4891 std::optional<unsigned> LocalIndex = allocateLocal(E);
4892 if (!LocalIndex)
4893 return false;
4894 if (!this->emitGetPtrLocal(*LocalIndex, E))
4895 return false;
4896 }
4897
4898 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
4899 // Put arguments on the stack.
4900 for (const auto *Arg : E->arguments()) {
4901 if (!this->visit(Arg))
4902 return false;
4903 }
4904 }
4905
4906 if (!this->emitCallBI(E, BuiltinID, E))
4907 return false;
4908
4909 if (DiscardResult && !ReturnType->isVoidType()) {
4910 assert(ReturnT);
4911 return this->emitPop(*ReturnT, E);
4912 }
4913
4914 return true;
4915 }
4916
stripDerivedToBaseCasts(const Expr * E)4917 static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4918 if (const auto *PE = dyn_cast<ParenExpr>(E))
4919 return stripDerivedToBaseCasts(PE->getSubExpr());
4920
4921 if (const auto *CE = dyn_cast<CastExpr>(E);
4922 CE &&
4923 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4924 return stripDerivedToBaseCasts(CE->getSubExpr());
4925
4926 return E;
4927 }
4928
4929 template <class Emitter>
VisitCallExpr(const CallExpr * E)4930 bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
4931 const FunctionDecl *FuncDecl = E->getDirectCallee();
4932
4933 if (FuncDecl) {
4934 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
4935 return VisitBuiltinCallExpr(E, BuiltinID);
4936
4937 // Calls to replaceable operator new/operator delete.
4938 if (FuncDecl->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
4939 if (FuncDecl->getDeclName().isAnyOperatorNew()) {
4940 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
4941 } else {
4942 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete);
4943 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
4944 }
4945 }
4946
4947 // Explicit calls to trivial destructors
4948 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
4949 DD && DD->isTrivial()) {
4950 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
4951 if (!this->visit(MemberCall->getImplicitObjectArgument()))
4952 return false;
4953 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
4954 this->emitPopPtr(E);
4955 }
4956 }
4957
4958 BlockScope<Emitter> CallScope(this, ScopeKind::Call);
4959
4960 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
4961 std::optional<PrimType> T = classify(ReturnType);
4962 bool HasRVO = !ReturnType->isVoidType() && !T;
4963
4964 if (HasRVO) {
4965 if (DiscardResult) {
4966 // If we need to discard the return value but the function returns its
4967 // value via an RVO pointer, we need to create one such pointer just
4968 // for this call.
4969 if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4970 if (!this->emitGetPtrLocal(*LocalIndex, E))
4971 return false;
4972 }
4973 } else {
4974 // We need the result. Prepare a pointer to return or
4975 // dup the current one.
4976 if (!Initializing) {
4977 if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4978 if (!this->emitGetPtrLocal(*LocalIndex, E))
4979 return false;
4980 }
4981 }
4982 if (!this->emitDupPtr(E))
4983 return false;
4984 }
4985 }
4986
4987 SmallVector<const Expr *, 8> Args(ArrayRef(E->getArgs(), E->getNumArgs()));
4988
4989 bool IsAssignmentOperatorCall = false;
4990 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
4991 OCE && OCE->isAssignmentOp()) {
4992 // Just like with regular assignments, we need to special-case assignment
4993 // operators here and evaluate the RHS (the second arg) before the LHS (the
4994 // first arg). We fix this by using a Flip op later.
4995 assert(Args.size() == 2);
4996 IsAssignmentOperatorCall = true;
4997 std::reverse(Args.begin(), Args.end());
4998 }
4999 // Calling a static operator will still
5000 // pass the instance, but we don't need it.
5001 // Discard it here.
5002 if (isa<CXXOperatorCallExpr>(E)) {
5003 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
5004 MD && MD->isStatic()) {
5005 if (!this->discard(E->getArg(0)))
5006 return false;
5007 // Drop first arg.
5008 Args.erase(Args.begin());
5009 }
5010 }
5011
5012 bool Devirtualized = false;
5013 std::optional<unsigned> CalleeOffset;
5014 // Add the (optional, implicit) This pointer.
5015 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
5016 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
5017 // If we end up creating a CallPtr op for this, we need the base of the
5018 // member pointer as the instance pointer, and later extract the function
5019 // decl as the function pointer.
5020 const Expr *Callee = E->getCallee();
5021 CalleeOffset =
5022 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
5023 if (!this->visit(Callee))
5024 return false;
5025 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
5026 return false;
5027 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5028 return false;
5029 if (!this->emitGetMemberPtrBase(E))
5030 return false;
5031 } else {
5032 const auto *InstancePtr = MC->getImplicitObjectArgument();
5033 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5034 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5035 const auto *Stripped = stripDerivedToBaseCasts(InstancePtr);
5036 if (isa<CXXThisExpr>(Stripped)) {
5037 FuncDecl =
5038 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5039 Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5040 Devirtualized = true;
5041 if (!this->visit(Stripped))
5042 return false;
5043 } else {
5044 if (!this->visit(InstancePtr))
5045 return false;
5046 }
5047 } else {
5048 if (!this->visit(InstancePtr))
5049 return false;
5050 }
5051 }
5052 } else if (const auto *PD =
5053 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
5054 if (!this->emitCheckPseudoDtor(E))
5055 return false;
5056 const Expr *Base = PD->getBase();
5057 if (!Base->isGLValue())
5058 return this->discard(Base);
5059 if (!this->visit(Base))
5060 return false;
5061 return this->emitEndLifetimePop(E);
5062 } else if (!FuncDecl) {
5063 const Expr *Callee = E->getCallee();
5064 CalleeOffset =
5065 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
5066 if (!this->visit(Callee))
5067 return false;
5068 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
5069 return false;
5070 }
5071
5072 if (!this->visitCallArgs(Args, FuncDecl))
5073 return false;
5074
5075 // Undo the argument reversal we did earlier.
5076 if (IsAssignmentOperatorCall) {
5077 assert(Args.size() == 2);
5078 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
5079 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
5080 if (!this->emitFlip(Arg2T, Arg1T, E))
5081 return false;
5082 }
5083
5084 if (FuncDecl) {
5085 const Function *Func = getFunction(FuncDecl);
5086 if (!Func)
5087 return false;
5088 assert(HasRVO == Func->hasRVO());
5089
5090 bool HasQualifier = false;
5091 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
5092 HasQualifier = ME->hasQualifier();
5093
5094 bool IsVirtual = false;
5095 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5096 IsVirtual = !Devirtualized && MD->isVirtual();
5097
5098 // In any case call the function. The return value will end up on the stack
5099 // and if the function has RVO, we already have the pointer on the stack to
5100 // write the result into.
5101 if (IsVirtual && !HasQualifier) {
5102 uint32_t VarArgSize = 0;
5103 unsigned NumParams =
5104 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5105 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5106 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5107
5108 if (!this->emitCallVirt(Func, VarArgSize, E))
5109 return false;
5110 } else if (Func->isVariadic()) {
5111 uint32_t VarArgSize = 0;
5112 unsigned NumParams =
5113 Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
5114 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
5115 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5116 if (!this->emitCallVar(Func, VarArgSize, E))
5117 return false;
5118 } else {
5119 if (!this->emitCall(Func, 0, E))
5120 return false;
5121 }
5122 } else {
5123 // Indirect call. Visit the callee, which will leave a FunctionPointer on
5124 // the stack. Cleanup of the returned value if necessary will be done after
5125 // the function call completed.
5126
5127 // Sum the size of all args from the call expr.
5128 uint32_t ArgSize = 0;
5129 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
5130 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
5131
5132 // Get the callee, either from a member pointer or function pointer saved in
5133 // CalleeOffset.
5134 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
5135 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
5136 return false;
5137 if (!this->emitGetMemberPtrDecl(E))
5138 return false;
5139 } else {
5140 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
5141 return false;
5142 }
5143 if (!this->emitCallPtr(ArgSize, E, E))
5144 return false;
5145 }
5146
5147 // Cleanup for discarded return values.
5148 if (DiscardResult && !ReturnType->isVoidType() && T)
5149 return this->emitPop(*T, E) && CallScope.destroyLocals();
5150
5151 return CallScope.destroyLocals();
5152 }
5153
5154 template <class Emitter>
VisitCXXDefaultInitExpr(const CXXDefaultInitExpr * E)5155 bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
5156 SourceLocScope<Emitter> SLS(this, E);
5157
5158 return this->delegate(E->getExpr());
5159 }
5160
5161 template <class Emitter>
VisitCXXDefaultArgExpr(const CXXDefaultArgExpr * E)5162 bool Compiler<Emitter>::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
5163 SourceLocScope<Emitter> SLS(this, E);
5164
5165 return this->delegate(E->getExpr());
5166 }
5167
5168 template <class Emitter>
VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr * E)5169 bool Compiler<Emitter>::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
5170 if (DiscardResult)
5171 return true;
5172
5173 return this->emitConstBool(E->getValue(), E);
5174 }
5175
5176 template <class Emitter>
VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr * E)5177 bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr(
5178 const CXXNullPtrLiteralExpr *E) {
5179 if (DiscardResult)
5180 return true;
5181
5182 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
5183 return this->emitNullPtr(Val, nullptr, E);
5184 }
5185
5186 template <class Emitter>
VisitGNUNullExpr(const GNUNullExpr * E)5187 bool Compiler<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) {
5188 if (DiscardResult)
5189 return true;
5190
5191 assert(E->getType()->isIntegerType());
5192
5193 PrimType T = classifyPrim(E->getType());
5194 return this->emitZero(T, E);
5195 }
5196
5197 template <class Emitter>
VisitCXXThisExpr(const CXXThisExpr * E)5198 bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
5199 if (DiscardResult)
5200 return true;
5201
5202 if (this->LambdaThisCapture.Offset > 0) {
5203 if (this->LambdaThisCapture.IsPtr)
5204 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
5205 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
5206 }
5207
5208 // In some circumstances, the 'this' pointer does not actually refer to the
5209 // instance pointer of the current function frame, but e.g. to the declaration
5210 // currently being initialized. Here we emit the necessary instruction(s) for
5211 // this scenario.
5212 if (!InitStackActive)
5213 return this->emitThis(E);
5214
5215 if (!InitStack.empty()) {
5216 // If our init stack is, for example:
5217 // 0 Stack: 3 (decl)
5218 // 1 Stack: 6 (init list)
5219 // 2 Stack: 1 (field)
5220 // 3 Stack: 6 (init list)
5221 // 4 Stack: 1 (field)
5222 //
5223 // We want to find the LAST element in it that's an init list,
5224 // which is marked with the K_InitList marker. The index right
5225 // before that points to an init list. We need to find the
5226 // elements before the K_InitList element that point to a base
5227 // (e.g. a decl or This), optionally followed by field, elem, etc.
5228 // In the example above, we want to emit elements [0..2].
5229 unsigned StartIndex = 0;
5230 unsigned EndIndex = 0;
5231 // Find the init list.
5232 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5233 if (InitStack[StartIndex].Kind == InitLink::K_InitList ||
5234 InitStack[StartIndex].Kind == InitLink::K_This) {
5235 EndIndex = StartIndex;
5236 --StartIndex;
5237 break;
5238 }
5239 }
5240
5241 // Walk backwards to find the base.
5242 for (; StartIndex > 0; --StartIndex) {
5243 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5244 continue;
5245
5246 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5247 InitStack[StartIndex].Kind != InitLink::K_Elem)
5248 break;
5249 }
5250
5251 // Emit the instructions.
5252 for (unsigned I = StartIndex; I != EndIndex; ++I) {
5253 if (InitStack[I].Kind == InitLink::K_InitList)
5254 continue;
5255 if (!InitStack[I].template emit<Emitter>(this, E))
5256 return false;
5257 }
5258 return true;
5259 }
5260 return this->emitThis(E);
5261 }
5262
visitStmt(const Stmt * S)5263 template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
5264 switch (S->getStmtClass()) {
5265 case Stmt::CompoundStmtClass:
5266 return visitCompoundStmt(cast<CompoundStmt>(S));
5267 case Stmt::DeclStmtClass:
5268 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
5269 case Stmt::ReturnStmtClass:
5270 return visitReturnStmt(cast<ReturnStmt>(S));
5271 case Stmt::IfStmtClass:
5272 return visitIfStmt(cast<IfStmt>(S));
5273 case Stmt::WhileStmtClass:
5274 return visitWhileStmt(cast<WhileStmt>(S));
5275 case Stmt::DoStmtClass:
5276 return visitDoStmt(cast<DoStmt>(S));
5277 case Stmt::ForStmtClass:
5278 return visitForStmt(cast<ForStmt>(S));
5279 case Stmt::CXXForRangeStmtClass:
5280 return visitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
5281 case Stmt::BreakStmtClass:
5282 return visitBreakStmt(cast<BreakStmt>(S));
5283 case Stmt::ContinueStmtClass:
5284 return visitContinueStmt(cast<ContinueStmt>(S));
5285 case Stmt::SwitchStmtClass:
5286 return visitSwitchStmt(cast<SwitchStmt>(S));
5287 case Stmt::CaseStmtClass:
5288 return visitCaseStmt(cast<CaseStmt>(S));
5289 case Stmt::DefaultStmtClass:
5290 return visitDefaultStmt(cast<DefaultStmt>(S));
5291 case Stmt::AttributedStmtClass:
5292 return visitAttributedStmt(cast<AttributedStmt>(S));
5293 case Stmt::CXXTryStmtClass:
5294 return visitCXXTryStmt(cast<CXXTryStmt>(S));
5295 case Stmt::NullStmtClass:
5296 return true;
5297 // Always invalid statements.
5298 case Stmt::GCCAsmStmtClass:
5299 case Stmt::MSAsmStmtClass:
5300 case Stmt::GotoStmtClass:
5301 return this->emitInvalid(S);
5302 case Stmt::LabelStmtClass:
5303 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
5304 default: {
5305 if (const auto *E = dyn_cast<Expr>(S))
5306 return this->discard(E);
5307 return false;
5308 }
5309 }
5310 }
5311
5312 template <class Emitter>
visitCompoundStmt(const CompoundStmt * S)5313 bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) {
5314 BlockScope<Emitter> Scope(this);
5315 for (const auto *InnerStmt : S->body())
5316 if (!visitStmt(InnerStmt))
5317 return false;
5318 return Scope.destroyLocals();
5319 }
5320
5321 template <class Emitter>
maybeEmitDeferredVarInit(const VarDecl * VD)5322 bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
5323 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
5324 for (auto *BD : DD->flat_bindings())
5325 if (auto *KD = BD->getHoldingVar(); KD && !this->visitVarDecl(KD))
5326 return false;
5327 }
5328 return true;
5329 }
5330
5331 template <class Emitter>
visitDeclStmt(const DeclStmt * DS,bool EvaluateConditionDecl)5332 bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS,
5333 bool EvaluateConditionDecl) {
5334 for (const auto *D : DS->decls()) {
5335 if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, BaseUsingDecl,
5336 FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(D))
5337 continue;
5338
5339 const auto *VD = dyn_cast<VarDecl>(D);
5340 if (!VD)
5341 return false;
5342 if (!this->visitVarDecl(VD))
5343 return false;
5344
5345 // Register decomposition decl holding vars.
5346 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
5347 return false;
5348 }
5349
5350 return true;
5351 }
5352
5353 template <class Emitter>
visitReturnStmt(const ReturnStmt * RS)5354 bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
5355 if (this->InStmtExpr)
5356 return this->emitUnsupported(RS);
5357
5358 if (const Expr *RE = RS->getRetValue()) {
5359 LocalScope<Emitter> RetScope(this);
5360 if (ReturnType) {
5361 // Primitive types are simply returned.
5362 if (!this->visit(RE))
5363 return false;
5364 this->emitCleanup();
5365 return this->emitRet(*ReturnType, RS);
5366 } else if (RE->getType()->isVoidType()) {
5367 if (!this->visit(RE))
5368 return false;
5369 } else {
5370 InitLinkScope<Emitter> ILS(this, InitLink::RVO());
5371 // RVO - construct the value in the return location.
5372 if (!this->emitRVOPtr(RE))
5373 return false;
5374 if (!this->visitInitializer(RE))
5375 return false;
5376 if (!this->emitPopPtr(RE))
5377 return false;
5378
5379 this->emitCleanup();
5380 return this->emitRetVoid(RS);
5381 }
5382 }
5383
5384 // Void return.
5385 this->emitCleanup();
5386 return this->emitRetVoid(RS);
5387 }
5388
visitIfStmt(const IfStmt * IS)5389 template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
5390 auto visitChildStmt = [&](const Stmt *S) -> bool {
5391 LocalScope<Emitter> SScope(this);
5392 if (!visitStmt(S))
5393 return false;
5394 return SScope.destroyLocals();
5395 };
5396 if (auto *CondInit = IS->getInit())
5397 if (!visitStmt(CondInit))
5398 return false;
5399
5400 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
5401 if (!visitDeclStmt(CondDecl))
5402 return false;
5403
5404 // Save ourselves compiling some code and the jumps, etc. if the condition is
5405 // stataically known to be either true or false. We could look at more cases
5406 // here, but I think all the ones that actually happen are using a
5407 // ConstantExpr.
5408 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
5409 if (*BoolValue)
5410 return visitChildStmt(IS->getThen());
5411 else if (const Stmt *Else = IS->getElse())
5412 return visitChildStmt(Else);
5413 return true;
5414 }
5415
5416 // Otherwise, compile the condition.
5417 if (IS->isNonNegatedConsteval()) {
5418 if (!this->emitIsConstantContext(IS))
5419 return false;
5420 } else if (IS->isNegatedConsteval()) {
5421 if (!this->emitIsConstantContext(IS))
5422 return false;
5423 if (!this->emitInv(IS))
5424 return false;
5425 } else {
5426 if (!this->visitBool(IS->getCond()))
5427 return false;
5428 }
5429
5430 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
5431 return false;
5432
5433 if (const Stmt *Else = IS->getElse()) {
5434 LabelTy LabelElse = this->getLabel();
5435 LabelTy LabelEnd = this->getLabel();
5436 if (!this->jumpFalse(LabelElse))
5437 return false;
5438 if (!visitChildStmt(IS->getThen()))
5439 return false;
5440 if (!this->jump(LabelEnd))
5441 return false;
5442 this->emitLabel(LabelElse);
5443 if (!visitChildStmt(Else))
5444 return false;
5445 this->emitLabel(LabelEnd);
5446 } else {
5447 LabelTy LabelEnd = this->getLabel();
5448 if (!this->jumpFalse(LabelEnd))
5449 return false;
5450 if (!visitChildStmt(IS->getThen()))
5451 return false;
5452 this->emitLabel(LabelEnd);
5453 }
5454
5455 return true;
5456 }
5457
5458 template <class Emitter>
visitWhileStmt(const WhileStmt * S)5459 bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
5460 const Expr *Cond = S->getCond();
5461 const Stmt *Body = S->getBody();
5462
5463 LabelTy CondLabel = this->getLabel(); // Label before the condition.
5464 LabelTy EndLabel = this->getLabel(); // Label after the loop.
5465 LoopScope<Emitter> LS(this, EndLabel, CondLabel);
5466
5467 this->fallthrough(CondLabel);
5468 this->emitLabel(CondLabel);
5469
5470 {
5471 LocalScope<Emitter> CondScope(this);
5472 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5473 if (!visitDeclStmt(CondDecl))
5474 return false;
5475
5476 if (!this->visitBool(Cond))
5477 return false;
5478
5479 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
5480 return false;
5481
5482 if (!this->jumpFalse(EndLabel))
5483 return false;
5484
5485 if (!this->visitStmt(Body))
5486 return false;
5487
5488 if (!CondScope.destroyLocals())
5489 return false;
5490 }
5491 if (!this->jump(CondLabel))
5492 return false;
5493 this->fallthrough(EndLabel);
5494 this->emitLabel(EndLabel);
5495
5496 return true;
5497 }
5498
visitDoStmt(const DoStmt * S)5499 template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
5500 const Expr *Cond = S->getCond();
5501 const Stmt *Body = S->getBody();
5502
5503 LabelTy StartLabel = this->getLabel();
5504 LabelTy EndLabel = this->getLabel();
5505 LabelTy CondLabel = this->getLabel();
5506 LoopScope<Emitter> LS(this, EndLabel, CondLabel);
5507
5508 this->fallthrough(StartLabel);
5509 this->emitLabel(StartLabel);
5510
5511 {
5512 LocalScope<Emitter> CondScope(this);
5513 if (!this->visitStmt(Body))
5514 return false;
5515 this->fallthrough(CondLabel);
5516 this->emitLabel(CondLabel);
5517 if (!this->visitBool(Cond))
5518 return false;
5519
5520 if (!CondScope.destroyLocals())
5521 return false;
5522 }
5523 if (!this->jumpTrue(StartLabel))
5524 return false;
5525
5526 this->fallthrough(EndLabel);
5527 this->emitLabel(EndLabel);
5528 return true;
5529 }
5530
5531 template <class Emitter>
visitForStmt(const ForStmt * S)5532 bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
5533 // for (Init; Cond; Inc) { Body }
5534 const Stmt *Init = S->getInit();
5535 const Expr *Cond = S->getCond();
5536 const Expr *Inc = S->getInc();
5537 const Stmt *Body = S->getBody();
5538
5539 LabelTy EndLabel = this->getLabel();
5540 LabelTy CondLabel = this->getLabel();
5541 LabelTy IncLabel = this->getLabel();
5542 LoopScope<Emitter> LS(this, EndLabel, IncLabel);
5543
5544 if (Init && !this->visitStmt(Init))
5545 return false;
5546
5547 this->fallthrough(CondLabel);
5548 this->emitLabel(CondLabel);
5549
5550 // Start of loop body.
5551 LocalScope<Emitter> CondScope(this);
5552 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5553 if (!visitDeclStmt(CondDecl))
5554 return false;
5555
5556 if (Cond) {
5557 if (!this->visitBool(Cond))
5558 return false;
5559 if (!this->jumpFalse(EndLabel))
5560 return false;
5561 }
5562 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
5563 return false;
5564
5565 if (Body && !this->visitStmt(Body))
5566 return false;
5567
5568 this->fallthrough(IncLabel);
5569 this->emitLabel(IncLabel);
5570 if (Inc && !this->discard(Inc))
5571 return false;
5572
5573 if (!CondScope.destroyLocals())
5574 return false;
5575 if (!this->jump(CondLabel))
5576 return false;
5577 // End of loop body.
5578
5579 this->emitLabel(EndLabel);
5580 // If we jumped out of the loop above, we still need to clean up the condition
5581 // scope.
5582 return CondScope.destroyLocals();
5583 }
5584
5585 template <class Emitter>
visitCXXForRangeStmt(const CXXForRangeStmt * S)5586 bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
5587 const Stmt *Init = S->getInit();
5588 const Expr *Cond = S->getCond();
5589 const Expr *Inc = S->getInc();
5590 const Stmt *Body = S->getBody();
5591 const Stmt *BeginStmt = S->getBeginStmt();
5592 const Stmt *RangeStmt = S->getRangeStmt();
5593 const Stmt *EndStmt = S->getEndStmt();
5594
5595 LabelTy EndLabel = this->getLabel();
5596 LabelTy CondLabel = this->getLabel();
5597 LabelTy IncLabel = this->getLabel();
5598 LoopScope<Emitter> LS(this, EndLabel, IncLabel);
5599
5600 // Emit declarations needed in the loop.
5601 if (Init && !this->visitStmt(Init))
5602 return false;
5603 if (!this->visitStmt(RangeStmt))
5604 return false;
5605 if (!this->visitStmt(BeginStmt))
5606 return false;
5607 if (!this->visitStmt(EndStmt))
5608 return false;
5609
5610 // Now the condition as well as the loop variable assignment.
5611 this->fallthrough(CondLabel);
5612 this->emitLabel(CondLabel);
5613 if (!this->visitBool(Cond))
5614 return false;
5615 if (!this->jumpFalse(EndLabel))
5616 return false;
5617
5618 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
5619 return false;
5620
5621 // Body.
5622 {
5623 if (!this->visitStmt(Body))
5624 return false;
5625
5626 this->fallthrough(IncLabel);
5627 this->emitLabel(IncLabel);
5628 if (!this->discard(Inc))
5629 return false;
5630 }
5631
5632 if (!this->jump(CondLabel))
5633 return false;
5634
5635 this->fallthrough(EndLabel);
5636 this->emitLabel(EndLabel);
5637 return true;
5638 }
5639
5640 template <class Emitter>
visitBreakStmt(const BreakStmt * S)5641 bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
5642 if (!BreakLabel)
5643 return false;
5644
5645 for (VariableScope<Emitter> *C = VarScope; C != BreakVarScope;
5646 C = C->getParent())
5647 C->emitDestruction();
5648 return this->jump(*BreakLabel);
5649 }
5650
5651 template <class Emitter>
visitContinueStmt(const ContinueStmt * S)5652 bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
5653 if (!ContinueLabel)
5654 return false;
5655
5656 for (VariableScope<Emitter> *C = VarScope;
5657 C && C->getParent() != ContinueVarScope; C = C->getParent())
5658 C->emitDestruction();
5659 return this->jump(*ContinueLabel);
5660 }
5661
5662 template <class Emitter>
visitSwitchStmt(const SwitchStmt * S)5663 bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
5664 const Expr *Cond = S->getCond();
5665 if (Cond->containsErrors())
5666 return false;
5667
5668 PrimType CondT = this->classifyPrim(Cond->getType());
5669 LocalScope<Emitter> LS(this);
5670
5671 LabelTy EndLabel = this->getLabel();
5672 OptLabelTy DefaultLabel = std::nullopt;
5673 unsigned CondVar =
5674 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
5675
5676 if (const auto *CondInit = S->getInit())
5677 if (!visitStmt(CondInit))
5678 return false;
5679
5680 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5681 if (!visitDeclStmt(CondDecl))
5682 return false;
5683
5684 // Initialize condition variable.
5685 if (!this->visit(Cond))
5686 return false;
5687 if (!this->emitSetLocal(CondT, CondVar, S))
5688 return false;
5689
5690 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
5691 return false;
5692
5693 CaseMap CaseLabels;
5694 // Create labels and comparison ops for all case statements.
5695 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
5696 SC = SC->getNextSwitchCase()) {
5697 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
5698 // FIXME: Implement ranges.
5699 if (CS->caseStmtIsGNURange())
5700 return false;
5701 CaseLabels[SC] = this->getLabel();
5702
5703 const Expr *Value = CS->getLHS();
5704 PrimType ValueT = this->classifyPrim(Value->getType());
5705
5706 // Compare the case statement's value to the switch condition.
5707 if (!this->emitGetLocal(CondT, CondVar, CS))
5708 return false;
5709 if (!this->visit(Value))
5710 return false;
5711
5712 // Compare and jump to the case label.
5713 if (!this->emitEQ(ValueT, S))
5714 return false;
5715 if (!this->jumpTrue(CaseLabels[CS]))
5716 return false;
5717 } else {
5718 assert(!DefaultLabel);
5719 DefaultLabel = this->getLabel();
5720 }
5721 }
5722
5723 // If none of the conditions above were true, fall through to the default
5724 // statement or jump after the switch statement.
5725 if (DefaultLabel) {
5726 if (!this->jump(*DefaultLabel))
5727 return false;
5728 } else {
5729 if (!this->jump(EndLabel))
5730 return false;
5731 }
5732
5733 SwitchScope<Emitter> SS(this, std::move(CaseLabels), EndLabel, DefaultLabel);
5734 if (!this->visitStmt(S->getBody()))
5735 return false;
5736 this->emitLabel(EndLabel);
5737
5738 return LS.destroyLocals();
5739 }
5740
5741 template <class Emitter>
visitCaseStmt(const CaseStmt * S)5742 bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) {
5743 this->emitLabel(CaseLabels[S]);
5744 return this->visitStmt(S->getSubStmt());
5745 }
5746
5747 template <class Emitter>
visitDefaultStmt(const DefaultStmt * S)5748 bool Compiler<Emitter>::visitDefaultStmt(const DefaultStmt *S) {
5749 this->emitLabel(*DefaultLabel);
5750 return this->visitStmt(S->getSubStmt());
5751 }
5752
5753 template <class Emitter>
visitAttributedStmt(const AttributedStmt * S)5754 bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
5755 if (this->Ctx.getLangOpts().CXXAssumptions &&
5756 !this->Ctx.getLangOpts().MSVCCompat) {
5757 for (const Attr *A : S->getAttrs()) {
5758 auto *AA = dyn_cast<CXXAssumeAttr>(A);
5759 if (!AA)
5760 continue;
5761
5762 assert(isa<NullStmt>(S->getSubStmt()));
5763
5764 const Expr *Assumption = AA->getAssumption();
5765 if (Assumption->isValueDependent())
5766 return false;
5767
5768 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
5769 continue;
5770
5771 // Evaluate assumption.
5772 if (!this->visitBool(Assumption))
5773 return false;
5774
5775 if (!this->emitAssume(Assumption))
5776 return false;
5777 }
5778 }
5779
5780 // Ignore other attributes.
5781 return this->visitStmt(S->getSubStmt());
5782 }
5783
5784 template <class Emitter>
visitCXXTryStmt(const CXXTryStmt * S)5785 bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
5786 // Ignore all handlers.
5787 return this->visitStmt(S->getTryBlock());
5788 }
5789
5790 template <class Emitter>
emitLambdaStaticInvokerBody(const CXXMethodDecl * MD)5791 bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
5792 assert(MD->isLambdaStaticInvoker());
5793 assert(MD->hasBody());
5794 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
5795
5796 const CXXRecordDecl *ClosureClass = MD->getParent();
5797 const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
5798 assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
5799 const Function *Func = this->getFunction(LambdaCallOp);
5800 if (!Func)
5801 return false;
5802 assert(Func->hasThisPointer());
5803 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
5804
5805 if (Func->hasRVO()) {
5806 if (!this->emitRVOPtr(MD))
5807 return false;
5808 }
5809
5810 // The lambda call operator needs an instance pointer, but we don't have
5811 // one here, and we don't need one either because the lambda cannot have
5812 // any captures, as verified above. Emit a null pointer. This is then
5813 // special-cased when interpreting to not emit any misleading diagnostics.
5814 if (!this->emitNullPtr(0, nullptr, MD))
5815 return false;
5816
5817 // Forward all arguments from the static invoker to the lambda call operator.
5818 for (const ParmVarDecl *PVD : MD->parameters()) {
5819 auto It = this->Params.find(PVD);
5820 assert(It != this->Params.end());
5821
5822 // We do the lvalue-to-rvalue conversion manually here, so no need
5823 // to care about references.
5824 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
5825 if (!this->emitGetParam(ParamType, It->second.Offset, MD))
5826 return false;
5827 }
5828
5829 if (!this->emitCall(Func, 0, LambdaCallOp))
5830 return false;
5831
5832 this->emitCleanup();
5833 if (ReturnType)
5834 return this->emitRet(*ReturnType, MD);
5835
5836 // Nothing to do, since we emitted the RVO pointer above.
5837 return this->emitRetVoid(MD);
5838 }
5839
5840 template <class Emitter>
checkLiteralType(const Expr * E)5841 bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
5842 if (Ctx.getLangOpts().CPlusPlus23)
5843 return true;
5844
5845 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
5846 return true;
5847
5848 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
5849 }
5850
5851 template <class Emitter>
compileConstructor(const CXXConstructorDecl * Ctor)5852 bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
5853 assert(!ReturnType);
5854
5855 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
5856 const Expr *InitExpr) -> bool {
5857 // We don't know what to do with these, so just return false.
5858 if (InitExpr->getType().isNull())
5859 return false;
5860
5861 if (std::optional<PrimType> T = this->classify(InitExpr)) {
5862 if (!this->visit(InitExpr))
5863 return false;
5864
5865 if (F->isBitField())
5866 return this->emitInitThisBitField(*T, F, FieldOffset, InitExpr);
5867 return this->emitInitThisField(*T, FieldOffset, InitExpr);
5868 }
5869 // Non-primitive case. Get a pointer to the field-to-initialize
5870 // on the stack and call visitInitialzer() for it.
5871 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
5872 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
5873 return false;
5874
5875 if (!this->visitInitializer(InitExpr))
5876 return false;
5877
5878 return this->emitFinishInitPop(InitExpr);
5879 };
5880
5881 const RecordDecl *RD = Ctor->getParent();
5882 const Record *R = this->getRecord(RD);
5883 if (!R)
5884 return false;
5885
5886 if (R->isUnion() && Ctor->isCopyOrMoveConstructor()) {
5887 if (R->getNumFields() == 0)
5888 return this->emitRetVoid(Ctor);
5889 // union copy and move ctors are special.
5890 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
5891 if (!this->emitThis(Ctor))
5892 return false;
5893
5894 auto PVD = Ctor->getParamDecl(0);
5895 ParamOffset PO = this->Params[PVD]; // Must exist.
5896
5897 if (!this->emitGetParam(PT_Ptr, PO.Offset, Ctor))
5898 return false;
5899
5900 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
5901 this->emitRetVoid(Ctor);
5902 }
5903
5904 InitLinkScope<Emitter> InitScope(this, InitLink::This());
5905 for (const auto *Init : Ctor->inits()) {
5906 // Scope needed for the initializers.
5907 BlockScope<Emitter> Scope(this);
5908
5909 const Expr *InitExpr = Init->getInit();
5910 if (const FieldDecl *Member = Init->getMember()) {
5911 const Record::Field *F = R->getField(Member);
5912
5913 if (!emitFieldInitializer(F, F->Offset, InitExpr))
5914 return false;
5915 } else if (const Type *Base = Init->getBaseClass()) {
5916 const auto *BaseDecl = Base->getAsCXXRecordDecl();
5917 assert(BaseDecl);
5918
5919 if (Init->isBaseVirtual()) {
5920 assert(R->getVirtualBase(BaseDecl));
5921 if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
5922 return false;
5923
5924 } else {
5925 // Base class initializer.
5926 // Get This Base and call initializer on it.
5927 const Record::Base *B = R->getBase(BaseDecl);
5928 assert(B);
5929 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
5930 return false;
5931 }
5932
5933 if (!this->visitInitializer(InitExpr))
5934 return false;
5935 if (!this->emitFinishInitPop(InitExpr))
5936 return false;
5937 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
5938 assert(IFD->getChainingSize() >= 2);
5939
5940 unsigned NestedFieldOffset = 0;
5941 const Record::Field *NestedField = nullptr;
5942 for (const NamedDecl *ND : IFD->chain()) {
5943 const auto *FD = cast<FieldDecl>(ND);
5944 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
5945 assert(FieldRecord);
5946
5947 NestedField = FieldRecord->getField(FD);
5948 assert(NestedField);
5949
5950 NestedFieldOffset += NestedField->Offset;
5951 }
5952 assert(NestedField);
5953
5954 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr))
5955 return false;
5956
5957 // Mark all chain links as initialized.
5958 unsigned InitFieldOffset = 0;
5959 for (const NamedDecl *ND : IFD->chain().drop_back()) {
5960 const auto *FD = cast<FieldDecl>(ND);
5961 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
5962 assert(FieldRecord);
5963 NestedField = FieldRecord->getField(FD);
5964 InitFieldOffset += NestedField->Offset;
5965 assert(NestedField);
5966 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
5967 return false;
5968 if (!this->emitFinishInitPop(InitExpr))
5969 return false;
5970 }
5971
5972 } else {
5973 assert(Init->isDelegatingInitializer());
5974 if (!this->emitThis(InitExpr))
5975 return false;
5976 if (!this->visitInitializer(Init->getInit()))
5977 return false;
5978 if (!this->emitPopPtr(InitExpr))
5979 return false;
5980 }
5981
5982 if (!Scope.destroyLocals())
5983 return false;
5984 }
5985
5986 if (const auto *Body = Ctor->getBody())
5987 if (!visitStmt(Body))
5988 return false;
5989
5990 return this->emitRetVoid(SourceInfo{});
5991 }
5992
5993 template <class Emitter>
compileDestructor(const CXXDestructorDecl * Dtor)5994 bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
5995 const RecordDecl *RD = Dtor->getParent();
5996 const Record *R = this->getRecord(RD);
5997 if (!R)
5998 return false;
5999
6000 if (!Dtor->isTrivial() && Dtor->getBody()) {
6001 if (!this->visitStmt(Dtor->getBody()))
6002 return false;
6003 }
6004
6005 if (!this->emitThis(Dtor))
6006 return false;
6007
6008 if (!this->emitCheckDestruction(Dtor))
6009 return false;
6010
6011 assert(R);
6012 if (!R->isUnion()) {
6013 // First, destroy all fields.
6014 for (const Record::Field &Field : llvm::reverse(R->fields())) {
6015 const Descriptor *D = Field.Desc;
6016 if (!D->isPrimitive() && !D->isPrimitiveArray()) {
6017 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
6018 return false;
6019 if (!this->emitDestruction(D, SourceInfo{}))
6020 return false;
6021 if (!this->emitPopPtr(SourceInfo{}))
6022 return false;
6023 }
6024 }
6025 }
6026
6027 for (const Record::Base &Base : llvm::reverse(R->bases())) {
6028 if (Base.R->isAnonymousUnion())
6029 continue;
6030
6031 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
6032 return false;
6033 if (!this->emitRecordDestruction(Base.R, {}))
6034 return false;
6035 if (!this->emitPopPtr(SourceInfo{}))
6036 return false;
6037 }
6038
6039 // FIXME: Virtual bases.
6040 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
6041 }
6042
6043 template <class Emitter>
compileUnionAssignmentOperator(const CXXMethodDecl * MD)6044 bool Compiler<Emitter>::compileUnionAssignmentOperator(
6045 const CXXMethodDecl *MD) {
6046 if (!this->emitThis(MD))
6047 return false;
6048
6049 auto PVD = MD->getParamDecl(0);
6050 ParamOffset PO = this->Params[PVD]; // Must exist.
6051
6052 if (!this->emitGetParam(PT_Ptr, PO.Offset, MD))
6053 return false;
6054
6055 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
6056 }
6057
6058 template <class Emitter>
visitFunc(const FunctionDecl * F)6059 bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
6060 // Classify the return type.
6061 ReturnType = this->classify(F->getReturnType());
6062
6063 this->CompilingFunction = F;
6064
6065 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
6066 return this->compileConstructor(Ctor);
6067 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
6068 return this->compileDestructor(Dtor);
6069
6070 // Emit custom code if this is a lambda static invoker.
6071 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
6072 const RecordDecl *RD = MD->getParent();
6073
6074 if (RD->isUnion() &&
6075 (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()))
6076 return this->compileUnionAssignmentOperator(MD);
6077
6078 if (MD->isLambdaStaticInvoker())
6079 return this->emitLambdaStaticInvokerBody(MD);
6080 }
6081
6082 // Regular functions.
6083 if (const auto *Body = F->getBody())
6084 if (!visitStmt(Body))
6085 return false;
6086
6087 // Emit a guard return to protect against a code path missing one.
6088 if (F->getReturnType()->isVoidType())
6089 return this->emitRetVoid(SourceInfo{});
6090 return this->emitNoRet(SourceInfo{});
6091 }
6092
6093 template <class Emitter>
VisitUnaryOperator(const UnaryOperator * E)6094 bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
6095 const Expr *SubExpr = E->getSubExpr();
6096 if (SubExpr->getType()->isAnyComplexType())
6097 return this->VisitComplexUnaryOperator(E);
6098 if (SubExpr->getType()->isVectorType())
6099 return this->VisitVectorUnaryOperator(E);
6100 if (SubExpr->getType()->isFixedPointType())
6101 return this->VisitFixedPointUnaryOperator(E);
6102 std::optional<PrimType> T = classify(SubExpr->getType());
6103
6104 switch (E->getOpcode()) {
6105 case UO_PostInc: { // x++
6106 if (!Ctx.getLangOpts().CPlusPlus14)
6107 return this->emitInvalid(E);
6108 if (!T)
6109 return this->emitError(E);
6110
6111 if (!this->visit(SubExpr))
6112 return false;
6113
6114 if (T == PT_Ptr) {
6115 if (!this->emitIncPtr(E))
6116 return false;
6117
6118 return DiscardResult ? this->emitPopPtr(E) : true;
6119 }
6120
6121 if (T == PT_Float) {
6122 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
6123 : this->emitIncf(getFPOptions(E), E);
6124 }
6125
6126 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
6127 : this->emitInc(*T, E->canOverflow(), E);
6128 }
6129 case UO_PostDec: { // x--
6130 if (!Ctx.getLangOpts().CPlusPlus14)
6131 return this->emitInvalid(E);
6132 if (!T)
6133 return this->emitError(E);
6134
6135 if (!this->visit(SubExpr))
6136 return false;
6137
6138 if (T == PT_Ptr) {
6139 if (!this->emitDecPtr(E))
6140 return false;
6141
6142 return DiscardResult ? this->emitPopPtr(E) : true;
6143 }
6144
6145 if (T == PT_Float) {
6146 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
6147 : this->emitDecf(getFPOptions(E), E);
6148 }
6149
6150 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
6151 : this->emitDec(*T, E->canOverflow(), E);
6152 }
6153 case UO_PreInc: { // ++x
6154 if (!Ctx.getLangOpts().CPlusPlus14)
6155 return this->emitInvalid(E);
6156 if (!T)
6157 return this->emitError(E);
6158
6159 if (!this->visit(SubExpr))
6160 return false;
6161
6162 if (T == PT_Ptr) {
6163 if (!this->emitLoadPtr(E))
6164 return false;
6165 if (!this->emitConstUint8(1, E))
6166 return false;
6167 if (!this->emitAddOffsetUint8(E))
6168 return false;
6169 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
6170 }
6171
6172 // Post-inc and pre-inc are the same if the value is to be discarded.
6173 if (DiscardResult) {
6174 if (T == PT_Float)
6175 return this->emitIncfPop(getFPOptions(E), E);
6176 return this->emitIncPop(*T, E->canOverflow(), E);
6177 }
6178
6179 if (T == PT_Float) {
6180 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
6181 if (!this->emitLoadFloat(E))
6182 return false;
6183 APFloat F(TargetSemantics, 1);
6184 if (!this->emitFloat(F, E))
6185 return false;
6186
6187 if (!this->emitAddf(getFPOptions(E), E))
6188 return false;
6189 if (!this->emitStoreFloat(E))
6190 return false;
6191 } else {
6192 assert(isIntegralType(*T));
6193 if (!this->emitPreInc(*T, E->canOverflow(), E))
6194 return false;
6195 }
6196 return E->isGLValue() || this->emitLoadPop(*T, E);
6197 }
6198 case UO_PreDec: { // --x
6199 if (!Ctx.getLangOpts().CPlusPlus14)
6200 return this->emitInvalid(E);
6201 if (!T)
6202 return this->emitError(E);
6203
6204 if (!this->visit(SubExpr))
6205 return false;
6206
6207 if (T == PT_Ptr) {
6208 if (!this->emitLoadPtr(E))
6209 return false;
6210 if (!this->emitConstUint8(1, E))
6211 return false;
6212 if (!this->emitSubOffsetUint8(E))
6213 return false;
6214 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
6215 }
6216
6217 // Post-dec and pre-dec are the same if the value is to be discarded.
6218 if (DiscardResult) {
6219 if (T == PT_Float)
6220 return this->emitDecfPop(getFPOptions(E), E);
6221 return this->emitDecPop(*T, E->canOverflow(), E);
6222 }
6223
6224 if (T == PT_Float) {
6225 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
6226 if (!this->emitLoadFloat(E))
6227 return false;
6228 APFloat F(TargetSemantics, 1);
6229 if (!this->emitFloat(F, E))
6230 return false;
6231
6232 if (!this->emitSubf(getFPOptions(E), E))
6233 return false;
6234 if (!this->emitStoreFloat(E))
6235 return false;
6236 } else {
6237 assert(isIntegralType(*T));
6238 if (!this->emitPreDec(*T, E->canOverflow(), E))
6239 return false;
6240 }
6241 return E->isGLValue() || this->emitLoadPop(*T, E);
6242 }
6243 case UO_LNot: // !x
6244 if (!T)
6245 return this->emitError(E);
6246
6247 if (DiscardResult)
6248 return this->discard(SubExpr);
6249
6250 if (!this->visitBool(SubExpr))
6251 return false;
6252
6253 if (!this->emitInv(E))
6254 return false;
6255
6256 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
6257 return this->emitCast(PT_Bool, ET, E);
6258 return true;
6259 case UO_Minus: // -x
6260 if (!T)
6261 return this->emitError(E);
6262
6263 if (!this->visit(SubExpr))
6264 return false;
6265 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
6266 case UO_Plus: // +x
6267 if (!T)
6268 return this->emitError(E);
6269
6270 if (!this->visit(SubExpr)) // noop
6271 return false;
6272 return DiscardResult ? this->emitPop(*T, E) : true;
6273 case UO_AddrOf: // &x
6274 if (E->getType()->isMemberPointerType()) {
6275 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
6276 // member can be formed.
6277 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
6278 }
6279 // We should already have a pointer when we get here.
6280 return this->delegate(SubExpr);
6281 case UO_Deref: // *x
6282 if (DiscardResult)
6283 return this->discard(SubExpr);
6284
6285 if (!this->visit(SubExpr))
6286 return false;
6287
6288 if (classifyPrim(SubExpr) == PT_Ptr)
6289 return this->emitNarrowPtr(E);
6290 return true;
6291
6292 case UO_Not: // ~x
6293 if (!T)
6294 return this->emitError(E);
6295
6296 if (!this->visit(SubExpr))
6297 return false;
6298 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
6299 case UO_Real: // __real x
6300 assert(T);
6301 return this->delegate(SubExpr);
6302 case UO_Imag: { // __imag x
6303 assert(T);
6304 if (!this->discard(SubExpr))
6305 return false;
6306 return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
6307 }
6308 case UO_Extension:
6309 return this->delegate(SubExpr);
6310 case UO_Coawait:
6311 assert(false && "Unhandled opcode");
6312 }
6313
6314 return false;
6315 }
6316
6317 template <class Emitter>
VisitComplexUnaryOperator(const UnaryOperator * E)6318 bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
6319 const Expr *SubExpr = E->getSubExpr();
6320 assert(SubExpr->getType()->isAnyComplexType());
6321
6322 if (DiscardResult)
6323 return this->discard(SubExpr);
6324
6325 std::optional<PrimType> ResT = classify(E);
6326 auto prepareResult = [=]() -> bool {
6327 if (!ResT && !Initializing) {
6328 std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
6329 if (!LocalIndex)
6330 return false;
6331 return this->emitGetPtrLocal(*LocalIndex, E);
6332 }
6333
6334 return true;
6335 };
6336
6337 // The offset of the temporary, if we created one.
6338 unsigned SubExprOffset = ~0u;
6339 auto createTemp = [=, &SubExprOffset]() -> bool {
6340 SubExprOffset =
6341 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
6342 if (!this->visit(SubExpr))
6343 return false;
6344 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
6345 };
6346
6347 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
6348 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
6349 if (!this->emitGetLocal(PT_Ptr, Offset, E))
6350 return false;
6351 return this->emitArrayElemPop(ElemT, Index, E);
6352 };
6353
6354 switch (E->getOpcode()) {
6355 case UO_Minus:
6356 if (!prepareResult())
6357 return false;
6358 if (!createTemp())
6359 return false;
6360 for (unsigned I = 0; I != 2; ++I) {
6361 if (!getElem(SubExprOffset, I))
6362 return false;
6363 if (!this->emitNeg(ElemT, E))
6364 return false;
6365 if (!this->emitInitElem(ElemT, I, E))
6366 return false;
6367 }
6368 break;
6369
6370 case UO_Plus: // +x
6371 case UO_AddrOf: // &x
6372 case UO_Deref: // *x
6373 return this->delegate(SubExpr);
6374
6375 case UO_LNot:
6376 if (!this->visit(SubExpr))
6377 return false;
6378 if (!this->emitComplexBoolCast(SubExpr))
6379 return false;
6380 if (!this->emitInv(E))
6381 return false;
6382 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
6383 return this->emitCast(PT_Bool, ET, E);
6384 return true;
6385
6386 case UO_Real:
6387 return this->emitComplexReal(SubExpr);
6388
6389 case UO_Imag:
6390 if (!this->visit(SubExpr))
6391 return false;
6392
6393 if (SubExpr->isLValue()) {
6394 if (!this->emitConstUint8(1, E))
6395 return false;
6396 return this->emitArrayElemPtrPopUint8(E);
6397 }
6398
6399 // Since our _Complex implementation does not map to a primitive type,
6400 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
6401 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
6402
6403 case UO_Not: // ~x
6404 if (!this->visit(SubExpr))
6405 return false;
6406 // Negate the imaginary component.
6407 if (!this->emitArrayElem(ElemT, 1, E))
6408 return false;
6409 if (!this->emitNeg(ElemT, E))
6410 return false;
6411 if (!this->emitInitElem(ElemT, 1, E))
6412 return false;
6413 return DiscardResult ? this->emitPopPtr(E) : true;
6414
6415 case UO_Extension:
6416 return this->delegate(SubExpr);
6417
6418 default:
6419 return this->emitInvalid(E);
6420 }
6421
6422 return true;
6423 }
6424
6425 template <class Emitter>
VisitVectorUnaryOperator(const UnaryOperator * E)6426 bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
6427 const Expr *SubExpr = E->getSubExpr();
6428 assert(SubExpr->getType()->isVectorType());
6429
6430 if (DiscardResult)
6431 return this->discard(SubExpr);
6432
6433 auto UnaryOp = E->getOpcode();
6434 if (UnaryOp == UO_Extension)
6435 return this->delegate(SubExpr);
6436
6437 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
6438 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
6439 return this->emitInvalid(E);
6440
6441 // Nothing to do here.
6442 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
6443 return this->delegate(SubExpr);
6444
6445 if (!Initializing) {
6446 std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
6447 if (!LocalIndex)
6448 return false;
6449 if (!this->emitGetPtrLocal(*LocalIndex, E))
6450 return false;
6451 }
6452
6453 // The offset of the temporary, if we created one.
6454 unsigned SubExprOffset =
6455 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
6456 if (!this->visit(SubExpr))
6457 return false;
6458 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
6459 return false;
6460
6461 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
6462 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
6463 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
6464 if (!this->emitGetLocal(PT_Ptr, Offset, E))
6465 return false;
6466 return this->emitArrayElemPop(ElemT, Index, E);
6467 };
6468
6469 switch (UnaryOp) {
6470 case UO_Minus:
6471 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6472 if (!getElem(SubExprOffset, I))
6473 return false;
6474 if (!this->emitNeg(ElemT, E))
6475 return false;
6476 if (!this->emitInitElem(ElemT, I, E))
6477 return false;
6478 }
6479 break;
6480 case UO_LNot: { // !x
6481 // In C++, the logic operators !, &&, || are available for vectors. !v is
6482 // equivalent to v == 0.
6483 //
6484 // The result of the comparison is a vector of the same width and number of
6485 // elements as the comparison operands with a signed integral element type.
6486 //
6487 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
6488 QualType ResultVecTy = E->getType();
6489 PrimType ResultVecElemT =
6490 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
6491 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6492 if (!getElem(SubExprOffset, I))
6493 return false;
6494 // operator ! on vectors returns -1 for 'truth', so negate it.
6495 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
6496 return false;
6497 if (!this->emitInv(E))
6498 return false;
6499 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
6500 return false;
6501 if (!this->emitNeg(ElemT, E))
6502 return false;
6503 if (ElemT != ResultVecElemT &&
6504 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
6505 return false;
6506 if (!this->emitInitElem(ResultVecElemT, I, E))
6507 return false;
6508 }
6509 break;
6510 }
6511 case UO_Not: // ~x
6512 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6513 if (!getElem(SubExprOffset, I))
6514 return false;
6515 if (ElemT == PT_Bool) {
6516 if (!this->emitInv(E))
6517 return false;
6518 } else {
6519 if (!this->emitComp(ElemT, E))
6520 return false;
6521 }
6522 if (!this->emitInitElem(ElemT, I, E))
6523 return false;
6524 }
6525 break;
6526 default:
6527 llvm_unreachable("Unsupported unary operators should be handled up front");
6528 }
6529 return true;
6530 }
6531
6532 template <class Emitter>
visitDeclRef(const ValueDecl * D,const Expr * E)6533 bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
6534 if (DiscardResult)
6535 return true;
6536
6537 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6538 return this->emitConst(ECD->getInitVal(), E);
6539 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
6540 const Function *F = getFunction(FuncDecl);
6541 return F && this->emitGetFnPtr(F, E);
6542 }
6543 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
6544 if (std::optional<unsigned> Index = P.getOrCreateGlobal(D)) {
6545 if (!this->emitGetPtrGlobal(*Index, E))
6546 return false;
6547 if (std::optional<PrimType> T = classify(E->getType())) {
6548 if (!this->visitAPValue(TPOD->getValue(), *T, E))
6549 return false;
6550 return this->emitInitGlobal(*T, *Index, E);
6551 }
6552 return this->visitAPValueInitializer(TPOD->getValue(), E,
6553 TPOD->getType());
6554 }
6555 return false;
6556 }
6557
6558 // References are implemented via pointers, so when we see a DeclRefExpr
6559 // pointing to a reference, we need to get its value directly (i.e. the
6560 // pointer to the actual value) instead of a pointer to the pointer to the
6561 // value.
6562 bool IsReference = D->getType()->isReferenceType();
6563
6564 // Local variables.
6565 if (auto It = Locals.find(D); It != Locals.end()) {
6566 const unsigned Offset = It->second.Offset;
6567 if (IsReference)
6568 return this->emitGetLocal(classifyPrim(E), Offset, E);
6569 return this->emitGetPtrLocal(Offset, E);
6570 }
6571 // Global variables.
6572 if (auto GlobalIndex = P.getGlobal(D)) {
6573 if (IsReference) {
6574 if (!Ctx.getLangOpts().CPlusPlus11)
6575 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
6576 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
6577 }
6578
6579 return this->emitGetPtrGlobal(*GlobalIndex, E);
6580 }
6581 // Function parameters.
6582 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
6583 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
6584 if (IsReference || !It->second.IsPtr)
6585 return this->emitGetParam(classifyPrim(E), It->second.Offset, E);
6586
6587 return this->emitGetPtrParam(It->second.Offset, E);
6588 }
6589 }
6590
6591 // In case we need to re-visit a declaration.
6592 auto revisit = [&](const VarDecl *VD) -> bool {
6593 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
6594 return false;
6595 auto VarState = this->visitDecl(VD, /*IsConstexprUnknown=*/true);
6596
6597 if (!this->emitPopCC(E))
6598 return false;
6599
6600 if (VarState.notCreated())
6601 return true;
6602 if (!VarState)
6603 return false;
6604 // Retry.
6605 return this->visitDeclRef(D, E);
6606 };
6607
6608 // Lambda captures.
6609 if (auto It = this->LambdaCaptures.find(D);
6610 It != this->LambdaCaptures.end()) {
6611 auto [Offset, IsPtr] = It->second;
6612
6613 if (IsPtr)
6614 return this->emitGetThisFieldPtr(Offset, E);
6615 return this->emitGetPtrThisField(Offset, E);
6616 }
6617
6618 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
6619 DRE && DRE->refersToEnclosingVariableOrCapture()) {
6620 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
6621 return revisit(VD);
6622 }
6623
6624 if (const auto *BD = dyn_cast<BindingDecl>(D))
6625 return this->visit(BD->getBinding());
6626
6627 // Avoid infinite recursion.
6628 if (D == InitializingDecl)
6629 return this->emitDummyPtr(D, E);
6630
6631 // Try to lazily visit (or emit dummy pointers for) declarations
6632 // we haven't seen yet.
6633 // For C.
6634 if (!Ctx.getLangOpts().CPlusPlus) {
6635 if (const auto *VD = dyn_cast<VarDecl>(D);
6636 VD && VD->getAnyInitializer() &&
6637 VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak())
6638 return revisit(VD);
6639 return this->emitDummyPtr(D, E);
6640 }
6641
6642 // ... and C++.
6643 const auto *VD = dyn_cast<VarDecl>(D);
6644 if (!VD)
6645 return this->emitDummyPtr(D, E);
6646
6647 const auto typeShouldBeVisited = [&](QualType T) -> bool {
6648 if (T.isConstant(Ctx.getASTContext()))
6649 return true;
6650 return T->isReferenceType();
6651 };
6652
6653 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
6654 typeShouldBeVisited(VD->getType())) {
6655 if (const Expr *Init = VD->getAnyInitializer();
6656 Init && !Init->isValueDependent()) {
6657 // Whether or not the evaluation is successul doesn't really matter
6658 // here -- we will create a global variable in any case, and that
6659 // will have the state of initializer evaluation attached.
6660 APValue V;
6661 SmallVector<PartialDiagnosticAt> Notes;
6662 (void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
6663 true);
6664 return this->visitDeclRef(D, E);
6665 }
6666 return revisit(VD);
6667 }
6668
6669 // FIXME: The evaluateValue() check here is a little ridiculous, since
6670 // it will ultimately call into Context::evaluateAsInitializer(). In
6671 // other words, we're evaluating the initializer, just to know if we can
6672 // evaluate the initializer.
6673 if (VD->isLocalVarDecl() && typeShouldBeVisited(VD->getType()) &&
6674 VD->getInit() && !VD->getInit()->isValueDependent()) {
6675
6676 if (VD->evaluateValue())
6677 return revisit(VD);
6678
6679 if (!IsReference)
6680 return this->emitDummyPtr(D, E);
6681
6682 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
6683 /*InitializerFailed=*/true, E);
6684 }
6685
6686 return this->emitDummyPtr(D, E);
6687 }
6688
6689 template <class Emitter>
VisitDeclRefExpr(const DeclRefExpr * E)6690 bool Compiler<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
6691 const auto *D = E->getDecl();
6692 return this->visitDeclRef(D, E);
6693 }
6694
emitCleanup()6695 template <class Emitter> void Compiler<Emitter>::emitCleanup() {
6696 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent())
6697 C->emitDestruction();
6698 }
6699
6700 template <class Emitter>
collectBaseOffset(const QualType BaseType,const QualType DerivedType)6701 unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
6702 const QualType DerivedType) {
6703 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
6704 if (const auto *R = Ty->getPointeeCXXRecordDecl())
6705 return R;
6706 return Ty->getAsCXXRecordDecl();
6707 };
6708 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
6709 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
6710
6711 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
6712 }
6713
6714 /// Emit casts from a PrimType to another PrimType.
6715 template <class Emitter>
emitPrimCast(PrimType FromT,PrimType ToT,QualType ToQT,const Expr * E)6716 bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
6717 QualType ToQT, const Expr *E) {
6718
6719 if (FromT == PT_Float) {
6720 // Floating to floating.
6721 if (ToT == PT_Float) {
6722 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
6723 return this->emitCastFP(ToSem, getRoundingMode(E), E);
6724 }
6725
6726 if (ToT == PT_IntAP)
6727 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
6728 getFPOptions(E), E);
6729 if (ToT == PT_IntAPS)
6730 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
6731 getFPOptions(E), E);
6732
6733 // Float to integral.
6734 if (isIntegralType(ToT) || ToT == PT_Bool)
6735 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
6736 }
6737
6738 if (isIntegralType(FromT) || FromT == PT_Bool) {
6739 if (ToT == PT_IntAP)
6740 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
6741 if (ToT == PT_IntAPS)
6742 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
6743
6744 // Integral to integral.
6745 if (isIntegralType(ToT) || ToT == PT_Bool)
6746 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
6747
6748 if (ToT == PT_Float) {
6749 // Integral to floating.
6750 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
6751 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
6752 }
6753 }
6754
6755 return false;
6756 }
6757
6758 /// Emits __real(SubExpr)
6759 template <class Emitter>
emitComplexReal(const Expr * SubExpr)6760 bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
6761 assert(SubExpr->getType()->isAnyComplexType());
6762
6763 if (DiscardResult)
6764 return this->discard(SubExpr);
6765
6766 if (!this->visit(SubExpr))
6767 return false;
6768 if (SubExpr->isLValue()) {
6769 if (!this->emitConstUint8(0, SubExpr))
6770 return false;
6771 return this->emitArrayElemPtrPopUint8(SubExpr);
6772 }
6773
6774 // Rvalue, load the actual element.
6775 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
6776 0, SubExpr);
6777 }
6778
6779 template <class Emitter>
emitComplexBoolCast(const Expr * E)6780 bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
6781 assert(!DiscardResult);
6782 PrimType ElemT = classifyComplexElementType(E->getType());
6783 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
6784 // for us, that means (bool)E[0] || (bool)E[1]
6785 if (!this->emitArrayElem(ElemT, 0, E))
6786 return false;
6787 if (ElemT == PT_Float) {
6788 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
6789 return false;
6790 } else {
6791 if (!this->emitCast(ElemT, PT_Bool, E))
6792 return false;
6793 }
6794
6795 // We now have the bool value of E[0] on the stack.
6796 LabelTy LabelTrue = this->getLabel();
6797 if (!this->jumpTrue(LabelTrue))
6798 return false;
6799
6800 if (!this->emitArrayElemPop(ElemT, 1, E))
6801 return false;
6802 if (ElemT == PT_Float) {
6803 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
6804 return false;
6805 } else {
6806 if (!this->emitCast(ElemT, PT_Bool, E))
6807 return false;
6808 }
6809 // Leave the boolean value of E[1] on the stack.
6810 LabelTy EndLabel = this->getLabel();
6811 this->jump(EndLabel);
6812
6813 this->emitLabel(LabelTrue);
6814 if (!this->emitPopPtr(E))
6815 return false;
6816 if (!this->emitConstBool(true, E))
6817 return false;
6818
6819 this->fallthrough(EndLabel);
6820 this->emitLabel(EndLabel);
6821
6822 return true;
6823 }
6824
6825 template <class Emitter>
emitComplexComparison(const Expr * LHS,const Expr * RHS,const BinaryOperator * E)6826 bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
6827 const BinaryOperator *E) {
6828 assert(E->isComparisonOp());
6829 assert(!Initializing);
6830 assert(!DiscardResult);
6831
6832 PrimType ElemT;
6833 bool LHSIsComplex;
6834 unsigned LHSOffset;
6835 if (LHS->getType()->isAnyComplexType()) {
6836 LHSIsComplex = true;
6837 ElemT = classifyComplexElementType(LHS->getType());
6838 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
6839 if (!this->visit(LHS))
6840 return false;
6841 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
6842 return false;
6843 } else {
6844 LHSIsComplex = false;
6845 PrimType LHST = classifyPrim(LHS->getType());
6846 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
6847 if (!this->visit(LHS))
6848 return false;
6849 if (!this->emitSetLocal(LHST, LHSOffset, E))
6850 return false;
6851 }
6852
6853 bool RHSIsComplex;
6854 unsigned RHSOffset;
6855 if (RHS->getType()->isAnyComplexType()) {
6856 RHSIsComplex = true;
6857 ElemT = classifyComplexElementType(RHS->getType());
6858 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
6859 if (!this->visit(RHS))
6860 return false;
6861 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
6862 return false;
6863 } else {
6864 RHSIsComplex = false;
6865 PrimType RHST = classifyPrim(RHS->getType());
6866 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
6867 if (!this->visit(RHS))
6868 return false;
6869 if (!this->emitSetLocal(RHST, RHSOffset, E))
6870 return false;
6871 }
6872
6873 auto getElem = [&](unsigned LocalOffset, unsigned Index,
6874 bool IsComplex) -> bool {
6875 if (IsComplex) {
6876 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
6877 return false;
6878 return this->emitArrayElemPop(ElemT, Index, E);
6879 }
6880 return this->emitGetLocal(ElemT, LocalOffset, E);
6881 };
6882
6883 for (unsigned I = 0; I != 2; ++I) {
6884 // Get both values.
6885 if (!getElem(LHSOffset, I, LHSIsComplex))
6886 return false;
6887 if (!getElem(RHSOffset, I, RHSIsComplex))
6888 return false;
6889 // And compare them.
6890 if (!this->emitEQ(ElemT, E))
6891 return false;
6892
6893 if (!this->emitCastBoolUint8(E))
6894 return false;
6895 }
6896
6897 // We now have two bool values on the stack. Compare those.
6898 if (!this->emitAddUint8(E))
6899 return false;
6900 if (!this->emitConstUint8(2, E))
6901 return false;
6902
6903 if (E->getOpcode() == BO_EQ) {
6904 if (!this->emitEQUint8(E))
6905 return false;
6906 } else if (E->getOpcode() == BO_NE) {
6907 if (!this->emitNEUint8(E))
6908 return false;
6909 } else
6910 return false;
6911
6912 // In C, this returns an int.
6913 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
6914 return this->emitCast(PT_Bool, ResT, E);
6915 return true;
6916 }
6917
6918 /// When calling this, we have a pointer of the local-to-destroy
6919 /// on the stack.
6920 /// Emit destruction of record types (or arrays of record types).
6921 template <class Emitter>
emitRecordDestruction(const Record * R,SourceInfo Loc)6922 bool Compiler<Emitter>::emitRecordDestruction(const Record *R, SourceInfo Loc) {
6923 assert(R);
6924 assert(!R->isAnonymousUnion());
6925 const CXXDestructorDecl *Dtor = R->getDestructor();
6926 if (!Dtor || Dtor->isTrivial())
6927 return true;
6928
6929 assert(Dtor);
6930 const Function *DtorFunc = getFunction(Dtor);
6931 if (!DtorFunc)
6932 return false;
6933 assert(DtorFunc->hasThisPointer());
6934 assert(DtorFunc->getNumParams() == 1);
6935 if (!this->emitDupPtr(Loc))
6936 return false;
6937 return this->emitCall(DtorFunc, 0, Loc);
6938 }
6939 /// When calling this, we have a pointer of the local-to-destroy
6940 /// on the stack.
6941 /// Emit destruction of record types (or arrays of record types).
6942 template <class Emitter>
emitDestruction(const Descriptor * Desc,SourceInfo Loc)6943 bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc,
6944 SourceInfo Loc) {
6945 assert(Desc);
6946 assert(!Desc->isPrimitive());
6947 assert(!Desc->isPrimitiveArray());
6948
6949 // Can happen if the decl is invalid.
6950 if (Desc->isDummy())
6951 return true;
6952
6953 // Arrays.
6954 if (Desc->isArray()) {
6955 const Descriptor *ElemDesc = Desc->ElemDesc;
6956 assert(ElemDesc);
6957
6958 // Don't need to do anything for these.
6959 if (ElemDesc->isPrimitiveArray())
6960 return true;
6961
6962 // If this is an array of record types, check if we need
6963 // to call the element destructors at all. If not, try
6964 // to save the work.
6965 if (const Record *ElemRecord = ElemDesc->ElemRecord) {
6966 if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor();
6967 !Dtor || Dtor->isTrivial())
6968 return true;
6969 }
6970
6971 if (unsigned N = Desc->getNumElems()) {
6972 for (ssize_t I = N - 1; I >= 0; --I) {
6973 if (!this->emitConstUint64(I, Loc))
6974 return false;
6975 if (!this->emitArrayElemPtrUint64(Loc))
6976 return false;
6977 if (!this->emitDestruction(ElemDesc, Loc))
6978 return false;
6979 if (!this->emitPopPtr(Loc))
6980 return false;
6981 }
6982 }
6983 return true;
6984 }
6985
6986 assert(Desc->ElemRecord);
6987 if (Desc->ElemRecord->isAnonymousUnion())
6988 return true;
6989
6990 return this->emitRecordDestruction(Desc->ElemRecord, Loc);
6991 }
6992
6993 /// Create a dummy pointer for the given decl (or expr) and
6994 /// push a pointer to it on the stack.
6995 template <class Emitter>
emitDummyPtr(const DeclTy & D,const Expr * E)6996 bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
6997 assert(!DiscardResult && "Should've been checked before");
6998
6999 unsigned DummyID = P.getOrCreateDummy(D);
7000
7001 if (!this->emitGetPtrGlobal(DummyID, E))
7002 return false;
7003 if (E->getType()->isVoidType())
7004 return true;
7005
7006 // Convert the dummy pointer to another pointer type if we have to.
7007 if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
7008 if (isPtrType(PT))
7009 return this->emitDecayPtr(PT_Ptr, PT, E);
7010 return false;
7011 }
7012 return true;
7013 }
7014
7015 template <class Emitter>
emitFloat(const APFloat & F,const Expr * E)7016 bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
7017 assert(!DiscardResult && "Should've been checked before");
7018
7019 if (Floating::singleWord(F.getSemantics()))
7020 return this->emitConstFloat(Floating(F), E);
7021
7022 APInt I = F.bitcastToAPInt();
7023 return this->emitConstFloat(
7024 Floating(const_cast<uint64_t *>(I.getRawData()),
7025 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
7026 E);
7027 }
7028
7029 // This function is constexpr if and only if To, From, and the types of
7030 // all subobjects of To and From are types T such that...
7031 // (3.1) - is_union_v<T> is false;
7032 // (3.2) - is_pointer_v<T> is false;
7033 // (3.3) - is_member_pointer_v<T> is false;
7034 // (3.4) - is_volatile_v<T> is false; and
7035 // (3.5) - T has no non-static data members of reference type
7036 template <class Emitter>
emitBuiltinBitCast(const CastExpr * E)7037 bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
7038 const Expr *SubExpr = E->getSubExpr();
7039 QualType FromType = SubExpr->getType();
7040 QualType ToType = E->getType();
7041 std::optional<PrimType> ToT = classify(ToType);
7042
7043 assert(!ToType->isReferenceType());
7044
7045 // Prepare storage for the result in case we discard.
7046 if (DiscardResult && !Initializing && !ToT) {
7047 std::optional<unsigned> LocalIndex = allocateLocal(E);
7048 if (!LocalIndex)
7049 return false;
7050 if (!this->emitGetPtrLocal(*LocalIndex, E))
7051 return false;
7052 }
7053
7054 // Get a pointer to the value-to-cast on the stack.
7055 // For CK_LValueToRValueBitCast, this is always an lvalue and
7056 // we later assume it to be one (i.e. a PT_Ptr). However,
7057 // we call this function for other utility methods where
7058 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
7059 if (SubExpr->isGLValue() || FromType->isVectorType()) {
7060 if (!this->visit(SubExpr))
7061 return false;
7062 } else if (std::optional<PrimType> FromT = classify(SubExpr)) {
7063 unsigned TempOffset =
7064 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
7065 if (!this->visit(SubExpr))
7066 return false;
7067 if (!this->emitSetLocal(*FromT, TempOffset, E))
7068 return false;
7069 if (!this->emitGetPtrLocal(TempOffset, E))
7070 return false;
7071 } else {
7072 return false;
7073 }
7074
7075 if (!ToT) {
7076 if (!this->emitBitCast(E))
7077 return false;
7078 return DiscardResult ? this->emitPopPtr(E) : true;
7079 }
7080 assert(ToT);
7081
7082 const llvm::fltSemantics *TargetSemantics = nullptr;
7083 if (ToT == PT_Float)
7084 TargetSemantics = &Ctx.getFloatSemantics(ToType);
7085
7086 // Conversion to a primitive type. FromType can be another
7087 // primitive type, or a record/array.
7088 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
7089 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
7090 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
7091
7092 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
7093 ResultBitWidth, TargetSemantics, E))
7094 return false;
7095
7096 if (DiscardResult)
7097 return this->emitPop(*ToT, E);
7098
7099 return true;
7100 }
7101
7102 namespace clang {
7103 namespace interp {
7104
7105 template class Compiler<ByteCodeEmitter>;
7106 template class Compiler<EvalEmitter>;
7107
7108 } // namespace interp
7109 } // namespace clang
7110