1 /* src/vm/jit/sparc64/md.c - machine dependent SPARC 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 $Id: md.c 6265 2007-01-02 20:40:57Z edwin $
37 #include "vm/jit/sparc64/md-abi.h"
39 #include "vm/exceptions.h"
40 #include "vm/stringlocal.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/stacktrace.h"
44 /* assembler function prototypes **********************************************/
45 void asm_store_fp_state_reg(u8 *mem);
46 void asm_load_fp_state_reg(u8 *mem);
50 /* shift away 13-bit immediate, mask rd and rs1 */
51 #define SHIFT_AND_MASK(instr) \
52 ((instr >> 13) & 0x60fc1)
54 /* NOP is defined as a SETHI instruction with rd and imm. set to zero */
55 /* therefore we check if the 22-bit immediate is zero */
56 #define IS_SETHI(instr) \
57 (((instr & 0xc1c00000) == 0x01000000) \
58 && ((instr & 0x3fffff) != 0x0))
60 #define IS_LDX_IMM(instr) \
61 (((instr >> 13) & 0x60fc1) == 0x602c1)
63 #define IS_SUB(instr) \
64 (((instr >> 13) & 0x60fc0) == 0x40100)
66 inline s2 decode_13bit_imm(u4 instr) {
69 /* mask everything else in the instruction */
70 imm = instr & 0x00001fff;
72 /* sign extend 13-bit to 16-bit */
79 /* md_init *********************************************************************
81 Do some machine dependent initialization.
83 *******************************************************************************/
91 /* md_stacktrace_get_returnaddress *********************************************
93 Returns the return address of the current stackframe, specified by
94 the passed stack pointer and the stack frame size.
96 *******************************************************************************/
98 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
101 /* flush register windows to the stack */
104 /* the return address resides in register i7, the last register in the
105 * 16-extended-word save area
107 ra = *((u1 **) (sp + 120 + BIAS));
109 /* NOTE: on SPARC ra is the address of the call instruction */
114 u1 *md_get_framepointer(u1 *sp)
117 /* flush register windows to the stack */
120 fp = *((u1 **) (sp + 112 + BIAS));
125 u1 *md_get_pv_from_stackframe(u1 *sp)
128 /* flush register windows to the stack */
131 pv = *((u1 **) (sp + 104 + BIAS));
136 /* md_codegen_get_pv_from_pc ***************************************************
138 This reconstructs and returns the PV of a method given a return address
139 pointer. (basically, same was as the generated code following the jump does)
145 277afffe ldah pv,-2(ra)
146 237ba61c lda pv,-23012(pv)
148 *******************************************************************************/
150 u1 *md_codegen_get_pv_from_pc(u1 *ra)
158 /* get the instruction word after jump and nop */
159 mcode = *((u4 *) (ra+8) );
161 /* check if we have a sethi insruction */
162 if (IS_SETHI(mcode)) {
165 /* get 22-bit immediate of sethi instruction */
166 offset = (s4) (mcode & 0x3fffff);
167 offset = offset << 10;
170 mcode = *((u4 *) (ra+12) );
171 xor_imm = decode_13bit_imm(mcode);
178 mcode_masked = SHIFT_AND_MASK(mcode);
180 assert(mcode_masked == 0x40001);
182 /* mask and extend the negative sign for the 13 bit immediate */
183 offset = decode_13bit_imm(mcode);
191 /* md_get_method_patch_address *************************************************
193 Gets the patch address of the currently compiled method. The offset
194 is extracted from the load instruction(s) before the jump and added
195 to the right base address (PV or REG_METHODPTR).
197 INVOKESTATIC/SPECIAL:
199 ???????? ldx [i5 - 72],o5
200 ???????? jmp o5 <-- ra
203 w/ sethi (mptr in dseg out of 13-bit simm range)
205 ???????? sethi hi(0x2000),o5
206 ???????? sub i5,o5,o5
207 ???????? ldx [o5 - 72],o5
208 ???????? jmp o5 <-- ra
213 ???????? ldx [o0 + 0},g2
214 ???????? ldx [g2 + 0],o5
215 ???????? jmp o5 <-- ra
220 ???????? ldx [o0 + 0},g2
221 ???????? ldx [g2 - 112],g2
222 ???????? ldx [g2 + 24],o5
223 ???????? jmp o5 <-- ra
226 *******************************************************************************/
228 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
230 u4 mcode, mcode_sethi, mcode_masked;
234 /* go back to the location of a possible sethi (3 instruction before jump) */
235 /* note: ra is the address of the jump instruction on SPARC */
237 mcode_sethi = *((u4 *) (ra - 3 * 4));
239 /* check for sethi instruction */
241 if (IS_SETHI(mcode_sethi)) {
242 u4 mcode_sub, mcode_ldx;
244 mcode_sub = *((u4 *) (ra - 2 * 4));
245 mcode_ldx = *((u4 *) (ra - 1 * 4));
247 /* make sure the sequence of instructions is a loadhi */
248 if ((IS_SUB(mcode_sub)) && (IS_LDX_IMM(mcode_ldx)))
252 /* get 22-bit immediate of sethi instruction */
254 offset = (s4) (mcode_sethi & 0x3fffff);
255 offset = offset << 10;
257 /* goto next instruction */
259 /* make sure it's a sub instruction (pv - big_disp) */
260 assert(IS_SUB(mcode_sub));
263 /* get displacement of load instruction */
265 assert(IS_LDX_IMM(mcode_ldx));
267 offset += decode_13bit_imm(mcode_ldx);
269 pa = sfi->pv + offset;
275 /* we didn't find a sethi, or it didn't belong to a loadhi */
276 /* check for simple (one-instruction) load */
278 mcode = *((u4 *) iptr);
280 /* shift and mask rd */
282 mcode_masked = (mcode >> 13) & 0x060fff;
284 /* get the offset from the instruction */
286 offset = decode_13bit_imm(mcode);
288 /* check for call with rs1 == REG_METHODPTR: ldx [g2+x],pv_caller */
290 if (mcode_masked == 0x0602c5) {
291 /* in this case we use the passed method pointer */
293 /* return NULL if no mptr was specified (used for replacement) */
301 /* in the normal case we check for a `ldx [i5+x],pv_caller' instruction */
303 assert(mcode_masked == 0x0602fb);
305 /* and get the final data segment address */
307 pa = sfi->pv + offset;
315 /* md_cacheflush ***************************************************************
317 Calls the system's function to flush the instruction and data
320 *******************************************************************************/
322 void md_cacheflush(u1 *addr, s4 nbytes)
328 /* md_dcacheflush **************************************************************
330 Calls the system's function to flush the data cache.
332 *******************************************************************************/
334 void md_dcacheflush(u1 *addr, s4 nbytes)
336 /* XXX don't know yet */
337 /* printf("md_dcacheflush\n"); */
338 __asm__ __volatile__ ( "membar 0x7F" : : : "memory" );
342 /* md_patch_replacement_point **************************************************
344 Patch the given replacement point.
346 *******************************************************************************/
348 #if defined(ENABLE_REPLACEMENT)
349 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
354 /* restore the patched-over instruction */
355 *(u4*)(rp->pc) = *(u4*)(savedmcode);
358 /* save the current machine code */
359 *(u4*)(savedmcode) = *(u4*)(rp->pc);
361 /* build the machine code for the patch */
362 assert(0); /* XXX build trap instruction below */
365 /* write the new machine code */
366 *(u4*)(rp->pc) = (u4) mcode;
369 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
377 /* flush instruction cache */
378 /* md_icacheflush(rp->pc,4); */
380 #endif /* defined(ENABLE_REPLACEMENT) */
383 * These are local overrides for various environment variables in Emacs.
384 * Please do not remove this and leave it at the end of the file, where
385 * Emacs will automagically detect them.
386 * ---------------------------------------------------------------------
389 * indent-tabs-mode: t
393 * vim:noexpandtab:sw=4:ts=4: