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