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
35 #include "vm/jit/sparc64/md-abi.h"
37 #include "vm/exceptions.h"
38 #include "vm/stringlocal.h"
39 #include "vm/jit/asmpart.h"
40 #include "vm/jit/stacktrace.h"
42 /* assembler function prototypes **********************************************/
43 void asm_store_fp_state_reg(u8 *mem);
44 void asm_load_fp_state_reg(u8 *mem);
48 /* shift away 13-bit immediate, mask rd and rs1 */
49 #define SHIFT_AND_MASK(instr) \
50 ((instr >> 13) & 0x60fc1)
52 /* NOP is defined as a SETHI instruction with rd and imm. set to zero */
53 /* therefore we check if the 22-bit immediate is zero */
54 #define IS_SETHI(instr) \
55 (((instr & 0xc1c00000) == 0x01000000) \
56 && ((instr & 0x3fffff) != 0x0))
58 #define IS_LDX_IMM(instr) \
59 (((instr >> 13) & 0x60fc1) == 0x602c1)
61 #define IS_SUB(instr) \
62 (((instr >> 13) & 0x60fc0) == 0x40100)
64 inline s2 decode_13bit_imm(u4 instr) {
67 /* mask everything else in the instruction */
68 imm = instr & 0x00001fff;
70 /* sign extend 13-bit to 16-bit */
77 /* md_init *********************************************************************
79 Do some machine dependent initialization.
81 *******************************************************************************/
89 /* md_stacktrace_get_returnaddress *********************************************
91 Returns the return address of the current stackframe, specified by
92 the passed stack pointer and the stack frame size.
94 *******************************************************************************/
96 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
99 /* flush register windows to the stack */
102 /* the return address resides in register i7, the last register in the
103 * 16-extended-word save area
105 ra = *((u1 **) (sp + 120 + BIAS));
107 /* NOTE: on SPARC ra is the address of the call instruction */
112 u1 *md_get_framepointer(u1 *sp)
115 /* flush register windows to the stack */
118 fp = *((u1 **) (sp + 112 + BIAS));
123 u1 *md_get_pv_from_stackframe(u1 *sp)
126 /* flush register windows to the stack */
129 pv = *((u1 **) (sp + 104 + BIAS));
134 /* md_codegen_get_pv_from_pc ***************************************************
136 This reconstructs and returns the PV of a method given a return address
137 pointer. (basically, same was as the generated code following the jump does)
143 277afffe ldah pv,-2(ra)
144 237ba61c lda pv,-23012(pv)
146 *******************************************************************************/
148 u1 *md_codegen_get_pv_from_pc(u1 *ra)
156 /* get the instruction word after jump and nop */
157 mcode = *((u4 *) (ra+8) );
159 /* check if we have a sethi insruction */
160 if (IS_SETHI(mcode)) {
163 /* get 22-bit immediate of sethi instruction */
164 offset = (s4) (mcode & 0x3fffff);
165 offset = offset << 10;
168 mcode = *((u4 *) (ra+12) );
169 xor_imm = decode_13bit_imm(mcode);
176 mcode_masked = SHIFT_AND_MASK(mcode);
178 assert(mcode_masked == 0x40001);
180 /* mask and extend the negative sign for the 13 bit immediate */
181 offset = decode_13bit_imm(mcode);
189 /* md_get_method_patch_address *************************************************
191 Gets the patch address of the currently compiled method. The offset
192 is extracted from the load instruction(s) before the jump and added
193 to the right base address (PV or REG_METHODPTR).
195 INVOKESTATIC/SPECIAL:
197 ???????? ldx [i5 - 72],o5
198 ???????? jmp o5 <-- ra
201 w/ sethi (mptr in dseg out of 13-bit simm range)
203 ???????? sethi hi(0x2000),o5
204 ???????? sub i5,o5,o5
205 ???????? ldx [o5 - 72],o5
206 ???????? jmp o5 <-- ra
211 ???????? ldx [o0 + 0},g2
212 ???????? ldx [g2 + 0],o5
213 ???????? jmp o5 <-- ra
218 ???????? ldx [o0 + 0},g2
219 ???????? ldx [g2 - 112],g2
220 ???????? ldx [g2 + 24],o5
221 ???????? jmp o5 <-- ra
224 *******************************************************************************/
226 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
228 u4 mcode, mcode_sethi, mcode_masked;
232 /* go back to the location of a possible sethi (3 instruction before jump) */
233 /* note: ra is the address of the jump instruction on SPARC */
235 mcode_sethi = *((u4 *) (ra - 3 * 4));
237 /* check for sethi instruction */
239 if (IS_SETHI(mcode_sethi)) {
240 u4 mcode_sub, mcode_ldx;
242 mcode_sub = *((u4 *) (ra - 2 * 4));
243 mcode_ldx = *((u4 *) (ra - 1 * 4));
245 /* make sure the sequence of instructions is a loadhi */
246 if ((IS_SUB(mcode_sub)) && (IS_LDX_IMM(mcode_ldx)))
250 /* get 22-bit immediate of sethi instruction */
252 offset = (s4) (mcode_sethi & 0x3fffff);
253 offset = offset << 10;
255 /* goto next instruction */
257 /* make sure it's a sub instruction (pv - big_disp) */
258 assert(IS_SUB(mcode_sub));
261 /* get displacement of load instruction */
263 assert(IS_LDX_IMM(mcode_ldx));
265 offset += decode_13bit_imm(mcode_ldx);
267 pa = sfi->pv + offset;
273 /* we didn't find a sethi, or it didn't belong to a loadhi */
274 /* check for simple (one-instruction) load */
276 mcode = *((u4 *) iptr);
278 /* shift and mask rd */
280 mcode_masked = (mcode >> 13) & 0x060fff;
282 /* get the offset from the instruction */
284 offset = decode_13bit_imm(mcode);
286 /* check for call with rs1 == REG_METHODPTR: ldx [g2+x],pv_caller */
288 if (mcode_masked == 0x0602c5) {
289 /* in this case we use the passed method pointer */
291 /* return NULL if no mptr was specified (used for replacement) */
299 /* in the normal case we check for a `ldx [i5+x],pv_caller' instruction */
301 assert(mcode_masked == 0x0602fb);
303 /* and get the final data segment address */
305 pa = sfi->pv + offset;
313 /* md_cacheflush ***************************************************************
315 Calls the system's function to flush the instruction and data
318 *******************************************************************************/
320 void md_cacheflush(u1 *addr, s4 nbytes)
326 /* md_dcacheflush **************************************************************
328 Calls the system's function to flush the data cache.
330 *******************************************************************************/
332 void md_dcacheflush(u1 *addr, s4 nbytes)
334 /* XXX don't know yet */
335 /* printf("md_dcacheflush\n"); */
336 __asm__ __volatile__ ( "membar 0x7F" : : : "memory" );
340 /* md_patch_replacement_point **************************************************
342 Patch the given replacement point.
344 *******************************************************************************/
346 #if defined(ENABLE_REPLACEMENT)
347 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
352 /* restore the patched-over instruction */
353 *(u4*)(rp->pc) = *(u4*)(savedmcode);
356 /* save the current machine code */
357 *(u4*)(savedmcode) = *(u4*)(rp->pc);
359 /* build the machine code for the patch */
360 assert(0); /* XXX build trap instruction below */
363 /* write the new machine code */
364 *(u4*)(rp->pc) = (u4) mcode;
367 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
375 /* flush instruction cache */
376 /* md_icacheflush(rp->pc,4); */
378 #endif /* defined(ENABLE_REPLACEMENT) */
381 * These are local overrides for various environment variables in Emacs.
382 * Please do not remove this and leave it at the end of the file, where
383 * Emacs will automagically detect them.
384 * ---------------------------------------------------------------------
387 * indent-tabs-mode: t
391 * vim:noexpandtab:sw=4:ts=4: