• 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_BUILTINS_CONTAINERS_DEQUE_STUB_BUILDER_H
17 #define ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_DEQUE_STUB_BUILDER_H
18 #include "ecmascript/compiler/stub_builder-inl.h"
19 #include "ecmascript/js_api/js_api_deque.h"
20 
21 namespace panda::ecmascript::kungfu {
22 class ContainersDequeStubBuilder : public StubBuilder {
23 public:
ContainersDequeStubBuilder(StubBuilder * parent)24     explicit ContainersDequeStubBuilder(StubBuilder *parent)
25         : StubBuilder(parent) {}
26     ~ContainersDequeStubBuilder() override = default;
27     NO_MOVE_SEMANTIC(ContainersDequeStubBuilder);
28     NO_COPY_SEMANTIC(ContainersDequeStubBuilder);
GenerateCircuit()29     void GenerateCircuit() override {}
30 
GetSize(GateRef obj)31     GateRef GetSize(GateRef obj)
32     {
33         GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
34         GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
35         GateRef capacity = Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
36         GateRef first = GetFirst(obj);
37         GateRef last = GetLast(obj);
38         return Int32Mod(Int32Add(Int32Sub(last, first), capacity), capacity);
39     }
40 
Get(GateRef obj,GateRef index)41     GateRef Get(GateRef obj, GateRef index)
42     {
43         GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
44         GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
45         GateRef capacity = Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
46         GateRef first = GetFirst(obj);
47         GateRef curIndex = Int32Mod(Int32Add(first, index), capacity);
48         return GetValueFromTaggedArray(elements, curIndex);
49     }
50 
GetFirst(GateRef obj)51     GateRef GetFirst(GateRef obj)
52     {
53         return Load(VariableType::INT32(), obj, IntPtr(JSAPIDeque::FIRST_OFFSET));
54     }
55 
GetLast(GateRef obj)56     GateRef GetLast(GateRef obj)
57     {
58         return Load(VariableType::INT32(), obj, IntPtr(JSAPIDeque::LAST_OFFSET));
59     }
60 
GetElementsLength(GateRef obj)61     GateRef GetElementsLength(GateRef obj)
62     {
63         GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
64         GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
65         return Load(VariableType::INT32(), elements, IntPtr(TaggedArray::LENGTH_OFFSET));
66     }
67 };
68 }  // namespace panda::ecmascript::kungfu
69 #endif  // ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_DEQUE_STUB_BUILDER_H