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