builtins.h 8.9 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 28
namespace compiler {
class CodeAssemblerState;
}
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

  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 54
        builtin_count,

55
#define EXTRACT_NAME(Name, ...) k##Name,
56 57 58 59
    // Define kFirstBytecodeHandler,
    kFirstBytecodeHandler =
        FirstFromVarArgs(BUILTIN_LIST_BYTECODE_HANDLERS(EXTRACT_NAME) 0)
#undef EXTRACT_NAME
60 61
  };

62 63
  static const int32_t kNoBuiltinId = -1;

64 65 66 67 68 69 70 71
  static constexpr int kFirstWideBytecodeHandler =
      kFirstBytecodeHandler + kNumberOfBytecodeHandlers;
  static constexpr int kFirstExtraWideBytecodeHandler =
      kFirstWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
  static constexpr int kLastBytecodeHandlerPlusOne =
      kFirstExtraWideBytecodeHandler + kNumberOfWideBytecodeHandlers;
  STATIC_ASSERT(kLastBytecodeHandlerPlusOne == builtin_count);

72
  static constexpr bool IsBuiltinId(int maybe_id) {
73 74 75
    return 0 <= maybe_id && maybe_id < builtin_count;
  }

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

79 80 81
  static BailoutId GetContinuationBailoutId(Name name);
  static Name GetBuiltinFromBailoutId(BailoutId);

82
  // Convenience wrappers.
83 84
  Handle<Code> CallFunction(ConvertReceiverMode = ConvertReceiverMode::kAny);
  Handle<Code> Call(ConvertReceiverMode = ConvertReceiverMode::kAny);
85 86 87
  Handle<Code> NonPrimitiveToPrimitive(
      ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
  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 103
  static const char* name(int index);

104 105 106 107
  // Support for --print-builtin-size and --print-builtin-code.
  void PrintBuiltinCode();
  void PrintBuiltinSize();

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

112 113 114
  static Kind KindOf(int index);
  static const char* KindNameOf(int index);

115
  static bool IsCpp(int index);
116

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

121 122
  // As above, but safe to access off the main thread since the check is done
  // 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 129
  // True, iff the given builtin contains no isolate-specific code and can be
  // embedded into the binary.
130 131 132 133 134 135 136 137
  static constexpr bool kAllBuiltinsAreIsolateIndependent = true;
  static constexpr bool AllBuiltinsAreIsolateIndependent() {
    return kAllBuiltinsAreIsolateIndependent;
  }
  static constexpr bool IsIsolateIndependent(int index) {
    STATIC_ASSERT(kAllBuiltinsAreIsolateIndependent);
    return kAllBuiltinsAreIsolateIndependent;
  }
138

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

143 144 145
  // Emits a CodeCreateEvent for every builtin.
  static void EmitCodeCreateEvents(Isolate* isolate);

146 147
  bool is_initialized() const { return initialized_; }

148 149 150 151 152 153
  // Used by SetupIsolateDelegate and Deserializer.
  void MarkInitialized() {
    DCHECK(!initialized_);
    initialized_ = true;
  }

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

159
  static void Generate_Adaptor(MacroAssembler* masm, Address builtin_address);
160

161 162 163 164
  static void Generate_CEntry(MacroAssembler* masm, int result_size,
                              SaveFPRegsMode save_doubles, ArgvMode argv_mode,
                              bool builtin_exit_frame);

165 166 167
  static bool AllowDynamicFunction(Isolate* isolate, Handle<JSFunction> target,
                                   Handle<JSObject> target_global_proxy);

168 169 170
  // 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.
171 172 173 174 175
  // 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);
176

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

181 182 183 184 185
  // 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);

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  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;
  }

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 221 222
  static void Generate_CallOrConstructVarargs(MacroAssembler* masm,
                                              Handle<Code> code);
  static void Generate_CallOrConstructForwardVarargs(MacroAssembler* masm,
223
                                                     CallOrConstructMode mode,
224
                                                     Handle<Code> code);
225

226 227
  static void Generate_InterpreterPushArgsThenCallImpl(
      MacroAssembler* masm, ConvertReceiverMode receiver_mode,
228
      InterpreterPushArgsMode mode);
229

230
  static void Generate_InterpreterPushArgsThenConstructImpl(
231
      MacroAssembler* masm, InterpreterPushArgsMode mode);
232

233 234 235
#define DECLARE_ASM(Name, ...) \
  static void Generate_##Name(MacroAssembler* masm);
#define DECLARE_TF(Name, ...) \
236
  static void Generate_##Name(compiler::CodeAssemblerState* state);
237

238 239
  BUILTIN_LIST(IGNORE_BUILTIN, DECLARE_TF, DECLARE_TF, DECLARE_TF, DECLARE_TF,
               IGNORE_BUILTIN, DECLARE_ASM)
240

241 242
#undef DECLARE_ASM
#undef DECLARE_TF
243

244 245
  Isolate* isolate_;
  bool initialized_ = false;
246

247 248 249 250 251
  // 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;

252
  friend class SetupIsolateDelegate;
253 254

  DISALLOW_COPY_AND_ASSIGN(Builtins);
255 256
};

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

260 261
}  // namespace internal
}  // namespace v8
262

263
#endif  // V8_BUILTINS_BUILTINS_H_