• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 "asyncGeneratorFunctionBuilder.h"
17 
18 #include <compiler/base/catchTable.h>
19 #include <compiler/core/pandagen.h>
20 #include <ir/base/scriptFunction.h>
21 
22 namespace panda::es2panda::compiler {
Prepare(const ir::ScriptFunction * node)23 void AsyncGeneratorFunctionBuilder::Prepare(const ir::ScriptFunction *node)
24 {
25     RegScope rs(pg_);
26     VReg callee = FunctionReg(node);
27     VReg completionType = pg_->AllocReg();
28     VReg completionValue = pg_->AllocReg();
29 
30     pg_->CreateAsyncGeneratorObj(node, callee);
31     pg_->StoreAccumulator(node, funcObj_);
32 
33     pg_->SetLabel(node, catchTable_->LabelSet().TryBegin());
34 
35     pg_->LoadConst(node, Constant::JS_UNDEFINED);
36     SuspendResumeExecution(node, completionType, completionValue);
37 }
38 
CleanUp(const ir::ScriptFunction * node) const39 void AsyncGeneratorFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const
40 {
41     const auto &labelSet = catchTable_->LabelSet();
42 
43     RegScope rs(pg_);
44     VReg retVal = pg_->AllocReg();
45 
46     pg_->SetLabel(node, labelSet.TryEnd());
47     pg_->SetLabel(node, labelSet.CatchBegin());
48     pg_->StoreAccumulator(node, retVal);
49     pg_->GeneratorComplete(node, funcObj_);
50     pg_->LoadAccumulator(node, retVal);
51     pg_->AsyncGeneratorReject(node, funcObj_);
52     pg_->EmitReturn(node);
53     pg_->SetLabel(node, labelSet.CatchEnd());
54 }
55 
DirectReturn(const ir::AstNode * node) const56 void AsyncGeneratorFunctionBuilder::DirectReturn(const ir::AstNode *node) const
57 {
58     RegScope rs(pg_);
59     VReg retVal = pg_->AllocReg();
60     VReg canSuspend = pg_->AllocReg();
61 
62     pg_->StoreAccumulator(node, retVal);
63     pg_->StoreConst(node, canSuspend, Constant::JS_TRUE);
64 
65     pg_->GeneratorComplete(node, funcObj_);
66     pg_->AsyncGeneratorResolve(node, funcObj_, retVal, canSuspend);
67     pg_->EmitReturn(node);
68 }
69 
ImplicitReturn(const ir::AstNode * node) const70 void AsyncGeneratorFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
71 {
72     pg_->LoadConst(node, Constant::JS_UNDEFINED);
73     DirectReturn(node);
74 }
75 
ExplicitReturn(const ir::AstNode * node) const76 void AsyncGeneratorFunctionBuilder::ExplicitReturn(const ir::AstNode *node) const
77 {
78     RegScope rs(pg_);
79     VReg resumeType = pg_->AllocReg();
80     VReg resumeValue = pg_->AllocReg();
81     VReg canSuspend = pg_->AllocReg();
82 
83     pg_->StoreAccumulator(node, resumeValue);
84     pg_->GeneratorComplete(node, funcObj_);
85     pg_->LoadAccumulator(node, resumeValue);
86     pg_->AsyncFunctionAwait(node, funcObj_);
87     SuspendResumeExecution(node, resumeType, resumeValue);
88     pg_->StoreConst(node, canSuspend, Constant::JS_TRUE);
89     pg_->AsyncGeneratorResolve(node, funcObj_, resumeValue, canSuspend);
90     pg_->EmitReturn(node);
91 }
92 
Yield(const ir::AstNode * node)93 void AsyncGeneratorFunctionBuilder::Yield(const ir::AstNode *node)
94 {
95     RegScope rs(pg_);
96     VReg value = pg_->AllocReg();
97     VReg resumeType = pg_->AllocReg();
98     VReg resumeValue = pg_->AllocReg();
99     auto *notReturn = pg_->AllocLabel();
100     auto *normalCompletion = pg_->AllocLabel();
101     auto *notThrowCompletion = pg_->AllocLabel();
102 
103     // 27.6.3.8.5 Set value to ? Await(value).
104     Await(node);
105     pg_->StoreAccumulator(node, value);
106 
107     AsyncYield(node, value, resumeType, resumeValue);
108 
109     // 27.6.3.8.8.a If resumptionValue.[[Type]] is not return
110     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
111     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, notReturn);
112 
113     // 27.6.3.8.8.b Let awaited be Await(resumptionValue.[[Value]]).
114     pg_->LoadAccumulator(node, resumeValue);
115     pg_->AsyncFunctionAwait(node, funcObj_);
116     SuspendResumeExecution(node, resumeType, resumeValue);
117 
118     // 27.6.3.8.8.c. If awaited.[[Type]] is throw, return Completion(awaited).
119     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
120     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, normalCompletion);
121     pg_->LoadAccumulator(node, resumeValue);
122     pg_->EmitThrow(node);
123 
124     pg_->SetLabel(node, normalCompletion);
125     // 27.6.3.8.8.d. Assert: awaited.[[Type]] is normal.
126     // 27.6.3.8.8.e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }
127     pg_->ControlFlowChangeBreak();
128     pg_->LoadAccumulator(node, resumeValue);
129     DirectReturn(node);
130 
131     pg_->SetLabel(node, notReturn);
132     // 27.6.3.8.8.a return Completion(resumptionValue)
133     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
134     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, notThrowCompletion);
135     pg_->LoadAccumulator(node, resumeValue);
136     pg_->EmitThrow(node);
137     pg_->SetLabel(node, notThrowCompletion);
138     pg_->LoadAccumulator(node, resumeValue);
139 }
140 
GeneratorKind() const141 IteratorType AsyncGeneratorFunctionBuilder::GeneratorKind() const
142 {
143     return IteratorType::ASYNC;
144 }
145 }  // namespace panda::es2panda::compiler
146