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