blob: bcc3e89a7a1cae393568c721a2f6e13502662347 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031namespace v8 { namespace internal {
32
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033// Define list of builtins implemented in C.
34#define BUILTIN_LIST_C(V) \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000035 V(Illegal) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000037 V(EmptyFunction) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038 \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000039 V(ArrayCode) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040 \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000041 V(ArrayPush) \
42 V(ArrayPop) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043 \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000044 V(HandleApiCall) \
45 V(HandleApiCallAsFunction)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
47
48// Define list of builtins implemented in assembly.
49#define BUILTIN_LIST_A(V) \
50 V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED) \
51 V(JSConstructCall, BUILTIN, UNINITIALIZED) \
52 V(JSEntryTrampoline, BUILTIN, UNINITIALIZED) \
53 V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED) \
54 \
55 V(Return_DebugBreak, BUILTIN, DEBUG_BREAK) \
56 V(Return_DebugBreakEntry, BUILTIN, DEBUG_BREAK) \
57 V(ConstructCall_DebugBreak, BUILTIN, DEBUG_BREAK) \
58 V(StubNoRegisters_DebugBreak, BUILTIN, DEBUG_BREAK) \
59 \
60 V(LoadIC_Miss, BUILTIN, UNINITIALIZED) \
61 V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED) \
62 V(StoreIC_Miss, BUILTIN, UNINITIALIZED) \
63 V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED) \
64 \
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000065 V(StoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
kasperl@chromium.org1accd572008-10-07 10:57:21 +000066 V(KeyedStoreIC_ExtendStorage, BUILTIN, UNINITIALIZED) \
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000067 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068 V(LoadIC_Initialize, LOAD_IC, UNINITIALIZED) \
69 V(LoadIC_PreMonomorphic, LOAD_IC, PREMONOMORPHIC) \
70 V(LoadIC_Normal, LOAD_IC, MONOMORPHIC) \
71 V(LoadIC_ArrayLength, LOAD_IC, MONOMORPHIC) \
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000072 V(LoadIC_StringLength, LOAD_IC, MONOMORPHIC) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 V(LoadIC_FunctionPrototype, LOAD_IC, MONOMORPHIC) \
74 V(LoadIC_Megamorphic, LOAD_IC, MEGAMORPHIC) \
75 V(LoadIC_DebugBreak, LOAD_IC, DEBUG_BREAK) \
76 \
77 V(KeyedLoadIC_Initialize, KEYED_LOAD_IC, UNINITIALIZED) \
78 V(KeyedLoadIC_PreMonomorphic, KEYED_LOAD_IC, PREMONOMORPHIC) \
79 V(KeyedLoadIC_Generic, KEYED_LOAD_IC, MEGAMORPHIC) \
80 V(KeyedLoadIC_DebugBreak, KEYED_LOAD_IC, DEBUG_BREAK) \
81 \
82 V(StoreIC_Initialize, STORE_IC, UNINITIALIZED) \
83 V(StoreIC_Megamorphic, STORE_IC, MEGAMORPHIC) \
84 V(StoreIC_DebugBreak, STORE_IC, DEBUG_BREAK) \
85 \
86 V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED) \
87 V(KeyedStoreIC_Generic, KEYED_STORE_IC, MEGAMORPHIC) \
88 V(KeyedStoreIC_DebugBreak, KEYED_STORE_IC, DEBUG_BREAK) \
89 \
90 /* Uses KeyedLoadIC_Initialize; must be after in list. */ \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000091 V(FunctionCall, BUILTIN, UNINITIALIZED) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 V(FunctionApply, BUILTIN, UNINITIALIZED)
93
94
95// Define list of builtins implemented in JavaScript.
96#define BUILTINS_LIST_JS(V) \
97 V(EQUALS, 1) \
98 V(STRICT_EQUALS, 1) \
99 V(COMPARE, 2) \
100 V(ADD, 1) \
101 V(SUB, 1) \
102 V(MUL, 1) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 V(DIV, 1) \
104 V(MOD, 1) \
105 V(INC, 0) \
106 V(DEC, 0) \
107 V(BIT_OR, 1) \
108 V(BIT_AND, 1) \
109 V(BIT_XOR, 1) \
110 V(UNARY_MINUS, 0) \
111 V(BIT_NOT, 0) \
112 V(SHL, 1) \
113 V(SAR, 1) \
114 V(SHR, 1) \
115 V(DELETE, 1) \
116 V(IN, 1) \
117 V(INSTANCE_OF, 1) \
118 V(GET_KEYS, 0) \
119 V(FILTER_KEY, 1) \
120 V(CALL_NON_FUNCTION, 0) \
121 V(TO_OBJECT, 0) \
122 V(TO_NUMBER, 0) \
123 V(TO_STRING, 0) \
124 V(APPLY_PREPARE, 1) \
125 V(APPLY_OVERFLOW, 1)
126
127
128class ObjectVisitor;
129
130
131class Builtins : public AllStatic {
132 public:
133 // Generate all builtin code objects. Should be called once during
134 // VM initialization.
135 static void Setup(bool create_heap_objects);
136 static void TearDown();
137
138 // Garbage collection support.
139 static void IterateBuiltins(ObjectVisitor* v);
140
141 // Disassembler support.
142 static const char* Lookup(byte* pc);
143
144 enum Name {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000145#define DEF_ENUM_C(name) name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146#define DEF_ENUM_A(name, kind, state) name,
147 BUILTIN_LIST_C(DEF_ENUM_C)
148 BUILTIN_LIST_A(DEF_ENUM_A)
149#undef DEF_ENUM_C
150#undef DEF_ENUM_A
151 builtin_count
152 };
153
154 enum CFunctionId {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000155#define DEF_ENUM_C(name) c_##name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 BUILTIN_LIST_C(DEF_ENUM_C)
157#undef DEF_ENUM_C
158 cfunction_count
159 };
160
161 enum JavaScript {
162#define DEF_ENUM(name, ignore) name,
163 BUILTINS_LIST_JS(DEF_ENUM)
164#undef DEF_ENUM
165 id_count
166 };
167
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 static Code* builtin(Name name) {
169 // Code::cast cannot be used here since we access builtins
170 // during the marking phase of mark sweep. See IC::Clear.
171 return reinterpret_cast<Code*>(builtins_[name]);
172 }
173
174 static Address builtin_address(Name name) {
175 return reinterpret_cast<Address>(&builtins_[name]);
176 }
177
178 static Address c_function_address(CFunctionId id) {
179 return c_functions_[id];
180 }
181
182 static const char* GetName(JavaScript id) { return javascript_names_[id]; }
183 static int GetArgumentsCount(JavaScript id) { return javascript_argc_[id]; }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000184 static Handle<Code> GetCode(JavaScript id, bool* resolved);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 static int NumberOfJavaScriptBuiltins() { return id_count; }
186
187 // Called from stub-cache.cc.
188 static void Generate_CallIC_DebugBreak(MacroAssembler* masm);
189
190 static Object* builtin_passed_function;
191
192 private:
193 // The external C++ functions called from the code.
194 static Address c_functions_[cfunction_count];
195
196 // Note: These are always Code objects, but to conform with
197 // IterateBuiltins() above which assumes Object**'s for the callback
198 // function f, we use an Object* array here.
199 static Object* builtins_[builtin_count];
200 static const char* names_[builtin_count];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 static const char* javascript_names_[id_count];
202 static int javascript_argc_[id_count];
203
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000204 static void Generate_Adaptor(MacroAssembler* masm, CFunctionId id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 static void Generate_JSConstructCall(MacroAssembler* masm);
206 static void Generate_JSEntryTrampoline(MacroAssembler* masm);
207 static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
208 static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000209
210 static void Generate_FunctionCall(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 static void Generate_FunctionApply(MacroAssembler* masm);
212
213 static void Generate_LoadIC_DebugBreak(MacroAssembler* masm);
214 static void Generate_StoreIC_DebugBreak(MacroAssembler* masm);
215 static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm);
216 static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm);
217 static void Generate_ConstructCall_DebugBreak(MacroAssembler* masm);
218 static void Generate_Return_DebugBreak(MacroAssembler* masm);
219 static void Generate_Return_DebugBreakEntry(MacroAssembler* masm);
220 static void Generate_StubNoRegisters_DebugBreak(MacroAssembler* masm);
221};
222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223} } // namespace v8::internal
224
225#endif // V8_BUILTINS_H_