* Removed all Id tags.
[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 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 */
26
27
28 #include "config.h"
29 #include "vm/types.h"
30
31 #include "vm/jit/x86_64/md-abi.h"
32
33 #include "vm/global.h"
34
35 #include "vm/jit/abi.h"
36 #include "vm/jit/jit.h" /* for REG_* (maybe can be removed) */
37
38 #include "vmcore/descriptor.h"
39
40
41 /* register descripton array **************************************************/
42
43 s4 nregdescint[] = {
44     REG_RET, REG_ARG, REG_ARG, REG_TMP, REG_RES, REG_SAV, REG_ARG, REG_ARG,
45     REG_ARG, REG_ARG, REG_RES, REG_RES, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
46     REG_END
47 };
48
49 const char *abi_registers_integer_name[] = {
50         "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
51         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15"
52 };
53
54 const s4 abi_registers_integer_argument[] = {
55         7,  /* a0 */
56         6,  /* a1 */
57         2,  /* a2 */
58         1,  /* a3 */
59         8,  /* a4 */
60         9,  /* a5 */
61 };
62
63 const s4 abi_registers_integer_saved[] = {
64         7,  /* s0 */
65         12, /* s1 */
66         13, /* s2 */
67         14, /* s3 */
68         15, /* s4 */
69 };
70
71 const s4 abi_registers_integer_temporary[] = {
72         3,  /* t0 */
73 };
74
75
76 /* float registers *************************************************************
77
78    xmm0,   xmm1,   xmm2,   xmm3,   xmm4,   xmm5,   xmm6,   xmm7,
79    (fa0)   (fa1)   (fa2)   (fa3)   (fa4)   (fa5)   (fa6)   (fa7)
80
81    xmm8,   xmm9,   xmm10,  xmm11,  xmm12,  xmm13,  xmm14,  xmm15
82    (ftmp1) (ftmp2) (ftmp3) (ft0)   (ft1)   (ft2)   (ft3)   (ft4)
83
84 *******************************************************************************/
85
86 s4 nregdescfloat[] = {
87     REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
88     REG_RES, REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
89     REG_END
90 };
91
92
93 const s4 abi_registers_float_argument[] = {
94         0,  /* fa0 */
95         1,  /* fa1 */
96         2,  /* fa2 */
97         3,  /* fa3 */
98         4,  /* fa4 */
99         5,  /* fa5 */
100         6,  /* fa6 */
101         7,  /* fa7 */
102 };
103
104 const s4 abi_registers_float_saved[] = {
105         -1,
106 };
107
108 const s4 abi_registers_float_temporary[] = {
109         11, /* ft0 */
110         12, /* ft1 */
111         13, /* ft2 */
112         14, /* ft3 */
113         15, /* ft4 */
114 };
115
116
117 /* md_param_alloc **************************************************************
118
119    XXX
120
121 *******************************************************************************/
122
123 void md_param_alloc(methoddesc *md)
124 {
125         paramdesc *pd;
126         s4         i;
127         s4         iarg;
128         s4         farg;
129         s4         stacksize;
130
131         /* set default values */
132
133         iarg      = 0;
134         farg      = 0;
135         stacksize = 0;
136
137         /* get params field of methoddesc */
138
139         pd = md->params;
140
141         for (i = 0; i < md->paramcount; i++, pd++) {
142                 switch (md->paramtypes[i].type) {
143                 case TYPE_INT:
144                 case TYPE_ADR:
145                 case TYPE_LNG:
146                         if (iarg < INT_ARG_CNT) {
147                                 pd->inmemory = false;
148                                 pd->index    = iarg;
149                                 pd->regoff   = abi_registers_integer_argument[iarg];
150                                 iarg++;
151                         }
152                         else {
153                                 pd->inmemory = true;
154                                 pd->index    = stacksize;
155                                 pd->regoff   = stacksize * 8;
156                                 stacksize++;
157                         }
158                         break;
159
160                 case TYPE_FLT:
161                 case TYPE_DBL:
162                         if (farg < FLT_ARG_CNT) {
163                                 pd->inmemory = false;
164                                 pd->index    = farg;
165                                 pd->regoff   = abi_registers_float_argument[farg];
166                                 farg++;
167                         }
168                         else {
169                                 pd->inmemory = true;
170                                 pd->index    = stacksize;
171                                 pd->regoff   = stacksize * 8;
172                                 stacksize++;
173                         }
174                         break;
175                 }
176         }
177
178         /* Since XMM0 (==A0) is used for passing return values, this
179            argument register usage has to be regarded, too. */
180
181         if (IS_FLT_DBL_TYPE(md->returntype.type))
182                 if (farg < 1)
183                         farg = 1;
184
185         /* fill register and stack usage */
186
187         md->argintreguse = iarg;
188         md->argfltreguse = farg;
189         md->memuse       = stacksize;
190 }
191
192
193 /* md_param_alloc_native *******************************************************
194
195    Pre-allocate arguments according the native ABI.
196
197 *******************************************************************************/
198
199 void md_param_alloc_native(methoddesc *md)
200 {
201         /* On x86_64 we use the same ABI for JIT method calls as for
202            native method calls. */
203
204         md_param_alloc(md);
205 }
206
207
208 /* md_return_alloc *************************************************************
209
210    Precolor the Java Stackelement containing the Return Value. Only
211    for float/ double types straight forward possible, since INT_LNG
212    types use "reserved" registers Float/Double values use a00 as
213    return register.
214
215    --- in
216    jd:                      jitdata of the current method
217    stackslot:               Java Stackslot to contain the Return Value
218
219    --- out
220    if precoloring was possible:
221    VAR(stackslot->varnum)->flags     = PREALLOC
222                                      ->vv.regoff = [REG_RESULT|REG_FRESULT]
223    rd->arg[flt|int]reguse   set to a value according the register usage
224
225    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
226
227 *******************************************************************************/
228
229 void md_return_alloc(jitdata *jd, stackptr stackslot)
230 {
231         methodinfo   *m;
232         registerdata *rd;
233         methoddesc   *md;
234
235         /* get required compiler data */
236
237         m  = jd->m;
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 (!jd->isleafmethod || (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  */