1 /* src/vm/jit/powerpc/md.c - machine dependent PowerPC functions
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
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 $Id: md.c 8247 2007-07-31 12:06:44Z michi $
37 #include "vm/jit/powerpc/codegen.h"
39 #include "vm/global.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/stacktrace.h"
44 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
45 # include "vm/jit/disass.h" /* XXX debug */
46 # include "vmcore/options.h" /* XXX debug */
50 /* md_init *********************************************************************
52 Do some machine dependent initialization.
54 *******************************************************************************/
62 /* md_stacktrace_get_returnaddress *********************************************
64 Returns the return address of the current stackframe, specified by
65 the passed stack pointer and the stack frame size.
67 *******************************************************************************/
69 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
73 /* on PowerPC the return address is located in the linkage area */
75 ra = *((u1 **) (sp + framesize + LA_LR_OFFSET));
81 /* md_get_method_patch_address *************************************************
83 Gets the patch address of the currently compiled method. The offset
84 is extracted from the load instruction(s) before the jump and added
85 to the right base address (PV or REG_METHODPTR).
89 81adffd4 lwz r13,-44(r13)
95 81830000 lwz r12,0(r3)
96 81ac0000 lwz r13,0(r12)
102 81830000 lwz r12,0(r3)
103 818c0000 lwz r12,0(r12)
104 81ac0000 lwz r13,0(r12)
108 *******************************************************************************/
110 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
116 /* go back to the actual load instruction (3 instructions) */
120 /* get first instruction word (lwz) */
122 mcode = *((u4 *) ra);
124 /* check if we have 2 instructions (addis, addi) */
126 if ((mcode >> 16) == 0x3c19) {
127 /* XXX write a regression for this */
131 /* get displacement of first instruction (addis) */
133 offset = (s4) (mcode << 16);
135 /* get displacement of second instruction (addi) */
137 mcode = *((u4 *) (ra + 1 * 4));
139 assert((mcode >> 16) != 0x6739);
141 offset += (s2) (mcode & 0x0000ffff);
144 /* get the offset from the instruction */
146 offset = (s2) (mcode & 0x0000ffff);
148 /* check for load from PV */
150 if ((mcode >> 16) == 0x81ad) {
151 /* get the final data segment address */
153 pa = sfi->pv + offset;
155 else if ((mcode >> 16) == 0x81ac) {
156 /* in this case we use the passed method pointer */
158 /* return NULL if no mptr was specified (used for replacement) */
166 /* catch any problems */
168 vm_abort("md_get_method_patch_address: unknown instruction %x",
171 /* keep compiler happy */
181 /* md_codegen_get_pv_from_pc ***************************************************
186 39abffe0 addi r13,r11,-32
191 3dabffff addis r13,r11,-1
192 39ad68b0 addi r13,r13,26800
194 *******************************************************************************/
196 u1 *md_codegen_get_pv_from_pc(u1 *ra)
202 /* get first instruction word after jump */
204 mcode = *((u4 *) (ra + 1 * 4));
206 /* check if we have 2 instructions (addis, addi) */
208 if ((mcode >> 16) == 0x3dab) {
209 /* get displacement of first instruction (addis) */
211 offset = (s4) (mcode << 16);
213 /* get displacement of second instruction (addi) */
215 mcode = *((u4 *) (ra + 2 * 4));
217 /* check for addi instruction */
219 assert((mcode >> 16) == 0x39ad);
221 offset += (s2) (mcode & 0x0000ffff);
224 /* check for addi instruction */
226 assert((mcode >> 16) == 0x39ab);
228 /* get offset of first instruction (addi) */
230 offset = (s2) (mcode & 0x0000ffff);
233 /* calculate PV via RA + offset */
241 /* md_cacheflush ***************************************************************
243 Calls the system's function to flush the instruction and data
246 *******************************************************************************/
248 void md_cacheflush(u1 *addr, s4 nbytes)
250 asm_cacheflush(addr, nbytes);
254 /* md_icacheflush **************************************************************
256 Calls the system's function to flush the instruction cache.
258 *******************************************************************************/
260 void md_icacheflush(u1 *addr, s4 nbytes)
262 asm_cacheflush(addr, nbytes);
266 /* md_dcacheflush **************************************************************
268 Calls the system's function to flush the data cache.
270 *******************************************************************************/
272 void md_dcacheflush(u1 *addr, s4 nbytes)
274 asm_cacheflush(addr, nbytes);
278 /* md_patch_replacement_point **************************************************
280 Patch the given replacement point.
282 *******************************************************************************/
284 #if defined(ENABLE_REPLACEMENT)
285 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
290 /* restore the patched-over instruction */
291 *(u4*)(rp->pc) = *(u4*)(savedmcode);
294 /* save the current machine code */
295 *(u4*)(savedmcode) = *(u4*)(rp->pc);
297 /* build the machine code for the patch */
298 assert(0); /* XXX build trap instruction below */
301 /* write the new machine code */
302 *(u4*)(rp->pc) = (u4) mcode;
305 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
313 /* flush instruction cache */
314 md_icacheflush(rp->pc,4);
316 #endif /* defined(ENABLE_REPLACEMENT) */
319 * These are local overrides for various environment variables in Emacs.
320 * Please do not remove this and leave it at the end of the file, where
321 * Emacs will automagically detect them.
322 * ---------------------------------------------------------------------
325 * indent-tabs-mode: t
329 * vim:noexpandtab:sw=4:ts=4: