1 /* src/vm/jit/alpha/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"
45 /* shift away 13-bit immediate, mask rd and rs1 */
46 #define SHIFT_AND_MASK(instr) \
47 ((instr >> 13) & 0x60fc1)
49 #define IS_SETHI(instr) \
50 ((instr & 0xc1c00000) == 0x00800000)
52 inline s2 decode_13bit_imm(u4 instr) {
55 /* mask everything else in the instruction */
56 imm = instr & 0x00001fff;
58 /* sign extend 13-bit to 16-bit */
65 /* md_init *********************************************************************
67 Do some machine dependent initialization.
69 *******************************************************************************/
77 /* md_codegen_patch_branch *****************************************************
79 Back-patches a branch instruction.
81 *******************************************************************************/
83 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
87 s4 disp; /* branch displacement */
89 /* calculate the patch position */
91 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
93 /* get the instruction before the exception point */
97 /* Calculate the branch displacement. SPARC displacements regard current
98 PC as base => (branchmpc - 4 */
100 disp = (targetmpc - (branchmpc - 4)) >> 2;
103 /* check for BPcc or FBPfcc instruction */
104 if (((mcode >> 16) & 0xc1c0) == 0x0040) {
106 /* check branch displacement (19-bit)*/
108 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff))
109 vm_abort("branch displacement is out of range: %d > +/-%d", disp, 0x003ffff);
111 /* patch the branch instruction before the mcodeptr */
113 mcodeptr[-1] |= (disp & 0x007ffff);
115 /* check for BPr instruction */
116 else if (((mcode >> 16) & 0xd1c0) == 0x00c0) {
118 /* check branch displacement (16-bit)*/
120 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x0007fff))
121 vm_abort("branch displacement is out of range: %d > +/-%d", disp, 0x0007fff);
123 /* patch the upper 2-bit of the branch displacement */
124 mcodeptr[-1] |= ((disp & 0xc000) << 6);
126 /* patch the lower 14-bit of the branch displacement */
127 mcodeptr[-1] |= (disp & 0x003fff);
135 /* md_stacktrace_get_returnaddress *********************************************
137 Returns the return address of the current stackframe, specified by
138 the passed stack pointer and the stack frame size.
140 *******************************************************************************/
142 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
145 /* flush register windows to the stack */
148 /* the return address resides in register i7, the last register in the
149 * 16-extended-word save area
151 ra = *((u1 **) (sp + 120 + BIAS));
153 /* NOTE: on SPARC ra is the address of the call instruction */
158 u1 *md_get_framepointer(u1 *sp)
161 /* flush register windows to the stack */
164 fp = *((u1 **) (sp + 112 + BIAS));
169 u1 *md_get_pv_from_stackframe(u1 *sp)
172 /* flush register windows to the stack */
175 pv = *((u1 **) (sp + 104 + BIAS));
180 /* md_codegen_get_pv_from_pc ***************************************************
182 This reconstructs and returns the PV of a method given a return address
183 pointer. (basically, same was as the generated code following the jump does)
189 277afffe ldah pv,-2(ra)
190 237ba61c lda pv,-23012(pv)
192 *******************************************************************************/
194 u1 *md_codegen_get_pv_from_pc(u1 *ra)
203 /* get the instruction word after jump and nop */
204 mcode = *((u4 *) (ra+8) );
206 /* check if we have 2 instructions (ldah, lda) */
208 mcode_masked = SHIFT_AND_MASK(mcode);
210 if (mcode_masked == 0x40001) {
212 /* get displacement of first instruction (ldah) */
214 offset = (s4) (mcode << 16);
217 /* get displacement of second instruction (lda) */
219 mcode = *((u4 *) (ra + 1 * 4));
221 assert((mcode >> 16) == 0x237b);
223 offset = (s2) (mcode & 0x0000ffff);
227 /* get displacement of first instruction (lda) */
229 assert((mcode >> 16) == 0x237a);
231 /* mask and extend the negative sign for the 13 bit immediate */
232 offset = decode_13bit_imm(mcode);
244 /* md_get_method_patch_address *************************************************
246 Gets the patch address of the currently compiled method. The offset
247 is extracted from the load instruction(s) before the jump and added
248 to the right base address (PV or REG_METHODPTR).
250 INVOKESTATIC/SPECIAL:
252 dfdeffb8 ldx [i5 - 72],o5
259 df3e0000 ld [g2 + 0],o5
266 df39ff90 ld [g2 - 112],g2
267 df3e0018 ld [g2 + 24],o5
271 *******************************************************************************/
273 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
275 u4 mcode, mcode_masked;
279 /* go back to the actual load instruction (1 instruction before jump) */
280 /* ra is the address of the jump instruction on SPARC */
283 /* get first instruction word on current PC */
285 mcode = *((u4 *) ra);
288 /* check if we have 2 instructions (lui) */
290 if (IS_SETHI(mcode)) {
291 /* XXX write a regression for this */
294 /* get displacement of first instruction (lui) */
296 offset = (s4) (mcode << 16);
298 /* get displacement of second instruction (daddiu) */
300 mcode = *((u4 *) (ra + 1 * 4));
302 assert((mcode >> 16) != 0x6739);
304 offset += (s2) (mcode & 0x0000ffff);
308 /* shift and maks rd */
310 mcode_masked = (mcode >> 13) & 0x060fff;
312 /* get the offset from the instruction */
314 offset = decode_13bit_imm(mcode);
316 /* check for call with rs1 == REG_METHODPTR: ldx [g2+x],pv_caller */
318 if (mcode_masked == 0x0602c5) {
319 /* in this case we use the passed method pointer */
321 /* return NULL if no mptr was specified (used for replacement) */
329 /* in the normal case we check for a `ldx [i5+x],pv_caller' instruction */
331 assert(mcode_masked == 0x0602fb);
333 /* and get the final data segment address */
335 pa = sfi->pv + offset;
343 /* md_cacheflush ***************************************************************
345 Calls the system's function to flush the instruction and data
348 *******************************************************************************/
350 void md_cacheflush(u1 *addr, s4 nbytes)
356 /* md_dcacheflush **************************************************************
358 Calls the system's function to flush the data cache.
360 *******************************************************************************/
362 void md_dcacheflush(u1 *addr, s4 nbytes)
364 /* XXX don't know yet */
368 /* md_patch_replacement_point **************************************************
370 Patch the given replacement point.
372 *******************************************************************************/
374 #if defined(ENABLE_REPLACEMENT)
375 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
383 /* restore the patched-over instruction */
384 *(u4*)(rp->pc) = *(u4*)(savedmcode);
387 /* save the current machine code */
388 *(u4*)(savedmcode) = *(u4*)(rp->pc);
390 /* build the machine code for the patch */
391 disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
392 + index * REPLACEMENT_STUB_SIZE
395 mcode = (((s4)(0x00))<<30) | ((0)<<29) | ((0x8)<<25) | (0x1<<22) | (0<<20)
396 | (1 << 19 ) | ((disp) & 0x007ffff);
398 /* write the new machine code */
399 *(u4*)(rp->pc) = (u4) mcode;
402 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
410 /* flush instruction cache */
411 /* md_icacheflush(rp->pc,4); */
413 #endif /* defined(ENABLE_REPLACEMENT) */
416 * These are local overrides for various environment variables in Emacs.
417 * Please do not remove this and leave it at the end of the file, where
418 * Emacs will automagically detect them.
419 * ---------------------------------------------------------------------
422 * indent-tabs-mode: t
426 * vim:noexpandtab:sw=4:ts=4: