* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <stdint.h>
31
32 #include "vm/jit/powerpc/darwin/md-abi.h"
33
34 #include "vm/global.h"
35
36 #include "vm/jit/abi.h"
37
38 #include "vmcore/descriptor.h"
39
40
41 /* register descripton arrays *************************************************/
42
43 int nregdescint[] = {
44         /* zero,      sp,      t0,   a0/v0,   a0/v1,      a2,      a3,      a4,   */
45         REG_RES, REG_RES, REG_TMP, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
46
47         /*   a5,      a6,      a7,   itmp1,   itmp2,      pv,      s0,      s1,   */
48         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, REG_SAV,
49
50         /*itmp3,      t1,      t2,      t3,      t4,      t5,      t6,      t7,   */
51         REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
52
53         /*   s2,      s3,      s4,      s5,      s6,      s7,      s8,      s9,   */
54         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
55
56         REG_END
57 };
58
59 const char *abi_registers_integer_name[] = {
60         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
61         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
62         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
63         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
64 };
65
66 const int abi_registers_integer_argument[] = {
67         3,  /* a0 */
68         4,  /* a1 */
69         5,  /* a2 */
70         6,  /* a3 */
71         7,  /* a4 */
72         8,  /* a5 */
73         9,  /* a6 */
74         10, /* a7 */
75 };
76
77 const int abi_registers_integer_saved[] = {
78         14, /* s0 */
79         15, /* s1 */
80         24, /* s2 */
81         25, /* s3 */
82         26, /* s4 */
83         27, /* s5 */
84         28, /* s6 */
85         29, /* s7 */
86         30, /* s8 */
87         31, /* s9 */
88 };
89
90 const int abi_registers_integer_temporary[] = {
91         2,  /* t0 */
92         17, /* t1 */
93         18, /* t2 */
94         19, /* t3 */
95         20, /* t4 */
96         21, /* t5 */
97         22, /* t6 */
98         23, /* t7 */
99 };
100
101
102 int nregdescfloat[] = {
103         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
104         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
105
106         /*  fa7,     fa8,     fa9,    fa10,    fa11,    fa12,     fs0,     fs1,   */
107         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_SAV, REG_SAV,
108
109         /*ftmp1,   ftmp2,     ft0,     ft1,     ft2,     ft3,     ft4,     ft5,   */
110         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
111
112         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
113         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
114
115         REG_END
116 };
117
118 const int abi_registers_float_argument[] = {
119         1,  /* fa0  */
120         2,  /* fa1  */
121         3,  /* fa2  */
122         4,  /* fa3  */
123         5,  /* fa4  */
124         6,  /* fa5  */
125         7,  /* fa6  */
126         8,  /* fa7  */
127         9,  /* fa8  */
128         10, /* fa9  */
129         11, /* fa10 */
130         12, /* fa11 */
131         13, /* fa12 */
132 };
133
134 const int abi_registers_float_saved[] = {
135         14, /* fs0  */
136         15, /* fs1  */
137         24, /* fs2  */
138         25, /* fs3  */
139         26, /* fs4  */
140         27, /* fs5  */
141         28, /* fs6  */
142         29, /* fs7  */
143         30, /* fs8  */
144         31, /* fs9  */
145 };
146
147 const int abi_registers_float_temporary[] = {
148         17, /* ft5  */
149         18, /* ft6  */
150         19, /* ft7  */
151         20, /* ft8  */
152         21, /* ft9  */
153         22, /* ft10 */
154         23, /* ft11 */
155 };
156
157
158 /* md_param_alloc **************************************************************
159
160    Allocate Arguments to Stackslots according the Calling Conventions
161
162    --- in
163    md->paramcount:           Number of arguments for this method
164    md->paramtypes[].type:    Argument types
165    
166    --- out
167    md->params[].inmemory:    Argument spilled on stack
168    md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
169    md->memuse:               Stackslots needed for argument spilling
170    md->argintreguse:         max number of integer arguments used
171    md->argfltreguse:         max number of float arguments used
172
173 *******************************************************************************/
174
175 void md_param_alloc(methoddesc *md)
176 {
177         paramdesc *pd;
178         int        i;
179         int        iarg;
180         int        farg;
181         int        stacksize;
182
183         /* set default values */
184
185         iarg      = 0;
186         farg      = 0;
187         stacksize = LA_SIZE_IN_POINTERS;
188
189         /* get params field of methoddesc */
190
191         pd = md->params;
192
193         for (i = 0; i < md->paramcount; i++, pd++) {
194                 switch (md->paramtypes[i].type) {
195                 case TYPE_INT:
196                 case TYPE_ADR:
197                         if (iarg < INT_ARG_CNT) {
198                                 pd->inmemory = false;
199                                 pd->index    = iarg;
200                                 pd->regoff   = abi_registers_integer_argument[iarg];
201                                 iarg++;
202                         }
203                         else {
204                                 pd->inmemory = true;
205                                 pd->index    = stacksize;
206                                 pd->regoff   = stacksize * 8;
207                                 stacksize++;
208                         }
209                         break;
210
211                 case TYPE_LNG:
212                         if (iarg < INT_ARG_CNT - 1) {
213                                 pd->inmemory = false;
214                                 pd->index    = PACK_REGS(iarg + 1, iarg);
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                                 pd->inmemory = true;
222                                 pd->index    = stacksize;
223                                 pd->regoff   = stacksize * 8;
224                                 iarg         = INT_ARG_CNT;
225                                 stacksize++;
226                         }
227                         break;
228
229                 case TYPE_FLT:
230                 case TYPE_DBL:
231                         if (farg < FLT_ARG_CNT) {
232                                 pd->inmemory = false;
233                                 pd->index    = farg;
234                                 pd->regoff   = abi_registers_float_argument[farg];
235                                 farg++;
236                         }
237                         else {
238                                 pd->inmemory = true;
239                                 pd->index    = stacksize;
240                                 pd->regoff   = stacksize * 8;
241                                 stacksize++;
242                         }
243                         break;
244                 }
245         }
246
247
248         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
249         /* argument register usage has to be regarded, too                        */
250
251         if (IS_INT_LNG_TYPE(md->returntype.type)) {
252                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
253                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
254         }
255         else {
256                 if (IS_FLT_DBL_TYPE(md->returntype.type))
257                         if (farg < 1)
258                                 farg = 1;
259         }
260
261         /* fill register and stack usage */
262
263         md->argintreguse = iarg;
264         md->argfltreguse = farg;
265         md->memuse       = stacksize;
266 }
267
268
269 /* md_param_alloc_native *******************************************************
270
271    Pre-allocate arguments according the native ABI.
272
273 *******************************************************************************/
274
275 void md_param_alloc_native(methoddesc *md)
276 {
277         paramdesc *pd;
278         int        i;
279         int        iarg;
280         int        farg;
281         int        stacksize;
282
283         /* set default values */
284
285         iarg      = 0;
286         farg      = 0;
287         stacksize = LA_SIZE_IN_POINTERS;
288
289         /* get params field of methoddesc */
290
291         pd = md->params;
292
293         for (i = 0; i < md->paramcount; i++, pd++) {
294                 switch (md->paramtypes[i].type) {
295                 case TYPE_INT:
296                 case TYPE_ADR:
297                         if (iarg < INT_ARG_CNT) {
298                                 pd->inmemory = false;
299                                 pd->regoff   = abi_registers_integer_argument[iarg];
300                                 iarg++;
301                         }
302                         else {
303                                 pd->inmemory = true;
304                                 pd->regoff   = stacksize * 4;
305                         }
306                         stacksize++;
307                         break;
308
309                 case TYPE_LNG:
310                         if (iarg < INT_ARG_CNT - 1) {
311                                 pd->inmemory = false;
312                                 pd->regoff   =
313                                         PACK_REGS(abi_registers_integer_argument[iarg + 1],
314                                                           abi_registers_integer_argument[iarg]);
315                                 iarg += 2;
316                         }
317                         else {
318                                 pd->inmemory = true;
319                                 pd->regoff   = stacksize * 4;
320                                 iarg         = INT_ARG_CNT;
321                         }
322                         stacksize += 2;
323                         break;
324
325                 case TYPE_FLT:
326                         if (farg < FLT_ARG_CNT) {
327                                 pd->inmemory = false;
328                                 pd->regoff   = abi_registers_float_argument[farg];
329                                 iarg++;                   /* skip 1 integer argument register */
330                                 farg++;
331                         }
332                         else {
333                                 pd->inmemory = true;
334                                 pd->regoff   = stacksize * 4;
335                         }
336                         stacksize++;
337                         break;
338
339                 case TYPE_DBL:
340                         if (farg < FLT_ARG_CNT) {
341                                 pd->inmemory = false;
342                                 pd->regoff   = abi_registers_float_argument[farg];
343                                 iarg += 2;               /* skip 2 integer argument registers */
344                                 farg++;
345                         }
346                         else {
347                                 pd->inmemory = true;
348                                 pd->regoff   = stacksize * 4;
349                         }
350                         stacksize += 2;
351                         break;
352                 }
353         }
354
355
356         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
357         /* argument register usage has to be regarded, too                        */
358
359         if (IS_INT_LNG_TYPE(md->returntype.type)) {
360                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
361                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
362         }
363         else {
364                 if (IS_FLT_DBL_TYPE(md->returntype.type))
365                         if (farg < 1)
366                                 farg = 1;
367         }
368
369         /* fill register and stack usage */
370
371         md->argintreguse = iarg;
372         md->argfltreguse = farg;
373         md->memuse       = stacksize;
374 }
375
376
377 /* md_return_alloc *************************************************************
378
379    Precolor the Java Stackelement containing the Return Value, if
380    possible. (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
381    for float/double)
382
383    --- in
384    jd:                      jitdata of the current method
385    stackslot:               Java Stackslot to contain the Return Value
386    
387    --- out
388    if precoloring was possible:
389    VAR(stackslot->varnum)->flags  = PREALLOC
390                                      ->regoff = [REG_RESULT|REG_FRESULT]
391    rd->arg[flt|int]reguse   set to a value according the register usage
392
393    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
394
395 *******************************************************************************/
396
397 void md_return_alloc(jitdata *jd, stackptr stackslot)
398 {
399         methodinfo   *m;
400         registerdata *rd;
401         methoddesc   *md;
402
403         /* get required compiler data */
404
405         m  = jd->m;
406         rd = jd->rd;
407
408         md = m->parseddesc;
409
410         /* In Leafmethods Local Vars holding parameters are precolored to
411            their argument register -> so leafmethods with paramcount > 0
412            could already use R3 == a00! */
413
414         if (!jd->isleafmethod || (md->paramcount == 0)) {
415                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
416                    has not to survive method invokations. */
417
418                 if (!(stackslot->flags & SAVEDVAR)) {
419
420                         VAR(stackslot->varnum)->flags = PREALLOC;
421
422                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
423                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
424                                         if (rd->argintreguse < 1)
425                                                 rd->argintreguse = 1;
426
427                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
428                                 }
429                                 else {
430                                         if (rd->argintreguse < 2)
431                                                 rd->argintreguse = 2;
432
433                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
434                                 }
435                         }
436                         else {
437                                 if (rd->argfltreguse < 1)
438                                         rd->argfltreguse = 1;
439
440                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
441                         }
442                 }
443         }
444 }
445
446
447 /*
448  * These are local overrides for various environment variables in Emacs.
449  * Please do not remove this and leave it at the end of the file, where
450  * Emacs will automagically detect them.
451  * ---------------------------------------------------------------------
452  * Local variables:
453  * mode: c
454  * indent-tabs-mode: t
455  * c-basic-offset: 4
456  * tab-width: 4
457  * End:
458  */