* src/vm/jit/sparc64/codegen.h: Reworked float handling, single precision floats...
[cacao.git] / src / vm / jit / sparc64 / emit.c
1 /* src/vm/jit/sparc64/emit.c - Sparc 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             Alexander Jordan
29
30    Changes: 
31
32    $Id: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
33
34 */
35
36 #include "vm/types.h"
37
38 #include "md-abi.h"
39
40 #include "vm/jit/sparc64/codegen.h"
41
42 #include "vm/exceptions.h"
43 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
44 #include "vm/jit/abi-asm.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/emit-common.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/replace.h"
50
51 /* how to leaf optimization in the emitted stubs?? */
52 #define REG_PV REG_PV_CALLEE
53
54
55 /* emit_load *******************************************************************
56
57    Emits a possible load of an operand.
58
59 *******************************************************************************/
60
61 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
62 {
63         codegendata  *cd;
64         s4            disp;
65         s4            reg;
66
67         /* get required compiler data */
68
69         cd = jd->cd;
70
71         if (src->flags & INMEMORY) {
72                 COUNT_SPILLS;
73
74                 disp = USESTACK + src->vv.regoff * 8;
75
76                 if (IS_FLT_DBL_TYPE(src->type))
77                         M_DLD(tempreg, REG_SP, disp);
78                 else
79                         M_LDX(tempreg, REG_SP, disp);
80
81                 reg = tempreg;
82         }
83         else
84                 reg = src->vv.regoff;
85
86         return reg;
87 }
88
89
90 /* emit_store ******************************************************************
91
92    Emits a possible store to variable.
93
94 *******************************************************************************/
95
96 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
97 {
98         codegendata  *cd;
99         s4            disp;
100
101         /* get required compiler data */
102
103         cd = jd->cd;
104
105         if (dst->flags & INMEMORY) {
106                 COUNT_SPILLS;
107
108                 disp = USESTACK + dst->vv.regoff * 8;
109
110                 if (IS_FLT_DBL_TYPE(dst->type))
111                         M_DST(d, REG_SP, disp);
112                 else
113                         M_STX(d, REG_SP, disp);
114         }
115 }
116
117
118 /* emit_copy *******************************************************************
119
120    Generates a register/memory to register/memory copy.
121
122 *******************************************************************************/
123
124 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
125 {
126         codegendata  *cd;
127         registerdata *rd;
128         s4            s1, d;
129
130         /* get required compiler data */
131
132         cd = jd->cd;
133         rd = jd->rd;
134
135         if ((src->vv.regoff != dst->vv.regoff) ||
136                 ((src->flags ^ dst->flags) & INMEMORY)) {
137
138                 /* If one of the variables resides in memory, we can eliminate
139                    the register move from/to the temporary register with the
140                    order of getting the destination register and the load. */
141
142                 if (IS_INMEMORY(src->flags)) {
143                         d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
144                         s1 = emit_load(jd, iptr, src, d);
145                 }
146                 else {
147                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
148                         d = codegen_reg_of_var(iptr->opc, dst, s1);
149                 }
150
151                 if (s1 != d) {
152                         if (IS_FLT_DBL_TYPE(src->type))
153                                 M_FMOV(s1, d);
154                 else
155                                 M_MOV(s1, d);
156                 }
157
158                 emit_store(jd, iptr, dst, d);
159         }
160 }
161
162
163 /* emit_iconst *****************************************************************
164
165    XXX
166
167 *******************************************************************************/
168
169 void emit_iconst(codegendata *cd, s4 d, s4 value)
170 {
171         s4 disp;
172
173         if ((value >= -4096) && (value <= 4095)) {
174                 M_XOR_IMM(REG_ZERO, value, d);
175         } else {
176                 disp = dseg_add_s4(cd, value);
177                 M_ILD(d, REG_PV_CALLEE, disp);
178         }
179 }
180
181
182 /* emit_lconst *****************************************************************
183
184    XXX
185
186 *******************************************************************************/
187
188 void emit_lconst(codegendata *cd, s4 d, s8 value)
189 {
190         s4 disp;
191
192         if ((value >= -4096) && (value <= 4095)) {
193                 M_XOR_IMM(REG_ZERO, value, d);  
194         } else {
195                 disp = dseg_add_s8(cd, value);
196                 M_LDX(d, REG_PV_CALLEE, disp);
197         }
198 }
199
200
201 /* emit_exception_stubs ********************************************************
202
203    Generates the code for the exception stubs.
204
205 *******************************************************************************/
206
207 void emit_exception_stubs(jitdata *jd)
208 {
209 }
210
211 /* emit_patcher_stubs **********************************************************
212
213    Generates the code for the patcher stubs.
214
215 *******************************************************************************/
216
217 void emit_patcher_stubs(jitdata *jd)
218 {
219         codegendata *cd;
220         patchref    *pref;
221         u4           mcode[2];
222         u1          *savedmcodeptr;
223         u1          *tmpmcodeptr;
224         s4           targetdisp;
225         s4           disp;
226
227         /* get required compiler data */
228
229         cd = jd->cd;
230
231         /* generate code patching stub call code */
232
233         targetdisp = 0;
234
235         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
236                 /* check code segment size */
237
238                 MCODECHECK(100);
239
240                 /* Get machine code which is patched back in later. The
241                    call is 2 instruction words long. */
242
243                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
244
245                 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
246                    SPARC causes a SIGSEGV */
247
248                 mcode[0] = ((u4 *) tmpmcodeptr)[0];
249                 mcode[1] = ((u4 *) tmpmcodeptr)[1];
250
251                 /* Patch in the call to call the following code (done at
252                    compile time). */
253
254                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
255                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
256
257                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
258
259                 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
260                         *exceptionptr =
261                                 new_internalerror("Jump offset is out of range: %d > +/-%d",
262                                                                   disp, 0x003ffff);
263                         return;
264                 }
265
266                 M_BR(disp);
267                 M_NOP;
268
269         cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
270
271                 /* extend stack frame for wrapper data */
272
273                 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
274
275                 /* calculate return address and move it onto the stack */
276
277                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
278                 M_AST(REG_ITMP3, REG_SP, USESTACK + 5 * 8);
279
280                 /* move pointer to java_objectheader onto stack */
281
282 #if defined(ENABLE_THREADS)
283                 /* create a virtual java_objectheader */
284
285                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
286                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
287                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
288
289                 M_LDA(REG_ITMP3, REG_PV, disp);
290                 M_AST(REG_ITMP3, REG_SP, USESTACK + 4 * 8);
291 #else
292                 /* do nothing */
293 #endif
294
295                 /* move machine code onto stack */
296
297                 disp = dseg_add_s4(cd, mcode[0]);
298                 M_ILD(REG_ITMP3, REG_PV, disp);
299                 M_IST(REG_ITMP3, REG_SP, USESTACK + 3 * 8);
300
301                 disp = dseg_add_s4(cd, mcode[1]);
302                 M_ILD(REG_ITMP3, REG_PV, disp);
303                 M_IST(REG_ITMP3, REG_SP, USESTACK + 3 * 8 + 4);
304
305                 /* move class/method/field reference onto stack */
306
307                 disp = dseg_add_address(cd, pref->ref);
308                 M_ALD(REG_ITMP3, REG_PV, disp);
309                 M_AST(REG_ITMP3, REG_SP, USESTACK + 2 * 8);
310
311         /* move data segment displacement onto stack */
312
313                 disp = dseg_add_s4(cd, pref->disp);
314                 M_ILD(REG_ITMP3, REG_PV, disp);
315                 M_IST(REG_ITMP3, REG_SP, USESTACK + 1 * 8);
316
317                 /* move patcher function pointer onto stack */
318
319                 disp = dseg_add_address(cd, pref->patcher);
320                 M_ALD(REG_ITMP3, REG_PV, disp);
321                 M_AST(REG_ITMP3, REG_SP, USESTACK + 0 * 8);
322
323                 if (targetdisp == 0) {
324                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
325
326                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
327                         M_ALD(REG_ITMP3, REG_PV, disp);
328                         M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
329                         M_NOP;
330                 }
331                 else {
332                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
333                                 (((u4 *) cd->mcodeptr));
334
335                         M_BR(disp);
336                         M_NOP;
337                 }
338         }
339 }
340
341
342 /* emit_replacement_stubs ******************************************************
343
344    Generates the code for the replacement stubs.
345
346 *******************************************************************************/
347
348 void emit_replacement_stubs(jitdata *jd)
349 {
350 }
351
352 /* emit_verbosecall_enter ******************************************************
353
354    Generates the code for the call trace.
355
356 *******************************************************************************/
357
358 #if !defined(NDEBUG)
359 void emit_verbosecall_enter(jitdata *jd)
360 {
361         methodinfo   *m;
362         codegendata  *cd;
363         registerdata *rd;
364         methoddesc   *md;
365         s4            disp;
366         s4            i, t;
367
368         /* get required compiler data */
369
370         m  = jd->m;
371         cd = jd->cd;
372         rd = jd->rd;
373
374         md = m->parseddesc;
375
376         /* mark trace code */
377
378         M_NOP;
379
380         /* we're calling a c function allocate paramter array */
381         M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT + ABI_PARAMARRAY_SLOTS) * 8);
382
383         /* save float argument registers */
384
385         for (i = 0; i < FLT_ARG_CNT; i++)
386                 M_DST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS + (1 + i) * 8);
387
388         /* save temporary registers for leaf methods */
389 /* XXX no leaf optimization yet
390         if (jd->isleafmethod) {
391                 for (i = 0; i < INT_TMP_CNT; i++)
392                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
393
394                 for (i = 0; i < FLT_TMP_CNT; i++)
395                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
396         }
397 */
398         /* load int/float arguments into integer argument registers */
399
400         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
401                 t = md->paramtypes[i].type;
402
403                 if (IS_INT_LNG_TYPE(t)) {
404                         M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
405                 }
406                 else {
407                         if (IS_2_WORD_TYPE(t)) {
408                                 M_DST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS);
409                                 M_LDX(rd->argintregs[i], REG_SP, USESTACK_PARAMS);
410                         }
411                         else {
412                                 M_FST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS);
413                                 M_ILD(rd->argintregs[i], REG_SP, USESTACK_PARAMS);
414                         }
415                 }
416         }
417         
418         
419         /* method info pointer is passed in argument register 5 */
420         disp = dseg_add_address(cd, m);
421         M_ALD(REG_OUT5, REG_PV_CALLEE, disp);
422         disp = dseg_add_functionptr(cd, builtin_trace_args);
423         M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
424         M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
425         M_NOP;
426
427         /* restore float argument registers */
428
429         for (i = 0; i < FLT_ARG_CNT; i++)
430                 M_DLD(rd->argfltregs[i], REG_SP, USESTACK_PARAMS + (1 + i) * 8);
431
432         /* restore temporary registers for leaf methods */
433 /* XXX no leaf optimization yet
434         if (jd->isleafmethod) {
435                 for (i = 0; i < INT_TMP_CNT; i++)
436                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
437
438                 for (i = 0; i < FLT_TMP_CNT; i++)
439                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
440         }
441 */
442         M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT + ABI_PARAMARRAY_SLOTS) * 8);
443
444         /* mark trace code */
445
446         M_NOP;
447 }
448 #endif /* !defined(NDEBUG) */
449
450
451 /* emit_verbosecall_exit *******************************************************
452
453    Generates the code for the call trace.
454
455 *******************************************************************************/
456
457 #if !defined(NDEBUG)
458 void emit_verbosecall_exit(jitdata *jd)
459 {
460         methodinfo   *m;
461         codegendata  *cd;
462         registerdata *rd;
463         s4            disp;
464
465         /* get required compiler data */
466
467         m  = jd->m;
468         cd = jd->cd;
469         rd = jd->rd;
470
471         /* mark trace code */
472
473         M_NOP;
474         
475         /* we're calling a c function allocate paramter array */
476         M_LDA(REG_SP, REG_SP, -(1 + ABI_PARAMARRAY_SLOTS) * 8);
477
478         M_DST(REG_FRESULT, REG_SP, USESTACK_PARAMS);
479
480         disp = dseg_add_address(cd, m);
481         M_ALD(rd->argintregs[0], REG_PV_CALLEE, disp);
482
483         M_MOV(REG_RESULT_CALLEE, rd->argintregs[1]);
484         M_DMOV(REG_FRESULT, 2);
485         M_FMOV(REG_FRESULT, 2);
486
487         disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
488         M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
489         M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
490         M_NOP;
491
492         M_DLD(REG_FRESULT, REG_SP, USESTACK_PARAMS);
493
494         M_LDA(REG_SP, REG_SP, (1 + ABI_PARAMARRAY_SLOTS) * 8);
495
496         /* mark trace code */
497
498         M_NOP;
499 }
500 #endif /* !defined(NDEBUG) */
501
502
503 /*
504  * These are local overrides for various environment variables in Emacs.
505  * Please do not remove this and leave it at the end of the file, where
506  * Emacs will automagically detect them.
507  * ---------------------------------------------------------------------
508  * Local variables:
509  * mode: c
510  * indent-tabs-mode: t
511  * c-basic-offset: 4
512  * tab-width: 4
513  * End:
514  * vim:noexpandtab:sw=4:ts=4:
515  */