turbo-assembler.h 5.93 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

#ifndef V8_TURBO_ASSEMBLER_H_
#define V8_TURBO_ASSEMBLER_H_

#include "src/assembler-arch.h"
9
#include "src/base/template-utils.h"
10
#include "src/builtins/builtins.h"
11
#include "src/roots.h"
12 13 14 15 16 17

namespace v8 {
namespace internal {

// Common base class for platform-specific TurboAssemblers containing
// platform-independent bits.
18
class V8_EXPORT_PRIVATE TurboAssemblerBase : public Assembler {
19
 public:
20 21 22 23
  Isolate* isolate() const {
    DCHECK(!options().v8_agnostic_code);
    return isolate_;
  }
24 25 26 27 28 29 30 31 32

  Handle<HeapObject> CodeObject() const {
    DCHECK(!code_object_.is_null());
    return code_object_;
  }

  bool root_array_available() const { return root_array_available_; }
  void set_root_array_available(bool v) { root_array_available_ = v; }

33
  bool trap_on_abort() const { return trap_on_abort_; }
34 35 36

  bool should_abort_hard() const { return hard_abort_; }
  void set_abort_hard(bool v) { hard_abort_ = v; }
37

38 39 40 41 42
  void set_builtin_index(int i) { maybe_builtin_index_ = i; }

  void set_has_frame(bool v) { has_frame_ = v; }
  bool has_frame() const { return has_frame_; }

43 44 45 46
  // Calls the given builtin. If builtins are embedded, the trampoline Code
  // object on the heap is not used.
  virtual void CallBuiltinPointer(Register builtin_pointer) = 0;

47 48 49 50 51 52 53 54 55
  // Calls/jumps to the given Code object. If builtins are embedded, the
  // trampoline Code object on the heap is not used.
  virtual void CallCodeObject(Register code_object) = 0;
  virtual void JumpCodeObject(Register code_object) = 0;

  // Loads the given Code object's entry point into the destination register.
  virtual void LoadCodeObjectEntry(Register destination,
                                   Register code_object) = 0;

56 57 58 59 60 61 62 63
  // Loads the given constant or external reference without embedding its direct
  // pointer. The produced code is isolate-independent.
  void IndirectLoadConstant(Register destination, Handle<HeapObject> object);
  void IndirectLoadExternalReference(Register destination,
                                     ExternalReference reference);

  virtual void LoadFromConstantsTable(Register destination,
                                      int constant_index) = 0;
64

65
  // Corresponds to: destination = kRootRegister + offset.
66 67
  virtual void LoadRootRegisterOffset(Register destination,
                                      intptr_t offset) = 0;
68 69

  // Corresponds to: destination = [kRootRegister + offset].
70
  virtual void LoadRootRelative(Register destination, int32_t offset) = 0;
71

72
  virtual void LoadRoot(Register destination, RootIndex index) = 0;
73

74
  static int32_t RootRegisterOffsetForRootIndex(RootIndex root_index);
75 76
  static int32_t RootRegisterOffsetForBuiltinIndex(int builtin_index);

77
  // Returns the root-relative offset to reference.address().
78 79 80
  static intptr_t RootRegisterOffsetForExternalReference(
      Isolate* isolate, const ExternalReference& reference);

81 82 83 84 85
  // Returns the root-relative offset to the external reference table entry,
  // which itself contains reference.address().
  static int32_t RootRegisterOffsetForExternalReferenceTableEntry(
      Isolate* isolate, const ExternalReference& reference);

86
  // An address is addressable through kRootRegister if it is located within
87
  // isolate->root_register_addressable_region().
88 89 90
  static bool IsAddressableThroughRootRegister(
      Isolate* isolate, const ExternalReference& reference);

91
 protected:
92 93 94 95 96
  TurboAssemblerBase(const AssemblerOptions& options, void* buffer,
                     int buffer_size)
      : TurboAssemblerBase(nullptr, options.EnableV8AgnosticCode(), buffer,
                           buffer_size, CodeObjectRequired::kNo) {}

97 98 99
  TurboAssemblerBase(Isolate* isolate, const AssemblerOptions& options,
                     void* buffer, int buffer_size,
                     CodeObjectRequired create_code_object);
100

101 102
  void RecordCommentForOffHeapTrampoline(int builtin_index);

103 104 105 106 107 108 109 110
  Isolate* const isolate_ = nullptr;

  // This handle will be patched with the code object on installation.
  Handle<HeapObject> code_object_;

  // Whether kRootRegister has been initialized.
  bool root_array_available_ = true;

111 112 113
  // Immediately trap instead of calling {Abort} when debug code fails.
  bool trap_on_abort_ = FLAG_trap_on_abort;

114 115 116
  // Emit a C call to abort instead of a runtime call.
  bool hard_abort_ = false;

117 118 119 120 121 122 123 124
  // May be set while generating builtins.
  int maybe_builtin_index_ = Builtins::kNoBuiltinId;

  bool has_frame_ = false;

  DISALLOW_IMPLICIT_CONSTRUCTORS(TurboAssemblerBase);
};

125 126 127
// Avoids emitting calls to the {Builtins::kAbort} builtin when emitting debug
// code during the lifetime of this scope object. For disabling debug code
// entirely use the {DontEmitDebugCodeScope} instead.
128
class HardAbortScope {
129
 public:
130 131 132
  explicit HardAbortScope(TurboAssemblerBase* assembler)
      : assembler_(assembler), old_value_(assembler->should_abort_hard()) {
    assembler_->set_abort_hard(true);
133
  }
134
  ~HardAbortScope() { assembler_->set_abort_hard(old_value_); }
135 136 137 138 139 140

 private:
  TurboAssemblerBase* assembler_;
  bool old_value_;
};

141
#ifdef DEBUG
142 143 144 145 146 147 148
struct CountIfValidRegisterFunctor {
  template <typename RegType>
  constexpr int operator()(int count, RegType reg) const {
    return count + (reg.is_valid() ? 1 : 0);
  }
};

149 150 151 152 153 154 155
template <typename RegType, typename... RegTypes,
          // All arguments must be either Register or DoubleRegister.
          typename = typename std::enable_if<
              base::is_same<Register, RegType, RegTypes...>::value ||
              base::is_same<DoubleRegister, RegType, RegTypes...>::value>::type>
inline bool AreAliased(RegType first_reg, RegTypes... regs) {
  int num_different_regs = NumRegs(RegType::ListOf(first_reg, regs...));
156 157
  int num_given_regs =
      base::fold(CountIfValidRegisterFunctor{}, 0, first_reg, regs...);
158 159
  return num_different_regs < num_given_regs;
}
160 161
#endif

162 163 164 165
}  // namespace internal
}  // namespace v8

#endif  // V8_TURBO_ASSEMBLER_H_