1 /*
2  * Copyright (c) 2023 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_MCR_LOWERING_H
17 #define ECMASCRIPT_COMPILER_MCR_LOWERING_H
18 
19 #include "ecmascript/compiler/circuit.h"
20 #include "ecmascript/compiler/circuit_builder-inl.h"
21 #include "ecmascript/compiler/combined_pass_visitor.h"
22 #include "ecmascript/compiler/compilation_env.h"
23 #include "ecmascript/compiler/gate_accessor.h"
24 
25 namespace panda::ecmascript::kungfu {
26 class MCRLowering : public PassVisitor {
27 public:
MCRLowering(CompilationEnv* env, Circuit *circuit, RPOVisitor *visitor, CompilationConfig *cmpCfg, Chunk *chunk)28     MCRLowering(CompilationEnv* env, Circuit *circuit, RPOVisitor *visitor, CompilationConfig *cmpCfg, Chunk *chunk)
29         : PassVisitor(circuit, chunk, visitor), env_(env), circuit_(circuit), acc_(circuit),
30           builder_(circuit, cmpCfg), glue_(acc_.GetGlueFromArgList())
31     {
32     }
33     ~MCRLowering() = default;
34 
35     GateRef VisitGate(GateRef gate) override;
36     StateDepend LowerConvert(StateDepend stateDepend, GateRef gate);
37 private:
38 
39     void DeleteStateSplit(GateRef gate);
40     void LowerArrayGuardianCheck(GateRef gate);
41     void LowerHeapObjectCheck(GateRef gate);
42     void LowerTaggedIsHeapObject(GateRef gate);
43     void LowerIsMarkerCellValid(GateRef gate);
44     void LowerIsSpecificObjectType(GateRef gate);
45     void LowerHClassStableArrayCheck(GateRef gate);
46     void LowerGetConstPool(GateRef gate);
47     void LowerGetUnsharedConstpool(GateRef gate);
48     void LowerLoadConstOffset(GateRef gate);
49     void LowerLoadHClassFromConstpool(GateRef gate);
50     void LowerStoreConstOffset(GateRef gate);
51     void LowerConvertHoleAsUndefined(GateRef gate);
52     void LowerCheckAndConvert(GateRef gate);
53     void LowerCheckUInt32AndConvert(GateRef gate, GateRef frameState);
54     void LowerCheckTaggedIntAndConvert(GateRef gate, GateRef frameState);
55     void LowerCheckTaggedDoubleAndConvert(GateRef gate, GateRef frameState, Label *exit);
56     void LowerCheckTaggedNumberAndConvert(GateRef gate, GateRef frameState, Label *exit);
57     void LowerCheckTaggedBoolAndConvert(GateRef gate, GateRef frameState);
58     void LowerCheckSpecialHoleAndConvert(GateRef gate, GateRef frameState);
59     void LowerCheckSupportAndConvert(GateRef gate, GateRef frameState);
60     void LowerGetGlobalEnv(GateRef gate);
61     void LowerGetGlobalEnvObj(GateRef gate);
62     void LowerGetGlobalEnvObjHClass(GateRef gate);
63     void LowerGetGlobalConstantValue(GateRef gate);
64     void LowerInt32CheckRightIsZero(GateRef gate);
65     void LowerRemainderIsNegativeZero(GateRef gate);
66     void LowerFloat64CheckRightIsZero(GateRef gate);
67     void LowerValueCheckNegOverflow(GateRef gate);
68     void LowerOverflowCheck(GateRef gate);
69     void LowerInt32UnsignedUpperBoundCheck(GateRef gate);
70     void LowerInt32DivWithCheck(GateRef gate);
71     void LowerLexVarIsHoleCheck(GateRef gate);
72     void LowerIsUndefinedOrHoleCheck(GateRef gate);
73     void LowerIsNotUndefinedOrHoleCheck(GateRef gate);
74     void LowerIsEcmaObjectCheck(GateRef gate);
75     void LowerIsDataViewCheck(GateRef gate);
76     void LowerStoreMemory(GateRef gate);
77     void LowerCheckNullAndConvert(GateRef gate, GateRef frameState);
78     void LowerUndefinedAndConvert(GateRef gate, GateRef frameState);
79     void LowerMigrateFromRawValueToHeapValues(GateRef gate);
80     void LowerMigrateFromHeapValueToRawValue(GateRef gate);
81     void LowerMigrateFromHoleIntToHoleNumber(GateRef gate);
82     void LowerMigrateFromHoleNumberToHoleInt(GateRef gate);
83     void LowerHeapObjectIsEcmaObject(GateRef gate);
84     void LowerIsCallableCheck(GateRef gate);
85     void LowerCheckFloat64AndConvert(GateRef gate, GateRef frameState, Label *exit);
86 
87     GateRef ConvertSpecialHoleIntToTagged(GateRef gate, Label* exit);
88     GateRef ConvertSpecialHoleDoubleToTagged(GateRef gate, Label* exit);
89     GateRef ConvertBoolToTaggedBoolean(GateRef gate);
90     GateRef ConvertInt32ToFloat64(GateRef gate);
91     GateRef ConvertUInt32ToFloat64(GateRef gate);
92     GateRef ConvertInt32ToTaggedInt(GateRef gate);
93     GateRef ConvertUInt32ToTaggedNumber(GateRef gate, Label *exit);
94     GateRef ConvertFloat64ToBool(GateRef gate);
95     GateRef ConvertFloat64ToInt32(GateRef gate, Label *exit);
96     GateRef ConvertFloat64ToTaggedDouble(GateRef gate);
97     GateRef ConvertTaggedIntToInt32(GateRef gate);
98     GateRef ConvertTaggedIntToFloat64(GateRef gate);
99     GateRef ConvertTaggedDoubleToInt32(GateRef gate, Label *exit);
100     GateRef ConvertTaggedDoubleToFloat64(GateRef gate);
101     GateRef ConvertTaggedNumberToBool(GateRef gate, Label *exit);
102     GateRef ConvertTaggedNumberToInt32(GateRef gate, Label *exit);
103     GateRef ConvertTaggedNumberToFloat64(GateRef gate, Label *exit);
104     GateRef ConvertTaggedBooleanToBool(GateRef gate);
105     void HeapAllocateInSOld(GateRef gate);
106     void InitializeWithSpeicalValue(Label *exit, GateRef object, GateRef glue, GateRef value,
107                                     GateRef start, GateRef end);
108     CompilationEnv *env_;
109     Circuit *circuit_;
110     GateAccessor acc_;
111     CircuitBuilder builder_;
112     GateRef glue_ {Circuit::NullGate()};
113 };
114 }  // panda::ecmascript::kungfu
115 #endif  // ECMASCRIPT_COMPILER_MCR_LOWERING_H
116