1 //===--- ByteCodeExprGen.cpp - Code generator for expressions ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ByteCodeExprGen.h" 10 #include "ByteCodeEmitter.h" 11 #include "ByteCodeGenError.h" 12 #include "ByteCodeStmtGen.h" 13 #include "Context.h" 14 #include "Floating.h" 15 #include "Function.h" 16 #include "PrimType.h" 17 #include "Program.h" 18 #include "State.h" 19 20 using namespace clang; 21 using namespace clang::interp; 22 23 using APSInt = llvm::APSInt; 24 25 namespace clang { 26 namespace interp { 27 28 /// Scope used to handle temporaries in toplevel variable declarations. 29 template <class Emitter> class DeclScope final : public VariableScope<Emitter> { 30 public: 31 DeclScope(ByteCodeExprGen<Emitter> *Ctx, const ValueDecl *VD) 32 : VariableScope<Emitter>(Ctx), Scope(Ctx->P, VD) {} 33 34 void addExtended(const Scope::Local &Local) override { 35 return this->addLocal(Local); 36 } 37 38 private: 39 Program::DeclScope Scope; 40 }; 41 42 /// Scope used to handle initialization methods. 43 template <class Emitter> class OptionScope final { 44 public: 45 /// Root constructor, compiling or discarding primitives. 46 OptionScope(ByteCodeExprGen<Emitter> *Ctx, bool NewDiscardResult) 47 : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult) { 48 Ctx->DiscardResult = NewDiscardResult; 49 } 50 51 ~OptionScope() { Ctx->DiscardResult = OldDiscardResult; } 52 53 private: 54 /// Parent context. 55 ByteCodeExprGen<Emitter> *Ctx; 56 /// Old discard flag to restore. 57 bool OldDiscardResult; 58 }; 59 60 } // namespace interp 61 } // namespace clang 62 63 template <class Emitter> 64 bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) { 65 auto *SubExpr = CE->getSubExpr(); 66 switch (CE->getCastKind()) { 67 68 case CK_LValueToRValue: { 69 return dereference( 70 SubExpr, DerefKind::Read, 71 [](PrimType) { 72 // Value loaded - nothing to do here. 73 return true; 74 }, 75 [this, CE](PrimType T) { 76 // Pointer on stack - dereference it. 77 if (!this->emitLoadPop(T, CE)) 78 return false; 79 return DiscardResult ? this->emitPop(T, CE) : true; 80 }); 81 } 82 83 case CK_UncheckedDerivedToBase: 84 case CK_DerivedToBase: { 85 if (!this->visit(SubExpr)) 86 return false; 87 88 return this->emitDerivedToBaseCasts(getRecordTy(SubExpr->getType()), 89 getRecordTy(CE->getType()), CE); 90 } 91 92 case CK_FloatingCast: { 93 if (!this->visit(SubExpr)) 94 return false; 95 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType()); 96 return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE); 97 } 98 99 case CK_IntegralToFloating: { 100 std::optional<PrimType> FromT = classify(SubExpr->getType()); 101 if (!FromT) 102 return false; 103 104 if (!this->visit(SubExpr)) 105 return false; 106 107 const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType()); 108 llvm::RoundingMode RM = getRoundingMode(CE); 109 return this->emitCastIntegralFloating(*FromT, TargetSemantics, RM, CE); 110 } 111 112 case CK_FloatingToBoolean: 113 case CK_FloatingToIntegral: { 114 std::optional<PrimType> ToT = classify(CE->getType()); 115 116 if (!ToT) 117 return false; 118 119 if (!this->visit(SubExpr)) 120 return false; 121 122 return this->emitCastFloatingIntegral(*ToT, CE); 123 } 124 125 case CK_NullToPointer: 126 if (DiscardResult) 127 return true; 128 return this->emitNull(classifyPrim(CE->getType()), CE); 129 130 case CK_ArrayToPointerDecay: 131 case CK_AtomicToNonAtomic: 132 case CK_ConstructorConversion: 133 case CK_FunctionToPointerDecay: 134 case CK_NonAtomicToAtomic: 135 case CK_NoOp: 136 case CK_UserDefinedConversion: 137 return this->visit(SubExpr); 138 139 case CK_IntegralToBoolean: 140 case CK_IntegralCast: { 141 std::optional<PrimType> FromT = classify(SubExpr->getType()); 142 std::optional<PrimType> ToT = classify(CE->getType()); 143 if (!FromT || !ToT) 144 return false; 145 146 if (!this->visit(SubExpr)) 147 return false; 148 149 if (FromT == ToT) 150 return true; 151 152 return this->emitCast(*FromT, *ToT, CE); 153 } 154 155 case CK_PointerToBoolean: { 156 // Just emit p != nullptr for this. 157 if (!this->visit(SubExpr)) 158 return false; 159 160 if (!this->emitNullPtr(CE)) 161 return false; 162 163 return this->emitNEPtr(CE); 164 } 165 166 case CK_ToVoid: 167 return discard(SubExpr); 168 169 default: 170 assert(false && "Cast not implemented"); 171 } 172 llvm_unreachable("Unhandled clang::CastKind enum"); 173 } 174 175 template <class Emitter> 176 bool ByteCodeExprGen<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) { 177 if (DiscardResult) 178 return true; 179 180 return this->emitConst(LE->getValue(), LE); 181 } 182 183 template <class Emitter> 184 bool ByteCodeExprGen<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) { 185 if (DiscardResult) 186 return true; 187 188 return this->emitConstFloat(E->getValue(), E); 189 } 190 191 template <class Emitter> 192 bool ByteCodeExprGen<Emitter>::VisitParenExpr(const ParenExpr *PE) { 193 const Expr *SubExpr = PE->getSubExpr(); 194 195 if (DiscardResult) 196 return this->discard(SubExpr); 197 198 return this->visit(SubExpr); 199 } 200 201 template <class Emitter> 202 bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) { 203 // Need short-circuiting for these. 204 if (BO->isLogicalOp()) 205 return this->VisitLogicalBinOp(BO); 206 207 const Expr *LHS = BO->getLHS(); 208 const Expr *RHS = BO->getRHS(); 209 210 // Typecheck the args. 211 std::optional<PrimType> LT = classify(LHS->getType()); 212 std::optional<PrimType> RT = classify(RHS->getType()); 213 std::optional<PrimType> T = classify(BO->getType()); 214 if (!LT || !RT || !T) { 215 return this->bail(BO); 216 } 217 218 auto Discard = [this, T, BO](bool Result) { 219 if (!Result) 220 return false; 221 return DiscardResult ? this->emitPop(*T, BO) : true; 222 }; 223 224 // Deal with operations which have composite or void types. 225 if (BO->isCommaOp()) { 226 if (!discard(LHS)) 227 return false; 228 return Discard(this->visit(RHS)); 229 } 230 231 // Pointer arithmetic special case. 232 if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) { 233 if (T == PT_Ptr || (LT == PT_Ptr && RT == PT_Ptr)) 234 return this->VisitPointerArithBinOp(BO); 235 } 236 237 if (!visit(LHS) || !visit(RHS)) 238 return false; 239 240 // For languages such as C, cast the result of one 241 // of our comparision opcodes to T (which is usually int). 242 auto MaybeCastToBool = [this, T, BO](bool Result) { 243 if (!Result) 244 return false; 245 if (DiscardResult) 246 return this->emitPop(*T, BO); 247 if (T != PT_Bool) 248 return this->emitCast(PT_Bool, *T, BO); 249 return true; 250 }; 251 252 switch (BO->getOpcode()) { 253 case BO_EQ: 254 return MaybeCastToBool(this->emitEQ(*LT, BO)); 255 case BO_NE: 256 return MaybeCastToBool(this->emitNE(*LT, BO)); 257 case BO_LT: 258 return MaybeCastToBool(this->emitLT(*LT, BO)); 259 case BO_LE: 260 return MaybeCastToBool(this->emitLE(*LT, BO)); 261 case BO_GT: 262 return MaybeCastToBool(this->emitGT(*LT, BO)); 263 case BO_GE: 264 return MaybeCastToBool(this->emitGE(*LT, BO)); 265 case BO_Sub: 266 if (BO->getType()->isFloatingType()) 267 return Discard(this->emitSubf(getRoundingMode(BO), BO)); 268 return Discard(this->emitSub(*T, BO)); 269 case BO_Add: 270 if (BO->getType()->isFloatingType()) 271 return Discard(this->emitAddf(getRoundingMode(BO), BO)); 272 return Discard(this->emitAdd(*T, BO)); 273 case BO_Mul: 274 if (BO->getType()->isFloatingType()) 275 return Discard(this->emitMulf(getRoundingMode(BO), BO)); 276 return Discard(this->emitMul(*T, BO)); 277 case BO_Rem: 278 return Discard(this->emitRem(*T, BO)); 279 case BO_Div: 280 if (BO->getType()->isFloatingType()) 281 return Discard(this->emitDivf(getRoundingMode(BO), BO)); 282 return Discard(this->emitDiv(*T, BO)); 283 case BO_Assign: 284 if (DiscardResult) 285 return this->emitStorePop(*T, BO); 286 return this->emitStore(*T, BO); 287 case BO_And: 288 return Discard(this->emitBitAnd(*T, BO)); 289 case BO_Or: 290 return Discard(this->emitBitOr(*T, BO)); 291 case BO_Shl: 292 return Discard(this->emitShl(*LT, *RT, BO)); 293 case BO_Shr: 294 return Discard(this->emitShr(*LT, *RT, BO)); 295 case BO_Xor: 296 return Discard(this->emitBitXor(*T, BO)); 297 case BO_LOr: 298 case BO_LAnd: 299 llvm_unreachable("Already handled earlier"); 300 default: 301 return this->bail(BO); 302 } 303 304 llvm_unreachable("Unhandled binary op"); 305 } 306 307 /// Perform addition/subtraction of a pointer and an integer or 308 /// subtraction of two pointers. 309 template <class Emitter> 310 bool ByteCodeExprGen<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) { 311 BinaryOperatorKind Op = E->getOpcode(); 312 const Expr *LHS = E->getLHS(); 313 const Expr *RHS = E->getRHS(); 314 315 if ((Op != BO_Add && Op != BO_Sub) || 316 (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType())) 317 return false; 318 319 std::optional<PrimType> LT = classify(LHS); 320 std::optional<PrimType> RT = classify(RHS); 321 322 if (!LT || !RT) 323 return false; 324 325 if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) { 326 if (Op != BO_Sub) 327 return false; 328 329 assert(E->getType()->isIntegerType()); 330 if (!visit(RHS) || !visit(LHS)) 331 return false; 332 333 return this->emitSubPtr(classifyPrim(E->getType()), E); 334 } 335 336 PrimType OffsetType; 337 if (LHS->getType()->isIntegerType()) { 338 if (!visit(RHS) || !visit(LHS)) 339 return false; 340 OffsetType = *LT; 341 } else if (RHS->getType()->isIntegerType()) { 342 if (!visit(LHS) || !visit(RHS)) 343 return false; 344 OffsetType = *RT; 345 } else { 346 return false; 347 } 348 349 if (Op == BO_Add) 350 return this->emitAddOffset(OffsetType, E); 351 else if (Op == BO_Sub) 352 return this->emitSubOffset(OffsetType, E); 353 354 return this->bail(E); 355 } 356 357 template <class Emitter> 358 bool ByteCodeExprGen<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) { 359 assert(E->isLogicalOp()); 360 BinaryOperatorKind Op = E->getOpcode(); 361 const Expr *LHS = E->getLHS(); 362 const Expr *RHS = E->getRHS(); 363 364 if (Op == BO_LOr) { 365 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE. 366 LabelTy LabelTrue = this->getLabel(); 367 LabelTy LabelEnd = this->getLabel(); 368 369 if (!this->visit(LHS)) 370 return false; 371 if (!this->jumpTrue(LabelTrue)) 372 return false; 373 374 if (!this->visit(RHS)) 375 return false; 376 if (!this->jump(LabelEnd)) 377 return false; 378 379 this->emitLabel(LabelTrue); 380 this->emitConstBool(true, E); 381 this->fallthrough(LabelEnd); 382 this->emitLabel(LabelEnd); 383 384 if (DiscardResult) 385 return this->emitPopBool(E); 386 387 return true; 388 } 389 390 // Logical AND. 391 // Visit LHS. Only visit RHS if LHS was TRUE. 392 LabelTy LabelFalse = this->getLabel(); 393 LabelTy LabelEnd = this->getLabel(); 394 395 if (!this->visit(LHS)) 396 return false; 397 if (!this->jumpFalse(LabelFalse)) 398 return false; 399 400 if (!this->visit(RHS)) 401 return false; 402 if (!this->jump(LabelEnd)) 403 return false; 404 405 this->emitLabel(LabelFalse); 406 this->emitConstBool(false, E); 407 this->fallthrough(LabelEnd); 408 this->emitLabel(LabelEnd); 409 410 if (DiscardResult) 411 return this->emitPopBool(E); 412 413 return true; 414 } 415 416 template <class Emitter> 417 bool ByteCodeExprGen<Emitter>::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 418 std::optional<PrimType> T = classify(E); 419 420 if (!T) 421 return false; 422 423 return this->visitZeroInitializer(E->getType(), E); 424 } 425 426 template <class Emitter> 427 bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr( 428 const ArraySubscriptExpr *E) { 429 const Expr *Base = E->getBase(); 430 const Expr *Index = E->getIdx(); 431 PrimType IndexT = classifyPrim(Index->getType()); 432 433 // Take pointer of LHS, add offset from RHS. 434 // What's left on the stack after this is a pointer. 435 if (!this->visit(Base)) 436 return false; 437 438 if (!this->visit(Index)) 439 return false; 440 441 if (!this->emitArrayElemPtrPop(IndexT, E)) 442 return false; 443 444 if (DiscardResult) 445 return this->emitPopPtr(E); 446 447 return true; 448 } 449 450 template <class Emitter> 451 bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) { 452 for (const Expr *Init : E->inits()) { 453 if (!this->visit(Init)) 454 return false; 455 } 456 return true; 457 } 458 459 template <class Emitter> 460 bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr( 461 const SubstNonTypeTemplateParmExpr *E) { 462 return this->visit(E->getReplacement()); 463 } 464 465 template <class Emitter> 466 bool ByteCodeExprGen<Emitter>::VisitConstantExpr(const ConstantExpr *E) { 467 // TODO: Check if the ConstantExpr already has a value set and if so, 468 // use that instead of evaluating it again. 469 return this->visit(E->getSubExpr()); 470 } 471 472 static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx, 473 UnaryExprOrTypeTrait Kind) { 474 bool AlignOfReturnsPreferred = 475 ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; 476 477 // C++ [expr.alignof]p3: 478 // When alignof is applied to a reference type, the result is the 479 // alignment of the referenced type. 480 if (const auto *Ref = T->getAs<ReferenceType>()) 481 T = Ref->getPointeeType(); 482 483 // __alignof is defined to return the preferred alignment. 484 // Before 8, clang returned the preferred alignment for alignof and 485 // _Alignof as well. 486 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) 487 return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T)); 488 489 return ASTCtx.getTypeAlignInChars(T); 490 } 491 492 template <class Emitter> 493 bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr( 494 const UnaryExprOrTypeTraitExpr *E) { 495 UnaryExprOrTypeTrait Kind = E->getKind(); 496 ASTContext &ASTCtx = Ctx.getASTContext(); 497 498 if (Kind == UETT_SizeOf) { 499 QualType ArgType = E->getTypeOfArgument(); 500 CharUnits Size; 501 if (ArgType->isVoidType() || ArgType->isFunctionType()) 502 Size = CharUnits::One(); 503 else { 504 if (ArgType->isDependentType() || !ArgType->isConstantSizeType()) 505 return false; 506 507 Size = ASTCtx.getTypeSizeInChars(ArgType); 508 } 509 510 return this->emitConst(Size.getQuantity(), E); 511 } 512 513 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) { 514 CharUnits Size; 515 516 if (E->isArgumentType()) { 517 QualType ArgType = E->getTypeOfArgument(); 518 519 Size = AlignOfType(ArgType, ASTCtx, Kind); 520 } else { 521 // Argument is an expression, not a type. 522 const Expr *Arg = E->getArgumentExpr()->IgnoreParens(); 523 524 // The kinds of expressions that we have special-case logic here for 525 // should be kept up to date with the special checks for those 526 // expressions in Sema. 527 528 // alignof decl is always accepted, even if it doesn't make sense: we 529 // default to 1 in those cases. 530 if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg)) 531 Size = ASTCtx.getDeclAlign(DRE->getDecl(), 532 /*RefAsPointee*/ true); 533 else if (const auto *ME = dyn_cast<MemberExpr>(Arg)) 534 Size = ASTCtx.getDeclAlign(ME->getMemberDecl(), 535 /*RefAsPointee*/ true); 536 else 537 Size = AlignOfType(Arg->getType(), ASTCtx, Kind); 538 } 539 540 return this->emitConst(Size.getQuantity(), E); 541 } 542 543 return false; 544 } 545 546 template <class Emitter> 547 bool ByteCodeExprGen<Emitter>::VisitMemberExpr(const MemberExpr *E) { 548 if (DiscardResult) 549 return true; 550 551 // 'Base.Member' 552 const Expr *Base = E->getBase(); 553 const ValueDecl *Member = E->getMemberDecl(); 554 555 if (!this->visit(Base)) 556 return false; 557 558 // Base above gives us a pointer on the stack. 559 // TODO: Implement non-FieldDecl members. 560 if (const auto *FD = dyn_cast<FieldDecl>(Member)) { 561 const RecordDecl *RD = FD->getParent(); 562 const Record *R = getRecord(RD); 563 const Record::Field *F = R->getField(FD); 564 // Leave a pointer to the field on the stack. 565 if (F->Decl->getType()->isReferenceType()) 566 return this->emitGetFieldPop(PT_Ptr, F->Offset, E); 567 return this->emitGetPtrField(F->Offset, E); 568 } 569 570 return false; 571 } 572 573 template <class Emitter> 574 bool ByteCodeExprGen<Emitter>::VisitArrayInitIndexExpr( 575 const ArrayInitIndexExpr *E) { 576 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated 577 // stand-alone, e.g. via EvaluateAsInt(). 578 if (!ArrayIndex) 579 return false; 580 return this->emitConst(*ArrayIndex, E); 581 } 582 583 template <class Emitter> 584 bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 585 return this->visit(E->getSourceExpr()); 586 } 587 588 template <class Emitter> 589 bool ByteCodeExprGen<Emitter>::VisitAbstractConditionalOperator( 590 const AbstractConditionalOperator *E) { 591 return this->visitConditional( 592 E, [this](const Expr *E) { return this->visit(E); }); 593 } 594 595 template <class Emitter> 596 bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) { 597 unsigned StringIndex = P.createGlobalString(E); 598 return this->emitGetPtrGlobal(StringIndex, E); 599 } 600 601 template <class Emitter> 602 bool ByteCodeExprGen<Emitter>::VisitCharacterLiteral( 603 const CharacterLiteral *E) { 604 return this->emitConst(E->getValue(), E); 605 } 606 607 template <class Emitter> 608 bool ByteCodeExprGen<Emitter>::VisitFloatCompoundAssignOperator( 609 const CompoundAssignOperator *E) { 610 assert(E->getType()->isFloatingType()); 611 612 const Expr *LHS = E->getLHS(); 613 const Expr *RHS = E->getRHS(); 614 llvm::RoundingMode RM = getRoundingMode(E); 615 QualType LHSComputationType = E->getComputationLHSType(); 616 QualType ResultType = E->getComputationResultType(); 617 std::optional<PrimType> LT = classify(LHSComputationType); 618 std::optional<PrimType> RT = classify(ResultType); 619 620 if (!LT || !RT) 621 return false; 622 623 // C++17 onwards require that we evaluate the RHS first. 624 // Compute RHS and save it in a temporary variable so we can 625 // load it again later. 626 if (!visit(RHS)) 627 return false; 628 629 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true); 630 if (!this->emitSetLocal(*RT, TempOffset, E)) 631 return false; 632 633 // First, visit LHS. 634 if (!visit(LHS)) 635 return false; 636 if (!this->emitLoad(*LT, E)) 637 return false; 638 639 // If necessary, convert LHS to its computation type. 640 if (LHS->getType() != LHSComputationType) { 641 const auto *TargetSemantics = &Ctx.getFloatSemantics(LHSComputationType); 642 643 if (!this->emitCastFP(TargetSemantics, RM, E)) 644 return false; 645 } 646 647 // Now load RHS. 648 if (!this->emitGetLocal(*RT, TempOffset, E)) 649 return false; 650 651 switch (E->getOpcode()) { 652 case BO_AddAssign: 653 if (!this->emitAddf(RM, E)) 654 return false; 655 break; 656 case BO_SubAssign: 657 if (!this->emitSubf(RM, E)) 658 return false; 659 break; 660 case BO_MulAssign: 661 if (!this->emitMulf(RM, E)) 662 return false; 663 break; 664 case BO_DivAssign: 665 if (!this->emitDivf(RM, E)) 666 return false; 667 break; 668 default: 669 return false; 670 } 671 672 // If necessary, convert result to LHS's type. 673 if (LHS->getType() != ResultType) { 674 const auto *TargetSemantics = &Ctx.getFloatSemantics(LHS->getType()); 675 676 if (!this->emitCastFP(TargetSemantics, RM, E)) 677 return false; 678 } 679 680 if (DiscardResult) 681 return this->emitStorePop(*LT, E); 682 return this->emitStore(*LT, E); 683 } 684 685 template <class Emitter> 686 bool ByteCodeExprGen<Emitter>::VisitPointerCompoundAssignOperator( 687 const CompoundAssignOperator *E) { 688 BinaryOperatorKind Op = E->getOpcode(); 689 const Expr *LHS = E->getLHS(); 690 const Expr *RHS = E->getRHS(); 691 std::optional<PrimType> LT = classify(LHS->getType()); 692 std::optional<PrimType> RT = classify(RHS->getType()); 693 694 if (Op != BO_AddAssign && Op != BO_SubAssign) 695 return false; 696 697 if (!LT || !RT) 698 return false; 699 assert(*LT == PT_Ptr); 700 701 if (!visit(LHS)) 702 return false; 703 704 if (!this->emitLoadPtr(LHS)) 705 return false; 706 707 if (!visit(RHS)) 708 return false; 709 710 if (Op == BO_AddAssign) 711 this->emitAddOffset(*RT, E); 712 else 713 this->emitSubOffset(*RT, E); 714 715 if (DiscardResult) 716 return this->emitStorePopPtr(E); 717 return this->emitStorePtr(E); 718 } 719 720 template <class Emitter> 721 bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator( 722 const CompoundAssignOperator *E) { 723 724 // Handle floating point operations separately here, since they 725 // require special care. 726 if (E->getType()->isFloatingType()) 727 return VisitFloatCompoundAssignOperator(E); 728 729 if (E->getType()->isPointerType()) 730 return VisitPointerCompoundAssignOperator(E); 731 732 const Expr *LHS = E->getLHS(); 733 const Expr *RHS = E->getRHS(); 734 std::optional<PrimType> LHSComputationT = 735 classify(E->getComputationLHSType()); 736 std::optional<PrimType> LT = classify(LHS->getType()); 737 std::optional<PrimType> RT = classify(E->getComputationResultType()); 738 std::optional<PrimType> ResultT = classify(E->getType()); 739 740 if (!LT || !RT || !ResultT || !LHSComputationT) 741 return false; 742 743 assert(!E->getType()->isPointerType() && "Handled above"); 744 assert(!E->getType()->isFloatingType() && "Handled above"); 745 746 // C++17 onwards require that we evaluate the RHS first. 747 // Compute RHS and save it in a temporary variable so we can 748 // load it again later. 749 // FIXME: Compound assignments are unsequenced in C, so we might 750 // have to figure out how to reject them. 751 if (!visit(RHS)) 752 return false; 753 754 unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true); 755 756 if (!this->emitSetLocal(*RT, TempOffset, E)) 757 return false; 758 759 // Get LHS pointer, load its value and cast it to the 760 // computation type if necessary. 761 if (!visit(LHS)) 762 return false; 763 if (!this->emitLoad(*LT, E)) 764 return false; 765 if (*LT != *LHSComputationT) { 766 if (!this->emitCast(*LT, *LHSComputationT, E)) 767 return false; 768 } 769 770 // Get the RHS value on the stack. 771 if (!this->emitGetLocal(*RT, TempOffset, E)) 772 return false; 773 774 // Perform operation. 775 switch (E->getOpcode()) { 776 case BO_AddAssign: 777 if (!this->emitAdd(*LHSComputationT, E)) 778 return false; 779 break; 780 case BO_SubAssign: 781 if (!this->emitSub(*LHSComputationT, E)) 782 return false; 783 break; 784 case BO_MulAssign: 785 if (!this->emitMul(*LHSComputationT, E)) 786 return false; 787 break; 788 case BO_DivAssign: 789 if (!this->emitDiv(*LHSComputationT, E)) 790 return false; 791 break; 792 case BO_RemAssign: 793 if (!this->emitRem(*LHSComputationT, E)) 794 return false; 795 break; 796 case BO_ShlAssign: 797 if (!this->emitShl(*LHSComputationT, *RT, E)) 798 return false; 799 break; 800 case BO_ShrAssign: 801 if (!this->emitShr(*LHSComputationT, *RT, E)) 802 return false; 803 break; 804 case BO_AndAssign: 805 if (!this->emitBitAnd(*LHSComputationT, E)) 806 return false; 807 break; 808 case BO_XorAssign: 809 if (!this->emitBitXor(*LHSComputationT, E)) 810 return false; 811 break; 812 case BO_OrAssign: 813 if (!this->emitBitOr(*LHSComputationT, E)) 814 return false; 815 break; 816 default: 817 llvm_unreachable("Unimplemented compound assign operator"); 818 } 819 820 // And now cast from LHSComputationT to ResultT. 821 if (*ResultT != *LHSComputationT) { 822 if (!this->emitCast(*LHSComputationT, *ResultT, E)) 823 return false; 824 } 825 826 // And store the result in LHS. 827 if (DiscardResult) 828 return this->emitStorePop(*ResultT, E); 829 return this->emitStore(*ResultT, E); 830 } 831 832 template <class Emitter> 833 bool ByteCodeExprGen<Emitter>::VisitExprWithCleanups( 834 const ExprWithCleanups *E) { 835 const Expr *SubExpr = E->getSubExpr(); 836 837 assert(E->getNumObjects() == 0 && "TODO: Implement cleanups"); 838 if (DiscardResult) 839 return this->discard(SubExpr); 840 841 return this->visit(SubExpr); 842 } 843 844 template <class Emitter> 845 bool ByteCodeExprGen<Emitter>::VisitMaterializeTemporaryExpr( 846 const MaterializeTemporaryExpr *E) { 847 const Expr *SubExpr = E->getSubExpr(); 848 std::optional<PrimType> SubExprT = classify(SubExpr); 849 850 if (E->getStorageDuration() == SD_Static) { 851 if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) { 852 const LifetimeExtendedTemporaryDecl *TempDecl = 853 E->getLifetimeExtendedTemporaryDecl(); 854 855 if (!this->visitInitializer(SubExpr)) 856 return false; 857 858 if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E)) 859 return false; 860 return this->emitGetPtrGlobal(*GlobalIndex, E); 861 } 862 863 return false; 864 } 865 866 // For everyhing else, use local variables. 867 if (SubExprT) { 868 if (std::optional<unsigned> LocalIndex = allocateLocalPrimitive( 869 SubExpr, *SubExprT, /*IsConst=*/true, /*IsExtended=*/true)) { 870 if (!this->visitInitializer(SubExpr)) 871 return false; 872 this->emitSetLocal(*SubExprT, *LocalIndex, E); 873 return this->emitGetPtrLocal(*LocalIndex, E); 874 } 875 } else { 876 if (std::optional<unsigned> LocalIndex = 877 allocateLocal(SubExpr, /*IsExtended=*/true)) { 878 if (!this->emitGetPtrLocal(*LocalIndex, E)) 879 return false; 880 return this->visitInitializer(SubExpr); 881 } 882 } 883 return false; 884 } 885 886 template <class Emitter> 887 bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr( 888 const CompoundLiteralExpr *E) { 889 std::optional<PrimType> T = classify(E->getType()); 890 const Expr *Init = E->getInitializer(); 891 if (E->isFileScope()) { 892 if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) { 893 if (classify(E->getType())) 894 return this->visit(Init); 895 if (!this->emitGetPtrGlobal(*GlobalIndex, E)) 896 return false; 897 return this->visitInitializer(Init); 898 } 899 } 900 901 // Otherwise, use a local variable. 902 if (T) { 903 // For primitive types, we just visit the initializer. 904 return this->visit(Init); 905 } else { 906 if (std::optional<unsigned> LocalIndex = allocateLocal(Init)) { 907 if (!this->emitGetPtrLocal(*LocalIndex, E)) 908 return false; 909 return this->visitInitializer(Init); 910 } 911 } 912 913 return false; 914 } 915 916 template <class Emitter> 917 bool ByteCodeExprGen<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) { 918 return this->emitConstBool(E->getValue(), E); 919 } 920 921 template <class Emitter> 922 bool ByteCodeExprGen<Emitter>::VisitLambdaExpr(const LambdaExpr *E) { 923 // XXX We assume here that a pointer-to-initialize is on the stack. 924 925 const Record *R = P.getOrCreateRecord(E->getLambdaClass()); 926 927 auto *CaptureInitIt = E->capture_init_begin(); 928 // Initialize all fields (which represent lambda captures) of the 929 // record with their initializers. 930 for (const Record::Field &F : R->fields()) { 931 const Expr *Init = *CaptureInitIt; 932 ++CaptureInitIt; 933 934 if (std::optional<PrimType> T = classify(Init)) { 935 if (!this->visit(Init)) 936 return false; 937 938 if (!this->emitSetField(*T, F.Offset, E)) 939 return false; 940 } else { 941 if (!this->emitDupPtr(E)) 942 return false; 943 944 if (!this->emitGetPtrField(F.Offset, E)) 945 return false; 946 947 if (!this->visitInitializer(Init)) 948 return false; 949 950 if (!this->emitPopPtr(E)) 951 return false; 952 } 953 } 954 955 return true; 956 } 957 958 template <class Emitter> 959 bool ByteCodeExprGen<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) { 960 if (DiscardResult) 961 return true; 962 963 return this->visit(E->getFunctionName()); 964 } 965 966 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) { 967 if (E->containsErrors()) 968 return false; 969 970 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true); 971 return this->Visit(E); 972 } 973 974 template <class Emitter> 975 bool ByteCodeExprGen<Emitter>::visit(const Expr *E) { 976 if (E->containsErrors()) 977 return false; 978 979 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false); 980 return this->Visit(E); 981 } 982 983 template <class Emitter> 984 bool ByteCodeExprGen<Emitter>::visitBool(const Expr *E) { 985 if (std::optional<PrimType> T = classify(E->getType())) { 986 return visit(E); 987 } else { 988 return this->bail(E); 989 } 990 } 991 992 /// Visit a conditional operator, i.e. `A ? B : C`. 993 /// \V determines what function to call for the B and C expressions. 994 template <class Emitter> 995 bool ByteCodeExprGen<Emitter>::visitConditional( 996 const AbstractConditionalOperator *E, 997 llvm::function_ref<bool(const Expr *)> V) { 998 999 const Expr *Condition = E->getCond(); 1000 const Expr *TrueExpr = E->getTrueExpr(); 1001 const Expr *FalseExpr = E->getFalseExpr(); 1002 1003 LabelTy LabelEnd = this->getLabel(); // Label after the operator. 1004 LabelTy LabelFalse = this->getLabel(); // Label for the false expr. 1005 1006 if (!this->visit(Condition)) 1007 return false; 1008 1009 // C special case: Convert to bool because our jump ops need that. 1010 // TODO: We probably want this to be done in visitBool(). 1011 if (std::optional<PrimType> CondT = classify(Condition->getType()); 1012 CondT && CondT != PT_Bool) { 1013 if (!this->emitCast(*CondT, PT_Bool, E)) 1014 return false; 1015 } 1016 1017 if (!this->jumpFalse(LabelFalse)) 1018 return false; 1019 1020 if (!V(TrueExpr)) 1021 return false; 1022 if (!this->jump(LabelEnd)) 1023 return false; 1024 1025 this->emitLabel(LabelFalse); 1026 1027 if (!V(FalseExpr)) 1028 return false; 1029 1030 this->fallthrough(LabelEnd); 1031 this->emitLabel(LabelEnd); 1032 1033 return true; 1034 } 1035 1036 template <class Emitter> 1037 bool ByteCodeExprGen<Emitter>::visitZeroInitializer(QualType QT, 1038 const Expr *E) { 1039 // FIXME: We need the QualType to get the float semantics, but that means we 1040 // classify it over and over again in array situations. 1041 PrimType T = classifyPrim(QT); 1042 1043 switch (T) { 1044 case PT_Bool: 1045 return this->emitZeroBool(E); 1046 case PT_Sint8: 1047 return this->emitZeroSint8(E); 1048 case PT_Uint8: 1049 return this->emitZeroUint8(E); 1050 case PT_Sint16: 1051 return this->emitZeroSint16(E); 1052 case PT_Uint16: 1053 return this->emitZeroUint16(E); 1054 case PT_Sint32: 1055 return this->emitZeroSint32(E); 1056 case PT_Uint32: 1057 return this->emitZeroUint32(E); 1058 case PT_Sint64: 1059 return this->emitZeroSint64(E); 1060 case PT_Uint64: 1061 return this->emitZeroUint64(E); 1062 case PT_Ptr: 1063 return this->emitNullPtr(E); 1064 case PT_FnPtr: 1065 return this->emitNullFnPtr(E); 1066 case PT_Float: { 1067 return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E); 1068 } 1069 } 1070 llvm_unreachable("unknown primitive type"); 1071 } 1072 1073 template <class Emitter> 1074 bool ByteCodeExprGen<Emitter>::dereference( 1075 const Expr *LV, DerefKind AK, llvm::function_ref<bool(PrimType)> Direct, 1076 llvm::function_ref<bool(PrimType)> Indirect) { 1077 if (std::optional<PrimType> T = classify(LV->getType())) { 1078 if (!LV->refersToBitField()) { 1079 // Only primitive, non bit-field types can be dereferenced directly. 1080 if (const auto *DE = dyn_cast<DeclRefExpr>(LV)) { 1081 if (!DE->getDecl()->getType()->isReferenceType()) { 1082 if (const auto *PD = dyn_cast<ParmVarDecl>(DE->getDecl())) 1083 return dereferenceParam(LV, *T, PD, AK, Direct, Indirect); 1084 if (const auto *VD = dyn_cast<VarDecl>(DE->getDecl())) 1085 return dereferenceVar(LV, *T, VD, AK, Direct, Indirect); 1086 } 1087 } 1088 } 1089 1090 if (!visit(LV)) 1091 return false; 1092 return Indirect(*T); 1093 } 1094 1095 return false; 1096 } 1097 1098 template <class Emitter> 1099 bool ByteCodeExprGen<Emitter>::dereferenceParam( 1100 const Expr *LV, PrimType T, const ParmVarDecl *PD, DerefKind AK, 1101 llvm::function_ref<bool(PrimType)> Direct, 1102 llvm::function_ref<bool(PrimType)> Indirect) { 1103 auto It = this->Params.find(PD); 1104 if (It != this->Params.end()) { 1105 unsigned Idx = It->second; 1106 switch (AK) { 1107 case DerefKind::Read: 1108 return DiscardResult ? true : this->emitGetParam(T, Idx, LV); 1109 1110 case DerefKind::Write: 1111 if (!Direct(T)) 1112 return false; 1113 if (!this->emitSetParam(T, Idx, LV)) 1114 return false; 1115 return DiscardResult ? true : this->emitGetPtrParam(Idx, LV); 1116 1117 case DerefKind::ReadWrite: 1118 if (!this->emitGetParam(T, Idx, LV)) 1119 return false; 1120 if (!Direct(T)) 1121 return false; 1122 if (!this->emitSetParam(T, Idx, LV)) 1123 return false; 1124 return DiscardResult ? true : this->emitGetPtrParam(Idx, LV); 1125 } 1126 return true; 1127 } 1128 1129 // If the param is a pointer, we can dereference a dummy value. 1130 if (!DiscardResult && T == PT_Ptr && AK == DerefKind::Read) { 1131 if (auto Idx = P.getOrCreateDummy(PD)) 1132 return this->emitGetPtrGlobal(*Idx, PD); 1133 return false; 1134 } 1135 1136 // Value cannot be produced - try to emit pointer and do stuff with it. 1137 return visit(LV) && Indirect(T); 1138 } 1139 1140 template <class Emitter> 1141 bool ByteCodeExprGen<Emitter>::dereferenceVar( 1142 const Expr *LV, PrimType T, const VarDecl *VD, DerefKind AK, 1143 llvm::function_ref<bool(PrimType)> Direct, 1144 llvm::function_ref<bool(PrimType)> Indirect) { 1145 auto It = Locals.find(VD); 1146 if (It != Locals.end()) { 1147 const auto &L = It->second; 1148 switch (AK) { 1149 case DerefKind::Read: 1150 if (!this->emitGetLocal(T, L.Offset, LV)) 1151 return false; 1152 return DiscardResult ? this->emitPop(T, LV) : true; 1153 1154 case DerefKind::Write: 1155 if (!Direct(T)) 1156 return false; 1157 if (!this->emitSetLocal(T, L.Offset, LV)) 1158 return false; 1159 return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV); 1160 1161 case DerefKind::ReadWrite: 1162 if (!this->emitGetLocal(T, L.Offset, LV)) 1163 return false; 1164 if (!Direct(T)) 1165 return false; 1166 if (!this->emitSetLocal(T, L.Offset, LV)) 1167 return false; 1168 return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV); 1169 } 1170 } else if (auto Idx = P.getGlobal(VD)) { 1171 switch (AK) { 1172 case DerefKind::Read: 1173 if (!this->emitGetGlobal(T, *Idx, LV)) 1174 return false; 1175 return DiscardResult ? this->emitPop(T, LV) : true; 1176 1177 case DerefKind::Write: 1178 if (!Direct(T)) 1179 return false; 1180 if (!this->emitSetGlobal(T, *Idx, LV)) 1181 return false; 1182 return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV); 1183 1184 case DerefKind::ReadWrite: 1185 if (!this->emitGetGlobal(T, *Idx, LV)) 1186 return false; 1187 if (!Direct(T)) 1188 return false; 1189 if (!this->emitSetGlobal(T, *Idx, LV)) 1190 return false; 1191 return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV); 1192 } 1193 } 1194 1195 // If the declaration is a constant value, emit it here even 1196 // though the declaration was not evaluated in the current scope. 1197 // The access mode can only be read in this case. 1198 if (!DiscardResult && AK == DerefKind::Read) { 1199 if (VD->hasLocalStorage() && VD->hasInit() && !VD->isConstexpr()) { 1200 QualType VT = VD->getType(); 1201 if (VT.isConstQualified() && VT->isFundamentalType()) 1202 return this->visit(VD->getInit()); 1203 } 1204 } 1205 1206 // Value cannot be produced - try to emit pointer. 1207 return visit(LV) && Indirect(T); 1208 } 1209 1210 template <class Emitter> 1211 template <typename T> 1212 bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) { 1213 switch (classifyPrim(E->getType())) { 1214 case PT_Sint8: 1215 return this->emitConstSint8(Value, E); 1216 case PT_Uint8: 1217 return this->emitConstUint8(Value, E); 1218 case PT_Sint16: 1219 return this->emitConstSint16(Value, E); 1220 case PT_Uint16: 1221 return this->emitConstUint16(Value, E); 1222 case PT_Sint32: 1223 return this->emitConstSint32(Value, E); 1224 case PT_Uint32: 1225 return this->emitConstUint32(Value, E); 1226 case PT_Sint64: 1227 return this->emitConstSint64(Value, E); 1228 case PT_Uint64: 1229 return this->emitConstUint64(Value, E); 1230 case PT_Bool: 1231 return this->emitConstBool(Value, E); 1232 case PT_Ptr: 1233 case PT_FnPtr: 1234 case PT_Float: 1235 llvm_unreachable("Invalid integral type"); 1236 break; 1237 } 1238 llvm_unreachable("unknown primitive type"); 1239 } 1240 1241 template <class Emitter> 1242 bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) { 1243 if (Value.isSigned()) 1244 return this->emitConst(Value.getSExtValue(), E); 1245 return this->emitConst(Value.getZExtValue(), E); 1246 } 1247 1248 template <class Emitter> 1249 unsigned ByteCodeExprGen<Emitter>::allocateLocalPrimitive(DeclTy &&Src, 1250 PrimType Ty, 1251 bool IsConst, 1252 bool IsExtended) { 1253 // Make sure we don't accidentally register the same decl twice. 1254 if (const auto *VD = 1255 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) { 1256 assert(!P.getGlobal(VD)); 1257 assert(!Locals.contains(VD)); 1258 } 1259 1260 // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g. 1261 // (int){12} in C. Consider using Expr::isTemporaryObject() instead 1262 // or isa<MaterializeTemporaryExpr>(). 1263 Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst, 1264 Src.is<const Expr *>()); 1265 Scope::Local Local = this->createLocal(D); 1266 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) 1267 Locals.insert({VD, Local}); 1268 VarScope->add(Local, IsExtended); 1269 return Local.Offset; 1270 } 1271 1272 template <class Emitter> 1273 std::optional<unsigned> 1274 ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src, bool IsExtended) { 1275 // Make sure we don't accidentally register the same decl twice. 1276 if (const auto *VD = 1277 dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) { 1278 assert(!P.getGlobal(VD)); 1279 assert(!Locals.contains(VD)); 1280 } 1281 1282 QualType Ty; 1283 const ValueDecl *Key = nullptr; 1284 const Expr *Init = nullptr; 1285 bool IsTemporary = false; 1286 if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) { 1287 Key = VD; 1288 Ty = VD->getType(); 1289 1290 if (const auto *VarD = dyn_cast<VarDecl>(VD)) 1291 Init = VarD->getInit(); 1292 } 1293 if (auto *E = Src.dyn_cast<const Expr *>()) { 1294 IsTemporary = true; 1295 Ty = E->getType(); 1296 } 1297 1298 Descriptor *D = P.createDescriptor( 1299 Src, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(), 1300 IsTemporary, /*IsMutable=*/false, Init); 1301 if (!D) 1302 return {}; 1303 1304 Scope::Local Local = this->createLocal(D); 1305 if (Key) 1306 Locals.insert({Key, Local}); 1307 VarScope->add(Local, IsExtended); 1308 return Local.Offset; 1309 } 1310 1311 // NB: When calling this function, we have a pointer to the 1312 // array-to-initialize on the stack. 1313 template <class Emitter> 1314 bool ByteCodeExprGen<Emitter>::visitArrayInitializer(const Expr *Initializer) { 1315 assert(Initializer->getType()->isArrayType()); 1316 1317 // TODO: Fillers? 1318 if (const auto *InitList = dyn_cast<InitListExpr>(Initializer)) { 1319 unsigned ElementIndex = 0; 1320 for (const Expr *Init : InitList->inits()) { 1321 if (std::optional<PrimType> T = classify(Init->getType())) { 1322 // Visit the primitive element like normal. 1323 if (!this->visit(Init)) 1324 return false; 1325 if (!this->emitInitElem(*T, ElementIndex, Init)) 1326 return false; 1327 } else { 1328 // Advance the pointer currently on the stack to the given 1329 // dimension. 1330 if (!this->emitConstUint32(ElementIndex, Init)) 1331 return false; 1332 if (!this->emitArrayElemPtrUint32(Init)) 1333 return false; 1334 if (!visitInitializer(Init)) 1335 return false; 1336 if (!this->emitPopPtr(Init)) 1337 return false; 1338 } 1339 1340 ++ElementIndex; 1341 } 1342 return true; 1343 } else if (const auto *DIE = dyn_cast<CXXDefaultInitExpr>(Initializer)) { 1344 return this->visitInitializer(DIE->getExpr()); 1345 } else if (const auto *AILE = dyn_cast<ArrayInitLoopExpr>(Initializer)) { 1346 // TODO: This compiles to quite a lot of bytecode if the array is larger. 1347 // Investigate compiling this to a loop, or at least try to use 1348 // the AILE's Common expr. 1349 const Expr *SubExpr = AILE->getSubExpr(); 1350 size_t Size = AILE->getArraySize().getZExtValue(); 1351 std::optional<PrimType> ElemT = classify(SubExpr->getType()); 1352 1353 // So, every iteration, we execute an assignment here 1354 // where the LHS is on the stack (the target array) 1355 // and the RHS is our SubExpr. 1356 for (size_t I = 0; I != Size; ++I) { 1357 ArrayIndexScope<Emitter> IndexScope(this, I); 1358 1359 if (ElemT) { 1360 if (!this->visit(SubExpr)) 1361 return false; 1362 if (!this->emitInitElem(*ElemT, I, Initializer)) 1363 return false; 1364 } else { 1365 // Get to our array element and recurse into visitInitializer() 1366 if (!this->emitConstUint64(I, SubExpr)) 1367 return false; 1368 if (!this->emitArrayElemPtrUint64(SubExpr)) 1369 return false; 1370 if (!visitInitializer(SubExpr)) 1371 return false; 1372 if (!this->emitPopPtr(Initializer)) 1373 return false; 1374 } 1375 } 1376 return true; 1377 } else if (const auto *IVIE = dyn_cast<ImplicitValueInitExpr>(Initializer)) { 1378 const ArrayType *AT = IVIE->getType()->getAsArrayTypeUnsafe(); 1379 assert(AT); 1380 const auto *CAT = cast<ConstantArrayType>(AT); 1381 size_t NumElems = CAT->getSize().getZExtValue(); 1382 1383 if (std::optional<PrimType> ElemT = classify(CAT->getElementType())) { 1384 // TODO(perf): For int and bool types, we can probably just skip this 1385 // since we memset our Block*s to 0 and so we have the desired value 1386 // without this. 1387 for (size_t I = 0; I != NumElems; ++I) { 1388 if (!this->visitZeroInitializer(CAT->getElementType(), Initializer)) 1389 return false; 1390 if (!this->emitInitElem(*ElemT, I, Initializer)) 1391 return false; 1392 } 1393 } else { 1394 assert(false && "default initializer for non-primitive type"); 1395 } 1396 1397 return true; 1398 } else if (const auto *Ctor = dyn_cast<CXXConstructExpr>(Initializer)) { 1399 const ConstantArrayType *CAT = 1400 Ctx.getASTContext().getAsConstantArrayType(Ctor->getType()); 1401 assert(CAT); 1402 size_t NumElems = CAT->getSize().getZExtValue(); 1403 const Function *Func = getFunction(Ctor->getConstructor()); 1404 if (!Func || !Func->isConstexpr()) 1405 return false; 1406 1407 // FIXME(perf): We're calling the constructor once per array element here, 1408 // in the old intepreter we had a special-case for trivial constructors. 1409 for (size_t I = 0; I != NumElems; ++I) { 1410 if (!this->emitConstUint64(I, Initializer)) 1411 return false; 1412 if (!this->emitArrayElemPtrUint64(Initializer)) 1413 return false; 1414 1415 // Constructor arguments. 1416 for (const auto *Arg : Ctor->arguments()) { 1417 if (!this->visit(Arg)) 1418 return false; 1419 } 1420 1421 if (!this->emitCall(Func, Initializer)) 1422 return false; 1423 } 1424 return true; 1425 } else if (const auto *SL = dyn_cast<StringLiteral>(Initializer)) { 1426 const ConstantArrayType *CAT = 1427 Ctx.getASTContext().getAsConstantArrayType(SL->getType()); 1428 assert(CAT && "a string literal that's not a constant array?"); 1429 1430 // If the initializer string is too long, a diagnostic has already been 1431 // emitted. Read only the array length from the string literal. 1432 unsigned N = 1433 std::min(unsigned(CAT->getSize().getZExtValue()), SL->getLength()); 1434 size_t CharWidth = SL->getCharByteWidth(); 1435 1436 for (unsigned I = 0; I != N; ++I) { 1437 uint32_t CodeUnit = SL->getCodeUnit(I); 1438 1439 if (CharWidth == 1) { 1440 this->emitConstSint8(CodeUnit, SL); 1441 this->emitInitElemSint8(I, SL); 1442 } else if (CharWidth == 2) { 1443 this->emitConstUint16(CodeUnit, SL); 1444 this->emitInitElemUint16(I, SL); 1445 } else if (CharWidth == 4) { 1446 this->emitConstUint32(CodeUnit, SL); 1447 this->emitInitElemUint32(I, SL); 1448 } else { 1449 llvm_unreachable("unsupported character width"); 1450 } 1451 } 1452 return true; 1453 } else if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Initializer)) { 1454 return visitInitializer(CLE->getInitializer()); 1455 } else if (const auto *EWC = dyn_cast<ExprWithCleanups>(Initializer)) { 1456 return visitInitializer(EWC->getSubExpr()); 1457 } 1458 1459 assert(false && "Unknown expression for array initialization"); 1460 return false; 1461 } 1462 1463 template <class Emitter> 1464 bool ByteCodeExprGen<Emitter>::visitRecordInitializer(const Expr *Initializer) { 1465 Initializer = Initializer->IgnoreParenImpCasts(); 1466 assert(Initializer->getType()->isRecordType()); 1467 1468 if (const auto CtorExpr = dyn_cast<CXXConstructExpr>(Initializer)) { 1469 const Function *Func = getFunction(CtorExpr->getConstructor()); 1470 1471 if (!Func) 1472 return false; 1473 1474 // The This pointer is already on the stack because this is an initializer, 1475 // but we need to dup() so the call() below has its own copy. 1476 if (!this->emitDupPtr(Initializer)) 1477 return false; 1478 1479 // Constructor arguments. 1480 for (const auto *Arg : CtorExpr->arguments()) { 1481 if (!this->visit(Arg)) 1482 return false; 1483 } 1484 1485 return this->emitCall(Func, Initializer); 1486 } else if (const auto *InitList = dyn_cast<InitListExpr>(Initializer)) { 1487 const Record *R = getRecord(InitList->getType()); 1488 1489 unsigned InitIndex = 0; 1490 for (const Expr *Init : InitList->inits()) { 1491 1492 if (!this->emitDupPtr(Initializer)) 1493 return false; 1494 1495 if (std::optional<PrimType> T = classify(Init)) { 1496 const Record::Field *FieldToInit = R->getField(InitIndex); 1497 if (!this->visit(Init)) 1498 return false; 1499 1500 if (!this->emitInitField(*T, FieldToInit->Offset, Initializer)) 1501 return false; 1502 1503 if (!this->emitPopPtr(Initializer)) 1504 return false; 1505 ++InitIndex; 1506 } else { 1507 // Initializer for a direct base class. 1508 if (const Record::Base *B = R->getBase(Init->getType())) { 1509 if (!this->emitGetPtrBasePop(B->Offset, Init)) 1510 return false; 1511 1512 if (!this->visitInitializer(Init)) 1513 return false; 1514 1515 if (!this->emitPopPtr(Initializer)) 1516 return false; 1517 // Base initializers don't increase InitIndex, since they don't count 1518 // into the Record's fields. 1519 } else { 1520 const Record::Field *FieldToInit = R->getField(InitIndex); 1521 // Non-primitive case. Get a pointer to the field-to-initialize 1522 // on the stack and recurse into visitInitializer(). 1523 if (!this->emitGetPtrField(FieldToInit->Offset, Init)) 1524 return false; 1525 1526 if (!this->visitInitializer(Init)) 1527 return false; 1528 1529 if (!this->emitPopPtr(Initializer)) 1530 return false; 1531 ++InitIndex; 1532 } 1533 } 1534 } 1535 1536 return true; 1537 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Initializer)) { 1538 // RVO functions expect a pointer to initialize on the stack. 1539 // Dup our existing pointer so it has its own copy to use. 1540 if (!this->emitDupPtr(Initializer)) 1541 return false; 1542 1543 return this->visit(CE); 1544 } else if (const auto *DIE = dyn_cast<CXXDefaultInitExpr>(Initializer)) { 1545 return this->visitInitializer(DIE->getExpr()); 1546 } else if (const auto *CE = dyn_cast<CastExpr>(Initializer)) { 1547 return this->visitInitializer(CE->getSubExpr()); 1548 } else if (const auto *CE = dyn_cast<CXXBindTemporaryExpr>(Initializer)) { 1549 return this->visitInitializer(CE->getSubExpr()); 1550 } else if (const auto *ACO = 1551 dyn_cast<AbstractConditionalOperator>(Initializer)) { 1552 return this->visitConditional( 1553 ACO, [this](const Expr *E) { return this->visitRecordInitializer(E); }); 1554 } else if (const auto *LE = dyn_cast<LambdaExpr>(Initializer)) { 1555 return this->VisitLambdaExpr(LE); 1556 } 1557 1558 return false; 1559 } 1560 1561 template <class Emitter> 1562 bool ByteCodeExprGen<Emitter>::visitInitializer(const Expr *Initializer) { 1563 QualType InitializerType = Initializer->getType(); 1564 1565 if (InitializerType->isArrayType()) 1566 return visitArrayInitializer(Initializer); 1567 1568 if (InitializerType->isRecordType()) 1569 return visitRecordInitializer(Initializer); 1570 1571 // Otherwise, visit the expression like normal. 1572 return this->visit(Initializer); 1573 } 1574 1575 template <class Emitter> 1576 const RecordType *ByteCodeExprGen<Emitter>::getRecordTy(QualType Ty) { 1577 if (const PointerType *PT = dyn_cast<PointerType>(Ty)) 1578 return PT->getPointeeType()->getAs<RecordType>(); 1579 else 1580 return Ty->getAs<RecordType>(); 1581 } 1582 1583 template <class Emitter> 1584 Record *ByteCodeExprGen<Emitter>::getRecord(QualType Ty) { 1585 if (auto *RecordTy = getRecordTy(Ty)) { 1586 return getRecord(RecordTy->getDecl()); 1587 } 1588 return nullptr; 1589 } 1590 1591 template <class Emitter> 1592 Record *ByteCodeExprGen<Emitter>::getRecord(const RecordDecl *RD) { 1593 return P.getOrCreateRecord(RD); 1594 } 1595 1596 template <class Emitter> 1597 const Function *ByteCodeExprGen<Emitter>::getFunction(const FunctionDecl *FD) { 1598 assert(FD); 1599 const Function *Func = P.getFunction(FD); 1600 bool IsBeingCompiled = Func && !Func->isFullyCompiled(); 1601 bool WasNotDefined = Func && !Func->isConstexpr() && !Func->hasBody(); 1602 1603 if (IsBeingCompiled) 1604 return Func; 1605 1606 if (!Func || WasNotDefined) { 1607 if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(Ctx, P).compileFunc(FD)) 1608 Func = *R; 1609 else { 1610 llvm::consumeError(R.takeError()); 1611 return nullptr; 1612 } 1613 } 1614 1615 return Func; 1616 } 1617 1618 template <class Emitter> 1619 bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *Exp) { 1620 ExprScope<Emitter> RootScope(this); 1621 if (!visit(Exp)) 1622 return false; 1623 1624 if (std::optional<PrimType> T = classify(Exp)) 1625 return this->emitRet(*T, Exp); 1626 else 1627 return this->emitRetValue(Exp); 1628 } 1629 1630 /// Toplevel visitDecl(). 1631 /// We get here from evaluateAsInitializer(). 1632 /// We need to evaluate the initializer and return its value. 1633 template <class Emitter> 1634 bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) { 1635 assert(!VD->isInvalidDecl() && "Trying to constant evaluate an invalid decl"); 1636 1637 // Create and initialize the variable. 1638 if (!this->visitVarDecl(VD)) 1639 return false; 1640 1641 // Get a pointer to the variable 1642 if (Context::shouldBeGloballyIndexed(VD)) { 1643 auto GlobalIndex = P.getGlobal(VD); 1644 assert(GlobalIndex); // visitVarDecl() didn't return false. 1645 if (!this->emitGetPtrGlobal(*GlobalIndex, VD)) 1646 return false; 1647 } else { 1648 auto Local = Locals.find(VD); 1649 assert(Local != Locals.end()); // Same here. 1650 if (!this->emitGetPtrLocal(Local->second.Offset, VD)) 1651 return false; 1652 } 1653 1654 // Return the value 1655 if (std::optional<PrimType> VarT = classify(VD->getType())) { 1656 if (!this->emitLoadPop(*VarT, VD)) 1657 return false; 1658 1659 return this->emitRet(*VarT, VD); 1660 } 1661 1662 return this->emitRetValue(VD); 1663 } 1664 1665 template <class Emitter> 1666 bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) { 1667 // We don't know what to do with these, so just return false. 1668 if (VD->getType().isNull()) 1669 return false; 1670 1671 const Expr *Init = VD->getInit(); 1672 std::optional<PrimType> VarT = classify(VD->getType()); 1673 1674 if (Context::shouldBeGloballyIndexed(VD)) { 1675 // We've already seen and initialized this global. 1676 if (P.getGlobal(VD)) 1677 return true; 1678 1679 std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init); 1680 1681 if (!GlobalIndex) 1682 return this->bail(VD); 1683 1684 assert(Init); 1685 { 1686 DeclScope<Emitter> LocalScope(this, VD); 1687 1688 if (VarT) { 1689 if (!this->visit(Init)) 1690 return false; 1691 return this->emitInitGlobal(*VarT, *GlobalIndex, VD); 1692 } 1693 return this->visitGlobalInitializer(Init, *GlobalIndex); 1694 } 1695 } else { 1696 VariableScope<Emitter> LocalScope(this); 1697 if (VarT) { 1698 unsigned Offset = this->allocateLocalPrimitive( 1699 VD, *VarT, VD->getType().isConstQualified()); 1700 if (Init) { 1701 // Compile the initializer in its own scope. 1702 ExprScope<Emitter> Scope(this); 1703 if (!this->visit(Init)) 1704 return false; 1705 1706 return this->emitSetLocal(*VarT, Offset, VD); 1707 } 1708 } else { 1709 if (std::optional<unsigned> Offset = this->allocateLocal(VD)) { 1710 if (Init) 1711 return this->visitLocalInitializer(Init, *Offset); 1712 } 1713 } 1714 return true; 1715 } 1716 1717 return false; 1718 } 1719 1720 template <class Emitter> 1721 bool ByteCodeExprGen<Emitter>::VisitBuiltinCallExpr(const CallExpr *E) { 1722 const Function *Func = getFunction(E->getDirectCallee()); 1723 if (!Func) 1724 return false; 1725 1726 // Put arguments on the stack. 1727 for (const auto *Arg : E->arguments()) { 1728 if (!this->visit(Arg)) 1729 return false; 1730 } 1731 1732 if (!this->emitCallBI(Func, E)) 1733 return false; 1734 1735 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext()); 1736 if (DiscardResult && !ReturnType->isVoidType()) { 1737 PrimType T = classifyPrim(ReturnType); 1738 return this->emitPop(T, E); 1739 } 1740 1741 return true; 1742 } 1743 1744 template <class Emitter> 1745 bool ByteCodeExprGen<Emitter>::VisitCallExpr(const CallExpr *E) { 1746 if (E->getBuiltinCallee()) 1747 return VisitBuiltinCallExpr(E); 1748 1749 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext()); 1750 std::optional<PrimType> T = classify(ReturnType); 1751 bool HasRVO = !ReturnType->isVoidType() && !T; 1752 1753 if (HasRVO && DiscardResult) { 1754 // If we need to discard the return value but the function returns its 1755 // value via an RVO pointer, we need to create one such pointer just 1756 // for this call. 1757 if (std::optional<unsigned> LocalIndex = allocateLocal(E)) { 1758 if (!this->emitGetPtrLocal(*LocalIndex, E)) 1759 return false; 1760 } 1761 } 1762 1763 // Put arguments on the stack. 1764 for (const auto *Arg : E->arguments()) { 1765 if (!this->visit(Arg)) 1766 return false; 1767 } 1768 1769 if (const FunctionDecl *FuncDecl = E->getDirectCallee()) { 1770 const Function *Func = getFunction(FuncDecl); 1771 if (!Func) 1772 return false; 1773 // If the function is being compiled right now, this is a recursive call. 1774 // In that case, the function can't be valid yet, even though it will be 1775 // later. 1776 // If the function is already fully compiled but not constexpr, it was 1777 // found to be faulty earlier on, so bail out. 1778 if (Func->isFullyCompiled() && !Func->isConstexpr()) 1779 return false; 1780 1781 assert(HasRVO == Func->hasRVO()); 1782 1783 bool HasQualifier = false; 1784 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee())) 1785 HasQualifier = ME->hasQualifier(); 1786 1787 bool IsVirtual = false; 1788 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) 1789 IsVirtual = MD->isVirtual(); 1790 1791 // In any case call the function. The return value will end up on the stack 1792 // and if the function has RVO, we already have the pointer on the stack to 1793 // write the result into. 1794 if (IsVirtual && !HasQualifier) { 1795 if (!this->emitCallVirt(Func, E)) 1796 return false; 1797 } else { 1798 if (!this->emitCall(Func, E)) 1799 return false; 1800 } 1801 } else { 1802 // Indirect call. Visit the callee, which will leave a FunctionPointer on 1803 // the stack. Cleanup of the returned value if necessary will be done after 1804 // the function call completed. 1805 if (!this->visit(E->getCallee())) 1806 return false; 1807 1808 if (!this->emitCallPtr(E)) 1809 return false; 1810 } 1811 1812 // Cleanup for discarded return values. 1813 if (DiscardResult && !ReturnType->isVoidType() && T) 1814 return this->emitPop(*T, E); 1815 1816 return true; 1817 } 1818 1819 template <class Emitter> 1820 bool ByteCodeExprGen<Emitter>::VisitCXXMemberCallExpr( 1821 const CXXMemberCallExpr *E) { 1822 // Get a This pointer on the stack. 1823 if (!this->visit(E->getImplicitObjectArgument())) 1824 return false; 1825 1826 return VisitCallExpr(E); 1827 } 1828 1829 template <class Emitter> 1830 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultInitExpr( 1831 const CXXDefaultInitExpr *E) { 1832 assert(classify(E->getType())); 1833 return this->visit(E->getExpr()); 1834 } 1835 1836 template <class Emitter> 1837 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultArgExpr( 1838 const CXXDefaultArgExpr *E) { 1839 return this->visit(E->getExpr()); 1840 } 1841 1842 template <class Emitter> 1843 bool ByteCodeExprGen<Emitter>::VisitCXXBoolLiteralExpr( 1844 const CXXBoolLiteralExpr *E) { 1845 if (DiscardResult) 1846 return true; 1847 1848 return this->emitConstBool(E->getValue(), E); 1849 } 1850 1851 template <class Emitter> 1852 bool ByteCodeExprGen<Emitter>::VisitCXXNullPtrLiteralExpr( 1853 const CXXNullPtrLiteralExpr *E) { 1854 if (DiscardResult) 1855 return true; 1856 1857 return this->emitNullPtr(E); 1858 } 1859 1860 template <class Emitter> 1861 bool ByteCodeExprGen<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) { 1862 if (DiscardResult) 1863 return true; 1864 return this->emitThis(E); 1865 } 1866 1867 template <class Emitter> 1868 bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) { 1869 const Expr *SubExpr = E->getSubExpr(); 1870 std::optional<PrimType> T = classify(SubExpr->getType()); 1871 1872 switch (E->getOpcode()) { 1873 case UO_PostInc: { // x++ 1874 if (!this->visit(SubExpr)) 1875 return false; 1876 1877 if (T == PT_Ptr) { 1878 if (!this->emitIncPtr(E)) 1879 return false; 1880 1881 return DiscardResult ? this->emitPopPtr(E) : true; 1882 } 1883 1884 if (T == PT_Float) { 1885 return DiscardResult ? this->emitIncfPop(getRoundingMode(E), E) 1886 : this->emitIncf(getRoundingMode(E), E); 1887 } 1888 1889 return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E); 1890 } 1891 case UO_PostDec: { // x-- 1892 if (!this->visit(SubExpr)) 1893 return false; 1894 1895 if (T == PT_Ptr) { 1896 if (!this->emitDecPtr(E)) 1897 return false; 1898 1899 return DiscardResult ? this->emitPopPtr(E) : true; 1900 } 1901 1902 if (T == PT_Float) { 1903 return DiscardResult ? this->emitDecfPop(getRoundingMode(E), E) 1904 : this->emitDecf(getRoundingMode(E), E); 1905 } 1906 1907 return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E); 1908 } 1909 case UO_PreInc: { // ++x 1910 if (!this->visit(SubExpr)) 1911 return false; 1912 1913 if (T == PT_Ptr) { 1914 this->emitLoadPtr(E); 1915 this->emitConstUint8(1, E); 1916 this->emitAddOffsetUint8(E); 1917 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); 1918 } 1919 1920 // Post-inc and pre-inc are the same if the value is to be discarded. 1921 if (DiscardResult) { 1922 if (T == PT_Float) 1923 return this->emitIncfPop(getRoundingMode(E), E); 1924 return this->emitIncPop(*T, E); 1925 } 1926 1927 if (T == PT_Float) { 1928 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType()); 1929 this->emitLoadFloat(E); 1930 this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E); 1931 this->emitAddf(getRoundingMode(E), E); 1932 return this->emitStoreFloat(E); 1933 } 1934 this->emitLoad(*T, E); 1935 this->emitConst(1, E); 1936 this->emitAdd(*T, E); 1937 return this->emitStore(*T, E); 1938 } 1939 case UO_PreDec: { // --x 1940 if (!this->visit(SubExpr)) 1941 return false; 1942 1943 if (T == PT_Ptr) { 1944 this->emitLoadPtr(E); 1945 this->emitConstUint8(1, E); 1946 this->emitSubOffsetUint8(E); 1947 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E); 1948 } 1949 1950 // Post-dec and pre-dec are the same if the value is to be discarded. 1951 if (DiscardResult) { 1952 if (T == PT_Float) 1953 return this->emitDecfPop(getRoundingMode(E), E); 1954 return this->emitDecPop(*T, E); 1955 } 1956 1957 if (T == PT_Float) { 1958 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType()); 1959 this->emitLoadFloat(E); 1960 this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E); 1961 this->emitSubf(getRoundingMode(E), E); 1962 return this->emitStoreFloat(E); 1963 } 1964 this->emitLoad(*T, E); 1965 this->emitConst(1, E); 1966 this->emitSub(*T, E); 1967 return this->emitStore(*T, E); 1968 } 1969 case UO_LNot: // !x 1970 if (!this->visit(SubExpr)) 1971 return false; 1972 // The Inv doesn't change anything, so skip it if we don't need the result. 1973 return DiscardResult ? this->emitPop(*T, E) : this->emitInvBool(E); 1974 case UO_Minus: // -x 1975 if (!this->visit(SubExpr)) 1976 return false; 1977 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E); 1978 case UO_Plus: // +x 1979 if (!this->visit(SubExpr)) // noop 1980 return false; 1981 return DiscardResult ? this->emitPop(*T, E) : true; 1982 case UO_AddrOf: // &x 1983 // We should already have a pointer when we get here. 1984 if (!this->visit(SubExpr)) 1985 return false; 1986 return DiscardResult ? this->emitPop(*T, E) : true; 1987 case UO_Deref: // *x 1988 return dereference( 1989 SubExpr, DerefKind::Read, 1990 [](PrimType) { 1991 llvm_unreachable("Dereferencing requires a pointer"); 1992 return false; 1993 }, 1994 [this, E](PrimType T) { 1995 return DiscardResult ? this->emitPop(T, E) : true; 1996 }); 1997 case UO_Not: // ~x 1998 if (!this->visit(SubExpr)) 1999 return false; 2000 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E); 2001 case UO_Real: // __real x 2002 case UO_Imag: // __imag x 2003 case UO_Extension: 2004 case UO_Coawait: 2005 assert(false && "Unhandled opcode"); 2006 } 2007 2008 return false; 2009 } 2010 2011 template <class Emitter> 2012 bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) { 2013 if (DiscardResult) 2014 return true; 2015 2016 const auto *D = E->getDecl(); 2017 2018 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) { 2019 return this->emitConst(ECD->getInitVal(), E); 2020 } else if (const auto *BD = dyn_cast<BindingDecl>(D)) { 2021 return this->visit(BD->getBinding()); 2022 } else if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) { 2023 const Function *F = getFunction(FuncDecl); 2024 return F && this->emitGetFnPtr(F, E); 2025 } 2026 2027 // References are implemented via pointers, so when we see a DeclRefExpr 2028 // pointing to a reference, we need to get its value directly (i.e. the 2029 // pointer to the actual value) instead of a pointer to the pointer to the 2030 // value. 2031 bool IsReference = D->getType()->isReferenceType(); 2032 2033 // Check for local/global variables and parameters. 2034 if (auto It = Locals.find(D); It != Locals.end()) { 2035 const unsigned Offset = It->second.Offset; 2036 2037 if (IsReference) 2038 return this->emitGetLocal(PT_Ptr, Offset, E); 2039 return this->emitGetPtrLocal(Offset, E); 2040 } else if (auto GlobalIndex = P.getGlobal(D)) { 2041 if (IsReference) 2042 return this->emitGetGlobalPtr(*GlobalIndex, E); 2043 2044 return this->emitGetPtrGlobal(*GlobalIndex, E); 2045 } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) { 2046 if (auto It = this->Params.find(PVD); It != this->Params.end()) { 2047 if (IsReference) 2048 return this->emitGetParamPtr(It->second, E); 2049 return this->emitGetPtrParam(It->second, E); 2050 } 2051 } 2052 2053 // Handle lambda captures. 2054 if (auto It = this->LambdaCaptures.find(D); 2055 It != this->LambdaCaptures.end()) { 2056 auto [Offset, IsReference] = It->second; 2057 2058 if (IsReference) 2059 return this->emitGetThisFieldPtr(Offset, E); 2060 return this->emitGetPtrThisField(Offset, E); 2061 } 2062 2063 return false; 2064 } 2065 2066 template <class Emitter> 2067 void ByteCodeExprGen<Emitter>::emitCleanup() { 2068 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) 2069 C->emitDestruction(); 2070 } 2071 2072 template <class Emitter> 2073 bool ByteCodeExprGen<Emitter>::emitDerivedToBaseCasts( 2074 const RecordType *DerivedType, const RecordType *BaseType, const Expr *E) { 2075 // Pointer of derived type is already on the stack. 2076 const auto *FinalDecl = cast<CXXRecordDecl>(BaseType->getDecl()); 2077 const RecordDecl *CurDecl = DerivedType->getDecl(); 2078 const Record *CurRecord = getRecord(CurDecl); 2079 assert(CurDecl && FinalDecl); 2080 for (;;) { 2081 assert(CurRecord->getNumBases() > 0); 2082 // One level up 2083 for (const Record::Base &B : CurRecord->bases()) { 2084 const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl); 2085 2086 if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) { 2087 // This decl will lead us to the final decl, so emit a base cast. 2088 if (!this->emitGetPtrBasePop(B.Offset, E)) 2089 return false; 2090 2091 CurRecord = B.R; 2092 CurDecl = BaseDecl; 2093 break; 2094 } 2095 } 2096 if (CurDecl == FinalDecl) 2097 return true; 2098 } 2099 2100 llvm_unreachable("Couldn't find the base class?"); 2101 return false; 2102 } 2103 2104 /// When calling this, we have a pointer of the local-to-destroy 2105 /// on the stack. 2106 /// Emit destruction of record types (or arrays of record types). 2107 /// FIXME: Handle virtual destructors. 2108 template <class Emitter> 2109 bool ByteCodeExprGen<Emitter>::emitRecordDestruction(const Descriptor *Desc) { 2110 assert(Desc); 2111 assert(!Desc->isPrimitive()); 2112 assert(!Desc->isPrimitiveArray()); 2113 2114 // Arrays. 2115 if (Desc->isArray()) { 2116 const Descriptor *ElemDesc = Desc->ElemDesc; 2117 const Record *ElemRecord = ElemDesc->ElemRecord; 2118 assert(ElemRecord); // This is not a primitive array. 2119 2120 if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor(); 2121 Dtor && !Dtor->isTrivial()) { 2122 for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) { 2123 if (!this->emitConstUint64(I, SourceInfo{})) 2124 return false; 2125 if (!this->emitArrayElemPtrUint64(SourceInfo{})) 2126 return false; 2127 if (!this->emitRecordDestruction(Desc->ElemDesc)) 2128 return false; 2129 } 2130 } 2131 return this->emitPopPtr(SourceInfo{}); 2132 } 2133 2134 const Record *R = Desc->ElemRecord; 2135 assert(R); 2136 // First, destroy all fields. 2137 for (const Record::Field &Field : llvm::reverse(R->fields())) { 2138 const Descriptor *D = Field.Desc; 2139 if (!D->isPrimitive() && !D->isPrimitiveArray()) { 2140 if (!this->emitDupPtr(SourceInfo{})) 2141 return false; 2142 if (!this->emitGetPtrField(Field.Offset, SourceInfo{})) 2143 return false; 2144 if (!this->emitRecordDestruction(D)) 2145 return false; 2146 } 2147 } 2148 2149 // FIXME: Unions need to be handled differently here. We don't want to 2150 // call the destructor of its members. 2151 2152 // Now emit the destructor and recurse into base classes. 2153 if (const CXXDestructorDecl *Dtor = R->getDestructor(); 2154 Dtor && !Dtor->isTrivial()) { 2155 const Function *DtorFunc = getFunction(Dtor); 2156 if (DtorFunc && DtorFunc->isConstexpr()) { 2157 assert(DtorFunc->hasThisPointer()); 2158 assert(DtorFunc->getNumParams() == 1); 2159 if (!this->emitDupPtr(SourceInfo{})) 2160 return false; 2161 if (!this->emitCall(DtorFunc, SourceInfo{})) 2162 return false; 2163 } 2164 } 2165 2166 for (const Record::Base &Base : llvm::reverse(R->bases())) { 2167 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{})) 2168 return false; 2169 if (!this->emitRecordDestruction(Base.Desc)) 2170 return false; 2171 } 2172 // FIXME: Virtual bases. 2173 2174 // Remove the instance pointer. 2175 return this->emitPopPtr(SourceInfo{}); 2176 } 2177 2178 namespace clang { 2179 namespace interp { 2180 2181 template class ByteCodeExprGen<ByteCodeEmitter>; 2182 template class ByteCodeExprGen<EvalEmitter>; 2183 2184 } // namespace interp 2185 } // namespace clang 2186