V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
turbo-assembler.cc
1 // Copyright 2018 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 #include "src/turbo-assembler.h"
6 
7 #include "src/builtins/builtins.h"
8 #include "src/builtins/constants-table-builder.h"
9 #include "src/isolate-data.h"
10 #include "src/isolate-inl.h"
11 #include "src/lsan.h"
12 #include "src/snapshot/serializer-common.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 TurboAssemblerBase::TurboAssemblerBase(Isolate* isolate,
18  const AssemblerOptions& options,
19  void* buffer, int buffer_size,
20  CodeObjectRequired create_code_object)
21  : Assembler(options, buffer, buffer_size), isolate_(isolate) {
22  if (create_code_object == CodeObjectRequired::kYes) {
23  code_object_ = Handle<HeapObject>::New(
24  ReadOnlyRoots(isolate).self_reference_marker(), isolate);
25  }
26 }
27 
28 void TurboAssemblerBase::IndirectLoadConstant(Register destination,
29  Handle<HeapObject> object) {
30  CHECK(root_array_available_);
31 
32  // Before falling back to the (fairly slow) lookup from the constants table,
33  // check if any of the fast paths can be applied.
34 
35  int builtin_index;
36  RootIndex root_index;
37  if (isolate()->roots_table().IsRootHandle(object, &root_index)) {
38  // Roots are loaded relative to the root register.
39  LoadRoot(destination, root_index);
40  } else if (isolate()->builtins()->IsBuiltinHandle(object, &builtin_index)) {
41  // Similar to roots, builtins may be loaded from the builtins table.
42  LoadRootRelative(destination,
43  RootRegisterOffsetForBuiltinIndex(builtin_index));
44  } else if (object.is_identical_to(code_object_) &&
45  Builtins::IsBuiltinId(maybe_builtin_index_)) {
46  // The self-reference loaded through Codevalue() may also be a builtin
47  // and thus viable for a fast load.
48  LoadRootRelative(destination,
49  RootRegisterOffsetForBuiltinIndex(maybe_builtin_index_));
50  } else {
51  CHECK(isolate()->ShouldLoadConstantsFromRootList());
52  // Ensure the given object is in the builtins constants table and fetch its
53  // index.
54  BuiltinsConstantsTableBuilder* builder =
55  isolate()->builtins_constants_table_builder();
56  uint32_t index = builder->AddObject(object);
57 
58  // Slow load from the constants table.
59  LoadFromConstantsTable(destination, index);
60  }
61 }
62 
63 void TurboAssemblerBase::IndirectLoadExternalReference(
64  Register destination, ExternalReference reference) {
65  CHECK(root_array_available_);
66 
67  if (IsAddressableThroughRootRegister(isolate(), reference)) {
68  // Some external references can be efficiently loaded as an offset from
69  // kRootRegister.
70  intptr_t offset =
71  RootRegisterOffsetForExternalReference(isolate(), reference);
72  LoadRootRegisterOffset(destination, offset);
73  } else {
74  // Otherwise, do a memory load from the external reference table.
75  LoadRootRelative(
76  destination,
77  RootRegisterOffsetForExternalReferenceTableEntry(isolate(), reference));
78  }
79 }
80 
81 // static
82 int32_t TurboAssemblerBase::RootRegisterOffsetForRootIndex(
83  RootIndex root_index) {
84  return IsolateData::root_slot_offset(root_index);
85 }
86 
87 // static
88 int32_t TurboAssemblerBase::RootRegisterOffsetForBuiltinIndex(
89  int builtin_index) {
90  return IsolateData::builtin_slot_offset(builtin_index);
91 }
92 
93 // static
94 intptr_t TurboAssemblerBase::RootRegisterOffsetForExternalReference(
95  Isolate* isolate, const ExternalReference& reference) {
96  return static_cast<intptr_t>(reference.address() - isolate->isolate_root());
97 }
98 
99 // static
100 int32_t TurboAssemblerBase::RootRegisterOffsetForExternalReferenceTableEntry(
101  Isolate* isolate, const ExternalReference& reference) {
102  // Encode as an index into the external reference table stored on the
103  // isolate.
104  ExternalReferenceEncoder encoder(isolate);
105  ExternalReferenceEncoder::Value v = encoder.Encode(reference.address());
106  CHECK(!v.is_from_api());
107 
108  return IsolateData::external_reference_table_offset() +
109  ExternalReferenceTable::OffsetOfEntry(v.index());
110 }
111 
112 // static
113 bool TurboAssemblerBase::IsAddressableThroughRootRegister(
114  Isolate* isolate, const ExternalReference& reference) {
115  Address address = reference.address();
116  return isolate->root_register_addressable_region().contains(address);
117 }
118 
119 void TurboAssemblerBase::RecordCommentForOffHeapTrampoline(int builtin_index) {
120  if (!FLAG_code_comments) return;
121  size_t len = strlen("-- Inlined Trampoline to --") +
122  strlen(Builtins::name(builtin_index)) + 1;
123  Vector<char> buffer = Vector<char>::New(static_cast<int>(len));
124  char* buffer_start = buffer.start();
125  LSAN_IGNORE_OBJECT(buffer_start);
126  SNPrintF(buffer, "-- Inlined Trampoline to %s --",
127  Builtins::name(builtin_index));
128  RecordComment(buffer_start);
129 }
130 
131 } // namespace internal
132 } // namespace v8
Definition: libplatform.h:13