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