1 /* src/vm/jit/powerpc64/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 $
38 #include "vm/jit/powerpc64/codegen.h"
40 #include "vm/global.h"
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/stacktrace.h"
45 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
46 #include "vmcore/options.h" /* XXX debug */
47 #include "vm/jit/disass.h" /* XXX debug */
51 /* md_init *********************************************************************
53 Do some machine dependent initialization.
55 *******************************************************************************/
63 /* md_stacktrace_get_returnaddress *********************************************
65 Returns the return address of the current stackframe, specified by
66 the passed stack pointer and the stack frame size.
68 *******************************************************************************/
70 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
74 /* on PowerPC the return address is located in the linkage area */
76 ra = *((u1 **) (sp + framesize + LA_LR_OFFSET));
81 /* md_codegen_patch_branch *****************************************************
83 Back-patches a branch instruction.
84 Changes the dispacment of the jump instruction.
86 *******************************************************************************/
88 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
91 s4 disp; /* branch displacement */
94 /* calculate the patch position */
95 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
97 /* Calculate the branch displacement. */
98 disp = targetmpc - branchmpc + 4;
101 if ((mcode & 0xfc000000) == 0x40000000) {
102 /* conditional jump bcx */
103 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
104 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
107 mcode |= (((disp)& M_BCMASK));
108 } else if ((mcode & 0xfc000000) == 0x48000000) {
109 /* unconditional jump bx */
110 if ((disp < (s4) 0xfc000000) || (disp > (s4) 0x03ffffff))
111 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x0cffffff);
113 mcode |= (((disp)& M_BMASK));
115 vm_abort("md_codegen_patch_branch, patching unsupported branch: %xd", mcode);
118 mcodeptr[-1] = mcode;
123 /* md_get_method_patch_address *************************************************
125 Gets the patch address of the currently compiled method. The offset
126 is extracted from the load instruction(s) before the jump and added
127 to the right base address (PV or REG_METHODPTR).
129 INVOKESTATIC/SPECIAL:
131 e9ceffb8 ld r14,-72(r14)
137 FIXME 81830000 lwz r12,0(r3)
138 e9cc0000 ld r14,0(r12)
145 FIXME 81830000 lwz r12,0(r3)
146 FIXME 818c0000 lwz r12,0(r12)
147 FIXME 81ac0000 lwz r13,0(r12)
151 *******************************************************************************/
153 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
159 /* go back to the actual load instruction (3 instructions) */
163 /* get first instruction word (lwz) */
165 mcode = *((u4 *) ra);
167 /* check if we have 2 instructions (addis, addi) */
169 if ((mcode >> 16) == 0x3c19) {
170 /* XXX write a regression for this */
174 /* get displacement of first instruction (addis) */
176 offset = (s4) (mcode << 16);
178 /* get displacement of second instruction (addi) */
180 mcode = *((u4 *) (ra + 1 * 4));
182 assert((mcode >> 16) != 0x6739);
184 offset += (s2) (mcode & 0x0000ffff);
187 /* get the offset from the instruction */
189 offset = (s2) (mcode & 0x0000ffff);
191 /* check for load from PV */
193 if ((mcode >> 16) == 0xe9ce) {
194 /* get the final data segment address */
196 pa = sfi->pv + offset;
198 else if ((mcode >> 16) == 0xe9cc) {
199 /* in this case we use the passed method pointer */
201 /* return NULL if no mptr was specified (used for replacement) */
209 /* catch any problems */
211 vm_abort("md_get_method_patch_address: unknown instruction %x",
214 /* keep compiler happy */
224 /* md_codegen_get_pv_from_pc ***************************************************
229 39cbffe0 addi r14,r11,-32
234 3dcbffff addis r14,r11,-1
235 39ce68b0 addi r14,r13,26800
237 *******************************************************************************/
239 u1 *md_codegen_get_pv_from_pc(u1 *ra)
245 /* get first instruction word after jump */
247 mcode = *((u4 *) (ra + 1 * 4));
249 /* check if we have 2 instructions (addis, addi) */
251 if ((mcode >> 16) == 0x3dcb) {
252 /* get displacement of first instruction (addis) */
254 offset = (s4) (mcode << 16);
256 /* get displacement of second instruction (addi) */
258 mcode = *((u4 *) (ra + 2 * 4));
260 /* check for addi instruction */
262 assert((mcode >> 16) == 0x39ce);
264 offset += (s2) (mcode & 0x0000ffff);
266 else if ((mcode >> 16) == 0x39cb) {
267 /* get offset of first instruction (addi) */
269 offset = (s2) (mcode & 0x0000ffff);
272 vm_abort("md_codegen_get_pv_from_pc: unknown instruction %x", mcode);
274 /* keep compiler happy */
279 /* calculate PV via RA + offset */
287 /* md_cacheflush ***************************************************************
289 Calls the system's function to flush the instruction and data
292 *******************************************************************************/
294 void md_cacheflush(u1 *addr, s4 nbytes)
296 asm_cacheflush(addr, nbytes);
300 /* md_icacheflush **************************************************************
302 Calls the system's function to flush the instruction cache.
304 *******************************************************************************/
306 void md_icacheflush(u1 *addr, s4 nbytes)
308 asm_cacheflush(addr, nbytes);
312 /* md_dcacheflush **************************************************************
314 Calls the system's function to flush the data cache.
316 *******************************************************************************/
318 void md_dcacheflush(u1 *addr, s4 nbytes)
320 asm_cacheflush(addr, nbytes);
324 /* md_patch_replacement_point **************************************************
326 Patch the given replacement point.
328 *******************************************************************************/
330 #if defined(ENABLE_REPLACEMENT)
331 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
336 /* restore the patched-over instruction */
337 *(u4*)(rp->pc) = *(u4*)(savedmcode);
340 /* save the current machine code */
341 *(u4*)(savedmcode) = *(u4*)(rp->pc);
343 /* build the machine code for the patch */
344 assert(0); /* XXX build trap instruction below */
347 /* write the new machine code */
348 *(u4*)(rp->pc) = (u4) mcode;
351 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
359 /* flush instruction cache */
360 md_icacheflush(rp->pc,4);
362 #endif /* defined(ENABLE_REPLACEMENT) */
365 * These are local overrides for various environment variables in Emacs.
366 * Please do not remove this and leave it at the end of the file, where
367 * Emacs will automagically detect them.
368 * ---------------------------------------------------------------------
371 * indent-tabs-mode: t
375 * vim:noexpandtab:sw=4:ts=4: