builtins.h 8.97 KB
Newer Older
danno@chromium.org's avatar
danno@chromium.org committed
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_BUILTINS_BUILTINS_H_
#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 13
namespace v8 {
namespace internal {
14

15
class ByteArray;
16
class CallInterfaceDescriptor;
17
class Callable;
marja's avatar
marja committed
18 19 20 21
template <typename T>
class Handle;
class Isolate;

22
// Forward declarations.
23
class BailoutId;
24
class RootVisitor;
25
enum class InterpreterPushArgsMode : unsigned;
26 27
namespace compiler {
class CodeAssemblerState;
28
}  // namespace compiler
29

30 31 32 33 34
template <typename T>
static constexpr T FirstFromVarArgs(T x, ...) noexcept {
  return x;
}

35 36 37 38
// Convenience macro to avoid generating named accessors for all builtins.
#define BUILTIN_CODE(isolate, name) \
  (isolate)->builtins()->builtin_handle(Builtins::k##name)

39
class Builtins {
40
 public:
41
  explicit Builtins(Isolate* isolate) : isolate_(isolate) {}
42

43 44 45
  Builtins(const Builtins&) = delete;
  Builtins& operator=(const Builtins&) = delete;

46
  void TearDown();
47 48

  // Disassembler support.
49
  const char* Lookup(Address pc);
50

51
  enum Name : int32_t {
52
#define DEF_ENUM(Name, ...) k##Name,
53
    BUILTIN_LIST(DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM, DEF_ENUM,
54
                 DEF_ENUM)
55
#undef DEF_ENUM
56 57
        builtin_count,

58
#define EXTRACT_NAME(Name, ...) k##Name,
59 60 61 62
    // Define kFirstBytecodeHandler,
    kFirstBytecodeHandler =
        FirstFromVarArgs(BUILTIN_LIST_BYTECODE_HANDLERS(EXTRACT_NAME) 0)
#undef EXTRACT_NAME
63 64
  };

65 66
  static const int32_t kNoBuiltinId = -1;

67 68 69 70 71 72 73 74
  static constexpr int kFirstWideBytecodeHandler =
      kFirstBytecodeHandler + kNumberOfBytecodeHandlers;
  static constexpr int kFirstExtraWideBytecodeHandler =
      kFirstWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
  static constexpr int kLastBytecodeHandlerPlusOne =
      kFirstExtraWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
  STATIC_ASSERT(kLastBytecodeHandlerPlusOne == builtin_count);

75
  static constexpr bool IsBuiltinId(int maybe_id) {
76 77 78
    return 0 <= maybe_id && maybe_id < builtin_count;
  }

79
  // The different builtin kinds are documented in builtins-definitions.h.
80
  enum Kind { CPP, TFJ, TFC, TFS, TFH, BCH, ASM };
81

82 83 84
  static BailoutId GetContinuationBailoutId(Name name);
  static Name GetBuiltinFromBailoutId(BailoutId);

85
  // Convenience wrappers.
86 87
  Handle<Code> CallFunction(ConvertReceiverMode = ConvertReceiverMode::kAny);
  Handle<Code> Call(ConvertReceiverMode = ConvertReceiverMode::kAny);
88 89 90
  Handle<Code> NonPrimitiveToPrimitive(
      ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
  Handle<Code> OrdinaryToPrimitive(OrdinaryToPrimitiveHint hint);
91
  Handle<Code> JSConstructStubGeneric();
92

93
  // Used by CreateOffHeapTrampolines in isolate.cc.
94
  void set_builtin(int index, Code builtin);
95

96
  V8_EXPORT_PRIVATE Code builtin(int index);
97
  V8_EXPORT_PRIVATE Handle<Code> builtin_handle(int index);
98

99
  static CallInterfaceDescriptor CallInterfaceDescriptorFor(Name name);
100
  V8_EXPORT_PRIVATE static Callable CallableFor(Isolate* isolate, Name name);
101
  static bool HasJSLinkage(int index);
102

103
  static int GetStackParameterCount(Name name);
104

105 106
  static const char* name(int index);

107 108 109 110
  // Support for --print-builtin-size and --print-builtin-code.
  void PrintBuiltinCode();
  void PrintBuiltinSize();

111 112 113 114
  // Returns the C++ entry point for builtins implemented in C++, and the null
  // Address otherwise.
  static Address CppEntryOf(int index);

115 116 117
  static Kind KindOf(int index);
  static const char* KindNameOf(int index);

118
  static bool IsCpp(int index);
119

120 121
  // True, iff the given code object is a builtin. Note that this does not
  // necessarily mean that its kind is Code::BUILTIN.
122
  static bool IsBuiltin(const Code code);
123

124 125
  // As above, but safe to access off the main thread since the check is done
  // by handle location. Similar to Heap::IsRootHandle.
126
  bool IsBuiltinHandle(Handle<HeapObject> maybe_code, int* index) const;
127

128
  // True, iff the given code object is a builtin with off-heap embedded code.
129
  static bool IsIsolateIndependentBuiltin(const Code code);
130

131 132
  // True, iff the given builtin contains no isolate-specific code and can be
  // embedded into the binary.
133 134 135 136 137 138 139 140
  static constexpr bool kAllBuiltinsAreIsolateIndependent = true;
  static constexpr bool AllBuiltinsAreIsolateIndependent() {
    return kAllBuiltinsAreIsolateIndependent;
  }
  static constexpr bool IsIsolateIndependent(int index) {
    STATIC_ASSERT(kAllBuiltinsAreIsolateIndependent);
    return kAllBuiltinsAreIsolateIndependent;
  }
141

142 143 144
  // Initializes the table of builtin entry points based on the current contents
  // of the builtins table.
  static void InitializeBuiltinEntryTable(Isolate* isolate);
145

146 147 148
  // Emits a CodeCreateEvent for every builtin.
  static void EmitCodeCreateEvents(Isolate* isolate);

149 150
  bool is_initialized() const { return initialized_; }

151 152 153 154 155 156
  // Used by SetupIsolateDelegate and Deserializer.
  void MarkInitialized() {
    DCHECK(!initialized_);
    initialized_ = true;
  }

157
  V8_WARN_UNUSED_RESULT static MaybeHandle<Object> InvokeApiFunction(
158 159 160
      Isolate* isolate, bool is_construct, Handle<HeapObject> function,
      Handle<Object> receiver, int argc, Handle<Object> args[],
      Handle<HeapObject> new_target);
161

162
  static void Generate_Adaptor(MacroAssembler* masm, Address builtin_address);
163

164 165 166 167
  static void Generate_CEntry(MacroAssembler* masm, int result_size,
                              SaveFPRegsMode save_doubles, ArgvMode argv_mode,
                              bool builtin_exit_frame);

168 169 170
  static bool AllowDynamicFunction(Isolate* isolate, Handle<JSFunction> target,
                                   Handle<JSObject> target_global_proxy);

171 172 173
  // Creates a trampoline code object that jumps to the given off-heap entry.
  // The result should not be used directly, but only from the related Factory
  // function.
174 175 176 177 178
  // TODO(delphick): Come up with a better name since it may not generate an
  // executable trampoline.
  static Handle<Code> GenerateOffHeapTrampolineFor(
      Isolate* isolate, Address off_heap_entry, int32_t kind_specific_flags,
      bool generate_jump_to_instruction_stream);
179

180 181 182 183
  // Generate the RelocInfo ByteArray that would be generated for an offheap
  // trampoline.
  static Handle<ByteArray> GenerateOffHeapTrampolineRelocInfo(Isolate* isolate);

184 185 186 187 188
  // Only builtins with JS linkage should ever need to be called via their
  // trampoline Code object. The remaining builtins have non-executable Code
  // objects.
  static bool CodeObjectIsExecutable(int builtin_index);

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  static bool IsJSEntryVariant(int builtin_index) {
    switch (builtin_index) {
      case kJSEntry:
      case kJSConstructEntry:
      case kJSRunMicrotasksEntry:
        return true;
      default:
        return false;
    }
    UNREACHABLE();
  }

  int js_entry_handler_offset() const {
    DCHECK_NE(js_entry_handler_offset_, 0);
    return js_entry_handler_offset_;
  }

  void SetJSEntryHandlerOffset(int offset) {
    // Check the stored offset is either uninitialized or unchanged (we
    // generate multiple variants of this builtin but they should all have the
    // same handler offset).
    CHECK(js_entry_handler_offset_ == 0 || js_entry_handler_offset_ == offset);
    js_entry_handler_offset_ = offset;
  }

214
 private:
215
  static void Generate_CallFunction(MacroAssembler* masm,
216
                                    ConvertReceiverMode mode);
217

218
  static void Generate_CallBoundFunctionImpl(MacroAssembler* masm);
219

220
  static void Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode);
221

222
  enum class CallOrConstructMode { kCall, kConstruct };
223 224 225
  static void Generate_CallOrConstructVarargs(MacroAssembler* masm,
                                              Handle<Code> code);
  static void Generate_CallOrConstructForwardVarargs(MacroAssembler* masm,
226
                                                     CallOrConstructMode mode,
227
                                                     Handle<Code> code);
228

229 230
  static void Generate_InterpreterPushArgsThenCallImpl(
      MacroAssembler* masm, ConvertReceiverMode receiver_mode,
231
      InterpreterPushArgsMode mode);
232

233
  static void Generate_InterpreterPushArgsThenConstructImpl(
234
      MacroAssembler* masm, InterpreterPushArgsMode mode);
235

236 237 238
#define DECLARE_ASM(Name, ...) \
  static void Generate_##Name(MacroAssembler* masm);
#define DECLARE_TF(Name, ...) \
239
  static void Generate_##Name(compiler::CodeAssemblerState* state);
240

241 242
  BUILTIN_LIST(IGNORE_BUILTIN, DECLARE_TF, DECLARE_TF, DECLARE_TF, DECLARE_TF,
               IGNORE_BUILTIN, DECLARE_ASM)
243

244 245
#undef DECLARE_ASM
#undef DECLARE_TF
246

247 248
  Isolate* isolate_;
  bool initialized_ = false;
249

250 251 252 253 254
  // Stores the offset of exception handler entry point (the handler_entry
  // label) in JSEntry and its variants. It's used to generate the handler table
  // during codegen (mksnapshot-only).
  int js_entry_handler_offset_ = 0;

255
  friend class SetupIsolateDelegate;
256 257
};

258 259 260
Builtins::Name ExampleBuiltinForTorqueFunctionPointerType(
    size_t function_pointer_type_id);

261 262
}  // namespace internal
}  // namespace v8
263

264
#endif  // V8_BUILTINS_BUILTINS_H_