* This is a rather huge commit, which changes the build order of
[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 7246 2007-01-29 18:49:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "vm/jit/powerpc/linux/md-abi.h"
34
35 #include "vm/global.h"
36
37 #include "vm/jit/abi.h"
38
39 #include "vmcore/descriptor.h"
40
41
42 #define _ALIGN(a)    do { if ((a) & 1) (a)++; } while (0)
43
44
45 /* register descripton array **************************************************/
46
47 s4 nregdescint[] = {
48         /* zero,      sp, NO(sys),   a0/v0,   a0/v1,      a2,      a3,      a4,   */
49         REG_RES, REG_RES, REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
50
51         /*   a5,      a6,      a7,   itmp1,   itmp2,      pv,      s0,      s1,   */
52         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, REG_SAV,
53
54         /*itmp3,      t0,      t1,      t2,      t3,      t4,      t5,      t6,   */
55         REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
56
57         /*   s2,      s3,      s4,      s5,      s6,      s7,      s8,      s9,   */
58         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
59
60         REG_END
61 };
62
63 char *regs[] = {
64         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
65         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
66         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
67         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
68 };
69
70
71 s4 nregdescfloat[] = {
72         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
73         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
74
75         /*  fa7,     ft0,     ft1,     ft2,     ft3,     ft4,     fs0,     fs1,   */
76         REG_ARG, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_SAV, REG_SAV,
77
78         /*ftmp1,   ftmp2,     ft5,     ft6,     ft7,     ft8,     ft9,    ft10,   */
79         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
80
81         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
82         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
83
84         REG_END
85 };
86
87
88 /* md_param_alloc **************************************************************
89
90    Allocate Arguments to Stackslots according the Calling Conventions
91
92    --- in
93    md->paramcount:           Number of arguments for this method
94    md->paramtypes[].type:    Argument types
95
96    --- out
97    md->params[].inmemory:    Argument spilled on stack
98    md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
99    md->memuse:               Stackslots needed for argument spilling
100    md->argintreguse:         max number of integer arguments used
101    md->argfltreguse:         max number of float arguments used
102
103 *******************************************************************************/
104
105 void md_param_alloc(methoddesc *md)
106 {
107         paramdesc *pd;
108         s4         i;
109         s4         iarg;
110         s4         farg;
111         s4         stacksize;
112
113         /* set default values */
114
115         iarg = 0;
116         farg = 0;
117         stacksize = LA_SIZE_IN_POINTERS;
118
119         /* get params field of methoddesc */
120
121         pd = md->params;
122
123         for (i = 0; i < md->paramcount; i++, pd++) {
124                 switch (md->paramtypes[i].type) {
125                 case TYPE_INT:
126                 case TYPE_ADR:
127                         if (iarg < INT_ARG_CNT) {
128                                 pd->inmemory = false;
129                                 pd->regoff = iarg;
130                                 iarg++;
131                         } else {
132                                 pd->inmemory = true;
133                                 pd->regoff = stacksize;
134                                 stacksize++;
135                         }
136                         break;
137                 case TYPE_LNG:
138                         if (iarg < INT_ARG_CNT - 1) {
139                                 _ALIGN(iarg);
140                                 pd->inmemory = false;
141                                                              /* rd->arg[int|flt]regs index !! */
142                                 pd->regoff = PACK_REGS(iarg + 1, iarg); 
143                                 iarg += 2;
144                         } else {
145                                 _ALIGN(stacksize);
146                                 pd->inmemory = true;
147                                 pd->regoff = stacksize;
148                                 iarg = INT_ARG_CNT;
149                                 stacksize += 2;
150                         }
151                         break;
152                 case TYPE_FLT:
153                         if (farg < FLT_ARG_CNT) {
154                                 pd->inmemory = false;
155                                 pd->regoff = farg;
156                                 farg++;
157                         } else {
158                                 pd->inmemory = true;
159                                 pd->regoff = stacksize;
160                                 stacksize++;
161                         }
162                         break;
163                 case TYPE_DBL:
164                         if (farg < FLT_ARG_CNT) {
165                                 pd->inmemory = false;
166                                 pd->regoff = farg;
167                                 farg++;
168                         } else {
169                                 _ALIGN(stacksize);
170                                 pd->inmemory = true;
171                                 pd->regoff = stacksize;
172                                 stacksize += 2;
173                         }
174                         break;
175                 }
176         }
177
178         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
179         /* argument register usage has to be regarded, too                        */
180         if (IS_INT_LNG_TYPE(md->returntype.type)) {
181                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
182                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
183         } else {
184                 if (IS_FLT_DBL_TYPE(md->returntype.type))
185                         if (farg < 1)
186                                 farg = 1;
187         }
188
189         /* fill register and stack usage */
190
191         md->argintreguse = iarg;
192         md->argfltreguse = farg;
193         md->memuse = stacksize;
194 }
195
196
197 /* md_return_alloc *************************************************************
198
199    Precolor the Java Stackelement containing the Return Value, if
200    possible.  (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
201    for float/double)
202
203    --- in
204    jd:                      jitdata of the current method
205    stackslot:               Java Stackslot to contain the Return Value
206
207    --- out
208    if precoloring was possible:
209    VAR(stackslot->varnum)->flags       = PREALLOC
210                                      ->regoff      = [REG_RESULT|REG_FRESULT]
211    rd->arg[flt|int]reguse   set to a value according the register usage
212
213    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
214
215 *******************************************************************************/
216
217 void md_return_alloc(jitdata *jd, stackptr stackslot)
218 {
219         methodinfo   *m;
220         registerdata *rd;
221         methoddesc   *md;
222
223         /* get required compiler data */
224
225         m  = jd->m;
226         rd = jd->rd;
227
228         md = m->parseddesc;
229
230         /* In Leafmethods Local Vars holding parameters are precolored to
231            their argument register -> so leafmethods with paramcount > 0
232            could already use R3 == a00! */
233
234         if (!jd->isleafmethod || (md->paramcount == 0)) {
235                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
236                    has not to survive method invokations. */
237
238                 if (!(stackslot->flags & SAVEDVAR)) {
239                         VAR(stackslot->varnum)->flags = PREALLOC;
240
241                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
242                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
243                                         if (rd->argintreguse < 1)
244                                                 rd->argintreguse = 1;
245
246                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
247                                 }
248                                 else {
249                                         if (rd->argintreguse < 2)
250                                                 rd->argintreguse = 2;
251
252                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
253                                 }
254                         }
255                         else { /* float/double */
256                                 if (rd->argfltreguse < 1)
257                                         rd->argfltreguse = 1;
258
259                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
260                         }
261                 }
262         }
263 }
264
265
266 /*
267  * These are local overrides for various environment variables in Emacs.
268  * Please do not remove this and leave it at the end of the file, where
269  * Emacs will automagically detect them.
270  * ---------------------------------------------------------------------
271  * Local variables:
272  * mode: c
273  * indent-tabs-mode: t
274  * c-basic-offset: 4
275  * tab-width: 4
276  * End:
277  */