* src/vm/jit/mips/emit.c (emit_copy): Fixed arguments to
[cacao.git] / src / vm / jit / powerpc64 / emit.c
1 /* src/vm/jit/powerpc64/emit.c - PowerPC code emitter functions
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: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39
40 #include "vm/types.h"
41
42 #include "md-abi.h"
43 #include "vm/jit/powerpc64/codegen.h"
44
45 #include "vm/builtin.h"
46 #include "vm/jit/emit-common.h"
47 #include "vm/jit/jit.h"
48
49
50 /* emit_load *******************************************************************
51
52    Emits a possible load of an operand.
53
54 *******************************************************************************/
55
56 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
57 {
58         codegendata  *cd;
59         s4            disp;
60         s4            reg;
61
62         /* get required compiler data */
63
64         cd = jd->cd;
65
66         if (src->flags & INMEMORY) {
67                 COUNT_SPILLS;
68
69                 disp = src->vv.regoff * 8;
70
71                 if (IS_FLT_DBL_TYPE(src->type)) {
72                         if (IS_2_WORD_TYPE(src->type))
73                                 M_DLD(tempreg, REG_SP, disp);
74                         else
75                                 M_FLD(tempreg, REG_SP, disp);
76                 }
77                 else {
78                 /*      if (IS_2_WORD_TYPE(src->type))
79                                 M_LLD(tempreg, REG_SP, disp);
80                         else
81                                 M_ILD(tempreg, REG_SP, disp);
82                 */
83                         M_LLD(tempreg, REG_SP, disp);
84                 }
85
86                 reg = tempreg;
87         }
88         else
89                 reg = src->vv.regoff;
90
91         return reg;
92 }
93
94
95 /* emit_store ******************************************************************
96
97    Emits a possible store to a variable.
98
99 *******************************************************************************/
100
101 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
102 {
103         codegendata  *cd;
104
105         /* get required compiler data */
106
107         cd = jd->cd;
108
109         if (dst->flags & INMEMORY) {
110                 COUNT_SPILLS;
111
112                 if (IS_FLT_DBL_TYPE(dst->type)) {
113                         if (IS_2_WORD_TYPE(dst->type))
114                                 M_DST(d, REG_SP, dst->vv.regoff * 8);
115                         else
116                                 M_FST(d, REG_SP, dst->vv.regoff * 8);
117                 }
118                 else {
119                         M_LST(d, REG_SP, dst->vv.regoff * 8);
120                 }
121         }
122 }
123
124
125 /* emit_copy *******************************************************************
126
127    Generates a register/memory to register/memory copy.
128
129 *******************************************************************************/
130
131 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
132 {
133         codegendata  *cd;
134         registerdata *rd;
135         s4            s1, d;
136
137         /* get required compiler data */
138
139         cd = jd->cd;
140         rd = jd->rd;
141
142         if ((src->vv.regoff != dst->vv.regoff) ||
143                 ((src->flags ^ dst->flags) & INMEMORY)) {
144
145                 /* If one of the variables resides in memory, we can eliminate
146                    the register move from/to the temporary register with the
147                    order of getting the destination register and the load. */
148
149                 if (IS_INMEMORY(src->flags)) {
150                         d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
151                         s1 = emit_load(jd, iptr, src, d);
152                 }
153                 else {
154                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
155                         d = codegen_reg_of_var(iptr->opc, dst, s1);
156                 }
157
158                 if (s1 != d) {
159                         if (IS_FLT_DBL_TYPE(src->type))
160                                 M_FMOV(s1, d);
161                         else
162                                 M_MOV(s1, d);
163                 }
164
165                 emit_store(jd, iptr, dst, d);
166         }
167 }
168
169
170 /* emit_iconst *****************************************************************
171
172    XXX
173
174 *******************************************************************************/
175
176 void emit_iconst(codegendata *cd, s4 d, s4 value)
177 {
178         s4 disp;
179
180         if ((value >= -32768) && (value <= 32767))
181                 M_LDA_INTERN(d, REG_ZERO, value);
182         else {
183                 disp = dseg_adds4(cd, value);
184                 M_ILD(d, REG_PV, disp);
185         }
186 }
187
188
189 /* emit_verbosecall_enter ******************************************************
190  *
191  *    Generates the code for the call trace.
192  *
193  ********************************************************************************/
194
195 void emit_verbosecall_enter (jitdata *jd)
196 {
197         methodinfo   *m;
198         codegendata  *cd;
199         registerdata *rd;
200         s4 s1, p, t, d;
201         int stack_size;
202         methoddesc *md;
203
204         /* get required compiler data */
205
206         m  = jd->m;
207         cd = jd->cd;
208         rd = jd->rd;
209
210         md = m->parseddesc;
211         
212         /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
213         /* For Darwin:                                                        */
214         /* TODO                                                               */
215         /* For Linux:                                                         */
216         /* setup stack for TRACE_ARGS_NUM registers                           */
217         /* == LA_SIZE + PA_SIZE + 8 (methodinfo argument) + TRACE_ARGS_NUM*8 + 8 (itmp1)              */
218         
219         /* in nativestubs no Place to save the LR (Link Register) would be needed */
220         /* but since the stack frame has to be aligned the 4 Bytes would have to  */
221         /* be padded again */
222
223 #if defined(__DARWIN__)
224         stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
225 #else
226         stack_size = LA_SIZE + PA_SIZE + 8 + TRACE_ARGS_NUM * 8 + 8;
227 #endif
228
229         /* mark trace code */
230         M_NOP;
231
232         M_MFLR(REG_ZERO);
233         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
234         M_STDU(REG_SP, REG_SP, -stack_size);
235
236         for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++) {
237                 t = md->paramtypes[p].type;
238                 if (IS_INT_LNG_TYPE(t)) {
239                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
240                                 M_LST(rd->argintregs[md->params[p].regoff], REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
241                         } else { /* Param on Stack */
242                                 s1 = (md->params[p].regoff + cd->stackframesize) * 8 + stack_size;
243                                 M_LLD(REG_ITMP2, REG_SP, s1);
244                                 M_LST(REG_ITMP2, REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
245                         }
246                 } else { /* IS_FLT_DBL_TYPE(t) */
247                         if (!md->params[p].inmemory) { /* in Arg Reg */
248                                 s1 = rd->argfltregs[md->params[p].regoff];
249                                 M_DST(s1, REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
250                         } else { /* on Stack */
251                                 /* this should not happen */
252                                 assert(0);
253                         }
254                 }
255         }
256
257 #if defined(__DARWIN__)
258         #warning "emit_verbosecall_enter not implemented"
259 #else
260         /* LINUX */
261         /* Set integer and float argument registers for trace_args call */
262         /* offset to saved integer argument registers                   */
263         for (p = 0; (p < TRACE_ARGS_NUM) && (p < md->paramcount); p++) {
264                 t = md->paramtypes[p].type;
265                 if (IS_INT_LNG_TYPE(t)) {
266                         M_LLD(rd->argintregs[p], REG_SP,LA_SIZE + PA_SIZE + 8 + p * 8);
267                 } else { /* Float/Dbl */
268                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
269                                 /* use reserved Place on Stack (sp + 5 * 16) to copy  */
270                                 /* float/double arg reg to int reg                    */
271                                 s1 = rd->argfltregs[md->params[p].regoff];
272                                 M_MOV(s1, rd->argintregs[p]);
273                         } else  {
274                                 assert(0);
275                         }
276                 }
277         }
278 #endif
279
280         /* put methodinfo pointer on Stackframe */
281         p = dseg_addaddress(cd, m);
282         M_ALD(REG_ITMP1, REG_PV, p);
283 #if defined(__DARWIN__)
284         M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8); 
285 #else
286         if (TRACE_ARGS_NUM == 8)        {
287                 /* need to pass via stack */
288                 M_AST(REG_ITMP1, REG_SP, LA_SIZE + PA_SIZE);
289         } else {
290                 /* pass via register, reg 3 is the first  */
291                 M_MOV(REG_ITMP1, 3 + TRACE_ARGS_NUM);
292         }
293 #endif
294         /* call via function descriptor */
295         /* XXX: what about TOC? */
296         p = dseg_addaddress(cd, builtin_trace_args);
297         M_ALD(REG_ITMP2, REG_PV, p);
298         M_ALD(REG_ITMP1, REG_ITMP2, 0);
299         M_MTCTR(REG_ITMP1);
300         M_JSR;
301
302 #if defined(__DARWIN__)
303         #warning "emit_verbosecall_enter not implemented"
304 #else
305         /* LINUX */
306         for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++) {
307                 t = md->paramtypes[p].type;
308                 if (IS_INT_LNG_TYPE(t)) {
309                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
310                                 /* restore integer argument registers */
311                                 M_LLD(rd->argintregs[p], REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
312                         } else {
313                                 assert(0);      /* TODO: implement this */
314                         }
315                 } else { /* FLT/DBL */
316                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
317                                 M_DLD(rd->argfltregs[md->params[p].regoff], REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
318                         } else {
319                                 assert(0); /* this shoudl never happen */
320                         }
321                         
322                 }
323         }
324 #endif
325         M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
326         M_MTLR(REG_ZERO);
327         M_LDA(REG_SP, REG_SP, stack_size);
328
329         /* mark trace code */
330         M_NOP;
331 }
332
333
334 /* emit_verbosecall_exit ******************************************************
335  *
336  *    Generates the code for the call trace.
337  *
338  ********************************************************************************/
339
340 void emit_verbosecall_exit(jitdata *jd)
341 {
342         codegendata *cd = jd->cd;
343         s4 disp;
344
345         /* mark trace code */
346         M_NOP;
347
348         M_MFLR(REG_ZERO);
349         M_LDA(REG_SP, REG_SP, -(LA_SIZE+PA_SIZE+10*8));
350         M_DST(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
351         M_LST(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+1*8);
352         M_AST(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+2*8);
353
354 #if defined(__DARWIN__)
355         M_MOV(REG_RESULT, jd->rd->argintregs[1]);
356 #else
357         M_MOV(REG_RESULT, jd->rd->argintregs[1]);
358 #endif
359
360         disp = dseg_addaddress(cd, jd->m);
361         M_ALD(jd->rd->argintregs[0], REG_PV, disp);
362
363         M_FLTMOVE(REG_FRESULT, jd->rd->argfltregs[0]);
364         M_FLTMOVE(REG_FRESULT, jd->rd->argfltregs[1]);
365         disp = dseg_addaddress(cd, builtin_displaymethodstop);
366         /* call via function descriptor, XXX: what about TOC ? */
367         M_ALD(REG_ITMP2, REG_PV, disp);
368         M_ALD(REG_ITMP2, REG_ITMP2, 0);
369         M_MTCTR(REG_ITMP2);
370         M_JSR;
371
372         M_DLD(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
373         M_LLD(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+1*8);
374         M_ALD(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+2*8);
375         M_LDA(REG_SP, REG_SP, LA_SIZE+PA_SIZE+10*8);
376         M_MTLR(REG_ZERO);
377
378         /* mark trace code */
379         M_NOP;
380 }
381
382
383
384 /*
385  * These are local overrides for various environment variables in Emacs.
386  * Please do not remove this and leave it at the end of the file, where
387  * Emacs will automagically detect them.
388  * ---------------------------------------------------------------------
389  * Local variables:
390  * mode: c
391  * indent-tabs-mode: t
392  * c-basic-offset: 4
393  * tab-width: 4
394  * End:
395  * vim:noexpandtab:sw=4:ts=4:
396  */