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