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