Proper x86_64 mnemonics
[cacao.git] / src / vm / jit / x86_64 / md-abi.c
1 /* src/vm/jit/x86_64/md-abi.c - functions for x86_64 Linux ABI
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 #include "vm/types.h"
28
29 #include "vm/jit/x86_64/md-abi.h"
30
31 #include "vm/global.h"
32
33 #include "vm/jit/abi.h"
34 #include "vm/jit/jit.h" /* for REG_* (maybe can be removed) */
35 #include "vm/jit/stack.h"
36
37 #include "vmcore/descriptor.h"
38
39
40 /* register descripton array **************************************************/
41
42 s4 nregdescint[] = {
43     REG_RET, REG_ARG, REG_ARG, REG_TMP, REG_RES, REG_SAV, REG_ARG, REG_ARG,
44     REG_ARG, REG_ARG, REG_RES, REG_RES, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
45     REG_END
46 };
47
48 const char *abi_registers_integer_name[] = {
49         "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
50         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15"
51 };
52
53 const s4 abi_registers_integer_argument[] = {
54         7,  /* a0 */
55         6,  /* a1 */
56         2,  /* a2 */
57         1,  /* a3 */
58         8,  /* a4 */
59         9,  /* a5 */
60 };
61
62 const s4 abi_registers_integer_saved[] = {
63         5,  /* s0 */
64         12, /* s1 */
65         13, /* s2 */
66         14, /* s3 */
67         15, /* s4 */
68 };
69
70 const s4 abi_registers_integer_temporary[] = {
71         3,  /* t0 */
72 };
73
74
75 /* float registers *************************************************************
76
77    xmm0,   xmm1,   xmm2,   xmm3,   xmm4,   xmm5,   xmm6,   xmm7,
78    (fa0)   (fa1)   (fa2)   (fa3)   (fa4)   (fa5)   (fa6)   (fa7)
79
80    xmm8,   xmm9,   xmm10,  xmm11,  xmm12,  xmm13,  xmm14,  xmm15
81    (ftmp1) (ftmp2) (ftmp3) (ft0)   (ft1)   (ft2)   (ft3)   (ft4)
82
83 *******************************************************************************/
84
85 s4 nregdescfloat[] = {
86     REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
87     REG_RES, REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
88     REG_END
89 };
90
91
92 const s4 abi_registers_float_argument[] = {
93         0,  /* fa0 */
94         1,  /* fa1 */
95         2,  /* fa2 */
96         3,  /* fa3 */
97         4,  /* fa4 */
98         5,  /* fa5 */
99         6,  /* fa6 */
100         7,  /* fa7 */
101 };
102
103 const s4 abi_registers_float_saved[] = {
104         -1,
105 };
106
107 const s4 abi_registers_float_temporary[] = {
108         11, /* ft0 */
109         12, /* ft1 */
110         13, /* ft2 */
111         14, /* ft3 */
112         15, /* ft4 */
113 };
114
115
116 /* md_param_alloc **************************************************************
117
118    XXX
119
120 *******************************************************************************/
121
122 void md_param_alloc(methoddesc *md)
123 {
124         paramdesc *pd;
125         s4         i;
126         s4         iarg;
127         s4         farg;
128         s4         stacksize;
129
130         /* set default values */
131
132         iarg      = 0;
133         farg      = 0;
134         stacksize = 0;
135
136         /* get params field of methoddesc */
137
138         pd = md->params;
139
140         for (i = 0; i < md->paramcount; i++, pd++) {
141                 switch (md->paramtypes[i].type) {
142                 case TYPE_INT:
143                 case TYPE_ADR:
144                 case TYPE_LNG:
145                         if (iarg < INT_ARG_CNT) {
146                                 pd->inmemory = false;
147                                 pd->index    = iarg;
148                                 pd->regoff   = abi_registers_integer_argument[iarg];
149                                 iarg++;
150                         }
151                         else {
152                                 pd->inmemory = true;
153                                 pd->index    = stacksize;
154                                 pd->regoff   = stacksize * 8;
155                                 stacksize++;
156                         }
157                         break;
158
159                 case TYPE_FLT:
160                 case TYPE_DBL:
161                         if (farg < FLT_ARG_CNT) {
162                                 pd->inmemory = false;
163                                 pd->index    = farg;
164                                 pd->regoff   = abi_registers_float_argument[farg];
165                                 farg++;
166                         }
167                         else {
168                                 pd->inmemory = true;
169                                 pd->index    = stacksize;
170                                 pd->regoff   = stacksize * 8;
171                                 stacksize++;
172                         }
173                         break;
174                 }
175         }
176
177         /* Since XMM0 (==A0) is used for passing return values, this
178            argument register usage has to be regarded, too. */
179
180         if (IS_FLT_DBL_TYPE(md->returntype.type))
181                 if (farg < 1)
182                         farg = 1;
183
184         /* fill register and stack usage */
185
186         md->argintreguse = iarg;
187         md->argfltreguse = farg;
188         md->memuse       = stacksize;
189 }
190
191
192 /* md_param_alloc_native *******************************************************
193
194    Pre-allocate arguments according the native ABI.
195
196 *******************************************************************************/
197
198 void md_param_alloc_native(methoddesc *md)
199 {
200         /* On x86_64 we use the same ABI for JIT method calls as for
201            native method calls. */
202
203         md_param_alloc(md);
204 }
205
206
207 /* md_return_alloc *************************************************************
208
209    Precolor the Java Stackelement containing the Return Value. Only
210    for float/ double types straight forward possible, since INT_LNG
211    types use "reserved" registers Float/Double values use a00 as
212    return register.
213
214    --- in
215    jd:                      jitdata of the current method
216    stackslot:               Java Stackslot to contain the Return Value
217
218    --- out
219    if precoloring was possible:
220    VAR(stackslot->varnum)->flags     = PREALLOC
221                                      ->vv.regoff = [REG_RESULT|REG_FRESULT]
222    rd->arg[flt|int]reguse   set to a value according the register usage
223
224    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
225
226 *******************************************************************************/
227
228 void md_return_alloc(jitdata *jd, stackelement_t *stackslot)
229 {
230         methodinfo   *m;
231         codeinfo     *code;
232         registerdata *rd;
233         methoddesc   *md;
234
235         /* get required compiler data */
236
237         m    = jd->m;
238         code = jd->code;
239         rd   = jd->rd;
240
241         md = m->parseddesc;
242
243         /* precoloring only straightforward possible with flt/dbl types
244            For Address/Integer/Long REG_RESULT == rax == REG_ITMP1 and so
245            could be destroyed if the return value Stack Slot "lives too
246            long" */
247
248         if (IS_FLT_DBL_TYPE(md->returntype.type)) {
249                 /* In Leafmethods Local Vars holding parameters are precolored
250                    to their argument register -> so leafmethods with
251                    paramcount > 0 could already use a00! */
252
253                 if (!code_is_leafmethod(code) || (md->paramcount == 0)) {
254                         /* Only precolor the stackslot, if it is not a SAVEDVAR
255                            <-> has not to survive method invokations */
256
257                         if (!(stackslot->flags & SAVEDVAR)) {
258
259                                 VAR(stackslot->varnum)->flags = PREALLOC;
260
261                             /* float/double */
262                                 if (rd->argfltreguse < 1)
263                                         rd->argfltreguse = 1;
264
265                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
266                         }
267                 }
268         }
269 }
270
271
272 /*
273  * These are local overrides for various environment variables in Emacs.
274  * Please do not remove this and leave it at the end of the file, where
275  * Emacs will automagically detect them.
276  * ---------------------------------------------------------------------
277  * Local variables:
278  * mode: c
279  * indent-tabs-mode: t
280  * c-basic-offset: 4
281  * tab-width: 4
282  * End:
283  */