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