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 8247 2007-07-31 12:06:44Z 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_stacktrace_get_returnaddress *********************************************
54 Returns the return address of the current stackframe, specified by
55 the passed stack pointer and the stack frame size.
57 *******************************************************************************/
59 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
63 /* on MIPS the return address is located on the top of the stackframe */
65 /* XXX change this if we ever want to use 4-byte stackslots */
66 /* ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P)); */
67 ra = *((u1 **) (sp + framesize - 8));
73 /* md_get_method_patch_address *************************************************
75 Gets the patch address of the currently compiled method. The offset
76 is extracted from the load instruction(s) before the jump and added
77 to the right base address (PV or REG_METHODPTR).
81 dfdeffb8 ld s8,-72(s8)
95 df39ff90 ld t9,-112(t9)
100 *******************************************************************************/
102 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
108 /* go back to the actual load instruction (3 instructions on MIPS) */
112 /* get first instruction word on current PC */
114 mcode = *((u4 *) ra);
116 /* check if we have 2 instructions (lui) */
118 if ((mcode >> 16) == 0x3c19) {
119 /* XXX write a regression for this */
122 /* get displacement of first instruction (lui) */
124 offset = (s4) (mcode << 16);
126 /* get displacement of second instruction (daddiu) */
128 mcode = *((u4 *) (ra + 1 * 4));
130 assert((mcode >> 16) != 0x6739);
132 offset += (s2) (mcode & 0x0000ffff);
137 /* get first instruction (ld) */
139 mcode = *((u4 *) ra);
141 /* get the offset from the instruction */
143 offset = (s2) (mcode & 0x0000ffff);
145 /* check for call with REG_METHODPTR: ld s8,x(t9) */
147 #if SIZEOF_VOID_P == 8
148 if ((mcode >> 16) == 0xdf3e) {
150 if ((mcode >> 16) == 0x8f3e) {
152 /* in this case we use the passed method pointer */
154 /* return NULL if no mptr was specified (used for replacement) */
162 /* in the normal case we check for a `ld s8,x(s8)' instruction */
164 #if SIZEOF_VOID_P == 8
165 assert((mcode >> 16) == 0xdfde);
167 assert((mcode >> 16) == 0x8fde);
170 /* and get the final data segment address */
172 pa = sfi->pv + offset;
180 /* md_codegen_get_pv_from_pc ***************************************************
186 27feff9c addiu s8,ra,-100
188 *******************************************************************************/
190 u1 *md_codegen_get_pv_from_pc(u1 *ra)
196 /* get the offset of the instructions */
198 /* get first instruction word after jump */
200 mcode = *((u4 *) ra);
202 /* check if we have 2 instructions (lui, daddiu) */
204 if ((mcode >> 16) == 0x3c19) {
205 /* get displacement of first instruction (lui) */
207 offset = (s4) (mcode << 16);
209 /* get displacement of second instruction (daddiu) */
211 mcode = *((u4 *) (ra + 1 * 4));
213 #if SIZEOF_VOID_P == 8
214 assert((mcode >> 16) == 0x6739);
216 assert((mcode >> 16) == 0x2739);
219 offset += (s2) (mcode & 0x0000ffff);
222 /* get offset of first instruction (daddiu) */
224 mcode = *((u4 *) ra);
226 #if SIZEOF_VOID_P == 8
227 assert((mcode >> 16) == 0x67fe);
229 assert((mcode >> 16) == 0x27fe);
232 offset = (s2) (mcode & 0x0000ffff);
235 /* calculate PV via RA + offset */
243 /* md_cacheflush ***************************************************************
245 Calls the system's function to flush the instruction and data
248 *******************************************************************************/
250 void md_cacheflush(u1 *addr, s4 nbytes)
252 cacheflush(addr, nbytes, BCACHE);
256 /* md_icacheflush **************************************************************
258 Calls the system's function to flush the instruction cache.
260 *******************************************************************************/
262 void md_icacheflush(u1 *addr, s4 nbytes)
264 cacheflush(addr, nbytes, ICACHE);
268 /* md_dcacheflush **************************************************************
270 Calls the system's function to flush the data cache.
272 *******************************************************************************/
274 void md_dcacheflush(u1 *addr, s4 nbytes)
276 cacheflush(addr, nbytes, DCACHE);
280 /* md_patch_replacement_point **************************************************
282 Patch the given replacement point.
284 *******************************************************************************/
286 #if defined(ENABLE_REPLACEMENT)
287 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp,
296 /* restore the patched-over instruction */
297 *(u8*)(rp->pc) = *(u8*)(savedmcode);
300 /* save the current machine code */
301 *(u8*)(savedmcode) = *(u8*)(rp->pc);
303 /* build the machine code for the patch */
304 assert(0); /* XXX build trap instruction below */
307 /* write the new machine code */
308 *(u8*)(rp->pc) = mcode.both;
311 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
320 /* flush instruction cache */
321 md_icacheflush(rp->pc,2*4);
323 #endif /* defined(ENABLE_REPLACEMENT) */
326 * These are local overrides for various environment variables in Emacs.
327 * Please do not remove this and leave it at the end of the file, where
328 * Emacs will automagically detect them.
329 * ---------------------------------------------------------------------
332 * indent-tabs-mode: t
336 * vim:noexpandtab:sw=4:ts=4: