* src/vm/jit/codegen-common.cpp, src/vm/jit/x86_64/codegen.c: Generate
[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    Copyright (C) 2009 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31
32 #include "vm/types.h"
33
34 #include "md-abi.h"
35 #include "vm/jit/powerpc/codegen.h"
36 #include "vm/jit/powerpc/md.h"
37
38 #include "vm/global.h"
39 #include "vm/vm.hpp"
40
41 #include "vm/jit/jit.hpp"
42 #include "vm/jit/trap.hpp"
43
44
45 /* md_init *********************************************************************
46
47    Do some machine dependent initialization.
48
49 *******************************************************************************/
50
51 void md_init(void)
52 {
53         /* nothing to do */
54 }
55
56
57 /* md_jit_method_patch_address *************************************************
58
59    Gets the patch address of the currently compiled method. The offset
60    is extracted from the load instruction(s) before the jump and added
61    to the right base address (PV or REG_METHODPTR).
62
63    INVOKESTATIC/SPECIAL:
64
65    81adffd4    lwz     r13,-44(r13)
66    7da903a6    mtctr   r13
67    4e800421    bctrl
68
69    INVOKEVIRTUAL:
70
71    81830000    lwz     r12,0(r3)
72    81ac0000    lwz     r13,0(r12)
73    7da903a6    mtctr   r13
74    4e800421    bctrl
75
76    INVOKEINTERFACE:
77
78    81830000    lwz     r12,0(r3)
79    818c0000    lwz     r12,0(r12)
80    81ac0000    lwz     r13,0(r12)
81    7da903a6    mtctr   r13
82    4e800421    bctrl
83
84 *******************************************************************************/
85
86 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
87 {
88         uint32_t *pc;
89         uint32_t  mcode;
90         int32_t   offset;
91         void     *pa;
92
93         /* go back to the actual load instruction (3 instructions) */
94
95         pc = ((uint32_t *) ra) - 3;
96
97         /* get first instruction word (lwz) */
98
99         mcode = *pc;
100
101         /* check if we have 2 instructions (addis, addi) */
102
103         if ((mcode >> 16) == 0x3c19) {
104                 /* XXX write a regression for this */
105                 pa = NULL;
106                 assert(0);
107
108                 /* get displacement of first instruction (addis) */
109
110                 offset = (int32_t) (mcode << 16);
111
112                 /* get displacement of second instruction (addi) */
113
114                 mcode = *(pc + 1);
115
116                 assert((mcode >> 16) != 0x6739);
117
118                 offset += (int16_t) (mcode & 0x0000ffff);
119         }
120         else {
121                 /* get the offset from the instruction */
122
123                 offset = (int16_t) (mcode & 0x0000ffff);
124
125                 /* check for load from PV */
126
127                 if ((mcode >> 16) == 0x81ad) {
128                         /* get the final data segment address */
129
130                         pa = ((uint8_t *) pv) + offset;
131                 }
132                 else if ((mcode >> 16) == 0x81ac) {
133                         /* in this case we use the passed method pointer */
134
135                         /* return NULL if no mptr was specified (used for replacement) */
136
137                         if (mptr == NULL)
138                                 return NULL;
139
140                         pa = ((uint8_t *) mptr) + offset;
141                 }
142                 else {
143                         /* catch any problems */
144
145                         vm_abort("md_jit_method_patch_address: unknown instruction %x",
146                                          mcode);
147
148                         /* keep compiler happy */
149
150                         pa = NULL;
151                 }
152         }
153
154         return pa;
155 }
156
157
158 /**
159  * Decode the trap instruction at the given PC.
160  *
161  * @param trp information about trap to be filled
162  * @param sig signal number
163  * @param xpc exception PC
164  * @param es execution state of the machine
165  * @return true if trap was decoded successfully, false otherwise.
166  */
167 bool md_trap_decode(trapinfo_t* trp, int sig, void* xpc, executionstate_t* es)
168 {
169         // Get the exception-throwing instruction.
170         uint32_t mcode = *((uint32_t*) xpc);
171
172         switch (sig) {
173         case TRAP_SIGILL:
174                 // Check for valid trap instruction.
175                 if (patcher_is_valid_trap_instruction_at(xpc)) {
176                         trp->type  = TRAP_PATCHER;
177                         trp->value = 0;
178                         return true;
179                 }
180                 return false;
181
182         case TRAP_SIGTRAP:
183         {
184                 int s1 = M_OP3_GET_A(mcode);
185
186                 // For now we only handle ArrayIndexOutOfBoundsException.
187                 trp->type  = TRAP_ArrayIndexOutOfBoundsException;
188                 trp->value = es->intregs[s1];
189                 return true;
190         }
191
192         case TRAP_SIGSEGV:
193         {
194                 int       s1   = M_INSTR_OP2_IMM_A(mcode);
195                 uintptr_t addr = es->intregs[s1];
196
197                 // Check for special-load.
198                 if (s1 == REG_ZERO) {
199                         int16_t disp = M_INSTR_OP2_IMM_I(mcode);
200                         int     d    = M_INSTR_OP2_IMM_D(mcode);
201
202                         // We use the exception type as load displacement.
203                         trp->type  = disp;
204                         trp->value = es->intregs[d];
205                         return true;
206                 }
207
208                 // Check for implicit NullPointerException.
209                 if (addr == 0) {
210                         trp->type  = TRAP_NullPointerException;
211                         trp->value = 0;
212                         return true;
213                 }
214
215                 return false;
216         }
217
218         default:
219                 return false;
220         }
221 }
222
223
224 /* md_patch_replacement_point **************************************************
225
226    Patch the given replacement point.
227
228 *******************************************************************************/
229
230 #if defined(ENABLE_REPLACEMENT)
231 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
232 {
233         u4 mcode;
234
235         if (revert) {
236                 /* restore the patched-over instruction */
237                 *(u4*)(pc) = *(u4*)(savedmcode);
238         }
239         else {
240                 /* save the current machine code */
241                 *(u4*)(savedmcode) = *(u4*)(pc);
242
243                 /* build the machine code for the patch */
244                 assert(0); /* XXX build trap instruction below */
245                 mcode = 0;
246
247                 /* write the new machine code */
248                 *(u4*)(pc) = (u4) mcode;
249         }
250         
251         /* flush instruction cache */
252     md_icacheflush(pc,4);
253 }
254 #endif /* defined(ENABLE_REPLACEMENT) */
255
256
257 /*
258  * These are local overrides for various environment variables in Emacs.
259  * Please do not remove this and leave it at the end of the file, where
260  * Emacs will automagically detect them.
261  * ---------------------------------------------------------------------
262  * Local variables:
263  * mode: c
264  * indent-tabs-mode: t
265  * c-basic-offset: 4
266  * tab-width: 4
267  * End:
268  * vim:noexpandtab:sw=4:ts=4:
269  */