14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/bytecode_circuit_builder.h"
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit.h"
214514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit_builder.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
244514f5e3Sopenharmony_ciclass AsyncFunctionLowering {
254514f5e3Sopenharmony_cipublic:
264514f5e3Sopenharmony_ci    AsyncFunctionLowering(BytecodeCircuitBuilder *bcBuilder, Circuit *circuit, CompilationConfig *cmpCfg,
274514f5e3Sopenharmony_ci                          bool enableLog, const std::string& name)
284514f5e3Sopenharmony_ci        : bcBuilder_(bcBuilder), circuit_(circuit), builder_(circuit, cmpCfg), enableLog_(enableLog),
294514f5e3Sopenharmony_ci          accessor_(circuit), argAccessor_(circuit), stateEntry_(GetEntryBBStateOut()),
304514f5e3Sopenharmony_ci          dependEntry_(GetEntryBBDependOut()), methodName_(name)
314514f5e3Sopenharmony_ci    {
324514f5e3Sopenharmony_ci    }
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci    ~AsyncFunctionLowering() = default;
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    void ProcessAll();
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    bool IsAsyncRelated() const;
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    const std::string& GetMethodName() const
414514f5e3Sopenharmony_ci    {
424514f5e3Sopenharmony_ci        return methodName_;
434514f5e3Sopenharmony_ci    }
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ciprivate:
464514f5e3Sopenharmony_ci    bool IsLogEnabled() const
474514f5e3Sopenharmony_ci    {
484514f5e3Sopenharmony_ci        return enableLog_;
494514f5e3Sopenharmony_ci    }
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    void ProcessJumpTable();
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    void RebuildGeneratorCfg(GateRef resumeGate, GateRef restoreOffsetGate, GateRef ifFalseCondition, GateRef newTarget,
544514f5e3Sopenharmony_ci                             GateRef &firstState);
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    void UpdateValueSelector(GateRef prevLoopBeginGate, GateRef controlStateGate, GateRef prevBcOffsetPhiGate,
574514f5e3Sopenharmony_ci                             bool genNewValuePhiGate = true);
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    void CheckResumeInLoopBody(GateRef stateInGate, bool &resumeInLoopBody);
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    void ModifyStateInput(GateRef stateInGate, GateRef ifBranch, GateRef ifFalse);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    GateRef GetDependPhiFromLoopBegin(GateRef loopbegin) const;
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    GateRef GetEntryBBStateOut() const;
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    GateRef GetEntryBBDependOut() const;
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    BytecodeCircuitBuilder *bcBuilder_;
704514f5e3Sopenharmony_ci    Circuit *circuit_;
714514f5e3Sopenharmony_ci    CircuitBuilder builder_;
724514f5e3Sopenharmony_ci    bool enableLog_ {false};
734514f5e3Sopenharmony_ci    GateAccessor accessor_;
744514f5e3Sopenharmony_ci    ArgumentAccessor argAccessor_;
754514f5e3Sopenharmony_ci    GateRef stateEntry_ {Circuit::NullGate()};
764514f5e3Sopenharmony_ci    GateRef dependEntry_ {Circuit::NullGate()};
774514f5e3Sopenharmony_ci    std::string methodName_;
784514f5e3Sopenharmony_ci};
794514f5e3Sopenharmony_ci}  // panda::ecmascript::kungfu
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
82