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