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 "asyncFunctionBuilder.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
DirectReturn(const ir::AstNode * node) const24 void AsyncFunctionBuilder::DirectReturn(const ir::AstNode *node) const
25 {
26 pg_->AsyncFunctionResolve(node, funcObj_); // retVal is in acc
27 pg_->NotifyConcurrentResult(node);
28 // The INVALID_SOURCE_LOCATION is set in the implicitReturn method. When implicitReturn
29 // invokes the DirectReturn method, the EmitReturn needs to be associated with a valid node
30 pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
31 pg_->EmitReturn(node);
32 }
33
ImplicitReturn(const ir::AstNode * node) const34 void AsyncFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
35 {
36 pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION);
37 pg_->LoadConst(node, Constant::JS_UNDEFINED);
38 DirectReturn(node);
39 }
40
ExplicitReturn(const ir::AstNode * node) const41 void AsyncFunctionBuilder::ExplicitReturn(const ir::AstNode *node) const
42 {
43 DirectReturn(node);
44 }
45
Prepare(const ir::ScriptFunction * node)46 void AsyncFunctionBuilder::Prepare(const ir::ScriptFunction *node)
47 {
48 pg_->AsyncFunctionEnter(node);
49 pg_->StoreAccumulator(node, funcObj_);
50 pg_->SetLabel(node, catchTable_->LabelSet().TryBegin());
51 }
52
CleanUp(const ir::ScriptFunction * node) const53 void AsyncFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const
54 {
55 RegScope rs(pg_);
56 const auto &labelSet = catchTable_->LabelSet();
57
58 pg_->SetLabel(node, labelSet.TryEnd());
59 pg_->SetLabel(node, labelSet.CatchBegin());
60 VReg exception = pg_->AllocReg();
61 pg_->StoreAccumulator(node, exception);
62 pg_->AsyncFunctionReject(node, funcObj_);
63 pg_->NotifyConcurrentResult(node);
64 pg_->EmitReturn(node);
65 pg_->SetLabel(node, labelSet.CatchEnd());
66 }
67 } // namespace panda::es2panda::compiler
68