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