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