* generic_symbol_at_address: Added, they were implemented in dis-buf.c
[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 4026 2006-01-01 16:39:15Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include <assert.h>
40 #include <stdarg.h>
41
42 #include "vm/types.h"
43
44 #include "mm/memory.h"
45 #include "vm/jit/x86_64/dis-asm.h"
46
47
48 /* global variables ***********************************************************/
49
50 char mylinebuf[512];
51 int mylen;
52
53
54 /* name table for 16 integer registers */
55
56 char *regs[] = {
57         "rax",
58         "rcx",
59         "rdx",
60         "rbx",
61         "rsp",
62         "rbp",
63         "rsi",
64         "rdi",
65     "r8",
66     "r9",
67     "r10",
68     "r11",
69     "r12",
70     "r13",
71     "r14",
72     "r15"
73 };
74
75
76 /* myprintf ********************************************************************
77
78    Required by i386-dis.c, prints the stuff into a buffer.
79
80 *******************************************************************************/
81
82 void myprintf(PTR p, const char *fmt, ...)
83 {
84         va_list ap;
85         va_start(ap, fmt);
86         mylen += vsprintf(mylinebuf + mylen, fmt, ap);
87         va_end(ap);
88 }
89
90
91 /* buffer_read_memory **********************************************************
92
93    Required by i386-dis.c, copy some stuff to another memory.
94
95 *******************************************************************************/
96
97 int buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
98 {
99         MCOPY(myaddr, (void *) memaddr, u1, length);
100
101         return 0;
102 }
103
104
105 /* generic_symbol_at_address ***************************************************
106
107    Required by i386-dis.c, just return 1.
108
109 *******************************************************************************/
110
111 int generic_symbol_at_address(bfd_vma addr, struct disassemble_info *info)
112 {
113         return 1;
114 }
115
116
117 /* generic_print_address *******************************************************
118
119    Required by i386-dis.c, just print the address.
120
121 *******************************************************************************/
122
123 void generic_print_address(bfd_vma addr, struct disassemble_info *info)
124 {
125         myprintf(info->stream, "0x%016lx", addr);
126 }
127
128
129 /* perror_memory ***************************************************************
130
131    Required by i386-dis.c, jsut assert in case.
132
133 *******************************************************************************/
134
135 void perror_memory(int status, bfd_vma memaddr, struct disassemble_info *info)
136 {
137         assert(0);
138 }
139
140
141 /* disassinstr *****************************************************************
142
143    Outputs a disassembler listing of one machine code instruction on
144    `stdout'.
145
146    code: pointer to machine code
147
148 *******************************************************************************/
149
150 u1 *disassinstr(u1 *code)
151 {
152         static disassemble_info info;
153         static int dis_initialized;
154         s4 seqlen;
155         s4 i;
156
157         if (!dis_initialized) {
158                 INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
159                 info.mach = bfd_mach_x86_64;
160                 dis_initialized = 1;
161         }
162
163         printf("0x%016lx:   ", (s8) code);
164         mylen = 0;
165         seqlen = print_insn_i386((bfd_vma) code, &info);
166
167         for (i = 0; i < seqlen; i++) {
168                 printf("%02x ", *(code + i));
169         }
170         
171         for (; i < 10; i++) {
172                 printf("   ");
173         }
174
175         printf("   %s\n", mylinebuf);
176
177         return code + seqlen;
178 }
179
180
181 /* disassemble *****************************************************************
182
183    Outputs a disassembler listing of some machine code on `stdout'.
184
185    code: pointer to first instruction
186    len:  code size
187
188 *******************************************************************************/
189
190 void disassemble(u1 *start, u1 *end)
191 {
192         disassemble_info info;
193
194         INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
195         info.mach = bfd_mach_x86_64;
196
197         printf("  --- disassembler listing ---\n");
198         for (; start < end; )
199                 start = disassinstr(start);
200 }
201
202
203 /*
204  * These are local overrides for various environment variables in Emacs.
205  * Please do not remove this and leave it at the end of the file, where
206  * Emacs will automagically detect them.
207  * ---------------------------------------------------------------------
208  * Local variables:
209  * mode: c
210  * indent-tabs-mode: t
211  * c-basic-offset: 4
212  * tab-width: 4
213  * End:
214  */