1 /* src/vm/jit/sparc64/emit.c - Sparc code emitter functions
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
33 #include "vm/jit/sparc64/codegen.h"
34 #include "vm/jit/sparc64/md-abi.h"
36 #include "mm/memory.h"
38 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
39 #include "vm/jit/abi-asm.h"
40 #include "vm/jit/asmpart.h"
41 #include "vm/builtin.h"
42 #include "vm/jit/dseg.h"
43 #include "vm/jit/emit-common.h"
44 #include "vm/jit/jit.h"
45 #include "vm/jit/replace.h"
47 #include "vmcore/options.h"
49 /* how to leaf optimization in the emitted stubs?? */
50 #define REG_PV REG_PV_CALLEE
53 /* emit_load *******************************************************************
55 Emits a possible load of an operand.
57 *******************************************************************************/
59 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
65 /* get required compiler data */
69 if (src->flags & INMEMORY) {
72 disp = JITSTACK + src->vv.regoff * 8;
79 M_LDX(tempreg, REG_SP, disp);
83 M_DLD(tempreg, REG_SP, disp);
86 vm_abort("emit_load: unknown type %d", src->type);
99 /* emit_store ******************************************************************
101 Emits a possible store to variable.
103 *******************************************************************************/
105 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
110 /* get required compiler data */
114 if (dst->flags & INMEMORY) {
117 disp = JITSTACK + dst->vv.regoff * 8;
124 M_STX(d, REG_SP, disp);
128 M_DST(d, REG_SP, disp);
131 vm_abort("emit_store: unknown type %d", dst->type);
138 /* emit_copy *******************************************************************
140 Generates a register/memory to register/memory copy.
142 *******************************************************************************/
144 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
150 /* get required compiler data */
155 if ((src->vv.regoff != dst->vv.regoff) ||
156 ((src->flags ^ dst->flags) & INMEMORY)) {
158 /* If one of the variables resides in memory, we can eliminate
159 the register move from/to the temporary register with the
160 order of getting the destination register and the load. */
162 if (IS_INMEMORY(src->flags)) {
163 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
164 s1 = emit_load(jd, iptr, src, d);
167 s1 = emit_load(jd, iptr, src, REG_IFTMP);
168 d = codegen_reg_of_var(iptr->opc, dst, s1);
184 vm_abort("emit_copy: unknown type %d", src->type);
189 emit_store(jd, iptr, dst, d);
194 /* emit_iconst *****************************************************************
198 *******************************************************************************/
200 void emit_iconst(codegendata *cd, s4 d, s4 value)
204 if ((value >= -4096) && (value <= 4095)) {
205 M_XOR_IMM(REG_ZERO, value, d);
207 disp = dseg_add_s4(cd, value);
208 M_ILD(d, REG_PV_CALLEE, disp);
213 /* emit_lconst *****************************************************************
217 *******************************************************************************/
219 void emit_lconst(codegendata *cd, s4 d, s8 value)
223 if ((value >= -4096) && (value <= 4095)) {
224 M_XOR_IMM(REG_ZERO, value, d);
226 disp = dseg_add_s8(cd, value);
227 M_LDX(d, REG_PV_CALLEE, disp);
232 /* emit_arithmetic_check *******************************************************
234 Emit an ArithmeticException check.
236 *******************************************************************************/
238 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
240 if (INSTRUCTION_MUST_CHECK(iptr)) {
242 codegen_add_arithmeticexception_ref(cd);
248 /* emit_arrayindexoutofbounds_check ********************************************
250 Emit an ArrayIndexOutOfBoundsException check.
252 *******************************************************************************/
254 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
256 if (INSTRUCTION_MUST_CHECK(iptr)) {
257 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
258 M_CMP(s2, REG_ITMP3);
260 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
265 /* emit_nullpointer_check ******************************************************
267 Emit a NullPointerException check.
269 *******************************************************************************/
271 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
273 if (INSTRUCTION_MUST_CHECK(iptr)) {
275 codegen_add_nullpointerexception_ref(cd);
280 /* emit_exception_stubs ********************************************************
282 Generates the code for the exception stubs.
284 *******************************************************************************/
286 void emit_exception_stubs(jitdata *jd)
290 /* emit_patcher_stubs **********************************************************
292 Generates the code for the patcher stubs.
294 *******************************************************************************/
296 void emit_patcher_stubs(jitdata *jd)
306 /* get required compiler data */
310 /* generate code patching stub call code */
314 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
315 /* check code segment size */
319 /* Get machine code which is patched back in later. The
320 call is 2 instruction words long. */
322 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
324 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
325 SPARC causes a SIGSEGV */
327 mcode[0] = ((u4 *) tmpmcodeptr)[0];
328 mcode[1] = ((u4 *) tmpmcodeptr)[1];
330 /* Patch in the call to call the following code (done at
333 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
334 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
336 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
338 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
339 vm_abort("Jump offset is out of range: %d > +/-%d",
347 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
349 /* extend stack frame for wrapper data */
351 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
353 /* calculate return address and move it onto the stack */
355 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
356 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
358 /* move pointer to java_objectheader onto stack */
360 #if defined(ENABLE_THREADS)
361 /* create a virtual java_objectheader */
363 (void) dseg_add_unique_address(cd, NULL); /* flcword */
364 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
365 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
367 M_LDA(REG_ITMP3, REG_PV, disp);
368 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
373 /* move machine code onto stack */
375 disp = dseg_add_s4(cd, mcode[0]);
376 M_ILD(REG_ITMP3, REG_PV, disp);
377 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
379 disp = dseg_add_s4(cd, mcode[1]);
380 M_ILD(REG_ITMP3, REG_PV, disp);
381 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
383 /* move class/method/field reference onto stack */
385 disp = dseg_add_address(cd, pref->ref);
386 M_ALD(REG_ITMP3, REG_PV, disp);
387 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
389 /* move data segment displacement onto stack */
391 disp = dseg_add_s4(cd, pref->disp);
392 M_ILD(REG_ITMP3, REG_PV, disp);
393 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
395 /* move patcher function pointer onto stack */
397 disp = dseg_add_functionptr(cd, pref->patcher);
398 M_ALD(REG_ITMP3, REG_PV, disp);
399 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
401 if (targetdisp == 0) {
402 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
404 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
405 M_ALD(REG_ITMP3, REG_PV, disp);
406 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
410 disp = (((u4 *) cd->mcodebase) + targetdisp) -
411 (((u4 *) cd->mcodeptr));
420 /* emit_replacement_stubs ******************************************************
422 Generates the code for the replacement stubs.
424 *******************************************************************************/
426 #if defined(ENABLE_REPLACEMENT)
427 void emit_replacement_stubs(jitdata *jd)
430 #endif /* defined(ENABLE_REPLACEMENT) */
432 /* emit_verbosecall_enter ******************************************************
434 Generates the code for the call trace.
436 *******************************************************************************/
439 void emit_verbosecall_enter(jitdata *jd)
448 /* get required compiler data */
456 /* mark trace code */
461 M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT) * 8);
463 /* save float argument registers */
465 for (i = 0; i < FLT_ARG_CNT; i++)
466 M_DST(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
468 /* save temporary registers for leaf methods */
469 /* XXX no leaf optimization yet
470 if (jd->isleafmethod) {
471 for (i = 0; i < INT_TMP_CNT; i++)
472 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
474 for (i = 0; i < FLT_TMP_CNT; i++)
475 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
478 /* load int/float arguments into integer argument registers */
480 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
481 t = md->paramtypes[i].type;
483 /* using all available argument registers, this adds complexity */
485 if (IS_INT_LNG_TYPE(t)) {
486 if (i < INT_ARG_CNT) {
487 M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
491 M_LDX(REG_OUT5, REG_FP, JITSTACK);
495 assert(i < 4); /* XXX only 4 float reg args right now! */
496 if (IS_2_WORD_TYPE(t)) {
497 M_DST(rd->argfltregs[i], REG_SP, JITSTACK);
498 M_LDX(rd->argintregs[i], REG_SP, JITSTACK);
501 M_FST(rd->argfltregs[i], REG_SP, JITSTACK);
502 M_ILD(rd->argintregs[i], REG_SP, JITSTACK);
508 /* method info pointer is passed in argument register 5 */
509 disp = dseg_add_address(cd, m);
510 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
511 M_AST(REG_ITMP1, REG_SP, CSTACK);
512 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
513 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
514 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
517 /* restore float argument registers */
519 for (i = 0; i < FLT_ARG_CNT; i++)
520 M_DLD(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
522 /* restore temporary registers for leaf methods */
523 /* XXX no leaf optimization yet
524 if (jd->isleafmethod) {
525 for (i = 0; i < INT_TMP_CNT; i++)
526 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
528 for (i = 0; i < FLT_TMP_CNT; i++)
529 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
532 M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT) * 8);
534 /* mark trace code */
538 #endif /* !defined(NDEBUG) */
541 /* emit_verbosecall_exit *******************************************************
543 Generates the code for the call trace.
545 *******************************************************************************/
548 void emit_verbosecall_exit(jitdata *jd)
555 /* get required compiler data */
561 /* mark trace code */
566 M_LDA(REG_SP, REG_SP, -(1 * 8));
568 M_DST(REG_FRESULT, REG_SP, JITSTACK);
570 M_MOV(REG_RESULT_CALLEE, rd->argintregs[0]);
571 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
572 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
574 disp = dseg_add_functionptr(cd, m);
575 M_ALD(rd->argintregs[3], REG_PV_CALLEE, disp);
577 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
578 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
579 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
582 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
584 M_LDA(REG_SP, REG_SP, 1 * 8);
586 /* mark trace code */
590 #endif /* !defined(NDEBUG) */
594 * These are local overrides for various environment variables in Emacs.
595 * Please do not remove this and leave it at the end of the file, where
596 * Emacs will automagically detect them.
597 * ---------------------------------------------------------------------
600 * indent-tabs-mode: t
604 * vim:noexpandtab:sw=4:ts=4: