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 5940 2006-11-09 09:59:28Z tbfg $
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 */
205 /* catch any problems */
215 /* md_codegen_get_pv_from_pc ***************************************************
220 39cbffe0 addi r14,r11,-32
225 3dcbffff addis r14,r11,-1
226 39ce68b0 addi r14,r13,26800
228 *******************************************************************************/
230 u1 *md_codegen_get_pv_from_pc(u1 *ra)
236 /* get first instruction word after jump */
238 mcode = *((u4 *) (ra + 1 * 4));
240 /* check if we have 2 instructions (addis, addi) */
242 if ((mcode >> 16) == 0x3dcb) {
243 /* get displacement of first instruction (addis) */
245 offset = (s4) (mcode << 16);
247 /* get displacement of second instruction (addi) */
249 mcode = *((u4 *) (ra + 2 * 4));
251 /* check for addi instruction */
253 assert((mcode >> 16) == 0x39ce);
255 offset += (s2) (mcode & 0x0000ffff);
258 /* check for addi instruction */
260 assert((mcode >> 16) == 0x39cb);
262 /* get offset of first instruction (addi) */
264 offset = (s2) (mcode & 0x0000ffff);
267 /* calculate PV via RA + offset */
275 /* md_cacheflush ***************************************************************
277 Calls the system's function to flush the instruction and data
280 *******************************************************************************/
282 void md_cacheflush(u1 *addr, s4 nbytes)
284 asm_cacheflush(addr, nbytes);
288 /* md_icacheflush **************************************************************
290 Calls the system's function to flush the instruction cache.
292 *******************************************************************************/
294 void md_icacheflush(u1 *addr, s4 nbytes)
296 asm_cacheflush(addr, nbytes);
300 /* md_dcacheflush **************************************************************
302 Calls the system's function to flush the data cache.
304 *******************************************************************************/
306 void md_dcacheflush(u1 *addr, s4 nbytes)
308 asm_cacheflush(addr, nbytes);
312 /* md_patch_replacement_point **************************************************
314 Patch the given replacement point.
316 *******************************************************************************/
318 void md_patch_replacement_point(rplpoint *rp)
322 /* save the current machine code */
323 mcode = *(u4*)rp->pc;
325 /* write the new machine code */
326 *(u4*)(rp->pc) = (u4) rp->mcode;
328 /* store saved mcode */
331 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
339 /* flush instruction cache */
340 md_icacheflush(rp->pc,4);
344 * These are local overrides for various environment variables in Emacs.
345 * Please do not remove this and leave it at the end of the file, where
346 * Emacs will automagically detect them.
347 * ---------------------------------------------------------------------
350 * indent-tabs-mode: t
354 * vim:noexpandtab:sw=4:ts=4: