1 /* src/vm/jit/mips/md.c - machine dependent MIPS 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 6180 2006-12-11 23:29:26Z twisti $
39 #include <sys/cachectl.h>
43 #include "toolbox/logging.h"
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
47 #include "vm/jit/stacktrace.h"
49 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
50 #include "vm/options.h" /* XXX debug */
51 #include "vm/jit/disass.h" /* XXX debug */
55 /* md_codegen_patch_branch *****************************************************
57 Back-patches a branch instruction.
59 *******************************************************************************/
61 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
65 s4 disp; /* branch displacement */
69 /* calculate the patch position */
71 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
73 /* get the instruction before the exception point */
77 /* check for: ori t9,t9,0 */
79 if ((mcode >> 16) == 0x3739) {
80 /* Calculate the branch displacement. For jumps we need a
81 displacement relative to PV. */
86 hi = (short) ((disp - lo) >> 16);
88 /* patch the two instructions before the mcodeptr */
90 mcodeptr[-2] |= (hi & 0x0000ffff);
91 mcodeptr[-1] |= (lo & 0x0000ffff);
94 /* Calculate the branch displacement. For branches we need a
95 displacement relative and shifted to the branch PC. */
97 disp = (targetmpc - branchmpc) >> 2;
99 /* On the MIPS we can only branch signed 16-bit instruction words
100 (signed 18-bit = 32KB = +/- 16KB). Check this! */
102 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
103 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
105 /* patch the branch instruction before the mcodeptr */
107 mcodeptr[-1] |= (disp & 0x0000ffff);
112 /* md_stacktrace_get_returnaddress *********************************************
114 Returns the return address of the current stackframe, specified by
115 the passed stack pointer and the stack frame size.
117 *******************************************************************************/
119 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
123 /* on MIPS the return address is located on the top of the stackframe */
125 /* XXX change this if we ever want to use 4-byte stackslots */
126 /* ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P)); */
127 ra = *((u1 **) (sp + framesize - 8));
133 /* md_get_method_patch_address *************************************************
135 Gets the patch address of the currently compiled method. The offset
136 is extracted from the load instruction(s) before the jump and added
137 to the right base address (PV or REG_METHODPTR).
139 INVOKESTATIC/SPECIAL:
141 dfdeffb8 ld s8,-72(s8)
155 df39ff90 ld t9,-112(t9)
156 df3e0018 ld s8,24(t9)
160 *******************************************************************************/
162 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
168 /* go back to the actual load instruction (3 instructions on MIPS) */
172 /* get first instruction word on current PC */
174 mcode = *((u4 *) ra);
176 /* check if we have 2 instructions (lui) */
178 if ((mcode >> 16) == 0x3c19) {
179 /* XXX write a regression for this */
182 /* get displacement of first instruction (lui) */
184 offset = (s4) (mcode << 16);
186 /* get displacement of second instruction (daddiu) */
188 mcode = *((u4 *) (ra + 1 * 4));
190 assert((mcode >> 16) != 0x6739);
192 offset += (s2) (mcode & 0x0000ffff);
195 /* get first instruction (ld) */
197 mcode = *((u4 *) ra);
199 /* get the offset from the instruction */
201 offset = (s2) (mcode & 0x0000ffff);
203 /* check for call with REG_METHODPTR: ld s8,x(t9) */
205 #if SIZEOF_VOID_P == 8
206 if ((mcode >> 16) == 0xdf3e) {
208 if ((mcode >> 16) == 0x8f3e) {
210 /* in this case we use the passed method pointer */
212 /* return NULL if no mptr was specified (used for replacement) */
220 /* in the normal case we check for a `ld s8,x(s8)' instruction */
222 #if SIZEOF_VOID_P == 8
223 assert((mcode >> 16) == 0xdfde);
225 assert((mcode >> 16) == 0x8fde);
228 /* and get the final data segment address */
230 pa = sfi->pv + offset;
238 /* md_codegen_get_pv_from_pc ***************************************************
244 27feff9c addiu s8,ra,-100
246 *******************************************************************************/
248 u1 *md_codegen_get_pv_from_pc(u1 *ra)
254 /* get the offset of the instructions */
256 /* get first instruction word after jump */
258 mcode = *((u4 *) ra);
260 /* check if we have 2 instructions (lui, daddiu) */
262 if ((mcode >> 16) == 0x3c19) {
263 /* get displacement of first instruction (lui) */
265 offset = (s4) (mcode << 16);
267 /* get displacement of second instruction (daddiu) */
269 mcode = *((u4 *) (ra + 1 * 4));
271 #if SIZEOF_VOID_P == 8
272 assert((mcode >> 16) == 0x6739);
274 assert((mcode >> 16) == 0x2739);
277 offset += (s2) (mcode & 0x0000ffff);
280 /* get offset of first instruction (daddiu) */
282 mcode = *((u4 *) ra);
284 #if SIZEOF_VOID_P == 8
285 assert((mcode >> 16) == 0x67fe);
287 assert((mcode >> 16) == 0x27fe);
290 offset = (s2) (mcode & 0x0000ffff);
293 /* calculate PV via RA + offset */
301 /* md_cacheflush ***************************************************************
303 Calls the system's function to flush the instruction and data
306 *******************************************************************************/
308 void md_cacheflush(u1 *addr, s4 nbytes)
310 cacheflush(addr, nbytes, BCACHE);
314 /* md_icacheflush **************************************************************
316 Calls the system's function to flush the instruction cache.
318 *******************************************************************************/
320 void md_icacheflush(u1 *addr, s4 nbytes)
322 cacheflush(addr, nbytes, ICACHE);
326 /* md_dcacheflush **************************************************************
328 Calls the system's function to flush the data cache.
330 *******************************************************************************/
332 void md_dcacheflush(u1 *addr, s4 nbytes)
334 cacheflush(addr, nbytes, DCACHE);
338 /* md_patch_replacement_point **************************************************
340 Patch the given replacement point.
342 *******************************************************************************/
344 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp,
354 /* restore the patched-over instruction */
355 *(u8*)(rp->pc) = *(u8*)(savedmcode);
358 /* save the current machine code */
359 *(u8*)(savedmcode) = *(u8*)(rp->pc);
361 /* make machine code for patching */
363 disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
364 + index * REPLACEMENT_STUB_SIZE
367 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
369 new_internalerror("Jump offset is out of range: %d > +/-%d",
375 mcode.words[0] = (((0x04) << 26) | ((0) << 21) | ((0) << 16) | ((disp) & 0xffff));
376 mcode.words[1] = 0; /* NOP in delay slot */
378 /* write the new machine code */
379 *(u8*)(rp->pc) = mcode.both;
382 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
391 /* flush instruction cache */
392 md_icacheflush(rp->pc,2*4);
396 * These are local overrides for various environment variables in Emacs.
397 * Please do not remove this and leave it at the end of the file, where
398 * Emacs will automagically detect them.
399 * ---------------------------------------------------------------------
402 * indent-tabs-mode: t
406 * vim:noexpandtab:sw=4:ts=4: