* src/vm/jit/intrp/peephole.c: Updated to current codebase.
[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 7356 2007-02-14 11:00:28Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/powerpc/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 _ALIGN(a)    do { if ((a) & 1) (a)++; } while (0)
46
47
48 /* register descripton arrays *************************************************/
49
50 s4 nregdescint[] = {
51         /* zero,      sp, NO(sys),   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,      pv,      s0,      s1,   */
55         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, 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         /*   s2,      s3,      s4,      s5,      s6,      s7,      s8,      s9,   */
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         14, /* s0 */
86         15, /* s1 */
87         24, /* s2 */
88         25, /* s3 */
89         26, /* s4 */
90         27, /* s5 */
91         28, /* s6 */
92         29, /* s7 */
93         30, /* s8 */
94         31, /* s9 */
95 };
96
97 const s4 abi_registers_integer_temporary[] = {
98         17, /* t0 */
99         18, /* t1 */
100         19, /* t2 */
101         20, /* t3 */
102         21, /* t4 */
103         22, /* t5 */
104         23, /* t6 */
105 };
106
107
108 s4 nregdescfloat[] = {
109         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
110         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
111
112         /*  fa7,     ft0,     ft1,     ft2,     ft3,     ft4,     fs0,     fs1,   */
113         REG_ARG, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_SAV, REG_SAV,
114
115         /*ftmp1,   ftmp2,     ft5,     ft6,     ft7,     ft8,     ft9,    ft10,   */
116         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
117
118         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
119         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
120
121         REG_END
122 };
123
124
125 const s4 abi_registers_float_argument[] = {
126         1,  /* fa0  */
127         2,  /* fa1  */
128         3,  /* fa2  */
129         4,  /* fa3  */
130         5,  /* fa4  */
131         6,  /* fa5  */
132         7,  /* fa6  */
133         8,  /* fa7  */
134 };
135
136 const s4 abi_registers_float_saved[] = {
137         14, /* fs0  */
138         15, /* fs1  */
139         24, /* fs2  */
140         25, /* fs3  */
141         26, /* fs4  */
142         27, /* fs5  */
143         28, /* fs6  */
144         29, /* fs7  */
145         30, /* fs8  */
146         31, /* fs9  */
147 };
148
149 const s4 abi_registers_float_temporary[] = {
150         9,  /* ft0  */
151         10, /* ft1  */
152         11, /* ft2  */
153         12, /* ft3  */
154         13, /* ft4  */
155         17, /* ft5  */
156         18, /* ft6  */
157         19, /* ft7  */
158         20, /* ft8  */
159         21, /* ft9  */
160         22, /* ft10 */
161         23, /* ft11 */
162 };
163
164
165 /* md_param_alloc **************************************************************
166
167    Allocate Arguments to Stackslots according the Calling Conventions
168
169    --- in
170    md->paramcount:           Number of arguments for this method
171    md->paramtypes[].type:    Argument types
172
173    --- out
174    md->params[].inmemory:    Argument spilled on stack
175    md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
176    md->memuse:               Stackslots needed for argument spilling
177    md->argintreguse:         max number of integer arguments used
178    md->argfltreguse:         max number of float arguments used
179
180 *******************************************************************************/
181
182 void md_param_alloc(methoddesc *md)
183 {
184         paramdesc *pd;
185         s4         i;
186         s4         iarg;
187         s4         farg;
188         s4         stacksize;
189
190         /* set default values */
191
192         iarg = 0;
193         farg = 0;
194         stacksize = LA_SIZE_IN_POINTERS;
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_INT:
203                 case TYPE_ADR:
204                         if (iarg < INT_ARG_CNT) {
205                                 pd->inmemory  = false;
206 /*                              pd->regoff    = abi_registers_integer_argument[iarg]; */
207                                 pd->regoff    = iarg;
208                                 iarg++;
209                         }
210                         else {
211                                 pd->inmemory  = true;
212                                 pd->regoff    = stacksize;
213                                 stacksize++;
214                         }
215                         break;
216
217                 case TYPE_LNG:
218                         if (iarg < INT_ARG_CNT - 1) {
219                                 _ALIGN(iarg);
220                                 pd->inmemory  = false;
221 /*                              pd->regoff    = */
222 /*                                      PACK_REGS(abi_registers_integer_argument[iarg + 1], */
223 /*                                                        abi_registers_integer_argument[iarg]); */
224                                 pd->regoff    = PACK_REGS(iarg + 1, iarg);
225                                 iarg += 2;
226                         }
227                         else {
228                                 _ALIGN(stacksize);
229                                 pd->inmemory  = true;
230                                 pd->regoff    = stacksize;
231                                 iarg          = INT_ARG_CNT;
232                                 stacksize    += 2;
233                         }
234                         break;
235
236                 case TYPE_FLT:
237                         if (farg < FLT_ARG_CNT) {
238                                 pd->inmemory  = false;
239 /*                              pd->regoff    = abi_registers_float_argument[farg]; */
240                                 pd->regoff    = farg;
241                                 farg++;
242                         }
243                         else {
244                                 pd->inmemory  = true;
245                                 pd->regoff    = stacksize;
246                                 stacksize++;
247                         }
248                         break;
249
250                 case TYPE_DBL:
251                         if (farg < FLT_ARG_CNT) {
252                                 pd->inmemory  = false;
253 /*                              pd->regoff    = abi_registers_integer_argument[farg]; */
254                                 pd->regoff    = farg;
255                                 farg++;
256                         }
257                         else {
258                                 _ALIGN(stacksize);
259                                 pd->inmemory  = true;
260                                 pd->regoff    = stacksize;
261                                 stacksize    += 2;
262                         }
263                         break;
264
265                 default:
266                         assert(0);
267                 }
268         }
269
270         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return
271            values, this argument register usage has to be regarded,
272            too. */
273
274         if (IS_INT_LNG_TYPE(md->returntype.type)) {
275                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
276                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
277         }
278         else {
279                 if (IS_FLT_DBL_TYPE(md->returntype.type))
280                         if (farg < 1)
281                                 farg = 1;
282         }
283
284         /* fill register and stack usage */
285
286         md->argintreguse = iarg;
287         md->argfltreguse = farg;
288         md->memuse = stacksize;
289 }
290
291
292 /* md_param_alloc_native *******************************************************
293
294    Pre-allocate arguments according the native ABI.
295
296 *******************************************************************************/
297
298 void md_param_alloc_native(methoddesc *md)
299 {
300         /* On PowerPC we use the same ABI for JIT method calls as for
301            native method calls. */
302
303         md_param_alloc(md);
304 }
305
306
307 /* md_return_alloc *************************************************************
308
309    Precolor the Java Stackelement containing the Return Value, if
310    possible.  (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
311    for float/double)
312
313    --- in
314    jd:                      jitdata of the current method
315    stackslot:               Java Stackslot to contain the Return Value
316
317    --- out
318    if precoloring was possible:
319    VAR(stackslot->varnum)->flags       = PREALLOC
320                                      ->regoff      = [REG_RESULT|REG_FRESULT]
321    rd->arg[flt|int]reguse   set to a value according the register usage
322
323    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
324
325 *******************************************************************************/
326
327 void md_return_alloc(jitdata *jd, stackptr stackslot)
328 {
329         methodinfo   *m;
330         registerdata *rd;
331         methoddesc   *md;
332
333         /* get required compiler data */
334
335         m  = jd->m;
336         rd = jd->rd;
337
338         md = m->parseddesc;
339
340         /* In Leafmethods Local Vars holding parameters are precolored to
341            their argument register -> so leafmethods with paramcount > 0
342            could already use R3 == a00! */
343
344         if (!jd->isleafmethod || (md->paramcount == 0)) {
345                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
346                    has not to survive method invokations. */
347
348                 if (!(stackslot->flags & SAVEDVAR)) {
349                         VAR(stackslot->varnum)->flags = PREALLOC;
350
351                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
352                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
353                                         if (rd->argintreguse < 1)
354                                                 rd->argintreguse = 1;
355
356                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
357                                 }
358                                 else {
359                                         if (rd->argintreguse < 2)
360                                                 rd->argintreguse = 2;
361
362                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
363                                 }
364                         }
365                         else { /* float/double */
366                                 if (rd->argfltreguse < 1)
367                                         rd->argfltreguse = 1;
368
369                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
370                         }
371                 }
372         }
373 }
374
375
376 /*
377  * These are local overrides for various environment variables in Emacs.
378  * Please do not remove this and leave it at the end of the file, where
379  * Emacs will automagically detect them.
380  * ---------------------------------------------------------------------
381  * Local variables:
382  * mode: c
383  * indent-tabs-mode: t
384  * c-basic-offset: 4
385  * tab-width: 4
386  * End:
387  */