* src/vmcore/statistics.c (vm/jit/code.h): Added for codeinfo.
[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 7468 2007-03-06 11:12:03Z twisti $
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/jit.h" /* for REG_* (maybe can be removed) */
38
39 #include "vmcore/descriptor.h"
40
41
42 /* register descripton array **************************************************/
43
44 s4 nregdescint[] = {
45     REG_RET, REG_ARG, REG_ARG, REG_TMP, REG_RES, REG_SAV, REG_ARG, REG_ARG,
46     REG_ARG, REG_ARG, REG_RES, REG_RES, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
47     REG_END
48 };
49
50 const char *abi_registers_integer_name[] = {
51         "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
52         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15"
53 };
54
55
56 s4 nregdescfloat[] = {
57     REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
58     REG_RES, REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
59     REG_END
60 };
61
62
63 /* md_param_alloc **************************************************************
64
65    XXX
66
67 *******************************************************************************/
68
69 void md_param_alloc(methoddesc *md)
70 {
71         paramdesc *pd;
72         s4         i;
73         s4         iarg;
74         s4         farg;
75         s4         stacksize;
76
77         /* set default values */
78
79         iarg = 0;
80         farg = 0;
81         stacksize = 0;
82
83         /* get params field of methoddesc */
84
85         pd = md->params;
86
87         for (i = 0; i < md->paramcount; i++, pd++) {
88                 switch (md->paramtypes[i].type) {
89                 case TYPE_INT:
90                 case TYPE_ADR:
91                 case TYPE_LNG:
92                         if (iarg < INT_ARG_CNT) {
93                                 pd->inmemory = false;
94                                 pd->regoff   = iarg;
95                                 iarg++;
96                         }
97                         else {
98                                 pd->inmemory = true;
99                                 pd->regoff   = stacksize;
100                                 stacksize++;
101                         }
102                         break;
103
104                 case TYPE_FLT:
105                 case TYPE_DBL:
106                         if (farg < FLT_ARG_CNT) {
107                                 pd->inmemory = false;
108                                 pd->regoff   = farg;
109                                 farg++;
110                         }
111                         else {
112                                 pd->inmemory = true;
113                                 pd->regoff   = stacksize;
114                                 stacksize++;
115                         }
116                         break;
117                 }
118         }
119
120         /* Since XMM0 (==A0) is used for passing return values, this
121            argument register usage has to be regarded, too. */
122
123         if (IS_FLT_DBL_TYPE(md->returntype.type))
124                 if (farg < 1)
125                         farg = 1;
126
127         /* fill register and stack usage */
128
129         md->argintreguse = iarg;
130         md->argfltreguse = farg;
131         md->memuse       = stacksize;
132 }
133
134
135 /* md_param_alloc_native *******************************************************
136
137    Pre-allocate arguments according the native ABI.
138
139 *******************************************************************************/
140
141 void md_param_alloc_native(methoddesc *md)
142 {
143         /* On x86_64 we use the same ABI for JIT method calls as for
144            native method calls. */
145
146         md_param_alloc(md);
147 }
148
149
150 /* md_return_alloc *************************************************************
151
152    Precolor the Java Stackelement containing the Return Value. Only
153    for float/ double types straight forward possible, since INT_LNG
154    types use "reserved" registers Float/Double values use a00 as
155    return register.
156
157    --- in
158    jd:                      jitdata of the current method
159    stackslot:               Java Stackslot to contain the Return Value
160
161    --- out
162    if precoloring was possible:
163    VAR(stackslot->varnum)->flags     = PREALLOC
164                                      ->vv.regoff = [REG_RESULT|REG_FRESULT]
165    rd->arg[flt|int]reguse   set to a value according the register usage
166
167    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
168
169 *******************************************************************************/
170
171 void md_return_alloc(jitdata *jd, stackptr stackslot)
172 {
173         methodinfo   *m;
174         registerdata *rd;
175         methoddesc   *md;
176
177         /* get required compiler data */
178
179         m  = jd->m;
180         rd = jd->rd;
181
182         md = m->parseddesc;
183
184         /* precoloring only straightforward possible with flt/dbl types
185            For Address/Integer/Long REG_RESULT == rax == REG_ITMP1 and so
186            could be destroyed if the return value Stack Slot "lives too
187            long" */
188
189         if (IS_FLT_DBL_TYPE(md->returntype.type)) {
190                 /* In Leafmethods Local Vars holding parameters are precolored
191                    to their argument register -> so leafmethods with
192                    paramcount > 0 could already use a00! */
193
194                 if (!jd->isleafmethod || (md->paramcount == 0)) {
195                         /* Only precolor the stackslot, if it is not a SAVEDVAR
196                            <-> has not to survive method invokations */
197
198                         if (!(stackslot->flags & SAVEDVAR)) {
199
200                                 VAR(stackslot->varnum)->flags = PREALLOC;
201
202                             /* float/double */
203                                 if (rd->argfltreguse < 1)
204                                         rd->argfltreguse = 1;
205
206                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
207                         }
208                 }
209         }
210 }
211
212
213 /*
214  * These are local overrides for various environment variables in Emacs.
215  * Please do not remove this and leave it at the end of the file, where
216  * Emacs will automagically detect them.
217  * ---------------------------------------------------------------------
218  * Local variables:
219  * mode: c
220  * indent-tabs-mode: t
221  * c-basic-offset: 4
222  * tab-width: 4
223  * End:
224  */