1/*
2 * Copyright (c) 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#ifndef ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
17#define ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
18
19#include "ecmascript/compiler/bytecode_circuit_builder.h"
20#include "ecmascript/compiler/circuit.h"
21#include "ecmascript/compiler/circuit_builder.h"
22
23namespace panda::ecmascript::kungfu {
24class AsyncFunctionLowering {
25public:
26    AsyncFunctionLowering(BytecodeCircuitBuilder *bcBuilder, Circuit *circuit, CompilationConfig *cmpCfg,
27                          bool enableLog, const std::string& name)
28        : bcBuilder_(bcBuilder), circuit_(circuit), builder_(circuit, cmpCfg), enableLog_(enableLog),
29          accessor_(circuit), argAccessor_(circuit), stateEntry_(GetEntryBBStateOut()),
30          dependEntry_(GetEntryBBDependOut()), methodName_(name)
31    {
32    }
33
34    ~AsyncFunctionLowering() = default;
35
36    void ProcessAll();
37
38    bool IsAsyncRelated() const;
39
40    const std::string& GetMethodName() const
41    {
42        return methodName_;
43    }
44
45private:
46    bool IsLogEnabled() const
47    {
48        return enableLog_;
49    }
50
51    void ProcessJumpTable();
52
53    void RebuildGeneratorCfg(GateRef resumeGate, GateRef restoreOffsetGate, GateRef ifFalseCondition, GateRef newTarget,
54                             GateRef &firstState);
55
56    void UpdateValueSelector(GateRef prevLoopBeginGate, GateRef controlStateGate, GateRef prevBcOffsetPhiGate,
57                             bool genNewValuePhiGate = true);
58
59    void CheckResumeInLoopBody(GateRef stateInGate, bool &resumeInLoopBody);
60
61    void ModifyStateInput(GateRef stateInGate, GateRef ifBranch, GateRef ifFalse);
62
63    GateRef GetDependPhiFromLoopBegin(GateRef loopbegin) const;
64
65    GateRef GetEntryBBStateOut() const;
66
67    GateRef GetEntryBBDependOut() const;
68
69    BytecodeCircuitBuilder *bcBuilder_;
70    Circuit *circuit_;
71    CircuitBuilder builder_;
72    bool enableLog_ {false};
73    GateAccessor accessor_;
74    ArgumentAccessor argAccessor_;
75    GateRef stateEntry_ {Circuit::NullGate()};
76    GateRef dependEntry_ {Circuit::NullGate()};
77    std::string methodName_;
78};
79}  // panda::ecmascript::kungfu
80
81#endif // ECMASCRIPT_COMPILER_ASYNC_FUNCTION_LOWRING_H_
82