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