turbo-assembler.cc 4.64 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
#include "src/codegen/turbo-assembler.h"
6 7 8

#include "src/builtins/builtins.h"
#include "src/builtins/constants-table-builder.h"
9 10
#include "src/execution/isolate-data.h"
#include "src/execution/isolate-inl.h"
11 12 13 14 15
#include "src/snapshot/serializer-common.h"

namespace v8 {
namespace internal {

16 17
TurboAssemblerBase::TurboAssemblerBase(Isolate* isolate,
                                       const AssemblerOptions& options,
18 19 20
                                       CodeObjectRequired create_code_object,
                                       std::unique_ptr<AssemblerBuffer> buffer)
    : Assembler(options, std::move(buffer)), isolate_(isolate) {
21 22
  if (create_code_object == CodeObjectRequired::kYes) {
    code_object_ = Handle<HeapObject>::New(
23
        ReadOnlyRoots(isolate).self_reference_marker(), isolate);
24 25 26 27 28 29 30 31 32 33 34
  }
}

void TurboAssemblerBase::IndirectLoadConstant(Register destination,
                                              Handle<HeapObject> object) {
  CHECK(root_array_available_);

  // Before falling back to the (fairly slow) lookup from the constants table,
  // check if any of the fast paths can be applied.

  int builtin_index;
35
  RootIndex root_index;
36
  if (isolate()->roots_table().IsRootHandle(object, &root_index)) {
37 38 39 40
    // Roots are loaded relative to the root register.
    LoadRoot(destination, root_index);
  } else if (isolate()->builtins()->IsBuiltinHandle(object, &builtin_index)) {
    // Similar to roots, builtins may be loaded from the builtins table.
41 42
    LoadRootRelative(destination,
                     RootRegisterOffsetForBuiltinIndex(builtin_index));
43 44 45 46
  } else if (object.is_identical_to(code_object_) &&
             Builtins::IsBuiltinId(maybe_builtin_index_)) {
    // The self-reference loaded through Codevalue() may also be a builtin
    // and thus viable for a fast load.
47 48
    LoadRootRelative(destination,
                     RootRegisterOffsetForBuiltinIndex(maybe_builtin_index_));
49
  } else {
50
    CHECK(isolate()->IsGeneratingEmbeddedBuiltins());
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    // Ensure the given object is in the builtins constants table and fetch its
    // index.
    BuiltinsConstantsTableBuilder* builder =
        isolate()->builtins_constants_table_builder();
    uint32_t index = builder->AddObject(object);

    // Slow load from the constants table.
    LoadFromConstantsTable(destination, index);
  }
}

void TurboAssemblerBase::IndirectLoadExternalReference(
    Register destination, ExternalReference reference) {
  CHECK(root_array_available_);

66
  if (IsAddressableThroughRootRegister(isolate(), reference)) {
67 68
    // Some external references can be efficiently loaded as an offset from
    // kRootRegister.
69 70
    intptr_t offset =
        RootRegisterOffsetForExternalReference(isolate(), reference);
71 72 73
    LoadRootRegisterOffset(destination, offset);
  } else {
    // Otherwise, do a memory load from the external reference table.
74 75 76
    LoadRootRelative(
        destination,
        RootRegisterOffsetForExternalReferenceTableEntry(isolate(), reference));
77
  }
78 79
}

80
// static
81 82
int32_t TurboAssemblerBase::RootRegisterOffsetForRootIndex(
    RootIndex root_index) {
83
  return IsolateData::root_slot_offset(root_index);
84 85 86
}

// static
87 88
int32_t TurboAssemblerBase::RootRegisterOffsetForBuiltinIndex(
    int builtin_index) {
89
  return IsolateData::builtin_slot_offset(builtin_index);
90 91 92 93 94
}

// static
intptr_t TurboAssemblerBase::RootRegisterOffsetForExternalReference(
    Isolate* isolate, const ExternalReference& reference) {
95
  return static_cast<intptr_t>(reference.address() - isolate->isolate_root());
96 97 98
}

// static
99
int32_t TurboAssemblerBase::RootRegisterOffsetForExternalReferenceTableEntry(
100
    Isolate* isolate, const ExternalReference& reference) {
101 102 103 104 105 106
  // Encode as an index into the external reference table stored on the
  // isolate.
  ExternalReferenceEncoder encoder(isolate);
  ExternalReferenceEncoder::Value v = encoder.Encode(reference.address());
  CHECK(!v.is_from_api());

107
  return IsolateData::external_reference_table_offset() +
108
         ExternalReferenceTable::OffsetOfEntry(v.index());
109 110 111
}

// static
112 113 114 115
bool TurboAssemblerBase::IsAddressableThroughRootRegister(
    Isolate* isolate, const ExternalReference& reference) {
  Address address = reference.address();
  return isolate->root_register_addressable_region().contains(address);
116 117
}

118 119
void TurboAssemblerBase::RecordCommentForOffHeapTrampoline(int builtin_index) {
  if (!FLAG_code_comments) return;
120 121 122
  std::ostringstream str;
  str << "-- Inlined Trampoline to " << Builtins::name(builtin_index) << " --";
  RecordComment(str.str().c_str());
123 124
}

125 126
}  // namespace internal
}  // namespace v8