1// Copyright 2013, ARM Limited
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7//   * Redistributions of source code must retain the above copyright notice,
8//     this list of conditions and the following disclaimer.
9//   * Redistributions in binary form must reproduce the above copyright notice,
10//     this list of conditions and the following disclaimer in the documentation
11//     and/or other materials provided with the distribution.
12//   * Neither the name of ARM Limited nor the names of its contributors may be
13//     used to endorse or promote products derived from this software without
14//     specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef VIXL_A64_DISASM_A64_H
28#define VIXL_A64_DISASM_A64_H
29
30#include "globals-vixl.h"
31#include "utils-vixl.h"
32#include "instructions-a64.h"
33#include "decoder-a64.h"
34
35namespace vixl {
36
37class Disassembler: public DecoderVisitor {
38 public:
39  Disassembler();
40  Disassembler(char* text_buffer, int buffer_size);
41  virtual ~Disassembler();
42  char* GetOutput();
43
44  // Declare all Visitor functions.
45  #define DECLARE(A)  void Visit##A(Instruction* instr);
46  VISITOR_LIST(DECLARE)
47  #undef DECLARE
48
49 protected:
50  virtual void ProcessOutput(Instruction* instr);
51
52 private:
53  void Format(Instruction* instr, const char* mnemonic, const char* format);
54  void Substitute(Instruction* instr, const char* string);
55  int SubstituteField(Instruction* instr, const char* format);
56  int SubstituteRegisterField(Instruction* instr, const char* format);
57  int SubstituteImmediateField(Instruction* instr, const char* format);
58  int SubstituteLiteralField(Instruction* instr, const char* format);
59  int SubstituteBitfieldImmediateField(Instruction* instr, const char* format);
60  int SubstituteShiftField(Instruction* instr, const char* format);
61  int SubstituteExtendField(Instruction* instr, const char* format);
62  int SubstituteConditionField(Instruction* instr, const char* format);
63  int SubstitutePCRelAddressField(Instruction* instr, const char* format);
64  int SubstituteBranchTargetField(Instruction* instr, const char* format);
65  int SubstituteLSRegOffsetField(Instruction* instr, const char* format);
66  int SubstitutePrefetchField(Instruction* instr, const char* format);
67  int SubstituteBarrierField(Instruction* instr, const char* format);
68
69  inline bool RdIsZROrSP(Instruction* instr) const {
70    return (instr->Rd() == kZeroRegCode);
71  }
72
73  inline bool RnIsZROrSP(Instruction* instr) const {
74    return (instr->Rn() == kZeroRegCode);
75  }
76
77  inline bool RmIsZROrSP(Instruction* instr) const {
78    return (instr->Rm() == kZeroRegCode);
79  }
80
81  inline bool RaIsZROrSP(Instruction* instr) const {
82    return (instr->Ra() == kZeroRegCode);
83  }
84
85  bool IsMovzMovnImm(unsigned reg_size, uint64_t value);
86
87  void ResetOutput();
88  void AppendToOutput(const char* string, ...);
89
90  char* buffer_;
91  uint32_t buffer_pos_;
92  uint32_t buffer_size_;
93  bool own_buffer_;
94};
95
96
97class PrintDisassembler: public Disassembler {
98 public:
99  explicit PrintDisassembler(FILE* stream) : stream_(stream) { }
100  ~PrintDisassembler() { }
101
102 protected:
103  virtual void ProcessOutput(Instruction* instr);
104
105 private:
106  FILE *stream_;
107};
108}  // namespace vixl
109
110#endif  // VIXL_A64_DISASM_A64_H
111