1 // Copyright 2007-2008 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 #ifndef V8_DISASM_H_ 6 #define V8_DISASM_H_ 7 8 #include "src/utils.h" 9 10 namespace disasm { 11 12 typedef unsigned char byte; 13 14 // Interface and default implementation for converting addresses and 15 // register-numbers to text. The default implementation is machine 16 // specific. 17 class NameConverter { 18 public: ~NameConverter()19 virtual ~NameConverter() {} 20 virtual const char* NameOfCPURegister(int reg) const; 21 virtual const char* NameOfByteCPURegister(int reg) const; 22 virtual const char* NameOfXMMRegister(int reg) const; 23 virtual const char* NameOfAddress(byte* addr) const; 24 virtual const char* NameOfConstant(byte* addr) const; 25 virtual const char* NameInCode(byte* addr) const; 26 27 // Given a root-relative offset, returns either a name or nullptr if none is 28 // found. 29 // TODO(jgruber,v8:7989): This is a temporary solution until we can preserve 30 // code comments through snapshotting. RootRelativeName(int offset)31 virtual const char* RootRelativeName(int offset) const { UNREACHABLE(); } 32 33 protected: 34 v8::internal::EmbeddedVector<char, 128> tmp_buffer_; 35 }; 36 37 38 // A generic Disassembler interface 39 class Disassembler { 40 public: 41 enum UnimplementedOpcodeAction : int8_t { 42 kContinueOnUnimplementedOpcode, 43 kAbortOnUnimplementedOpcode 44 }; 45 46 // Caller deallocates converter. 47 explicit Disassembler(const NameConverter& converter, 48 UnimplementedOpcodeAction unimplemented_opcode_action = 49 kAbortOnUnimplementedOpcode) converter_(converter)50 : converter_(converter), 51 unimplemented_opcode_action_(unimplemented_opcode_action) {} 52 unimplemented_opcode_action()53 UnimplementedOpcodeAction unimplemented_opcode_action() const { 54 return unimplemented_opcode_action_; 55 } 56 57 // Writes one disassembled instruction into 'buffer' (0-terminated). 58 // Returns the length of the disassembled machine instruction in bytes. 59 int InstructionDecode(v8::internal::Vector<char> buffer, byte* instruction); 60 61 // Returns -1 if instruction does not mark the beginning of a constant pool, 62 // or the number of entries in the constant pool beginning here. 63 int ConstantPoolSizeAt(byte* instruction); 64 65 // Write disassembly into specified file 'f' using specified NameConverter 66 // (see constructor). 67 static void Disassemble(FILE* f, byte* begin, byte* end, 68 UnimplementedOpcodeAction unimplemented_action = 69 kAbortOnUnimplementedOpcode); 70 71 private: 72 const NameConverter& converter_; 73 const UnimplementedOpcodeAction unimplemented_opcode_action_; 74 75 DISALLOW_IMPLICIT_CONSTRUCTORS(Disassembler); 76 }; 77 78 } // namespace disasm 79 80 #endif // V8_DISASM_H_ 81