• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef ECMASCRIPT_COMPILER_TYPED_NATIVE_INLINE_LOWERING_H
17 #define ECMASCRIPT_COMPILER_TYPED_NATIVE_INLINE_LOWERING_H
18 
19 #include <cstdint>
20 #include "ecmascript/compiler/circuit.h"
21 #include "ecmascript/compiler/combined_pass_visitor.h"
22 #include "ecmascript/compiler/pass_manager.h"
23 #include "ecmascript/compiler/share_gate_meta_data.h"
24 #include "ecmascript/compiler/variable_type.h"
25 namespace panda::ecmascript::kungfu {
26 class TypedNativeInlineLowering : public PassVisitor {
27 public:
TypedNativeInlineLowering(Circuit * circuit,RPOVisitor * visitor,PassContext * ctx,CompilationConfig * cmpCfg,Chunk * chunk)28     TypedNativeInlineLowering(Circuit* circuit,
29                               RPOVisitor* visitor,
30                               PassContext *ctx,
31                               CompilationConfig* cmpCfg,
32                               Chunk* chunk)
33         : PassVisitor(circuit, chunk, visitor),
34           circuit_(circuit),
35           acc_(circuit),
36           builder_(circuit, cmpCfg),
37           compilationEnv_(ctx->GetCompilationEnv()),
38           isLiteCG_(ctx->GetCompilationEnv()->GetJSOptions().IsCompilerEnableLiteCG()),
39           glue_(acc_.GetGlueFromArgList()) {}
40     ~TypedNativeInlineLowering() = default;
41     GateRef VisitGate(GateRef gate) override;
42 private:
43     enum class DataViewProtoFunc : uint8_t { GET = 0, SET = 1 };
44     enum class ArrayFindVariant : uint8_t { FIND = 0, FINDINDEX = 1 };
45 
46     enum ElmentSize : uint32_t { BITS_8 = 1, BITS_16 = 2, BITS_32 = 4, BITS_64 = 8 };
47 
48     void ReplaceGateWithPendingException(GateRef gate, GateRef glue, GateRef state, GateRef depend, GateRef value);
49     void LowerGeneralUnaryMath(GateRef gate, RuntimeStubCSigns::ID stubId);
50     void LowerMathAtan2(GateRef gate);
51     void LowerTrunc(GateRef gate);
52     template <bool IS_CEIL>
53     void LowerMathCeilFloor(GateRef gate);
54     template <bool IS_CEIL>
55     void LowerMathCeilFloorWithIntrinsic(GateRef gate);
56     template <bool IS_CEIL>
57     void LowerMathCeilFloorWithRuntimeCall(GateRef gate);
58     void LowerMathPow(GateRef gate);
59     void LowerMathExp(GateRef gate);
60     void LowerMathSignInt(GateRef gate);
61     void LowerMathSignTagged(GateRef gate);
62     void LowerClz32Float64(GateRef gate);
63     void LowerClz32Int32(GateRef gate);
64     void LowerMathSqrt(GateRef gate);
65     void LowerNewNumber(GateRef gate);
66     template <bool IS_UNSIGNED>
67     void LowerBigIntAsIntN(GateRef gate);
68     GateRef BuildRounding(GateRef gate, GateRef value, OpCode op);
69     void LowerTaggedRounding(GateRef gate);
70     void LowerDoubleRounding(GateRef gate);
71     void LowerArrayBufferIsView(GateRef gate);
72     void LowerDataViewProtoFunc(GateRef gate, DataViewProtoFunc proto);
73     GateRef BuildDoubleIsFinite(GateRef value);
74     void LowerNumberIsFinite(GateRef gate);
75     GateRef BuildTaggedIsInteger(GateRef gate, GateRef value, bool safe);
76     void LowerNumberIsInteger(GateRef gate, OpCode op);
77     void LowerNumberIsNaN(GateRef gate);
78     void LowerNumberParseFloat(GateRef gate);
79     void LowerNumberParseInt(GateRef gate);
80     void LowerDateGetTime(GateRef gate);
81     void LowerBigIntConstructor(GateRef gate);
82     GateRef BuildTaggedPointerOverflowInt32(GateRef value);
83     void LowerStringCharCodeAt(GateRef gate);
84     void LowerStringSubstring(GateRef gate);
85     void LowerStringSubStr(GateRef gate);
86     void LowerStringSlice(GateRef gate);
87     template <bool IS_SIGNED>
88     void LowerBigIntConstructorInt32(GateRef gate);
89     GateRef BuiltinIdToSize(GateRef ID);
90     GateRef GetValueFromBuffer(GateRef bufferIndex, GateRef dataPointer, GateRef isLittleEndian, GateRef builtinId);
91     GateRef SetValueInBuffer(GateRef bufferIndex,
92                              GateRef value,
93                              GateRef dataPointer,
94                              GateRef isLittleEndian,
95                              GateRef builtinId,
96                              GateRef glue);
97 
98     GateRef BuildIntAbs(GateRef value);
99     GateRef BuildDoubleAbs(GateRef value);
100     GateRef BuildTNumberAbs(GateRef param);
101     void LowerAbs(GateRef gate);
102     void LowerIntAbs(GateRef gate);
103     void LowerDoubleAbs(GateRef gate);
104 
105     template<bool IS_MAX>
106     GateRef BuildIntMinMax(GateRef int1, GateRef int2, GateRef in1, GateRef in2);
107     template<bool IS_MAX>
108     GateRef BuildIntMinMax(GateRef in1, GateRef in2);
109     template<bool IS_MAX>
110     GateRef BuildDoubleMinMax(GateRef double1, GateRef double2, GateRef in1, GateRef in2);
111     template<bool IS_MAX>
112     GateRef BuildDoubleMinMax(GateRef in1, GateRef in2);
113     template<bool IS_MAX>
114     void LowerTNumberMinMax(GateRef gate);
115     template<bool IS_MAX>
116     void LowerMathMinMaxWithIntrinsic(GateRef gate);
117     template<bool IS_MAX>
118     void LowerMinMax(GateRef gate);
119     template<bool IS_MAX>
120     void LowerIntMinMax(GateRef gate);
121     template<bool IS_MAX>
122     void LowerDoubleMinMax(GateRef gate);
123     GateRef NumberToInt32(GateRef gate);
124     void LowerMathImul(GateRef gate);
125     void LowerGlobalIsFinite(GateRef gate);
126     void LowerGlobalIsNan(GateRef gate);
127     void LowerGeneralWithoutArgs(GateRef gate, RuntimeStubCSigns::ID stubId);
128     GateRef AllocateArrayIterator(GateRef glue, GateRef self, GateRef iteratorHClass, IterationKind iterationKind);
129     void LowerTypedArrayIterator(GateRef gate, CommonStubCSigns::ID index, IterationKind iterationKind);
130 
131     GateRef LowerGlobalDoubleIsFinite(GateRef value);
132     GateRef LowerGlobalTNumberIsFinite(GateRef value);
133     GateRef LowerGlobalTNumberIsNan(GateRef value);
134 
135     void LowerObjectIs(GateRef gate);
136     void LowerObjectGetPrototypeOf(GateRef gate);
137     void LowerObjectCreate(GateRef gate);
138     void LowerObjectIsPrototypeOf(GateRef gate);
139     void LowerObjectHasOwnProperty(GateRef gate);
140     void LowerReflectGetPrototypeOf(GateRef gate);
141     void LowerReflectGet(GateRef gate);
142     void LowerReflectHas(GateRef gate);
143     void LowerReflectConstruct(GateRef gate);
144     void LowerReflectApply(GateRef gate);
145     void LowerFunctionPrototypeApply(GateRef gate);
146     void LowerFunctionPrototypeBind(GateRef gate);
147     void LowerFunctionPrototypeCall(GateRef gate);
148     void LowerArraySort(GateRef gate);
149 
150     void LowerToCommonStub(GateRef gate, CommonStubCSigns::ID id);
151 
152     GateRef FindFrameState(GateRef gate);
153     void LowerArrayIncludesIndexOf(GateRef gate);
154     void LowerArrayIteratorBuiltin(GateRef gate);
155     IterationKind GetArrayIterKindFromBuilin(BuiltinsStubCSigns::ID callID);
156     void LowerArrayForEach(GateRef gate);
157     void LowerArrayFindOrFindIndex(GateRef gate);
158     void LowerArrayFilter(GateRef gate);
159     void LowerArrayMap(GateRef gate);
160     void LowerArraySome(GateRef gate);
161     void LowerArrayEvery(GateRef gate);
162     void LowerArrayPop(GateRef gate);
163     void LowerArrayPush(GateRef gate);
164     void LowerArraySlice(GateRef gate);
165     void CheckAndCalcuSliceIndex(GateRef length,
166                                  GateRef startHandler,
167                                  GateRef endHandler,
168                                  Label* exit,
169                                  Label* checkIndexDone,
170                                  Variable* res,
171                                  Variable* start,
172                                  Variable* end);
173     GateRef CheckAndConvertToUInt(GateRef glue, GateRef msg, Label* notIntegerStr, Label* nonZeroLength);
174 
175 private:
176     Circuit* circuit_ {nullptr};
177     GateAccessor acc_;
178     CircuitBuilder builder_;
179     const CompilationEnv *compilationEnv_ {nullptr};
180     bool isLiteCG_ {false};
181     GateRef glue_ {Circuit::NullGate()};
182 };
183 }
184 #endif  // ECMASCRIPT_COMPILER_TYPED_HCR_LOWERING_H
185