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