Merge to subtype-trunk (manually: src/vm/linker.hpp, src/vm/vftbl.hpp)
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "vm/jit/arm/md.h"
32 #include "vm/jit/arm/md-abi.h"
33
34
35 /* md_init *********************************************************************
36
37    Do some machine dependent initialization.
38
39 *******************************************************************************/
40
41 void md_init(void)
42 {
43         /* do nothing here */
44 }
45
46
47 /* md_jit_method_patch_address *************************************************
48
49    Gets the patch address of the currently compiled method. The offset
50    is extracted from the load instruction(s) before the jump and added
51    to the right base address (PV or REG_METHODPTR).
52
53    Machine code:
54
55    e51cc040    ldr   ip, [ip, #-64]
56    e1a0e00f    mov   lr, pc
57    e1a0f00c    mov   pc, ip
58
59    or
60
61    e590b000    ldr   fp, [r0]
62    e59bc000    ldr   ip, [fp]
63    e1a0e00f    mov   lr, pc
64    e1a0f00c    mov   pc, ip
65
66    How we find out the patching address to store new method pointer:
67     - loaded IP with LDR IP,[METHODPTR]?
68         yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
69     - loaded IP from data segment
70         yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
71         recompute pointer to data segment, maybe larger offset 
72
73 *******************************************************************************/
74
75 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
76 {
77         uint32_t *pc;
78         uint32_t  mcode;
79         int32_t   offset;
80         void     *pa;                       /* patch address                      */
81
82         /* Go back to the actual load instruction. */
83
84         pc = ((uint32_t *) ra) - 3;
85
86         /* Get first instruction word on current PC. */
87
88         mcode = pc[0];
89
90         /* sanity check: are we inside jit code? */
91
92         assert(pc[1] == 0xe1a0e00f /*MOV LR,PC*/);
93         assert(pc[2] == 0xe1a0f00c /*MOV PC,IP*/);
94
95         /* get the load instruction and offset */
96
97         offset = (int32_t) (mcode & 0x0fff);
98
99         assert ((mcode & 0xff70f000) == 0xe510c000);
100
101         if ((mcode & 0x000f0000) == 0x000b0000) {
102                 /* sanity check: offset was positive */
103
104                 assert((mcode & 0x00800000) == 0x00800000);
105
106                 /* return NULL if no mptr was specified (used for replacement) */
107
108                 if (mptr == NULL)
109                         return NULL;
110
111                 /* we loaded from REG_METHODPTR */
112
113                 pa = ((uint8_t *) mptr) + offset;
114         }
115         else {
116                 /* sanity check: we loaded from REG_IP; offset was negative or zero */
117
118                 assert((mcode & 0x008f0000) == 0x000c0000 ||
119                        (mcode & 0x008f0fff) == 0x008c0000);
120
121                 /* we loaded from data segment; offset can be larger */
122
123                 mcode = pc[-1];
124
125                 /* check for "SUB IP, IP, #??, ROTL 12" */
126
127                 if ((mcode & 0xffffff00) == 0xe24cca00)
128                         offset += (int32_t) ((mcode & 0x00ff) << 12);
129
130                 /* and get the final data segment address */
131
132                 pa = ((uint8_t *) pv) - offset;        
133         }
134
135         return pa;
136 }
137
138
139 /**
140  * Patch the given replacement point.
141  */
142 #if defined(ENABLE_REPLACEMENT)
143 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
144 {
145         vm_abort("md_patch_replacement_point: IMPLEMENT ME!");
146 }
147 #endif
148
149
150 /*
151  * These are local overrides for various environment variables in Emacs.
152  * Please do not remove this and leave it at the end of the file, where
153  * Emacs will automagically detect them.
154  * ---------------------------------------------------------------------
155  * Local variables:
156  * mode: c
157  * indent-tabs-mode: t
158  * c-basic-offset: 4
159  * tab-width: 4
160  * End:
161  * vim:noexpandtab:sw=4:ts=4:
162  */