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