1 /* 2 * Copyright (c) 2022 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_NEW_OBJECT_STUB_BUILDER_H 17 #define ECMASCRIPT_COMPILER_NEW_OBJECT_STUB_BUILDER_H 18 19 #include "ecmascript/compiler/builtins/builtins_string_stub_builder.h" 20 #include "ecmascript/compiler/profiler_operation.h" 21 #include "ecmascript/compiler/stub_builder.h" 22 23 namespace panda::ecmascript::kungfu { 24 25 struct TraceIdInfo { 26 GateRef pc = 0; 27 GateRef traceId = 0; 28 bool isPc = true; 29 }; 30 31 class NewObjectStubBuilder : public StubBuilder { 32 public: NewObjectStubBuilder(StubBuilder * parent)33 explicit NewObjectStubBuilder(StubBuilder *parent) 34 : StubBuilder(parent) {} NewObjectStubBuilder(Environment * env)35 explicit NewObjectStubBuilder(Environment *env) 36 : StubBuilder(env) {} 37 ~NewObjectStubBuilder() override = default; 38 NO_MOVE_SEMANTIC(NewObjectStubBuilder); 39 NO_COPY_SEMANTIC(NewObjectStubBuilder); GenerateCircuit()40 void GenerateCircuit() override {} 41 SetParameters(GateRef glue,GateRef size)42 void SetParameters(GateRef glue, GateRef size) 43 { 44 glue_ = glue; 45 size_ = size; 46 } 47 SetGlue(GateRef glue)48 void SetGlue(GateRef glue) 49 { 50 glue_ = glue; 51 } 52 53 void NewLexicalEnv(Variable *result, Label *exit, GateRef numSlots, GateRef parent); 54 void NewJSObject(Variable *result, Label *exit, GateRef hclass, GateRef size); 55 void NewJSObject(Variable *result, Label *exit, GateRef hclass); 56 void NewSObject(Variable *result, Label *exit, GateRef hclass); 57 GateRef NewJSObject(GateRef glue, GateRef hclass); 58 GateRef NewSObject(GateRef glue, GateRef hclass); 59 GateRef NewJSFunctionByHClass(GateRef glue, GateRef method, GateRef hclass, 60 FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND); 61 GateRef NewSFunctionByHClass(GateRef glue, GateRef method, GateRef hclass, 62 FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND); 63 GateRef CloneJSFunction(GateRef glue, GateRef value); 64 GateRef CloneProperties(GateRef glue, GateRef currentEnv, GateRef elements, GateRef obj); 65 GateRef NewAccessorData(GateRef glue); 66 GateRef CloneObjectLiteral(GateRef glue, GateRef literal, GateRef currentEnv); 67 GateRef CreateObjectHavingMethod(GateRef glue, GateRef literal, GateRef currentEnv); 68 GateRef NewJSProxy(GateRef glue, GateRef target, GateRef handler); 69 GateRef NewJSArray(GateRef glue, GateRef hclass); 70 GateRef NewTaggedArray(GateRef glue, GateRef len); 71 GateRef NewTaggedArrayInOld(GateRef glue, GateRef len); 72 GateRef NewMutantTaggedArray(GateRef glue, GateRef len); 73 GateRef NewMutantTaggedArrayInOld(GateRef glue, GateRef len); 74 GateRef ExtendArrayWithOptimizationCheck(GateRef glue, GateRef elements, GateRef newLen); 75 GateRef CopyArray(GateRef glue, GateRef elements, GateRef oldLen, GateRef newLen, 76 RegionSpaceFlag spaceType = RegionSpaceFlag::IN_YOUNG_SPACE); 77 GateRef ExtendArrayCheck(GateRef glue, GateRef elements, GateRef newLen, 78 RegionSpaceFlag spaceType = RegionSpaceFlag::IN_YOUNG_SPACE); 79 void ExtendArray(Variable *result, GateRef glue, GateRef elements, GateRef newLen, Label *exit, 80 RegionSpaceFlag spaceType = RegionSpaceFlag::IN_YOUNG_SPACE, bool isMutantArray = false); 81 void ExtendMutantArray(Variable *result, GateRef glue, GateRef elements, GateRef newLen, Label *exit, 82 RegionSpaceFlag spaceType = RegionSpaceFlag::IN_YOUNG_SPACE); 83 GateRef NewJSArrayWithSize(GateRef hclass, GateRef size); 84 GateRef NewJSArrayWithHClass(GateRef hclass, GateRef length); 85 GateRef NewJSForinIterator(GateRef glue, GateRef receiver, GateRef keys, GateRef cachedHclass); 86 GateRef LoadHClassFromMethod(GateRef glue, GateRef method); 87 GateRef LoadSHClassFromMethod(GateRef glue, GateRef method); 88 GateRef NewJSFunction(GateRef glue, GateRef method, 89 FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND); 90 GateRef NewJSSendableFunction(GateRef glue, GateRef method, 91 FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND); 92 void NewJSFunction(GateRef glue, GateRef jsFunc, GateRef index, GateRef length, GateRef lexEnv, 93 Variable *result, Label *success, Label *failed, GateRef slotId, 94 FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND); 95 void SetProfileTypeInfoCellToFunction(GateRef jsFunc, GateRef definedFunc, GateRef slotId); 96 GateRef NewJSBoundFunction(GateRef glue, GateRef target, GateRef boundThis, GateRef args); 97 GateRef EnumerateObjectProperties(GateRef glue, GateRef obj); 98 void NewArgumentsList(Variable *result, Label *exit, GateRef sp, GateRef startIdx, GateRef numArgs); 99 void FillArgumentsList(GateRef argumentsList, GateRef sp, GateRef startIdx, GateRef numArgs); 100 GateRef NewArgumentsListObj(GateRef numArgs); 101 void NewArgumentsObj(Variable *result, Label *exit, GateRef argumentsList, GateRef numArgs); 102 // Requires result to be array of arguments 103 void AssignRestArg(Variable *result, Label *exit, GateRef sp, GateRef startIdx, GateRef numArgs); 104 void AllocLineStringObject(Variable *result, Label *exit, GateRef length, bool compressed); 105 void AllocSlicedStringObject(Variable *result, Label *exit, GateRef from, GateRef length, 106 FlatStringStubBuilder *flatString); 107 void AllocTreeStringObject(Variable *result, Label *exit, GateRef first, GateRef second, 108 GateRef length, bool compressed); 109 void HeapAlloc(Variable *result, Label *exit, RegionSpaceFlag spaceType, GateRef hclass); 110 void NewJSArrayLiteral(Variable *result, Label *exit, RegionSpaceFlag spaceType, GateRef obj, GateRef hclass, 111 GateRef trackInfo, bool isEmptyArray); 112 GateRef NewTrackInfo(GateRef glue, GateRef cachedHClass, GateRef cachedFunc, RegionSpaceFlag spaceFlag, 113 GateRef arraySize); 114 // Note: The size is the num of bytes, it is required to be divisible by 8. 115 void InitializeWithSpeicalValue(Label *exit, GateRef object, GateRef value, GateRef start, GateRef end, 116 MemoryAttribute mAttr = MemoryAttribute::Default()); 117 GateRef FastNewThisObject(GateRef glue, GateRef ctor); 118 GateRef FastSuperAllocateThis(GateRef glue, GateRef superCtor, GateRef newTarget); 119 GateRef NewThisObjectChecked(GateRef glue, GateRef ctor); 120 GateRef CreateEmptyObject(GateRef glue); 121 GateRef CreateEmptyArray(GateRef glue); 122 GateRef CreateEmptyArray(GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo, 123 GateRef profileTypeInfo, GateRef slotId, ProfileOperation callback); 124 GateRef CreateArrayWithBuffer(GateRef glue, GateRef index, GateRef jsFunc, TraceIdInfo traceIdInfo, 125 GateRef profileTypeInfo, GateRef slotId, ProfileOperation callback); 126 void NewTaggedArrayChecked(Variable *result, GateRef len, Label *exit); 127 void NewMutantTaggedArrayChecked(Variable *result, GateRef len, Label *exit); 128 template <typename IteratorType, typename CollectionType> 129 void CreateJSCollectionIterator(Variable *result, Label *exit, GateRef set, GateRef kind); 130 void CreateJSTypedArrayIterator(Variable *result, Label *exit, GateRef set, GateRef kind); 131 GateRef NewTaggedSubArray(GateRef glue, GateRef srcTypedArray, GateRef elementSize, GateRef newLength, 132 GateRef beginIndex, GateRef arrayCls, GateRef buffer); 133 GateRef NewTypedArray(GateRef glue, GateRef srcTypedArray, GateRef srcType, GateRef length); 134 GateRef NewTypedArraySameType(GateRef glue, GateRef srcTypedArray, GateRef srcType, GateRef length); 135 GateRef NewJSObjectByConstructor(GateRef glue, GateRef constructor, GateRef newTarget); 136 GateRef NewFloat32ArrayObj(GateRef glue, GateRef glueGlobalEnv); 137 GateRef NewFloat32ArrayWithSize(GateRef glue, GateRef size); 138 GateRef NewTypedArrayFromCtor(GateRef glue, GateRef ctor, GateRef length, Label *slowPath); 139 void NewByteArray(Variable *result, Label *exit, GateRef elementSize, GateRef length); 140 GateRef NewProfileTypeInfoCell(GateRef glue, GateRef value); 141 GateRef GetElementSizeFromType(GateRef glue, GateRef type); 142 GateRef GetOnHeapHClassFromType(GateRef glue, GateRef type); 143 GateRef GetNotOnHeapHClassFromType(GateRef glue, GateRef type); 144 GateRef CreateArrayFromList(GateRef glue, GateRef elements, GateRef kind); 145 GateRef CreateListFromArrayLike(GateRef glue, GateRef arrayObj); 146 void CreateJSIteratorResult(GateRef glue, Variable *res, GateRef value, GateRef done, Label *exit); 147 148 private: 149 static constexpr int MAX_TAGGED_ARRAY_LENGTH = 50; 150 static constexpr int MAX_EXTEND_ARRAY_LENGTH = 2048; 151 GateRef LoadTrackInfo(GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo, 152 GateRef profileTypeInfo, GateRef slotId, GateRef slotValue, GateRef arrayLiteral, ProfileOperation callback); 153 GateRef LoadArrayHClassSlowPath( 154 GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo, GateRef arrayLiteral, ProfileOperation callback); 155 GateRef CreateEmptyArrayCommon(GateRef glue, GateRef hclass, GateRef trackInfo); 156 void AllocateInYoungPrologue(Variable *result, Label *callRuntime, Label *exit); 157 void AllocateInYoung(Variable *result, Label *exit, GateRef hclass); 158 void AllocateInYoung(Variable *result, Label *error, Label *noError, GateRef hclass); 159 void AllocateInSOldPrologue(Variable *result, Label *callRuntime, Label *exit); 160 void AllocateInSOld(Variable *result, Label *exit, GateRef hclass); 161 void InitializeTaggedArrayWithSpeicalValue(Label *exit, 162 GateRef array, GateRef value, GateRef start, GateRef length); 163 void InitializeObject(Variable *result); 164 GateRef glue_ {Circuit::NullGate()}; 165 GateRef size_ {0}; 166 }; 167 } // namespace panda::ecmascript::kungfu 168 #endif // ECMASCRIPT_COMPILER_NEW_OBJECT_STUB_BUILDER_H 169