3595f407a4c2688c48b44d5e280a53e974c7329f
[cacao.git] / src / vm / jit / powerpc / md.c
1 /* src/vm/jit/powerpc/md.c - machine dependent PowerPC 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/types.h"
32
33 #include "md-abi.h"
34 #include "vm/jit/powerpc/codegen.h"
35 #include "vm/jit/powerpc/md.h"
36
37 #include "vm/global.h"
38 #include "vm/vm.hpp"
39
40 #include "vm/jit/jit.hpp"
41
42
43 /* md_init *********************************************************************
44
45    Do some machine dependent initialization.
46
47 *******************************************************************************/
48
49 void md_init(void)
50 {
51         /* nothing to do */
52 }
53
54
55 /* md_jit_method_patch_address *************************************************
56
57    Gets the patch address of the currently compiled method. The offset
58    is extracted from the load instruction(s) before the jump and added
59    to the right base address (PV or REG_METHODPTR).
60
61    INVOKESTATIC/SPECIAL:
62
63    81adffd4    lwz     r13,-44(r13)
64    7da903a6    mtctr   r13
65    4e800421    bctrl
66
67    INVOKEVIRTUAL:
68
69    81830000    lwz     r12,0(r3)
70    81ac0000    lwz     r13,0(r12)
71    7da903a6    mtctr   r13
72    4e800421    bctrl
73
74    INVOKEINTERFACE:
75
76    81830000    lwz     r12,0(r3)
77    818c0000    lwz     r12,0(r12)
78    81ac0000    lwz     r13,0(r12)
79    7da903a6    mtctr   r13
80    4e800421    bctrl
81
82 *******************************************************************************/
83
84 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
85 {
86         uint32_t *pc;
87         uint32_t  mcode;
88         int32_t   offset;
89         void     *pa;
90
91         /* go back to the actual load instruction (3 instructions) */
92
93         pc = ((uint32_t *) ra) - 3;
94
95         /* get first instruction word (lwz) */
96
97         mcode = *pc;
98
99         /* check if we have 2 instructions (addis, addi) */
100
101         if ((mcode >> 16) == 0x3c19) {
102                 /* XXX write a regression for this */
103                 pa = NULL;
104                 assert(0);
105
106                 /* get displacement of first instruction (addis) */
107
108                 offset = (int32_t) (mcode << 16);
109
110                 /* get displacement of second instruction (addi) */
111
112                 mcode = *(pc + 1);
113
114                 assert((mcode >> 16) != 0x6739);
115
116                 offset += (int16_t) (mcode & 0x0000ffff);
117         }
118         else {
119                 /* get the offset from the instruction */
120
121                 offset = (int16_t) (mcode & 0x0000ffff);
122
123                 /* check for load from PV */
124
125                 if ((mcode >> 16) == 0x81ad) {
126                         /* get the final data segment address */
127
128                         pa = ((uint8_t *) pv) + offset;
129                 }
130                 else if ((mcode >> 16) == 0x81ac) {
131                         /* in this case we use the passed method pointer */
132
133                         /* return NULL if no mptr was specified (used for replacement) */
134
135                         if (mptr == NULL)
136                                 return NULL;
137
138                         pa = ((uint8_t *) mptr) + offset;
139                 }
140                 else {
141                         /* catch any problems */
142
143                         vm_abort("md_jit_method_patch_address: unknown instruction %x",
144                                          mcode);
145
146                         /* keep compiler happy */
147
148                         pa = NULL;
149                 }
150         }
151
152         return pa;
153 }
154
155
156 /* md_patch_replacement_point **************************************************
157
158    Patch the given replacement point.
159
160 *******************************************************************************/
161
162 #if defined(ENABLE_REPLACEMENT)
163 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
164 {
165         u4 mcode;
166
167         if (revert) {
168                 /* restore the patched-over instruction */
169                 *(u4*)(pc) = *(u4*)(savedmcode);
170         }
171         else {
172                 /* save the current machine code */
173                 *(u4*)(savedmcode) = *(u4*)(pc);
174
175                 /* build the machine code for the patch */
176                 assert(0); /* XXX build trap instruction below */
177                 mcode = 0;
178
179                 /* write the new machine code */
180                 *(u4*)(pc) = (u4) mcode;
181         }
182         
183         /* flush instruction cache */
184     md_icacheflush(pc,4);
185 }
186 #endif /* defined(ENABLE_REPLACEMENT) */
187
188
189 /*
190  * These are local overrides for various environment variables in Emacs.
191  * Please do not remove this and leave it at the end of the file, where
192  * Emacs will automagically detect them.
193  * ---------------------------------------------------------------------
194  * Local variables:
195  * mode: c
196  * indent-tabs-mode: t
197  * c-basic-offset: 4
198  * tab-width: 4
199  * End:
200  * vim:noexpandtab:sw=4:ts=4:
201  */