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