* Removed all Id tags.
[cacao.git] / src / vm / jit / i386 / md.c
1 /* src/vm/jit/i386/md.c - machine dependent i386 functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31
32 #include "vm/types.h"
33
34 #include "vm/global.h"
35
36 #include "vm/jit/asmpart.h"
37 #include "vm/jit/codegen-common.h"
38 #include "vm/jit/md.h"
39
40 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
41 #include "vmcore/options.h" /* XXX debug */
42 #include "vm/jit/disass.h" /* XXX debug */
43 #endif
44
45
46 /* md_init *********************************************************************
47
48    Do some machine dependent initialization.
49
50 *******************************************************************************/
51
52 void md_init(void)
53 {
54         (void) asm_md_init();
55 }
56
57
58 /* md_stacktrace_get_returnaddress *********************************************
59
60    Returns the return address of the current stackframe, specified by
61    the passed stack pointer and the stack frame size.
62
63 *******************************************************************************/
64
65 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
66 {
67         u1 *ra;
68
69         /* on i386 the return address is above the current stack frame */
70
71         ra = *((u1 **) (sp + framesize));
72
73         return ra;
74 }
75
76
77 /* md_get_method_patch_address *************************************************
78
79    Gets the patch address of the currently compiled method. The offset
80    is extracted from the load instruction(s) before the jump and added
81    to the right base address (PV or REG_METHODPTR).
82
83    INVOKESTATIC/SPECIAL:
84
85    b9 30 00 49 b7             mov    $0xb7490030,%ecx
86    ff d1                      call   *%ecx
87
88    INVOKEVIRTUAL:
89
90    8b 08                      mov    (%eax),%ecx
91    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
92    ff d2                      call   *%edx
93
94    INVOKEINTERFACE:
95
96    8b 08                      mov    (%eax),%ecx
97    8b 89 00 00 00 00          mov    0x0(%ecx),%ecx
98    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
99    ff d2                      call   *%edx
100
101 *******************************************************************************/
102
103 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
104 {
105         u1  mcode;
106         s4  offset;
107         u1 *pa;                             /* patch address                      */
108
109         /* go back to the actual call instruction (2-bytes) */
110
111         ra = ra - 2;
112
113         /* get the last byte of the call */
114
115         mcode = ra[1];
116
117         /* check for the different calls */
118
119         if (mcode == 0xd1) {
120                 /* INVOKESTATIC/SPECIAL */
121
122                 /* patch address is 4-bytes before the call instruction */
123
124                 pa = ra - 4;
125         }
126         else if (mcode == 0xd2) {
127                 /* INVOKEVIRTUAL/INTERFACE */
128
129                 /* return NULL if no mptr was specified (used for replacement) */
130
131                 if (mptr == NULL)
132                         return NULL;
133
134                 /* Get the offset from the instruction (the offset address is
135                    4-bytes before the call instruction). */
136
137                 offset = *((s4 *) (ra - 4));
138
139                 /* add the offset to the method pointer */
140
141                 pa = mptr + offset;
142         }
143         else {
144                 /* catch any problems */
145
146                 pa = NULL; /* avoid warnings */
147
148                 vm_abort("couldn't find a proper call instruction sequence");
149         }
150
151         return pa;
152 }
153
154
155 /* md_codegen_get_pv_from_pc ***************************************************
156
157    On this architecture just a wrapper function to
158    codegen_get_pv_from_pc.
159
160 *******************************************************************************/
161
162 u1 *md_codegen_get_pv_from_pc(u1 *ra)
163 {
164         u1 *pv;
165
166         /* Get the start address of the function which contains this
167        address from the method table. */
168
169         pv = codegen_get_pv_from_pc(ra);
170
171         return pv;
172 }
173
174
175 /* md_cacheflush ***************************************************************
176
177    Calls the system's function to flush the instruction and data
178    cache.
179
180 *******************************************************************************/
181
182 void md_cacheflush(u1 *addr, s4 nbytes)
183 {
184         /* do nothing */
185 }
186
187
188 /* md_icacheflush **************************************************************
189
190    Calls the system's function to flush the instruction cache.
191
192 *******************************************************************************/
193
194 void md_icacheflush(u1 *addr, s4 nbytes)
195 {
196         /* do nothing */
197 }
198
199
200 /* md_dcacheflush **************************************************************
201
202    Calls the system's function to flush the data cache.
203
204 *******************************************************************************/
205
206 void md_dcacheflush(u1 *addr, s4 nbytes)
207 {
208         /* do nothing */
209 }
210
211
212 /* md_patch_replacement_point **************************************************
213
214    Patch the given replacement point.
215
216 *******************************************************************************/
217
218 #if defined(ENABLE_REPLACEMENT)
219 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
220 {
221         u8 mcode;
222
223         /* XXX this is probably unsafe! */
224
225         if (index < 0) {
226                 /* write spinning instruction */
227                 *(u2*)(rp->pc) = 0xebfe;
228
229                 /* write 5th byte */
230                 rp->pc[4] = savedmcode[4];
231
232                 /* write first word */
233                 *(u4*)(rp->pc) = *(u4*)(savedmcode);
234         }
235         else {
236                 /* save the current machine code */
237                 *(u4*)(savedmcode) = *(u4*)(rp->pc);
238                 savedmcode[4] = rp->pc[4];
239
240                 /* build the machine code for the patch */
241                 assert(0); /* XXX build trap instruction below */
242                 mcode = 0;
243
244                 /* write spinning instruction */
245                 *(u2*)(rp->pc) = 0xebfe;
246
247                 /* write 5th byte */
248                 rp->pc[4] = (mcode >> 32);
249
250                 /* write first word */
251                 *(u4*)(rp->pc) = (u4) mcode;
252         }
253
254 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
255         {
256                 u1* u1ptr = rp->pc;
257                 DISASSINSTR(u1ptr);
258                 fflush(stdout);
259         }
260 #endif
261                         
262     /* XXX if required asm_cacheflush(rp->pc,8); */
263 }
264 #endif /* defined(ENABLE_REPLACEMENT) */
265
266 /*
267  * These are local overrides for various environment variables in Emacs.
268  * Please do not remove this and leave it at the end of the file, where
269  * Emacs will automagically detect them.
270  * ---------------------------------------------------------------------
271  * Local variables:
272  * mode: c
273  * indent-tabs-mode: t
274  * c-basic-offset: 4
275  * tab-width: 4
276  * End:
277  * vim:noexpandtab:sw=4:ts=4:
278  */