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