1 //===--- ByteCodeStmtGen.cpp - Code generator for expressions ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ByteCodeStmtGen.h"
10 #include "ByteCodeEmitter.h"
11 #include "ByteCodeGenError.h"
18 using namespace clang;
19 using namespace clang::interp;
21 template <typename T> using Expected = llvm::Expected<T>;
22 template <typename T> using Optional = llvm::Optional<T>;
27 /// Scope managing label targets.
28 template <class Emitter> class LabelScope {
30 virtual ~LabelScope() { }
33 LabelScope(ByteCodeStmtGen<Emitter> *Ctx) : Ctx(Ctx) {}
34 /// ByteCodeStmtGen instance.
35 ByteCodeStmtGen<Emitter> *Ctx;
38 /// Sets the context for break/continue statements.
39 template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
41 using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
42 using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
44 LoopScope(ByteCodeStmtGen<Emitter> *Ctx, LabelTy BreakLabel,
45 LabelTy ContinueLabel)
46 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
47 OldContinueLabel(Ctx->ContinueLabel) {
48 this->Ctx->BreakLabel = BreakLabel;
49 this->Ctx->ContinueLabel = ContinueLabel;
53 this->Ctx->BreakLabel = OldBreakLabel;
54 this->Ctx->ContinueLabel = OldContinueLabel;
58 OptLabelTy OldBreakLabel;
59 OptLabelTy OldContinueLabel;
62 // Sets the context for a switch scope, mapping labels.
63 template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
65 using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
66 using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
67 using CaseMap = typename ByteCodeStmtGen<Emitter>::CaseMap;
69 SwitchScope(ByteCodeStmtGen<Emitter> *Ctx, CaseMap &&CaseLabels,
70 LabelTy BreakLabel, OptLabelTy DefaultLabel)
71 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
72 OldDefaultLabel(this->Ctx->DefaultLabel),
73 OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
74 this->Ctx->BreakLabel = BreakLabel;
75 this->Ctx->DefaultLabel = DefaultLabel;
76 this->Ctx->CaseLabels = std::move(CaseLabels);
80 this->Ctx->BreakLabel = OldBreakLabel;
81 this->Ctx->DefaultLabel = OldDefaultLabel;
82 this->Ctx->CaseLabels = std::move(OldCaseLabels);
86 OptLabelTy OldBreakLabel;
87 OptLabelTy OldDefaultLabel;
88 CaseMap OldCaseLabels;
94 template <class Emitter>
95 bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
96 // Classify the return type.
97 ReturnType = this->classify(F->getReturnType());
99 // Set up fields and context if a constructor.
100 if (auto *MD = dyn_cast<CXXMethodDecl>(F))
101 return this->bail(MD);
103 if (auto *Body = F->getBody())
104 if (!visitStmt(Body))
107 // Emit a guard return to protect against a code path missing one.
108 if (F->getReturnType()->isVoidType())
109 return this->emitRetVoid(SourceInfo{});
111 return this->emitNoRet(SourceInfo{});
114 template <class Emitter>
115 bool ByteCodeStmtGen<Emitter>::visitStmt(const Stmt *S) {
116 switch (S->getStmtClass()) {
117 case Stmt::CompoundStmtClass:
118 return visitCompoundStmt(cast<CompoundStmt>(S));
119 case Stmt::DeclStmtClass:
120 return visitDeclStmt(cast<DeclStmt>(S));
121 case Stmt::ReturnStmtClass:
122 return visitReturnStmt(cast<ReturnStmt>(S));
123 case Stmt::IfStmtClass:
124 return visitIfStmt(cast<IfStmt>(S));
125 case Stmt::NullStmtClass:
128 if (auto *Exp = dyn_cast<Expr>(S))
129 return this->discard(Exp);
130 return this->bail(S);
135 template <class Emitter>
136 bool ByteCodeStmtGen<Emitter>::visitCompoundStmt(
137 const CompoundStmt *CompoundStmt) {
138 BlockScope<Emitter> Scope(this);
139 for (auto *InnerStmt : CompoundStmt->body())
140 if (!visitStmt(InnerStmt))
145 template <class Emitter>
146 bool ByteCodeStmtGen<Emitter>::visitDeclStmt(const DeclStmt *DS) {
147 for (auto *D : DS->decls()) {
148 // Variable declarator.
149 if (auto *VD = dyn_cast<VarDecl>(D)) {
150 if (!visitVarDecl(VD))
155 // Decomposition declarator.
156 if (auto *DD = dyn_cast<DecompositionDecl>(D)) {
157 return this->bail(DD);
164 template <class Emitter>
165 bool ByteCodeStmtGen<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
166 if (const Expr *RE = RS->getRetValue()) {
167 ExprScope<Emitter> RetScope(this);
169 // Primitive types are simply returned.
170 if (!this->visit(RE))
173 return this->emitRet(*ReturnType, RS);
175 // RVO - construct the value in the return location.
176 auto ReturnLocation = [this, RE] { return this->emitGetParamPtr(0, RE); };
177 if (!this->visitInitializer(RE, ReturnLocation))
180 return this->emitRetVoid(RS);
184 if (!this->emitRetVoid(RS))
190 template <class Emitter>
191 bool ByteCodeStmtGen<Emitter>::visitIfStmt(const IfStmt *IS) {
192 BlockScope<Emitter> IfScope(this);
193 if (auto *CondInit = IS->getInit())
194 if (!visitStmt(IS->getInit()))
197 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
198 if (!visitDeclStmt(CondDecl))
201 if (!this->visitBool(IS->getCond()))
204 if (const Stmt *Else = IS->getElse()) {
205 LabelTy LabelElse = this->getLabel();
206 LabelTy LabelEnd = this->getLabel();
207 if (!this->jumpFalse(LabelElse))
209 if (!visitStmt(IS->getThen()))
211 if (!this->jump(LabelEnd))
213 this->emitLabel(LabelElse);
214 if (!visitStmt(Else))
216 this->emitLabel(LabelEnd);
218 LabelTy LabelEnd = this->getLabel();
219 if (!this->jumpFalse(LabelEnd))
221 if (!visitStmt(IS->getThen()))
223 this->emitLabel(LabelEnd);
229 template <class Emitter>
230 bool ByteCodeStmtGen<Emitter>::visitVarDecl(const VarDecl *VD) {
231 auto DT = VD->getType();
233 if (!VD->hasLocalStorage()) {
234 // No code generation required.
238 // Integers, pointers, primitives.
239 if (Optional<PrimType> T = this->classify(DT)) {
240 auto Off = this->allocateLocalPrimitive(VD, *T, DT.isConstQualified());
241 // Compile the initialiser in its own scope.
243 ExprScope<Emitter> Scope(this);
244 if (!this->visit(VD->getInit()))
248 return this->emitSetLocal(*T, Off, VD);
250 // Composite types - allocate storage and initialize it.
251 if (auto Off = this->allocateLocal(VD)) {
252 return this->visitLocalInitializer(VD->getInit(), *Off);
254 return this->bail(VD);
262 template class ByteCodeStmtGen<ByteCodeEmitter>;
264 } // namespace interp