blob: 418948f750d04e57edea191d22d82b6e90f09d72 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
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) \
51 \
52 V(HandleApiCall, NEEDS_CALLED_FUNCTION) \
53 V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION) \
54 V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS) \
55 V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS)
Steve Blocka7e24c12009-10-30 11:49:00 +000056
57
58// Define list of builtins implemented in assembly.
Steve Block3ce2e202009-11-05 08:53:23 +000059#define BUILTIN_LIST_A(V) \
60 V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED) \
61 V(JSConstructCall, BUILTIN, UNINITIALIZED) \
62 V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED) \
Leon Clarkee46be812010-01-19 14:06:41 +000063 V(JSConstructStubApi, BUILTIN, UNINITIALIZED) \
Steve Block3ce2e202009-11-05 08:53:23 +000064 V(JSEntryTrampoline, BUILTIN, UNINITIALIZED) \
65 V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED) \
66 \
67 V(LoadIC_Miss, BUILTIN, UNINITIALIZED) \
68 V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED) \
69 V(StoreIC_Miss, BUILTIN, UNINITIALIZED) \
70 V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED) \
71 \
72 V(StoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
73 V(KeyedStoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
74 \
75 V(LoadIC_Initialize, LOAD_IC, UNINITIALIZED) \
76 V(LoadIC_PreMonomorphic, LOAD_IC, PREMONOMORPHIC) \
77 V(LoadIC_Normal, LOAD_IC, MONOMORPHIC) \
78 V(LoadIC_ArrayLength, LOAD_IC, MONOMORPHIC) \
79 V(LoadIC_StringLength, LOAD_IC, MONOMORPHIC) \
80 V(LoadIC_FunctionPrototype, LOAD_IC, MONOMORPHIC) \
81 V(LoadIC_Megamorphic, LOAD_IC, MEGAMORPHIC) \
82 \
83 V(KeyedLoadIC_Initialize, KEYED_LOAD_IC, UNINITIALIZED) \
84 V(KeyedLoadIC_PreMonomorphic, KEYED_LOAD_IC, PREMONOMORPHIC) \
85 V(KeyedLoadIC_Generic, KEYED_LOAD_IC, MEGAMORPHIC) \
Leon Clarkee46be812010-01-19 14:06:41 +000086 V(KeyedLoadIC_String, KEYED_LOAD_IC, MEGAMORPHIC) \
Steve Block3ce2e202009-11-05 08:53:23 +000087 V(KeyedLoadIC_ExternalByteArray, KEYED_LOAD_IC, MEGAMORPHIC) \
88 V(KeyedLoadIC_ExternalUnsignedByteArray, KEYED_LOAD_IC, MEGAMORPHIC) \
89 V(KeyedLoadIC_ExternalShortArray, KEYED_LOAD_IC, MEGAMORPHIC) \
90 V(KeyedLoadIC_ExternalUnsignedShortArray, KEYED_LOAD_IC, MEGAMORPHIC) \
91 V(KeyedLoadIC_ExternalIntArray, KEYED_LOAD_IC, MEGAMORPHIC) \
92 V(KeyedLoadIC_ExternalUnsignedIntArray, KEYED_LOAD_IC, MEGAMORPHIC) \
93 V(KeyedLoadIC_ExternalFloatArray, KEYED_LOAD_IC, MEGAMORPHIC) \
94 \
95 V(StoreIC_Initialize, STORE_IC, UNINITIALIZED) \
96 V(StoreIC_Megamorphic, STORE_IC, MEGAMORPHIC) \
97 \
98 V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED) \
99 V(KeyedStoreIC_Generic, KEYED_STORE_IC, MEGAMORPHIC) \
100 V(KeyedStoreIC_ExternalByteArray, KEYED_STORE_IC, MEGAMORPHIC) \
101 V(KeyedStoreIC_ExternalUnsignedByteArray, KEYED_STORE_IC, MEGAMORPHIC) \
102 V(KeyedStoreIC_ExternalShortArray, KEYED_STORE_IC, MEGAMORPHIC) \
103 V(KeyedStoreIC_ExternalUnsignedShortArray, KEYED_STORE_IC, MEGAMORPHIC) \
104 V(KeyedStoreIC_ExternalIntArray, KEYED_STORE_IC, MEGAMORPHIC) \
105 V(KeyedStoreIC_ExternalUnsignedIntArray, KEYED_STORE_IC, MEGAMORPHIC) \
106 V(KeyedStoreIC_ExternalFloatArray, KEYED_STORE_IC, MEGAMORPHIC) \
107 \
108 /* Uses KeyedLoadIC_Initialize; must be after in list. */ \
109 V(FunctionCall, BUILTIN, UNINITIALIZED) \
110 V(FunctionApply, BUILTIN, UNINITIALIZED) \
111 \
112 V(ArrayCode, BUILTIN, UNINITIALIZED) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 V(ArrayConstructCode, BUILTIN, UNINITIALIZED)
114
115#ifdef ENABLE_DEBUGGER_SUPPORT
116// Define list of builtins used by the debugger implemented in assembly.
117#define BUILTIN_LIST_DEBUG_A(V) \
118 V(Return_DebugBreak, BUILTIN, DEBUG_BREAK) \
119 V(ConstructCall_DebugBreak, BUILTIN, DEBUG_BREAK) \
120 V(StubNoRegisters_DebugBreak, BUILTIN, DEBUG_BREAK) \
121 V(LoadIC_DebugBreak, LOAD_IC, DEBUG_BREAK) \
122 V(KeyedLoadIC_DebugBreak, KEYED_LOAD_IC, DEBUG_BREAK) \
123 V(StoreIC_DebugBreak, STORE_IC, DEBUG_BREAK) \
124 V(KeyedStoreIC_DebugBreak, KEYED_STORE_IC, DEBUG_BREAK)
125#else
126#define BUILTIN_LIST_DEBUG_A(V)
127#endif
128
129// Define list of builtins implemented in JavaScript.
130#define BUILTINS_LIST_JS(V) \
131 V(EQUALS, 1) \
132 V(STRICT_EQUALS, 1) \
133 V(COMPARE, 2) \
134 V(ADD, 1) \
135 V(SUB, 1) \
136 V(MUL, 1) \
137 V(DIV, 1) \
138 V(MOD, 1) \
139 V(BIT_OR, 1) \
140 V(BIT_AND, 1) \
141 V(BIT_XOR, 1) \
142 V(UNARY_MINUS, 0) \
143 V(BIT_NOT, 0) \
144 V(SHL, 1) \
145 V(SAR, 1) \
146 V(SHR, 1) \
147 V(DELETE, 1) \
148 V(IN, 1) \
149 V(INSTANCE_OF, 1) \
150 V(GET_KEYS, 0) \
151 V(FILTER_KEY, 1) \
152 V(CALL_NON_FUNCTION, 0) \
153 V(CALL_NON_FUNCTION_AS_CONSTRUCTOR, 0) \
154 V(TO_OBJECT, 0) \
155 V(TO_NUMBER, 0) \
156 V(TO_STRING, 0) \
157 V(STRING_ADD_LEFT, 1) \
158 V(STRING_ADD_RIGHT, 1) \
159 V(APPLY_PREPARE, 1) \
Leon Clarkee46be812010-01-19 14:06:41 +0000160 V(APPLY_OVERFLOW, 1) \
161 V(STRING_CHAR_AT, 1)
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
163
164class ObjectVisitor;
165
166
167class Builtins : public AllStatic {
168 public:
169 // Generate all builtin code objects. Should be called once during
170 // VM initialization.
171 static void Setup(bool create_heap_objects);
172 static void TearDown();
173
174 // Garbage collection support.
175 static void IterateBuiltins(ObjectVisitor* v);
176
177 // Disassembler support.
178 static const char* Lookup(byte* pc);
179
180 enum Name {
Leon Clarkee46be812010-01-19 14:06:41 +0000181#define DEF_ENUM_C(name, ignore) name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000182#define DEF_ENUM_A(name, kind, state) name,
183 BUILTIN_LIST_C(DEF_ENUM_C)
184 BUILTIN_LIST_A(DEF_ENUM_A)
185 BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
186#undef DEF_ENUM_C
187#undef DEF_ENUM_A
188 builtin_count
189 };
190
191 enum CFunctionId {
Leon Clarkee46be812010-01-19 14:06:41 +0000192#define DEF_ENUM_C(name, ignore) c_##name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 BUILTIN_LIST_C(DEF_ENUM_C)
194#undef DEF_ENUM_C
195 cfunction_count
196 };
197
198 enum JavaScript {
199#define DEF_ENUM(name, ignore) name,
200 BUILTINS_LIST_JS(DEF_ENUM)
201#undef DEF_ENUM
202 id_count
203 };
204
205 static Code* builtin(Name name) {
206 // Code::cast cannot be used here since we access builtins
207 // during the marking phase of mark sweep. See IC::Clear.
208 return reinterpret_cast<Code*>(builtins_[name]);
209 }
210
211 static Address builtin_address(Name name) {
212 return reinterpret_cast<Address>(&builtins_[name]);
213 }
214
215 static Address c_function_address(CFunctionId id) {
216 return c_functions_[id];
217 }
218
219 static const char* GetName(JavaScript id) { return javascript_names_[id]; }
220 static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
221 static Handle<Code> GetCode(JavaScript id, bool* resolved);
222 static int NumberOfJavaScriptBuiltins() { return id_count; }
223
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 private:
225 // The external C++ functions called from the code.
226 static Address c_functions_[cfunction_count];
227
228 // Note: These are always Code objects, but to conform with
229 // IterateBuiltins() above which assumes Object**'s for the callback
230 // function f, we use an Object* array here.
231 static Object* builtins_[builtin_count];
232 static const char* names_[builtin_count];
233 static const char* javascript_names_[id_count];
234 static int javascript_argc_[id_count];
235
Leon Clarkee46be812010-01-19 14:06:41 +0000236 static void Generate_Adaptor(MacroAssembler* masm,
237 CFunctionId id,
238 BuiltinExtraArguments extra_args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 static void Generate_JSConstructCall(MacroAssembler* masm);
240 static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000241 static void Generate_JSConstructStubApi(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242 static void Generate_JSEntryTrampoline(MacroAssembler* masm);
243 static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
244 static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
245
246 static void Generate_FunctionCall(MacroAssembler* masm);
247 static void Generate_FunctionApply(MacroAssembler* masm);
248
249 static void Generate_ArrayCode(MacroAssembler* masm);
250 static void Generate_ArrayConstructCode(MacroAssembler* masm);
251};
252
253} } // namespace v8::internal
254
255#endif // V8_BUILTINS_H_