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 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
30 $Id: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
38 #include "vm/jit/sparc64/codegen.h"
39 #include "vm/jit/sparc64/md-abi.h"
41 #include "mm/memory.h"
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"
54 /* how to leaf optimization in the emitted stubs?? */
55 #define REG_PV REG_PV_CALLEE
58 /* emit_load *******************************************************************
60 Emits a possible load of an operand.
62 *******************************************************************************/
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
70 /* get required compiler data */
74 if (src->flags & INMEMORY) {
77 disp = JITSTACK + src->vv.regoff * 8;
79 if (IS_FLT_DBL_TYPE(src->type))
80 M_DLD(tempreg, REG_SP, disp);
82 M_LDX(tempreg, REG_SP, disp);
93 /* emit_store ******************************************************************
95 Emits a possible store to variable.
97 *******************************************************************************/
99 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
104 /* get required compiler data */
108 if (dst->flags & INMEMORY) {
111 disp = JITSTACK + dst->vv.regoff * 8;
113 if (IS_FLT_DBL_TYPE(dst->type))
114 M_DST(d, REG_SP, disp);
116 M_STX(d, REG_SP, disp);
121 /* emit_copy *******************************************************************
123 Generates a register/memory to register/memory copy.
125 *******************************************************************************/
127 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
133 /* get required compiler data */
138 if ((src->vv.regoff != dst->vv.regoff) ||
139 ((src->flags ^ dst->flags) & INMEMORY)) {
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. */
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);
150 s1 = emit_load(jd, iptr, src, REG_IFTMP);
151 d = codegen_reg_of_var(iptr->opc, dst, s1);
155 if (IS_FLT_DBL_TYPE(src->type))
161 emit_store(jd, iptr, dst, d);
166 /* emit_iconst *****************************************************************
170 *******************************************************************************/
172 void emit_iconst(codegendata *cd, s4 d, s4 value)
176 if ((value >= -4096) && (value <= 4095)) {
177 M_XOR_IMM(REG_ZERO, value, d);
179 disp = dseg_add_s4(cd, value);
180 M_ILD(d, REG_PV_CALLEE, disp);
185 /* emit_lconst *****************************************************************
189 *******************************************************************************/
191 void emit_lconst(codegendata *cd, s4 d, s8 value)
195 if ((value >= -4096) && (value <= 4095)) {
196 M_XOR_IMM(REG_ZERO, value, d);
198 disp = dseg_add_s8(cd, value);
199 M_LDX(d, REG_PV_CALLEE, disp);
204 /* emit_arithmetic_check *******************************************************
206 Emit an ArithmeticException check.
208 *******************************************************************************/
210 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
212 if (INSTRUCTION_MUST_CHECK(iptr)) {
214 codegen_add_arithmeticexception_ref(cd);
220 /* emit_arrayindexoutofbounds_check ********************************************
222 Emit an ArrayIndexOutOfBoundsException check.
224 *******************************************************************************/
226 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
228 if (INSTRUCTION_MUST_CHECK(iptr)) {
229 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
230 M_CMP(s2, REG_ITMP3);
232 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
237 /* emit_nullpointer_check ******************************************************
239 Emit a NullPointerException check.
241 *******************************************************************************/
243 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
245 if (INSTRUCTION_MUST_CHECK(iptr)) {
247 codegen_add_nullpointerexception_ref(cd);
252 /* emit_exception_stubs ********************************************************
254 Generates the code for the exception stubs.
256 *******************************************************************************/
258 void emit_exception_stubs(jitdata *jd)
262 /* emit_patcher_stubs **********************************************************
264 Generates the code for the patcher stubs.
266 *******************************************************************************/
268 void emit_patcher_stubs(jitdata *jd)
278 /* get required compiler data */
282 /* generate code patching stub call code */
286 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
287 /* check code segment size */
291 /* Get machine code which is patched back in later. The
292 call is 2 instruction words long. */
294 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
296 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
297 SPARC causes a SIGSEGV */
299 mcode[0] = ((u4 *) tmpmcodeptr)[0];
300 mcode[1] = ((u4 *) tmpmcodeptr)[1];
302 /* Patch in the call to call the following code (done at
305 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
306 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
308 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
310 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
311 vm_abort("Jump offset is out of range: %d > +/-%d",
319 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
321 /* extend stack frame for wrapper data */
323 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
325 /* calculate return address and move it onto the stack */
327 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
328 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
330 /* move pointer to java_objectheader onto stack */
332 #if defined(ENABLE_THREADS)
333 /* create a virtual java_objectheader */
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 */
339 M_LDA(REG_ITMP3, REG_PV, disp);
340 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
345 /* move machine code onto stack */
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);
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);
355 /* move class/method/field reference onto stack */
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);
361 /* move data segment displacement onto stack */
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);
367 /* move patcher function pointer onto stack */
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);
373 if (targetdisp == 0) {
374 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
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);
382 disp = (((u4 *) cd->mcodebase) + targetdisp) -
383 (((u4 *) cd->mcodeptr));
392 /* emit_replacement_stubs ******************************************************
394 Generates the code for the replacement stubs.
396 *******************************************************************************/
398 #if defined(ENABLE_REPLACEMENT)
399 void emit_replacement_stubs(jitdata *jd)
402 #endif /* defined(ENABLE_REPLACEMENT) */
404 /* emit_verbosecall_enter ******************************************************
406 Generates the code for the call trace.
408 *******************************************************************************/
411 void emit_verbosecall_enter(jitdata *jd)
420 /* get required compiler data */
428 /* mark trace code */
433 M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT) * 8);
435 /* save float argument registers */
437 for (i = 0; i < FLT_ARG_CNT; i++)
438 M_DST(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
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);
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);
450 /* load int/float arguments into integer argument registers */
452 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
453 t = md->paramtypes[i].type;
455 if (IS_INT_LNG_TYPE(t)) {
456 M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
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);
464 M_FST(rd->argfltregs[i], REG_SP, JITSTACK);
465 M_ILD(rd->argintregs[i], REG_SP, JITSTACK);
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);
479 /* restore float argument registers */
481 for (i = 0; i < FLT_ARG_CNT; i++)
482 M_DLD(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
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);
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);
494 M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT) * 8);
496 /* mark trace code */
500 #endif /* !defined(NDEBUG) */
503 /* emit_verbosecall_exit *******************************************************
505 Generates the code for the call trace.
507 *******************************************************************************/
510 void emit_verbosecall_exit(jitdata *jd)
517 /* get required compiler data */
523 /* mark trace code */
528 M_LDA(REG_SP, REG_SP, -(1 * 8));
530 M_DST(REG_FRESULT, REG_SP, JITSTACK);
532 disp = dseg_add_address(cd, m);
533 M_ALD(rd->argintregs[0], REG_PV_CALLEE, disp);
535 M_MOV(REG_RESULT_CALLEE, rd->argintregs[1]);
536 M_DMOV(REG_FRESULT, 2); /* applies for flt and dbl values */
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);
543 M_DLD(REG_FRESULT, REG_SP, JITSTACK);
545 M_LDA(REG_SP, REG_SP, 1 * 8);
547 /* mark trace code */
551 #endif /* !defined(NDEBUG) */
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 * ---------------------------------------------------------------------
561 * indent-tabs-mode: t
565 * vim:noexpandtab:sw=4:ts=4: