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