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
32 #include <sys/cachectl.h>
36 #include "toolbox/logging.h"
38 #include "vm/global.h"
41 #include "vm/jit/codegen-common.h" /* REMOVEME: only for codegendata */
42 #include "vm/jit/stacktrace.h"
44 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
45 #include "vmcore/options.h" /* XXX debug */
46 #include "vm/jit/disass.h" /* XXX debug */
50 /* md_stacktrace_get_returnaddress *********************************************
52 Returns the return address of the current stackframe, specified by
53 the passed stack pointer and the stack frame size.
55 *******************************************************************************/
57 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
61 /* on MIPS the return address is located on the top of the stackframe */
63 /* XXX change this if we ever want to use 4-byte stackslots */
64 /* ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P)); */
65 ra = *((u1 **) (sp + framesize - 8));
71 /* md_get_method_patch_address *************************************************
73 Gets the patch address of the currently compiled method. The offset
74 is extracted from the load instruction(s) before the jump and added
75 to the right base address (PV or REG_METHODPTR).
79 dfdeffb8 ld s8,-72(s8)
93 df39ff90 ld t9,-112(t9)
98 *******************************************************************************/
100 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
106 /* go back to the actual load instruction (3 instructions on MIPS) */
110 /* get first instruction word on current PC */
112 mcode = *((u4 *) ra);
114 /* check if we have 2 instructions (lui) */
116 if ((mcode >> 16) == 0x3c19) {
117 /* XXX write a regression for this */
120 /* get displacement of first instruction (lui) */
122 offset = (s4) (mcode << 16);
124 /* get displacement of second instruction (daddiu) */
126 mcode = *((u4 *) (ra + 1 * 4));
128 assert((mcode >> 16) != 0x6739);
130 offset += (s2) (mcode & 0x0000ffff);
135 /* get first instruction (ld) */
137 mcode = *((u4 *) ra);
139 /* get the offset from the instruction */
141 offset = (s2) (mcode & 0x0000ffff);
143 /* check for call with REG_METHODPTR: ld s8,x(t9) */
145 #if SIZEOF_VOID_P == 8
146 if ((mcode >> 16) == 0xdf3e) {
148 if ((mcode >> 16) == 0x8f3e) {
150 /* in this case we use the passed method pointer */
152 /* return NULL if no mptr was specified (used for replacement) */
160 /* in the normal case we check for a `ld s8,x(s8)' instruction */
162 #if SIZEOF_VOID_P == 8
163 assert((mcode >> 16) == 0xdfde);
165 assert((mcode >> 16) == 0x8fde);
168 /* and get the final data segment address */
170 pa = sfi->pv + offset;
178 /* md_codegen_get_pv_from_pc ***************************************************
184 27feff9c addiu s8,ra,-100
186 *******************************************************************************/
188 u1 *md_codegen_get_pv_from_pc(u1 *ra)
194 /* get the offset of the instructions */
196 /* get first instruction word after jump */
198 mcode = *((u4 *) ra);
200 /* check if we have 2 instructions (lui, daddiu) */
202 if ((mcode >> 16) == 0x3c19) {
203 /* get displacement of first instruction (lui) */
205 offset = (s4) (mcode << 16);
207 /* get displacement of second instruction (daddiu) */
209 mcode = *((u4 *) (ra + 1 * 4));
211 #if SIZEOF_VOID_P == 8
212 assert((mcode >> 16) == 0x6739);
214 assert((mcode >> 16) == 0x2739);
217 offset += (s2) (mcode & 0x0000ffff);
220 /* get offset of first instruction (daddiu) */
222 mcode = *((u4 *) ra);
224 #if SIZEOF_VOID_P == 8
225 assert((mcode >> 16) == 0x67fe);
227 assert((mcode >> 16) == 0x27fe);
230 offset = (s2) (mcode & 0x0000ffff);
233 /* calculate PV via RA + offset */
241 /* md_cacheflush ***************************************************************
243 Calls the system's function to flush the instruction and data
246 *******************************************************************************/
248 void md_cacheflush(u1 *addr, s4 nbytes)
250 cacheflush(addr, nbytes, BCACHE);
254 /* md_icacheflush **************************************************************
256 Calls the system's function to flush the instruction cache.
258 *******************************************************************************/
260 void md_icacheflush(u1 *addr, s4 nbytes)
262 cacheflush(addr, nbytes, ICACHE);
266 /* md_dcacheflush **************************************************************
268 Calls the system's function to flush the data cache.
270 *******************************************************************************/
272 void md_dcacheflush(u1 *addr, s4 nbytes)
274 cacheflush(addr, nbytes, DCACHE);
278 /* md_patch_replacement_point **************************************************
280 Patch the given replacement point.
282 *******************************************************************************/
284 #if defined(ENABLE_REPLACEMENT)
285 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp,
294 /* restore the patched-over instruction */
295 *(u8*)(rp->pc) = *(u8*)(savedmcode);
298 /* save the current machine code */
299 *(u8*)(savedmcode) = *(u8*)(rp->pc);
301 /* build the machine code for the patch */
302 assert(0); /* XXX build trap instruction below */
305 /* write the new machine code */
306 *(u8*)(rp->pc) = mcode.both;
309 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
318 /* flush instruction cache */
319 md_icacheflush(rp->pc,2*4);
321 #endif /* defined(ENABLE_REPLACEMENT) */
324 * These are local overrides for various environment variables in Emacs.
325 * Please do not remove this and leave it at the end of the file, where
326 * Emacs will automagically detect them.
327 * ---------------------------------------------------------------------
330 * indent-tabs-mode: t
334 * vim:noexpandtab:sw=4:ts=4: