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