d7cd720fe14d005836e27d95545324fb57d2d34b
[platform/upstream/v8.git] / src / builtins.h
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_BUILTINS_H_
6 #define V8_BUILTINS_H_
7
8 namespace v8 {
9 namespace internal {
10
11 // Specifies extra arguments required by a C++ builtin.
12 enum BuiltinExtraArguments {
13   NO_EXTRA_ARGUMENTS = 0,
14   NEEDS_CALLED_FUNCTION = 1
15 };
16
17
18 #define CODE_AGE_LIST_WITH_ARG(V, A)     \
19   V(Quadragenarian, A)                   \
20   V(Quinquagenarian, A)                  \
21   V(Sexagenarian, A)                     \
22   V(Septuagenarian, A)                   \
23   V(Octogenarian, A)
24
25 #define CODE_AGE_LIST_IGNORE_ARG(X, V) V(X)
26
27 #define CODE_AGE_LIST(V) \
28   CODE_AGE_LIST_WITH_ARG(CODE_AGE_LIST_IGNORE_ARG, V)
29
30 #define CODE_AGE_LIST_COMPLETE(V)                  \
31   V(ToBeExecutedOnce)                              \
32   V(NotExecuted)                                   \
33   V(ExecutedOnce)                                  \
34   V(NoAge)                                         \
35   CODE_AGE_LIST_WITH_ARG(CODE_AGE_LIST_IGNORE_ARG, V)
36
37 #define DECLARE_CODE_AGE_BUILTIN(C, V)             \
38   V(Make##C##CodeYoungAgainOddMarking, BUILTIN,    \
39     UNINITIALIZED, kNoExtraICState)                \
40   V(Make##C##CodeYoungAgainEvenMarking, BUILTIN,   \
41     UNINITIALIZED, kNoExtraICState)
42
43
44 // Define list of builtins implemented in C++.
45 #define BUILTIN_LIST_C(V)                                    \
46   V(Illegal, NO_EXTRA_ARGUMENTS)                             \
47                                                              \
48   V(EmptyFunction, NO_EXTRA_ARGUMENTS)                       \
49                                                              \
50   V(ArrayPush, NO_EXTRA_ARGUMENTS)                           \
51   V(ArrayPop, NO_EXTRA_ARGUMENTS)                            \
52   V(ArrayShift, NO_EXTRA_ARGUMENTS)                          \
53   V(ArrayUnshift, NO_EXTRA_ARGUMENTS)                        \
54   V(ArraySlice, NO_EXTRA_ARGUMENTS)                          \
55   V(ArraySplice, NO_EXTRA_ARGUMENTS)                         \
56   V(ArrayConcat, NO_EXTRA_ARGUMENTS)                         \
57                                                              \
58   V(HandleApiCall, NEEDS_CALLED_FUNCTION)                    \
59   V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION)           \
60   V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS)             \
61   V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS)          \
62                                                              \
63   V(RestrictedFunctionPropertiesThrower, NO_EXTRA_ARGUMENTS) \
64   V(RestrictedStrictArgumentsPropertiesThrower, NO_EXTRA_ARGUMENTS)
65
66 // Define list of builtins implemented in assembly.
67 #define BUILTIN_LIST_A(V)                                                    \
68   V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)     \
69   V(InOptimizationQueue, BUILTIN, UNINITIALIZED, kNoExtraICState)            \
70   V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED, kNoExtraICState)         \
71   V(JSConstructStubForDerived, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
72   V(JSConstructStubApi, BUILTIN, UNINITIALIZED, kNoExtraICState)             \
73   V(JSEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
74   V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)     \
75   V(InterpreterEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)     \
76   V(InterpreterExitTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
77   V(CompileLazy, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
78   V(CompileOptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
79   V(CompileOptimizedConcurrent, BUILTIN, UNINITIALIZED, kNoExtraICState)     \
80   V(NotifyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
81   V(NotifySoftDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)          \
82   V(NotifyLazyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)          \
83   V(NotifyStubFailure, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
84   V(NotifyStubFailureSaveDoubles, BUILTIN, UNINITIALIZED, kNoExtraICState)   \
85                                                                              \
86   V(LoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
87   V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
88   V(StoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)                   \
89   V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
90   V(LoadIC_Getter_ForDeopt, LOAD_IC, MONOMORPHIC, kNoExtraICState)           \
91   V(KeyedLoadIC_Megamorphic, KEYED_LOAD_IC, MEGAMORPHIC, kNoExtraICState)    \
92                                                                              \
93   V(KeyedLoadIC_Megamorphic_Strong, KEYED_LOAD_IC, MEGAMORPHIC,              \
94     LoadICState::kStrongModeState)                                           \
95                                                                              \
96   V(StoreIC_Setter_ForDeopt, STORE_IC, MONOMORPHIC,                          \
97     StoreICState::kStrictModeState)                                          \
98                                                                              \
99   V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED, kNoExtraICState) \
100   V(KeyedStoreIC_PreMonomorphic, KEYED_STORE_IC, PREMONOMORPHIC,             \
101     kNoExtraICState)                                                         \
102   V(KeyedStoreIC_Megamorphic, KEYED_STORE_IC, MEGAMORPHIC, kNoExtraICState)  \
103                                                                              \
104   V(KeyedStoreIC_Initialize_Strict, KEYED_STORE_IC, UNINITIALIZED,           \
105     StoreICState::kStrictModeState)                                          \
106   V(KeyedStoreIC_PreMonomorphic_Strict, KEYED_STORE_IC, PREMONOMORPHIC,      \
107     StoreICState::kStrictModeState)                                          \
108   V(KeyedStoreIC_Megamorphic_Strict, KEYED_STORE_IC, MEGAMORPHIC,            \
109     StoreICState::kStrictModeState)                                          \
110                                                                              \
111   V(FunctionCall, BUILTIN, UNINITIALIZED, kNoExtraICState)                   \
112   V(FunctionApply, BUILTIN, UNINITIALIZED, kNoExtraICState)                  \
113   V(ReflectApply, BUILTIN, UNINITIALIZED, kNoExtraICState)                   \
114   V(ReflectConstruct, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
115                                                                              \
116   V(InternalArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
117   V(ArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState)                      \
118                                                                              \
119   V(StringConstructCode, BUILTIN, UNINITIALIZED, kNoExtraICState)            \
120                                                                              \
121   V(OnStackReplacement, BUILTIN, UNINITIALIZED, kNoExtraICState)             \
122   V(InterruptCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)                 \
123   V(OsrAfterStackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)             \
124   V(StackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)                     \
125                                                                              \
126   V(MarkCodeAsToBeExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState)     \
127   V(MarkCodeAsExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState)         \
128   V(MarkCodeAsExecutedTwice, BUILTIN, UNINITIALIZED, kNoExtraICState)        \
129   CODE_AGE_LIST_WITH_ARG(DECLARE_CODE_AGE_BUILTIN, V)
130
131 // Define list of builtin handlers implemented in assembly.
132 #define BUILTIN_LIST_H(V)                    \
133   V(LoadIC_Slow,             LOAD_IC)        \
134   V(LoadIC_Slow_Strong,      LOAD_IC)        \
135   V(KeyedLoadIC_Slow,        KEYED_LOAD_IC)  \
136   V(KeyedLoadIC_Slow_Strong, KEYED_LOAD_IC)  \
137   V(StoreIC_Slow,            STORE_IC)       \
138   V(KeyedStoreIC_Slow,       KEYED_STORE_IC) \
139   V(LoadIC_Normal,           LOAD_IC)        \
140   V(LoadIC_Normal_Strong,    LOAD_IC)        \
141   V(StoreIC_Normal,          STORE_IC)
142
143 // Define list of builtins used by the debugger implemented in assembly.
144 #define BUILTIN_LIST_DEBUG_A(V)                                 \
145   V(Return_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState)    \
146   V(Slot_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState)      \
147   V(PlainReturn_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState) \
148   V(FrameDropper_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState)
149
150 // Define list of builtins implemented in JavaScript.
151 #define BUILTINS_LIST_JS(V)                \
152   V(EQUALS, 1)                             \
153   V(STRICT_EQUALS, 1)                      \
154   V(COMPARE, 2)                            \
155   V(COMPARE_STRONG, 2)                     \
156   V(ADD, 1)                                \
157   V(ADD_STRONG, 1)                         \
158   V(SUB, 1)                                \
159   V(SUB_STRONG, 1)                         \
160   V(MUL, 1)                                \
161   V(MUL_STRONG, 1)                         \
162   V(DIV, 1)                                \
163   V(DIV_STRONG, 1)                         \
164   V(MOD, 1)                                \
165   V(MOD_STRONG, 1)                         \
166   V(BIT_OR, 1)                             \
167   V(BIT_OR_STRONG, 1)                      \
168   V(BIT_AND, 1)                            \
169   V(BIT_AND_STRONG, 1)                     \
170   V(BIT_XOR, 1)                            \
171   V(BIT_XOR_STRONG, 1)                     \
172   V(SHL, 1)                                \
173   V(SHL_STRONG, 1)                         \
174   V(SAR, 1)                                \
175   V(SAR_STRONG, 1)                         \
176   V(SHR, 1)                                \
177   V(SHR_STRONG, 1)                         \
178   V(DELETE, 2)                             \
179   V(IN, 1)                                 \
180   V(INSTANCE_OF, 1)                        \
181   V(CALL_NON_FUNCTION, 0)                  \
182   V(CALL_NON_FUNCTION_AS_CONSTRUCTOR, 0)   \
183   V(CALL_FUNCTION_PROXY, 1)                \
184   V(CALL_FUNCTION_PROXY_AS_CONSTRUCTOR, 1) \
185   V(TO_OBJECT, 0)                          \
186   V(TO_NUMBER, 0)                          \
187   V(TO_STRING, 0)                          \
188   V(TO_NAME, 0)                            \
189   V(STRING_ADD_LEFT, 1)                    \
190   V(STRING_ADD_LEFT_STRONG, 1)             \
191   V(STRING_ADD_RIGHT, 1)                   \
192   V(STRING_ADD_RIGHT_STRONG, 1)            \
193   V(APPLY_PREPARE, 1)                      \
194   V(REFLECT_APPLY_PREPARE, 1)              \
195   V(REFLECT_CONSTRUCT_PREPARE, 2)          \
196   V(CONCAT_ITERABLE_TO_ARRAY, 1)           \
197   V(STACK_OVERFLOW, 1)
198
199 class BuiltinFunctionTable;
200 class ObjectVisitor;
201
202
203 class Builtins {
204  public:
205   ~Builtins();
206
207   // Generate all builtin code objects. Should be called once during
208   // isolate initialization.
209   void SetUp(Isolate* isolate, bool create_heap_objects);
210   void TearDown();
211
212   // Garbage collection support.
213   void IterateBuiltins(ObjectVisitor* v);
214
215   // Disassembler support.
216   const char* Lookup(byte* pc);
217
218   enum Name {
219 #define DEF_ENUM_C(name, ignore) k##name,
220 #define DEF_ENUM_A(name, kind, state, extra) k##name,
221 #define DEF_ENUM_H(name, kind) k##name,
222     BUILTIN_LIST_C(DEF_ENUM_C)
223     BUILTIN_LIST_A(DEF_ENUM_A)
224     BUILTIN_LIST_H(DEF_ENUM_H)
225     BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
226 #undef DEF_ENUM_C
227 #undef DEF_ENUM_A
228     builtin_count
229   };
230
231   enum CFunctionId {
232 #define DEF_ENUM_C(name, ignore) c_##name,
233     BUILTIN_LIST_C(DEF_ENUM_C)
234 #undef DEF_ENUM_C
235     cfunction_count
236   };
237
238   enum JavaScript {
239 #define DEF_ENUM(name, ignore) name,
240     BUILTINS_LIST_JS(DEF_ENUM)
241 #undef DEF_ENUM
242     id_count
243   };
244
245 #define DECLARE_BUILTIN_ACCESSOR_C(name, ignore) Handle<Code> name();
246 #define DECLARE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
247   Handle<Code> name();
248 #define DECLARE_BUILTIN_ACCESSOR_H(name, kind) Handle<Code> name();
249   BUILTIN_LIST_C(DECLARE_BUILTIN_ACCESSOR_C)
250   BUILTIN_LIST_A(DECLARE_BUILTIN_ACCESSOR_A)
251   BUILTIN_LIST_H(DECLARE_BUILTIN_ACCESSOR_H)
252   BUILTIN_LIST_DEBUG_A(DECLARE_BUILTIN_ACCESSOR_A)
253 #undef DECLARE_BUILTIN_ACCESSOR_C
254 #undef DECLARE_BUILTIN_ACCESSOR_A
255
256   Code* builtin(Name name) {
257     // Code::cast cannot be used here since we access builtins
258     // during the marking phase of mark sweep. See IC::Clear.
259     return reinterpret_cast<Code*>(builtins_[name]);
260   }
261
262   Address builtin_address(Name name) {
263     return reinterpret_cast<Address>(&builtins_[name]);
264   }
265
266   static Address c_function_address(CFunctionId id) {
267     return c_functions_[id];
268   }
269
270   static const char* GetName(JavaScript id) { return javascript_names_[id]; }
271   const char* name(int index) {
272     DCHECK(index >= 0);
273     DCHECK(index < builtin_count);
274     return names_[index];
275   }
276   static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
277   static int NumberOfJavaScriptBuiltins() { return id_count; }
278
279   bool is_initialized() const { return initialized_; }
280
281   MUST_USE_RESULT static MaybeHandle<Object> InvokeApiFunction(
282       Handle<JSFunction> function, Handle<Object> receiver, int argc,
283       Handle<Object> args[]);
284
285  private:
286   Builtins();
287
288   // The external C++ functions called from the code.
289   static Address const c_functions_[cfunction_count];
290
291   // Note: These are always Code objects, but to conform with
292   // IterateBuiltins() above which assumes Object**'s for the callback
293   // function f, we use an Object* array here.
294   Object* builtins_[builtin_count];
295   const char* names_[builtin_count];
296   static const char* const javascript_names_[id_count];
297   static int const javascript_argc_[id_count];
298
299   static void Generate_Adaptor(MacroAssembler* masm,
300                                CFunctionId id,
301                                BuiltinExtraArguments extra_args);
302   static void Generate_CompileLazy(MacroAssembler* masm);
303   static void Generate_InOptimizationQueue(MacroAssembler* masm);
304   static void Generate_CompileOptimized(MacroAssembler* masm);
305   static void Generate_CompileOptimizedConcurrent(MacroAssembler* masm);
306   static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
307   static void Generate_JSConstructStubForDerived(MacroAssembler* masm);
308   static void Generate_JSConstructStubApi(MacroAssembler* masm);
309   static void Generate_JSEntryTrampoline(MacroAssembler* masm);
310   static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
311   static void Generate_InterpreterEntryTrampoline(MacroAssembler* masm);
312   static void Generate_InterpreterExitTrampoline(MacroAssembler* masm);
313   static void Generate_NotifyDeoptimized(MacroAssembler* masm);
314   static void Generate_NotifySoftDeoptimized(MacroAssembler* masm);
315   static void Generate_NotifyLazyDeoptimized(MacroAssembler* masm);
316   static void Generate_NotifyStubFailure(MacroAssembler* masm);
317   static void Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm);
318   static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
319
320   static void Generate_FunctionCall(MacroAssembler* masm);
321   static void Generate_FunctionApply(MacroAssembler* masm);
322   static void Generate_ReflectApply(MacroAssembler* masm);
323   static void Generate_ReflectConstruct(MacroAssembler* masm);
324
325   static void Generate_InternalArrayCode(MacroAssembler* masm);
326   static void Generate_ArrayCode(MacroAssembler* masm);
327
328   static void Generate_StringConstructCode(MacroAssembler* masm);
329   static void Generate_OnStackReplacement(MacroAssembler* masm);
330   static void Generate_OsrAfterStackCheck(MacroAssembler* masm);
331   static void Generate_InterruptCheck(MacroAssembler* masm);
332   static void Generate_StackCheck(MacroAssembler* masm);
333
334 #define DECLARE_CODE_AGE_BUILTIN_GENERATOR(C)                \
335   static void Generate_Make##C##CodeYoungAgainEvenMarking(   \
336       MacroAssembler* masm);                                 \
337   static void Generate_Make##C##CodeYoungAgainOddMarking(    \
338       MacroAssembler* masm);
339   CODE_AGE_LIST(DECLARE_CODE_AGE_BUILTIN_GENERATOR)
340 #undef DECLARE_CODE_AGE_BUILTIN_GENERATOR
341
342   static void Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm);
343   static void Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm);
344   static void Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm);
345
346   static void InitBuiltinFunctionTable();
347
348   bool initialized_;
349
350   friend class BuiltinFunctionTable;
351   friend class Isolate;
352
353   DISALLOW_COPY_AND_ASSIGN(Builtins);
354 };
355
356 } }  // namespace v8::internal
357
358 #endif  // V8_BUILTINS_H_