Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / jit / powerpc64 / linux / md-abi.c
1 /* src/vm/jit/powerpc64/linux/md-abi.c - functions for PowerPC64 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 7596 2007-03-28 21:05:53Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/powerpc64/linux/md-abi.h"
37
38 #include "vm/global.h"
39
40 #include "vm/jit/abi.h"
41
42 #include "vmcore/descriptor.h"
43
44
45 #define CACAO_ALIGN(a)    do { if ((a) & 1) (a)++; } while (0)
46
47
48 /* register descripton array **************************************************/
49
50 s4 nregdescint[] = {
51         /* zero,      sp,     TOC,   a0/v0,   a1/v1,      a2,      a3,      a4,   */
52         REG_RES, REG_RES, REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
53
54         /*   a5,      a6,      a7,   itmp1,   itmp2, NO(SYS),      pv,      s0,   */
55         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_RES, REG_SAV,
56
57         /*itmp3,      t0,      t1,      t2,      t3,      t4,      t5,      t6,   */
58         REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
59
60         /*   s1,      s2,      s3,      s4,      s5,      s6,      s7,      s8,   */
61         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
62
63         REG_END
64 };
65
66 const char *abi_registers_integer_name[] = {
67         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
68         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
69         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
70         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
71 };
72
73
74 s4 nregdescfloat[] = {
75         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
76         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
77
78         /*  fa7,     fa8,     fa9,    fa10,    fa11,    fa12,   ftmp1,   ftmp2,   */
79         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES,
80
81         /*  fs0,     fs1,     fs2,     fs3,     fs4,     fs5,     fs6,     fs7    */
82         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
83
84         /*  fs8,     fs9,    fs10,    fs11,    fs12,    fs13,    fs14,    fs15    */
85         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
86
87         REG_END
88 };
89
90
91 /* md_param_alloc **************************************************************
92
93    Allocate Arguments to Stackslots according the Calling Conventions
94
95    --- in
96    md->paramcount:           Number of arguments for this method
97    md->paramtypes[].type:    Argument types
98
99    --- out
100    md->params[].inmemory:    Argument spilled on stack
101    md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
102    md->memuse:               Stackslots needed for argument spilling
103    md->argintreguse:         max number of integer arguments used
104    md->argfltreguse:         max number of float arguments used
105
106 *******************************************************************************/
107
108 void md_param_alloc(methoddesc *md)
109 {
110         paramdesc *pd;
111         s4         i;
112         s4         iarg;
113         s4         farg;
114         s4         arg;
115         s4         stacksize, stackcount;
116
117
118         /* set default values */
119
120         iarg = 0;
121         farg = 0;
122         arg = 0;
123         stacksize = LA_SIZE_IN_POINTERS;
124         stackcount = 0;
125
126         /* get params field of methoddesc */
127
128         pd = md->params;
129
130         for (i = 0; i < md->paramcount; i++, pd++) {
131                 switch (md->paramtypes[i].type) {
132                 case TYPE_LNG:
133                 case TYPE_INT:
134                 case TYPE_ADR:
135                         if (iarg < INT_ARG_CNT) {
136                                 pd->inmemory = false;
137                                 pd->regoff = iarg;
138                                 iarg++;
139                         } else {
140                                 pd->inmemory = true;
141                                 pd->regoff = stacksize + stackcount;
142                         }
143                         break;
144                 case TYPE_FLT:
145                 case TYPE_DBL:
146                         if (farg < FLT_ARG_CNT) {
147                                 pd->inmemory = false;
148                                 pd->regoff = farg;
149                                 farg++;
150                                 if (arg < INT_ARG_CNT) {
151                                         iarg++;         /* yes, that is true, floating arguments take int register slots away */
152                                 }
153                         } else {
154                                 pd->inmemory = true;
155                                 pd->regoff = stacksize + stackcount ;
156                         }
157                         break;
158                 default:
159                         assert(0);
160                 }
161                 arg++;
162                 stackcount++;
163         }
164
165         /* Since R3, F1 (==A0, A0) are used for passing return values, this */
166         /* argument register usage has to be regarded, too                        */
167         if (IS_INT_LNG_TYPE(md->returntype.type)) {
168                 if (iarg < 1)
169                         iarg = 1;
170         } else if (IS_FLT_DBL_TYPE(md->returntype.type)) {
171                 if (farg < 1)
172                         farg = 1;
173         }
174
175         /* fill register and stack usage, parameter areas is at least PA_SIZE_IN_POINTERS */
176
177         md->argintreguse = iarg;
178         md->argfltreguse = farg;
179         md->memuse = stacksize + (stackcount<PA_SIZE_IN_POINTERS? PA_SIZE_IN_POINTERS: stackcount);     
180 }
181
182
183 /* md_param_alloc_native *******************************************************
184
185    Pre-allocate arguments according the native ABI.
186
187 *******************************************************************************/
188
189 void md_param_alloc_native(methoddesc *md)
190 {
191         /* On PowerPC64 we use the same ABI for JIT method calls as for
192            native method calls. */
193
194         md_param_alloc(md);
195 }
196
197
198 /* md_return_alloc *************************************************************
199
200    Precolor the Java Stackelement containing the Return Value, if
201    possible.  (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
202    for float/double)
203
204    --- in
205    jd:                      jitdata of the current method
206    stackslot:               Java Stackslot to contain the Return Value
207
208    --- out
209    if precoloring was possible:
210    VAR(stackslot->varnum)->flags     = PREALLOC
211    VAR(stackslot->varnum)->vv.regoff = [REG_RESULT, REG_FRESULT]
212    rd->arg[flt|int]reguse   set to a value according the register usage
213
214 *******************************************************************************/
215
216 void md_return_alloc(jitdata *jd, stackptr stackslot)
217 {
218         methodinfo *m;
219         codeinfo *code;
220         registerdata *rd;
221         methoddesc *md;
222
223         /* get required compiler data */
224
225         m = jd->m;
226         code = jd->code;
227         rd = jd->rd;
228
229         md = m->parseddesc;
230         
231         /* In Leafmethods Local Vars holding parameters are precolored to
232            their argument register -> so leafmethods with paramcount > 0
233            could already use R3 == a00! */
234
235         if (!jd->isleafmethod || (md->paramcount == 0)) {
236                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
237                    has not to survive method invokations. */
238
239                 if (!(stackslot->flags & SAVEDVAR)) {
240
241                         VAR(stackslot->varnum)->flags = PREALLOC;
242
243                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
244                                 if (rd->argintreguse < 1)
245                                         rd->argintreguse = 1;
246
247                                 VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
248                         } else { /* float/double */
249                                 if (rd->argfltreguse < 1)
250                                         rd->argfltreguse = 1;
251
252                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
253                         }
254                 }
255         }
256 }
257
258
259 /*
260  * These are local overrides for various environment variables in Emacs.
261  * Please do not remove this and leave it at the end of the file, where
262  * Emacs will automagically detect them.
263  * ---------------------------------------------------------------------
264  * Local variables:
265  * mode: c
266  * indent-tabs-mode: t
267  * c-basic-offset: 4
268  * tab-width: 4
269  * End:
270  */