disasm.h 2.7 KB
Newer Older
1
// Copyright 2007-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_DISASM_H_
#define V8_DISASM_H_

8 9
#include "src/utils.h"

10 11 12 13 14 15 16 17 18
namespace disasm {

typedef unsigned char byte;

// Interface and default implementation for converting addresses and
// register-numbers to text.  The default implementation is machine
// specific.
class NameConverter {
 public:
19
  virtual ~NameConverter() = default;
20
  virtual const char* NameOfCPURegister(int reg) const;
21
  virtual const char* NameOfByteCPURegister(int reg) const;
22 23 24 25
  virtual const char* NameOfXMMRegister(int reg) const;
  virtual const char* NameOfAddress(byte* addr) const;
  virtual const char* NameOfConstant(byte* addr) const;
  virtual const char* NameInCode(byte* addr) const;
26

27 28
  // Given a root-register-relative offset, returns either a name or nullptr if
  // none is found.
29 30 31 32
  // TODO(jgruber,v8:7989): This is a temporary solution until we can preserve
  // code comments through snapshotting.
  virtual const char* RootRelativeName(int offset) const { UNREACHABLE(); }

33 34
 protected:
  v8::internal::EmbeddedVector<char, 128> tmp_buffer_;
35 36 37 38 39 40
};


// A generic Disassembler interface
class Disassembler {
 public:
41 42 43 44 45
  enum UnimplementedOpcodeAction : int8_t {
    kContinueOnUnimplementedOpcode,
    kAbortOnUnimplementedOpcode
  };

46
  // Caller deallocates converter.
47 48 49 50 51
  explicit Disassembler(const NameConverter& converter,
                        UnimplementedOpcodeAction unimplemented_opcode_action =
                            kAbortOnUnimplementedOpcode)
      : converter_(converter),
        unimplemented_opcode_action_(unimplemented_opcode_action) {}
52

53 54 55
  UnimplementedOpcodeAction unimplemented_opcode_action() const {
    return unimplemented_opcode_action_;
  }
56 57 58

  // Writes one disassembled instruction into 'buffer' (0-terminated).
  // Returns the length of the disassembled machine instruction in bytes.
59
  int InstructionDecode(v8::internal::Vector<char> buffer, byte* instruction);
60

61 62 63 64
  // Returns -1 if instruction does not mark the beginning of a constant pool,
  // or the number of entries in the constant pool beginning here.
  int ConstantPoolSizeAt(byte* instruction);

65 66
  // Write disassembly into specified file 'f' using specified NameConverter
  // (see constructor).
67 68 69
  static void Disassemble(FILE* f, byte* begin, byte* end,
                          UnimplementedOpcodeAction unimplemented_action =
                              kAbortOnUnimplementedOpcode);
70

71 72
 private:
  const NameConverter& converter_;
73
  const UnimplementedOpcodeAction unimplemented_opcode_action_;
74 75

  DISALLOW_IMPLICIT_CONSTRUCTORS(Disassembler);
76 77 78 79 80
};

}  // namespace disasm

#endif  // V8_DISASM_H_