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