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