• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "generatorFunctionBuilder.h"
17 
18 #include <compiler/core/pandagen.h>
19 #include <compiler/base/catchTable.h>
20 #include <ir/base/scriptFunction.h>
21 
22 namespace panda::es2panda::compiler {
23 
Prepare(const ir::ScriptFunction * node)24 void GeneratorFunctionBuilder::Prepare(const ir::ScriptFunction *node)
25 {
26     VReg callee = FunctionReg(node);
27     VReg completionType = pg_->AllocReg();
28     VReg completionValue = pg_->AllocReg();
29 
30     pg_->CreateGeneratorObj(node, callee);
31     pg_->StoreAccumulator(node, funcObj_);
32     pg_->LoadConst(node, Constant::JS_UNDEFINED);
33     SuspendResumeExecution(node, completionType, completionValue);
34     HandleCompletion(node, completionType, completionValue);
35 }
36 
CleanUp(const ir::ScriptFunction * node) const37 void GeneratorFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const
38 {
39     const auto &labelSet = catchTable_->LabelSet();
40 
41     pg_->SetLabel(node, labelSet.TryEnd());
42     pg_->SetLabel(node, labelSet.CatchBegin());
43     pg_->EmitThrow(node);
44     pg_->SetLabel(node, labelSet.CatchEnd());
45 }
46 
DirectReturn(const ir::AstNode * node) const47 void GeneratorFunctionBuilder::DirectReturn(const ir::AstNode *node) const
48 {
49     pg_->EmitReturn(node);
50 }
51 
ImplicitReturn(const ir::AstNode * node) const52 void GeneratorFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
53 {
54     pg_->LoadConst(node, Constant::JS_UNDEFINED);
55     DirectReturn(node);
56 }
57 
ExplicitReturn(const ir::AstNode * node) const58 void GeneratorFunctionBuilder::ExplicitReturn(const ir::AstNode *node) const
59 {
60     DirectReturn(node);
61 }
62 
Yield(const ir::AstNode * node)63 void GeneratorFunctionBuilder::Yield(const ir::AstNode *node)
64 {
65     RegScope rs(pg_);
66     VReg value = pg_->AllocReg();
67     VReg done = pg_->AllocReg();
68     VReg completionType = pg_->AllocReg();
69     VReg completionValue = pg_->AllocReg();
70 
71     pg_->StoreAccumulator(node, value);
72     pg_->LoadConst(node, Constant::JS_FALSE);
73     pg_->StoreAccumulator(node, done);
74     pg_->CreateIterResultObject(node, value, done);
75     SuspendResumeExecution(node, completionType, completionValue);
76 
77     HandleCompletion(node, completionType, completionValue);
78 }
79 
80 }  // namespace panda::es2panda::compiler
81