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
29 Changes: Edwin Steiner
31 $Id: md.c 4744 2006-04-06 12:51:53Z twisti $
40 #include <sys/cachectl.h>
44 #include "toolbox/logging.h"
45 #include "vm/global.h"
46 #include "vm/jit/stacktrace.h"
48 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
49 #include "vm/options.h" /* XXX debug */
50 #include "vm/jit/disass.h" /* XXX debug */
54 /* md_stacktrace_get_returnaddress *********************************************
56 Returns the return address of the current stackframe, specified by
57 the passed stack pointer and the stack frame size.
59 *******************************************************************************/
61 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
65 /* on MIPS the return address is located on the top of the stackframe */
67 /* XXX change this if we ever want to use 4-byte stackslots */
68 /* ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P)); */
69 ra = *((u1 **) (sp + framesize - 8));
75 /* md_assembler_get_patch_address **********************************************
77 Gets the patch address of the currently compiled method. The offset
78 is extracted from the load instruction(s) before the jump and added
79 to the right base address (PV or REG_METHODPTR).
81 *******************************************************************************/
83 u1 *md_assembler_get_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
89 /* go back to the actual load instruction (3 instructions on MIPS) */
93 /* get first instruction word on current PC */
97 /* check if we have 2 instructions (lui) */
99 if ((mcode >> 16) == 0x3c19) {
100 /* XXX write a regression for this */
103 /* get displacement of first instruction (lui) */
105 offset = (s4) (mcode << 16);
107 /* get displacement of second instruction (daddiu) */
109 mcode = *((u4 *) (ra + 1 * 4));
111 assert((mcode >> 16) != 0x6739);
113 offset += (s2) (mcode & 0x0000ffff);
116 /* get first instruction (ld) */
118 mcode = *((u4 *) ra);
120 /* get the offset from the instruction */
122 offset = (s2) (mcode & 0x0000ffff);
124 /* check for call with REG_METHODPTR: ld s8,x(t9) */
126 #if SIZEOF_VOID_P == 8
127 if ((mcode >> 16) == 0xdf3e) {
129 if ((mcode >> 16) == 0x8f3e) {
131 /* in this case we use the passed method pointer */
136 /* in the normal case we check for a `ld s8,x(s8)' instruction */
138 #if SIZEOF_VOID_P == 8
139 assert((mcode >> 16) == 0xdfde);
141 assert((mcode >> 16) == 0x8fde);
144 /* and get the final data segment address */
146 pa = sfi->pv + offset;
154 /* md_codegen_findmethod *******************************************************
160 27feff9c addiu s8,ra,-100
162 *******************************************************************************/
164 u1 *md_codegen_findmethod(u1 *ra)
170 /* get the offset of the instructions */
172 /* get first instruction word after jump */
174 mcode = *((u4 *) ra);
176 /* check if we have 2 instructions (lui, daddiu) */
178 if ((mcode >> 16) == 0x3c19) {
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 #if SIZEOF_VOID_P == 8
188 assert((mcode >> 16) == 0x6739);
190 assert((mcode >> 16) == 0x2739);
193 offset += (s2) (mcode & 0x0000ffff);
196 /* get offset of first instruction (daddiu) */
198 mcode = *((u4 *) ra);
200 #if SIZEOF_VOID_P == 8
201 assert((mcode >> 16) == 0x67fe);
203 assert((mcode >> 16) == 0x27fe);
206 offset = (s2) (mcode & 0x0000ffff);
209 /* calculate PV via RA + offset */
217 /* md_cacheflush ***************************************************************
219 Calls the system's function to flush the instruction and data
222 *******************************************************************************/
224 void md_cacheflush(u1 *addr, s4 nbytes)
226 cacheflush(addr, nbytes, BCACHE);
230 /* md_icacheflush **************************************************************
232 Calls the system's function to flush the instruction cache.
234 *******************************************************************************/
236 void md_icacheflush(u1 *addr, s4 nbytes)
238 cacheflush(addr, nbytes, ICACHE);
242 /* md_dcacheflush **************************************************************
244 Calls the system's function to flush the data cache.
246 *******************************************************************************/
248 void md_dcacheflush(u1 *addr, s4 nbytes)
250 cacheflush(addr, nbytes, DCACHE);
254 /* md_patch_replacement_point **************************************************
256 Patch the given replacement point.
258 *******************************************************************************/
260 void md_patch_replacement_point(rplpoint *rp)
264 /* save the current machine code */
265 mcode = *(u8*)rp->pc;
267 /* write the new machine code */
268 *(u8*)(rp->pc) = rp->mcode;
270 /* store saved mcode */
273 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
282 /* flush instruction cache */
283 md_icacheflush(rp->pc,2*4);
287 * These are local overrides for various environment variables in Emacs.
288 * Please do not remove this and leave it at the end of the file, where
289 * Emacs will automagically detect them.
290 * ---------------------------------------------------------------------
293 * indent-tabs-mode: t
297 * vim:noexpandtab:sw=4:ts=4: