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