1 /* src/vm/jit/mips/md.c - machine dependent MIPS 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 7483 2007-03-08 13:17:40Z michi $
34 #include <sys/cachectl.h>
38 #include "toolbox/logging.h"
40 #include "vm/global.h"
43 #include "vm/jit/codegen-common.h" /* REMOVEME: only for codegendata */
44 #include "vm/jit/stacktrace.h"
46 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
47 #include "vmcore/options.h" /* XXX debug */
48 #include "vm/jit/disass.h" /* XXX debug */
52 /* md_codegen_patch_branch *****************************************************
54 Back-patches a branch instruction.
56 *******************************************************************************/
58 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
62 s4 disp; /* branch displacement */
66 /* calculate the patch position */
68 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
70 /* get the instruction before the exception point */
74 /* check for: ori t9,t9,0 */
76 if ((mcode >> 16) == 0x3739) {
77 /* Calculate the branch displacement. For jumps we need a
78 displacement relative to PV. */
83 hi = (short) ((disp - lo) >> 16);
85 /* patch the two instructions before the mcodeptr */
87 mcodeptr[-2] |= (hi & 0x0000ffff);
88 mcodeptr[-1] |= (lo & 0x0000ffff);
91 /* Calculate the branch displacement. For branches we need a
92 displacement relative and shifted to the branch PC. */
94 disp = (targetmpc - branchmpc) >> 2;
96 /* On the MIPS we can only branch signed 16-bit instruction words
97 (signed 18-bit = 32KB = +/- 16KB). Check this! */
99 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff))
100 vm_abort("jump displacement is out of range: %d > +/-%d", disp, 0x00007fff);
102 /* patch the branch instruction before the mcodeptr */
104 mcodeptr[-1] |= (disp & 0x0000ffff);
109 /* md_stacktrace_get_returnaddress *********************************************
111 Returns the return address of the current stackframe, specified by
112 the passed stack pointer and the stack frame size.
114 *******************************************************************************/
116 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
120 /* on MIPS the return address is located on the top of the stackframe */
122 /* XXX change this if we ever want to use 4-byte stackslots */
123 /* ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P)); */
124 ra = *((u1 **) (sp + framesize - 8));
130 /* md_get_method_patch_address *************************************************
132 Gets the patch address of the currently compiled method. The offset
133 is extracted from the load instruction(s) before the jump and added
134 to the right base address (PV or REG_METHODPTR).
136 INVOKESTATIC/SPECIAL:
138 dfdeffb8 ld s8,-72(s8)
152 df39ff90 ld t9,-112(t9)
153 df3e0018 ld s8,24(t9)
157 *******************************************************************************/
159 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
165 /* go back to the actual load instruction (3 instructions on MIPS) */
169 /* get first instruction word on current PC */
171 mcode = *((u4 *) ra);
173 /* check if we have 2 instructions (lui) */
175 if ((mcode >> 16) == 0x3c19) {
176 /* XXX write a regression for this */
179 /* get displacement of first instruction (lui) */
181 offset = (s4) (mcode << 16);
183 /* get displacement of second instruction (daddiu) */
185 mcode = *((u4 *) (ra + 1 * 4));
187 assert((mcode >> 16) != 0x6739);
189 offset += (s2) (mcode & 0x0000ffff);
194 /* get first instruction (ld) */
196 mcode = *((u4 *) ra);
198 /* get the offset from the instruction */
200 offset = (s2) (mcode & 0x0000ffff);
202 /* check for call with REG_METHODPTR: ld s8,x(t9) */
204 #if SIZEOF_VOID_P == 8
205 if ((mcode >> 16) == 0xdf3e) {
207 if ((mcode >> 16) == 0x8f3e) {
209 /* in this case we use the passed method pointer */
211 /* return NULL if no mptr was specified (used for replacement) */
219 /* in the normal case we check for a `ld s8,x(s8)' instruction */
221 #if SIZEOF_VOID_P == 8
222 assert((mcode >> 16) == 0xdfde);
224 assert((mcode >> 16) == 0x8fde);
227 /* and get the final data segment address */
229 pa = sfi->pv + offset;
237 /* md_codegen_get_pv_from_pc ***************************************************
243 27feff9c addiu s8,ra,-100
245 *******************************************************************************/
247 u1 *md_codegen_get_pv_from_pc(u1 *ra)
253 /* get the offset of the instructions */
255 /* get first instruction word after jump */
257 mcode = *((u4 *) ra);
259 /* check if we have 2 instructions (lui, daddiu) */
261 if ((mcode >> 16) == 0x3c19) {
262 /* get displacement of first instruction (lui) */
264 offset = (s4) (mcode << 16);
266 /* get displacement of second instruction (daddiu) */
268 mcode = *((u4 *) (ra + 1 * 4));
270 #if SIZEOF_VOID_P == 8
271 assert((mcode >> 16) == 0x6739);
273 assert((mcode >> 16) == 0x2739);
276 offset += (s2) (mcode & 0x0000ffff);
279 /* get offset of first instruction (daddiu) */
281 mcode = *((u4 *) ra);
283 #if SIZEOF_VOID_P == 8
284 assert((mcode >> 16) == 0x67fe);
286 assert((mcode >> 16) == 0x27fe);
289 offset = (s2) (mcode & 0x0000ffff);
292 /* calculate PV via RA + offset */
300 /* md_cacheflush ***************************************************************
302 Calls the system's function to flush the instruction and data
305 *******************************************************************************/
307 void md_cacheflush(u1 *addr, s4 nbytes)
309 cacheflush(addr, nbytes, BCACHE);
313 /* md_icacheflush **************************************************************
315 Calls the system's function to flush the instruction cache.
317 *******************************************************************************/
319 void md_icacheflush(u1 *addr, s4 nbytes)
321 cacheflush(addr, nbytes, ICACHE);
325 /* md_dcacheflush **************************************************************
327 Calls the system's function to flush the data cache.
329 *******************************************************************************/
331 void md_dcacheflush(u1 *addr, s4 nbytes)
333 cacheflush(addr, nbytes, DCACHE);
337 /* md_patch_replacement_point **************************************************
339 Patch the given replacement point.
341 *******************************************************************************/
343 #if defined(ENABLE_REPLACEMENT)
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))
368 vm_abort("Jump offset is out of range: %d > +/-%d",
372 mcode.words[0] = (((0x04) << 26) | ((0) << 21) | ((0) << 16) | ((disp) & 0xffff));
373 mcode.words[1] = 0; /* NOP in delay slot */
375 /* write the new machine code */
376 *(u8*)(rp->pc) = mcode.both;
379 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
388 /* flush instruction cache */
389 md_icacheflush(rp->pc,2*4);
391 #endif /* defined(ENABLE_REPLACEMENT) */
394 * These are local overrides for various environment variables in Emacs.
395 * Please do not remove this and leave it at the end of the file, where
396 * Emacs will automagically detect them.
397 * ---------------------------------------------------------------------
400 * indent-tabs-mode: t
404 * vim:noexpandtab:sw=4:ts=4: