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