Merged trunk and subtype.
[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, 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 "vm/global.h"
34 #include "vm/vm.hpp"
35
36 #include "vm/jit/asmpart.h"
37 #include "vm/jit/jit.hpp"
38
39
40 /* md_init *********************************************************************
41
42    Do some machine dependent initialization.
43
44 *******************************************************************************/
45
46 void md_init(void)
47 {
48         (void) asm_md_init();
49 }
50
51
52 /* md_jit_method_patch_address *************************************************
53
54    Gets the patch address of the currently compiled method. The offset
55    is extracted from the load instruction(s) before the jump and added
56    to the right base address (PV or REG_METHODPTR).
57
58    INVOKESTATIC/SPECIAL:
59
60    b9 30 00 49 b7             mov    $0xb7490030,%ecx
61    ff d1                      call   *%ecx
62
63    INVOKEVIRTUAL:
64
65    8b 08                      mov    (%eax),%ecx
66    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
67    ff d2                      call   *%edx
68
69    INVOKEINTERFACE:
70
71    8b 08                      mov    (%eax),%ecx
72    8b 89 00 00 00 00          mov    0x0(%ecx),%ecx
73    8b 91 00 00 00 00          mov    0x0(%ecx),%edx
74    ff d2                      call   *%edx
75
76 *******************************************************************************/
77
78 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
79 {
80         uint8_t  *pc;
81         uint16_t  opcode;
82         int32_t   disp;
83         void     *pa;                                            /* patch address */
84
85         /* go back to the actual call instruction (2-bytes) */
86
87         pc = ((uint8_t *) ra) - 2;
88
89         /* Get the opcode of the call. */
90
91         opcode = *((uint16_t *) pc);
92
93         /* check for the different calls */
94
95         switch (opcode) {
96         case 0xd1ff:
97                 /* INVOKESTATIC/SPECIAL */
98
99                 /* Patch address is 4-bytes before the call instruction. */
100
101                 pa = pc - 4;
102                 break;
103
104         case 0xd2ff:
105                 /* INVOKEVIRTUAL/INTERFACE */
106
107                 /* Return NULL if no mptr was specified (used for
108                    replacement). */
109
110                 if (mptr == NULL)
111                         return NULL;
112
113                 /* Get the displacement from the instruction (the displacement
114                    address is 4-bytes before the call instruction). */
115
116                 disp = *((int32_t *) (pc - 4));
117
118                 /* Add the displacement to the method pointer. */
119
120                 pa = ((uint8_t *) mptr) + disp;
121                 break;
122
123         default:
124                 vm_abort_disassemble(pc, 1, "md_jit_method_patch_address: unknown instruction %x", opcode);
125                 return NULL;
126         }
127
128         return pa;
129 }
130
131
132 /* md_patch_replacement_point **************************************************
133
134    Patch the given replacement point.
135
136 *******************************************************************************/
137
138 #if defined(ENABLE_REPLACEMENT)
139 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
140 {
141         u2 mcode;
142
143         if (revert) {
144                 /* write saved machine code */
145                 *(u2*)(pc) = *(u2*)(savedmcode);
146         }
147         else {
148                 /* save the current machine code */
149                 *(u2*)(savedmcode) = *(u2*)(pc);
150
151                 /* build the machine code for the patch */
152                 mcode = 0x0b0f;
153
154                 /* write new machine code */
155                 *(u2*)(pc) = mcode;
156         }
157
158     /* XXX if required asm_cacheflush(pc,8); */
159 }
160 #endif /* defined(ENABLE_REPLACEMENT) */
161
162 /*
163  * These are local overrides for various environment variables in Emacs.
164  * Please do not remove this and leave it at the end of the file, where
165  * Emacs will automagically detect them.
166  * ---------------------------------------------------------------------
167  * Local variables:
168  * mode: c
169  * indent-tabs-mode: t
170  * c-basic-offset: 4
171  * tab-width: 4
172  * End:
173  * vim:noexpandtab:sw=4:ts=4:
174  */