blob: d2b4be2f78868e53ec0819bb42ed0267dca59088 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Leon Clarkee46be812010-01-19 14:06:41 +000034// 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) \
Andrei Popescu402d9372010-02-26 13:31:12 +000051 V(ArrayShift, NO_EXTRA_ARGUMENTS) \
52 V(ArrayUnshift, NO_EXTRA_ARGUMENTS) \
53 V(ArraySlice, NO_EXTRA_ARGUMENTS) \
54 V(ArraySplice, NO_EXTRA_ARGUMENTS) \
Steve Block6ded16b2010-05-10 14:33:55 +010055 V(ArrayConcat, NO_EXTRA_ARGUMENTS) \
Leon Clarkee46be812010-01-19 14:06:41 +000056 \
57 V(HandleApiCall, NEEDS_CALLED_FUNCTION) \
Andrei Popescu402d9372010-02-26 13:31:12 +000058 V(FastHandleApiCall, NO_EXTRA_ARGUMENTS) \
Leon Clarkee46be812010-01-19 14:06:41 +000059 V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION) \
60 V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS) \
61 V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS)
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63
64// Define list of builtins implemented in assembly.
Steve Block3ce2e202009-11-05 08:53:23 +000065#define BUILTIN_LIST_A(V) \
66 V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED) \
67 V(JSConstructCall, BUILTIN, UNINITIALIZED) \
Kristian Monsen0d5e1162010-09-30 15:31:59 +010068 V(JSConstructStubCountdown, BUILTIN, UNINITIALIZED) \
Steve Block3ce2e202009-11-05 08:53:23 +000069 V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED) \
Leon Clarkee46be812010-01-19 14:06:41 +000070 V(JSConstructStubApi, BUILTIN, UNINITIALIZED) \
Steve Block3ce2e202009-11-05 08:53:23 +000071 V(JSEntryTrampoline, BUILTIN, UNINITIALIZED) \
72 V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED) \
Iain Merrick75681382010-08-19 15:07:18 +010073 V(LazyCompile, BUILTIN, UNINITIALIZED) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010074 V(LazyRecompile, BUILTIN, UNINITIALIZED) \
75 V(NotifyDeoptimized, BUILTIN, UNINITIALIZED) \
76 V(NotifyLazyDeoptimized, BUILTIN, UNINITIALIZED) \
77 V(NotifyOSR, BUILTIN, UNINITIALIZED) \
Steve Block3ce2e202009-11-05 08:53:23 +000078 \
79 V(LoadIC_Miss, BUILTIN, UNINITIALIZED) \
80 V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED) \
81 V(StoreIC_Miss, BUILTIN, UNINITIALIZED) \
82 V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED) \
83 \
Steve Block3ce2e202009-11-05 08:53:23 +000084 V(LoadIC_Initialize, LOAD_IC, UNINITIALIZED) \
85 V(LoadIC_PreMonomorphic, LOAD_IC, PREMONOMORPHIC) \
86 V(LoadIC_Normal, LOAD_IC, MONOMORPHIC) \
87 V(LoadIC_ArrayLength, LOAD_IC, MONOMORPHIC) \
88 V(LoadIC_StringLength, LOAD_IC, MONOMORPHIC) \
89 V(LoadIC_FunctionPrototype, LOAD_IC, MONOMORPHIC) \
90 V(LoadIC_Megamorphic, LOAD_IC, MEGAMORPHIC) \
91 \
92 V(KeyedLoadIC_Initialize, KEYED_LOAD_IC, UNINITIALIZED) \
93 V(KeyedLoadIC_PreMonomorphic, KEYED_LOAD_IC, PREMONOMORPHIC) \
94 V(KeyedLoadIC_Generic, KEYED_LOAD_IC, MEGAMORPHIC) \
Leon Clarkee46be812010-01-19 14:06:41 +000095 V(KeyedLoadIC_String, KEYED_LOAD_IC, MEGAMORPHIC) \
Steve Block3ce2e202009-11-05 08:53:23 +000096 V(KeyedLoadIC_ExternalByteArray, KEYED_LOAD_IC, MEGAMORPHIC) \
97 V(KeyedLoadIC_ExternalUnsignedByteArray, KEYED_LOAD_IC, MEGAMORPHIC) \
98 V(KeyedLoadIC_ExternalShortArray, KEYED_LOAD_IC, MEGAMORPHIC) \
99 V(KeyedLoadIC_ExternalUnsignedShortArray, KEYED_LOAD_IC, MEGAMORPHIC) \
100 V(KeyedLoadIC_ExternalIntArray, KEYED_LOAD_IC, MEGAMORPHIC) \
101 V(KeyedLoadIC_ExternalUnsignedIntArray, KEYED_LOAD_IC, MEGAMORPHIC) \
102 V(KeyedLoadIC_ExternalFloatArray, KEYED_LOAD_IC, MEGAMORPHIC) \
Andrei Popescu402d9372010-02-26 13:31:12 +0000103 V(KeyedLoadIC_IndexedInterceptor, KEYED_LOAD_IC, MEGAMORPHIC) \
Steve Block3ce2e202009-11-05 08:53:23 +0000104 \
105 V(StoreIC_Initialize, STORE_IC, UNINITIALIZED) \
Steve Block6ded16b2010-05-10 14:33:55 +0100106 V(StoreIC_ArrayLength, STORE_IC, MONOMORPHIC) \
Steve Block8defd9f2010-07-08 12:39:36 +0100107 V(StoreIC_Normal, STORE_IC, MONOMORPHIC) \
Steve Block3ce2e202009-11-05 08:53:23 +0000108 V(StoreIC_Megamorphic, STORE_IC, MEGAMORPHIC) \
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109 V(StoreIC_GlobalProxy, STORE_IC, MEGAMORPHIC) \
Steve Block3ce2e202009-11-05 08:53:23 +0000110 \
111 V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED) \
112 V(KeyedStoreIC_Generic, KEYED_STORE_IC, MEGAMORPHIC) \
113 V(KeyedStoreIC_ExternalByteArray, KEYED_STORE_IC, MEGAMORPHIC) \
114 V(KeyedStoreIC_ExternalUnsignedByteArray, KEYED_STORE_IC, MEGAMORPHIC) \
115 V(KeyedStoreIC_ExternalShortArray, KEYED_STORE_IC, MEGAMORPHIC) \
116 V(KeyedStoreIC_ExternalUnsignedShortArray, KEYED_STORE_IC, MEGAMORPHIC) \
117 V(KeyedStoreIC_ExternalIntArray, KEYED_STORE_IC, MEGAMORPHIC) \
118 V(KeyedStoreIC_ExternalUnsignedIntArray, KEYED_STORE_IC, MEGAMORPHIC) \
119 V(KeyedStoreIC_ExternalFloatArray, KEYED_STORE_IC, MEGAMORPHIC) \
120 \
121 /* Uses KeyedLoadIC_Initialize; must be after in list. */ \
122 V(FunctionCall, BUILTIN, UNINITIALIZED) \
123 V(FunctionApply, BUILTIN, UNINITIALIZED) \
124 \
125 V(ArrayCode, BUILTIN, UNINITIALIZED) \
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100126 V(ArrayConstructCode, BUILTIN, UNINITIALIZED) \
127 \
Ben Murdochb0fe1622011-05-05 13:52:32 +0100128 V(StringConstructCode, BUILTIN, UNINITIALIZED) \
129 \
130 V(OnStackReplacement, BUILTIN, UNINITIALIZED)
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100131
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133#ifdef ENABLE_DEBUGGER_SUPPORT
134// Define list of builtins used by the debugger implemented in assembly.
135#define BUILTIN_LIST_DEBUG_A(V) \
136 V(Return_DebugBreak, BUILTIN, DEBUG_BREAK) \
137 V(ConstructCall_DebugBreak, BUILTIN, DEBUG_BREAK) \
138 V(StubNoRegisters_DebugBreak, BUILTIN, DEBUG_BREAK) \
139 V(LoadIC_DebugBreak, LOAD_IC, DEBUG_BREAK) \
140 V(KeyedLoadIC_DebugBreak, KEYED_LOAD_IC, DEBUG_BREAK) \
141 V(StoreIC_DebugBreak, STORE_IC, DEBUG_BREAK) \
Steve Block6ded16b2010-05-10 14:33:55 +0100142 V(KeyedStoreIC_DebugBreak, KEYED_STORE_IC, DEBUG_BREAK) \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100143 V(Slot_DebugBreak, BUILTIN, DEBUG_BREAK) \
Steve Block6ded16b2010-05-10 14:33:55 +0100144 V(PlainReturn_LiveEdit, BUILTIN, DEBUG_BREAK) \
145 V(FrameDropper_LiveEdit, BUILTIN, DEBUG_BREAK)
Steve Blocka7e24c12009-10-30 11:49:00 +0000146#else
147#define BUILTIN_LIST_DEBUG_A(V)
148#endif
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(ADD, 1) \
156 V(SUB, 1) \
157 V(MUL, 1) \
158 V(DIV, 1) \
159 V(MOD, 1) \
160 V(BIT_OR, 1) \
161 V(BIT_AND, 1) \
162 V(BIT_XOR, 1) \
163 V(UNARY_MINUS, 0) \
164 V(BIT_NOT, 0) \
165 V(SHL, 1) \
166 V(SAR, 1) \
167 V(SHR, 1) \
168 V(DELETE, 1) \
169 V(IN, 1) \
170 V(INSTANCE_OF, 1) \
171 V(GET_KEYS, 0) \
172 V(FILTER_KEY, 1) \
173 V(CALL_NON_FUNCTION, 0) \
174 V(CALL_NON_FUNCTION_AS_CONSTRUCTOR, 0) \
175 V(TO_OBJECT, 0) \
176 V(TO_NUMBER, 0) \
177 V(TO_STRING, 0) \
178 V(STRING_ADD_LEFT, 1) \
179 V(STRING_ADD_RIGHT, 1) \
180 V(APPLY_PREPARE, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100181 V(APPLY_OVERFLOW, 1)
Steve Blocka7e24c12009-10-30 11:49:00 +0000182
183
184class ObjectVisitor;
185
186
187class Builtins : public AllStatic {
188 public:
189 // Generate all builtin code objects. Should be called once during
190 // VM initialization.
191 static void Setup(bool create_heap_objects);
192 static void TearDown();
193
194 // Garbage collection support.
195 static void IterateBuiltins(ObjectVisitor* v);
196
197 // Disassembler support.
198 static const char* Lookup(byte* pc);
199
200 enum Name {
Leon Clarkee46be812010-01-19 14:06:41 +0000201#define DEF_ENUM_C(name, ignore) name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000202#define DEF_ENUM_A(name, kind, state) name,
203 BUILTIN_LIST_C(DEF_ENUM_C)
204 BUILTIN_LIST_A(DEF_ENUM_A)
205 BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
206#undef DEF_ENUM_C
207#undef DEF_ENUM_A
208 builtin_count
209 };
210
211 enum CFunctionId {
Leon Clarkee46be812010-01-19 14:06:41 +0000212#define DEF_ENUM_C(name, ignore) c_##name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 BUILTIN_LIST_C(DEF_ENUM_C)
214#undef DEF_ENUM_C
215 cfunction_count
216 };
217
218 enum JavaScript {
219#define DEF_ENUM(name, ignore) name,
220 BUILTINS_LIST_JS(DEF_ENUM)
221#undef DEF_ENUM
222 id_count
223 };
224
225 static Code* builtin(Name name) {
226 // Code::cast cannot be used here since we access builtins
227 // during the marking phase of mark sweep. See IC::Clear.
228 return reinterpret_cast<Code*>(builtins_[name]);
229 }
230
231 static Address builtin_address(Name name) {
232 return reinterpret_cast<Address>(&builtins_[name]);
233 }
234
235 static Address c_function_address(CFunctionId id) {
236 return c_functions_[id];
237 }
238
239 static const char* GetName(JavaScript id) { return javascript_names_[id]; }
240 static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
241 static Handle<Code> GetCode(JavaScript id, bool* resolved);
242 static int NumberOfJavaScriptBuiltins() { return id_count; }
243
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 private:
245 // The external C++ functions called from the code.
246 static Address c_functions_[cfunction_count];
247
248 // Note: These are always Code objects, but to conform with
249 // IterateBuiltins() above which assumes Object**'s for the callback
250 // function f, we use an Object* array here.
251 static Object* builtins_[builtin_count];
252 static const char* names_[builtin_count];
253 static const char* javascript_names_[id_count];
254 static int javascript_argc_[id_count];
255
Leon Clarkee46be812010-01-19 14:06:41 +0000256 static void Generate_Adaptor(MacroAssembler* masm,
257 CFunctionId id,
258 BuiltinExtraArguments extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 static void Generate_JSConstructCall(MacroAssembler* masm);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100260 static void Generate_JSConstructStubCountdown(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000262 static void Generate_JSConstructStubApi(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 static void Generate_JSEntryTrampoline(MacroAssembler* masm);
264 static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
Iain Merrick75681382010-08-19 15:07:18 +0100265 static void Generate_LazyCompile(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266 static void Generate_LazyRecompile(MacroAssembler* masm);
267 static void Generate_NotifyDeoptimized(MacroAssembler* masm);
268 static void Generate_NotifyLazyDeoptimized(MacroAssembler* masm);
269 static void Generate_NotifyOSR(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
271
272 static void Generate_FunctionCall(MacroAssembler* masm);
273 static void Generate_FunctionApply(MacroAssembler* masm);
274
275 static void Generate_ArrayCode(MacroAssembler* masm);
276 static void Generate_ArrayConstructCode(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100277
278 static void Generate_StringConstructCode(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100279
280 static void Generate_OnStackReplacement(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000281};
282
283} } // namespace v8::internal
284
285#endif // V8_BUILTINS_H_