1/*
2 * Copyright (c) 2021-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_RT_CALL_SIGNATURE_H
17#define ECMASCRIPT_COMPILER_RT_CALL_SIGNATURE_H
18
19#include "ecmascript/compiler/call_signature.h"
20#include "ecmascript/stubs/runtime_stub_list.h"
21
22namespace panda::ecmascript::kungfu {
23class RuntimeStubCSigns {
24public:
25    enum ID {
26#define DEF_RUNTIME_STUB_ID(name) ID_##name,
27        RUNTIME_STUB_LIST(DEF_RUNTIME_STUB_ID)
28#undef DEF_RUNTIME_STUB_ID
29        NUM_OF_STUBS
30    };
31
32    enum NoGCStubID {
33#define DEF_RUNTIME_STUB_ID(name) NOGCSTUB_ID_##name,
34        RUNTIME_ASM_STUB_LIST(DEF_RUNTIME_STUB_ID)
35        RUNTIME_STUB_WITHOUT_GC_LIST(DEF_RUNTIME_STUB_ID)
36#undef DEF_RUNTIME_STUB_ID
37        NUM_OF_RTSTUBS_WITHOUT_GC
38    };
39
40    enum AsmStubID {
41#define DEF_RUNTIME_STUB_ID(name) ASM_STUB_ID_##name,
42        RUNTIME_ASM_STUB_LIST(DEF_RUNTIME_STUB_ID)
43#undef DEF_RUNTIME_STUB_ID
44        NUM_OF_ASM_STUBS
45    };
46
47    static void Initialize();
48
49    static bool IsAsmStub(uint32_t index)
50    {
51        return index < AsmStubID::NUM_OF_ASM_STUBS;
52    }
53
54    static void GetASMCSigns(std::vector<const CallSignature*>& callSigns);
55
56    static const CallSignature *Get(size_t index)
57    {
58        ASSERT(index < NUM_OF_RTSTUBS_WITHOUT_GC);
59        return &callSigns_[index];
60    }
61
62    static std::string GetRTName(int index)
63    {
64        ASSERT(index < NUM_OF_STUBS);
65        switch (index) {
66#define DEF_STUB_NAME(name) case ID_##name: { return std::string("RTStub_") + #name; }
67RUNTIME_STUB_LIST(DEF_STUB_NAME)
68#undef DEF_STUB_NAME
69            default:
70                return "unknown";
71        }
72        return "unknown";
73    }
74
75    static bool IsCold(int index)
76    {
77        ASSERT(index < NUM_OF_STUBS);
78        switch (index) {
79            case ID_CheckSafePoint:
80                return true;
81            default:
82                return false;
83        }
84        return false;
85    }
86
87    static const CallSignature* GetOptimizedCallSign()
88    {
89        return &optimizedCallSign_;
90    }
91
92    static const CallSignature* GetOptimizedFastCallSign()
93    {
94        return &optimizedFastCallSign_;
95    }
96
97private:
98    static CallSignature callSigns_[NUM_OF_RTSTUBS_WITHOUT_GC];
99    static CallSignature optimizedCallSign_;
100    static CallSignature optimizedFastCallSign_;
101};
102static_assert(static_cast<int>(kungfu::RuntimeStubCSigns::ID_CallRuntime) ==
103              static_cast<int>(kungfu::RuntimeStubCSigns::ASM_STUB_ID_CallRuntime));
104static_assert(static_cast<int>(kungfu::RuntimeStubCSigns::ID_AsmInterpreterEntry) ==
105              static_cast<int>(kungfu::RuntimeStubCSigns::ASM_STUB_ID_AsmInterpreterEntry));
106#define RTSTUB_ID(name) kungfu::RuntimeStubCSigns::ID_##name
107} // namespace panda::ecmascript::kungfu
108#endif  // ECMASCRIPT_COMPILER_RT_CALL_SIGNATURE_H