1/*
2 * Copyright (c) 2023-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_ARRAY_STUB_BUILDER_H
17#define ECMASCRIPT_COMPILER_BUILTINS_ARRAY_STUB_BUILDER_H
18#include "ecmascript/compiler/circuit_builder.h"
19#include "ecmascript/compiler/gate.h"
20#include "ecmascript/compiler/share_gate_meta_data.h"
21#include "ecmascript/compiler/stub_builder-inl.h"
22#include "ecmascript/compiler/builtins/builtins_stubs.h"
23
24namespace panda::ecmascript::kungfu {
25class BuiltinsArrayStubBuilder : public BuiltinsStubBuilder {
26public:
27    explicit BuiltinsArrayStubBuilder(StubBuilder *parent)
28        : BuiltinsStubBuilder(parent) {}
29    explicit BuiltinsArrayStubBuilder(Environment* env): BuiltinsStubBuilder(env) {}
30    ~BuiltinsArrayStubBuilder() override = default;
31    NO_MOVE_SEMANTIC(BuiltinsArrayStubBuilder);
32    NO_COPY_SEMANTIC(BuiltinsArrayStubBuilder);
33    void GenerateCircuit() override {}
34
35#define DECLARE_BUILTINS_ARRAY_STUB_BUILDER(method, ...)           \
36    void method(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit, Label *slowPath);
37BUILTINS_WITH_ARRAY_STUB_BUILDER(DECLARE_BUILTINS_ARRAY_STUB_BUILDER)
38#undef DECLARE_BUILTINS_ARRAY_STUB_BUILDER
39
40    void Sort(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *result, Label *exit, Label *slowPath);
41
42    void SortAfterArgs(GateRef glue, GateRef thisValue, GateRef callbackFnHandle,
43                       Variable *result, Label *exit, Label *slowPath, GateRef hir = Circuit::NullGate());
44
45    void GenArrayConstructor(GateRef glue, GateRef nativeCode, GateRef func,
46        GateRef newTarget, GateRef thisValue, GateRef numArgs);
47
48    void FastCreateArrayWithArgv(GateRef glue, Variable *res, GateRef argc, GateRef hclass, Label *exit);
49
50    void ElementsKindHclassCompare(GateRef glue, GateRef arrayCls, Label *matchCls, Label *slowPath);
51
52    GateRef IsConcatSpreadable(GateRef glue, GateRef obj);
53
54    void InitializeArray(GateRef glue, GateRef count, Variable *result, GateRef intialHClass);
55
56    GateRef NewArray(GateRef glue, GateRef count);
57
58    GateRef NewArrayWithHClass(GateRef glue, GateRef hclass);
59
60    GateRef CalculatePositionWithLength(GateRef position, GateRef length);
61
62    GateRef DoSort(GateRef glue, GateRef receiver, GateRef receiverState,
63        Variable *result, Label *exit, Label *slowPath, GateRef hir = Circuit::NullGate());
64
65    void FastReverse(GateRef glue, GateRef thisValue, GateRef len,
66                     ElementsKind kind, Variable *result, Label *exit);
67private:
68    static constexpr uint32_t MAX_LENGTH_ZERO = 0;
69    static constexpr uint32_t MAX_LENGTH_ONE = 1;
70    struct JsArrayRequirements {
71        bool stable = false;
72        bool defaultConstructor = false;
73    };
74    GateRef IsJsArrayWithLengthLimit(GateRef glue, GateRef object,
75        uint32_t maxLength, JsArrayRequirements requirements);
76    GateRef CreateSpliceDeletedArray(GateRef glue, GateRef thisValue, GateRef actualDeleteCount,
77        GateRef intialHClass, GateRef start);
78    void DoReverse(GateRef glue, GateRef fromArray, GateRef toArray, bool holeToUndefined, bool getWithKind,
79                   MemoryAttribute mAttr);
80};
81}  // namespace panda::ecmascript::kungfu
82#endif  // ECMASCRIPT_COMPILER_BUILTINS_ARRAY_STUB_BUILDER_H
83