• 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_CALL_SIGNATURE_H
17 #define ECMASCRIPT_COMPILER_BUILTINS_CALL_SIGNATURE_H
18 
19 #include "ecmascript/base/config.h"
20 #include "ecmascript/compiler/rt_call_signature.h"
21 #include "ecmascript/global_env_constants.h"
22 
23 namespace panda::ecmascript::kungfu {
24 
25 #define PADDING_BUILTINS_STUB_LIST(V)               \
26     V(NONE)
27 
28 // BUILTINS_STUB_LIST is shared both ASM Interpreter and AOT.
29 // AOT_BUILTINS_STUB_LIST is used in AOT only.
30 #define BUILTINS_STUB_LIST(V)                       \
31     BUILTINS_METHOD_STUB_LIST(V)                    \
32     BUILTINS_CONSTRUCTOR_STUB_LIST(V)
33 
34 #define BUILTINS_METHOD_STUB_LIST(V)                \
35     V(CharCodeAt)                                   \
36     V(IndexOf)                                      \
37     V(Substring)                                    \
38     V(CharAt)                                       \
39     V(VectorForEach)                                \
40     V(VectorReplaceAllElements)                     \
41     V(StackForEach)                                 \
42     V(PlainArrayForEach)                            \
43     V(QueueForEach)                                 \
44     V(DequeForEach)                                 \
45     V(LightWeightMapForEach)                        \
46     V(LightWeightSetForEach)                        \
47     V(HashMapForEach)                               \
48     V(HashSetForEach)                               \
49     V(LinkedListForEach)                            \
50     V(ListForEach)                                  \
51     V(ArrayListForEach)                             \
52     V(ArrayListReplaceAllElements)                  \
53     V(FunctionPrototypeApply)
54 
55 #define BUILTINS_CONSTRUCTOR_STUB_LIST(V)           \
56     V(BooleanConstructor)                           \
57     V(DateConstructor)                              \
58     V(ArrayConstructor)
59 
60 #define AOT_BUILTINS_STUB_LIST(V)                   \
61     V(SQRT)                                         \
62     V(COS)                                          \
63     V(SIN)                                          \
64     V(ACOS)                                         \
65     V(ATAN)                                         \
66     V(ABS)                                          \
67     V(FLOOR)                                        \
68     V(LocaleCompare)
69 
70 class BuiltinsStubCSigns {
71 public:
72     enum ID {
73 #define DEF_STUB_ID(name) name,
74         PADDING_BUILTINS_STUB_LIST(DEF_STUB_ID)
75         BUILTINS_STUB_LIST(DEF_STUB_ID)
76 #undef DEF_STUB_ID
77         NUM_OF_BUILTINS_STUBS,
78 #define DEF_STUB_ID(name) name,
79         AOT_BUILTINS_STUB_LIST(DEF_STUB_ID)
80 #undef DEF_STUB_ID
81         BUILTINS_CONSTRUCTOR_STUB_FIRST = BooleanConstructor,
82         INVALID = 0xFF,
83     };
84 
85     static void Initialize();
86 
87     static void GetCSigns(std::vector<const CallSignature*>& callSigns);
88 
Get(size_t index)89     static const CallSignature *Get(size_t index)
90     {
91         ASSERT(index < NUM_OF_BUILTINS_STUBS);
92         return &callSigns_[index];
93     }
94 
GetName(int index)95     static const std::string &GetName(int index)
96     {
97         ASSERT(index < NUM_OF_BUILTINS_STUBS);
98         return callSigns_[index].GetName();
99     }
100 
BuiltinsCSign()101     static const CallSignature* BuiltinsCSign()
102     {
103         return &builtinsCSign_;
104     }
105 
BuiltinsWithArgvCSign()106     static const CallSignature* BuiltinsWithArgvCSign()
107     {
108         return &builtinsWithArgvCSign_;
109     }
110 
IsFastBuiltin(ID builtinId)111     static bool IsFastBuiltin(ID builtinId)
112     {
113         return builtinId > NONE && builtinId < NUM_OF_BUILTINS_STUBS;
114     }
115 
IsTypedBuiltin(ID builtinId)116     static bool IsTypedBuiltin(ID builtinId)
117     {
118         switch (builtinId) {
119             case BuiltinsStubCSigns::ID::COS:
120             case BuiltinsStubCSigns::ID::SIN:
121             case BuiltinsStubCSigns::ID::ACOS:
122             case BuiltinsStubCSigns::ID::ATAN:
123             case BuiltinsStubCSigns::ID::ABS:
124             case BuiltinsStubCSigns::ID::FLOOR:
125             case BuiltinsStubCSigns::ID::SQRT:
126                 return true;
127             default:
128                 return false;
129         }
130     }
131 
GetConstantIndex(ID builtinId)132     static ConstantIndex GetConstantIndex(ID builtinId)
133     {
134         switch (builtinId) {
135             case BuiltinsStubCSigns::ID::COS:
136                 return ConstantIndex::MATH_COS_FUNCTION_INDEX;
137             case BuiltinsStubCSigns::ID::SIN:
138                 return ConstantIndex::MATH_SIN_FUNCTION_INDEX;
139             case BuiltinsStubCSigns::ID::ACOS:
140                 return ConstantIndex::MATH_ACOS_FUNCTION_INDEX;
141             case BuiltinsStubCSigns::ID::ATAN:
142                 return ConstantIndex::MATH_ATAN_FUNCTION_INDEX;
143             case BuiltinsStubCSigns::ID::ABS:
144                 return ConstantIndex::MATH_ABS_FUNCTION_INDEX;
145             case BuiltinsStubCSigns::ID::FLOOR:
146                 return ConstantIndex::MATH_FLOOR_FUNCTION_INDEX;
147             case BuiltinsStubCSigns::ID::SQRT:
148                 return ConstantIndex::MATH_SQRT_FUNCTION_INDEX;
149             default:
150                 LOG_COMPILER(FATAL) << "this branch is unreachable";
151                 UNREACHABLE();
152         }
153     }
154 
GetBuiltinId(std::string idStr)155     static ID GetBuiltinId(std::string idStr)
156     {
157         const std::map<std::string, BuiltinsStubCSigns::ID> str2BuiltinId = {
158             {"sqrt", SQRT},
159             {"cos", COS},
160             {"sin", SIN},
161             {"acos", ACOS},
162             {"atan", ATAN},
163             {"abs", ABS},
164             {"floor", FLOOR},
165             {"localeCompare", LocaleCompare},
166         };
167         if (str2BuiltinId.count(idStr) > 0) {
168             return str2BuiltinId.at(idStr);
169         }
170         return NONE;
171     }
172 
173 private:
174     static CallSignature callSigns_[NUM_OF_BUILTINS_STUBS];
175     static CallSignature builtinsCSign_;
176     static CallSignature builtinsWithArgvCSign_;
177 };
178 
179 enum class BuiltinsArgs : size_t {
180     GLUE = 0,
181     NATIVECODE,
182     FUNC,
183     NEWTARGET,
184     THISVALUE,
185     NUMARGS,
186     ARG0_OR_ARGV,
187     ARG1,
188     ARG2,
189     NUM_OF_INPUTS,
190 };
191 
192 #define BUILTINS_STUB_ID(name) kungfu::BuiltinsStubCSigns::name
193 #define IS_TYPED_BUILTINS_ID(id) kungfu::BuiltinsStubCSigns::IsTypedBuiltin(id)
194 #define GET_TYPED_CONSTANT_INDEX(id) kungfu::BuiltinsStubCSigns::GetConstantIndex(id)
195 }  // namespace panda::ecmascript::kungfu
196 #endif  // ECMASCRIPT_COMPILER_BUILTINS_CALL_SIGNATURE_H
197