V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
builtins.h
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/globals.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 class ByteArray;
16 class Callable;
17 template <typename T>
18 class Handle;
19 class Isolate;
20 
21 // Forward declarations.
22 class BailoutId;
23 class RootVisitor;
24 enum class InterpreterPushArgsMode : unsigned;
25 namespace compiler {
26 class CodeAssemblerState;
27 }
28 
29 template <typename T>
30 static constexpr T FirstFromVarArgs(T x, ...) noexcept {
31  return x;
32 }
33 
34 // Convenience macro to avoid generating named accessors for all builtins.
35 #define BUILTIN_CODE(isolate, name) \
36  (isolate)->builtins()->builtin_handle(Builtins::k##name)
37 
38 class Builtins {
39  public:
40  explicit Builtins(Isolate* isolate) : isolate_(isolate) {}
41 
42  void TearDown();
43 
44  // Disassembler support.
45  const char* Lookup(Address pc);
46 
47  enum Name : int32_t {
48 #define DEF_ENUM(Name, ...) k##Name,
49  BUILTIN_LIST(DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM,
50  DEF_ENUM, DEF_ENUM)
51 #undef DEF_ENUM
52  builtin_count,
53 
54 #define EXTRACT_NAME(Name, ...) k##Name,
55  // Define kFirstBytecodeHandler,
56  kFirstBytecodeHandler =
57  FirstFromVarArgs(BUILTIN_LIST_BYTECODE_HANDLERS(EXTRACT_NAME) 0)
58 #undef EXTRACT_NAME
59  };
60 
61  static const int32_t kNoBuiltinId = -1;
62 
63  static constexpr bool IsBuiltinId(int maybe_id) {
64  return 0 <= maybe_id && maybe_id < builtin_count;
65  }
66 
67  // The different builtin kinds are documented in builtins-definitions.h.
68  enum Kind { CPP, API, TFJ, TFC, TFS, TFH, BCH, ASM };
69 
70  static BailoutId GetContinuationBailoutId(Name name);
71  static Name GetBuiltinFromBailoutId(BailoutId);
72 
73  // Convenience wrappers.
74  Handle<Code> CallFunction(ConvertReceiverMode = ConvertReceiverMode::kAny);
75  Handle<Code> Call(ConvertReceiverMode = ConvertReceiverMode::kAny);
76  Handle<Code> NonPrimitiveToPrimitive(
77  ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
78  Handle<Code> OrdinaryToPrimitive(OrdinaryToPrimitiveHint hint);
79  Handle<Code> InterpreterPushArgsThenCall(ConvertReceiverMode receiver_mode,
80  InterpreterPushArgsMode mode);
81  Handle<Code> InterpreterPushArgsThenConstruct(InterpreterPushArgsMode mode);
82  Handle<Code> NewFunctionContext(ScopeType scope_type);
83  Handle<Code> JSConstructStubGeneric();
84 
85  // Used by CreateOffHeapTrampolines in isolate.cc.
86  void set_builtin(int index, Code builtin);
87 
88  Code builtin(int index);
89  V8_EXPORT_PRIVATE Handle<Code> builtin_handle(int index);
90 
91  V8_EXPORT_PRIVATE static Callable CallableFor(Isolate* isolate, Name name);
92 
93  static int GetStackParameterCount(Name name);
94 
95  static const char* name(int index);
96 
97  // Support for --print-builtin-size and --print-builtin-code.
98  void PrintBuiltinCode();
99  void PrintBuiltinSize();
100 
101  // Returns the C++ entry point for builtins implemented in C++, and the null
102  // Address otherwise.
103  static Address CppEntryOf(int index);
104 
105  static Kind KindOf(int index);
106  static const char* KindNameOf(int index);
107 
108  static bool IsCpp(int index);
109  static bool HasCppImplementation(int index);
110 
111  // True, iff the given code object is a builtin. Note that this does not
112  // necessarily mean that its kind is Code::BUILTIN.
113  static bool IsBuiltin(const Code code);
114 
115  // As above, but safe to access off the main thread since the check is done
116  // by handle location. Similar to Heap::IsRootHandle.
117  bool IsBuiltinHandle(Handle<HeapObject> maybe_code, int* index) const;
118 
119  // True, iff the given code object is a builtin with off-heap embedded code.
120  static bool IsIsolateIndependentBuiltin(const Code code);
121 
122  static constexpr int kFirstWideBytecodeHandler =
123  kFirstBytecodeHandler + kNumberOfBytecodeHandlers;
124  static constexpr int kFirstExtraWideBytecodeHandler =
125  kFirstWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
126  STATIC_ASSERT(kFirstExtraWideBytecodeHandler +
127  kNumberOfWideBytecodeHandlers ==
128  builtin_count);
129 
130  // True, iff the given builtin contains no isolate-specific code and can be
131  // embedded into the binary.
132  static bool IsIsolateIndependent(int index) { return true; }
133 
134  // Wasm runtime stubs are treated specially by wasm. To guarantee reachability
135  // through near jumps, their code is completely copied into a fresh off-heap
136  // area.
137  static bool IsWasmRuntimeStub(int index);
138 
139  bool is_initialized() const { return initialized_; }
140 
141  // Used by SetupIsolateDelegate and Deserializer.
142  void MarkInitialized() {
143  DCHECK(!initialized_);
144  initialized_ = true;
145  }
146 
147  V8_WARN_UNUSED_RESULT static MaybeHandle<Object> InvokeApiFunction(
148  Isolate* isolate, bool is_construct, Handle<HeapObject> function,
149  Handle<Object> receiver, int argc, Handle<Object> args[],
150  Handle<HeapObject> new_target);
151 
152  enum ExitFrameType { EXIT, BUILTIN_EXIT };
153 
154  static void Generate_Adaptor(MacroAssembler* masm, Address builtin_address,
155  ExitFrameType exit_frame_type);
156 
157  static void Generate_CEntry(MacroAssembler* masm, int result_size,
158  SaveFPRegsMode save_doubles, ArgvMode argv_mode,
159  bool builtin_exit_frame);
160 
161  static bool AllowDynamicFunction(Isolate* isolate, Handle<JSFunction> target,
162  Handle<JSObject> target_global_proxy);
163 
164  // Creates a trampoline code object that jumps to the given off-heap entry.
165  // The result should not be used directly, but only from the related Factory
166  // function.
167  static Handle<Code> GenerateOffHeapTrampolineFor(Isolate* isolate,
168  Address off_heap_entry);
169 
170  // Generate the RelocInfo ByteArray that would be generated for an offheap
171  // trampoline.
172  static Handle<ByteArray> GenerateOffHeapTrampolineRelocInfo(Isolate* isolate);
173 
174  private:
175  static void Generate_CallFunction(MacroAssembler* masm,
176  ConvertReceiverMode mode);
177 
178  static void Generate_CallBoundFunctionImpl(MacroAssembler* masm);
179 
180  static void Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode);
181 
182  enum class CallOrConstructMode { kCall, kConstruct };
183  static void Generate_CallOrConstructVarargs(MacroAssembler* masm,
184  Handle<Code> code);
185  static void Generate_CallOrConstructForwardVarargs(MacroAssembler* masm,
186  CallOrConstructMode mode,
187  Handle<Code> code);
188 
189  static void Generate_InterpreterPushArgsThenCallImpl(
190  MacroAssembler* masm, ConvertReceiverMode receiver_mode,
191  InterpreterPushArgsMode mode);
192 
193  static void Generate_InterpreterPushArgsThenConstructImpl(
194  MacroAssembler* masm, InterpreterPushArgsMode mode);
195 
196 #define DECLARE_ASM(Name, ...) \
197  static void Generate_##Name(MacroAssembler* masm);
198 #define DECLARE_TF(Name, ...) \
199  static void Generate_##Name(compiler::CodeAssemblerState* state);
200 
201  BUILTIN_LIST(IGNORE_BUILTIN, IGNORE_BUILTIN, DECLARE_TF, DECLARE_TF,
202  DECLARE_TF, DECLARE_TF, IGNORE_BUILTIN, DECLARE_ASM)
203 
204 #undef DECLARE_ASM
205 #undef DECLARE_TF
206 
207  Isolate* isolate_;
208  bool initialized_ = false;
209 
210  friend class SetupIsolateDelegate;
211 
212  DISALLOW_COPY_AND_ASSIGN(Builtins);
213 };
214 
215 Builtins::Name ExampleBuiltinForTorqueFunctionPointerType(
216  size_t function_pointer_type_id);
217 
218 } // namespace internal
219 } // namespace v8
220 
221 #endif // V8_BUILTINS_BUILTINS_H_
Definition: libplatform.h:13