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