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