md_return_alloc added to precolor the stack slots containing the return
[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 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes: Christian Ullrich
30
31    $Id: md-abi.c 2872 2005-06-29 12:42:19Z christian $
32
33 */
34
35
36 #include "vm/jit/powerpc/types.h"
37 #include "vm/jit/powerpc/darwin/md-abi.h"
38
39 #include "vm/descriptor.h"
40 #include "vm/global.h"
41
42
43 /* md_param_alloc **************************************************************
44
45  Allocate Arguments to Stackslots according the Calling Conventions
46
47 --- in
48 md->paramcount:           Number of arguments for this method
49 md->paramtypes[].type:    Argument types
50
51 --- out
52 md->params[].inmemory:    Argument spilled on stack
53 md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
54 md->memuse:               Stackslots needed for argument spilling
55 md->argintreguse:         max number of integer arguments used
56 md->argfltreguse:         max number of float arguments used
57
58 *******************************************************************************/
59
60 void md_param_alloc(methoddesc *md)
61 {
62         paramdesc *pd;
63         s4         i;
64         s4         iarg;
65         s4         farg;
66         s4         stacksize;
67
68         /* set default values */
69
70         iarg = 0;
71         farg = 0;
72         stacksize = LA_WORD_SIZE;
73
74         /* get params field of methoddesc */
75
76         pd = md->params;
77
78         for (i = 0; i < md->paramcount; i++, pd++) {
79                 switch (md->paramtypes[i].type) {
80                 case TYPE_INT:
81                 case TYPE_ADR:
82                         if (iarg < INT_ARG_CNT) {
83                                 pd->inmemory = false;
84                                 pd->regoff = iarg;           /* rd->arg[int|flt]regs index !! */
85                                 iarg++;
86                         } else {
87                                 pd->inmemory = true;
88                                 pd->regoff = stacksize;
89                         }
90                         stacksize++;
91                         break;
92                 case TYPE_LNG:
93                         if (iarg < INT_ARG_CNT - 1) {
94                                 pd->inmemory = false;
95                                                              /* rd->arg[int|flt]regs index !! */
96                                 pd->regoff = PACK_REGS(iarg + 1, iarg); 
97                                 iarg += 2;
98                         } else {
99                                 pd->inmemory = true;
100                                 pd->regoff = stacksize;
101                                 iarg = INT_ARG_CNT;
102                         }
103                         stacksize += 2;
104                         break;
105                 case TYPE_FLT:
106                         if (farg < FLT_ARG_CNT) {
107                                 pd->inmemory = false;
108                                 pd->regoff = farg;           /* rd->arg[int|flt]regs index !! */
109                                 iarg++;     /* skip 1 integer argument register */
110                                 farg++;
111                         } else {
112                                 pd->inmemory = true;
113                                 pd->regoff = stacksize;
114                         }
115                         stacksize++;
116                         break;
117                 case TYPE_DBL:
118                         if (farg < FLT_ARG_CNT) {
119                                 pd->inmemory = false;
120                                 pd->regoff = farg;           /* rd->arg[int|flt]regs index !! */
121                                 iarg += 2;  /* skip 2 integer argument registers */
122                                 farg++;
123                         } else {
124                                 pd->inmemory = true;
125                                 pd->regoff = stacksize;
126                         }
127                         stacksize += 2;
128                         break;
129                 }
130         }
131
132
133         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
134         /* argument register usage has to be regarded, too                        */
135         if (IS_INT_LNG_TYPE(md->returntype.type)) {
136                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
137                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
138         } else {
139                 if (IS_FLT_DBL_TYPE(md->returntype.type))
140                         if (farg < 1)
141                                 farg = 1;
142         }
143
144         /* fill register and stack usage */
145         md->argintreguse = iarg;
146         md->argfltreguse = farg;
147         md->memuse = stacksize;
148 }
149
150 /* md_return_alloc *************************************************************
151
152  Precolor the Java Stackelement containing the Return Value, if possible.
153  (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00 for float/double)
154
155 --- in
156 m:                       Methodinfo of current method
157 return_type:             Return Type of the Method (TYPE_INT.. TYPE_ADR)
158                          TYPE_VOID is not allowed!
159 stackslot:               Java Stackslot to contain the Return Value
160
161 --- out
162 if precoloring was possible:
163 stackslot->varkind       =ARGVAR
164          ->varnum        =-1
165                  ->flags         =0
166                  ->regoff        =[REG_RESULT, (REG_RESULT2/REG_RESULT), REG_FRESULT]
167 rd->arg[flt|int]reguse   set to a value according the register usage
168                                  
169
170 *******************************************************************************/
171 void md_return_alloc(methodinfo *m, registerdata *rd, s4 return_type,
172                                          stackptr stackslot) {
173         /* In Leafmethods Local Vars holding parameters are precolored to their   */
174         /* argument register -> so leafmethods with paramcount > 0 could already  */
175         /* use  R3 == a00! */
176         if (!m->isleafmethod || (m->paramcount == 0)) {
177                 /* Only precolor the stackslot, if it is not a SAVEDVAR <-> has not   */
178                 /* to survive method invokations */
179                 if (!(stackslot->flags & SAVEDVAR)) {
180                         stackslot->varkind = ARGVAR;
181                         stackslot->varnum = -1;
182                         stackslot->flags = 0;
183                         if ( IS_INT_LNG_TYPE(return_type) ) {
184                                 if (IS_2_WORD_TYPE(return_type)) {
185                                         if (rd->argintreguse < 1) rd->argintreguse = 1;
186                                         stackslot->regoff = REG_RESULT;
187                                 } else {
188                                         if (rd->argintreguse < 2) rd->argintreguse = 2;
189                                         stackslot->regoff = PACK_REGS(REG_RESULT2, REG_RESULT);
190                                 }
191                         } else { /* float/double */
192                                 if (rd->argfltreguse < 1) rd->argfltreguse = 1;
193                                 stackslot->regoff = REG_FRESULT;
194                         }
195                 }
196         }
197 }
198
199 /*
200  * These are local overrides for various environment variables in Emacs.
201  * Please do not remove this and leave it at the end of the file, where
202  * Emacs will automagically detect them.
203  * ---------------------------------------------------------------------
204  * Local variables:
205  * mode: c
206  * indent-tabs-mode: t
207  * c-basic-offset: 4
208  * tab-width: 4
209  * End:
210  */