1 /* src/vm/jit/sparc64/emit.c - Sparc code emitter functions
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
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
32 $Id: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
40 #include "vm/jit/sparc64/codegen.h"
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"
51 /* how to leaf optimization in the emitted stubs?? */
52 #define REG_PV REG_PV_CALLEE
55 /* emit_load *******************************************************************
57 Emits a possible load of an operand.
59 *******************************************************************************/
61 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
67 /* get required compiler data */
71 if (src->flags & INMEMORY) {
74 disp = USESTACK + src->vv.regoff * 8;
76 if (IS_FLT_DBL_TYPE(src->type))
77 M_DLD(tempreg, REG_SP, disp);
79 M_LDX(tempreg, REG_SP, disp);
90 /* emit_store ******************************************************************
92 Emits a possible store to variable.
94 *******************************************************************************/
96 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
101 /* get required compiler data */
105 if (dst->flags & INMEMORY) {
108 disp = USESTACK + dst->vv.regoff * 8;
110 if (IS_FLT_DBL_TYPE(dst->type))
111 M_DST(d, REG_SP, disp);
113 M_STX(d, REG_SP, disp);
118 /* emit_copy *******************************************************************
120 Generates a register/memory to register/memory copy.
122 *******************************************************************************/
124 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
130 /* get required compiler data */
135 if ((src->vv.regoff != dst->vv.regoff) ||
136 ((src->flags ^ dst->flags) & INMEMORY)) {
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. */
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);
147 s1 = emit_load(jd, iptr, src, REG_IFTMP);
148 d = codegen_reg_of_var(iptr->opc, dst, s1);
152 if (IS_FLT_DBL_TYPE(src->type))
158 emit_store(jd, iptr, dst, d);
163 /* emit_iconst *****************************************************************
167 *******************************************************************************/
169 void emit_iconst(codegendata *cd, s4 d, s4 value)
173 if ((value >= -4096) && (value <= 4095)) {
174 M_XOR_IMM(REG_ZERO, value, d);
176 disp = dseg_add_s4(cd, value);
177 M_ILD(d, REG_PV_CALLEE, disp);
182 /* emit_lconst *****************************************************************
186 *******************************************************************************/
188 void emit_lconst(codegendata *cd, s4 d, s8 value)
192 if ((value >= -4096) && (value <= 4095)) {
193 M_XOR_IMM(REG_ZERO, value, d);
195 disp = dseg_add_s8(cd, value);
196 M_LDX(d, REG_PV_CALLEE, disp);
200 /* emit_arrayindexoutofbounds_check ********************************************
202 Emit an ArrayIndexOutOfBoundsException check.
204 *******************************************************************************/
206 void emit_arrayindexoutofbounds_check(codegendata *cd, s4 s1, s4 s2)
210 /* emit_nullpointer_check ******************************************************
212 Emit a NullPointerException check.
214 *******************************************************************************/
216 void emit_nullpointer_check(codegendata *cd, s4 reg)
220 /* emit_exception_stubs ********************************************************
222 Generates the code for the exception stubs.
224 *******************************************************************************/
226 void emit_exception_stubs(jitdata *jd)
230 /* emit_patcher_stubs **********************************************************
232 Generates the code for the patcher stubs.
234 *******************************************************************************/
236 void emit_patcher_stubs(jitdata *jd)
246 /* get required compiler data */
250 /* generate code patching stub call code */
254 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
255 /* check code segment size */
259 /* Get machine code which is patched back in later. The
260 call is 2 instruction words long. */
262 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
264 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
265 SPARC causes a SIGSEGV */
267 mcode[0] = ((u4 *) tmpmcodeptr)[0];
268 mcode[1] = ((u4 *) tmpmcodeptr)[1];
270 /* Patch in the call to call the following code (done at
273 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
274 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
276 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
278 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
280 new_internalerror("Jump offset is out of range: %d > +/-%d",
288 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
290 /* extend stack frame for wrapper data */
292 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
294 /* calculate return address and move it onto the stack */
296 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
297 M_AST(REG_ITMP3, REG_SP, USESTACK + 5 * 8);
299 /* move pointer to java_objectheader onto stack */
301 #if defined(ENABLE_THREADS)
302 /* create a virtual java_objectheader */
304 (void) dseg_add_unique_address(cd, NULL); /* flcword */
305 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
306 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
308 M_LDA(REG_ITMP3, REG_PV, disp);
309 M_AST(REG_ITMP3, REG_SP, USESTACK + 4 * 8);
314 /* move machine code onto stack */
316 disp = dseg_add_s4(cd, mcode[0]);
317 M_ILD(REG_ITMP3, REG_PV, disp);
318 M_IST(REG_ITMP3, REG_SP, USESTACK + 3 * 8);
320 disp = dseg_add_s4(cd, mcode[1]);
321 M_ILD(REG_ITMP3, REG_PV, disp);
322 M_IST(REG_ITMP3, REG_SP, USESTACK + 3 * 8 + 4);
324 /* move class/method/field reference onto stack */
326 disp = dseg_add_address(cd, pref->ref);
327 M_ALD(REG_ITMP3, REG_PV, disp);
328 M_AST(REG_ITMP3, REG_SP, USESTACK + 2 * 8);
330 /* move data segment displacement onto stack */
332 disp = dseg_add_s4(cd, pref->disp);
333 M_ILD(REG_ITMP3, REG_PV, disp);
334 M_IST(REG_ITMP3, REG_SP, USESTACK + 1 * 8);
336 /* move patcher function pointer onto stack */
338 disp = dseg_add_address(cd, pref->patcher);
339 M_ALD(REG_ITMP3, REG_PV, disp);
340 M_AST(REG_ITMP3, REG_SP, USESTACK + 0 * 8);
342 if (targetdisp == 0) {
343 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
345 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
346 M_ALD(REG_ITMP3, REG_PV, disp);
347 M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
351 disp = (((u4 *) cd->mcodebase) + targetdisp) -
352 (((u4 *) cd->mcodeptr));
361 /* emit_replacement_stubs ******************************************************
363 Generates the code for the replacement stubs.
365 *******************************************************************************/
367 void emit_replacement_stubs(jitdata *jd)
371 /* emit_verbosecall_enter ******************************************************
373 Generates the code for the call trace.
375 *******************************************************************************/
378 void emit_verbosecall_enter(jitdata *jd)
387 /* get required compiler data */
395 /* mark trace code */
399 /* we're calling a c function allocate paramter array */
400 M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT + ABI_PARAMARRAY_SLOTS) * 8);
402 /* save float argument registers */
404 for (i = 0; i < FLT_ARG_CNT; i++)
405 M_DST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS + (1 + i) * 8);
407 /* save temporary registers for leaf methods */
408 /* XXX no leaf optimization yet
409 if (jd->isleafmethod) {
410 for (i = 0; i < INT_TMP_CNT; i++)
411 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
413 for (i = 0; i < FLT_TMP_CNT; i++)
414 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
417 /* load int/float arguments into integer argument registers */
419 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
420 t = md->paramtypes[i].type;
422 if (IS_INT_LNG_TYPE(t)) {
423 M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
426 if (IS_2_WORD_TYPE(t)) {
427 M_DST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS);
428 M_LDX(rd->argintregs[i], REG_SP, USESTACK_PARAMS);
431 M_FST(rd->argfltregs[i], REG_SP, USESTACK_PARAMS);
432 M_ILD(rd->argintregs[i], REG_SP, USESTACK_PARAMS);
438 /* method info pointer is passed in argument register 5 */
439 disp = dseg_add_address(cd, m);
440 M_ALD(REG_OUT5, REG_PV_CALLEE, disp);
441 disp = dseg_add_functionptr(cd, builtin_trace_args);
442 M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
443 M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
446 /* restore float argument registers */
448 for (i = 0; i < FLT_ARG_CNT; i++)
449 M_DLD(rd->argfltregs[i], REG_SP, USESTACK_PARAMS + (1 + i) * 8);
451 /* restore temporary registers for leaf methods */
452 /* XXX no leaf optimization yet
453 if (jd->isleafmethod) {
454 for (i = 0; i < INT_TMP_CNT; i++)
455 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
457 for (i = 0; i < FLT_TMP_CNT; i++)
458 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
461 M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT + ABI_PARAMARRAY_SLOTS) * 8);
463 /* mark trace code */
467 #endif /* !defined(NDEBUG) */
470 /* emit_verbosecall_exit *******************************************************
472 Generates the code for the call trace.
474 *******************************************************************************/
477 void emit_verbosecall_exit(jitdata *jd)
484 /* get required compiler data */
490 /* mark trace code */
494 /* we're calling a c function allocate paramter array */
495 M_LDA(REG_SP, REG_SP, -(1 + ABI_PARAMARRAY_SLOTS) * 8);
497 M_DST(REG_FRESULT, REG_SP, USESTACK_PARAMS);
499 disp = dseg_add_address(cd, m);
500 M_ALD(rd->argintregs[0], REG_PV_CALLEE, disp);
502 M_MOV(REG_RESULT_CALLEE, rd->argintregs[1]);
503 M_DMOV(REG_FRESULT, 2); /* applies for flt and dbl values */
505 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
506 M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
507 M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
510 M_DLD(REG_FRESULT, REG_SP, USESTACK_PARAMS);
512 M_LDA(REG_SP, REG_SP, (1 + ABI_PARAMARRAY_SLOTS) * 8);
514 /* mark trace code */
518 #endif /* !defined(NDEBUG) */
522 * These are local overrides for various environment variables in Emacs.
523 * Please do not remove this and leave it at the end of the file, where
524 * Emacs will automagically detect them.
525 * ---------------------------------------------------------------------
528 * indent-tabs-mode: t
532 * vim:noexpandtab:sw=4:ts=4: