* src/vm/jit/powerpc/darwin/md-abi.h (REG_FA0, REG_FA1): Added.
[cacao.git] / src / vm / jit / powerpc / darwin / md-abi.c
1 /* src/vm/jit/powerpc/darwin/md-abi.c - functions for PowerPC Darwin 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes: Christian Ullrich
30
31    $Id: md-abi.c 7770 2007-04-19 19:39:06Z twisti $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "vm/jit/powerpc/darwin/md-abi.h"
40
41 #include "vm/global.h"
42
43 #include "vm/jit/abi.h"
44
45 #include "vmcore/descriptor.h"
46
47
48 /* register descripton arrays *************************************************/
49
50 s4 nregdescint[] = {
51         /* zero,      sp,      t0,   a0/v0,   a0/v1,      a2,      a3,      a4,   */
52         REG_RES, REG_RES, REG_TMP, 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,      t1,      t2,      t3,      t4,      t5,      t6,      t7,   */
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         2,  /* t0 */
99         17, /* t1 */
100         18, /* t2 */
101         19, /* t3 */
102         20, /* t4 */
103         21, /* t5 */
104         22, /* t6 */
105         23, /* t7 */
106 };
107
108
109 s4 nregdescfloat[] = {
110         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
111         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
112
113         /*  fa7,     fa8,     fa9,    fa10,    fa11,    fa12,     fs0,     fs1,   */
114         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_SAV, REG_SAV,
115
116         /*ftmp1,   ftmp2,     ft0,     ft1,     ft2,     ft3,     ft4,     ft5,   */
117         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
118
119         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
120         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
121
122         REG_END
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         9,  /* fa8  */
135         10, /* fa9  */
136         11, /* fa10 */
137         12, /* fa11 */
138         13, /* fa12 */
139 };
140
141 const s4 abi_registers_float_saved[] = {
142         14, /* fs0  */
143         15, /* fs1  */
144         24, /* fs2  */
145         25, /* fs3  */
146         26, /* fs4  */
147         27, /* fs5  */
148         28, /* fs6  */
149         29, /* fs7  */
150         30, /* fs8  */
151         31, /* fs9  */
152 };
153
154 const s4 abi_registers_float_temporary[] = {
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                                 iarg++;
208                         }
209                         else {
210                                 pd->inmemory = true;
211                                 pd->regoff   = stacksize;
212                         }
213                         stacksize++;
214                         break;
215
216                 case TYPE_LNG:
217                         if (iarg < INT_ARG_CNT - 1) {
218                                 pd->inmemory = false;
219                                 pd->regoff   =
220                                         PACK_REGS(abi_registers_integer_argument[iarg + 1],
221                                                           abi_registers_integer_argument[iarg]);
222                                 iarg += 2;
223                         }
224                         else {
225                                 pd->inmemory = true;
226                                 pd->regoff   = stacksize;
227                                 iarg         = INT_ARG_CNT;
228                         }
229                         stacksize += 2;
230                         break;
231
232                 case TYPE_FLT:
233                         if (farg < FLT_ARG_CNT) {
234                                 pd->inmemory = false;
235                                 pd->regoff   = abi_registers_float_argument[farg];
236                                 iarg++;                   /* skip 1 integer argument register */
237                                 farg++;
238                         }
239                         else {
240                                 pd->inmemory = true;
241                                 pd->regoff   = stacksize;
242                         }
243                         stacksize++;
244                         break;
245
246                 case TYPE_DBL:
247                         if (farg < FLT_ARG_CNT) {
248                                 pd->inmemory = false;
249                                 pd->regoff   = abi_registers_float_argument[farg];
250                                 iarg += 2;               /* skip 2 integer argument registers */
251                                 farg++;
252                         }
253                         else {
254                                 pd->inmemory = true;
255                                 pd->regoff   = stacksize;
256                         }
257                         stacksize += 2;
258                         break;
259                 }
260         }
261
262
263         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
264         /* argument register usage has to be regarded, too                        */
265
266         if (IS_INT_LNG_TYPE(md->returntype.type)) {
267                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
268                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
269         }
270         else {
271                 if (IS_FLT_DBL_TYPE(md->returntype.type))
272                         if (farg < 1)
273                                 farg = 1;
274         }
275
276         /* fill register and stack usage */
277
278         md->argintreguse = iarg;
279         md->argfltreguse = farg;
280         md->memuse       = stacksize;
281 }
282
283
284 /* md_param_alloc_native *******************************************************
285
286    Pre-allocate arguments according the native ABI.
287
288 *******************************************************************************/
289
290 void md_param_alloc_native(methoddesc *md)
291 {
292         /* On PowerPC we use the same ABI for JIT method calls as for
293            native method calls. */
294
295         md_param_alloc(md);
296 }
297
298
299 /* md_return_alloc *************************************************************
300
301    Precolor the Java Stackelement containing the Return Value, if
302    possible. (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
303    for float/double)
304
305    --- in
306    jd:                      jitdata of the current method
307    stackslot:               Java Stackslot to contain the Return Value
308    
309    --- out
310    if precoloring was possible:
311    VAR(stackslot->varnum)->flags  = PREALLOC
312                                      ->regoff = [REG_RESULT|REG_FRESULT]
313    rd->arg[flt|int]reguse   set to a value according the register usage
314
315    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
316
317 *******************************************************************************/
318
319 void md_return_alloc(jitdata *jd, stackptr stackslot)
320 {
321         methodinfo   *m;
322         registerdata *rd;
323         methoddesc   *md;
324
325         /* get required compiler data */
326
327         m  = jd->m;
328         rd = jd->rd;
329
330         md = m->parseddesc;
331
332         /* In Leafmethods Local Vars holding parameters are precolored to
333            their argument register -> so leafmethods with paramcount > 0
334            could already use R3 == a00! */
335
336         if (!jd->isleafmethod || (md->paramcount == 0)) {
337                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
338                    has not to survive method invokations. */
339
340                 if (!(stackslot->flags & SAVEDVAR)) {
341
342                         VAR(stackslot->varnum)->flags = PREALLOC;
343
344                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
345                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
346                                         if (rd->argintreguse < 1)
347                                                 rd->argintreguse = 1;
348
349                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
350                                 }
351                                 else {
352                                         if (rd->argintreguse < 2)
353                                                 rd->argintreguse = 2;
354
355                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
356                                 }
357                         }
358                         else {
359                                 if (rd->argfltreguse < 1)
360                                         rd->argfltreguse = 1;
361
362                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
363                         }
364                 }
365         }
366 }
367
368
369 /*
370  * These are local overrides for various environment variables in Emacs.
371  * Please do not remove this and leave it at the end of the file, where
372  * Emacs will automagically detect them.
373  * ---------------------------------------------------------------------
374  * Local variables:
375  * mode: c
376  * indent-tabs-mode: t
377  * c-basic-offset: 4
378  * tab-width: 4
379  * End:
380  */