* src/vm/jit/md.h: Removed.
[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 #include <stdint.h>
32
33 #include "vm/jit/arm/md.h"
34 #include "vm/jit/arm/md-abi.h"
35
36
37 /* md_init *********************************************************************
38
39    Do some machine dependent initialization.
40
41 *******************************************************************************/
42
43 void md_init(void)
44 {
45         /* do nothing here */
46 }
47
48
49 /* md_jit_method_patch_address *************************************************
50
51    Gets the patch address of the currently compiled method. The offset
52    is extracted from the load instruction(s) before the jump and added
53    to the right base address (PV or REG_METHODPTR).
54
55    Machine code:
56
57    e51cc040    ldr   ip, [ip, #-64]
58    e1a0e00f    mov   lr, pc
59    e1a0f00c    mov   pc, ip
60
61    or
62
63    e590b000    ldr   fp, [r0]
64    e59bc000    ldr   ip, [fp]
65    e1a0e00f    mov   lr, pc
66    e1a0f00c    mov   pc, ip
67
68    How we find out the patching address to store new method pointer:
69     - loaded IP with LDR IP,[METHODPTR]?
70         yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
71     - loaded IP from data segment
72         yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
73         recompute pointer to data segment, maybe larger offset 
74
75 *******************************************************************************/
76
77 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
78 {
79         uint32_t *pc;
80         uint32_t  mcode;
81         int32_t   offset;
82         void     *pa;                       /* patch address                      */
83
84         /* Go back to the actual load instruction. */
85
86         pc = ((uint32_t *) ra) - 3;
87
88         /* Get first instruction word on current PC. */
89
90         mcode = pc[0];
91
92         /* sanity check: are we inside jit code? */
93
94         assert(pc[1] == 0xe1a0e00f /*MOV LR,PC*/);
95         assert(pc[2] == 0xe1a0f00c /*MOV PC,IP*/);
96
97         /* get the load instruction and offset */
98
99         offset = (int32_t) (mcode & 0x0fff);
100
101         assert ((mcode & 0xff70f000) == 0xe510c000);
102
103         if ((mcode & 0x000f0000) == 0x000b0000) {
104                 /* sanity check: offset was positive */
105
106                 assert((mcode & 0x00800000) == 0x00800000);
107
108                 /* return NULL if no mptr was specified (used for replacement) */
109
110                 if (mptr == NULL)
111                         return NULL;
112
113                 /* we loaded from REG_METHODPTR */
114
115                 pa = ((uint8_t *) mptr) + offset;
116         }
117         else {
118                 /* sanity check: we loaded from REG_IP; offset was negative or zero */
119
120                 assert((mcode & 0x008f0000) == 0x000c0000 ||
121                        (mcode & 0x008f0fff) == 0x008c0000);
122
123                 /* we loaded from data segment; offset can be larger */
124
125                 mcode = pc[-1];
126
127                 /* check for "SUB IP, IP, #??, ROTL 12" */
128
129                 if ((mcode & 0xffffff00) == 0xe24cca00)
130                         offset += (int32_t) ((mcode & 0x00ff) << 12);
131
132                 /* and get the final data segment address */
133
134                 pa = ((uint8_t *) pv) - offset;        
135         }
136
137         return pa;
138 }
139
140
141 void *md_asm_codegen_get_pv_from_pc(void *ra)
142 {
143         return md_codegen_get_pv_from_pc(ra);
144 }
145
146
147 /*
148  * These are local overrides for various environment variables in Emacs.
149  * Please do not remove this and leave it at the end of the file, where
150  * Emacs will automagically detect them.
151  * ---------------------------------------------------------------------
152  * Local variables:
153  * mode: c
154  * indent-tabs-mode: t
155  * c-basic-offset: 4
156  * tab-width: 4
157  * End:
158  * vim:noexpandtab:sw=4:ts=4:
159  */