* configure.ac [ENABLE_STATICVM] (AC_CHECK_LIB(dl)): Only perform the
[cacao.git] / src / vm / jit / arm / md.c
1 /* src/vm/jit/arm/md.c - machine dependent Arm functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Michael Starzinger
28             Christian Thalinger
29
30    $Id: md.c 6591 2007-01-02 19:14:25Z twisti $
31
32 */
33
34
35 #include "config.h"
36
37 #include <assert.h>
38
39 #include "vm/global.h"
40 #include "vm/types.h"
41
42 #include "vm/jit/arm/md-abi.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/stringlocal.h"
46 #include "vm/jit/asmpart.h"
47
48
49 /* md_init *********************************************************************
50
51    Do some machine dependent initialization.
52
53 *******************************************************************************/
54
55 void md_init(void)
56 {
57         /* do nothing here */
58 }
59
60
61 /* md_codegen_patch_branch *****************************************************
62
63    Back-patches a branch instruction.
64
65 *******************************************************************************/
66
67 void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
68 {
69         s4 *mcodeptr;
70         s4  disp;                           /* branch displacement                */
71
72         /* calculate the patch position */
73
74         mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
75
76         /* Calculate the branch displacement. */
77
78         disp = (targetmpc - branchmpc - 4) >> 2;
79
80         if ((disp < (s4) 0xff000000) || (disp > (s4) 0x00ffffff))
81                 vm_abort("md_codegen_patch_branch: branch displacement out of range: %d > +/-%d", disp, 0x00ffffff);
82
83         /* patch the branch instruction before the mcodeptr */
84
85         mcodeptr[-1] |= (disp & 0x00ffffff);
86 }
87
88
89 /* md_stacktrace_get_returnaddress *********************************************
90
91    Returns the return address of the current stackframe, specified by
92    the passed stack pointer and the stack frame size.
93
94 *******************************************************************************/
95
96 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
97 {
98         u1 *ra;
99
100         /*printf("md_stacktrace_get_returnaddress(): called (sp=%x, framesize=%d)\n", sp, framesize);*/
101
102         /* on ARM the return address is located on the top of the stackframe */
103         /* ATTENTION: this is only true for non-leaf methods !!! */
104         ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
105
106         /*printf("md_stacktrace_get_returnaddress(): result (ra=%x)\n", ra);*/
107
108         return ra;
109 }
110
111
112 /* md_assembler_get_patch_address **********************************************
113
114    Gets the patch address of the currently compiled method. The offset
115    is extracted from the load instruction(s) before the jump and added
116    to the right base address (PV or REG_METHODPTR).
117
118    Machine code:
119
120    e51cc040    ldr   ip, [ip, #-64]
121    e1a0e00f    mov   lr, pc
122    e1a0f00c    mov   pc, ip
123
124    or
125
126    e590b000    ldr   fp, [r0]
127    e59bc000    ldr   ip, [fp]
128    e1a0e00f    mov   lr, pc
129    e1a0f00c    mov   pc, ip
130
131    How we find out the patching address to store new method pointer:
132     - loaded IP with LDR IP,[METHODPTR]?
133         yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
134     - loaded IP from data segment
135         yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
136         recompute pointer to data segment, maybe larger offset 
137
138 *******************************************************************************/
139
140 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
141 {
142         u4  mcode;
143         s4  offset;
144         u1 *pa;                             /* patch address                      */
145
146         /* sanity check: are we inside jit code? */
147
148         assert(*((u4 *) (ra - 2*4)) == 0xe1a0e00f /*MOV LR,PC*/);
149         assert(*((u4 *) (ra - 1*4)) == 0xe1a0f00c /*MOV PC,IP*/);
150
151         /* get the load instruction and offset */
152
153         mcode  = *((u4 *) (ra - 12));
154         offset = (s4) (mcode & 0x0fff);
155
156         assert ((mcode & 0xff70f000) == 0xe510c000);
157
158         if ((mcode & 0x000f0000) == 0x000b0000) {
159                 /* sanity check: offset was positive */
160
161                 assert((mcode & 0x00800000) == 0x00800000);
162
163                 /* we loaded from REG_METHODPTR */
164
165                 pa = mptr + offset;
166         }
167         else {
168                 /* sanity check: we loaded from REG_IP; offset was negative or zero */
169
170                 assert((mcode & 0x008f0000) == 0x000c0000 ||
171                        (mcode & 0x008f0fff) == 0x008c0000);
172
173                 /* we loaded from data segment; offset can be larger */
174
175                 mcode = *((u4 *) (ra - 4*4));
176
177                 /* check for "SUB IP, IP, #??, ROTL 12" */
178
179                 if ((mcode & 0xffffff00) == 0xe24cca00)
180                         offset += (s4) ((mcode & 0x00ff) << 12);
181
182                 /* and get the final data segment address */
183
184                 pa = sfi->pv - offset;        
185         }
186
187         return pa;
188 }
189
190
191 /* md_codegen_get_pv_from_pc ***************************************************
192
193    TODO: document me
194
195 *******************************************************************************/
196
197 u1 *md_codegen_get_pv_from_pc(u1 *ra)
198 {
199         u1 *pv;
200         u4  mcode1, mcode2, mcode3;
201
202         pv = ra;
203
204         /* this can either be a RECOMPUTE_IP in JIT code or a fake in asm_calljavafunction */
205         mcode1 = *((u4*) ra);
206         if ((mcode1 & 0xffffff00) == 0xe24fcf00 /*sub ip,pc,#__*/)
207                 pv -= (s4) ((mcode1 & 0x000000ff) <<  2);
208         else if ((mcode1 & 0xffffff00) == 0xe24fc000 /*sub ip,pc,#__*/)
209                 pv -= (s4) (mcode1 & 0x000000ff);
210         else {
211                 /* if this happens, we got an unexpected instruction at (*ra) */
212                 throw_cacao_exception_exit(string_java_lang_InternalError,
213                    "Unable to find method: %p (instr=%x)\n",
214                    ra, mcode1);
215         }
216
217         /* if we have a RECOMPUTE_IP there can be more than one instruction */
218         mcode2 = *((u4*) (ra + 4));
219         mcode3 = *((u4*) (ra + 8));
220         if ((mcode2 & 0xffffff00) == 0xe24ccb00 /*sub ip,ip,#__*/)
221                 pv -= (s4) ((mcode2 & 0x000000ff) << 10);
222         if ((mcode3 & 0xffffff00) == 0xe24cc700 /*sub ip,ip,#__*/)
223                 pv -= (s4) ((mcode3 & 0x000000ff) << 18);
224
225         /* we used PC-relative adressing; but now it is LR-relative */
226         pv += 8;
227
228         /* if we found our method the data segment has to be valid */
229         /* we check this by looking up the IsLeaf field, which has to be boolean */
230         assert( *((s4*)pv-4) == (s4)true || *((s4*)pv-4) == (s4)false ); 
231
232         return pv;
233 }
234
235
236 /* md_cacheflush ***************************************************************
237
238    Calls the system's function to flush the instruction and data
239    cache.
240
241 *******************************************************************************/
242
243 void md_cacheflush(u1 *addr, s4 nbytes)
244 {
245         asm_cacheflush(addr, nbytes);
246 }
247
248
249 /* md_icacheflush **************************************************************
250
251    Calls the system's function to flush the instruction cache.
252
253 *******************************************************************************/
254
255 void md_icacheflush(u1 *addr, s4 nbytes)
256 {
257         asm_cacheflush(addr, nbytes);
258 }
259
260
261 /* md_dcacheflush **************************************************************
262
263    Calls the system's function to flush the data cache.
264
265 *******************************************************************************/
266
267 void md_dcacheflush(u1 *addr, s4 nbytes)
268 {
269         asm_cacheflush(addr, nbytes);
270 }
271
272
273 /*
274  * These are local overrides for various environment variables in Emacs.
275  * Please do not remove this and leave it at the end of the file, where
276  * Emacs will automagically detect them.
277  * ---------------------------------------------------------------------
278  * Local variables:
279  * mode: c
280  * indent-tabs-mode: t
281  * c-basic-offset: 4
282  * tab-width: 4
283  * End:
284  * vim:noexpandtab:sw=4:ts=4:
285  */