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 47 void ReplaceGateWithPendingException(GateRef gate, GateRef glue, GateRef state, GateRef depend, GateRef value); 48 void LowerGeneralUnaryMath(GateRef gate, RuntimeStubCSigns::ID stubId); 49 void LowerMathAtan2(GateRef gate); 50 void LowerTrunc(GateRef gate); 51 template <bool IS_CEIL> 52 void LowerMathCeilFloor(GateRef gate); 53 template <bool IS_CEIL> 54 void LowerMathCeilFloorWithIntrinsic(GateRef gate); 55 template <bool IS_CEIL> 56 void LowerMathCeilFloorWithRuntimeCall(GateRef gate); 57 void LowerMathPow(GateRef gate); 58 void LowerMathExp(GateRef gate); 59 void LowerMathSignInt(GateRef gate); 60 void LowerMathSignTagged(GateRef gate); 61 void LowerClz32Float64(GateRef gate); 62 void LowerClz32Int32(GateRef gate); 63 void LowerMathSqrt(GateRef gate); 64 void LowerNewNumber(GateRef gate); 65 template <bool IS_UNSIGNED> 66 void LowerBigIntAsIntN(GateRef gate); 67 GateRef BuildRounding(GateRef gate, GateRef value, OpCode op); 68 void LowerTaggedRounding(GateRef gate); 69 void LowerDoubleRounding(GateRef gate); 70 void LowerArrayBufferIsView(GateRef gate); 71 void LowerDataViewProtoFunc(GateRef gate, DataViewProtoFunc proto); 72 GateRef BuildDoubleIsFinite(GateRef value); 73 void LowerNumberIsFinite(GateRef gate); 74 GateRef BuildTaggedIsInteger(GateRef gate, GateRef value, bool safe); 75 void LowerNumberIsInteger(GateRef gate, OpCode op); 76 void LowerNumberIsNaN(GateRef gate); 77 void LowerNumberParseFloat(GateRef gate); 78 void LowerNumberParseInt(GateRef gate); 79 void LowerDateGetTime(GateRef gate); 80 void LowerBigIntConstructor(GateRef gate); 81 GateRef BuildTaggedPointerOverflowInt32(GateRef value); 82 void LowerStringCharCodeAt(GateRef gate); 83 void LowerStringSubstring(GateRef gate); 84 void LowerStringSubStr(GateRef gate); 85 void LowerStringSlice(GateRef gate); 86 template <bool IS_SIGNED> 87 void LowerBigIntConstructorInt32(GateRef gate); 88 GateRef BuiltinIdToSize(GateRef ID); 89 GateRef GetValueFromBuffer(GateRef bufferIndex, GateRef dataPointer, GateRef isLittleEndian, GateRef builtinId); 90 GateRef SetValueInBuffer(GateRef bufferIndex, 91 GateRef value, 92 GateRef dataPointer, 93 GateRef isLittleEndian, 94 GateRef builtinId, 95 GateRef glue); 96 97 GateRef BuildIntAbs(GateRef value); 98 GateRef BuildDoubleAbs(GateRef value); 99 GateRef BuildTNumberAbs(GateRef param); 100 void LowerAbs(GateRef gate); 101 void LowerIntAbs(GateRef gate); 102 void LowerDoubleAbs(GateRef gate); 103 104 template<bool IS_MAX> 105 GateRef BuildIntMinMax(GateRef int1, GateRef int2, GateRef in1, GateRef in2); 106 template<bool IS_MAX> 107 GateRef BuildIntMinMax(GateRef in1, GateRef in2); 108 template<bool IS_MAX> 109 GateRef BuildDoubleMinMax(GateRef double1, GateRef double2, GateRef in1, GateRef in2); 110 template<bool IS_MAX> 111 GateRef BuildDoubleMinMax(GateRef in1, GateRef in2); 112 template<bool IS_MAX> 113 void LowerTNumberMinMax(GateRef gate); 114 template<bool IS_MAX> 115 void LowerMathMinMaxWithIntrinsic(GateRef gate); 116 template<bool IS_MAX> 117 void LowerMinMax(GateRef gate); 118 template<bool IS_MAX> 119 void LowerIntMinMax(GateRef gate); 120 template<bool IS_MAX> 121 void LowerDoubleMinMax(GateRef gate); 122 GateRef NumberToInt32(GateRef gate); 123 void LowerMathImul(GateRef gate); 124 void LowerGlobalIsFinite(GateRef gate); 125 void LowerGlobalIsNan(GateRef gate); 126 void LowerGeneralWithoutArgs(GateRef gate, RuntimeStubCSigns::ID stubId); 127 GateRef AllocateArrayIterator(GateRef glue, GateRef self, GateRef iteratorHClass, IterationKind iterationKind); 128 void LowerTypedArrayIterator(GateRef gate, CommonStubCSigns::ID index, IterationKind iterationKind); 129 130 GateRef LowerGlobalDoubleIsFinite(GateRef value); 131 GateRef LowerGlobalTNumberIsFinite(GateRef value); 132 GateRef LowerGlobalTNumberIsNan(GateRef value); 133 134 void LowerObjectIs(GateRef gate); 135 void LowerObjectGetPrototypeOf(GateRef gate); 136 void LowerObjectCreate(GateRef gate); 137 void LowerObjectIsPrototypeOf(GateRef gate); 138 void LowerObjectHasOwnProperty(GateRef gate); 139 void LowerReflectGetPrototypeOf(GateRef gate); 140 void LowerReflectGet(GateRef gate); 141 void LowerReflectHas(GateRef gate); 142 void LowerReflectConstruct(GateRef gate); 143 void LowerReflectApply(GateRef gate); 144 void LowerFunctionPrototypeApply(GateRef gate); 145 void LowerFunctionPrototypeBind(GateRef gate); 146 void LowerFunctionPrototypeCall(GateRef gate); 147 void LowerArraySort(GateRef gate); 148 149 void LowerToCommonStub(GateRef gate, CommonStubCSigns::ID id); 150 151 GateRef FindFrameState(GateRef gate); 152 void LowerArrayIncludesIndexOf(GateRef gate); 153 void LowerArrayIteratorBuiltin(GateRef gate); 154 IterationKind GetArrayIterKindFromBuilin(BuiltinsStubCSigns::ID callID); 155 void LowerArrayForEach(GateRef gate); 156 void LowerArrayFindOrFindIndex(GateRef gate); 157 void LowerArrayFilter(GateRef gate); 158 void LowerArrayMap(GateRef gate); 159 void LowerArraySome(GateRef gate); 160 void LowerArrayEvery(GateRef gate); 161 void LowerArrayPop(GateRef gate); 162 void LowerArrayPush(GateRef gate); 163 void LowerArraySlice(GateRef gate); 164 void CheckAndCalcuSliceIndex(GateRef length, 165 GateRef startHandler, 166 GateRef endHandler, 167 Label* exit, 168 Label* checkIndexDone, 169 Variable* res, 170 Variable* start, 171 Variable* end); 172 173 private: 174 Circuit* circuit_ {nullptr}; 175 GateAccessor acc_; 176 CircuitBuilder builder_; 177 const CompilationEnv *compilationEnv_ {nullptr}; 178 bool isLiteCG_ {false}; 179 }; 180 } 181 #endif // ECMASCRIPT_COMPILER_TYPED_HCR_LOWERING_H 182