* configure.ac (AC_CHECK_HEADERS): Added sys/socket.h.
[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, 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 8123 2007-06-20 23:50:55Z michi $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "vm/jit/powerpc/darwin/md-abi.h"
34
35 #include "vm/global.h"
36
37 #include "vm/jit/abi.h"
38
39 #include "vmcore/descriptor.h"
40
41
42 /* register descripton arrays *************************************************/
43
44 s4 nregdescint[] = {
45         /* zero,      sp,      t0,   a0/v0,   a0/v1,      a2,      a3,      a4,   */
46         REG_RES, REG_RES, REG_TMP, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
47
48         /*   a5,      a6,      a7,   itmp1,   itmp2,      pv,      s0,      s1,   */
49         REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, REG_SAV,
50
51         /*itmp3,      t1,      t2,      t3,      t4,      t5,      t6,      t7,   */
52         REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
53
54         /*   s2,      s3,      s4,      s5,      s6,      s7,      s8,      s9,   */
55         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
56
57         REG_END
58 };
59
60 const char *abi_registers_integer_name[] = {
61         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
62         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
63         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
64         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
65 };
66
67 const s4 abi_registers_integer_argument[] = {
68         3,  /* a0 */
69         4,  /* a1 */
70         5,  /* a2 */
71         6,  /* a3 */
72         7,  /* a4 */
73         8,  /* a5 */
74         9,  /* a6 */
75         10, /* a7 */
76 };
77
78 const s4 abi_registers_integer_saved[] = {
79         14, /* s0 */
80         15, /* s1 */
81         24, /* s2 */
82         25, /* s3 */
83         26, /* s4 */
84         27, /* s5 */
85         28, /* s6 */
86         29, /* s7 */
87         30, /* s8 */
88         31, /* s9 */
89 };
90
91 const s4 abi_registers_integer_temporary[] = {
92         2,  /* t0 */
93         17, /* t1 */
94         18, /* t2 */
95         19, /* t3 */
96         20, /* t4 */
97         21, /* t5 */
98         22, /* t6 */
99         23, /* t7 */
100 };
101
102
103 s4 nregdescfloat[] = {
104         /*ftmp3,  fa0/v0,     fa1,     fa2,     fa3,     fa4,     fa5,     fa6,   */
105         REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
106
107         /*  fa7,     fa8,     fa9,    fa10,    fa11,    fa12,     fs0,     fs1,   */
108         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_SAV, REG_SAV,
109
110         /*ftmp1,   ftmp2,     ft0,     ft1,     ft2,     ft3,     ft4,     ft5,   */
111         REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
112
113         /*  fs2,     fs3,     fs4,     fs5,     fs6,     fs7,     fs8,     fs9    */
114         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
115
116         REG_END
117 };
118
119 const s4 abi_registers_float_argument[] = {
120         1,  /* fa0  */
121         2,  /* fa1  */
122         3,  /* fa2  */
123         4,  /* fa3  */
124         5,  /* fa4  */
125         6,  /* fa5  */
126         7,  /* fa6  */
127         8,  /* fa7  */
128         9,  /* fa8  */
129         10, /* fa9  */
130         11, /* fa10 */
131         12, /* fa11 */
132         13, /* fa12 */
133 };
134
135 const s4 abi_registers_float_saved[] = {
136         14, /* fs0  */
137         15, /* fs1  */
138         24, /* fs2  */
139         25, /* fs3  */
140         26, /* fs4  */
141         27, /* fs5  */
142         28, /* fs6  */
143         29, /* fs7  */
144         30, /* fs8  */
145         31, /* fs9  */
146 };
147
148 const s4 abi_registers_float_temporary[] = {
149         17, /* ft5  */
150         18, /* ft6  */
151         19, /* ft7  */
152         20, /* ft8  */
153         21, /* ft9  */
154         22, /* ft10 */
155         23, /* ft11 */
156 };
157
158
159 /* md_param_alloc **************************************************************
160
161    Allocate Arguments to Stackslots according the Calling Conventions
162
163    --- in
164    md->paramcount:           Number of arguments for this method
165    md->paramtypes[].type:    Argument types
166    
167    --- out
168    md->params[].inmemory:    Argument spilled on stack
169    md->params[].regoff:      Stack offset or rd->arg[int|flt]regs index
170    md->memuse:               Stackslots needed for argument spilling
171    md->argintreguse:         max number of integer arguments used
172    md->argfltreguse:         max number of float arguments used
173
174 *******************************************************************************/
175
176 void md_param_alloc(methoddesc *md)
177 {
178         paramdesc *pd;
179         s4         i;
180         s4         iarg;
181         s4         farg;
182         s4         stacksize;
183
184         /* set default values */
185
186         iarg      = 0;
187         farg      = 0;
188         stacksize = LA_SIZE_IN_POINTERS;
189
190         /* get params field of methoddesc */
191
192         pd = md->params;
193
194         for (i = 0; i < md->paramcount; i++, pd++) {
195                 switch (md->paramtypes[i].type) {
196                 case TYPE_INT:
197                 case TYPE_ADR:
198                         if (iarg < INT_ARG_CNT) {
199                                 pd->inmemory = false;
200                                 pd->regoff   = abi_registers_integer_argument[iarg];
201                                 iarg++;
202                         }
203                         else {
204                                 pd->inmemory = true;
205                                 pd->regoff   = stacksize * 4;
206                         }
207                         stacksize++;
208                         break;
209
210                 case TYPE_LNG:
211                         if (iarg < INT_ARG_CNT - 1) {
212                                 pd->inmemory = false;
213                                 pd->regoff   =
214                                         PACK_REGS(abi_registers_integer_argument[iarg + 1],
215                                                           abi_registers_integer_argument[iarg]);
216                                 iarg += 2;
217                         }
218                         else {
219                                 pd->inmemory = true;
220                                 pd->regoff   = stacksize * 4;
221                                 iarg         = INT_ARG_CNT;
222                         }
223                         stacksize += 2;
224                         break;
225
226                 case TYPE_FLT:
227                         if (farg < FLT_ARG_CNT) {
228                                 pd->inmemory = false;
229                                 pd->regoff   = abi_registers_float_argument[farg];
230                                 iarg++;                   /* skip 1 integer argument register */
231                                 farg++;
232                         }
233                         else {
234                                 pd->inmemory = true;
235                                 pd->regoff   = stacksize * 4;
236                         }
237                         stacksize++;
238                         break;
239
240                 case TYPE_DBL:
241                         if (farg < FLT_ARG_CNT) {
242                                 pd->inmemory = false;
243                                 pd->regoff   = abi_registers_float_argument[farg];
244                                 iarg += 2;               /* skip 2 integer argument registers */
245                                 farg++;
246                         }
247                         else {
248                                 pd->inmemory = true;
249                                 pd->regoff   = stacksize * 4;
250                         }
251                         stacksize += 2;
252                         break;
253                 }
254         }
255
256
257         /* Since R3/R4, F1 (==A0/A1, A0) are used for passing return values, this */
258         /* argument register usage has to be regarded, too                        */
259
260         if (IS_INT_LNG_TYPE(md->returntype.type)) {
261                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
262                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
263         }
264         else {
265                 if (IS_FLT_DBL_TYPE(md->returntype.type))
266                         if (farg < 1)
267                                 farg = 1;
268         }
269
270         /* fill register and stack usage */
271
272         md->argintreguse = iarg;
273         md->argfltreguse = farg;
274         md->memuse       = stacksize;
275 }
276
277
278 /* md_param_alloc_native *******************************************************
279
280    Pre-allocate arguments according the native ABI.
281
282 *******************************************************************************/
283
284 void md_param_alloc_native(methoddesc *md)
285 {
286         /* On PowerPC we use the same ABI for JIT method calls as for
287            native method calls. */
288
289         md_param_alloc(md);
290 }
291
292
293 /* md_return_alloc *************************************************************
294
295    Precolor the Java Stackelement containing the Return Value, if
296    possible. (R3==a00 for int/adr, R4/R3 == a01/a00 for long, F1==a00
297    for float/double)
298
299    --- in
300    jd:                      jitdata of the current method
301    stackslot:               Java Stackslot to contain the Return Value
302    
303    --- out
304    if precoloring was possible:
305    VAR(stackslot->varnum)->flags  = PREALLOC
306                                      ->regoff = [REG_RESULT|REG_FRESULT]
307    rd->arg[flt|int]reguse   set to a value according the register usage
308
309    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
310
311 *******************************************************************************/
312
313 void md_return_alloc(jitdata *jd, stackptr stackslot)
314 {
315         methodinfo   *m;
316         registerdata *rd;
317         methoddesc   *md;
318
319         /* get required compiler data */
320
321         m  = jd->m;
322         rd = jd->rd;
323
324         md = m->parseddesc;
325
326         /* In Leafmethods Local Vars holding parameters are precolored to
327            their argument register -> so leafmethods with paramcount > 0
328            could already use R3 == a00! */
329
330         if (!jd->isleafmethod || (md->paramcount == 0)) {
331                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
332                    has not to survive method invokations. */
333
334                 if (!(stackslot->flags & SAVEDVAR)) {
335
336                         VAR(stackslot->varnum)->flags = PREALLOC;
337
338                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
339                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
340                                         if (rd->argintreguse < 1)
341                                                 rd->argintreguse = 1;
342
343                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
344                                 }
345                                 else {
346                                         if (rd->argintreguse < 2)
347                                                 rd->argintreguse = 2;
348
349                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
350                                 }
351                         }
352                         else {
353                                 if (rd->argfltreguse < 1)
354                                         rd->argfltreguse = 1;
355
356                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
357                         }
358                 }
359         }
360 }
361
362
363 /*
364  * These are local overrides for various environment variables in Emacs.
365  * Please do not remove this and leave it at the end of the file, where
366  * Emacs will automagically detect them.
367  * ---------------------------------------------------------------------
368  * Local variables:
369  * mode: c
370  * indent-tabs-mode: t
371  * c-basic-offset: 4
372  * tab-width: 4
373  * End:
374  */