f2b6b1e5d1f50b1a117c37331260b90eb9209e4a
[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(DateToPrimitive, NO_EXTRA_ARGUMENTS)                     \
61                                                              \
62   V(SymbolConstructor, NO_EXTRA_ARGUMENTS)                   \
63   V(SymbolConstructor_ConstructStub, NO_EXTRA_ARGUMENTS)     \
64                                                              \
65   V(HandleApiCall, NEEDS_CALLED_FUNCTION)                    \
66   V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION)           \
67   V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS)             \
68   V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS)          \
69                                                              \
70   V(RestrictedFunctionPropertiesThrower, NO_EXTRA_ARGUMENTS) \
71   V(RestrictedStrictArgumentsPropertiesThrower, NO_EXTRA_ARGUMENTS)
72
73 // Define list of builtins implemented in assembly.
74 #define BUILTIN_LIST_A(V)                                                     \
75   V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
76                                                                               \
77   V(CallFunction, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
78   V(Call, BUILTIN, UNINITIALIZED, kNoExtraICState)                            \
79                                                                               \
80   V(PushArgsAndCall, BUILTIN, UNINITIALIZED, kNoExtraICState)                 \
81                                                                               \
82   V(InOptimizationQueue, BUILTIN, UNINITIALIZED, kNoExtraICState)             \
83   V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED, kNoExtraICState)          \
84   V(JSConstructStubForDerived, BUILTIN, UNINITIALIZED, kNoExtraICState)       \
85   V(JSConstructStubApi, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
86   V(JSEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
87   V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
88   V(InterpreterEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
89   V(InterpreterExitTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState)       \
90   V(CompileLazy, BUILTIN, UNINITIALIZED, kNoExtraICState)                     \
91   V(CompileOptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)                \
92   V(CompileOptimizedConcurrent, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
93   V(NotifyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
94   V(NotifySoftDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)           \
95   V(NotifyLazyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState)           \
96   V(NotifyStubFailure, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
97   V(NotifyStubFailureSaveDoubles, BUILTIN, UNINITIALIZED, kNoExtraICState)    \
98                                                                               \
99   V(LoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)                     \
100   V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)                \
101   V(StoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
102   V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
103   V(LoadIC_Getter_ForDeopt, LOAD_IC, MONOMORPHIC, kNoExtraICState)            \
104   V(KeyedLoadIC_Megamorphic, KEYED_LOAD_IC, MEGAMORPHIC, kNoExtraICState)     \
105                                                                               \
106   V(KeyedLoadIC_Megamorphic_Strong, KEYED_LOAD_IC, MEGAMORPHIC,               \
107     LoadICState::kStrongModeState)                                            \
108                                                                               \
109   V(StoreIC_Setter_ForDeopt, STORE_IC, MONOMORPHIC,                           \
110     StoreICState::kStrictModeState)                                           \
111                                                                               \
112   V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED, kNoExtraICState)  \
113   V(KeyedStoreIC_PreMonomorphic, KEYED_STORE_IC, PREMONOMORPHIC,              \
114     kNoExtraICState)                                                          \
115   V(KeyedStoreIC_Megamorphic, KEYED_STORE_IC, MEGAMORPHIC, kNoExtraICState)   \
116                                                                               \
117   V(KeyedStoreIC_Initialize_Strict, KEYED_STORE_IC, UNINITIALIZED,            \
118     StoreICState::kStrictModeState)                                           \
119   V(KeyedStoreIC_PreMonomorphic_Strict, KEYED_STORE_IC, PREMONOMORPHIC,       \
120     StoreICState::kStrictModeState)                                           \
121   V(KeyedStoreIC_Megamorphic_Strict, KEYED_STORE_IC, MEGAMORPHIC,             \
122     StoreICState::kStrictModeState)                                           \
123                                                                               \
124   V(FunctionCall, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
125   V(FunctionApply, BUILTIN, UNINITIALIZED, kNoExtraICState)                   \
126   V(ReflectApply, BUILTIN, UNINITIALIZED, kNoExtraICState)                    \
127   V(ReflectConstruct, BUILTIN, UNINITIALIZED, kNoExtraICState)                \
128                                                                               \
129   V(InternalArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
130   V(ArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState)                       \
131                                                                               \
132   V(StringConstructor, BUILTIN, UNINITIALIZED, kNoExtraICState)               \
133   V(StringConstructor_ConstructStub, BUILTIN, UNINITIALIZED, kNoExtraICState) \
134                                                                               \
135   V(OnStackReplacement, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
136   V(InterruptCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)                  \
137   V(OsrAfterStackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)              \
138   V(StackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState)                      \
139                                                                               \
140   V(MarkCodeAsToBeExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState)      \
141   V(MarkCodeAsExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState)          \
142   V(MarkCodeAsExecutedTwice, BUILTIN, UNINITIALIZED, kNoExtraICState)         \
143   CODE_AGE_LIST_WITH_ARG(DECLARE_CODE_AGE_BUILTIN, V)
144
145 // Define list of builtin handlers implemented in assembly.
146 #define BUILTIN_LIST_H(V)                    \
147   V(LoadIC_Slow,             LOAD_IC)        \
148   V(LoadIC_Slow_Strong,      LOAD_IC)        \
149   V(KeyedLoadIC_Slow,        KEYED_LOAD_IC)  \
150   V(KeyedLoadIC_Slow_Strong, KEYED_LOAD_IC)  \
151   V(StoreIC_Slow,            STORE_IC)       \
152   V(KeyedStoreIC_Slow,       KEYED_STORE_IC) \
153   V(LoadIC_Normal,           LOAD_IC)        \
154   V(LoadIC_Normal_Strong,    LOAD_IC)        \
155   V(StoreIC_Normal,          STORE_IC)
156
157 // Define list of builtins used by the debugger implemented in assembly.
158 #define BUILTIN_LIST_DEBUG_A(V)                                 \
159   V(Return_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState)    \
160   V(Slot_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState)      \
161   V(PlainReturn_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState) \
162   V(FrameDropper_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState)
163
164
165 class BuiltinFunctionTable;
166 class ObjectVisitor;
167
168
169 class Builtins {
170  public:
171   ~Builtins();
172
173   // Generate all builtin code objects. Should be called once during
174   // isolate initialization.
175   void SetUp(Isolate* isolate, bool create_heap_objects);
176   void TearDown();
177
178   // Garbage collection support.
179   void IterateBuiltins(ObjectVisitor* v);
180
181   // Disassembler support.
182   const char* Lookup(byte* pc);
183
184   enum Name {
185 #define DEF_ENUM_C(name, ignore) k##name,
186 #define DEF_ENUM_A(name, kind, state, extra) k##name,
187 #define DEF_ENUM_H(name, kind) k##name,
188     BUILTIN_LIST_C(DEF_ENUM_C)
189     BUILTIN_LIST_A(DEF_ENUM_A)
190     BUILTIN_LIST_H(DEF_ENUM_H)
191     BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
192 #undef DEF_ENUM_C
193 #undef DEF_ENUM_A
194     builtin_count
195   };
196
197   enum CFunctionId {
198 #define DEF_ENUM_C(name, ignore) c_##name,
199     BUILTIN_LIST_C(DEF_ENUM_C)
200 #undef DEF_ENUM_C
201     cfunction_count
202   };
203
204 #define DECLARE_BUILTIN_ACCESSOR_C(name, ignore) Handle<Code> name();
205 #define DECLARE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
206   Handle<Code> name();
207 #define DECLARE_BUILTIN_ACCESSOR_H(name, kind) Handle<Code> name();
208   BUILTIN_LIST_C(DECLARE_BUILTIN_ACCESSOR_C)
209   BUILTIN_LIST_A(DECLARE_BUILTIN_ACCESSOR_A)
210   BUILTIN_LIST_H(DECLARE_BUILTIN_ACCESSOR_H)
211   BUILTIN_LIST_DEBUG_A(DECLARE_BUILTIN_ACCESSOR_A)
212 #undef DECLARE_BUILTIN_ACCESSOR_C
213 #undef DECLARE_BUILTIN_ACCESSOR_A
214
215   Code* builtin(Name name) {
216     // Code::cast cannot be used here since we access builtins
217     // during the marking phase of mark sweep. See IC::Clear.
218     return reinterpret_cast<Code*>(builtins_[name]);
219   }
220
221   Address builtin_address(Name name) {
222     return reinterpret_cast<Address>(&builtins_[name]);
223   }
224
225   static Address c_function_address(CFunctionId id) {
226     return c_functions_[id];
227   }
228
229   const char* name(int index) {
230     DCHECK(index >= 0);
231     DCHECK(index < builtin_count);
232     return names_[index];
233   }
234
235   bool is_initialized() const { return initialized_; }
236
237   MUST_USE_RESULT static MaybeHandle<Object> InvokeApiFunction(
238       Handle<JSFunction> function, Handle<Object> receiver, int argc,
239       Handle<Object> args[]);
240
241  private:
242   Builtins();
243
244   // The external C++ functions called from the code.
245   static Address const c_functions_[cfunction_count];
246
247   // Note: These are always Code objects, but to conform with
248   // IterateBuiltins() above which assumes Object**'s for the callback
249   // function f, we use an Object* array here.
250   Object* builtins_[builtin_count];
251   const char* names_[builtin_count];
252
253   static void Generate_Adaptor(MacroAssembler* masm,
254                                CFunctionId id,
255                                BuiltinExtraArguments extra_args);
256   static void Generate_CompileLazy(MacroAssembler* masm);
257   static void Generate_InOptimizationQueue(MacroAssembler* masm);
258   static void Generate_CompileOptimized(MacroAssembler* masm);
259   static void Generate_CompileOptimizedConcurrent(MacroAssembler* masm);
260   static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
261   static void Generate_JSConstructStubForDerived(MacroAssembler* masm);
262   static void Generate_JSConstructStubApi(MacroAssembler* masm);
263   static void Generate_JSEntryTrampoline(MacroAssembler* masm);
264   static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
265   static void Generate_InterpreterEntryTrampoline(MacroAssembler* masm);
266   static void Generate_InterpreterExitTrampoline(MacroAssembler* masm);
267   static void Generate_NotifyDeoptimized(MacroAssembler* masm);
268   static void Generate_NotifySoftDeoptimized(MacroAssembler* masm);
269   static void Generate_NotifyLazyDeoptimized(MacroAssembler* masm);
270   static void Generate_NotifyStubFailure(MacroAssembler* masm);
271   static void Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm);
272   static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
273
274   // ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
275   static void Generate_CallFunction(MacroAssembler* masm);
276   // ES6 section 7.3.12 Call(F, V, [argumentsList])
277   static void Generate_Call(MacroAssembler* masm);
278
279   static void Generate_PushArgsAndCall(MacroAssembler* masm);
280
281   static void Generate_FunctionCall(MacroAssembler* masm);
282   static void Generate_FunctionApply(MacroAssembler* masm);
283   static void Generate_ReflectApply(MacroAssembler* masm);
284   static void Generate_ReflectConstruct(MacroAssembler* masm);
285
286   static void Generate_InternalArrayCode(MacroAssembler* masm);
287   static void Generate_ArrayCode(MacroAssembler* masm);
288
289   static void Generate_StringConstructor(MacroAssembler* masm);
290   static void Generate_StringConstructor_ConstructStub(MacroAssembler* masm);
291   static void Generate_OnStackReplacement(MacroAssembler* masm);
292   static void Generate_OsrAfterStackCheck(MacroAssembler* masm);
293   static void Generate_InterruptCheck(MacroAssembler* masm);
294   static void Generate_StackCheck(MacroAssembler* masm);
295
296 #define DECLARE_CODE_AGE_BUILTIN_GENERATOR(C)                \
297   static void Generate_Make##C##CodeYoungAgainEvenMarking(   \
298       MacroAssembler* masm);                                 \
299   static void Generate_Make##C##CodeYoungAgainOddMarking(    \
300       MacroAssembler* masm);
301   CODE_AGE_LIST(DECLARE_CODE_AGE_BUILTIN_GENERATOR)
302 #undef DECLARE_CODE_AGE_BUILTIN_GENERATOR
303
304   static void Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm);
305   static void Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm);
306   static void Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm);
307
308   static void InitBuiltinFunctionTable();
309
310   bool initialized_;
311
312   friend class BuiltinFunctionTable;
313   friend class Isolate;
314
315   DISALLOW_COPY_AND_ASSIGN(Builtins);
316 };
317
318 } }  // namespace v8::internal
319
320 #endif  // V8_BUILTINS_H_