1/** 2 * Copyright (c) 2021-2024 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 22namespace ark::es2panda::compiler { 23void AsyncGeneratorFunctionBuilder::Prepare(const ir::ScriptFunction *node) const 24{ 25 VReg callee = FunctionReg(node); 26 27 pg_->CreateAsyncGeneratorObj(node, callee); 28 pg_->StoreAccumulator(node, funcObj_); 29 pg_->SuspendGenerator(node, funcObj_); 30 pg_->SetLabel(node, catchTable_->LabelSet().TryBegin()); 31} 32 33void AsyncGeneratorFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const 34{ 35 const auto &labelSet = catchTable_->LabelSet(); 36 37 pg_->SetLabel(node, labelSet.TryEnd()); 38 pg_->SetLabel(node, labelSet.CatchBegin()); 39 pg_->AsyncGeneratorReject(node, funcObj_); 40 pg_->EmitReturn(node); 41 pg_->SetLabel(node, labelSet.CatchEnd()); 42} 43 44void AsyncGeneratorFunctionBuilder::DirectReturn(const ir::AstNode *node) const 45{ 46 pg_->AsyncGeneratorResolve(node, funcObj_); 47 pg_->EmitReturn(node); 48} 49 50void AsyncGeneratorFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const 51{ 52 pg_->LoadConst(node, Constant::JS_UNDEFINED); 53 DirectReturn(node); 54} 55 56void AsyncGeneratorFunctionBuilder::Yield(const ir::AstNode *node) 57{ 58 Await(node); 59 60 RegScope rs(pg_); 61 VReg completionType = pg_->AllocReg(); 62 VReg completionValue = pg_->AllocReg(); 63 64 AsyncYield(node, completionType, completionValue); 65 66 auto *notReturnCompletion = pg_->AllocLabel(); 67 auto *normalCompletion = pg_->AllocLabel(); 68 auto *notThrowCompletion = pg_->AllocLabel(); 69 if (notReturnCompletion == nullptr || normalCompletion == nullptr || notThrowCompletion == nullptr) { 70 // NOTE(ipetrov): maybe need to change it 71 return; 72 } 73 74 // 27.6.3.8.8.a. If resumptionValue.[[Type]] is not return 75 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN)); 76 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notReturnCompletion); 77 // 27.6.3.8.8.b. Let awaited be Await(resumptionValue.[[Value]]). 78 pg_->LoadAccumulator(node, completionValue); 79 pg_->AsyncFunctionAwait(node, funcObj_); 80 SuspendResumeExecution(node, completionType, completionValue); 81 82 // 27.6.3.8.8.c. If awaited.[[Type]] is throw, return Completion(awaited). 83 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW)); 84 85 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, normalCompletion); 86 pg_->LoadAccumulator(node, completionValue); 87 pg_->EmitThrow(node); 88 89 pg_->SetLabel(node, normalCompletion); 90 // 27.6.3.8.8.d. Assert: awaited.[[Type]] is normal. 91 // 27.6.3.8.8.e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }. 92 pg_->ControlFlowChangeBreak(); 93 pg_->LoadAccumulator(node, completionValue); 94 pg_->DirectReturn(node); 95 96 pg_->SetLabel(node, notReturnCompletion); 97 // 27.6.3.8.8.a. return Completion(resumptionValue). 98 pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW)); 99 pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notThrowCompletion); 100 pg_->LoadAccumulator(node, completionValue); 101 pg_->EmitThrow(node); 102 pg_->SetLabel(node, notThrowCompletion); 103 pg_->LoadAccumulator(node, completionValue); 104} 105 106IteratorType AsyncGeneratorFunctionBuilder::GeneratorKind() const 107{ 108 return IteratorType::ASYNC; 109} 110} // namespace ark::es2panda::compiler 111