* Unified disassembler interface.
[cacao.git] / src / vm / jit / x86_64 / disass.c
1 /* src/vm/jit/x86_64/disass.c - wrapper functions for GNU binutils disassembler
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Andreas  Krall
28             Reinhard Grafl
29
30    Changes: Christian Thalinger
31
32    $Id: disass.c 3178 2005-09-14 18:14:34Z twisti $
33
34 */
35
36
37 #include <stdarg.h>
38
39 #include "mm/memory.h"
40 #include "vm/jit/x86_64/dis-asm.h"
41 #include "vm/jit/x86_64/types.h"
42
43
44 /* global variables ***********************************************************/
45
46 char mylinebuf[512];
47 int mylen;
48
49
50 /* name table for 16 integer registers */
51
52 char *regs[] = {
53         "rax",
54         "rcx",
55         "rdx",
56         "rbx",
57         "rsp",
58         "rbp",
59         "rsi",
60         "rdi",
61     "r8",
62     "r9",
63     "r10",
64     "r11",
65     "r12",
66     "r13",
67     "r14",
68     "r15"
69 };
70
71
72 void myprintf(PTR p, const char *fmt, ...)
73 {
74         va_list ap;
75         va_start(ap, fmt);
76         mylen += vsprintf(mylinebuf + mylen, fmt, ap);
77         va_end(ap);
78 }
79
80
81 int buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
82 {
83         if (length == 1) {
84                 *myaddr = *((u1 *) memaddr);
85
86         } else {
87                 MCOPY(myaddr, (void *) memaddr, u1, length);
88         }
89
90         return 0;
91 }
92
93
94 /* disassinstr *****************************************************************
95
96    Outputs a disassembler listing of one machine code instruction on
97    `stdout'.
98
99    code: pointer to machine code
100
101 *******************************************************************************/
102
103 u1 *disassinstr(u1 *code)
104 {
105         static disassemble_info info;
106         static int dis_initialized;
107         s4 seqlen;
108         s4 i;
109
110         if (!dis_initialized) {
111                 INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
112                 info.mach = bfd_mach_x86_64;
113                 dis_initialized = 1;
114         }
115
116         printf("0x%016lx:   ", (s8) code);
117         mylen = 0;
118         seqlen = print_insn_i386((bfd_vma) code, &info);
119
120         for (i = 0; i < seqlen; i++) {
121                 printf("%02x ", *(code + i));
122         }
123         
124         for (; i < 10; i++) {
125                 printf("   ");
126         }
127
128         printf("   %s\n", mylinebuf);
129
130         return code + seqlen;
131 }
132
133
134 /* disassemble *****************************************************************
135
136    Outputs a disassembler listing of some machine code on `stdout'.
137
138    code: pointer to first instruction
139    len:  code size
140
141 *******************************************************************************/
142
143 void disassemble(u1 *start, u1 *end)
144 {
145         disassemble_info info;
146
147         INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
148         info.mach = bfd_mach_x86_64;
149
150         printf("  --- disassembler listing ---\n");
151         for (; start < end; )
152                 start = disassinstr(start);
153 }
154
155
156 /*
157  * These are local overrides for various environment variables in Emacs.
158  * Please do not remove this and leave it at the end of the file, where
159  * Emacs will automagically detect them.
160  * ---------------------------------------------------------------------
161  * Local variables:
162  * mode: c
163  * indent-tabs-mode: t
164  * c-basic-offset: 4
165  * tab-width: 4
166  * End:
167  */