setup-builtins-internal.cc 14.5 KB
Newer Older
1 2 3 4 5
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/builtins/builtins.h"
6
#include "src/builtins/profile-data-reader.h"
7 8
#include "src/codegen/assembler-inl.h"
#include "src/codegen/interface-descriptors.h"
9
#include "src/codegen/macro-assembler-inl.h"
10
#include "src/codegen/macro-assembler.h"
11
#include "src/compiler/code-assembler.h"
12
#include "src/execution/isolate.h"
13
#include "src/handles/handles-inl.h"
14 15
#include "src/heap/heap-inl.h"  // For Heap::code_range.
#include "src/init/setup-isolate.h"
16
#include "src/interpreter/bytecodes.h"
17
#include "src/interpreter/interpreter-generator.h"
18
#include "src/interpreter/interpreter.h"
19
#include "src/objects/objects-inl.h"
20
#include "src/objects/shared-function-info.h"
21
#include "src/objects/smi.h"
22 23 24 25 26 27

namespace v8 {
namespace internal {

// Forward declarations for C++ builtins.
#define FORWARD_DECLARE(Name) \
28
  Address Builtin_##Name(int argc, Address* args, Isolate* isolate);
29
BUILTIN_LIST_C(FORWARD_DECLARE)
30
#undef FORWARD_DECLARE
31 32

namespace {
33

34 35 36
AssemblerOptions BuiltinAssemblerOptions(Isolate* isolate,
                                         int32_t builtin_index) {
  AssemblerOptions options = AssemblerOptions::Default(isolate);
37 38
  CHECK(!options.isolate_independent_code);
  CHECK(!options.use_pc_relative_calls_and_jumps);
39
  CHECK(!options.collect_win64_unwind_info);
40

41
  if (!isolate->IsGeneratingEmbeddedBuiltins()) {
42
    return options;
43
  }
44

45
  const base::AddressRegion& code_range = isolate->heap()->code_range();
46
  bool pc_relative_calls_fit_in_code_range =
47
      !code_range.is_empty() &&
48 49
      std::ceil(static_cast<float>(code_range.size() / MB)) <=
          kMaxPCRelativeCodeRangeInMB;
50 51 52

  options.isolate_independent_code = true;
  options.use_pc_relative_calls_and_jumps = pc_relative_calls_fit_in_code_range;
53
  options.collect_win64_unwind_info = true;
54

55 56 57
  return options;
}

58 59
using MacroAssemblerGenerator = void (*)(MacroAssembler*);
using CodeAssemblerGenerator = void (*)(compiler::CodeAssemblerState*);
60

61
Handle<Code> BuildPlaceholder(Isolate* isolate, int32_t builtin_index) {
62
  HandleScope scope(isolate);
63 64
  constexpr int kBufferSize = 1 * KB;
  byte buffer[kBufferSize];
65 66
  MacroAssembler masm(isolate, CodeObjectRequired::kYes,
                      ExternalAssemblerBuffer(buffer, kBufferSize));
67 68 69
  DCHECK(!masm.has_frame());
  {
    FrameScope scope(&masm, StackFrame::NONE);
70 71
    // The contents of placeholder don't matter, as long as they don't create
    // embedded constants or external references.
72
    masm.Move(kJavaScriptCallCodeStartRegister, Smi::zero());
73
    masm.Call(kJavaScriptCallCodeStartRegister);
74 75 76
  }
  CodeDesc desc;
  masm.GetCode(isolate, &desc);
77
  Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
78 79 80
                          .set_self_reference(masm.CodeObject())
                          .set_builtin_index(builtin_index)
                          .Build();
81 82 83
  return scope.CloseAndEscape(code);
}

84 85 86
Code BuildWithMacroAssembler(Isolate* isolate, int32_t builtin_index,
                             MacroAssemblerGenerator generator,
                             const char* s_name) {
87
  HandleScope scope(isolate);
88 89 90
  // Canonicalize handles, so that we can share constant pool entries pointing
  // to code targets without dereferencing their handles.
  CanonicalHandleScope canonical(isolate);
91 92
  constexpr int kBufferSize = 32 * KB;
  byte buffer[kBufferSize];
93 94

  MacroAssembler masm(isolate, BuiltinAssemblerOptions(isolate, builtin_index),
95 96
                      CodeObjectRequired::kYes,
                      ExternalAssemblerBuffer(buffer, kBufferSize));
97
  masm.set_builtin_index(builtin_index);
98
  DCHECK(!masm.has_frame());
99
  masm.CodeEntry();
100
  generator(&masm);
101 102 103 104 105 106 107 108

  int handler_table_offset = 0;

  // JSEntry builtins are a special case and need to generate a handler table.
  DCHECK_EQ(Builtins::KindOf(Builtins::kJSEntry), Builtins::ASM);
  DCHECK_EQ(Builtins::KindOf(Builtins::kJSConstructEntry), Builtins::ASM);
  DCHECK_EQ(Builtins::KindOf(Builtins::kJSRunMicrotasksEntry), Builtins::ASM);
  if (Builtins::IsJSEntryVariant(builtin_index)) {
109
    handler_table_offset = HandlerTable::EmitReturnTableStart(&masm);
110 111 112 113
    HandlerTable::EmitReturnEntry(
        &masm, 0, isolate->builtins()->js_entry_handler_offset());
  }

114
  CodeDesc desc;
115 116
  masm.GetCode(isolate, &desc, MacroAssembler::kNoSafepointTable,
               handler_table_offset);
117

118
  Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
119 120 121
                          .set_self_reference(masm.CodeObject())
                          .set_builtin_index(builtin_index)
                          .Build();
122
#if defined(V8_OS_WIN64)
123
  isolate->SetBuiltinUnwindData(builtin_index, masm.GetUnwindInfo());
124
#endif  // V8_OS_WIN64
125 126 127
  return *code;
}

128
Code BuildAdaptor(Isolate* isolate, int32_t builtin_index,
129
                  Address builtin_address, const char* name) {
130
  HandleScope scope(isolate);
131 132 133
  // Canonicalize handles, so that we can share constant pool entries pointing
  // to code targets without dereferencing their handles.
  CanonicalHandleScope canonical(isolate);
134 135
  constexpr int kBufferSize = 32 * KB;
  byte buffer[kBufferSize];
136
  MacroAssembler masm(isolate, BuiltinAssemblerOptions(isolate, builtin_index),
137 138
                      CodeObjectRequired::kYes,
                      ExternalAssemblerBuffer(buffer, kBufferSize));
139
  masm.set_builtin_index(builtin_index);
140
  DCHECK(!masm.has_frame());
141
  Builtins::Generate_Adaptor(&masm, builtin_address);
142
  CodeDesc desc;
143
  masm.GetCode(isolate, &desc);
144
  Handle<Code> code = Factory::CodeBuilder(isolate, desc, CodeKind::BUILTIN)
145 146 147
                          .set_self_reference(masm.CodeObject())
                          .set_builtin_index(builtin_index)
                          .Build();
148 149 150 151
  return *code;
}

// Builder for builtins implemented in TurboFan with JS linkage.
152 153 154
Code BuildWithCodeStubAssemblerJS(Isolate* isolate, int32_t builtin_index,
                                  CodeAssemblerGenerator generator, int argc,
                                  const char* name) {
155
  HandleScope scope(isolate);
156 157 158
  // Canonicalize handles, so that we can share constant pool entries pointing
  // to code targets without dereferencing their handles.
  CanonicalHandleScope canonical(isolate);
159

160
  Zone zone(isolate->allocator(), ZONE_NAME, kCompressGraphZone);
161
  const int argc_with_recv =
162
      (argc == kDontAdaptArgumentsSentinel) ? 0 : argc + 1;
163
  compiler::CodeAssemblerState state(
164
      isolate, &zone, argc_with_recv, CodeKind::BUILTIN, name,
165
      PoisoningMitigationLevel::kDontPoison, builtin_index);
166
  generator(&state);
167
  Handle<Code> code = compiler::CodeAssembler::GenerateCode(
168 169
      &state, BuiltinAssemblerOptions(isolate, builtin_index),
      ProfileDataFromFile::TryRead(name));
170 171 172 173
  return *code;
}

// Builder for builtins implemented in TurboFan with CallStub linkage.
174 175 176
Code BuildWithCodeStubAssemblerCS(Isolate* isolate, int32_t builtin_index,
                                  CodeAssemblerGenerator generator,
                                  CallDescriptors::Key interface_descriptor,
177
                                  const char* name) {
178
  HandleScope scope(isolate);
179 180 181
  // Canonicalize handles, so that we can share constant pool entries pointing
  // to code targets without dereferencing their handles.
  CanonicalHandleScope canonical(isolate);
182
  Zone zone(isolate->allocator(), ZONE_NAME, kCompressGraphZone);
183 184
  // The interface descriptor with given key must be initialized at this point
  // and this construction just queries the details from the descriptors table.
185
  CallInterfaceDescriptor descriptor(interface_descriptor);
186 187
  // Ensure descriptor is already initialized.
  DCHECK_LE(0, descriptor.GetRegisterParameterCount());
188
  compiler::CodeAssemblerState state(
189
      isolate, &zone, descriptor, CodeKind::BUILTIN, name,
190
      PoisoningMitigationLevel::kDontPoison, builtin_index);
191
  generator(&state);
192
  Handle<Code> code = compiler::CodeAssembler::GenerateCode(
193 194
      &state, BuiltinAssemblerOptions(isolate, builtin_index),
      ProfileDataFromFile::TryRead(name));
195 196
  return *code;
}
197

198 199
}  // anonymous namespace

200
// static
201
void SetupIsolateDelegate::AddBuiltin(Builtins* builtins, int index,
202
                                      Code code) {
203
  DCHECK_EQ(index, code.builtin_index());
204
  builtins->set_builtin(index, code);
205 206
}

207
// static
208 209 210 211 212 213
void SetupIsolateDelegate::PopulateWithPlaceholders(Isolate* isolate) {
  // Fill the builtins list with placeholders. References to these placeholder
  // builtins are eventually replaced by the actual builtins. This is to
  // support circular references between builtins.
  Builtins* builtins = isolate->builtins();
  HandleScope scope(isolate);
214 215 216
  for (int i = 0; i < Builtins::builtin_count; i++) {
    Handle<Code> placeholder = BuildPlaceholder(isolate, i);
    AddBuiltin(builtins, i, *placeholder);
217 218 219
  }
}

220
// static
221 222 223
void SetupIsolateDelegate::ReplacePlaceholders(Isolate* isolate) {
  // Replace references from all code objects to placeholders.
  Builtins* builtins = isolate->builtins();
224
  DisallowGarbageCollection no_gc;
225
  CodeSpaceMemoryModificationScope modification_scope(isolate->heap());
226 227
  static const int kRelocMask =
      RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
228 229
      RelocInfo::ModeMask(RelocInfo::FULL_EMBEDDED_OBJECT) |
      RelocInfo::ModeMask(RelocInfo::COMPRESSED_EMBEDDED_OBJECT) |
230
      RelocInfo::ModeMask(RelocInfo::RELATIVE_CODE_TARGET);
231
  HeapObjectIterator iterator(isolate->heap());
232 233
  for (HeapObject obj = iterator.Next(); !obj.is_null();
       obj = iterator.Next()) {
234
    if (!obj.IsCode()) continue;
235
    Code code = Code::cast(obj);
236 237 238
    bool flush_icache = false;
    for (RelocIterator it(code, kRelocMask); !it.done(); it.next()) {
      RelocInfo* rinfo = it.rinfo();
239
      if (RelocInfo::IsCodeTargetMode(rinfo->rmode())) {
240
        Code target = Code::GetCodeFromTargetAddress(rinfo->target_address());
241
        DCHECK_IMPLIES(RelocInfo::IsRelativeCodeTarget(rinfo->rmode()),
242 243 244 245
                       Builtins::IsIsolateIndependent(target.builtin_index()));
        if (!target.is_builtin()) continue;
        Code new_target = builtins->builtin(target.builtin_index());
        rinfo->set_target_address(new_target.raw_instruction_start(),
246 247
                                  UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
      } else {
248
        DCHECK(RelocInfo::IsEmbeddedObjectMode(rinfo->rmode()));
249
        Object object = rinfo->target_object();
250
        if (!object.IsCode()) continue;
251
        Code target = Code::cast(object);
252 253
        if (!target.is_builtin()) continue;
        Code new_target = builtins->builtin(target.builtin_index());
254 255
        rinfo->set_target_object(isolate->heap(), new_target,
                                 UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
256 257 258 259
      }
      flush_icache = true;
    }
    if (flush_icache) {
260 261
      FlushInstructionCache(code.raw_instruction_start(),
                            code.raw_instruction_size());
262 263 264 265
    }
  }
}

266
namespace {
267

268 269 270
Code GenerateBytecodeHandler(Isolate* isolate, int builtin_index,
                             interpreter::OperandScale operand_scale,
                             interpreter::Bytecode bytecode) {
271
  DCHECK(interpreter::Bytecodes::BytecodeHasHandler(bytecode, operand_scale));
272
  Handle<Code> code = interpreter::GenerateBytecodeHandler(
273 274
      isolate, Builtins::name(builtin_index), bytecode, operand_scale,
      builtin_index, BuiltinAssemblerOptions(isolate, builtin_index));
275
  return *code;
276
}
277

278 279
}  // namespace

280
// static
281 282 283 284
void SetupIsolateDelegate::SetupBuiltinsInternal(Isolate* isolate) {
  Builtins* builtins = isolate->builtins();
  DCHECK(!builtins->initialized_);

285 286
  PopulateWithPlaceholders(isolate);

287 288 289 290
  // Create a scope for the handles in the builtins.
  HandleScope scope(isolate);

  int index = 0;
291
  Code code;
292 293
#define BUILD_CPP(Name)                                                      \
  code = BuildAdaptor(isolate, index, FUNCTION_ADDR(Builtin_##Name), #Name); \
294
  AddBuiltin(builtins, index++, code);
295 296 297
#define BUILD_TFJ(Name, Argc, ...)                              \
  code = BuildWithCodeStubAssemblerJS(                          \
      isolate, index, &Builtins::Generate_##Name, Argc, #Name); \
298
  AddBuiltin(builtins, index++, code);
299 300 301 302 303
#define BUILD_TFC(Name, InterfaceDescriptor)                      \
  /* Return size is from the provided CallInterfaceDescriptor. */ \
  code = BuildWithCodeStubAssemblerCS(                            \
      isolate, index, &Builtins::Generate_##Name,                 \
      CallDescriptors::InterfaceDescriptor, #Name);               \
304
  AddBuiltin(builtins, index++, code);
305 306 307 308
#define BUILD_TFS(Name, ...)                                                   \
  /* Return size for generic TF builtins (stub linkage) is always 1. */        \
  code =                                                                       \
      BuildWithCodeStubAssemblerCS(isolate, index, &Builtins::Generate_##Name, \
309
                                   CallDescriptors::Name, #Name);              \
310
  AddBuiltin(builtins, index++, code);
311 312 313 314 315
#define BUILD_TFH(Name, InterfaceDescriptor)              \
  /* Return size for IC builtins/handlers is always 1. */ \
  code = BuildWithCodeStubAssemblerCS(                    \
      isolate, index, &Builtins::Generate_##Name,         \
      CallDescriptors::InterfaceDescriptor, #Name);       \
316
  AddBuiltin(builtins, index++, code);
317

318 319
#define BUILD_BCH(Name, OperandScale, Bytecode)                           \
  code = GenerateBytecodeHandler(isolate, index, OperandScale, Bytecode); \
320 321
  AddBuiltin(builtins, index++, code);

322
#define BUILD_ASM(Name, InterfaceDescriptor)                                \
323 324
  code = BuildWithMacroAssembler(isolate, index, Builtins::Generate_##Name, \
                                 #Name);                                    \
325 326
  AddBuiltin(builtins, index++, code);

327 328
  BUILTIN_LIST(BUILD_CPP, BUILD_TFJ, BUILD_TFC, BUILD_TFS, BUILD_TFH, BUILD_BCH,
               BUILD_ASM);
329 330 331

#undef BUILD_CPP
#undef BUILD_TFJ
332
#undef BUILD_TFC
333 334
#undef BUILD_TFS
#undef BUILD_TFH
335
#undef BUILD_BCH
336 337 338
#undef BUILD_ASM
  CHECK_EQ(Builtins::builtin_count, index);

339 340
  ReplacePlaceholders(isolate);

341
#define SET_PROMISE_REJECTION_PREDICTION(Name) \
342
  builtins->builtin(Builtins::k##Name).set_is_promise_rejection(true);
343 344 345 346

  BUILTIN_PROMISE_REJECTION_PREDICTION_LIST(SET_PROMISE_REJECTION_PREDICTION)
#undef SET_PROMISE_REJECTION_PREDICTION

347
#define SET_EXCEPTION_CAUGHT_PREDICTION(Name) \
348
  builtins->builtin(Builtins::k##Name).set_is_exception_caught(true);
349 350 351 352

  BUILTIN_EXCEPTION_CAUGHT_PREDICTION_LIST(SET_EXCEPTION_CAUGHT_PREDICTION)
#undef SET_EXCEPTION_CAUGHT_PREDICTION

353
  builtins->MarkInitialized();
354 355 356 357
}

}  // namespace internal
}  // namespace v8