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