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