* src/vm/jit/codegen-common.c (codegen_findmethod): Renamed to
[cacao.git] / src / vm / jit / i386 / md.c
1 /* src/vm/jit/i386/md.c - machine dependent i386 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 5233 2006-08-14 10:59:39Z twisti $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include <assert.h>
40
41 #include "vm/global.h"
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/codegen-common.h"
44
45 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
46 #include "vm/options.h" /* XXX debug */
47 #include "vm/jit/disass.h" /* XXX debug */
48 #endif
49
50
51 /* md_init *********************************************************************
52
53    Do some machine dependent initialization.
54
55 *******************************************************************************/
56
57 void md_init(void)
58 {
59         (void) asm_md_init();
60 }
61
62
63 /* md_stacktrace_get_returnaddress *********************************************
64
65    Returns the return address of the current stackframe, specified by
66    the passed stack pointer and the stack frame size.
67
68 *******************************************************************************/
69
70 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
71 {
72         u1 *ra;
73
74         /* on i386 the return address is above the current stack frame */
75
76         ra = *((u1 **) (sp + framesize));
77
78         return ra;
79 }
80
81
82 /* md_get_method_patch_address *************************************************
83
84    Gets the patch address of the currently compiled method. The offset
85    is extracted from the load instruction(s) before the jump and added
86    to the right base address (PV or REG_METHODPTR).
87
88    INVOKESTATIC/SPECIAL:
89
90    b9 30 00 49 b7             mov    $0xb7490030,%ecx
91    ff d1                      call   *%ecx
92
93    INVOKEVIRTUAL:
94
95    8b 08                      mov    (%eax),%ecx
96    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
97    ff d2                      call   *%edx
98
99    INVOKEINTERFACE:
100
101    8b 08                      mov    (%eax),%ecx
102    8b 89 00 00 00 00          mov    0x0(%ecx),%ecx
103    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
104    ff d2                      call   *%edx
105
106 *******************************************************************************/
107
108 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
109 {
110         u1  mcode;
111         s4  offset;
112         u1 *pa;                             /* patch address                      */
113
114         /* go back to the actual call instruction (2-bytes) */
115
116         ra = ra - 2;
117
118         /* get the last byte of the call */
119
120         mcode = ra[1];
121
122         /* check for the different calls */
123
124         if (mcode == 0xd1) {
125                 /* INVOKESTATIC/SPECIAL */
126
127                 /* patch address is 4-bytes before the call instruction */
128
129                 pa = ra - 4;
130         }
131         else if (mcode == 0xd2) {
132                 /* INVOKEVIRTUAL/INTERFACE */
133
134                 /* Get the offset from the instruction (the offset address is
135                    4-bytes before the call instruction). */
136
137                 offset = *((s4 *) (ra - 4));
138
139                 /* add the offset to the method pointer */
140
141                 pa = mptr + offset;
142         }
143         else {
144                 /* catch any problems */
145
146                 pa = NULL; /* avoid warnings */
147
148                 vm_abort("couldn't find a proper call instruction sequence");
149         }
150
151         return pa;
152 }
153
154
155 /* md_codegen_get_pv_from_pc ***************************************************
156
157    On this architecture just a wrapper function to
158    codegen_get_pv_from_pc.
159
160 *******************************************************************************/
161
162 u1 *md_codegen_get_pv_from_pc(u1 *ra)
163 {
164         u1 *pv;
165
166         /* Get the start address of the function which contains this
167        address from the method table. */
168
169         pv = codegen_get_pv_from_pc(ra);
170
171         return pv;
172 }
173
174
175 /* md_cacheflush ***************************************************************
176
177    Calls the system's function to flush the instruction and data
178    cache.
179
180 *******************************************************************************/
181
182 void md_cacheflush(u1 *addr, s4 nbytes)
183 {
184         /* do nothing */
185 }
186
187
188 /* md_icacheflush **************************************************************
189
190    Calls the system's function to flush the instruction cache.
191
192 *******************************************************************************/
193
194 void md_icacheflush(u1 *addr, s4 nbytes)
195 {
196         /* do nothing */
197 }
198
199
200 /* md_dcacheflush **************************************************************
201
202    Calls the system's function to flush the data cache.
203
204 *******************************************************************************/
205
206 void md_dcacheflush(u1 *addr, s4 nbytes)
207 {
208         /* do nothing */
209 }
210
211
212 /* md_patch_replacement_point **************************************************
213
214    Patch the given replacement point.
215
216 *******************************************************************************/
217
218 void md_patch_replacement_point(rplpoint *rp)
219 {
220     u8 mcode;
221
222         /* XXX this is probably unsafe! */
223
224         /* save the current machine code */
225         mcode = *(u8*)rp->pc;
226
227         /* write spinning instruction */
228         *(u2*)(rp->pc) = 0xebfe;
229
230         /* write 5th byte */
231         rp->pc[4] = (rp->mcode >> 32);
232
233         /* write first word */
234     *(u4*)(rp->pc) = (u4) rp->mcode;
235
236         /* store saved mcode */
237         rp->mcode = mcode;
238         
239 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
240         {
241                 u1* u1ptr = rp->pc;
242                 DISASSINSTR(u1ptr);
243                 fflush(stdout);
244         }
245 #endif
246                         
247     /* XXX if required asm_cacheflush(rp->pc,8); */
248 }
249
250 /*
251  * These are local overrides for various environment variables in Emacs.
252  * Please do not remove this and leave it at the end of the file, where
253  * Emacs will automagically detect them.
254  * ---------------------------------------------------------------------
255  * Local variables:
256  * mode: c
257  * indent-tabs-mode: t
258  * c-basic-offset: 4
259  * tab-width: 4
260  * End:
261  * vim:noexpandtab:sw=4:ts=4:
262  */