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_BUILTINS_CONTAINERS_QUEUE_STUB_BUILDER_H
17 #define ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_QUEUE_STUB_BUILDER_H
18 #include "ecmascript/compiler/builtins/builtins_stubs.h"
19 #include "ecmascript/js_api/js_api_queue.h"
20
21 namespace panda::ecmascript::kungfu {
22 class ContainersQueueStubBuilder : public BuiltinsStubBuilder {
23 public:
ContainersQueueStubBuilder(StubBuilder * parent)24 explicit ContainersQueueStubBuilder(StubBuilder *parent)
25 : BuiltinsStubBuilder(parent) {}
26 ~ContainersQueueStubBuilder() override = default;
27 NO_MOVE_SEMANTIC(ContainersQueueStubBuilder);
28 NO_COPY_SEMANTIC(ContainersQueueStubBuilder);
GenerateCircuit()29 void GenerateCircuit() override {}
30
31 #define DECLARE_CONTAINERS_QUEUE_STUB_BUILDER(method, ...) \
32 void method(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit, Label *slowPath);
BUILTINS_WITH_CONTAINERS_QUEUE_STUB_BUILDER(DECLARE_CONTAINERS_QUEUE_STUB_BUILDER)33 BUILTINS_WITH_CONTAINERS_QUEUE_STUB_BUILDER(DECLARE_CONTAINERS_QUEUE_STUB_BUILDER)
34 #undef DECLARE_CONTAINERS_QUEUE_STUB_BUILDER
35
36 GateRef GetArrayLength(GateRef obj)
37 {
38 auto env = GetEnvironment();
39 Label entry(env);
40 env->SubCfgEntry(&entry);
41 Label exit(env);
42 Label endGreatBeging(env);
43 Label endNotGreatBeging(env);
44 DEFVARIABLE(length, VariableType::INT32(), Int32(0));
45 GateRef begin = Load(VariableType::INT32(), obj, IntPtr(JSAPIQueue::FRONT_OFFSET));
46 GateRef end = Load(VariableType::INT32(), obj, IntPtr(JSAPIQueue::TAIL_OFFSET));
47 BRANCH(Int32GreaterThanOrEqual(end, begin), &endGreatBeging, &endNotGreatBeging);
48 Bind(&endGreatBeging);
49 {
50 length = Int32Sub(end, begin);
51 Jump(&exit);
52 }
53 Bind(&endNotGreatBeging);
54 {
55 GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
56 GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
57 GateRef elementsSize = Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
58 length = Int32Add(Int32Sub(end, begin), elementsSize);
59 Jump(&exit);
60 }
61 Bind(&exit);
62 auto ret = *length;
63 env->SubCfgExit();
64 return ret;
65 }
66
Get(GateRef obj,GateRef index)67 GateRef Get(GateRef obj, GateRef index)
68 {
69 GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
70 GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
71 GateRef capacity = Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
72 GateRef front = Load(VariableType::INT32(), obj, IntPtr(JSAPIQueue::FRONT_OFFSET));
73 GateRef curIndex = Int32Mod(Int32Add(front, index), capacity);
74 return GetValueFromTaggedArray(elements, curIndex);
75 }
76
GetNextPosition(GateRef obj,GateRef index)77 GateRef GetNextPosition(GateRef obj, GateRef index)
78 {
79 GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
80 GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
81 GateRef elementsSize = Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
82 return Int32Mod(Int32Add(index, Int32(1)), elementsSize);
83 }
84
GetCurrentFront(GateRef obj)85 GateRef GetCurrentFront(GateRef obj)
86 {
87 return Load(VariableType::INT32(), obj, IntPtr(JSAPIQueue::FRONT_OFFSET));
88 }
89 };
90 } // namespace panda::ecmascript::kungfu
91 #endif // ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_QUEUE_STUB_BUILDER_H
92