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