Merged revisions 8187-8244 via svnmerge from
[cacao.git] / src / vm / jit / powerpc / linux / md-abi.c
1 /* src/vm/jit/powerpc/linux/md-abi.c - functions for PowerPC 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 8190 2007-07-08 13:42:06Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "vm/jit/powerpc/linux/md-abi.h"
36
37 #include "vm/global.h"
38
39 #include "vm/jit/abi.h"
40
41 #include "vmcore/descriptor.h"
42
43
44 /* register descripton arrays *************************************************/
45
46 int32_t nregdescint[] = {
47         /* zero,      sp, NO(sys),   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,      pv,      s0,      s1,   */
51         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, 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         /*   s2,      s3,      s4,      s5,      s6,      s7,      s8,      s9,   */
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 int32_t 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 int32_t abi_registers_integer_saved[] = {
81         14, /* s0 */
82         15, /* s1 */
83         24, /* s2 */
84         25, /* s3 */
85         26, /* s4 */
86         27, /* s5 */
87         28, /* s6 */
88         29, /* s7 */
89         30, /* s8 */
90         31, /* s9 */
91 };
92
93 const int32_t 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 int32_t 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,     ft0,     ft1,     ft2,     ft3,     ft4,     fs0,     fs1,   */
109         REG_ARG, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_SAV, REG_SAV,
110
111         /*ftmp1,   ftmp2,     ft5,     ft6,     ft7,     ft8,     ft9,    ft10,   */
112         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
113
114         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
115         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
116
117         REG_END
118 };
119
120
121 const int32_t 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 };
131
132 const int32_t abi_registers_float_saved[] = {
133         14, /* fs0  */
134         15, /* fs1  */
135         24, /* fs2  */
136         25, /* fs3  */
137         26, /* fs4  */
138         27, /* fs5  */
139         28, /* fs6  */
140         29, /* fs7  */
141         30, /* fs8  */
142         31, /* fs9  */
143 };
144
145 const int32_t abi_registers_float_temporary[] = {
146         9,  /* ft0  */
147         10, /* ft1  */
148         11, /* ft2  */
149         12, /* ft3  */
150         13, /* ft4  */
151         18, /* ft5  */
152         19, /* ft6  */
153         20, /* ft7  */
154         21, /* ft8  */
155         22, /* ft9  */
156         23, /* ft10 */
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         int32_t    i;
181         int32_t    iarg;
182         int32_t    farg;
183         int32_t    stacksize;
184
185         /* set default values */
186
187         iarg = 0;
188         farg = 0;
189         stacksize = LA_SIZE_IN_POINTERS;
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_INT:
198                 case TYPE_ADR:
199                         if (iarg < INT_ARG_CNT) {
200                                 pd->inmemory  = false;
201                                 pd->regoff    = abi_registers_integer_argument[iarg];
202                                 iarg++;
203                         }
204                         else {
205                                 pd->inmemory  = true;
206                                 pd->regoff    = stacksize * 4;
207                                 stacksize++;
208                         }
209                         break;
210
211                 case TYPE_LNG:
212                         if (iarg < INT_ARG_CNT - 1) {
213                                 ALIGN_2(iarg);
214                                 pd->inmemory  = false;
215                                 pd->regoff    =
216                                         PACK_REGS(abi_registers_integer_argument[iarg + 1],
217                                                           abi_registers_integer_argument[iarg]);
218                                 iarg += 2;
219                         }
220                         else {
221                                 ALIGN_2(stacksize);
222                                 pd->inmemory  = true;
223                                 pd->regoff    = stacksize * 4;
224                                 iarg          = INT_ARG_CNT;
225                                 stacksize    += 2;
226                         }
227                         break;
228
229                 case TYPE_FLT:
230                         if (farg < FLT_ARG_CNT) {
231                                 pd->inmemory  = false;
232                                 pd->regoff    = abi_registers_float_argument[farg];
233                                 farg++;
234                         }
235                         else {
236                                 pd->inmemory  = true;
237                                 pd->regoff    = stacksize * 4;
238                                 stacksize++;
239                         }
240                         break;
241
242                 case TYPE_DBL:
243                         if (farg < FLT_ARG_CNT) {
244                                 pd->inmemory  = false;
245                                 pd->regoff    = abi_registers_float_argument[farg];
246                                 farg++;
247                         }
248                         else {
249                                 ALIGN_2(stacksize);
250                                 pd->inmemory  = true;
251                                 pd->regoff    = stacksize * 4;
252                                 stacksize    += 2;
253                         }
254                         break;
255
256                 default:
257                         assert(0);
258                 }
259         }
260
261         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return
262            values, this argument register usage has to be regarded,
263            too. */
264
265         if (IS_INT_LNG_TYPE(md->returntype.type)) {
266                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
267                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
268         }
269         else {
270                 if (IS_FLT_DBL_TYPE(md->returntype.type))
271                         if (farg < 1)
272                                 farg = 1;
273         }
274
275         /* fill register and stack usage */
276
277         md->argintreguse = iarg;
278         md->argfltreguse = farg;
279         md->memuse       = stacksize;
280 }
281
282
283 /* md_param_alloc_native *******************************************************
284
285    Pre-allocate arguments according the native ABI.
286
287 *******************************************************************************/
288
289 void md_param_alloc_native(methoddesc *md)
290 {
291         /* On PowerPC we use the same ABI for JIT method calls as for
292            native method calls. */
293
294         md_param_alloc(md);
295 }
296
297
298 /* md_return_alloc *************************************************************
299
300    Precolor the Java Stackelement containing the Return Value, if
301    possible.  (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
302    for float/double)
303
304    --- in
305    jd:                      jitdata of the current method
306    stackslot:               Java Stackslot to contain the Return Value
307
308    --- out
309    if precoloring was possible:
310    VAR(stackslot->varnum)->flags       = PREALLOC
311                                      ->regoff      = [REG_RESULT|REG_FRESULT]
312    rd->arg[flt|int]reguse   set to a value according the register usage
313
314    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
315
316 *******************************************************************************/
317
318 void md_return_alloc(jitdata *jd, stackptr stackslot)
319 {
320         methodinfo   *m;
321         registerdata *rd;
322         methoddesc   *md;
323
324         /* get required compiler data */
325
326         m  = jd->m;
327         rd = jd->rd;
328
329         md = m->parseddesc;
330
331         /* In Leafmethods Local Vars holding parameters are precolored to
332            their argument register -> so leafmethods with paramcount > 0
333            could already use R3 == a00! */
334
335         if (!jd->isleafmethod || (md->paramcount == 0)) {
336                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
337                    has not to survive method invokations. */
338
339                 if (!(stackslot->flags & SAVEDVAR)) {
340                         VAR(stackslot->varnum)->flags = PREALLOC;
341
342                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
343                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
344                                         if (rd->argintreguse < 1)
345                                                 rd->argintreguse = 1;
346
347                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
348                                 }
349                                 else {
350                                         if (rd->argintreguse < 2)
351                                                 rd->argintreguse = 2;
352
353                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
354                                 }
355                         }
356                         else { /* float/double */
357                                 if (rd->argfltreguse < 1)
358                                         rd->argfltreguse = 1;
359
360                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
361                         }
362                 }
363         }
364 }
365
366
367 /*
368  * These are local overrides for various environment variables in Emacs.
369  * Please do not remove this and leave it at the end of the file, where
370  * Emacs will automagically detect them.
371  * ---------------------------------------------------------------------
372  * Local variables:
373  * mode: c
374  * indent-tabs-mode: t
375  * c-basic-offset: 4
376  * tab-width: 4
377  * End:
378  */