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"
17 #include "clang/Basic/LLVM.h"
19 using namespace clang;
20 using namespace clang::interp;
25 /// Scope managing label targets.
26 template <class Emitter> class LabelScope {
28 virtual ~LabelScope() { }
31 LabelScope(ByteCodeStmtGen<Emitter> *Ctx) : Ctx(Ctx) {}
32 /// ByteCodeStmtGen instance.
33 ByteCodeStmtGen<Emitter> *Ctx;
36 /// Sets the context for break/continue statements.
37 template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
39 using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
40 using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
42 LoopScope(ByteCodeStmtGen<Emitter> *Ctx, LabelTy BreakLabel,
43 LabelTy ContinueLabel)
44 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
45 OldContinueLabel(Ctx->ContinueLabel) {
46 this->Ctx->BreakLabel = BreakLabel;
47 this->Ctx->ContinueLabel = ContinueLabel;
51 this->Ctx->BreakLabel = OldBreakLabel;
52 this->Ctx->ContinueLabel = OldContinueLabel;
56 OptLabelTy OldBreakLabel;
57 OptLabelTy OldContinueLabel;
60 // Sets the context for a switch scope, mapping labels.
61 template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
63 using LabelTy = typename ByteCodeStmtGen<Emitter>::LabelTy;
64 using OptLabelTy = typename ByteCodeStmtGen<Emitter>::OptLabelTy;
65 using CaseMap = typename ByteCodeStmtGen<Emitter>::CaseMap;
67 SwitchScope(ByteCodeStmtGen<Emitter> *Ctx, CaseMap &&CaseLabels,
68 LabelTy BreakLabel, OptLabelTy DefaultLabel)
69 : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
70 OldDefaultLabel(this->Ctx->DefaultLabel),
71 OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
72 this->Ctx->BreakLabel = BreakLabel;
73 this->Ctx->DefaultLabel = DefaultLabel;
74 this->Ctx->CaseLabels = std::move(CaseLabels);
78 this->Ctx->BreakLabel = OldBreakLabel;
79 this->Ctx->DefaultLabel = OldDefaultLabel;
80 this->Ctx->CaseLabels = std::move(OldCaseLabels);
84 OptLabelTy OldBreakLabel;
85 OptLabelTy OldDefaultLabel;
86 CaseMap OldCaseLabels;
92 template <class Emitter>
93 bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
94 // Classify the return type.
95 ReturnType = this->classify(F->getReturnType());
97 // Set up fields and context if a constructor.
98 if (auto *MD = dyn_cast<CXXMethodDecl>(F))
99 return this->bail(MD);
101 if (auto *Body = F->getBody())
102 if (!visitStmt(Body))
105 // Emit a guard return to protect against a code path missing one.
106 if (F->getReturnType()->isVoidType())
107 return this->emitRetVoid(SourceInfo{});
109 return this->emitNoRet(SourceInfo{});
112 template <class Emitter>
113 bool ByteCodeStmtGen<Emitter>::visitStmt(const Stmt *S) {
114 switch (S->getStmtClass()) {
115 case Stmt::CompoundStmtClass:
116 return visitCompoundStmt(cast<CompoundStmt>(S));
117 case Stmt::DeclStmtClass:
118 return visitDeclStmt(cast<DeclStmt>(S));
119 case Stmt::ReturnStmtClass:
120 return visitReturnStmt(cast<ReturnStmt>(S));
121 case Stmt::IfStmtClass:
122 return visitIfStmt(cast<IfStmt>(S));
123 case Stmt::NullStmtClass:
126 if (auto *Exp = dyn_cast<Expr>(S))
127 return this->discard(Exp);
128 return this->bail(S);
133 template <class Emitter>
134 bool ByteCodeStmtGen<Emitter>::visitCompoundStmt(
135 const CompoundStmt *CompoundStmt) {
136 BlockScope<Emitter> Scope(this);
137 for (auto *InnerStmt : CompoundStmt->body())
138 if (!visitStmt(InnerStmt))
143 template <class Emitter>
144 bool ByteCodeStmtGen<Emitter>::visitDeclStmt(const DeclStmt *DS) {
145 for (auto *D : DS->decls()) {
146 // Variable declarator.
147 if (auto *VD = dyn_cast<VarDecl>(D)) {
148 if (!visitVarDecl(VD))
153 // Decomposition declarator.
154 if (auto *DD = dyn_cast<DecompositionDecl>(D)) {
155 return this->bail(DD);
162 template <class Emitter>
163 bool ByteCodeStmtGen<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
164 if (const Expr *RE = RS->getRetValue()) {
165 ExprScope<Emitter> RetScope(this);
167 // Primitive types are simply returned.
168 if (!this->visit(RE))
171 return this->emitRet(*ReturnType, RS);
173 // RVO - construct the value in the return location.
174 auto ReturnLocation = [this, RE] { return this->emitGetParamPtr(0, RE); };
175 if (!this->visitInitializer(RE, ReturnLocation))
178 return this->emitRetVoid(RS);
182 if (!this->emitRetVoid(RS))
188 template <class Emitter>
189 bool ByteCodeStmtGen<Emitter>::visitIfStmt(const IfStmt *IS) {
190 BlockScope<Emitter> IfScope(this);
191 if (auto *CondInit = IS->getInit())
192 if (!visitStmt(IS->getInit()))
195 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
196 if (!visitDeclStmt(CondDecl))
199 if (!this->visitBool(IS->getCond()))
202 if (const Stmt *Else = IS->getElse()) {
203 LabelTy LabelElse = this->getLabel();
204 LabelTy LabelEnd = this->getLabel();
205 if (!this->jumpFalse(LabelElse))
207 if (!visitStmt(IS->getThen()))
209 if (!this->jump(LabelEnd))
211 this->emitLabel(LabelElse);
212 if (!visitStmt(Else))
214 this->emitLabel(LabelEnd);
216 LabelTy LabelEnd = this->getLabel();
217 if (!this->jumpFalse(LabelEnd))
219 if (!visitStmt(IS->getThen()))
221 this->emitLabel(LabelEnd);
227 template <class Emitter>
228 bool ByteCodeStmtGen<Emitter>::visitVarDecl(const VarDecl *VD) {
229 auto DT = VD->getType();
231 if (!VD->hasLocalStorage()) {
232 // No code generation required.
236 // Integers, pointers, primitives.
237 if (Optional<PrimType> T = this->classify(DT)) {
238 auto Off = this->allocateLocalPrimitive(VD, *T, DT.isConstQualified());
239 // Compile the initialiser in its own scope.
241 ExprScope<Emitter> Scope(this);
242 if (!this->visit(VD->getInit()))
246 return this->emitSetLocal(*T, Off, VD);
248 // Composite types - allocate storage and initialize it.
249 if (auto Off = this->allocateLocal(VD)) {
250 return this->visitLocalInitializer(VD->getInit(), *Off);
252 return this->bail(VD);
260 template class ByteCodeStmtGen<ByteCodeEmitter>;
262 } // namespace interp