• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef V8_BUILTINS_BUILTINS_H_
6 #define V8_BUILTINS_BUILTINS_H_
7 
8 #include "src/base/flags.h"
9 #include "src/builtins/builtins-definitions.h"
10 #include "src/common/globals.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 class ByteArray;
16 class CallInterfaceDescriptor;
17 class Callable;
18 template <typename T>
19 class Handle;
20 class Isolate;
21 
22 // Forward declarations.
23 class BailoutId;
24 class RootVisitor;
25 enum class InterpreterPushArgsMode : unsigned;
26 namespace compiler {
27 class CodeAssemblerState;
28 }  // namespace compiler
29 
30 template <typename T>
FirstFromVarArgs(T x,...)31 static constexpr T FirstFromVarArgs(T x, ...) noexcept {
32   return x;
33 }
34 
35 // Convenience macro to avoid generating named accessors for all builtins.
36 #define BUILTIN_CODE(isolate, name) \
37   (isolate)->builtins()->builtin_handle(Builtins::k##name)
38 
39 class Builtins {
40  public:
Builtins(Isolate * isolate)41   explicit Builtins(Isolate* isolate) : isolate_(isolate) {}
42 
43   void TearDown();
44 
45   // Disassembler support.
46   const char* Lookup(Address pc);
47 
48   enum Name : int32_t {
49 #define DEF_ENUM(Name, ...) k##Name,
50     BUILTIN_LIST(DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM,
51                  DEF_ENUM)
52 #undef DEF_ENUM
53         builtin_count,
54 
55 #define EXTRACT_NAME(Name, ...) k##Name,
56     // Define kFirstBytecodeHandler,
57     kFirstBytecodeHandler =
58         FirstFromVarArgs(BUILTIN_LIST_BYTECODE_HANDLERS(EXTRACT_NAME) 0)
59 #undef EXTRACT_NAME
60   };
61 
62   static const int32_t kNoBuiltinId = -1;
63 
64   static constexpr int kFirstWideBytecodeHandler =
65       kFirstBytecodeHandler + kNumberOfBytecodeHandlers;
66   static constexpr int kFirstExtraWideBytecodeHandler =
67       kFirstWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
68   static constexpr int kLastBytecodeHandlerPlusOne =
69       kFirstExtraWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
70   STATIC_ASSERT(kLastBytecodeHandlerPlusOne == builtin_count);
71 
IsBuiltinId(int maybe_id)72   static constexpr bool IsBuiltinId(int maybe_id) {
73     return 0 <= maybe_id && maybe_id < builtin_count;
74   }
75 
76   // The different builtin kinds are documented in builtins-definitions.h.
77   enum Kind { CPP, TFJ, TFC, TFS, TFH, BCH, ASM };
78 
79   static BailoutId GetContinuationBailoutId(Name name);
80   static Name GetBuiltinFromBailoutId(BailoutId);
81 
82   // Convenience wrappers.
83   Handle<Code> CallFunction(ConvertReceiverMode = ConvertReceiverMode::kAny);
84   Handle<Code> Call(ConvertReceiverMode = ConvertReceiverMode::kAny);
85   Handle<Code> NonPrimitiveToPrimitive(
86       ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
87   Handle<Code> OrdinaryToPrimitive(OrdinaryToPrimitiveHint hint);
88   Handle<Code> JSConstructStubGeneric();
89 
90   // Used by CreateOffHeapTrampolines in isolate.cc.
91   void set_builtin(int index, Code builtin);
92 
93   V8_EXPORT_PRIVATE Code builtin(int index);
94   V8_EXPORT_PRIVATE Handle<Code> builtin_handle(int index);
95 
96   static CallInterfaceDescriptor CallInterfaceDescriptorFor(Name name);
97   V8_EXPORT_PRIVATE static Callable CallableFor(Isolate* isolate, Name name);
98   static bool HasJSLinkage(int index);
99 
100   static int GetStackParameterCount(Name name);
101 
102   static const char* name(int index);
103 
104   // Support for --print-builtin-size and --print-builtin-code.
105   void PrintBuiltinCode();
106   void PrintBuiltinSize();
107 
108   // Returns the C++ entry point for builtins implemented in C++, and the null
109   // Address otherwise.
110   static Address CppEntryOf(int index);
111 
112   static Kind KindOf(int index);
113   static const char* KindNameOf(int index);
114 
115   static bool IsCpp(int index);
116 
117   // True, iff the given code object is a builtin. Note that this does not
118   // necessarily mean that its kind is Code::BUILTIN.
119   static bool IsBuiltin(const Code code);
120 
121   // As above, but safe to access off the main thread since the check is done
122   // by handle location. Similar to Heap::IsRootHandle.
123   bool IsBuiltinHandle(Handle<HeapObject> maybe_code, int* index) const;
124 
125   // True, iff the given code object is a builtin with off-heap embedded code.
126   static bool IsIsolateIndependentBuiltin(const Code code);
127 
128   // True, iff the given builtin contains no isolate-specific code and can be
129   // embedded into the binary.
130   static constexpr bool kAllBuiltinsAreIsolateIndependent = true;
AllBuiltinsAreIsolateIndependent()131   static constexpr bool AllBuiltinsAreIsolateIndependent() {
132     return kAllBuiltinsAreIsolateIndependent;
133   }
IsIsolateIndependent(int index)134   static constexpr bool IsIsolateIndependent(int index) {
135     STATIC_ASSERT(kAllBuiltinsAreIsolateIndependent);
136     return kAllBuiltinsAreIsolateIndependent;
137   }
138 
139   // Initializes the table of builtin entry points based on the current contents
140   // of the builtins table.
141   static void InitializeBuiltinEntryTable(Isolate* isolate);
142 
143   // Emits a CodeCreateEvent for every builtin.
144   static void EmitCodeCreateEvents(Isolate* isolate);
145 
is_initialized()146   bool is_initialized() const { return initialized_; }
147 
148   // Used by SetupIsolateDelegate and Deserializer.
MarkInitialized()149   void MarkInitialized() {
150     DCHECK(!initialized_);
151     initialized_ = true;
152   }
153 
154   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> InvokeApiFunction(
155       Isolate* isolate, bool is_construct, Handle<HeapObject> function,
156       Handle<Object> receiver, int argc, Handle<Object> args[],
157       Handle<HeapObject> new_target);
158 
159   static void Generate_Adaptor(MacroAssembler* masm, Address builtin_address);
160 
161   static void Generate_CEntry(MacroAssembler* masm, int result_size,
162                               SaveFPRegsMode save_doubles, ArgvMode argv_mode,
163                               bool builtin_exit_frame);
164 
165   static bool AllowDynamicFunction(Isolate* isolate, Handle<JSFunction> target,
166                                    Handle<JSObject> target_global_proxy);
167 
168   // Creates a trampoline code object that jumps to the given off-heap entry.
169   // The result should not be used directly, but only from the related Factory
170   // function.
171   // TODO(delphick): Come up with a better name since it may not generate an
172   // executable trampoline.
173   static Handle<Code> GenerateOffHeapTrampolineFor(
174       Isolate* isolate, Address off_heap_entry, int32_t kind_specific_flags,
175       bool generate_jump_to_instruction_stream);
176 
177   // Generate the RelocInfo ByteArray that would be generated for an offheap
178   // trampoline.
179   static Handle<ByteArray> GenerateOffHeapTrampolineRelocInfo(Isolate* isolate);
180 
181   // Only builtins with JS linkage should ever need to be called via their
182   // trampoline Code object. The remaining builtins have non-executable Code
183   // objects.
184   static bool CodeObjectIsExecutable(int builtin_index);
185 
IsJSEntryVariant(int builtin_index)186   static bool IsJSEntryVariant(int builtin_index) {
187     switch (builtin_index) {
188       case kJSEntry:
189       case kJSConstructEntry:
190       case kJSRunMicrotasksEntry:
191         return true;
192       default:
193         return false;
194     }
195     UNREACHABLE();
196   }
197 
js_entry_handler_offset()198   int js_entry_handler_offset() const {
199     DCHECK_NE(js_entry_handler_offset_, 0);
200     return js_entry_handler_offset_;
201   }
202 
SetJSEntryHandlerOffset(int offset)203   void SetJSEntryHandlerOffset(int offset) {
204     // Check the stored offset is either uninitialized or unchanged (we
205     // generate multiple variants of this builtin but they should all have the
206     // same handler offset).
207     CHECK(js_entry_handler_offset_ == 0 || js_entry_handler_offset_ == offset);
208     js_entry_handler_offset_ = offset;
209   }
210 
211  private:
212   static void Generate_CallFunction(MacroAssembler* masm,
213                                     ConvertReceiverMode mode);
214 
215   static void Generate_CallBoundFunctionImpl(MacroAssembler* masm);
216 
217   static void Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode);
218 
219   enum class CallOrConstructMode { kCall, kConstruct };
220   static void Generate_CallOrConstructVarargs(MacroAssembler* masm,
221                                               Handle<Code> code);
222   static void Generate_CallOrConstructForwardVarargs(MacroAssembler* masm,
223                                                      CallOrConstructMode mode,
224                                                      Handle<Code> code);
225 
226   static void Generate_InterpreterPushArgsThenCallImpl(
227       MacroAssembler* masm, ConvertReceiverMode receiver_mode,
228       InterpreterPushArgsMode mode);
229 
230   static void Generate_InterpreterPushArgsThenConstructImpl(
231       MacroAssembler* masm, InterpreterPushArgsMode mode);
232 
233 #define DECLARE_ASM(Name, ...) \
234   static void Generate_##Name(MacroAssembler* masm);
235 #define DECLARE_TF(Name, ...) \
236   static void Generate_##Name(compiler::CodeAssemblerState* state);
237 
238   BUILTIN_LIST(IGNORE_BUILTIN, DECLARE_TF, DECLARE_TF, DECLARE_TF, DECLARE_TF,
239                IGNORE_BUILTIN, DECLARE_ASM)
240 
241 #undef DECLARE_ASM
242 #undef DECLARE_TF
243 
244   Isolate* isolate_;
245   bool initialized_ = false;
246 
247   // Stores the offset of exception handler entry point (the handler_entry
248   // label) in JSEntry and its variants. It's used to generate the handler table
249   // during codegen (mksnapshot-only).
250   int js_entry_handler_offset_ = 0;
251 
252   friend class SetupIsolateDelegate;
253 
254   DISALLOW_COPY_AND_ASSIGN(Builtins);
255 };
256 
257 Builtins::Name ExampleBuiltinForTorqueFunctionPointerType(
258     size_t function_pointer_type_id);
259 
260 }  // namespace internal
261 }  // namespace v8
262 
263 #endif  // V8_BUILTINS_BUILTINS_H_
264