turbo-assembler.h 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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"
#include "src/heap/heap.h"

namespace v8 {
namespace internal {

// Common base class for platform-specific TurboAssemblers containing
// platform-independent bits.
16
class V8_EXPORT_PRIVATE TurboAssemblerBase : public Assembler {
17 18 19 20 21 22 23 24 25 26 27
 public:
  Isolate* isolate() const { return isolate_; }

  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; }

28
  bool trap_on_abort() const { return trap_on_abort_; }
29 30 31

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

33 34 35 36 37 38 39 40 41 42 43 44 45
  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_; }

  // 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;
46

47 48
  virtual void LoadRootRegisterOffset(Register destination,
                                      intptr_t offset) = 0;
49
  virtual void LoadRootRelative(Register destination, int32_t offset) = 0;
50 51 52

  virtual void LoadRoot(Register destination, Heap::RootListIndex index) = 0;

53 54 55 56 57 58 59 60 61 62 63 64 65 66
  static int32_t RootRegisterOffset(Heap::RootListIndex root_index);
  static int32_t RootRegisterOffsetForExternalReferenceIndex(
      int reference_index);

  static int32_t RootRegisterOffsetForBuiltinIndex(int builtin_index);

  static intptr_t RootRegisterOffsetForExternalReference(
      Isolate* isolate, const ExternalReference& reference);

  // An address is addressable through kRootRegister if it is located within
  // [isolate, roots_ + root_register_addressable_end_offset[.
  static bool IsAddressableThroughRootRegister(
      Isolate* isolate, const ExternalReference& reference);

67
 protected:
68 69 70
  TurboAssemblerBase(Isolate* isolate, const AssemblerOptions& options,
                     void* buffer, int buffer_size,
                     CodeObjectRequired create_code_object);
71 72 73 74 75 76 77 78 79

  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;

80 81 82
  // Immediately trap instead of calling {Abort} when debug code fails.
  bool trap_on_abort_ = FLAG_trap_on_abort;

83 84 85
  // Emit a C call to abort instead of a runtime call.
  bool hard_abort_ = false;

86 87 88 89 90 91 92 93
  // May be set while generating builtins.
  int maybe_builtin_index_ = Builtins::kNoBuiltinId;

  bool has_frame_ = false;

  DISALLOW_IMPLICIT_CONSTRUCTORS(TurboAssemblerBase);
};

94 95 96
// 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.
97
class HardAbortScope BASE_EMBEDDED {
98
 public:
99 100 101
  explicit HardAbortScope(TurboAssemblerBase* assembler)
      : assembler_(assembler), old_value_(assembler->should_abort_hard()) {
    assembler_->set_abort_hard(true);
102
  }
103
  ~HardAbortScope() { assembler_->set_abort_hard(old_value_); }
104 105 106 107 108 109

 private:
  TurboAssemblerBase* assembler_;
  bool old_value_;
};

110 111 112 113 114 115
// Helper stubs can be called in different ways depending on where the target
// code is located and how the call sequence is expected to look like:
//  - JavaScript: Call on-heap {Code} object via {RelocInfo::CODE_TARGET}.
//  - WebAssembly: Call native {WasmCode} stub via {RelocInfo::WASM_STUB_CALL}.
enum class StubCallMode { kCallOnHeapBuiltin, kCallWasmRuntimeStub };

116 117 118 119
}  // namespace internal
}  // namespace v8

#endif  // V8_TURBO_ASSEMBLER_H_