1 /* 2 * Copyright (c) 2021 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_HCR_LOWERING_H 17 #define ECMASCRIPT_COMPILER_TYPED_HCR_LOWERING_H 18 19 #include "ecmascript/compiler/argument_accessor.h" 20 #include "ecmascript/compiler/bytecode_circuit_builder.h" 21 #include "ecmascript/compiler/circuit_builder-inl.h" 22 #include "ecmascript/compiler/combined_pass_visitor.h" 23 #include "ecmascript/compiler/type_info_accessors.h" 24 #include "ecmascript/compiler/pass_manager.h" 25 26 namespace panda::ecmascript::kungfu { 27 // TypeHCRLowering Process 28 // SW: state wire, DW: depend wire, VW: value wire 29 // Before Type Lowering: 30 // SW DW VW 31 // | | | 32 // | | | 33 // v v v 34 // +-------------------+ 35 // | (HIR) | SW +--------------+ 36 // --DW| JS_BYTECODE |---------->| IF_EXCEPTION | 37 // | +-------------------+ +--------------+ 38 // | SW VW 39 // | | | 40 // | v | 41 // | +--------------+ | 42 // | | IF_SUCCESS | | 43 // | +--------------+ | 44 // | SW | 45 // | | | 46 // | v v 47 // | +-------------------+ 48 // | | (HIR) | 49 // --->| JS_BYTECODE | 50 // +-------------------+ 51 // 52 // After Type Lowering: 53 // SW 54 // | 55 // v 56 // +-------------------+ 57 // | IF_BRANCH | 58 // | (Type Check) | 59 // +-------------------+ 60 // SW SW 61 // | | 62 // V V 63 // +--------------+ +--------------+ 64 // | IF_TRUE | | IF_FALSE | 65 // +--------------+ +--------------+ 66 // VW DW SW SW DW VW 67 // | | | | | | 68 // | | V V | | 69 // | | +---------------+ +---------------------+ | | 70 // ------->| FAST PATH | | (HIR) |<------- 71 // +---------------+ | JS_BYTECODE | 72 // VW DW SW +---------------------+ 73 // | | | SW VW DW 74 // | | | | | | 75 // | | | v | | 76 // | | | +--------------+ | | 77 // | | | | IF_SUCCESS | | | 78 // | | | +--------------+ | | 79 // | | | SW | | 80 // | | | | | | 81 // | | v v | | 82 // | | +---------------------+ | | 83 // | | | MERGE | | | 84 // | | +---------------------+ | | 85 // | | SW SW SW | | 86 // ----|----|----------|-----|-- | | 87 // ---|----|----------|-----|-|-------|---- 88 // | | | | | | | 89 // v v v | v v v 90 // +-----------------+ | +----------------+ 91 // | DEPEND_SELECTOR | | | VALUE_SELECTOR | 92 // +-----------------+ | +----------------+ 93 // DW | VW 94 // | | | 95 // v v v 96 // +------------------------+ 97 // | (HIR) | 98 // | JS_BYTECODE | 99 // +------------------------+ 100 101 class TypedHCRLowering : public PassVisitor { 102 public: TypedHCRLowering(Circuit * circuit,CompilationEnv * env,RPOVisitor * visitor,CompilationConfig * cmpCfg,Chunk * chunk,bool enableLoweringBuiltin)103 TypedHCRLowering(Circuit* circuit, 104 CompilationEnv *env, 105 RPOVisitor* visitor, 106 CompilationConfig* cmpCfg, 107 Chunk* chunk, 108 bool enableLoweringBuiltin) 109 : PassVisitor(circuit, chunk, visitor), 110 circuit_(circuit), 111 compilationEnv_(env), 112 acc_(circuit), 113 builder_(circuit, cmpCfg), 114 dependEntry_(circuit->GetDependRoot()), 115 enableLoweringBuiltin_(enableLoweringBuiltin) 116 { 117 } 118 119 ~TypedHCRLowering() = default; 120 121 GateRef VisitGate(GateRef gate) override; 122 123 private: 124 void Lower(GateRef gate); 125 void LowerType(GateRef gate); 126 void LowerPrimitiveTypeCheck(GateRef gate); 127 void LowerTypeConvert(GateRef gate); 128 void LowerPrimitiveToNumber(GateRef dst, GateRef src, ParamType srcType); 129 void LowerIntCheck(GateRef gate); 130 void LowerDoubleCheck(GateRef gate); 131 void LowerNumberCheck(GateRef gate); 132 void LowerBooleanCheck(GateRef gate); 133 void LowerIndexCheck(GateRef gate); 134 void LowerObjectTypeCheck(GateRef gate); 135 void LowerSimpleHClassCheck(GateRef gate); 136 GateRef BuildCompareHClass(GateRef gate, GateRef frameState); 137 void LowerStableArrayCheck(GateRef gate); 138 void LowerTypedArrayCheck(GateRef gate); 139 void LowerEcmaStringCheck(GateRef gate); 140 void LowerEcmaMapCheck(GateRef gate); 141 void LowerFlattenTreeStringCheck(GateRef gate, GateRef glue); 142 void LowerLoadTypedArrayLength(GateRef gate); 143 void LowerStringLength(GateRef gate); 144 void LowerMapSize(GateRef gate); 145 void LowerCallPrivateGetter(GateRef gate, GateRef glue); 146 void LowerCallPrivateSetter(GateRef gate, GateRef glue); 147 void LowerLoadProperty(GateRef gate); 148 void LowerCallGetter(GateRef gate, GateRef glue); 149 void LowerStoreProperty(GateRef gate); 150 void LowerCallSetter(GateRef gate, GateRef glue); 151 void LowerLoadArrayLength(GateRef gate); 152 void LowerStoreElement(GateRef gate, GateRef glue); 153 void LowerLoadElement(GateRef gate); 154 void LowerLoadFromTaggedArray(GateRef gate); 155 void LowerStoreToTaggedArray(GateRef gate, GateRef glue); 156 void LowerRangeCheckPredicate(GateRef gate); 157 void LowerBuiltinPrototypeHClassCheck(GateRef gate); 158 void BuiltinInstanceStringTypeCheck(GateRef gate); 159 void LowerLoadBuiltinObject(GateRef gate); 160 void LowerTypedCreateObjWithBuffer(GateRef gate, GateRef glue); 161 void LowerNumberToString(GateRef gate, GateRef glue); 162 163 enum class ArrayState : uint8_t { 164 PACKED = 0, 165 HOLEY, 166 }; 167 void LowerArrayLoadElement(GateRef gate, ArrayState arrayState, TypedLoadOp op); 168 void LowerCowArrayCheck(GateRef gate, GateRef glue); 169 void LowerTypedArrayLoadElement(GateRef gate, BuiltinTypeId id); 170 void LowerStringLoadElement(GateRef gate); 171 GateRef BuildOnHeapTypedArrayLoadElement(GateRef receiver, GateRef offset, VariableType type); 172 GateRef BuildNotOnHeapTypedArrayLoadElement(GateRef receiver, GateRef offset, VariableType type); 173 GateRef BuildTypedArrayLoadElement(GateRef receiver, GateRef offset, VariableType type, Label *isByteArray, 174 Label *isArrayBuffer, Label *exit); 175 void LowerArrayStoreElement(GateRef gate, GateRef glue, TypedStoreOp kind); 176 void LowerTypedArrayStoreElement(GateRef gate, BuiltinTypeId id); 177 void OptStoreElementByOnHeapMode(GateRef gate, GateRef receiver, GateRef offset, GateRef value, Label *isByteArray, 178 Label *isArrayBuffer, Label *exit); 179 void BuildOnHeapTypedArrayStoreElement(GateRef receiver, GateRef offset, GateRef value); 180 void BuildNotOnHeapTypedArrayStoreElement(GateRef receiver, GateRef offset, GateRef value); 181 void BuildTypedArrayStoreElement(GateRef receiver, GateRef offset, GateRef value, Label *isByteArray, 182 Label *isArrayBuffer, Label *exit); 183 void LowerUInt8ClampedArrayStoreElement(GateRef gate); 184 void LowerTypedCallBuitin(GateRef gate); 185 void LowerCallTargetCheck(GateRef gate); 186 void LowerJSCallTargetCheck(GateRef gate); 187 void LowerJSCallTargetFromDefineFuncCheck(GateRef gate); 188 void LowerJSCallTargetTypeCheck(GateRef gate); 189 void LowerJSFastCallTargetTypeCheck(GateRef gate); 190 void LowerJSCallThisTargetTypeCheck(GateRef gate); 191 void LowerJSFastCallThisTargetTypeCheck(GateRef gate); 192 void LowerJSNoGCCallThisTargetTypeCheck(GateRef gate); 193 void LowerJSNoGCFastCallThisTargetTypeCheck(GateRef gate); 194 void LowerJSNewObjRangeCallTargetCheck(GateRef gate); 195 void LowerTypedNewAllocateThis(GateRef gate, GateRef glue); 196 void LowerTypedSuperAllocateThis(GateRef gate, GateRef glue); 197 void LowerGetSuperConstructor(GateRef gate); 198 void LowerJSInlineTargetTypeCheck(GateRef gate); 199 void SetDeoptTypeInfo(JSType jstype, DeoptType &type, size_t &typedArrayRootHclassIndex, 200 size_t &typedArrayRootHclassOnHeapIndex); 201 void LowerLookupHolder(GateRef gate); 202 void LowerLoadGetter(GateRef gate); 203 void LowerLoadSetter(GateRef gate); 204 void LowerPrototypeCheck(GateRef gate); 205 void LowerStringEqual(GateRef gate, GateRef glue); 206 void LowerStringAdd(GateRef gate, GateRef glue); 207 void LowerTypeOfCheck(GateRef gate); 208 void LowerTypeOf(GateRef gate, GateRef glue); 209 void LowerArrayConstructorCheck(GateRef gate, GateRef glue); 210 void NewArrayConstructorWithNoArgs(GateRef gate, GateRef glue); 211 void LowerArrayConstructor(GateRef gate, GateRef glue); 212 void LowerFloat32ArrayConstructorCheck(GateRef gate, GateRef glue); 213 void NewFloat32ArrayConstructorWithNoArgs(GateRef gate, GateRef glue); 214 void ConvertFloat32ArrayConstructorLength(GateRef len, Variable *arrayLength, 215 Label *arrayCreate, Label *slowPath); 216 void LowerFloat32ArrayConstructor(GateRef gate, GateRef glue); 217 void LowerObjectConstructorCheck(GateRef gate, GateRef glue); 218 void LowerObjectConstructor(GateRef gate, GateRef glue); 219 void LowerBooleanConstructorCheck(GateRef gate, GateRef glue); 220 void LowerBooleanConstructor(GateRef gate, GateRef glue); 221 GateRef NewJSPrimitiveRef(PrimitiveType type, GateRef glue, GateRef value); 222 void ReplaceGateWithPendingException(GateRef glue, GateRef gate, GateRef state, GateRef depend, GateRef value); 223 void LowerOrdinaryHasInstance(GateRef gate, GateRef glue); 224 void LowerProtoChangeMarkerCheck(GateRef gate); 225 void LowerMonoCallGetterOnProto(GateRef gate, GateRef glue); 226 void LowerMonoLoadPropertyOnProto(GateRef gate); 227 void LowerMonoStorePropertyLookUpProto(GateRef gate, GateRef glue); 228 void LowerMonoStoreProperty(GateRef gate, GateRef glue); 229 void LowerStringFromSingleCharCode(GateRef gate, GateRef glue); 230 void LowerMigrateArrayWithKind(GateRef gate); 231 void LowerEcmaObjectCheck(GateRef gate); 232 233 GateRef LowerCallRuntime(GateRef glue, GateRef hirGate, int index, const std::vector<GateRef> &args, 234 bool useLabel = false); 235 236 enum AccessorMode { 237 GETTER, 238 SETTER, 239 }; 240 241 GateRef CallAccessor(GateRef glue, GateRef gate, GateRef function, GateRef receiver, AccessorMode mode, 242 GateRef value = Circuit::NullGate()); 243 void BuiltinInstanceHClassCheck(Environment *env, GateRef gate); 244 void BuiltinPrototypeHClassCheck(Environment *env, GateRef gate); 245 void ReplaceHirWithPendingException(GateRef hirGate, GateRef glue, GateRef state, GateRef depend, GateRef value); 246 247 GateRef DoubleToTaggedDoublePtr(GateRef gate); 248 GateRef ChangeInt32ToFloat64(GateRef gate); 249 GateRef TruncDoubleToInt(GateRef gate); 250 GateRef IntToTaggedIntPtr(GateRef x); 251 GateType GetLeftType(GateRef gate); 252 GateType GetRightType(GateRef gate); 253 GateRef GetElementSize(BuiltinTypeId id); 254 VariableType GetVariableType(BuiltinTypeId id); 255 GateRef AllocateLineString(GateRef glue, GateRef length, GateRef canBeCompressed); 256 GateRef AllocateSlicedString(GateRef glue, GateRef flatString, GateRef length, GateRef canBeCompressed); 257 bool IsFirstConcatInStringAdd(GateRef gate) const; 258 bool ConcatIsInStringAdd(GateRef gate) const; 259 GetFrameState(GateRef gate)260 GateRef GetFrameState(GateRef gate) const 261 { 262 return acc_.GetFrameState(gate); 263 } 264 265 VariableType GetVarType(PropertyLookupResult plr); 266 GateRef GetLengthFromSupers(GateRef supers); 267 GateRef GetValueFromSupers(GateRef supers, size_t index); 268 GateRef LoadFromTaggedArray(GateRef array, size_t index); 269 GateRef LoadFromConstPool(GateRef unsharedConstPool, size_t index, size_t valVecType); 270 GateRef GetLengthFromString(GateRef gate); 271 GateRef LoadPropertyFromHolder(GateRef holder, PropertyLookupResult plr); 272 void StorePropertyOnHolder(GateRef holder, GateRef value, PropertyLookupResult plr, bool needBarrier); 273 274 Circuit *circuit_; 275 CompilationEnv *compilationEnv_ {nullptr}; 276 GateAccessor acc_; 277 CircuitBuilder builder_; 278 GateRef dependEntry_; 279 bool enableLoweringBuiltin_ {false}; 280 }; 281 } // panda::ecmascript::kungfu 282 #endif // ECMASCRIPT_COMPILER_TYPED_HCR_LOWERING_H 283