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