1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "src/handles.h"
13 // Specifies extra arguments required by a C++ builtin.
14 enum BuiltinExtraArguments {
15 NO_EXTRA_ARGUMENTS = 0,
16 NEEDS_CALLED_FUNCTION = 1
20 #define CODE_AGE_LIST_WITH_ARG(V, A) \
21 V(Quadragenarian, A) \
22 V(Quinquagenarian, A) \
24 V(Septuagenarian, A) \
27 #define CODE_AGE_LIST_IGNORE_ARG(X, V) V(X)
29 #define CODE_AGE_LIST(V) \
30 CODE_AGE_LIST_WITH_ARG(CODE_AGE_LIST_IGNORE_ARG, V)
32 #define CODE_AGE_LIST_COMPLETE(V) \
37 CODE_AGE_LIST_WITH_ARG(CODE_AGE_LIST_IGNORE_ARG, V)
39 #define DECLARE_CODE_AGE_BUILTIN(C, V) \
40 V(Make##C##CodeYoungAgainOddMarking, BUILTIN, \
41 UNINITIALIZED, kNoExtraICState) \
42 V(Make##C##CodeYoungAgainEvenMarking, BUILTIN, \
43 UNINITIALIZED, kNoExtraICState)
46 // Define list of builtins implemented in C++.
47 #define BUILTIN_LIST_C(V) \
48 V(Illegal, NO_EXTRA_ARGUMENTS) \
50 V(EmptyFunction, NO_EXTRA_ARGUMENTS) \
52 V(ArrayPush, NO_EXTRA_ARGUMENTS) \
53 V(ArrayPop, NO_EXTRA_ARGUMENTS) \
54 V(ArrayShift, NO_EXTRA_ARGUMENTS) \
55 V(ArrayUnshift, NO_EXTRA_ARGUMENTS) \
56 V(ArraySlice, NO_EXTRA_ARGUMENTS) \
57 V(ArraySplice, NO_EXTRA_ARGUMENTS) \
58 V(ArrayConcat, NO_EXTRA_ARGUMENTS) \
60 V(HandleApiCall, NEEDS_CALLED_FUNCTION) \
61 V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION) \
62 V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS) \
63 V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS) \
65 V(RestrictedFunctionPropertiesThrower, NO_EXTRA_ARGUMENTS) \
66 V(RestrictedStrictArgumentsPropertiesThrower, NO_EXTRA_ARGUMENTS)
68 // Define list of builtins implemented in assembly.
69 #define BUILTIN_LIST_A(V) \
70 V(ArgumentsAdaptorTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState) \
71 V(InOptimizationQueue, BUILTIN, UNINITIALIZED, kNoExtraICState) \
72 V(JSConstructStubGeneric, BUILTIN, UNINITIALIZED, kNoExtraICState) \
73 V(JSConstructStubForDerived, BUILTIN, UNINITIALIZED, kNoExtraICState) \
74 V(JSConstructStubApi, BUILTIN, UNINITIALIZED, kNoExtraICState) \
75 V(JSEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState) \
76 V(JSConstructEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState) \
77 V(InterpreterEntryTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState) \
78 V(InterpreterExitTrampoline, BUILTIN, UNINITIALIZED, kNoExtraICState) \
79 V(CompileLazy, BUILTIN, UNINITIALIZED, kNoExtraICState) \
80 V(CompileOptimized, BUILTIN, UNINITIALIZED, kNoExtraICState) \
81 V(CompileOptimizedConcurrent, BUILTIN, UNINITIALIZED, kNoExtraICState) \
82 V(NotifyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState) \
83 V(NotifySoftDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState) \
84 V(NotifyLazyDeoptimized, BUILTIN, UNINITIALIZED, kNoExtraICState) \
85 V(NotifyStubFailure, BUILTIN, UNINITIALIZED, kNoExtraICState) \
86 V(NotifyStubFailureSaveDoubles, BUILTIN, UNINITIALIZED, kNoExtraICState) \
88 V(LoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState) \
89 V(KeyedLoadIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState) \
90 V(StoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState) \
91 V(KeyedStoreIC_Miss, BUILTIN, UNINITIALIZED, kNoExtraICState) \
92 V(LoadIC_Getter_ForDeopt, LOAD_IC, MONOMORPHIC, kNoExtraICState) \
93 V(KeyedLoadIC_Megamorphic, KEYED_LOAD_IC, MEGAMORPHIC, kNoExtraICState) \
95 V(KeyedLoadIC_Megamorphic_Strong, KEYED_LOAD_IC, MEGAMORPHIC, \
96 LoadICState::kStrongModeState) \
98 V(StoreIC_Setter_ForDeopt, STORE_IC, MONOMORPHIC, \
99 StoreICState::kStrictModeState) \
101 V(KeyedStoreIC_Initialize, KEYED_STORE_IC, UNINITIALIZED, kNoExtraICState) \
102 V(KeyedStoreIC_PreMonomorphic, KEYED_STORE_IC, PREMONOMORPHIC, \
104 V(KeyedStoreIC_Megamorphic, KEYED_STORE_IC, MEGAMORPHIC, kNoExtraICState) \
106 V(KeyedStoreIC_Initialize_Strict, KEYED_STORE_IC, UNINITIALIZED, \
107 StoreICState::kStrictModeState) \
108 V(KeyedStoreIC_PreMonomorphic_Strict, KEYED_STORE_IC, PREMONOMORPHIC, \
109 StoreICState::kStrictModeState) \
110 V(KeyedStoreIC_Megamorphic_Strict, KEYED_STORE_IC, MEGAMORPHIC, \
111 StoreICState::kStrictModeState) \
113 V(FunctionCall, BUILTIN, UNINITIALIZED, kNoExtraICState) \
114 V(FunctionApply, BUILTIN, UNINITIALIZED, kNoExtraICState) \
115 V(ReflectApply, BUILTIN, UNINITIALIZED, kNoExtraICState) \
116 V(ReflectConstruct, BUILTIN, UNINITIALIZED, kNoExtraICState) \
118 V(InternalArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState) \
119 V(ArrayCode, BUILTIN, UNINITIALIZED, kNoExtraICState) \
121 V(StringConstructCode, BUILTIN, UNINITIALIZED, kNoExtraICState) \
123 V(OnStackReplacement, BUILTIN, UNINITIALIZED, kNoExtraICState) \
124 V(InterruptCheck, BUILTIN, UNINITIALIZED, kNoExtraICState) \
125 V(OsrAfterStackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState) \
126 V(StackCheck, BUILTIN, UNINITIALIZED, kNoExtraICState) \
128 V(MarkCodeAsToBeExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState) \
129 V(MarkCodeAsExecutedOnce, BUILTIN, UNINITIALIZED, kNoExtraICState) \
130 V(MarkCodeAsExecutedTwice, BUILTIN, UNINITIALIZED, kNoExtraICState) \
131 CODE_AGE_LIST_WITH_ARG(DECLARE_CODE_AGE_BUILTIN, V)
133 // Define list of builtin handlers implemented in assembly.
134 #define BUILTIN_LIST_H(V) \
135 V(LoadIC_Slow, LOAD_IC) \
136 V(LoadIC_Slow_Strong, LOAD_IC) \
137 V(KeyedLoadIC_Slow, KEYED_LOAD_IC) \
138 V(KeyedLoadIC_Slow_Strong, KEYED_LOAD_IC) \
139 V(StoreIC_Slow, STORE_IC) \
140 V(KeyedStoreIC_Slow, KEYED_STORE_IC) \
141 V(LoadIC_Normal, LOAD_IC) \
142 V(LoadIC_Normal_Strong, LOAD_IC) \
143 V(StoreIC_Normal, STORE_IC)
145 // Define list of builtins used by the debugger implemented in assembly.
146 #define BUILTIN_LIST_DEBUG_A(V) \
147 V(Return_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState) \
148 V(Slot_DebugBreak, BUILTIN, DEBUG_STUB, kNoExtraICState) \
149 V(PlainReturn_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState) \
150 V(FrameDropper_LiveEdit, BUILTIN, DEBUG_STUB, kNoExtraICState)
153 class BuiltinFunctionTable;
161 // Generate all builtin code objects. Should be called once during
162 // isolate initialization.
163 void SetUp(Isolate* isolate, bool create_heap_objects);
166 // Garbage collection support.
167 void IterateBuiltins(ObjectVisitor* v);
169 // Disassembler support.
170 const char* Lookup(byte* pc);
173 #define DEF_ENUM_C(name, ignore) k##name,
174 #define DEF_ENUM_A(name, kind, state, extra) k##name,
175 #define DEF_ENUM_H(name, kind) k##name,
176 BUILTIN_LIST_C(DEF_ENUM_C)
177 BUILTIN_LIST_A(DEF_ENUM_A)
178 BUILTIN_LIST_H(DEF_ENUM_H)
179 BUILTIN_LIST_DEBUG_A(DEF_ENUM_A)
186 #define DEF_ENUM_C(name, ignore) c_##name,
187 BUILTIN_LIST_C(DEF_ENUM_C)
192 #define DECLARE_BUILTIN_ACCESSOR_C(name, ignore) Handle<Code> name();
193 #define DECLARE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
195 #define DECLARE_BUILTIN_ACCESSOR_H(name, kind) Handle<Code> name();
196 BUILTIN_LIST_C(DECLARE_BUILTIN_ACCESSOR_C)
197 BUILTIN_LIST_A(DECLARE_BUILTIN_ACCESSOR_A)
198 BUILTIN_LIST_H(DECLARE_BUILTIN_ACCESSOR_H)
199 BUILTIN_LIST_DEBUG_A(DECLARE_BUILTIN_ACCESSOR_A)
200 #undef DECLARE_BUILTIN_ACCESSOR_C
201 #undef DECLARE_BUILTIN_ACCESSOR_A
203 Code* builtin(Name name) {
204 // Code::cast cannot be used here since we access builtins
205 // during the marking phase of mark sweep. See IC::Clear.
206 return reinterpret_cast<Code*>(builtins_[name]);
209 Address builtin_address(Name name) {
210 return reinterpret_cast<Address>(&builtins_[name]);
213 static Address c_function_address(CFunctionId id) {
214 return c_functions_[id];
217 const char* name(int index) {
219 DCHECK(index < builtin_count);
220 return names_[index];
223 bool is_initialized() const { return initialized_; }
225 MUST_USE_RESULT static MaybeHandle<Object> InvokeApiFunction(
226 Handle<JSFunction> function, Handle<Object> receiver, int argc,
227 Handle<Object> args[]);
232 // The external C++ functions called from the code.
233 static Address const c_functions_[cfunction_count];
235 // Note: These are always Code objects, but to conform with
236 // IterateBuiltins() above which assumes Object**'s for the callback
237 // function f, we use an Object* array here.
238 Object* builtins_[builtin_count];
239 const char* names_[builtin_count];
241 static void Generate_Adaptor(MacroAssembler* masm,
243 BuiltinExtraArguments extra_args);
244 static void Generate_CompileLazy(MacroAssembler* masm);
245 static void Generate_InOptimizationQueue(MacroAssembler* masm);
246 static void Generate_CompileOptimized(MacroAssembler* masm);
247 static void Generate_CompileOptimizedConcurrent(MacroAssembler* masm);
248 static void Generate_JSConstructStubGeneric(MacroAssembler* masm);
249 static void Generate_JSConstructStubForDerived(MacroAssembler* masm);
250 static void Generate_JSConstructStubApi(MacroAssembler* masm);
251 static void Generate_JSEntryTrampoline(MacroAssembler* masm);
252 static void Generate_JSConstructEntryTrampoline(MacroAssembler* masm);
253 static void Generate_InterpreterEntryTrampoline(MacroAssembler* masm);
254 static void Generate_InterpreterExitTrampoline(MacroAssembler* masm);
255 static void Generate_NotifyDeoptimized(MacroAssembler* masm);
256 static void Generate_NotifySoftDeoptimized(MacroAssembler* masm);
257 static void Generate_NotifyLazyDeoptimized(MacroAssembler* masm);
258 static void Generate_NotifyStubFailure(MacroAssembler* masm);
259 static void Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm);
260 static void Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm);
262 static void Generate_FunctionCall(MacroAssembler* masm);
263 static void Generate_FunctionApply(MacroAssembler* masm);
264 static void Generate_ReflectApply(MacroAssembler* masm);
265 static void Generate_ReflectConstruct(MacroAssembler* masm);
267 static void Generate_InternalArrayCode(MacroAssembler* masm);
268 static void Generate_ArrayCode(MacroAssembler* masm);
270 static void Generate_StringConstructCode(MacroAssembler* masm);
271 static void Generate_OnStackReplacement(MacroAssembler* masm);
272 static void Generate_OsrAfterStackCheck(MacroAssembler* masm);
273 static void Generate_InterruptCheck(MacroAssembler* masm);
274 static void Generate_StackCheck(MacroAssembler* masm);
276 #define DECLARE_CODE_AGE_BUILTIN_GENERATOR(C) \
277 static void Generate_Make##C##CodeYoungAgainEvenMarking( \
278 MacroAssembler* masm); \
279 static void Generate_Make##C##CodeYoungAgainOddMarking( \
280 MacroAssembler* masm);
281 CODE_AGE_LIST(DECLARE_CODE_AGE_BUILTIN_GENERATOR)
282 #undef DECLARE_CODE_AGE_BUILTIN_GENERATOR
284 static void Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm);
285 static void Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm);
286 static void Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm);
288 static void InitBuiltinFunctionTable();
292 friend class BuiltinFunctionTable;
293 friend class Isolate;
295 DISALLOW_COPY_AND_ASSIGN(Builtins);
298 } } // namespace v8::internal
300 #endif // V8_BUILTINS_H_