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