• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUILTINS_TYPEDARRAY_STUB_BUILDER_H
17 #define ECMASCRIPT_COMPILER_BUILTINS_TYPEDARRAY_STUB_BUILDER_H
18 
19 #include "ecmascript/compiler/builtins/builtins_stubs.h"
20 #include "ecmascript/compiler/stub_builder-inl.h"
21 #include "ecmascript/js_arraybuffer.h"
22 #include "ecmascript/js_typed_array.h"
23 
24 #define BUILTIN_COMPILER_TYPED_ARRAY_TYPES(V)                      \
25     V(INT8,           Int8,             0)                         \
26     V(UINT8,          Uint8,            1)                         \
27     V(UINT8_CLAMPED,  Uint8Clamped,     2)                         \
28     V(INT16,          Int16,            3)                         \
29     V(UINT16,         Uint16,           4)                         \
30     V(INT32,          Int32,            5)                         \
31     V(UINT32,         Uint32,           6)                         \
32     V(FLOAT32,        Float32,          7)                         \
33     V(FLOAT64,        Float64,          8)                         \
34     V(BIGINT64,       BigInt64,         9)                         \
35     V(BIGUINT64,      BigUint64,        10)
36 
37 namespace panda::ecmascript::kungfu {
38 class BuiltinsTypedArrayStubBuilder : public BuiltinsStubBuilder {
39 public:
BuiltinsTypedArrayStubBuilder(StubBuilder * parent,GateRef globalEnv)40     explicit BuiltinsTypedArrayStubBuilder(StubBuilder *parent, GateRef globalEnv)
41         : BuiltinsStubBuilder(parent, globalEnv) {}
42     ~BuiltinsTypedArrayStubBuilder() override = default;
43     NO_MOVE_SEMANTIC(BuiltinsTypedArrayStubBuilder);
44     NO_COPY_SEMANTIC(BuiltinsTypedArrayStubBuilder);
GenerateCircuit()45     void GenerateCircuit() override {}
46     GateRef FastGetPropertyByIndex(GateRef glue, GateRef array, GateRef index, GateRef jsType);
47     GateRef FastCopyElementToArray(GateRef glue, GateRef typedArray, GateRef array);
48     GateRef LoadTypedArrayElement(GateRef glue, GateRef array, GateRef key, GateRef jsType);
49     GateRef StoreTypedArrayElement(GateRef glue, GateRef array, GateRef index, GateRef value, GateRef jsType);
50     GateRef CheckTypedArrayIndexInRange(GateRef array, GateRef index);
51     GateRef GetValueFromBuffer(GateRef glue, GateRef buffer, GateRef index, GateRef offset, GateRef jsType);
52     GateRef GetDataPointFromBuffer(GateRef glue, GateRef arrBuf);
53     GateRef CalculatePositionWithLength(GateRef position, GateRef length);
54     void DoSort(GateRef glue, GateRef receiver, Variable *result, Label *exit, Label *slowPath);
55     void FastSetPropertyByIndex(GateRef glue, GateRef value, GateRef array, GateRef index, GateRef jsType);
56     void SetValueToBuffer(GateRef glue, GateRef value, GateRef buffer, GateRef index,
57                           GateRef offset, GateRef jsType, Label *slowPath);
58     void GenTypedArrayConstructor(GateRef glue, GateRef nativeCode, GateRef func,
59         GateRef newTarget, GateRef thisValue, GateRef numArgs, GateRef constructorName, const DataViewType arrayType);
60     GateRef AllocateTypedArray(GateRef glue, GateRef constructorName, GateRef func, GateRef newTarget,
61                                GateRef length, GateRef lengthTagged, const DataViewType arrayType);
62     GateRef AllocateTypedArray(GateRef glue, GateRef constructorName,
63                                GateRef func, GateRef newTarget, const DataViewType arrayType);
64     GateRef AllocateTypedArrayBuffer(GateRef glue, GateRef typedArray, GateRef length, GateRef lengthTagged,
65                                      const DataViewType arrayType);
66     void CreateFromArrayBuffer(Variable *result, GateRef glue, GateRef numArgs,
67                                Label *exit, Label *slowPath, const DataViewType arrayType);
68     void SetArrayBufferProperties(GateRef glue, GateRef typedArray,
69                                   GateRef newByteLength, GateRef offset, GateRef arrayLength);
70     GateRef GetOnHeapHclassFromType(GateRef glue, GateRef objHclass, const DataViewType arrayType);
71     void FastCopyElementFromArray(Variable *result, GateRef glue, GateRef array,
72         Label *exit, Label *slowPath, const DataViewType arrayType);
73     void FastCopyFromArrayToTypedArray(GateRef glue, GateRef array, Variable *result,
74         GateRef targetOffset, GateRef srcLength, Label *check, Label *slowPath,
75         const DataViewType arrayType, bool typedArrayFromCtor);
76     void CopyElementsToArrayBuffer(GateRef glue, GateRef srcLength, GateRef array,
77         GateRef buffer, GateRef targetByteIndex, const DataViewType arrayType, bool getWithKind);
78     void FastSetValueInBuffer(GateRef glue, GateRef buffer,
79         GateRef byteIndex, GateRef val, const DataViewType arrayType);
80     void SetValueInBufferForByte(GateRef glue, GateRef val, GateRef pointer, GateRef byteIndex);
81     template<typename T>
82     void SetValueInBufferForInteger(GateRef glue, GateRef val, GateRef pointer, GateRef byteIndex);
83     template<typename T>
84     void SetValueInBufferForFloat(GateRef glue, GateRef val, GateRef pointer, GateRef byteIndex);
85 
86 #define DECLARE_BUILTINS_TYPEDARRAY_STUB_BUILDER(method, ...)           \
87     void method(GateRef glue, GateRef numArgs, GateRef end, Variable *result, Label *exit, Label *slowPath);
88 BUILTINS_WITH_TYPEDARRAY_STUB_BUILDER(DECLARE_BUILTINS_TYPEDARRAY_STUB_BUILDER)
89 #undef DECLARE_BUILTINS_TYPEDARRAY_STUB_BUILDER
90 
91     void GetByteLength(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit,
92                        Label *slowPath);
93     void GetByteOffset(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit,
94                        Label *slowPath);
95     void Of(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit, Label *slowPath);
96 
GetViewedArrayBuffer(GateRef glue,GateRef array)97     GateRef GetViewedArrayBuffer(GateRef glue, GateRef array)
98     {
99         GateRef offset = IntPtr(JSTypedArray::VIEWED_ARRAY_BUFFER_OFFSET);
100         return Load(VariableType::JS_ANY(), glue, array, offset);
101     }
102 
GetArrayLength(GateRef array)103     GateRef GetArrayLength(GateRef array)
104     {
105         GateRef offset = IntPtr(JSTypedArray::ARRAY_LENGTH_OFFSET);
106         return LoadPrimitive(VariableType::INT32(), array, offset);
107     }
108 
GetByteOffset(GateRef array)109     GateRef GetByteOffset(GateRef array)
110     {
111         GateRef offset = IntPtr(JSTypedArray::BYTE_OFFSET_OFFSET);
112         return LoadPrimitive(VariableType::INT32(), array, offset);
113     }
114 
GetArrayBufferByteLength(GateRef buffer)115     GateRef GetArrayBufferByteLength(GateRef buffer)
116     {
117         GateRef offset = IntPtr(JSArrayBuffer::BYTE_LENGTH_OFFSET);
118         return LoadPrimitive(VariableType::INT32(), buffer, offset);
119     }
120 
GetExternalPointer(GateRef buffer)121     GateRef GetExternalPointer(GateRef buffer)
122     {
123         GateRef offset = IntPtr(JSNativePointer::POINTER_OFFSET);
124         return LoadPrimitive(VariableType::NATIVE_POINTER(), buffer, offset);
125     }
126 private:
ChangeByteArrayTaggedPointerToInt64(GateRef x)127     GateRef ChangeByteArrayTaggedPointerToInt64(GateRef x)
128     {
129         return GetEnvironment()->GetBuilder()->ChangeTaggedPointerToInt64(x);
130     }
131 private:
132     void BuildArrayIterator(GateRef glue, GateRef thisValue, GateRef numArgs,
133         Variable *result, Label *exit, Label *slowPath, IterationKind iteratorKind);
134 };
135 }  // namespace panda::ecmascript::kungfu
136 #endif  // ECMASCRIPT_COMPILER_BUILTINS_TYPEDARRAY_STUB_BUILDER_H