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