1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
//===--- ByteCodeStmtGen.cpp - Code generator for expressions ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ByteCodeStmtGen.h"
#include "ByteCodeEmitter.h"
#include "ByteCodeGenError.h"
#include "Context.h"
#include "Function.h"
#include "PrimType.h"
#include "Program.h"
#include "State.h"
#include "clang/Basic/LLVM.h"
using namespace clang;
using namespace clang::interp;
namespace clang {
namespace interp {
/// Scope managing label targets.
template <class Emitter> class LabelScope {
public:
virtual ~LabelScope() { }
protected:
LabelScope(ByteCodeStmtGen<Emitter> *Ctx) : Ctx(Ctx) {}
/// ByteCodeStmtGen instance.
ByteCodeStmtGen<Emitter> *Ctx;
};
/// Sets the context for break/continue statements.
template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
public:
using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
LoopScope(ByteCodeStmtGen<Emitter> *Ctx, LabelTy BreakLabel,
LabelTy ContinueLabel)
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
OldContinueLabel(Ctx->ContinueLabel) {
this->Ctx->BreakLabel = BreakLabel;
this->Ctx->ContinueLabel = ContinueLabel;
}
~LoopScope() {
this->Ctx->BreakLabel = OldBreakLabel;
this->Ctx->ContinueLabel = OldContinueLabel;
}
private:
OptLabelTy OldBreakLabel;
OptLabelTy OldContinueLabel;
};
// Sets the context for a switch scope, mapping labels.
template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
public:
using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
using CaseMap = typename ByteCodeStmtGen<Emitter>::CaseMap;
SwitchScope(ByteCodeStmtGen<Emitter> *Ctx, CaseMap &&CaseLabels,
LabelTy BreakLabel, OptLabelTy DefaultLabel)
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
OldDefaultLabel(this->Ctx->DefaultLabel),
OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
this->Ctx->BreakLabel = BreakLabel;
this->Ctx->DefaultLabel = DefaultLabel;
this->Ctx->CaseLabels = std::move(CaseLabels);
}
~SwitchScope() {
this->Ctx->BreakLabel = OldBreakLabel;
this->Ctx->DefaultLabel = OldDefaultLabel;
this->Ctx->CaseLabels = std::move(OldCaseLabels);
}
private:
OptLabelTy OldBreakLabel;
OptLabelTy OldDefaultLabel;
CaseMap OldCaseLabels;
};
} // namespace interp
} // namespace clang
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
// Classify the return type.
ReturnType = this->classify(F->getReturnType());
// Constructor. Set up field initializers.
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F)) {
const RecordDecl *RD = Ctor->getParent();
const Record *R = this->getRecord(RD);
if (!R)
return false;
for (const auto *Init : Ctor->inits()) {
// Scope needed for the initializers.
BlockScope<Emitter> Scope(this);
const Expr *InitExpr = Init->getInit();
if (const FieldDecl *Member = Init->getMember()) {
const Record::Field *F = R->getField(Member);
if (std::optional<PrimType> T = this->classify(InitExpr)) {
if (!this->visit(InitExpr))
return false;
if (!this->emitInitThisField(*T, F->Offset, InitExpr))
return false;
} else {
// Non-primitive case. Get a pointer to the field-to-initialize
// on the stack and call visitInitialzer() for it.
if (!this->emitThis(InitExpr))
return false;
if (!this->emitGetPtrField(F->Offset, InitExpr))
return false;
if (!this->visitInitializer(InitExpr))
return false;
if (!this->emitPopPtr(InitExpr))
return false;
}
} else if (const Type *Base = Init->getBaseClass()) {
// Base class initializer.
// Get This Base and call initializer on it.
const auto *BaseDecl = Base->getAsCXXRecordDecl();
assert(BaseDecl);
const Record::Base *B = R->getBase(BaseDecl);
assert(B);
if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
return false;
if (!this->visitInitializer(InitExpr))
return false;
if (!this->emitPopPtr(InitExpr))
return false;
}
}
}
if (const auto *Body = F->getBody())
if (!visitStmt(Body))
return false;
// Emit a guard return to protect against a code path missing one.
if (F->getReturnType()->isVoidType())
return this->emitRetVoid(SourceInfo{});
else
return this->emitNoRet(SourceInfo{});
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitStmt(const Stmt *S) {
switch (S->getStmtClass()) {
case Stmt::CompoundStmtClass:
return visitCompoundStmt(cast<CompoundStmt>(S));
case Stmt::DeclStmtClass:
return visitDeclStmt(cast<DeclStmt>(S));
case Stmt::ReturnStmtClass:
return visitReturnStmt(cast<ReturnStmt>(S));
case Stmt::IfStmtClass:
return visitIfStmt(cast<IfStmt>(S));
case Stmt::WhileStmtClass:
return visitWhileStmt(cast<WhileStmt>(S));
case Stmt::DoStmtClass:
return visitDoStmt(cast<DoStmt>(S));
case Stmt::ForStmtClass:
return visitForStmt(cast<ForStmt>(S));
case Stmt::CXXForRangeStmtClass:
return visitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
case Stmt::BreakStmtClass:
return visitBreakStmt(cast<BreakStmt>(S));
case Stmt::ContinueStmtClass:
return visitContinueStmt(cast<ContinueStmt>(S));
case Stmt::SwitchStmtClass:
return visitSwitchStmt(cast<SwitchStmt>(S));
case Stmt::CaseStmtClass:
return visitCaseStmt(cast<CaseStmt>(S));
case Stmt::DefaultStmtClass:
return visitDefaultStmt(cast<DefaultStmt>(S));
case Stmt::NullStmtClass:
return true;
default: {
if (auto *Exp = dyn_cast<Expr>(S))
return this->discard(Exp);
return this->bail(S);
}
}
}
/// Visits the given statment without creating a variable
/// scope for it in case it is a compound statement.
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitLoopBody(const Stmt *S) {
if (isa<NullStmt>(S))
return true;
if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
for (auto *InnerStmt : CS->body())
if (!visitStmt(InnerStmt))
return false;
return true;
}
return this->visitStmt(S);
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitCompoundStmt(
const CompoundStmt *CompoundStmt) {
BlockScope<Emitter> Scope(this);
for (auto *InnerStmt : CompoundStmt->body())
if (!visitStmt(InnerStmt))
return false;
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitDeclStmt(const DeclStmt *DS) {
for (auto *D : DS->decls()) {
if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl>(D))
continue;
const auto *VD = dyn_cast<VarDecl>(D);
if (!VD)
return false;
if (!this->visitVarDecl(VD))
return false;
}
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
if (const Expr *RE = RS->getRetValue()) {
ExprScope<Emitter> RetScope(this);
if (ReturnType) {
// Primitive types are simply returned.
if (!this->visit(RE))
return false;
this->emitCleanup();
return this->emitRet(*ReturnType, RS);
} else {
// RVO - construct the value in the return location.
if (!this->emitRVOPtr(RE))
return false;
if (!this->visitInitializer(RE))
return false;
if (!this->emitPopPtr(RE))
return false;
this->emitCleanup();
return this->emitRetVoid(RS);
}
}
// Void return.
this->emitCleanup();
return this->emitRetVoid(RS);
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitIfStmt(const IfStmt *IS) {
BlockScope<Emitter> IfScope(this);
if (IS->isNonNegatedConsteval())
return visitStmt(IS->getThen());
if (IS->isNegatedConsteval())
return IS->getElse() ? visitStmt(IS->getElse()) : true;
if (auto *CondInit = IS->getInit())
if (!visitStmt(IS->getInit()))
return false;
if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
if (!visitDeclStmt(CondDecl))
return false;
if (!this->visitBool(IS->getCond()))
return false;
if (const Stmt *Else = IS->getElse()) {
LabelTy LabelElse = this->getLabel();
LabelTy LabelEnd = this->getLabel();
if (!this->jumpFalse(LabelElse))
return false;
if (!visitStmt(IS->getThen()))
return false;
if (!this->jump(LabelEnd))
return false;
this->emitLabel(LabelElse);
if (!visitStmt(Else))
return false;
this->emitLabel(LabelEnd);
} else {
LabelTy LabelEnd = this->getLabel();
if (!this->jumpFalse(LabelEnd))
return false;
if (!visitStmt(IS->getThen()))
return false;
this->emitLabel(LabelEnd);
}
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitWhileStmt(const WhileStmt *S) {
const Expr *Cond = S->getCond();
const Stmt *Body = S->getBody();
LabelTy CondLabel = this->getLabel(); // Label before the condition.
LabelTy EndLabel = this->getLabel(); // Label after the loop.
LoopScope<Emitter> LS(this, EndLabel, CondLabel);
this->emitLabel(CondLabel);
if (!this->visitBool(Cond))
return false;
if (!this->jumpFalse(EndLabel))
return false;
LocalScope<Emitter> Scope(this);
{
DestructorScope<Emitter> DS(Scope);
if (!this->visitLoopBody(Body))
return false;
}
if (!this->jump(CondLabel))
return false;
this->emitLabel(EndLabel);
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitDoStmt(const DoStmt *S) {
const Expr *Cond = S->getCond();
const Stmt *Body = S->getBody();
LabelTy StartLabel = this->getLabel();
LabelTy EndLabel = this->getLabel();
LabelTy CondLabel = this->getLabel();
LoopScope<Emitter> LS(this, EndLabel, CondLabel);
LocalScope<Emitter> Scope(this);
this->emitLabel(StartLabel);
{
DestructorScope<Emitter> DS(Scope);
if (!this->visitLoopBody(Body))
return false;
this->emitLabel(CondLabel);
if (!this->visitBool(Cond))
return false;
}
if (!this->jumpTrue(StartLabel))
return false;
this->emitLabel(EndLabel);
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitForStmt(const ForStmt *S) {
// for (Init; Cond; Inc) { Body }
const Stmt *Init = S->getInit();
const Expr *Cond = S->getCond();
const Expr *Inc = S->getInc();
const Stmt *Body = S->getBody();
LabelTy EndLabel = this->getLabel();
LabelTy CondLabel = this->getLabel();
LabelTy IncLabel = this->getLabel();
LoopScope<Emitter> LS(this, EndLabel, IncLabel);
LocalScope<Emitter> Scope(this);
if (Init && !this->visitStmt(Init))
return false;
this->emitLabel(CondLabel);
if (Cond) {
if (!this->visitBool(Cond))
return false;
if (!this->jumpFalse(EndLabel))
return false;
}
{
DestructorScope<Emitter> DS(Scope);
if (Body && !this->visitLoopBody(Body))
return false;
this->emitLabel(IncLabel);
if (Inc && !this->discard(Inc))
return false;
}
if (!this->jump(CondLabel))
return false;
this->emitLabel(EndLabel);
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
const Stmt *Init = S->getInit();
const Expr *Cond = S->getCond();
const Expr *Inc = S->getInc();
const Stmt *Body = S->getBody();
const Stmt *BeginStmt = S->getBeginStmt();
const Stmt *RangeStmt = S->getRangeStmt();
const Stmt *EndStmt = S->getEndStmt();
const VarDecl *LoopVar = S->getLoopVariable();
LabelTy EndLabel = this->getLabel();
LabelTy CondLabel = this->getLabel();
LabelTy IncLabel = this->getLabel();
LoopScope<Emitter> LS(this, EndLabel, IncLabel);
// Emit declarations needed in the loop.
if (Init && !this->visitStmt(Init))
return false;
if (!this->visitStmt(RangeStmt))
return false;
if (!this->visitStmt(BeginStmt))
return false;
if (!this->visitStmt(EndStmt))
return false;
// Now the condition as well as the loop variable assignment.
this->emitLabel(CondLabel);
if (!this->visitBool(Cond))
return false;
if (!this->jumpFalse(EndLabel))
return false;
if (!this->visitVarDecl(LoopVar))
return false;
// Body.
LocalScope<Emitter> Scope(this);
{
DestructorScope<Emitter> DS(Scope);
if (!this->visitLoopBody(Body))
return false;
this->emitLabel(IncLabel);
if (!this->discard(Inc))
return false;
}
if (!this->jump(CondLabel))
return false;
this->emitLabel(EndLabel);
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitBreakStmt(const BreakStmt *S) {
if (!BreakLabel)
return false;
this->VarScope->emitDestructors();
return this->jump(*BreakLabel);
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitContinueStmt(const ContinueStmt *S) {
if (!ContinueLabel)
return false;
this->VarScope->emitDestructors();
return this->jump(*ContinueLabel);
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
const Expr *Cond = S->getCond();
PrimType CondT = this->classifyPrim(Cond->getType());
LabelTy EndLabel = this->getLabel();
OptLabelTy DefaultLabel = std::nullopt;
unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
if (const auto *CondInit = S->getInit())
if (!visitStmt(CondInit))
return false;
// Initialize condition variable.
if (!this->visit(Cond))
return false;
if (!this->emitSetLocal(CondT, CondVar, S))
return false;
CaseMap CaseLabels;
// Create labels and comparison ops for all case statements.
for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
SC = SC->getNextSwitchCase()) {
if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
// FIXME: Implement ranges.
if (CS->caseStmtIsGNURange())
return false;
CaseLabels[SC] = this->getLabel();
const Expr *Value = CS->getLHS();
PrimType ValueT = this->classifyPrim(Value->getType());
// Compare the case statement's value to the switch condition.
if (!this->emitGetLocal(CondT, CondVar, CS))
return false;
if (!this->visit(Value))
return false;
// Compare and jump to the case label.
if (!this->emitEQ(ValueT, S))
return false;
if (!this->jumpTrue(CaseLabels[CS]))
return false;
} else {
assert(!DefaultLabel);
DefaultLabel = this->getLabel();
}
}
// If none of the conditions above were true, fall through to the default
// statement or jump after the switch statement.
if (DefaultLabel) {
if (!this->jump(*DefaultLabel))
return false;
} else {
if (!this->jump(EndLabel))
return false;
}
SwitchScope<Emitter> SS(this, std::move(CaseLabels), EndLabel, DefaultLabel);
if (!this->visitStmt(S->getBody()))
return false;
this->emitLabel(EndLabel);
return true;
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitCaseStmt(const CaseStmt *S) {
this->emitLabel(CaseLabels[S]);
return this->visitStmt(S->getSubStmt());
}
template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitDefaultStmt(const DefaultStmt *S) {
this->emitLabel(*DefaultLabel);
return this->visitStmt(S->getSubStmt());
}
namespace clang {
namespace interp {
template class ByteCodeStmtGen<ByteCodeEmitter>;
} // namespace interp
} // namespace clang
|