1 /* src/vm/jit/arm/md.c - machine dependent Arm 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
35 #include "vm/jit/arm/md-abi.h"
37 #include "vm/exceptions.h"
38 #include "vm/global.h"
40 #include "vm/jit/asmpart.h"
41 #include "vm/jit/md.h"
44 /* md_init *********************************************************************
46 Do some machine dependent initialization.
48 *******************************************************************************/
56 /* md_stacktrace_get_returnaddress *********************************************
58 Returns the return address of the current stackframe, specified by
59 the passed stack pointer and the stack frame size.
61 *******************************************************************************/
63 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
67 /* On ARM the return address is located on the top of the
69 /* ATTENTION: This is only true for non-leaf methods!!! */
71 ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
77 /* md_jit_method_patch_address *************************************************
79 Gets the patch address of the currently compiled method. The offset
80 is extracted from the load instruction(s) before the jump and added
81 to the right base address (PV or REG_METHODPTR).
85 e51cc040 ldr ip, [ip, #-64]
96 How we find out the patching address to store new method pointer:
97 - loaded IP with LDR IP,[METHODPTR]?
98 yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
99 - loaded IP from data segment
100 yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
101 recompute pointer to data segment, maybe larger offset
103 *******************************************************************************/
105 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
110 void *pa; /* patch address */
112 /* Go back to the actual load instruction. */
114 pc = ((uint32_t *) ra) - 3;
116 /* Get first instruction word on current PC. */
120 /* sanity check: are we inside jit code? */
122 assert(pc[1] == 0xe1a0e00f /*MOV LR,PC*/);
123 assert(pc[2] == 0xe1a0f00c /*MOV PC,IP*/);
125 /* get the load instruction and offset */
127 offset = (int32_t) (mcode & 0x0fff);
129 assert ((mcode & 0xff70f000) == 0xe510c000);
131 if ((mcode & 0x000f0000) == 0x000b0000) {
132 /* sanity check: offset was positive */
134 assert((mcode & 0x00800000) == 0x00800000);
136 /* return NULL if no mptr was specified (used for replacement) */
141 /* we loaded from REG_METHODPTR */
143 pa = ((uint8_t *) mptr) + offset;
146 /* sanity check: we loaded from REG_IP; offset was negative or zero */
148 assert((mcode & 0x008f0000) == 0x000c0000 ||
149 (mcode & 0x008f0fff) == 0x008c0000);
151 /* we loaded from data segment; offset can be larger */
155 /* check for "SUB IP, IP, #??, ROTL 12" */
157 if ((mcode & 0xffffff00) == 0xe24cca00)
158 offset += (int32_t) ((mcode & 0x00ff) << 12);
160 /* and get the final data segment address */
162 pa = ((uint8_t *) pv) - offset;
169 /* md_codegen_get_pv_from_pc ***************************************************
173 *******************************************************************************/
175 u1 *md_codegen_get_pv_from_pc(u1 *ra)
178 u4 mcode1, mcode2, mcode3;
182 /* this can either be a RECOMPUTE_IP in JIT code or a fake in asm_calljavafunction */
183 mcode1 = *((u4*) ra);
184 if ((mcode1 & 0xffffff00) == 0xe24fcf00 /*sub ip,pc,#__*/)
185 pv -= (s4) ((mcode1 & 0x000000ff) << 2);
186 else if ((mcode1 & 0xffffff00) == 0xe24fc000 /*sub ip,pc,#__*/)
187 pv -= (s4) (mcode1 & 0x000000ff);
189 /* if this happens, we got an unexpected instruction at (*ra) */
190 vm_abort("Unable to find method: %p (instr=%x)", ra, mcode1);
193 /* if we have a RECOMPUTE_IP there can be more than one instruction */
194 mcode2 = *((u4*) (ra + 4));
195 mcode3 = *((u4*) (ra + 8));
196 if ((mcode2 & 0xffffff00) == 0xe24ccb00 /*sub ip,ip,#__*/)
197 pv -= (s4) ((mcode2 & 0x000000ff) << 10);
198 if ((mcode3 & 0xffffff00) == 0xe24cc700 /*sub ip,ip,#__*/)
199 pv -= (s4) ((mcode3 & 0x000000ff) << 18);
201 /* we used PC-relative adressing; but now it is LR-relative */
204 /* if we found our method the data segment has to be valid */
205 /* we check this by looking up the IsLeaf field, which has to be boolean */
206 /* assert( *((s4*)pv-8) == (s4)true || *((s4*)pv-8) == (s4)false ); */
212 /* md_cacheflush ***************************************************************
214 Calls the system's function to flush the instruction and data
217 *******************************************************************************/
219 void md_cacheflush(u1 *addr, s4 nbytes)
221 asm_cacheflush(addr, nbytes);
225 /* md_icacheflush **************************************************************
227 Calls the system's function to flush the instruction cache.
229 *******************************************************************************/
231 void md_icacheflush(u1 *addr, s4 nbytes)
233 asm_cacheflush(addr, nbytes);
237 /* md_dcacheflush **************************************************************
239 Calls the system's function to flush the data cache.
241 *******************************************************************************/
243 void md_dcacheflush(u1 *addr, s4 nbytes)
250 * These are local overrides for various environment variables in Emacs.
251 * Please do not remove this and leave it at the end of the file, where
252 * Emacs will automagically detect them.
253 * ---------------------------------------------------------------------
256 * indent-tabs-mode: t
260 * vim:noexpandtab:sw=4:ts=4: