• 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     ~TypedNativeInlineLowering() = default;
40     GateRef VisitGate(GateRef gate) override;
41 private:
42     enum class DataViewProtoFunc : uint8_t { GET = 0, SET = 1 };
43     enum class ArrayFindVariant : uint8_t { FIND = 0, FINDINDEX = 1 };
44 
45     enum ElmentSize : uint32_t { BITS_8 = 1, BITS_16 = 2, BITS_32 = 4, BITS_64 = 8 };
46     //TAR:targetKind only Int or Double, ARR:arrayElementsKind only Int or Number
47     enum NumberCompareKind : uint8_t { NONE = 0, TARINT_ARRINT, TARINT_ARRNUM, TARDOU_ARRINT, TARDOU_ARRNUM };
48 
NeedRunNaNLoopCheck(NumberCompareKind kind,BuiltinsStubCSigns::ID callID)49     inline bool NeedRunNaNLoopCheck(NumberCompareKind kind, BuiltinsStubCSigns::ID callID)
50     {
51         return ((kind == TARDOU_ARRNUM || kind == NONE) && callID == BuiltinsStubCSigns::ID::ArrayIncludes);
52     }
53 
54     void LowerGeneralUnaryMath(GateRef gate, RuntimeStubCSigns::ID stubId);
55     void LowerMathAtan2(GateRef gate);
56     void LowerTrunc(GateRef gate);
57     template <bool IS_CEIL>
58     void LowerMathCeilFloor(GateRef gate);
59     template <bool IS_CEIL>
60     void LowerMathCeilFloorWithIntrinsic(GateRef gate);
61     template <bool IS_CEIL>
62     void LowerMathCeilFloorWithRuntimeCall(GateRef gate);
63     void LowerMathPow(GateRef gate);
64     void LowerMathExp(GateRef gate);
65     void LowerMathSignInt(GateRef gate);
66     void LowerMathSignTagged(GateRef gate);
67     void LowerClz32Float64(GateRef gate);
68     void LowerClz32Int32(GateRef gate);
69     void LowerMathSqrt(GateRef gate);
70     void LowerNewNumber(GateRef gate);
71     template <bool IS_UNSIGNED>
72     void LowerBigIntAsIntN(GateRef gate);
73     GateRef BuildRounding(GateRef gate, GateRef value, OpCode op);
74     void LowerTaggedRounding(GateRef gate);
75     void LowerDoubleRounding(GateRef gate);
76     void LowerArrayBufferIsView(GateRef gate);
77     void LowerDataViewProtoFunc(GateRef gate, DataViewProtoFunc proto);
78     GateRef BuildDoubleIsFinite(GateRef value);
79     void LowerNumberIsFinite(GateRef gate);
80     GateRef BuildTaggedIsInteger(GateRef gate, GateRef value, bool safe);
81     void LowerNumberIsInteger(GateRef gate, OpCode op);
82     void LowerNumberIsNaN(GateRef gate);
83     void LowerNumberParseFloat(GateRef gate);
84     void LowerNumberParseInt(GateRef gate);
85     void LowerDateGetTime(GateRef gate);
86     void LowerBigIntConstructor(GateRef gate);
87     GateRef BuildTaggedPointerOverflowInt32(GateRef value);
88     void LowerStringSubstring(GateRef gate);
89     void LowerStringSubStr(GateRef gate);
90     void LowerStringSlice(GateRef gate);
91     template <bool IS_SIGNED>
92     void LowerBigIntConstructorInt32(GateRef gate);
93     GateRef BuiltinIdToSize(GateRef ID);
94     GateRef GetValueFromBuffer(GateRef bufferIndex, GateRef dataPointer, GateRef isLittleEndian, GateRef builtinId);
95     GateRef SetValueInBuffer(GateRef bufferIndex,
96                              GateRef value,
97                              GateRef dataPointer,
98                              GateRef isLittleEndian,
99                              GateRef builtinId,
100                              GateRef glue);
101 
102     GateRef BuildIntAbs(GateRef value);
103     GateRef BuildDoubleAbs(GateRef value);
104     GateRef BuildTNumberAbs(GateRef param);
105     void LowerAbs(GateRef gate);
106     void LowerIntAbs(GateRef gate);
107     void LowerDoubleAbs(GateRef gate);
108 
109     template<bool IS_MAX>
110     GateRef BuildIntMinMax(GateRef int1, GateRef int2, GateRef in1, GateRef in2);
111     template<bool IS_MAX>
112     GateRef BuildIntMinMax(GateRef in1, GateRef in2);
113     template<bool IS_MAX>
114     GateRef BuildDoubleMinMax(GateRef double1, GateRef double2, GateRef in1, GateRef in2);
115     template<bool IS_MAX>
116     GateRef BuildDoubleMinMax(GateRef in1, GateRef in2);
117     template<bool IS_MAX>
118     void LowerTNumberMinMax(GateRef gate);
119     template<bool IS_MAX>
120     void LowerMathMinMaxWithIntrinsic(GateRef gate);
121     template<bool IS_MAX>
122     void LowerMinMax(GateRef gate);
123     template<bool IS_MAX>
124     void LowerIntMinMax(GateRef gate);
125     template<bool IS_MAX>
126     void LowerDoubleMinMax(GateRef gate);
127     GateRef NumberToInt32(GateRef gate);
128     void LowerMathImul(GateRef gate);
129     void LowerGlobalIsFinite(GateRef gate);
130     void LowerGlobalIsNan(GateRef gate);
131     void LowerGeneralWithoutArgs(GateRef gate, RuntimeStubCSigns::ID stubId);
132     GateRef AllocateArrayIterator(GateRef glue, GateRef self, GateRef iteratorHClass, IterationKind iterationKind);
133     void LowerTypedArrayIterator(GateRef gate, CommonStubCSigns::ID index, IterationKind iterationKind);
134 
135     GateRef LowerGlobalDoubleIsFinite(GateRef value);
136     GateRef LowerGlobalTNumberIsFinite(GateRef value);
137     GateRef LowerGlobalTNumberIsNan(GateRef value);
138 
139     void LowerObjectIs(GateRef gate);
140     void LowerObjectGetPrototypeOf(GateRef gate);
141     void LowerObjectCreate(GateRef gate);
142     void LowerObjectIsPrototypeOf(GateRef gate);
143     void LowerObjectHasOwnProperty(GateRef gate);
144     void LowerReflectGetPrototypeOf(GateRef gate);
145     void LowerReflectGet(GateRef gate);
146     void LowerReflectHas(GateRef gate);
147     void LowerReflectConstruct(GateRef gate);
148     void LowerReflectApply(GateRef gate);
149     void LowerFunctionPrototypeApply(GateRef gate);
150     void LowerFunctionPrototypeBind(GateRef gate);
151     void LowerFunctionPrototypeCall(GateRef gate);
152     void LowerArraySort(GateRef gate);
153 
154     void LowerToCommonStub(GateRef gate, CommonStubCSigns::ID id);
155     void LowerToBuiltinStub(GateRef gate, BuiltinsStubCSigns::ID id);
156 
157     GateRef FindFrameState(GateRef gate);
158     void LowerArrayIncludesIndexOf(GateRef gate);
159     GateRef NormalCompareLoop(GateRef elements, GateRef fromIndex, GateRef targetElement, GateRef arrayLength,
160                               BuiltinsStubCSigns::ID callID, GateRef gate);
161     GateRef IncludesUndefinedLoop(GateRef elements, GateRef fromIndex, GateRef arrayLength);
162     GateRef TargetIntCompareLoop(GateRef elements,
163                                  GateRef fromIndex,
164                                  GateRef targetElement,
165                                  GateRef arrayLength,
166                                  NumberCompareKind kind,
167                                  bool arrayHasHole);
168     GateRef TargetIntCompareWithCompareKind(GateRef targetElement,
169                                         GateRef doubleTarget,
170                                         GateRef value,
171                                         NumberCompareKind kind);
172     GateRef TargetNumberCompareWithArrKind(GateRef doubleTarget, GateRef value, NumberCompareKind kind);
173     GateRef TargetNumberCompareLoop(GateRef elements,
174                                     GateRef fromIndex,
175                                     GateRef targetElement,
176                                     GateRef arrayLength,
177                                     BuiltinsStubCSigns::ID callID,
178                                     bool arrayHasHole,
179                                     NumberCompareKind kind);
180     GateRef TargetBigIntCompareLopp(GateRef elements, GateRef fromIndex, GateRef targetElement, GateRef arrayLength);
181     GateRef TargetStringCompareLoop(
182         GateRef elements, GateRef fromIndex, GateRef targetElement, GateRef arrayLength, bool arrayHasHole);
183     GateRef TargetEqualCompareLoop(
184         GateRef elements, GateRef fromIndex, GateRef targetElement, GateRef arrayLength, bool arrayHasHole);
185     void LowerArrayIteratorBuiltin(GateRef gate);
186     IterationKind GetArrayIterKindFromBuilin(BuiltinsStubCSigns::ID callID);
187     void LowerArrayForEach(GateRef gate);
188     void LowerArrayFindOrFindIndex(GateRef gate);
189     void LowerArrayFilter(GateRef gate);
190     void LowerArrayMap(GateRef gate);
191     void LowerArraySome(GateRef gate);
192     void LowerArrayEvery(GateRef gate);
193     void LowerArrayPop(GateRef gate);
194     void LowerArraySlice(GateRef gate);
195     void CheckAndCalcuSliceIndex(GateRef length,
196                                  GateRef startHandler,
197                                  GateRef endHandler,
198                                  Label* exit,
199                                  Label* checkIndexDone,
200                                  Variable* res,
201                                  Variable* start,
202                                  Variable* end);
203 
204 private:
205     Circuit* circuit_ {nullptr};
206     GateAccessor acc_;
207     CircuitBuilder builder_;
208     const CompilationEnv *compilationEnv_ {nullptr};
209     bool isLiteCG_ {false};
210 };
211 }
212 #endif  // ECMASCRIPT_COMPILER_TYPED_HCR_LOWERING_H
213