builtins.h revision 0d5e116f6aee03185f237311a943491bb079a768
1// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_BUILTINS_H_
29#define V8_BUILTINS_H_
30
31namespace v8 {
32namespace internal {
33
34// Specifies extra arguments required by a C++ builtin.
35enum BuiltinExtraArguments {
36  NO_EXTRA_ARGUMENTS = 0,
37  NEEDS_CALLED_FUNCTION = 1
38};
39
40
41// Define list of builtins implemented in C++.
42#define BUILTIN_LIST_C(V)                                           \
43  V(Illegal, NO_EXTRA_ARGUMENTS)                                    \
44                                                                    \
45  V(EmptyFunction, NO_EXTRA_ARGUMENTS)                              \
46                                                                    \
47  V(ArrayCodeGeneric, NO_EXTRA_ARGUMENTS)                           \
48                                                                    \
49  V(ArrayPush, NO_EXTRA_ARGUMENTS)                                  \
50  V(ArrayPop, NO_EXTRA_ARGUMENTS)                                   \
51  V(ArrayShift, NO_EXTRA_ARGUMENTS)                                 \
52  V(ArrayUnshift, NO_EXTRA_ARGUMENTS)                               \
53  V(ArraySlice, NO_EXTRA_ARGUMENTS)                                 \
54  V(ArraySplice, NO_EXTRA_ARGUMENTS)                                \
55  V(ArrayConcat, NO_EXTRA_ARGUMENTS)                                \
56                                                                    \
57  V(HandleApiCall, NEEDS_CALLED_FUNCTION)                           \
58  V(FastHandleApiCall, NO_EXTRA_ARGUMENTS)                          \
59  V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION)                  \
60  V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS)                    \
61  V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS)
62
63
64// Define list of builtins implemented in assembly.
65#define BUILTIN_LIST_A(V)                                                 \
66  V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED)                   \
67  V(JSConstructCall,            BUILTIN, UNINITIALIZED)                   \
68  V(JSConstructStubCountdown,   BUILTIN, UNINITIALIZED)                   \
69  V(JSConstructStubGeneric,     BUILTIN, UNINITIALIZED)                   \
70  V(JSConstructStubApi,         BUILTIN, UNINITIALIZED)                   \
71  V(JSEntryTrampoline,          BUILTIN, UNINITIALIZED)                   \
72  V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED)                   \
73  V(LazyCompile,                BUILTIN, UNINITIALIZED)                   \
74                                                                          \
75  V(LoadIC_Miss,                BUILTIN, UNINITIALIZED)                   \
76  V(KeyedLoadIC_Miss,           BUILTIN, UNINITIALIZED)                   \
77  V(StoreIC_Miss,               BUILTIN, UNINITIALIZED)                   \
78  V(KeyedStoreIC_Miss,          BUILTIN, UNINITIALIZED)                   \
79                                                                          \
80  V(LoadIC_Initialize,          LOAD_IC, UNINITIALIZED)                   \
81  V(LoadIC_PreMonomorphic,      LOAD_IC, PREMONOMORPHIC)                  \
82  V(LoadIC_Normal,              LOAD_IC, MONOMORPHIC)                     \
83  V(LoadIC_ArrayLength,         LOAD_IC, MONOMORPHIC)                     \
84  V(LoadIC_StringLength,        LOAD_IC, MONOMORPHIC)                     \
85  V(LoadIC_FunctionPrototype,   LOAD_IC, MONOMORPHIC)                     \
86  V(LoadIC_Megamorphic,         LOAD_IC, MEGAMORPHIC)                     \
87                                                                          \
88  V(KeyedLoadIC_Initialize,     KEYED_LOAD_IC, UNINITIALIZED)             \
89  V(KeyedLoadIC_PreMonomorphic, KEYED_LOAD_IC, PREMONOMORPHIC)            \
90  V(KeyedLoadIC_Generic,        KEYED_LOAD_IC, MEGAMORPHIC)               \
91  V(KeyedLoadIC_String,         KEYED_LOAD_IC, MEGAMORPHIC)               \
92  V(KeyedLoadIC_ExternalByteArray,          KEYED_LOAD_IC, MEGAMORPHIC)   \
93  V(KeyedLoadIC_ExternalUnsignedByteArray,  KEYED_LOAD_IC, MEGAMORPHIC)   \
94  V(KeyedLoadIC_ExternalShortArray,         KEYED_LOAD_IC, MEGAMORPHIC)   \
95  V(KeyedLoadIC_ExternalUnsignedShortArray, KEYED_LOAD_IC, MEGAMORPHIC)   \
96  V(KeyedLoadIC_ExternalIntArray,           KEYED_LOAD_IC, MEGAMORPHIC)   \
97  V(KeyedLoadIC_ExternalUnsignedIntArray,   KEYED_LOAD_IC, MEGAMORPHIC)   \
98  V(KeyedLoadIC_ExternalFloatArray,         KEYED_LOAD_IC, MEGAMORPHIC)   \
99  V(KeyedLoadIC_IndexedInterceptor,         KEYED_LOAD_IC, MEGAMORPHIC)   \
100                                                                          \
101  V(StoreIC_Initialize,         STORE_IC, UNINITIALIZED)                  \
102  V(StoreIC_ArrayLength,        STORE_IC, MONOMORPHIC)                    \
103  V(StoreIC_Normal,             STORE_IC, MONOMORPHIC)                    \
104  V(StoreIC_Megamorphic,        STORE_IC, MEGAMORPHIC)                    \
105                                                                          \
106  V(KeyedStoreIC_Initialize,    KEYED_STORE_IC, UNINITIALIZED)            \
107  V(KeyedStoreIC_Generic,       KEYED_STORE_IC, MEGAMORPHIC)              \
108  V(KeyedStoreIC_ExternalByteArray,          KEYED_STORE_IC, MEGAMORPHIC) \
109  V(KeyedStoreIC_ExternalUnsignedByteArray,  KEYED_STORE_IC, MEGAMORPHIC) \
110  V(KeyedStoreIC_ExternalShortArray,         KEYED_STORE_IC, MEGAMORPHIC) \
111  V(KeyedStoreIC_ExternalUnsignedShortArray, KEYED_STORE_IC, MEGAMORPHIC) \
112  V(KeyedStoreIC_ExternalIntArray,           KEYED_STORE_IC, MEGAMORPHIC) \
113  V(KeyedStoreIC_ExternalUnsignedIntArray,   KEYED_STORE_IC, MEGAMORPHIC) \
114  V(KeyedStoreIC_ExternalFloatArray,         KEYED_STORE_IC, MEGAMORPHIC) \
115                                                                          \
116  /* Uses KeyedLoadIC_Initialize; must be after in list. */               \
117  V(FunctionCall,               BUILTIN, UNINITIALIZED)                   \
118  V(FunctionApply,              BUILTIN, UNINITIALIZED)                   \
119                                                                          \
120  V(ArrayCode,                  BUILTIN, UNINITIALIZED)                   \
121  V(ArrayConstructCode,         BUILTIN, UNINITIALIZED)                   \
122                                                                          \
123  V(StringConstructCode,        BUILTIN, UNINITIALIZED)
124
125
126#ifdef ENABLE_DEBUGGER_SUPPORT
127// Define list of builtins used by the debugger implemented in assembly.
128#define BUILTIN_LIST_DEBUG_A(V)                                \
129  V(Return_DebugBreak,          BUILTIN, DEBUG_BREAK)          \
130  V(ConstructCall_DebugBreak,   BUILTIN, DEBUG_BREAK)          \
131  V(StubNoRegisters_DebugBreak, BUILTIN, DEBUG_BREAK)          \
132  V(LoadIC_DebugBreak,          LOAD_IC, DEBUG_BREAK)          \
133  V(KeyedLoadIC_DebugBreak,     KEYED_LOAD_IC, DEBUG_BREAK)    \
134  V(StoreIC_DebugBreak,         STORE_IC, DEBUG_BREAK)         \
135  V(KeyedStoreIC_DebugBreak,    KEYED_STORE_IC, DEBUG_BREAK)   \
136  V(Slot_DebugBreak,            BUILTIN, DEBUG_BREAK)          \
137  V(PlainReturn_LiveEdit,       BUILTIN, DEBUG_BREAK)          \
138  V(FrameDropper_LiveEdit,      BUILTIN, DEBUG_BREAK)
139#else
140#define BUILTIN_LIST_DEBUG_A(V)
141#endif
142
143// Define list of builtins implemented in JavaScript.
144#define BUILTINS_LIST_JS(V)              \
145  V(EQUALS, 1)                           \
146  V(STRICT_EQUALS, 1)                    \
147  V(COMPARE, 2)                          \
148  V(ADD, 1)                              \
149  V(SUB, 1)                              \
150  V(MUL, 1)                              \
151  V(DIV, 1)                              \
152  V(MOD, 1)                              \
153  V(BIT_OR, 1)                           \
154  V(BIT_AND, 1)                          \
155  V(BIT_XOR, 1)                          \
156  V(UNARY_MINUS, 0)                      \
157  V(BIT_NOT, 0)                          \
158  V(SHL, 1)                              \
159  V(SAR, 1)                              \
160  V(SHR, 1)                              \
161  V(DELETE, 1)                           \
162  V(IN, 1)                               \
163  V(INSTANCE_OF, 1)                      \
164  V(GET_KEYS, 0)                         \
165  V(FILTER_KEY, 1)                       \
166  V(CALL_NON_FUNCTION, 0)                \
167  V(CALL_NON_FUNCTION_AS_CONSTRUCTOR, 0) \
168  V(TO_OBJECT, 0)                        \
169  V(TO_NUMBER, 0)                        \
170  V(TO_STRING, 0)                        \
171  V(STRING_ADD_LEFT, 1)                  \
172  V(STRING_ADD_RIGHT, 1)                 \
173  V(APPLY_PREPARE, 1)                    \
174  V(APPLY_OVERFLOW, 1)
175
176
177class ObjectVisitor;
178
179
180class Builtins : public AllStatic {
181 public:
182  // Generate all builtin code objects. Should be called once during
183  // VM initialization.
184  static void Setup(bool create_heap_objects);
185  static void TearDown();
186
187  // Garbage collection support.
188  static void IterateBuiltins(ObjectVisitor* v);
189
190  // Disassembler support.
191  static const char* Lookup(byte* pc);
192
193  enum Name {
194#define DEF_ENUM_C(name, ignore) name,
195#define DEF_ENUM_A(name, kind, state) name,
196    BUILTIN_LIST_C(DEF_ENUM_C)
197    BUILTIN_LIST_A(DEF_ENUM_A)
198    BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
199#undef DEF_ENUM_C
200#undef DEF_ENUM_A
201    builtin_count
202  };
203
204  enum CFunctionId {
205#define DEF_ENUM_C(name, ignore) c_##name,
206    BUILTIN_LIST_C(DEF_ENUM_C)
207#undef DEF_ENUM_C
208    cfunction_count
209  };
210
211  enum JavaScript {
212#define DEF_ENUM(name, ignore) name,
213    BUILTINS_LIST_JS(DEF_ENUM)
214#undef DEF_ENUM
215    id_count
216  };
217
218  static Code* builtin(Name name) {
219    // Code::cast cannot be used here since we access builtins
220    // during the marking phase of mark sweep. See IC::Clear.
221    return reinterpret_cast<Code*>(builtins_[name]);
222  }
223
224  static Address builtin_address(Name name) {
225    return reinterpret_cast<Address>(&builtins_[name]);
226  }
227
228  static Address c_function_address(CFunctionId id) {
229    return c_functions_[id];
230  }
231
232  static const char* GetName(JavaScript id) { return javascript_names_[id]; }
233  static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
234  static Handle<Code> GetCode(JavaScript id, bool* resolved);
235  static int NumberOfJavaScriptBuiltins() { return id_count; }
236
237 private:
238  // The external C++ functions called from the code.
239  static Address c_functions_[cfunction_count];
240
241  // Note: These are always Code objects, but to conform with
242  // IterateBuiltins() above which assumes Object**'s for the callback
243  // function f, we use an Object* array here.
244  static Object* builtins_[builtin_count];
245  static const char* names_[builtin_count];
246  static const char* javascript_names_[id_count];
247  static int javascript_argc_[id_count];
248
249  static void Generate_Adaptor(MacroAssembler* masm,
250                               CFunctionId id,
251                               BuiltinExtraArguments extra_args);
252  static void Generate_JSConstructCall(MacroAssembler* masm);
253  static void Generate_JSConstructStubCountdown(MacroAssembler* masm);
254  static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
255  static void Generate_JSConstructStubApi(MacroAssembler* masm);
256  static void Generate_JSEntryTrampoline(MacroAssembler* masm);
257  static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
258  static void Generate_LazyCompile(MacroAssembler* masm);
259  static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
260
261  static void Generate_FunctionCall(MacroAssembler* masm);
262  static void Generate_FunctionApply(MacroAssembler* masm);
263
264  static void Generate_ArrayCode(MacroAssembler* masm);
265  static void Generate_ArrayConstructCode(MacroAssembler* masm);
266
267  static void Generate_StringConstructCode(MacroAssembler* masm);
268};
269
270} }  // namespace v8::internal
271
272#endif  // V8_BUILTINS_H_
273