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