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