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 7355 2007-02-14 10:57:32Z 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/stacktrace.h"
52 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
53 #include "vmcore/options.h" /* XXX debug */
54 #include "vm/jit/disass.h" /* XXX debug */
58 /* global variables ***********************************************************/
60 bool has_ext_instr_set = false; /* has instruction set extensions */
63 /* md_init *********************************************************************
65 Do some machine dependent initialization.
67 *******************************************************************************/
71 /* check for extended instruction set */
73 has_ext_instr_set = !asm_md_init();
75 #if defined(__LINUX__)
76 /* Linux on Digital Alpha needs an initialisation of the ieee
77 floating point control for IEEE compliant arithmetic (option
78 -mieee of GCC). Under Digital Unix this is done
81 /* initialize floating point control */
83 ieee_set_fp_control(ieee_get_fp_control()
84 & ~IEEE_TRAP_ENABLE_INV
85 & ~IEEE_TRAP_ENABLE_DZE
86 /* & ~IEEE_TRAP_ENABLE_UNF we dont want underflow */
87 & ~IEEE_TRAP_ENABLE_OVF);
92 /* md_codegen_patch_branch *****************************************************
94 Back-patches a branch instruction.
96 *******************************************************************************/
98 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
102 s4 disp; /* branch displacement */
104 /* calculate the patch position */
106 mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
108 /* get the instruction before the exception point */
110 mcode = mcodeptr[-1];
112 /* Calculate the branch displacement. For branches we need a
113 displacement relative and shifted to the branch PC. */
115 disp = (targetmpc - branchmpc) >> 2;
117 /* check branch displacement */
119 if ((disp < (s4) 0xffe00000) || (disp > (s4) 0x001fffff))
120 vm_abort("branch displacement is out of range: %d > +/-%d", disp, 0x001fffff);
122 /* patch the branch instruction before the mcodeptr */
124 mcodeptr[-1] |= (disp & 0x001fffff);
128 /* md_stacktrace_get_returnaddress *********************************************
130 Returns the return address of the current stackframe, specified by
131 the passed stack pointer and the stack frame size.
133 *******************************************************************************/
135 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
139 /* on Alpha the return address is located on the top of the stackframe */
141 ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
147 /* md_get_method_patch_address *************************************************
149 Gets the patch address of the currently compiled method. The offset
150 is extracted from the load instruction(s) before the jump and added
151 to the right base address (PV or REG_METHODPTR).
153 INVOKESTATIC/SPECIAL:
155 a77bffb8 ldq pv,-72(pv)
160 a7900000 ldq at,0(a0)
161 a77c0000 ldq pv,0(at)
166 a7900000 ldq at,0(a0)
167 a79cff98 ldq at,-104(at)
168 a77c0018 ldq pv,24(at)
171 *******************************************************************************/
173 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
177 u1 *pa; /* patch address */
179 /* go back to the actual load instruction (2 instructions on Alpha) */
183 /* get first instruction word on current PC */
185 mcode = *((u4 *) ra);
187 /* check if we have 2 instructions (lui) */
189 if ((mcode >> 16) == 0x3c19) {
190 /* XXX write a regression for this */
193 /* get displacement of first instruction (lui) */
195 offset = (s4) (mcode << 16);
197 /* get displacement of second instruction (daddiu) */
199 mcode = *((u4 *) (ra + 1 * 4));
201 assert((mcode >> 16) == 0x6739);
203 offset += (s2) (mcode & 0x0000ffff);
206 /* get first instruction (ldq) */
208 mcode = *((u4 *) ra);
210 /* get the offset from the instruction */
212 offset = (s2) (mcode & 0x0000ffff);
214 /* check for call with REG_METHODPTR: ldq pv,0(at) */
216 if ((mcode >> 16) == 0xa77c) {
217 /* in this case we use the passed method pointer */
219 /* return NULL if no mptr was specified (used for replacement) */
227 /* in the normal case we check for a `ldq pv,-72(pv)' instruction */
229 assert((mcode >> 16) == 0xa77b);
231 /* and get the final data segment address */
233 pa = sfi->pv + offset;
241 /* md_codegen_get_pv_from_pc ***************************************************
246 277afffe ldah pv,-2(ra)
247 237ba61c lda pv,-23012(pv)
249 *******************************************************************************/
251 u1 *md_codegen_get_pv_from_pc(u1 *ra)
259 /* get first instruction word after jump */
261 mcode = *((u4 *) ra);
263 /* check if we have 2 instructions (ldah, lda) */
265 if ((mcode >> 16) == 0x277a) {
266 /* get displacement of first instruction (ldah) */
268 offset = (s4) (mcode << 16);
271 /* get displacement of second instruction (lda) */
273 mcode = *((u4 *) (ra + 1 * 4));
275 assert((mcode >> 16) == 0x237b);
277 offset = (s2) (mcode & 0x0000ffff);
281 /* get displacement of first instruction (lda) */
283 assert((mcode >> 16) == 0x237a);
285 offset = (s2) (mcode & 0x0000ffff);
293 /* md_cacheflush ***************************************************************
295 Calls the system's function to flush the instruction and data
298 *******************************************************************************/
300 void md_cacheflush(u1 *addr, s4 nbytes)
302 asm_cacheflush(addr, nbytes);
306 /* md_icacheflush **************************************************************
308 Calls the system's function to flush the instruction cache.
310 *******************************************************************************/
312 void md_icacheflush(u1 *addr, s4 nbytes)
314 asm_cacheflush(addr, nbytes);
318 /* md_patch_replacement_point **************************************************
320 Patch the given replacement point.
322 *******************************************************************************/
324 #if defined(ENABLE_REPLACEMENT)
325 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
331 /* restore the patched-over instruction */
332 *(u4*)(rp->pc) = *(u4*)(savedmcode);
335 /* save the current machine code */
336 *(u4*)(savedmcode) = *(u4*)(rp->pc);
338 /* build the machine code for the patch */
339 disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
340 + index * REPLACEMENT_STUB_SIZE
344 mcode = (((s4) (0x30)) << 26) | ((REG_ZERO) << 21) | ((disp) & 0x1fffff);
346 /* write the new machine code */
347 *(u4*)(rp->pc) = mcode;
350 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
358 /* flush instruction cache */
359 md_icacheflush(rp->pc,4);
361 #endif /* defined(ENABLE_REPLACEMENT) */
364 * These are local overrides for various environment variables in Emacs.
365 * Please do not remove this and leave it at the end of the file, where
366 * Emacs will automagically detect them.
367 * ---------------------------------------------------------------------
370 * indent-tabs-mode: t
374 * vim:noexpandtab:sw=4:ts=4: