635861613e7fc77ac3bb539e2ff9218f7ae1e465
[cacao.git] / src / vm / jit / i386 / disass.c
1 /* jit/i386/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 665 2003-11-21 18:36:43Z jowenn $
34
35 */
36
37
38 #include <stdarg.h>
39 #include <string.h>
40 #include "disass.h"
41 #include "dis-asm.h"
42
43
44 char mylinebuf[512];
45 int mylen;
46
47
48 char *regs[] = {
49         "eax",
50         "ecx",
51         "edx",
52         "ebx",
53         "esp",
54         "ebp",
55         "esi",
56         "edi"
57 };
58
59
60 void myprintf(PTR p, const char *fmt, ...)
61 {
62         va_list ap;
63         va_start(ap, fmt);
64         mylen += vsprintf(mylinebuf + mylen, fmt, ap);
65         va_end(ap);
66 }
67
68 int buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
69 {
70         if (length == 1)
71                 *myaddr = *((u1 *) memaddr);
72         else
73                 memcpy(myaddr, (void *) memaddr, length);
74         return 0;
75 }
76
77
78
79 /* function disassinstr ********************************************************
80
81         outputs a disassembler listing of one machine code instruction on 'stdout'
82         c:   instructions machine code
83         pos: instructions address relative to method start
84
85 *******************************************************************************/
86
87 int disassinstr(u1 *code, int pos)
88 {
89         static disassemble_info info;
90         static int dis_initialized;
91         int seqlen;
92         int i;
93
94         if (!dis_initialized) {
95                 INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
96                 info.mach = bfd_mach_i386_i386;
97                 dis_initialized = 1;
98         }
99
100         printf("0x%08x:   ", (s4) code);
101         mylen = 0;
102         seqlen = print_insn_i386((bfd_vma) code, &info);
103
104         for (i = 0; i < seqlen; i++) {
105                 printf("%02x ", *(code++));
106         }
107
108         for (; i < 8; i++) {
109                 printf("   ");
110         }
111
112         printf("   %s\n", mylinebuf);
113
114         return (seqlen - 1);
115 }
116
117
118
119 /* function disassemble ********************************************************
120
121         outputs a disassembler listing of some machine code on 'stdout'
122         code: pointer to first instruction
123         len:  code size (number of instructions * 4)
124
125 *******************************************************************************/
126
127 void disassemble(u1 *code, int len)
128 {
129         int p;
130         int seqlen;
131         int i;
132         disassemble_info info;
133
134         INIT_DISASSEMBLE_INFO(info, NULL, myprintf);
135         info.mach = bfd_mach_i386_i386;
136
137         printf("  --- disassembler listing ---\n");
138         for (p = 0; p < len;) {
139                 printf("0x%08x:   ", (s4) code);
140                 mylen = 0;
141
142                 seqlen = print_insn_i386((bfd_vma) code, &info);
143                 p += seqlen;
144                 /*              myprintf(NULL, "\n"); */
145
146                 for (i = 0; i < seqlen; i++) {
147                         printf("%02x ", *(code++));
148                 }
149
150                 for (; i < 8; i++) {
151                         printf("   ");
152                 }
153
154                 printf("   %s\n", mylinebuf);
155         }
156 }
157
158
159 /*
160  * These are local overrides for various environment variables in Emacs.
161  * Please do not remove this and leave it at the end of the file, where
162  * Emacs will automagically detect them.
163  * ---------------------------------------------------------------------
164  * Local variables:
165  * mode: c
166  * indent-tabs-mode: t
167  * c-basic-offset: 4
168  * tab-width: 4
169  * End:
170  */