1 /* src/vm/jit/alpha/emit.c - Alpha 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 $
37 #include "vm/jit/alpha/codegen.h"
39 #include "mm/memory.h"
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
45 #include "vm/builtin.h"
47 #include "vm/jit/abi-asm.h"
48 #include "vm/jit/asmpart.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 #include "vmcore/options.h"
57 /* emit_load *******************************************************************
59 Emits a possible load of an operand.
61 *******************************************************************************/
63 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
68 /* get required compiler data */
72 if (IS_INMEMORY(src->flags)) {
75 if (IS_FLT_DBL_TYPE(src->type))
76 M_DLD(tempreg, REG_SP, src->vv.regoff * 8);
78 M_LLD(tempreg, REG_SP, src->vv.regoff * 8);
89 /* emit_store ******************************************************************
91 Emit a possible store for the given variable.
93 *******************************************************************************/
95 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
99 /* get required compiler data */
103 if (IS_INMEMORY(dst->flags)) {
106 if (IS_FLT_DBL_TYPE(dst->type))
107 M_DST(d, REG_SP, dst->vv.regoff * 8);
109 M_LST(d, REG_SP, dst->vv.regoff * 8);
114 /* emit_copy *******************************************************************
116 Generates a register/memory to register/memory copy.
118 *******************************************************************************/
120 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
125 /* get required compiler data */
129 if ((src->vv.regoff != dst->vv.regoff) ||
130 ((src->flags ^ dst->flags) & INMEMORY)) {
132 /* If one of the variables resides in memory, we can eliminate
133 the register move from/to the temporary register with the
134 order of getting the destination register and the load. */
136 if (IS_INMEMORY(src->flags)) {
137 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
138 s1 = emit_load(jd, iptr, src, d);
141 s1 = emit_load(jd, iptr, src, REG_IFTMP);
142 d = codegen_reg_of_var(iptr->opc, dst, s1);
146 if (IS_FLT_DBL_TYPE(src->type))
152 emit_store(jd, iptr, dst, d);
157 /* emit_iconst *****************************************************************
161 *******************************************************************************/
163 void emit_iconst(codegendata *cd, s4 d, s4 value)
167 if ((value >= -32768) && (value <= 32767))
168 M_LDA_INTERN(d, REG_ZERO, value);
170 disp = dseg_add_s4(cd, value);
171 M_ILD(d, REG_PV, disp);
176 /* emit_lconst *****************************************************************
180 *******************************************************************************/
182 void emit_lconst(codegendata *cd, s4 d, s8 value)
186 if ((value >= -32768) && (value <= 32767))
187 M_LDA_INTERN(d, REG_ZERO, value);
189 disp = dseg_add_s8(cd, value);
190 M_LLD(d, REG_PV, disp);
195 /* emit_arrayindexoutofbounds_check ********************************************
197 Emit an ArrayIndexOutOfBoundsException check.
199 *******************************************************************************/
201 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
203 if (INSTRUCTION_MUST_CHECK(iptr)) {
204 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
205 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
206 M_BEQZ(REG_ITMP3, 0);
207 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
212 /* emit_arraystore_check *******************************************************
214 Emit an ArrayStoreException check.
216 *******************************************************************************/
218 void emit_arraystore_check(codegendata *cd, instruction *iptr, s4 reg)
221 codegen_add_arraystoreexception_ref(cd);
225 /* emit_classcast_check ********************************************************
227 Emit a ClassCastException check.
229 *******************************************************************************/
231 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
233 if (INSTRUCTION_MUST_CHECK(iptr)) {
235 codegen_add_classcastexception_ref(cd, s1);
240 /* emit_nullpointer_check ******************************************************
242 Emit a NullPointerException check.
244 *******************************************************************************/
246 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
248 if (INSTRUCTION_MUST_CHECK(iptr)) {
250 codegen_add_nullpointerexception_ref(cd);
255 /* emit_exception_stubs ********************************************************
257 Generates the code for the exception stubs.
259 *******************************************************************************/
261 void emit_exception_stubs(jitdata *jd)
271 /* get required compiler data */
276 /* generate exception stubs */
280 for (er = cd->exceptionrefs; er != NULL; er = er->next) {
281 /* back-patch the branch to this exception code */
283 branchmpc = er->branchpos;
284 targetmpc = cd->mcodeptr - cd->mcodebase;
286 md_codegen_patch_branch(cd, branchmpc, targetmpc);
290 /* move index register into REG_ITMP1 */
292 /* Check if the exception is an
293 ArrayIndexOutOfBoundsException. If so, move index register
297 M_MOV(er->reg, rd->argintregs[4]);
299 /* calcuate exception address */
301 M_LDA(rd->argintregs[3], REG_PV, er->branchpos - 4);
303 /* move function to call into REG_ITMP3 */
305 disp = dseg_add_functionptr(cd, er->function);
306 M_ALD(REG_ITMP3, REG_PV, disp);
308 if (targetdisp == 0) {
309 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
311 M_MOV(REG_PV, rd->argintregs[0]);
312 M_MOV(REG_SP, rd->argintregs[1]);
314 if (jd->isleafmethod)
315 M_MOV(REG_RA, rd->argintregs[2]);
317 M_ALD(rd->argintregs[2],
318 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
320 M_LDA(REG_SP, REG_SP, -2 * 8);
321 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
323 if (jd->isleafmethod)
324 M_AST(REG_RA, REG_SP, 1 * 8);
326 M_MOV(REG_ITMP3, REG_PV);
327 M_JSR(REG_RA, REG_PV);
328 disp = (s4) (cd->mcodeptr - cd->mcodebase);
329 M_LDA(REG_PV, REG_RA, -disp);
331 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
333 if (jd->isleafmethod)
334 M_ALD(REG_RA, REG_SP, 1 * 8);
336 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
337 M_LDA(REG_SP, REG_SP, 2 * 8);
339 disp = dseg_add_functionptr(cd, asm_handle_exception);
340 M_ALD(REG_ITMP3, REG_PV, disp);
341 M_JMP(REG_ZERO, REG_ITMP3);
344 disp = (((u4 *) cd->mcodebase) + targetdisp) -
345 (((u4 *) cd->mcodeptr) + 1);
353 /* emit_patcher_stubs **********************************************************
355 Generates the code for the patcher stubs.
357 *******************************************************************************/
359 void emit_patcher_stubs(jitdata *jd)
369 /* get required compiler data */
373 /* generate code patching stub call code */
377 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
378 /* check code segment size */
382 /* Get machine code which is patched back in later. The
383 call is 1 instruction word long. */
385 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
387 mcode = *((u4 *) tmpmcodeptr);
389 /* Patch in the call to call the following code (done at
392 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
393 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
395 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
396 M_BSR(REG_ITMP3, disp);
398 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
400 /* create stack frame */
402 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
404 /* move return address onto stack */
406 M_AST(REG_ITMP3, REG_SP, 5 * 8);
408 /* move pointer to java_objectheader onto stack */
410 #if defined(ENABLE_THREADS)
411 /* create a virtual java_objectheader */
413 (void) dseg_add_unique_address(cd, NULL); /* flcword */
414 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
415 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
417 M_LDA(REG_ITMP3, REG_PV, disp);
418 M_AST(REG_ITMP3, REG_SP, 4 * 8);
423 /* move machine code onto stack */
425 disp = dseg_add_s4(cd, mcode);
426 M_ILD(REG_ITMP3, REG_PV, disp);
427 M_IST(REG_ITMP3, REG_SP, 3 * 8);
429 /* move class/method/field reference onto stack */
431 disp = dseg_add_address(cd, pref->ref);
432 M_ALD(REG_ITMP3, REG_PV, disp);
433 M_AST(REG_ITMP3, REG_SP, 2 * 8);
435 /* move data segment displacement onto stack */
437 disp = dseg_add_s4(cd, pref->disp);
438 M_ILD(REG_ITMP3, REG_PV, disp);
439 M_IST(REG_ITMP3, REG_SP, 1 * 8);
441 /* move patcher function pointer onto stack */
443 disp = dseg_add_functionptr(cd, pref->patcher);
444 M_ALD(REG_ITMP3, REG_PV, disp);
445 M_AST(REG_ITMP3, REG_SP, 0 * 8);
447 if (targetdisp == 0) {
448 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
450 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
451 M_ALD(REG_ITMP3, REG_PV, disp);
452 M_JMP(REG_ZERO, REG_ITMP3);
455 disp = (((u4 *) cd->mcodebase) + targetdisp) -
456 (((u4 *) cd->mcodeptr) + 1);
464 /* emit_replacement_stubs ******************************************************
466 Generates the code for the replacement stubs.
468 *******************************************************************************/
470 #if defined(ENABLE_REPLACEMENT)
471 void emit_replacement_stubs(jitdata *jd)
482 /* get required compiler data */
487 rplp = code->rplpoints;
489 /* store beginning of replacement stubs */
491 code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
493 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
494 /* do not generate stubs for non-trappable points */
496 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
499 /* check code segment size */
504 savedmcodeptr = cd->mcodeptr;
507 /* create stack frame - 16-byte aligned */
509 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
511 /* push address of `rplpoint` struct */
513 disp = dseg_add_address(cd, rplp);
514 M_ALD(REG_ITMP3, REG_PV, disp);
515 M_AST(REG_ITMP3, REG_SP, 0 * 8);
517 /* jump to replacement function */
519 disp = dseg_add_functionptr(cd, asm_replacement_out);
520 M_ALD(REG_ITMP3, REG_PV, disp);
521 M_JMP(REG_ZERO, REG_ITMP3);
523 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
526 #endif /* defined(ENABLE_REPLACEMENT) */
529 /* emit_verbosecall_enter ******************************************************
531 Generates the code for the call trace.
533 *******************************************************************************/
536 void emit_verbosecall_enter(jitdata *jd)
545 /* get required compiler data */
553 /* mark trace code */
557 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
558 M_AST(REG_RA, REG_SP, 1 * 8);
560 /* save argument registers */
562 for (i = 0; i < INT_ARG_CNT; i++)
563 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
565 for (i = 0; i < FLT_ARG_CNT; i++)
566 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
568 /* save temporary registers for leaf methods */
570 if (jd->isleafmethod) {
571 for (i = 0; i < INT_TMP_CNT; i++)
572 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
574 for (i = 0; i < FLT_TMP_CNT; i++)
575 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
578 /* load float arguments into integer registers */
580 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
581 t = md->paramtypes[i].type;
583 if (IS_FLT_DBL_TYPE(t)) {
584 if (IS_2_WORD_TYPE(t)) {
585 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
586 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
589 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
590 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
595 disp = dseg_add_address(cd, m);
596 M_ALD(REG_ITMP1, REG_PV, disp);
597 M_AST(REG_ITMP1, REG_SP, 0 * 8);
598 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
599 M_ALD(REG_PV, REG_PV, disp);
600 M_JSR(REG_RA, REG_PV);
601 disp = (s4) (cd->mcodeptr - cd->mcodebase);
602 M_LDA(REG_PV, REG_RA, -disp);
603 M_ALD(REG_RA, REG_SP, 1 * 8);
605 /* restore argument registers */
607 for (i = 0; i < INT_ARG_CNT; i++)
608 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
610 for (i = 0; i < FLT_ARG_CNT; i++)
611 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
613 /* restore temporary registers for leaf methods */
615 if (jd->isleafmethod) {
616 for (i = 0; i < INT_TMP_CNT; i++)
617 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
619 for (i = 0; i < FLT_TMP_CNT; i++)
620 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
623 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
625 /* mark trace code */
629 #endif /* !defined(NDEBUG) */
632 /* emit_verbosecall_exit *******************************************************
634 Generates the code for the call trace.
636 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
638 *******************************************************************************/
641 void emit_verbosecall_exit(jitdata *jd)
648 /* get required compiler data */
654 /* mark trace code */
658 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
659 M_AST(REG_RA, REG_SP, 0 * 8);
661 M_LST(REG_RESULT, REG_SP, 1 * 8);
662 M_DST(REG_FRESULT, REG_SP, 2 * 8);
664 M_MOV(REG_RESULT, REG_A0);
665 M_FMOV(REG_FRESULT, REG_FA1);
666 M_FMOV(REG_FRESULT, REG_FA2);
668 disp = dseg_add_address(cd, m);
669 M_ALD(REG_A3, REG_PV, disp);
671 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
672 M_ALD(REG_PV, REG_PV, disp);
673 M_JSR(REG_RA, REG_PV);
674 disp = (cd->mcodeptr - cd->mcodebase);
675 M_LDA(REG_PV, REG_RA, -disp);
677 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
678 M_LLD(REG_RESULT, REG_SP, 1 * 8);
680 M_ALD(REG_RA, REG_SP, 0 * 8);
681 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
683 /* mark trace code */
687 #endif /* !defined(NDEBUG) */
691 * These are local overrides for various environment variables in Emacs.
692 * Please do not remove this and leave it at the end of the file, where
693 * Emacs will automagically detect them.
694 * ---------------------------------------------------------------------
697 * indent-tabs-mode: t
701 * vim:noexpandtab:sw=4:ts=4: