1 /* src/vm/jit/i386/md.c - machine dependent i386 functions
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
30 $Id: md.c 6142 2006-12-07 23:02:52Z edwin $
41 #include "vm/global.h"
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/codegen-common.h"
45 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
46 #include "vm/options.h" /* XXX debug */
47 #include "vm/jit/disass.h" /* XXX debug */
51 /* md_init *********************************************************************
53 Do some machine dependent initialization.
55 *******************************************************************************/
63 /* md_codegen_patch_branch *****************************************************
65 Back-patches a branch instruction.
67 *******************************************************************************/
69 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
72 s4 disp; /* branch displacement */
74 /* calculate the patch position */
76 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
78 /* Calculate the branch displacement. */
80 disp = targetmpc - branchmpc;
82 /* I don't think we have to check for branch-displacement
83 overflow, +/-2GB should be enough. */
85 /* patch the branch instruction before the mcodeptr */
91 /* md_stacktrace_get_returnaddress *********************************************
93 Returns the return address of the current stackframe, specified by
94 the passed stack pointer and the stack frame size.
96 *******************************************************************************/
98 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
102 /* on i386 the return address is above the current stack frame */
104 ra = *((u1 **) (sp + framesize));
110 /* md_get_method_patch_address *************************************************
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).
116 INVOKESTATIC/SPECIAL:
118 b9 30 00 49 b7 mov $0xb7490030,%ecx
123 8b 08 mov (%eax),%ecx
124 8b 91 00 00 00 00 mov 0x0(%ecx),%edx
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
134 *******************************************************************************/
136 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
140 u1 *pa; /* patch address */
142 /* go back to the actual call instruction (2-bytes) */
146 /* get the last byte of the call */
150 /* check for the different calls */
153 /* INVOKESTATIC/SPECIAL */
155 /* patch address is 4-bytes before the call instruction */
159 else if (mcode == 0xd2) {
160 /* INVOKEVIRTUAL/INTERFACE */
162 /* return NULL if no mptr was specified (used for replacement) */
167 /* Get the offset from the instruction (the offset address is
168 4-bytes before the call instruction). */
170 offset = *((s4 *) (ra - 4));
172 /* add the offset to the method pointer */
177 /* catch any problems */
179 pa = NULL; /* avoid warnings */
181 vm_abort("couldn't find a proper call instruction sequence");
188 /* md_codegen_get_pv_from_pc ***************************************************
190 On this architecture just a wrapper function to
191 codegen_get_pv_from_pc.
193 *******************************************************************************/
195 u1 *md_codegen_get_pv_from_pc(u1 *ra)
199 /* Get the start address of the function which contains this
200 address from the method table. */
202 pv = codegen_get_pv_from_pc(ra);
208 /* md_cacheflush ***************************************************************
210 Calls the system's function to flush the instruction and data
213 *******************************************************************************/
215 void md_cacheflush(u1 *addr, s4 nbytes)
221 /* md_icacheflush **************************************************************
223 Calls the system's function to flush the instruction cache.
225 *******************************************************************************/
227 void md_icacheflush(u1 *addr, s4 nbytes)
233 /* md_dcacheflush **************************************************************
235 Calls the system's function to flush the data cache.
237 *******************************************************************************/
239 void md_dcacheflush(u1 *addr, s4 nbytes)
245 /* md_patch_replacement_point **************************************************
247 Patch the given replacement point.
249 *******************************************************************************/
251 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
256 /* XXX this is probably unsafe! */
259 /* write spinning instruction */
260 *(u2*)(rp->pc) = 0xebfe;
263 rp->pc[4] = savedmcode[4];
265 /* write first word */
266 *(u4*)(rp->pc) = *(u4*)(savedmcode);
269 /* save the current machine code */
270 *(u4*)(savedmcode) = *(u4*)(rp->pc);
271 savedmcode[4] = rp->pc[4];
273 /* build the machine code for the patch */
274 disp = (code->replacementstubs - rp->pc)
275 + index * REPLACEMENT_STUB_SIZE
278 mcode = 0xe9 | ((u8) disp << 8);
280 /* write spinning instruction */
281 *(u2*)(rp->pc) = 0xebfe;
284 rp->pc[4] = (mcode >> 32);
286 /* write first word */
287 *(u4*)(rp->pc) = (u4) mcode;
290 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
298 /* XXX if required asm_cacheflush(rp->pc,8); */
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 * ---------------------------------------------------------------------
308 * indent-tabs-mode: t
312 * vim:noexpandtab:sw=4:ts=4: