1 /* src/vm/jit/powerpc64/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
29 Changes: Edwin Steiner
31 $Id: md.c 6140 2006-12-07 22:45:09Z edwin $
43 #include "vm/global.h"
44 #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_stacktrace_get_returnaddress *********************************************
67 Returns the return address of the current stackframe, specified by
68 the passed stack pointer and the stack frame size.
70 *******************************************************************************/
72 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
76 /* on PowerPC the return address is located in the linkage area */
78 ra = *((u1 **) (sp + framesize + LA_LR_OFFSET));
83 /* md_codegen_patch_branch *****************************************************
85 Back-patches a branch instruction.
86 Changes the dispacment of the jump instruction.
88 *******************************************************************************/
90 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
93 s4 disp; /* branch displacement */
96 /* calculate the patch position */
97 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
99 /* Calculate the branch displacement. */
100 disp = targetmpc - branchmpc + 4;
101 mcode = mcodeptr[-1];
103 if ((mcode & 0xfc000000) == 0x40000000) {
104 /* conditional jump bcx */
105 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
106 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
109 mcode |= (((disp)& M_BCMASK));
110 } else if ((mcode & 0xfc000000) == 0x48000000) {
111 /* unconditional jump bx */
112 if ((disp < (s4) 0xfc000000) || (disp > (s4) 0x03ffffff))
113 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x0cffffff);
115 mcode |= (((disp)& M_BMASK));
117 vm_abort("md_codegen_patch_branch, patching unsupported branch: %xd", mcode);
120 mcodeptr[-1] = mcode;
125 /* md_get_method_patch_address *************************************************
127 Gets the patch address of the currently compiled method. The offset
128 is extracted from the load instruction(s) before the jump and added
129 to the right base address (PV or REG_METHODPTR).
131 INVOKESTATIC/SPECIAL:
133 e9ceffb8 ld r14,-72(r14)
139 FIXME 81830000 lwz r12,0(r3)
140 e9cc0000 ld r14,0(r12)
147 FIXME 81830000 lwz r12,0(r3)
148 FIXME 818c0000 lwz r12,0(r12)
149 FIXME 81ac0000 lwz r13,0(r12)
153 *******************************************************************************/
155 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
161 /* go back to the actual load instruction (3 instructions) */
165 /* get first instruction word (lwz) */
167 mcode = *((u4 *) ra);
169 /* check if we have 2 instructions (addis, addi) */
171 if ((mcode >> 16) == 0x3c19) {
172 /* XXX write a regression for this */
175 /* get displacement of first instruction (addis) */
177 offset = (s4) (mcode << 16);
179 /* get displacement of second instruction (addi) */
181 mcode = *((u4 *) (ra + 1 * 4));
183 assert((mcode >> 16) != 0x6739);
185 offset += (s2) (mcode & 0x0000ffff);
188 /* get the offset from the instruction */
190 offset = (s2) (mcode & 0x0000ffff);
192 /* check for load from PV */
194 if ((mcode >> 16) == 0xe9ce) {
195 /* get the final data segment address */
197 pa = sfi->pv + offset;
199 } else if ((mcode >> 16) == 0xe9cc) {
200 /* in this case we use the passed method pointer */
202 /* return NULL if no mptr was specified (used for replacement) */
210 /* catch any problems */
220 /* md_codegen_get_pv_from_pc ***************************************************
225 39cbffe0 addi r14,r11,-32
230 3dcbffff addis r14,r11,-1
231 39ce68b0 addi r14,r13,26800
233 *******************************************************************************/
235 u1 *md_codegen_get_pv_from_pc(u1 *ra)
241 /* get first instruction word after jump */
243 mcode = *((u4 *) (ra + 1 * 4));
245 /* check if we have 2 instructions (addis, addi) */
247 if ((mcode >> 16) == 0x3dcb) {
248 /* get displacement of first instruction (addis) */
250 offset = (s4) (mcode << 16);
252 /* get displacement of second instruction (addi) */
254 mcode = *((u4 *) (ra + 2 * 4));
256 /* check for addi instruction */
258 assert((mcode >> 16) == 0x39ce);
260 offset += (s2) (mcode & 0x0000ffff);
263 /* check for addi instruction */
265 assert((mcode >> 16) == 0x39cb);
267 /* get offset of first instruction (addi) */
269 offset = (s2) (mcode & 0x0000ffff);
272 /* calculate PV via RA + offset */
280 /* md_cacheflush ***************************************************************
282 Calls the system's function to flush the instruction and data
285 *******************************************************************************/
287 void md_cacheflush(u1 *addr, s4 nbytes)
289 asm_cacheflush(addr, nbytes);
293 /* md_icacheflush **************************************************************
295 Calls the system's function to flush the instruction cache.
297 *******************************************************************************/
299 void md_icacheflush(u1 *addr, s4 nbytes)
301 asm_cacheflush(addr, nbytes);
305 /* md_dcacheflush **************************************************************
307 Calls the system's function to flush the data cache.
309 *******************************************************************************/
311 void md_dcacheflush(u1 *addr, s4 nbytes)
313 asm_cacheflush(addr, nbytes);
317 /* md_patch_replacement_point **************************************************
319 Patch the given replacement point.
321 *******************************************************************************/
323 void md_patch_replacement_point(rplpoint *rp)
327 /* save the current machine code */
328 mcode = *(u4*)rp->pc;
330 /* write the new machine code */
331 *(u4*)(rp->pc) = (u4) rp->mcode;
333 /* store saved mcode */
336 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
344 /* flush instruction cache */
345 md_icacheflush(rp->pc,4);
349 * These are local overrides for various environment variables in Emacs.
350 * Please do not remove this and leave it at the end of the file, where
351 * Emacs will automagically detect them.
352 * ---------------------------------------------------------------------
355 * indent-tabs-mode: t
359 * vim:noexpandtab:sw=4:ts=4: