vhost-user-test: do not reinvent glib-compat.h
[qemu/ar7.git] / disas / arm-a64.cc
blobb57256b267acea64b1a0e407e85939b4ee21e49e
1 /*
2 * ARM A64 disassembly output wrapper to libvixl
3 * Copyright (c) 2013 Linaro Limited
4 * Written by Claudio Fontana
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "a64/disasm-a64.h"
22 extern "C" {
23 #include "disas/bfd.h"
26 using namespace vixl;
28 static Decoder *vixl_decoder = NULL;
29 static Disassembler *vixl_disasm = NULL;
31 /* We don't use libvixl's PrintDisassembler because its output
32 * is a little unhelpful (trailing newlines, for example).
33 * Instead we use our own very similar variant so we have
34 * control over the format.
36 class QEMUDisassembler : public Disassembler {
37 public:
38 QEMUDisassembler() : printf_(NULL), stream_(NULL) { }
39 ~QEMUDisassembler() { }
41 void SetStream(FILE *stream) {
42 stream_ = stream;
45 void SetPrintf(fprintf_function printf_fn) {
46 printf_ = printf_fn;
49 protected:
50 virtual void ProcessOutput(const Instruction *instr) {
51 printf_(stream_, "%08" PRIx32 " %s",
52 instr->InstructionBits(), GetOutput());
55 private:
56 fprintf_function printf_;
57 FILE *stream_;
60 static int vixl_is_initialized(void)
62 return vixl_decoder != NULL;
65 static void vixl_init() {
66 vixl_decoder = new Decoder();
67 vixl_disasm = new QEMUDisassembler();
68 vixl_decoder->AppendVisitor(vixl_disasm);
71 #define INSN_SIZE 4
73 /* Disassemble ARM A64 instruction. This is our only entry
74 * point from QEMU's C code.
76 int print_insn_arm_a64(uint64_t addr, disassemble_info *info)
78 uint8_t bytes[INSN_SIZE];
79 uint32_t instrval;
80 const Instruction *instr;
81 int status;
83 status = info->read_memory_func(addr, bytes, INSN_SIZE, info);
84 if (status != 0) {
85 info->memory_error_func(status, addr, info);
86 return -1;
89 if (!vixl_is_initialized()) {
90 vixl_init();
93 ((QEMUDisassembler *)vixl_disasm)->SetPrintf(info->fprintf_func);
94 ((QEMUDisassembler *)vixl_disasm)->SetStream(info->stream);
96 instrval = bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24;
97 instr = reinterpret_cast<const Instruction *>(&instrval);
98 vixl_disasm->MapCodeAddress(addr, instr);
99 vixl_decoder->Decode(instr);
101 return INSN_SIZE;