* src/vm/jit/code.c (code_get_stack_frame_size): Implement stack alignment
[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 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes: Edwin Steiner
30
31    $Id: md.c 4653 2006-03-18 04:14:17Z edwin $
32
33 */
34
35 #include "config.h"
36
37 #include <assert.h>
38
39 #include "vm/types.h"
40
41 #include "md-abi.h"
42
43 #include "vm/global.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/options.h" /* XXX debug */
46 #include "vm/jit/disass.h" /* XXX debug */
47
48
49 /* md_init *********************************************************************
50
51    Do some machine dependent initialization.
52
53 *******************************************************************************/
54
55 void md_init(void)
56 {
57         /* nothing to do */
58 }
59
60
61 /* md_stacktrace_get_returnaddress *********************************************
62
63    Returns the return address of the current stackframe, specified by
64    the passed stack pointer and the stack frame size.
65
66 *******************************************************************************/
67
68 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
69 {
70         u1 *ra;
71
72         /* on PowerPC the return address is located in the linkage area */
73
74         ra = *((u1 **) (sp + framesize + LA_LR_OFFSET));
75
76         return ra;
77 }
78
79
80 /* md_codegen_findmethod *******************************************************
81
82    Machine code:
83
84    7d6802a6    mflr    r11
85    39abffe0    addi    r13,r11,-32
86
87    or
88
89    7d6802a6    mflr    r11
90    3dabffff    addis   r13,r11,-1
91    39ad68b0    addi    r13,r13,26800
92
93 *******************************************************************************/
94
95 u1 *md_codegen_findmethod(u1 *ra)
96 {
97         u1 *pv;
98         u4  mcode;
99         s4  offset;
100
101         /* get first instruction word after jump */
102
103         mcode = *((u4 *) (ra + 1 * 4));
104
105         /* check if we have 2 instructions (addis, addi) */
106
107         if ((mcode >> 16) == 0x3dab) {
108                 /* get displacement of first instruction (addis) */
109
110                 offset = (s4) (mcode << 16);
111
112                 /* get displacement of second instruction (addi) */
113
114                 mcode = *((u4 *) (ra + 2 * 4));
115
116                 /* check for addi instruction */
117
118                 assert((mcode >> 16) == 0x39ad);
119
120                 offset += (s2) (mcode & 0x0000ffff);
121
122         } else {
123                 /* check for addi instruction */
124
125                 assert((mcode >> 16) == 0x39ab);
126
127                 /* get offset of first instruction (addi) */
128
129                 offset = (s2) (mcode & 0x0000ffff);
130         }
131
132         /* calculate PV via RA + offset */
133
134         pv = ra + offset;
135
136         return pv;
137 }
138
139
140 /* md_cacheflush ***************************************************************
141
142    Calls the system's function to flush the instruction and data
143    cache.
144
145 *******************************************************************************/
146
147 void md_cacheflush(u1 *addr, s4 nbytes)
148 {
149         asm_cacheflush(addr, nbytes);
150 }
151
152
153 /* md_icacheflush **************************************************************
154
155    Calls the system's function to flush the instruction cache.
156
157 *******************************************************************************/
158
159 void md_icacheflush(u1 *addr, s4 nbytes)
160 {
161         asm_cacheflush(addr, nbytes);
162 }
163
164
165 /* md_dcacheflush **************************************************************
166
167    Calls the system's function to flush the data cache.
168
169 *******************************************************************************/
170
171 void md_dcacheflush(u1 *addr, s4 nbytes)
172 {
173         asm_cacheflush(addr, nbytes);
174 }
175
176
177 /* md_patch_replacement_point **************************************************
178
179    Patch the given replacement point.
180
181 *******************************************************************************/
182
183 void md_patch_replacement_point(rplpoint *rp)
184 {
185     u8 mcode;
186
187         /* save the current machine code */
188         mcode = *(u4*)rp->pc;
189
190         /* write the new machine code */
191     *(u4*)(rp->pc) = (u4) rp->mcode;
192
193         /* store saved mcode */
194         rp->mcode = mcode;
195         
196         {
197                 u1* u1ptr = rp->pc;
198                 DISASSINSTR(u1ptr);
199                 fflush(stdout);
200         }
201                         
202         /* flush instruction cache */
203     md_icacheflush(rp->pc,4);
204 }
205
206 /*
207  * These are local overrides for various environment variables in Emacs.
208  * Please do not remove this and leave it at the end of the file, where
209  * Emacs will automagically detect them.
210  * ---------------------------------------------------------------------
211  * Local variables:
212  * mode: c
213  * indent-tabs-mode: t
214  * c-basic-offset: 4
215  * tab-width: 4
216  * End:
217  * vim:noexpandtab:sw=4:ts=4:
218  */