1 /* src/vm/jit/powerpc/md.c - machine dependent PowerPC 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 $
42 #include "vm/jit/powerpc/codegen.h"
44 #include "vm/global.h"
45 #include "vm/jit/asmpart.h"
47 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
48 #include "vm/options.h" /* XXX debug */
49 #include "vm/jit/disass.h" /* XXX debug */
53 /* md_init *********************************************************************
55 Do some machine dependent initialization.
57 *******************************************************************************/
65 /* md_codegen_patch_branch *****************************************************
67 Back-patches a branch instruction.
69 *******************************************************************************/
71 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
75 s4 disp; /* branch displacement */
77 /* calculate the patch position */
79 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
81 /* get the instruction before the exception point */
85 /* Calculate the branch displacement. */
87 disp = targetmpc - branchmpc + 4;
89 /* Check which branch instruction we have. Then mask it and patch
92 if ((mcode & 0xfc000000) == 0x48000000) {
95 if ((disp < (s4) 0xfe000000) || (disp > (s4) 0x01fffffc))
96 vm_abort("md_codegen_patch_branch: branch displacement out of range: %d > +/-%d", disp, 0x03fffffc);
98 mcode &= ~M_BMASK; /* mask out previous displacement, probably +4 */
99 mcode |= (disp & M_BMASK);
101 else if ((mcode & 0xfc000000) == 0x40000000) {
102 /* bcx (0x40000000) */
104 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
105 vm_abort("md_codegen_patch_branch: branch displacement out of range: %d > +/-%d", disp, 0x00007fff);
107 mcode &= ~M_BCMASK; /* mask out previous displacement, probably +4 */
108 mcode |= (disp & M_BCMASK);
111 vm_abort("md_codegen_patch_branch: unknown instruction 0x%08x", mcode);
113 /* patch the branch instruction before the mcodeptr */
115 mcodeptr[-1] = mcode;
119 /* md_stacktrace_get_returnaddress *********************************************
121 Returns the return address of the current stackframe, specified by
122 the passed stack pointer and the stack frame size.
124 *******************************************************************************/
126 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
130 /* on PowerPC the return address is located in the linkage area */
132 ra = *((u1 **) (sp + framesize + LA_LR_OFFSET));
138 /* md_get_method_patch_address *************************************************
140 Gets the patch address of the currently compiled method. The offset
141 is extracted from the load instruction(s) before the jump and added
142 to the right base address (PV or REG_METHODPTR).
144 INVOKESTATIC/SPECIAL:
146 81adffd4 lwz r13,-44(r13)
152 81830000 lwz r12,0(r3)
153 81ac0000 lwz r13,0(r12)
159 81830000 lwz r12,0(r3)
160 818c0000 lwz r12,0(r12)
161 81ac0000 lwz r13,0(r12)
165 *******************************************************************************/
167 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
173 /* go back to the actual load instruction (3 instructions) */
177 /* get first instruction word (lwz) */
179 mcode = *((u4 *) ra);
181 /* check if we have 2 instructions (addis, addi) */
183 if ((mcode >> 16) == 0x3c19) {
184 /* XXX write a regression for this */
187 /* get displacement of first instruction (addis) */
189 offset = (s4) (mcode << 16);
191 /* get displacement of second instruction (addi) */
193 mcode = *((u4 *) (ra + 1 * 4));
195 assert((mcode >> 16) != 0x6739);
197 offset += (s2) (mcode & 0x0000ffff);
200 /* get the offset from the instruction */
202 offset = (s2) (mcode & 0x0000ffff);
204 /* check for load from PV */
206 if ((mcode >> 16) == 0x81ad) {
207 /* get the final data segment address */
209 pa = sfi->pv + offset;
211 } else if ((mcode >> 16) == 0x81ac) {
212 /* in this case we use the passed method pointer */
214 /* return NULL if no mptr was specified (used for replacement) */
222 /* catch any problems */
232 /* md_codegen_get_pv_from_pc ***************************************************
237 39abffe0 addi r13,r11,-32
242 3dabffff addis r13,r11,-1
243 39ad68b0 addi r13,r13,26800
245 *******************************************************************************/
247 u1 *md_codegen_get_pv_from_pc(u1 *ra)
253 /* get first instruction word after jump */
255 mcode = *((u4 *) (ra + 1 * 4));
257 /* check if we have 2 instructions (addis, addi) */
259 if ((mcode >> 16) == 0x3dab) {
260 /* get displacement of first instruction (addis) */
262 offset = (s4) (mcode << 16);
264 /* get displacement of second instruction (addi) */
266 mcode = *((u4 *) (ra + 2 * 4));
268 /* check for addi instruction */
270 assert((mcode >> 16) == 0x39ad);
272 offset += (s2) (mcode & 0x0000ffff);
275 /* check for addi instruction */
277 assert((mcode >> 16) == 0x39ab);
279 /* get offset of first instruction (addi) */
281 offset = (s2) (mcode & 0x0000ffff);
284 /* calculate PV via RA + offset */
292 /* md_cacheflush ***************************************************************
294 Calls the system's function to flush the instruction and data
297 *******************************************************************************/
299 void md_cacheflush(u1 *addr, s4 nbytes)
301 asm_cacheflush(addr, nbytes);
305 /* md_icacheflush **************************************************************
307 Calls the system's function to flush the instruction cache.
309 *******************************************************************************/
311 void md_icacheflush(u1 *addr, s4 nbytes)
313 asm_cacheflush(addr, nbytes);
317 /* md_dcacheflush **************************************************************
319 Calls the system's function to flush the data cache.
321 *******************************************************************************/
323 void md_dcacheflush(u1 *addr, s4 nbytes)
325 asm_cacheflush(addr, nbytes);
329 /* md_patch_replacement_point **************************************************
331 Patch the given replacement point.
333 *******************************************************************************/
335 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
341 /* restore the patched-over instruction */
342 *(u4*)(rp->pc) = *(u4*)(savedmcode);
345 /* save the current machine code */
346 *(u4*)(savedmcode) = *(u4*)(rp->pc);
348 /* build the machine code for the patch */
349 disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
350 + index * REPLACEMENT_STUB_SIZE
353 mcode = (18 << 26) | ((((disp) * 4) + 4) & M_BMASK);
355 /* write the new machine code */
356 *(u4*)(rp->pc) = (u4) mcode;
359 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
367 /* flush instruction cache */
368 md_icacheflush(rp->pc,4);
372 * These are local overrides for various environment variables in Emacs.
373 * Please do not remove this and leave it at the end of the file, where
374 * Emacs will automagically detect them.
375 * ---------------------------------------------------------------------
378 * indent-tabs-mode: t
382 * vim:noexpandtab:sw=4:ts=4: