blob: 141d5b7a58135eafb52cb71c6a5198e8d7fbc695 [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
34// Define list of builtins implemented in C.
35#define BUILTIN_LIST_C(V) \
36 V(Illegal) \
37 \
38 V(EmptyFunction) \
39 \
40 V(ArrayCodeGeneric) \
41 \
42 V(ArrayPush) \
43 V(ArrayPop) \
44 \
45 V(HandleApiCall) \
46 V(HandleApiCallAsFunction) \
47 V(HandleApiCallAsConstructor)
48
49
50// Define list of builtins implemented in assembly.
51#define BUILTIN_LIST_A(V) \
52 V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED) \
53 V(JSConstructCall, BUILTIN, UNINITIALIZED) \
54 V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED) \
55 V(JSEntryTrampoline, BUILTIN, UNINITIALIZED) \
56 V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED) \
57 \
58 V(LoadIC_Miss, BUILTIN, UNINITIALIZED) \
59 V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED) \
60 V(StoreIC_Miss, BUILTIN, UNINITIALIZED) \
61 V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED) \
62 \
63 V(StoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
64 V(KeyedStoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
65 \
66 V(LoadIC_Initialize, LOAD_IC, UNINITIALIZED) \
67 V(LoadIC_PreMonomorphic, LOAD_IC, PREMONOMORPHIC) \
68 V(LoadIC_Normal, LOAD_IC, MONOMORPHIC) \
69 V(LoadIC_ArrayLength, LOAD_IC, MONOMORPHIC) \
70 V(LoadIC_StringLength, LOAD_IC, MONOMORPHIC) \
71 V(LoadIC_FunctionPrototype, LOAD_IC, MONOMORPHIC) \
72 V(LoadIC_Megamorphic, LOAD_IC, MEGAMORPHIC) \
73 \
74 V(KeyedLoadIC_Initialize, KEYED_LOAD_IC, UNINITIALIZED) \
75 V(KeyedLoadIC_PreMonomorphic, KEYED_LOAD_IC, PREMONOMORPHIC) \
76 V(KeyedLoadIC_Generic, KEYED_LOAD_IC, MEGAMORPHIC) \
77 \
78 V(StoreIC_Initialize, STORE_IC, UNINITIALIZED) \
79 V(StoreIC_Megamorphic, STORE_IC, MEGAMORPHIC) \
80 \
81 V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED) \
82 V(KeyedStoreIC_Generic, KEYED_STORE_IC, MEGAMORPHIC) \
83 \
84 /* Uses KeyedLoadIC_Initialize; must be after in list. */ \
85 V(FunctionCall, BUILTIN, UNINITIALIZED) \
86 V(FunctionApply, BUILTIN, UNINITIALIZED) \
87 \
88 V(ArrayCode, BUILTIN, UNINITIALIZED) \
89 V(ArrayConstructCode, BUILTIN, UNINITIALIZED)
90
91#ifdef ENABLE_DEBUGGER_SUPPORT
92// Define list of builtins used by the debugger implemented in assembly.
93#define BUILTIN_LIST_DEBUG_A(V) \
94 V(Return_DebugBreak, BUILTIN, DEBUG_BREAK) \
95 V(ConstructCall_DebugBreak, BUILTIN, DEBUG_BREAK) \
96 V(StubNoRegisters_DebugBreak, BUILTIN, DEBUG_BREAK) \
97 V(LoadIC_DebugBreak, LOAD_IC, DEBUG_BREAK) \
98 V(KeyedLoadIC_DebugBreak, KEYED_LOAD_IC, DEBUG_BREAK) \
99 V(StoreIC_DebugBreak, STORE_IC, DEBUG_BREAK) \
100 V(KeyedStoreIC_DebugBreak, KEYED_STORE_IC, DEBUG_BREAK)
101#else
102#define BUILTIN_LIST_DEBUG_A(V)
103#endif
104
105// Define list of builtins implemented in JavaScript.
106#define BUILTINS_LIST_JS(V) \
107 V(EQUALS, 1) \
108 V(STRICT_EQUALS, 1) \
109 V(COMPARE, 2) \
110 V(ADD, 1) \
111 V(SUB, 1) \
112 V(MUL, 1) \
113 V(DIV, 1) \
114 V(MOD, 1) \
115 V(BIT_OR, 1) \
116 V(BIT_AND, 1) \
117 V(BIT_XOR, 1) \
118 V(UNARY_MINUS, 0) \
119 V(BIT_NOT, 0) \
120 V(SHL, 1) \
121 V(SAR, 1) \
122 V(SHR, 1) \
123 V(DELETE, 1) \
124 V(IN, 1) \
125 V(INSTANCE_OF, 1) \
126 V(GET_KEYS, 0) \
127 V(FILTER_KEY, 1) \
128 V(CALL_NON_FUNCTION, 0) \
129 V(CALL_NON_FUNCTION_AS_CONSTRUCTOR, 0) \
130 V(TO_OBJECT, 0) \
131 V(TO_NUMBER, 0) \
132 V(TO_STRING, 0) \
133 V(STRING_ADD_LEFT, 1) \
134 V(STRING_ADD_RIGHT, 1) \
135 V(APPLY_PREPARE, 1) \
136 V(APPLY_OVERFLOW, 1)
137
138
139class ObjectVisitor;
140
141
142class Builtins : public AllStatic {
143 public:
144 // Generate all builtin code objects. Should be called once during
145 // VM initialization.
146 static void Setup(bool create_heap_objects);
147 static void TearDown();
148
149 // Garbage collection support.
150 static void IterateBuiltins(ObjectVisitor* v);
151
152 // Disassembler support.
153 static const char* Lookup(byte* pc);
154
155 enum Name {
156#define DEF_ENUM_C(name) name,
157#define DEF_ENUM_A(name, kind, state) name,
158 BUILTIN_LIST_C(DEF_ENUM_C)
159 BUILTIN_LIST_A(DEF_ENUM_A)
160 BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
161#undef DEF_ENUM_C
162#undef DEF_ENUM_A
163 builtin_count
164 };
165
166 enum CFunctionId {
167#define DEF_ENUM_C(name) c_##name,
168 BUILTIN_LIST_C(DEF_ENUM_C)
169#undef DEF_ENUM_C
170 cfunction_count
171 };
172
173 enum JavaScript {
174#define DEF_ENUM(name, ignore) name,
175 BUILTINS_LIST_JS(DEF_ENUM)
176#undef DEF_ENUM
177 id_count
178 };
179
180 static Code* builtin(Name name) {
181 // Code::cast cannot be used here since we access builtins
182 // during the marking phase of mark sweep. See IC::Clear.
183 return reinterpret_cast<Code*>(builtins_[name]);
184 }
185
186 static Address builtin_address(Name name) {
187 return reinterpret_cast<Address>(&builtins_[name]);
188 }
189
190 static Address c_function_address(CFunctionId id) {
191 return c_functions_[id];
192 }
193
194 static const char* GetName(JavaScript id) { return javascript_names_[id]; }
195 static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
196 static Handle<Code> GetCode(JavaScript id, bool* resolved);
197 static int NumberOfJavaScriptBuiltins() { return id_count; }
198
199 static Object* builtin_passed_function;
200
201 private:
202 // The external C++ functions called from the code.
203 static Address c_functions_[cfunction_count];
204
205 // Note: These are always Code objects, but to conform with
206 // IterateBuiltins() above which assumes Object**'s for the callback
207 // function f, we use an Object* array here.
208 static Object* builtins_[builtin_count];
209 static const char* names_[builtin_count];
210 static const char* javascript_names_[id_count];
211 static int javascript_argc_[id_count];
212
213 static void Generate_Adaptor(MacroAssembler* masm, CFunctionId id);
214 static void Generate_JSConstructCall(MacroAssembler* masm);
215 static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
216 static void Generate_JSEntryTrampoline(MacroAssembler* masm);
217 static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
218 static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
219
220 static void Generate_FunctionCall(MacroAssembler* masm);
221 static void Generate_FunctionApply(MacroAssembler* masm);
222
223 static void Generate_ArrayCode(MacroAssembler* masm);
224 static void Generate_ArrayConstructCode(MacroAssembler* masm);
225};
226
227} } // namespace v8::internal
228
229#endif // V8_BUILTINS_H_