1 /* src/vm/jit/alpha/md.c - machine dependent Alpha 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
25 $Id: md.c 7520 2007-03-14 21:16:04Z twisti $
35 #if defined(__LINUX__)
38 extern unsigned long ieee_get_fp_control();
39 extern void ieee_set_fp_control(unsigned long fp_control);
44 #include "vm/jit/alpha/md-abi.h"
46 #include "vm/exceptions.h"
47 #include "vm/stringlocal.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/codegen-common.h" /* REMOVE ME: for codegendata */
51 #include "vm/jit/stacktrace.h"
53 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
54 #include "vmcore/options.h" /* XXX debug */
55 #include "vm/jit/disass.h" /* XXX debug */
59 /* global variables ***********************************************************/
61 bool has_ext_instr_set = false; /* has instruction set extensions */
64 /* md_init *********************************************************************
66 Do some machine dependent initialization.
68 *******************************************************************************/
72 /* check for extended instruction set */
74 has_ext_instr_set = !asm_md_init();
76 #if defined(__LINUX__)
77 /* Linux on Digital Alpha needs an initialisation of the ieee
78 floating point control for IEEE compliant arithmetic (option
79 -mieee of GCC). Under Digital Unix this is done
82 /* initialize floating point control */
84 ieee_set_fp_control(ieee_get_fp_control()
85 & ~IEEE_TRAP_ENABLE_INV
86 & ~IEEE_TRAP_ENABLE_DZE
87 /* & ~IEEE_TRAP_ENABLE_UNF we dont want underflow */
88 & ~IEEE_TRAP_ENABLE_OVF);
93 /* md_codegen_patch_branch *****************************************************
95 Back-patches a branch instruction.
97 *******************************************************************************/
99 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
103 s4 disp; /* branch displacement */
105 /* calculate the patch position */
107 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
109 /* get the instruction before the exception point */
111 mcode = mcodeptr[-1];
113 /* Calculate the branch displacement. For branches we need a
114 displacement relative and shifted to the branch PC. */
116 disp = (targetmpc - branchmpc) >> 2;
118 /* check branch displacement */
120 if ((disp < (s4) 0xffe00000) || (disp > (s4) 0x001fffff))
121 vm_abort("branch displacement is out of range: %d > +/-%d", disp, 0x001fffff);
123 /* patch the branch instruction before the mcodeptr */
125 mcodeptr[-1] |= (disp & 0x001fffff);
129 /* md_stacktrace_get_returnaddress *********************************************
131 Returns the return address of the current stackframe, specified by
132 the passed stack pointer and the stack frame size.
134 *******************************************************************************/
136 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
140 /* on Alpha the return address is located on the top of the stackframe */
142 ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
148 /* md_get_method_patch_address *************************************************
150 Gets the patch address of the currently compiled method. The offset
151 is extracted from the load instruction(s) before the jump and added
152 to the right base address (PV or REG_METHODPTR).
154 INVOKESTATIC/SPECIAL:
156 a77bffb8 ldq pv,-72(pv)
161 a7900000 ldq at,0(a0)
162 a77c0000 ldq pv,0(at)
167 a7900000 ldq at,0(a0)
168 a79cff98 ldq at,-104(at)
169 a77c0018 ldq pv,24(at)
172 *******************************************************************************/
174 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
178 u1 *pa; /* patch address */
180 /* go back to the actual load instruction (2 instructions on Alpha) */
184 /* get first instruction word on current PC */
186 mcode = *((u4 *) ra);
188 /* check if we have 2 instructions (lui) */
190 if ((mcode >> 16) == 0x3c19) {
191 /* XXX write a regression for this */
194 /* get displacement of first instruction (lui) */
196 offset = (s4) (mcode << 16);
198 /* get displacement of second instruction (daddiu) */
200 mcode = *((u4 *) (ra + 1 * 4));
202 assert((mcode >> 16) == 0x6739);
204 offset += (s2) (mcode & 0x0000ffff);
207 /* get first instruction (ldq) */
209 mcode = *((u4 *) ra);
211 /* get the offset from the instruction */
213 offset = (s2) (mcode & 0x0000ffff);
215 /* check for call with REG_METHODPTR: ldq pv,0(at) */
217 if ((mcode >> 16) == 0xa77c) {
218 /* in this case we use the passed method pointer */
220 /* return NULL if no mptr was specified (used for replacement) */
228 /* in the normal case we check for a `ldq pv,-72(pv)' instruction */
230 assert((mcode >> 16) == 0xa77b);
232 /* and get the final data segment address */
234 pa = sfi->pv + offset;
242 /* md_codegen_get_pv_from_pc ***************************************************
247 277afffe ldah pv,-2(ra)
248 237ba61c lda pv,-23012(pv)
250 *******************************************************************************/
252 u1 *md_codegen_get_pv_from_pc(u1 *ra)
260 /* get first instruction word after jump */
262 mcode = *((u4 *) ra);
264 /* check if we have 2 instructions (ldah, lda) */
266 if ((mcode >> 16) == 0x277a) {
267 /* get displacement of first instruction (ldah) */
269 offset = (s4) (mcode << 16);
272 /* get displacement of second instruction (lda) */
274 mcode = *((u4 *) (ra + 1 * 4));
276 assert((mcode >> 16) == 0x237b);
278 offset = (s2) (mcode & 0x0000ffff);
282 /* get displacement of first instruction (lda) */
284 assert((mcode >> 16) == 0x237a);
286 offset = (s2) (mcode & 0x0000ffff);
294 /* md_cacheflush ***************************************************************
296 Calls the system's function to flush the instruction and data
299 *******************************************************************************/
301 void md_cacheflush(u1 *addr, s4 nbytes)
303 asm_cacheflush(addr, nbytes);
307 /* md_icacheflush **************************************************************
309 Calls the system's function to flush the instruction cache.
311 *******************************************************************************/
313 void md_icacheflush(u1 *addr, s4 nbytes)
315 asm_cacheflush(addr, nbytes);
319 /* md_patch_replacement_point **************************************************
321 Patch the given replacement point.
323 *******************************************************************************/
325 #if defined(ENABLE_REPLACEMENT)
326 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
332 /* restore the patched-over instruction */
333 *(u4*)(rp->pc) = *(u4*)(savedmcode);
336 /* save the current machine code */
337 *(u4*)(savedmcode) = *(u4*)(rp->pc);
339 /* build the machine code for the patch */
340 disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
341 + index * REPLACEMENT_STUB_SIZE
345 mcode = (((s4) (0x30)) << 26) | ((REG_ZERO) << 21) | ((disp) & 0x1fffff);
347 /* write the new machine code */
348 *(u4*)(rp->pc) = mcode;
351 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
359 /* flush instruction cache */
360 md_icacheflush(rp->pc,4);
362 #endif /* defined(ENABLE_REPLACEMENT) */
365 * These are local overrides for various environment variables in Emacs.
366 * Please do not remove this and leave it at the end of the file, where
367 * Emacs will automagically detect them.
368 * ---------------------------------------------------------------------
371 * indent-tabs-mode: t
375 * vim:noexpandtab:sw=4:ts=4: