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;
74 if (IS_FLT_DBL_TYPE(src->type))
75 M_DLD(tempreg, REG_SP, disp);
77 M_LDX(tempreg, REG_SP, disp);
88 /* emit_store ******************************************************************
90 Emits a possible store to variable.
92 *******************************************************************************/
94 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
99 /* get required compiler data */
103 if (dst->flags & INMEMORY) {
106 disp = JITSTACK + dst->vv.regoff * 8;
108 if (IS_FLT_DBL_TYPE(dst->type))
109 M_DST(d, REG_SP, disp);
111 M_STX(d, REG_SP, disp);
116 /* emit_copy *******************************************************************
118 Generates a register/memory to register/memory copy.
120 *******************************************************************************/
122 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
128 /* get required compiler data */
133 if ((src->vv.regoff != dst->vv.regoff) ||
134 ((src->flags ^ dst->flags) & INMEMORY)) {
136 /* If one of the variables resides in memory, we can eliminate
137 the register move from/to the temporary register with the
138 order of getting the destination register and the load. */
140 if (IS_INMEMORY(src->flags)) {
141 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
142 s1 = emit_load(jd, iptr, src, d);
145 s1 = emit_load(jd, iptr, src, REG_IFTMP);
146 d = codegen_reg_of_var(iptr->opc, dst, s1);
150 if (IS_FLT_DBL_TYPE(src->type))
156 emit_store(jd, iptr, dst, d);
161 /* emit_iconst *****************************************************************
165 *******************************************************************************/
167 void emit_iconst(codegendata *cd, s4 d, s4 value)
171 if ((value >= -4096) && (value <= 4095)) {
172 M_XOR_IMM(REG_ZERO, value, d);
174 disp = dseg_add_s4(cd, value);
175 M_ILD(d, REG_PV_CALLEE, disp);
180 /* emit_lconst *****************************************************************
184 *******************************************************************************/
186 void emit_lconst(codegendata *cd, s4 d, s8 value)
190 if ((value >= -4096) && (value <= 4095)) {
191 M_XOR_IMM(REG_ZERO, value, d);
193 disp = dseg_add_s8(cd, value);
194 M_LDX(d, REG_PV_CALLEE, disp);
199 /* emit_arithmetic_check *******************************************************
201 Emit an ArithmeticException check.
203 *******************************************************************************/
205 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
207 if (INSTRUCTION_MUST_CHECK(iptr)) {
209 codegen_add_arithmeticexception_ref(cd);
215 /* emit_arrayindexoutofbounds_check ********************************************
217 Emit an ArrayIndexOutOfBoundsException check.
219 *******************************************************************************/
221 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
223 if (INSTRUCTION_MUST_CHECK(iptr)) {
224 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
225 M_CMP(s2, REG_ITMP3);
227 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
232 /* emit_nullpointer_check ******************************************************
234 Emit a NullPointerException check.
236 *******************************************************************************/
238 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
240 if (INSTRUCTION_MUST_CHECK(iptr)) {
242 codegen_add_nullpointerexception_ref(cd);
247 /* emit_exception_stubs ********************************************************
249 Generates the code for the exception stubs.
251 *******************************************************************************/
253 void emit_exception_stubs(jitdata *jd)
257 /* emit_patcher_stubs **********************************************************
259 Generates the code for the patcher stubs.
261 *******************************************************************************/
263 void emit_patcher_stubs(jitdata *jd)
273 /* get required compiler data */
277 /* generate code patching stub call code */
281 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
282 /* check code segment size */
286 /* Get machine code which is patched back in later. The
287 call is 2 instruction words long. */
289 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
291 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
292 SPARC causes a SIGSEGV */
294 mcode[0] = ((u4 *) tmpmcodeptr)[0];
295 mcode[1] = ((u4 *) tmpmcodeptr)[1];
297 /* Patch in the call to call the following code (done at
300 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
301 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
303 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
305 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
306 vm_abort("Jump offset is out of range: %d > +/-%d",
314 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
316 /* extend stack frame for wrapper data */
318 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
320 /* calculate return address and move it onto the stack */
322 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
323 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
325 /* move pointer to java_objectheader onto stack */
327 #if defined(ENABLE_THREADS)
328 /* create a virtual java_objectheader */
330 (void) dseg_add_unique_address(cd, NULL); /* flcword */
331 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
332 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
334 M_LDA(REG_ITMP3, REG_PV, disp);
335 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
340 /* move machine code onto stack */
342 disp = dseg_add_s4(cd, mcode[0]);
343 M_ILD(REG_ITMP3, REG_PV, disp);
344 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
346 disp = dseg_add_s4(cd, mcode[1]);
347 M_ILD(REG_ITMP3, REG_PV, disp);
348 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
350 /* move class/method/field reference onto stack */
352 disp = dseg_add_address(cd, pref->ref);
353 M_ALD(REG_ITMP3, REG_PV, disp);
354 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
356 /* move data segment displacement onto stack */
358 disp = dseg_add_s4(cd, pref->disp);
359 M_ILD(REG_ITMP3, REG_PV, disp);
360 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
362 /* move patcher function pointer onto stack */
364 disp = dseg_add_functionptr(cd, pref->patcher);
365 M_ALD(REG_ITMP3, REG_PV, disp);
366 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
368 if (targetdisp == 0) {
369 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
371 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
372 M_ALD(REG_ITMP3, REG_PV, disp);
373 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
377 disp = (((u4 *) cd->mcodebase) + targetdisp) -
378 (((u4 *) cd->mcodeptr));
387 /* emit_replacement_stubs ******************************************************
389 Generates the code for the replacement stubs.
391 *******************************************************************************/
393 #if defined(ENABLE_REPLACEMENT)
394 void emit_replacement_stubs(jitdata *jd)
397 #endif /* defined(ENABLE_REPLACEMENT) */
399 /* emit_verbosecall_enter ******************************************************
401 Generates the code for the call trace.
403 *******************************************************************************/
406 void emit_verbosecall_enter(jitdata *jd)
415 /* get required compiler data */
423 /* mark trace code */
428 M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT) * 8);
430 /* save float argument registers */
432 for (i = 0; i < FLT_ARG_CNT; i++)
433 M_DST(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
435 /* save temporary registers for leaf methods */
436 /* XXX no leaf optimization yet
437 if (jd->isleafmethod) {
438 for (i = 0; i < INT_TMP_CNT; i++)
439 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
441 for (i = 0; i < FLT_TMP_CNT; i++)
442 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
445 /* load int/float arguments into integer argument registers */
447 for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
448 t = md->paramtypes[i].type;
450 /* using all available argument registers, this adds complexity */
452 if (IS_INT_LNG_TYPE(t)) {
453 if (i < INT_ARG_CNT) {
454 M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
458 M_LDX(REG_OUT5, REG_FP, JITSTACK);
462 assert(i < 4); /* XXX only 4 float reg args right now! */
463 if (IS_2_WORD_TYPE(t)) {
464 M_DST(rd->argfltregs[i], REG_SP, JITSTACK);
465 M_LDX(rd->argintregs[i], REG_SP, JITSTACK);
468 M_FST(rd->argfltregs[i], REG_SP, JITSTACK);
469 M_ILD(rd->argintregs[i], REG_SP, JITSTACK);
475 /* method info pointer is passed in argument register 5 */
476 disp = dseg_add_address(cd, m);
477 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
478 M_AST(REG_ITMP1, REG_SP, CSTACK);
479 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
480 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
481 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
484 /* restore float argument registers */
486 for (i = 0; i < FLT_ARG_CNT; i++)
487 M_DLD(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
489 /* restore temporary registers for leaf methods */
490 /* XXX no leaf optimization yet
491 if (jd->isleafmethod) {
492 for (i = 0; i < INT_TMP_CNT; i++)
493 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
495 for (i = 0; i < FLT_TMP_CNT; i++)
496 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
499 M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT) * 8);
501 /* mark trace code */
505 #endif /* !defined(NDEBUG) */
508 /* emit_verbosecall_exit *******************************************************
510 Generates the code for the call trace.
512 *******************************************************************************/
515 void emit_verbosecall_exit(jitdata *jd)
522 /* get required compiler data */
528 /* mark trace code */
533 M_LDA(REG_SP, REG_SP, -(1 * 8));
535 M_DST(REG_FRESULT, REG_SP, JITSTACK);
537 M_MOV(REG_RESULT_CALLEE, rd->argintregs[0]);
538 M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
539 M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
541 disp = dseg_add_functionptr(cd, m);
542 M_ALD(rd->argintregs[3], REG_PV_CALLEE, disp);
544 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
545 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
546 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
549 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
551 M_LDA(REG_SP, REG_SP, 1 * 8);
553 /* mark trace code */
557 #endif /* !defined(NDEBUG) */
561 * These are local overrides for various environment variables in Emacs.
562 * Please do not remove this and leave it at the end of the file, where
563 * Emacs will automagically detect them.
564 * ---------------------------------------------------------------------
567 * indent-tabs-mode: t
571 * vim:noexpandtab:sw=4:ts=4: