• 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(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 NewMutantTaggedArray(GateRef glue, GateRef len);
72     GateRef CopyArray(GateRef glue, GateRef elements, GateRef oldLen, GateRef newLen);
73     GateRef ExtendArray(GateRef glue, GateRef elements, GateRef newLen);
74     GateRef NewJSArrayWithSize(GateRef hclass, GateRef size);
75     GateRef NewJSForinIterator(GateRef glue, GateRef receiver, GateRef keys, GateRef cachedHclass);
76     GateRef LoadHClassFromMethod(GateRef glue, GateRef method);
77     GateRef LoadSHClassFromMethod(GateRef glue, GateRef method);
78     GateRef NewJSFunction(GateRef glue, GateRef method,
79                           FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND);
80     GateRef NewJSSendableFunction(GateRef glue, GateRef method,
81                                   FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND);
82     void NewJSFunction(GateRef glue, GateRef jsFunc, GateRef index, GateRef length, GateRef lexEnv,
83                        Variable *result, Label *success, Label *failed, GateRef slotId,
84                        FunctionKind targetKind = FunctionKind::LAST_FUNCTION_KIND);
85     void SetProfileTypeInfoCellToFunction(GateRef jsFunc, GateRef definedFunc, GateRef slotId);
86     GateRef NewJSBoundFunction(GateRef glue, GateRef target, GateRef boundThis, GateRef args);
87     GateRef EnumerateObjectProperties(GateRef glue, GateRef obj);
88     void NewArgumentsList(Variable *result, Label *exit, GateRef sp, GateRef startIdx, GateRef numArgs);
89     void FillArgumentsList(GateRef argumentsList, GateRef sp, GateRef startIdx, GateRef numArgs);
90     GateRef NewArgumentsListObj(GateRef numArgs);
91     void NewArgumentsObj(Variable *result, Label *exit, GateRef argumentsList, GateRef numArgs);
92     void AssignRestArg(Variable *result, Label *exit, GateRef sp, GateRef startIdx, GateRef numArgs,
93                        GateRef intialHClass);
94     void AllocLineStringObject(Variable *result, Label *exit, GateRef length, bool compressed);
95     void AllocSlicedStringObject(Variable *result, Label *exit, GateRef from, GateRef length,
96         FlatStringStubBuilder *flatString);
97     void AllocTreeStringObject(Variable *result, Label *exit, GateRef first, GateRef second,
98         GateRef length, bool compressed);
99     void HeapAlloc(Variable *result, Label *exit, RegionSpaceFlag spaceType, GateRef hclass);
100     void NewJSArrayLiteral(Variable *result, Label *exit, RegionSpaceFlag spaceType, GateRef obj, GateRef hclass,
101                            GateRef trackInfo, bool isEmptyArray);
102     GateRef NewTrackInfo(GateRef glue, GateRef cachedHClass, GateRef cachedFunc, RegionSpaceFlag spaceFlag,
103                          GateRef arraySize);
104     // Note: The size is the num of bytes, it is required to be divisible by 8.
105     void InitializeWithSpeicalValue(Label *exit, GateRef object, GateRef value, GateRef start, GateRef end,
106                                     MemoryAttribute mAttr = MemoryAttribute::Default());
107     GateRef FastNewThisObject(GateRef glue, GateRef ctor);
108     GateRef FastSuperAllocateThis(GateRef glue, GateRef superCtor, GateRef newTarget);
109     GateRef NewThisObjectChecked(GateRef glue, GateRef ctor);
110     GateRef CreateEmptyObject(GateRef glue);
111     GateRef CreateEmptyArray(GateRef glue);
112     GateRef CreateEmptyArray(GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo,
113         GateRef profileTypeInfo, GateRef slotId, ProfileOperation callback);
114     GateRef CreateArrayWithBuffer(GateRef glue, GateRef index, GateRef jsFunc, TraceIdInfo traceIdInfo,
115                                   GateRef profileTypeInfo, GateRef slotId, ProfileOperation callback);
116     void NewTaggedArrayChecked(Variable *result, GateRef len, Label *exit);
117     void NewMutantTaggedArrayChecked(Variable *result, GateRef len, Label *exit);
118     template <typename IteratorType, typename CollectionType>
119     void CreateJSCollectionIterator(Variable *result, Label *exit, GateRef set, GateRef kind);
120     void CreateJSTypedArrayIterator(Variable *result, Label *exit, GateRef set, GateRef kind);
121     GateRef NewTaggedSubArray(GateRef glue, GateRef srcTypedArray, GateRef elementSize, GateRef newLength,
122         GateRef beginIndex, GateRef arrayCls, GateRef buffer);
123     GateRef NewTypedArray(GateRef glue, GateRef srcTypedArray, GateRef srcType, GateRef length);
124     GateRef NewFloat32ArrayObj(GateRef glue, GateRef glueGlobalEnv);
125     GateRef NewFloat32ArrayWithSize(GateRef glue, GateRef size);
126     GateRef NewTypedArrayFromCtor(GateRef glue, GateRef ctor, GateRef length, Label *slowPath);
127     void NewByteArray(Variable *result, Label *exit, GateRef elementSize, GateRef length);
128     GateRef NewProfileTypeInfoCell(GateRef glue, GateRef value);
129     GateRef GetElementSizeFromType(GateRef glue, GateRef type);
130     GateRef GetOnHeapHClassFromType(GateRef glue, GateRef type);
131 private:
132     static constexpr int MAX_TAGGED_ARRAY_LENGTH = 50;
133     static constexpr int LOOP_UNROLL_FACTOR = 2;
134     GateRef LoadTrackInfo(GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo,
135         GateRef profileTypeInfo, GateRef slotId, GateRef slotValue, GateRef arrayLiteral, ProfileOperation callback);
136     GateRef LoadArrayHClassSlowPath(
137         GateRef glue, GateRef jsFunc, TraceIdInfo traceIdInfo, GateRef arrayLiteral, ProfileOperation callback);
138     GateRef CreateEmptyArrayCommon(GateRef glue, GateRef hclass, GateRef trackInfo);
139     void AllocateInYoungPrologue(Variable *result, Label *callRuntime, Label *exit);
140     void AllocateInYoung(Variable *result, Label *exit, GateRef hclass);
141     void AllocateInYoung(Variable *result, Label *error, Label *noError, GateRef hclass);
142     void AllocateInSOldPrologue(Variable *result, Label *callRuntime, Label *exit);
143     void AllocateInSOld(Variable *result, Label *exit, GateRef hclass);
144     void InitializeTaggedArrayWithSpeicalValue(Label *exit,
145         GateRef array, GateRef value, GateRef start, GateRef length);
146     GateRef glue_ {Circuit::NullGate()};
147     GateRef size_ {0};
148 };
149 }  // namespace panda::ecmascript::kungfu
150 #endif  // ECMASCRIPT_COMPILER_NEW_OBJECT_STUB_BUILDER_H
151