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_->AsyncFunctionAwait(node, funcObj_);
84 SuspendResumeExecution(node, resumeType, resumeValue);
85 pg_->GeneratorComplete(node, funcObj_);
86 pg_->StoreConst(node, canSuspend, Constant::JS_TRUE);
87 pg_->AsyncGeneratorResolve(node, funcObj_, resumeValue, canSuspend);
88 pg_->EmitReturn(node);
89 }
90
Yield(const ir::AstNode * node)91 void AsyncGeneratorFunctionBuilder::Yield(const ir::AstNode *node)
92 {
93 RegScope rs(pg_);
94 VReg value = pg_->AllocReg();
95 VReg resumeType = pg_->AllocReg();
96 VReg resumeValue = pg_->AllocReg();
97 auto *notReturn = pg_->AllocLabel();
98 auto *normalCompletion = pg_->AllocLabel();
99 auto *notThrowCompletion = pg_->AllocLabel();
100
101 // 27.6.3.8.5 Set value to ? Await(value).
102 Await(node);
103 pg_->StoreAccumulator(node, value);
104
105 AsyncYield(node, value, resumeType, resumeValue);
106
107 // 27.6.3.8.8.a If resumptionValue.[[Type]] is not return
108 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
109 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, notReturn);
110
111 // 27.6.3.8.8.b Let awaited be Await(resumptionValue.[[Value]]).
112 pg_->LoadAccumulator(node, resumeValue);
113 pg_->AsyncFunctionAwait(node, funcObj_);
114 SuspendResumeExecution(node, resumeType, resumeValue);
115
116 // 27.6.3.8.8.c. If awaited.[[Type]] is throw, return Completion(awaited).
117 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
118 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, normalCompletion);
119 pg_->LoadAccumulator(node, resumeValue);
120 pg_->EmitThrow(node);
121
122 pg_->SetLabel(node, normalCompletion);
123 // 27.6.3.8.8.d. Assert: awaited.[[Type]] is normal.
124 // 27.6.3.8.8.e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }
125 pg_->ControlFlowChangeBreak();
126 pg_->LoadAccumulator(node, resumeValue);
127 DirectReturn(node);
128
129 pg_->SetLabel(node, notReturn);
130 // 27.6.3.8.8.a return Completion(resumptionValue)
131 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
132 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, resumeType, notThrowCompletion);
133 pg_->LoadAccumulator(node, resumeValue);
134 pg_->EmitThrow(node);
135 pg_->SetLabel(node, notThrowCompletion);
136 pg_->LoadAccumulator(node, resumeValue);
137 }
138
GeneratorKind() const139 IteratorType AsyncGeneratorFunctionBuilder::GeneratorKind() const
140 {
141 return IteratorType::ASYNC;
142 }
143 } // namespace panda::es2panda::compiler
144