Merged 7484:52fc709fd248.
[cacao.git] / src / vm / jit / s390 / md-abi.c
1 /* src/vm/jit/x86_64/md-abi.c - functions for x86_64 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: Christian Thalinger
28
29    Changes:
30
31 */
32
33 #include "config.h"
34
35 #include "vm/global.h"
36 #include "vm/jit/jit.h"
37 #include "vm/jit/s390/md-abi.h"
38 #include "vm/types.h"
39
40 #include "vmcore/descriptor.h"
41
42 #include <assert.h>
43
44 /* register descripton array **************************************************/
45
46 s4 nregdescint[] = {
47         /*   r0,   itmp1,      a0,      a1,      a2,      a3,      a4,      s0, */
48         REG_TMP, REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_SAV,
49         /*   s1,      s2,      s3,      s4,   itmp2,    pv,  ra/itmp3,      sp */
50         REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_RES, REG_RES, REG_RES, REG_RES,
51     REG_END
52 };
53
54 const char *abi_registers_integer_name[] = {
55         "r0", "r1", "r2", "r3",
56         "r4", "r5", "r6", "r7",
57         "r8", "r9", "r10", "r11",
58         "r12", "r13", "r14", "r15"
59 };
60
61 const s4 abi_registers_integer_argument[] = {
62         2, /* r2/a0 */
63         3, /* r3/a1 */
64         4, /* r4/a2 */
65         5, /* r5/a3 */
66         6  /* r6/a4 */
67 };
68
69 const s4 abi_registers_integer_saved[] = {
70         7,  /* r7/s0 */
71         8,  /* r8/s1 */
72         9,  /* r9/s2 */
73         10, /* r10/s3 */
74         11  /* r11/s4 */
75 };
76
77 const s4 abi_registers_integer_temporary[] = {
78         0, /* r0 */
79 };
80
81 s4 nregdescfloat[] = {
82         REG_ARG, REG_TMP, REG_ARG, REG_TMP, REG_RES, REG_TMP, REG_RES, REG_TMP,
83         REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
84     REG_END
85 };
86
87 const s4 abi_registers_float_argument[] = {
88         0, /* f0/fa0 */
89         2  /* f2/fa2 */
90 };
91
92 const s4 abi_registers_float_saved[] = {
93         -1 /* none */
94 };
95
96 const s4 abi_registers_float_temporary[] = {
97         1,  /* f1/ft0 */
98         3,  /* f3/ft1 */
99         5,  /* f5/ft2 */
100         7,  /* f7/ft3 */
101         8,  /* f8/ft4 */
102         9,  /* f9/ft5 */
103         10, /* f10/ft6 */
104         11, /* f11/ft7 */
105         12, /* f12/ft8 */
106         13, /* f13/ft9 */
107         14, /* f14/ft10 */
108         15  /* f15/ft11 */
109 };
110
111 /* md_param_alloc_intern *******************************************************
112
113    Allocates parameters to registers or stackslots for both native and java
114    methods.
115
116    --- in:
117    slot: size in bytes of a stack slot
118    slots1w: number of stack slots used by a 1 word type parameter
119    slots2w: number of stack slots used by a 2 word type parameter
120
121 *******************************************************************************/
122
123 static void md_param_alloc_intern(methoddesc *md, s4 slot, s4 slots1w, s4 slots2w)
124 {
125         paramdesc *pd;
126         s4         i;
127         s4         iarg;
128         s4         farg;
129         s4         stacksize;
130
131         /* set default values */
132
133         iarg = 0;
134         farg = 0;
135         stacksize = 0;
136
137         /* get params field of methoddesc */
138
139         pd = md->params;
140
141         for (i = 0; i < md->paramcount; i++, pd++) {
142                 switch (md->paramtypes[i].type) {
143                 case TYPE_INT:
144                 case TYPE_ADR:
145                         if (iarg < INT_ARG_CNT) {
146                                 pd->inmemory  = false;
147                                 pd->regoff    = abi_registers_integer_argument[iarg]; 
148                                 pd->index     = iarg;
149                                 iarg++;
150                         }
151                         else {
152                                 pd->inmemory  = true;
153                                 pd->regoff    = stacksize * slot;
154                                 pd->index     = stacksize;
155                                 stacksize += slots1w;
156                         }
157                         break;
158
159                 case TYPE_LNG:
160                         if (iarg < INT_ARG_CNT - 1) {
161                                 /* _ALIGN(iarg); */
162                                 pd->inmemory  = false;
163                                 pd->regoff    = 
164                                         PACK_REGS(abi_registers_integer_argument[iarg + 1], 
165                                                           abi_registers_integer_argument[iarg]); 
166                                 pd->index     = PACK_REGS(iarg + 1, iarg);
167                                 iarg += 2;
168                         }
169                         else {
170                                 /* _ALIGN(stacksize); */
171                                 pd->inmemory  = true;
172                                 pd->regoff    = stacksize * slot;
173                                 pd->index     = stacksize;
174                                 iarg          = INT_ARG_CNT;
175                                 stacksize    += slots2w;
176                         }
177                         break;
178
179                 case TYPE_FLT:
180                         if (farg < FLT_ARG_CNT) {
181                                 pd->inmemory  = false;
182                                 pd->regoff    = abi_registers_float_argument[farg]; 
183                                 pd->index     = farg;
184                                 farg++;
185                         }
186                         else {
187                                 pd->inmemory  = true;
188                                 pd->regoff    = stacksize * slot;
189                                 pd->index     = stacksize;
190                                 stacksize += slots1w;
191                         }
192                         break;
193
194                 case TYPE_DBL:
195                         if (farg < FLT_ARG_CNT) {
196                                 pd->inmemory  = false;
197                                 pd->regoff    = abi_registers_float_argument[farg]; 
198                                 pd->index     = farg;
199                                 farg++;
200                         }
201                         else {
202                                 /* _ALIGN(stacksize); */
203                                 pd->inmemory  = true;
204                                 pd->regoff    = stacksize * slot;
205                                 pd->index     = stacksize;
206                                 stacksize    += slots2w;
207                         }
208                         break;
209
210                 default:
211                         assert(0);
212                 }
213         }
214
215         /* Since A0+A1/FA0 are used for passing return
216            values, this argument register usage has to be regarded,
217            too. */
218
219         if (IS_INT_LNG_TYPE(md->returntype.type)) {
220                 if (iarg < (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1))
221                         iarg = IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1;
222         }
223         else {
224                 if (IS_FLT_DBL_TYPE(md->returntype.type))
225                         if (farg < 1)
226                                 farg = 1;
227         }
228
229         /* fill register and stack usage */
230
231         md->argintreguse = iarg;
232         md->argfltreguse = farg;
233         md->memuse = stacksize;
234 }
235
236 void md_param_alloc(methoddesc *md)
237 {
238         md_param_alloc_intern(md, 8, 1, 1);
239 }
240
241 void md_param_alloc_native(methoddesc *md)
242 {
243         md_param_alloc_intern(md, 4, 1, 2);
244 }
245
246
247 /* md_return_alloc *************************************************************
248
249    Precolor the Java Stackelement containing the Return Value. Only
250    for float/ double types straight forward possible, since INT_LNG
251    types use "reserved" registers Float/Double values use a00 as
252    return register.
253
254    --- in
255    jd:                      jitdata of the current method
256    stackslot:               Java Stackslot to contain the Return Value
257
258    --- out
259    if precoloring was possible:
260    VAR(stackslot->varnum)->flags     = PREALLOC
261                                      ->vv.regoff = [REG_RESULT|REG_FRESULT]
262    rd->arg[flt|int]reguse   set to a value according the register usage
263
264    NOTE: Do not pass a LOCALVAR in stackslot->varnum.
265
266 *******************************************************************************/
267
268 void md_return_alloc(jitdata *jd, stackptr stackslot)
269 {
270         methodinfo   *m;
271         registerdata *rd;
272         methoddesc   *md;
273
274         /* get required compiler data */
275
276         m  = jd->m;
277         rd = jd->rd;
278
279         md = m->parseddesc;
280
281         /* In Leafmethods Local Vars holding parameters are precolored to
282            their argument register -> so leafmethods with paramcount > 0
283            could already use R3 == a00! */
284
285         if (!jd->isleafmethod || (md->paramcount == 0)) {
286                 /* Only precolor the stackslot, if it is not a SAVEDVAR <->
287                    has not to survive method invokations. */
288
289                 if (!(stackslot->flags & SAVEDVAR)) {
290                         VAR(stackslot->varnum)->flags = PREALLOC;
291
292                         if (IS_INT_LNG_TYPE(md->returntype.type)) {
293                                 if (!IS_2_WORD_TYPE(md->returntype.type)) {
294                                         if (rd->argintreguse < 1)
295                                                 rd->argintreguse = 1;
296
297                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT;
298                                 }
299                                 else {
300                                         if (rd->argintreguse < 2)
301                                                 rd->argintreguse = 2;
302
303                                         VAR(stackslot->varnum)->vv.regoff = REG_RESULT_PACKED;
304                                 }
305                         }
306                         else { /* float/double */
307                                 if (rd->argfltreguse < 1)
308                                         rd->argfltreguse = 1;
309
310                                 VAR(stackslot->varnum)->vv.regoff = REG_FRESULT;
311                         }
312                 }
313         }
314 }
315
316
317 /*
318  * These are local overrides for various environment variables in Emacs.
319  * Please do not remove this and leave it at the end of the file, where
320  * Emacs will automagically detect them.
321  * ---------------------------------------------------------------------
322  * Local variables:
323  * mode: c
324  * indent-tabs-mode: t
325  * c-basic-offset: 4
326  * tab-width: 4
327  * End:
328  */