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