• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dynamicContext.h"
17 
18 #include "checker/types/ets/etsObjectType.h"
19 #include "checker/types/type.h"
20 #include "compiler/core/envScope.h"
21 #include "compiler/core/ETSGen.h"
22 #include "compiler/core/pandagen.h"
23 #include "compiler/base/catchTable.h"
24 #include "ir/expressions/identifier.h"
25 #include "ir/base/catchClause.h"
26 #include "ir/statements/blockStatement.h"
27 #include "ir/statements/breakStatement.h"
28 #include "ir/statements/continueStatement.h"
29 #include "ir/statements/returnStatement.h"
30 #include "ir/statements/tryStatement.h"
31 #include "ir/statements/labelledStatement.h"
32 
33 namespace ark::es2panda::compiler {
DynamicContext(CodeGen * cg,LabelTarget target)34 DynamicContext::DynamicContext(CodeGen *cg, LabelTarget target) : cg_(cg), target_(target), prev_(Cg()->dynamicContext_)
35 {
36     Cg()->dynamicContext_ = this;
37 }
38 
~DynamicContext()39 DynamicContext::~DynamicContext()
40 {
41     Cg()->dynamicContext_ = prev_;
42 }
43 
LabelContext(CodeGen * cg,const ir::LabelledStatement * labelledStmt)44 LabelContext::LabelContext(CodeGen *cg, const ir::LabelledStatement *labelledStmt)
45     : DynamicContext(cg, LabelTarget(labelledStmt->Ident()->Name())), labelledStmt_(labelledStmt)
46 {
47     if (!labelledStmt->Body()->IsBlockStatement()) {
48         return;
49     }
50 
51     label_ = cg->AllocLabel();
52     Target().SetBreakTarget(label_);
53 }
54 
~LabelContext()55 LabelContext::~LabelContext()
56 {
57     if (label_ == nullptr) {
58         return;
59     }
60 
61     Cg()->SetLabel(labelledStmt_, label_);
62 }
63 
LexEnvContext(LoopEnvScope * envScope,PandaGen * pg,LabelTarget target)64 LexEnvContext::LexEnvContext(LoopEnvScope *envScope, PandaGen *pg, LabelTarget target)
65     : DynamicContext(pg, target), envScope_(envScope)
66 {
67     if (!envScope_->HasEnv()) {
68         return;
69     }
70 
71     catchTable_ = Cg()->CreateCatchTable();
72     ES2PANDA_ASSERT(catchTable_ != nullptr);
73     const auto &labelSet = catchTable_->LabelSet();
74     const auto *node = envScope_->Scope()->Node();
75 
76     Cg()->SetLabel(node, labelSet.TryBegin());
77 }
78 
~LexEnvContext()79 LexEnvContext::~LexEnvContext()
80 {
81     if (!envScope_->HasEnv()) {
82         return;
83     }
84 
85     const auto &labelSet = catchTable_->LabelSet();
86     const auto *node = envScope_->Scope()->Node();
87 
88     Cg()->SetLabel(node, labelSet.TryEnd());
89     Cg()->Branch(node, labelSet.CatchEnd());
90 
91     Cg()->SetLabel(node, labelSet.CatchBegin());
92     AsPandaGen()->PopLexEnv(node);
93     AsPandaGen()->EmitThrow(node);
94     Cg()->SetLabel(node, labelSet.CatchEnd());
95     AsPandaGen()->PopLexEnv(node);
96 }
97 
AsPandaGen() const98 PandaGen *LexEnvContext::AsPandaGen() const
99 {
100     return static_cast<PandaGen *>(Cg());
101 }
102 
HasTryCatch() const103 bool LexEnvContext::HasTryCatch() const
104 {
105     return envScope_->HasEnv();
106 }
107 
AbortContext(ControlFlowChange cfc,const util::StringView & targetLabel)108 void LexEnvContext::AbortContext([[maybe_unused]] ControlFlowChange cfc,
109                                  [[maybe_unused]] const util::StringView &targetLabel)
110 {
111     if (cfc == ControlFlowChange::CONTINUE || !envScope_->HasEnv()) {
112         return;
113     }
114 
115     const auto *node = envScope_->Scope()->Node();
116     AsPandaGen()->PopLexEnv(node);
117 }
118 
IteratorContext(PandaGen * pg,const Iterator & iterator,LabelTarget target)119 IteratorContext::IteratorContext(PandaGen *pg, const Iterator &iterator, LabelTarget target)
120     : DynamicContext(pg, target), iterator_(iterator), catchTable_(pg->CreateCatchTable())
121 {
122     const auto &labelSet = catchTable_->LabelSet();
123     pg->SetLabel(iterator_.Node(), labelSet.TryBegin());
124 }
125 
~IteratorContext()126 IteratorContext::~IteratorContext()
127 {
128     const auto &labelSet = catchTable_->LabelSet();
129     const auto *node = iterator_.Node();
130 
131     Cg()->SetLabel(node, labelSet.TryEnd());
132     Cg()->Branch(node, labelSet.CatchEnd());
133 
134     Cg()->SetLabel(node, labelSet.CatchBegin());
135     iterator_.Close(true);
136     Cg()->SetLabel(node, labelSet.CatchEnd());
137 }
138 
AbortContext(ControlFlowChange cfc,const util::StringView & targetLabel)139 void IteratorContext::AbortContext([[maybe_unused]] ControlFlowChange cfc,
140                                    [[maybe_unused]] const util::StringView &targetLabel)
141 {
142     if (cfc == ControlFlowChange::CONTINUE && Target().ContinueLabel() == targetLabel) {
143         return;
144     }
145 
146     iterator_.Close(false);
147 }
148 
InitFinalizer()149 void TryContext::InitFinalizer()
150 {
151     ES2PANDA_ASSERT(tryStmt_);
152 
153     if (!hasFinalizer_ || (tryStmt_->FinallyBlock() == nullptr)) {
154         return;
155     }
156 
157     auto *pg = static_cast<PandaGen *>(Cg());
158 
159     finalizerRun_ = pg->AllocReg();
160     pg->StoreConst(tryStmt_, finalizerRun_, Constant::JS_UNDEFINED);
161 }
162 
InitCatchTable()163 void CatchContext::InitCatchTable()
164 {
165     auto *pg = static_cast<PandaGen *>(Cg());
166     catchTable_ = pg->CreateCatchTable();
167 }
168 
LabelSet() const169 const TryLabelSet &CatchContext::LabelSet() const
170 {
171     return catchTable_->LabelSet();
172 }
173 
HasFinalizer() const174 bool TryContext::HasFinalizer() const
175 {
176     return hasFinalizer_;
177 }
178 
EmitFinalizer()179 void TryContext::EmitFinalizer()
180 {
181     if (!hasFinalizer_ || inFinalizer_ || (tryStmt_->FinallyBlock() == nullptr)) {
182         return;
183     }
184 
185     auto *pg = static_cast<PandaGen *>(Cg());
186     inFinalizer_ = true;
187     tryStmt_->FinallyBlock()->Compile(pg);
188     inFinalizer_ = false;
189 }
190 
AddNewCathTable(const util::StringView assemblerType)191 CatchTable *ETSCatchContext::AddNewCathTable(const util::StringView assemblerType)
192 {
193     auto *cg = Cg();
194 
195     CatchTable *catchTable = cg->CreateCatchTable(assemblerType);
196     catchTables_.push_back(catchTable);
197 
198     return catchTable;
199 }
200 
AddNewCathTable(const util::StringView assemblerType,const LabelPair tryLabelPair)201 CatchTable *ETSCatchContext::AddNewCathTable(const util::StringView assemblerType, const LabelPair tryLabelPair)
202 {
203     auto *cg = Cg();
204 
205     CatchTable *catchTable = cg->CreateCatchTable(tryLabelPair, assemblerType);
206     catchTables_.push_back(catchTable);
207 
208     return catchTable;
209 }
210 
EmitFinalizer(LabelPair trycatchLabelPair,const ArenaVector<std::pair<compiler::LabelPair,const ir::Statement * >> & finalizerInsertions)211 void ETSTryContext::EmitFinalizer(
212     LabelPair trycatchLabelPair,
213     const ArenaVector<std::pair<compiler::LabelPair, const ir::Statement *>> &finalizerInsertions)
214 {
215     ES2PANDA_ASSERT(tryStmt_);
216 
217     if (!hasFinalizer_ || (tryStmt_->FinallyBlock() == nullptr)) {
218         return;
219     }
220     auto *etsg = static_cast<ETSGen *>(Cg());
221 
222     CatchTable *finalizerTable = AddNewCathTable("", trycatchLabelPair);
223     // First compile of the finaly clause, executed if the statement executed normally
224     tryStmt_->FinallyBlock()->Compile(etsg);
225 
226     ES2PANDA_ASSERT(finalizerTable != nullptr);
227     etsg->Branch(tryStmt_, finalizerTable->LabelSet().CatchEnd());
228 
229     for (std::pair<compiler::LabelPair, const ir::Statement *> insertion : finalizerInsertions) {
230         EmitFinalizerInsertion(etsg, insertion.first, insertion.second);
231     }
232 
233     etsg->SetLabel(tryStmt_, finalizerTable->LabelSet().CatchBegin());
234 
235     compiler::VReg exception = etsg->StoreException(tryStmt_);
236     // Third compile of the finaly clause, executed if the statement executed abruptly
237     tryStmt_->FinallyBlock()->Compile(etsg);
238 
239     etsg->LoadAccumulator(tryStmt_, exception);
240     etsg->EmitThrow(tryStmt_, exception);
241 
242     etsg->SetLabel(tryStmt_, finalizerTable->LabelSet().CatchEnd());
243 }
244 
EmitFinalizerInsertion(ETSGen * etsg,compiler::LabelPair labelPair,const ir::Statement * statement)245 void ETSTryContext::EmitFinalizerInsertion(ETSGen *etsg, compiler::LabelPair labelPair, const ir::Statement *statement)
246 {
247     etsg->SetLabel(tryStmt_, labelPair.Begin());
248 
249     ES2PANDA_ASSERT(statement != nullptr);
250     bool isReturn = statement->IsReturnStatement();
251 
252     compiler::RegScope rs(etsg);
253     compiler::VReg res = etsg->AllocReg();
254 
255     if (isReturn) {
256         etsg->SetAccumulatorType(statement->AsReturnStatement()->ReturnType());
257         etsg->StoreAccumulator(tryStmt_, res);
258         etsg->SetVRegType(res, statement->AsReturnStatement()->ReturnType());
259     }
260 
261     // Second compile of the finaly clause, executed if the statement executed normally, but abrupted by
262     // return, break, or continue statements.
263     tryStmt_->FinallyBlock()->Compile(etsg);
264 
265     if (isReturn) {
266         etsg->SetAccumulatorType(statement->AsReturnStatement()->ReturnType());
267         etsg->LoadAccumulator(tryStmt_, res);
268     }
269 
270     if (labelPair.End() != nullptr) {
271         etsg->Branch(tryStmt_, labelPair.End());
272     } else if (isReturn) {
273         if (etsg->CheckControlFlowChange()) {
274             etsg->StoreAccumulator(tryStmt_, res);
275             etsg->ControlFlowChangeBreak();
276             etsg->LoadAccumulator(tryStmt_, res);
277         }
278 
279         etsg->ReturnAcc(tryStmt_);
280     } else if (statement->IsBreakStatement()) {
281         compiler::Label *target = etsg->ControlFlowChangeBreak(statement->AsBreakStatement()->Ident());
282         etsg->Branch(tryStmt_, target);
283     } else if (statement->IsContinueStatement()) {
284         compiler::Label *target = etsg->ControlFlowChangeContinue(statement->AsContinueStatement()->Ident());
285         etsg->Branch(tryStmt_, target);
286     } else {
287         ES2PANDA_UNREACHABLE();
288     }
289 }
290 
291 }  // namespace ark::es2panda::compiler
292