Merged trunk and subtype.
[cacao.git] / src / vm / jit / x86_64 / md.c
1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 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 #include <stdlib.h>
31
32 #include "vm/jit/x86_64/md-abi.h"
33
34 #include "vm/vm.hpp"
35
36 #include "vm/jit/codegen-common.hpp"
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         /* nothing to do */
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    4d 8b 15 e2 fe ff ff             mov    -286(%rip),%r10
61    49 ff d2                         rex64Z callq  *%r10
62
63    INVOKEVIRTUAL:
64
65    4c 8b 17                         mov    (%rdi),%r10
66    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
67    48 ff d3                         rex64 callq  *%rax
68
69    INVOKEINTERFACE:
70
71    4c 8b 17                         mov    (%rdi),%r10
72    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
73    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
74    48 ff d3                         rex64 callq  *%r11
75
76 *******************************************************************************/
77
78 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
79 {
80         uint8_t *pc;
81         uint8_t  mcode;
82         int32_t  offset;
83         void    *pa;                        /* patch address                      */
84
85         /* go back to the actual call instruction (3-bytes) */
86
87         pc = ((uint8_t *) ra) - 3;
88
89         /* get the last byte of the call */
90
91         mcode = pc[2];
92
93         /* check for the different calls */
94
95         if (mcode == 0xd2) {
96                 /* INVOKESTATIC/SPECIAL */
97
98                 /* Get the offset from the instruction (the offset address is
99                    4-bytes before the call instruction). */
100
101                 offset = *((int32_t *) (pc - 4));
102
103                 /* add the offset to the return address (IP-relative addressing) */
104
105                 pa = pc + offset;
106         }
107         else if (mcode == 0xd3) {
108                 /* INVOKEVIRTUAL/INTERFACE */
109
110                 /* return NULL if no mptr was specified (used for replacement) */
111
112                 if (mptr == NULL)
113                         return NULL;
114
115                 /* Get the offset from the instruction (the offset address is
116                    4-bytes before the call instruction). */
117
118                 offset = *((int32_t *) (pc - 4));
119
120                 /* add the offset to the method pointer */
121
122                 pa = ((uint8_t *) mptr) + offset;
123         }
124         else {
125                 /* catch any problems */
126
127                 vm_abort("md_jit_method_patch_address: unknown instruction %x", mcode);
128
129                 /* keep compiler happy */
130
131                 pa = NULL;
132         }
133
134         return pa;
135 }
136
137
138 /* md_patch_replacement_point **************************************************
139
140    Patch the given replacement point.
141
142 *******************************************************************************/
143
144 #if defined(ENABLE_REPLACEMENT)
145 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
146 {
147         u2 mcode;
148
149         if (revert) {
150                 /* write saved machine code */
151                 *(u2*)(pc) = *(u2*)(savedmcode);
152         }
153         else {
154                 /* save the current machine code */
155                 *(u2*)(savedmcode) = *(u2*)(pc);
156
157                 /* build the machine code for the patch */
158                 mcode = 0x0b0f;
159
160                 /* write new machine code */
161                 *(u2*)(pc) = (u2) mcode;
162         }
163
164     /* XXX if required asm_cacheflush(pc,8); */
165 }
166 #endif /* defined(ENABLE_REPLACEMENT) */
167
168
169 /*
170  * These are local overrides for various environment variables in Emacs.
171  * Please do not remove this and leave it at the end of the file, where
172  * Emacs will automagically detect them.
173  * ---------------------------------------------------------------------
174  * Local variables:
175  * mode: c
176  * indent-tabs-mode: t
177  * c-basic-offset: 4
178  * tab-width: 4
179  * End:
180  * vim:noexpandtab:sw=4:ts=4:
181  */