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