1 /* src/vm/jit/alpha/emit.c - Alpha 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
29 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
39 #include "vm/jit/alpha/codegen.h"
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
45 #include "vm/builtin.h"
46 #include "vm/options.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"
55 /* emit_load *******************************************************************
57 Emits a possible load of an operand.
59 *******************************************************************************/
61 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 /* get required compiler data */
70 if (IS_INMEMORY(src->flags)) {
73 if (IS_FLT_DBL_TYPE(src->type))
74 M_DLD(tempreg, REG_SP, src->vv.regoff * 8);
76 M_LLD(tempreg, REG_SP, src->vv.regoff * 8);
87 /* emit_store ******************************************************************
89 Emit a possible store for the given variable.
91 *******************************************************************************/
93 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
97 /* get required compiler data */
101 if (IS_INMEMORY(dst->flags)) {
104 if (IS_FLT_DBL_TYPE(dst->type))
105 M_DST(d, REG_SP, dst->vv.regoff * 8);
107 M_LST(d, REG_SP, dst->vv.regoff * 8);
112 /* emit_copy *******************************************************************
114 Generates a register/memory to register/memory copy.
116 *******************************************************************************/
118 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
123 /* get required compiler data */
127 if ((src->vv.regoff != dst->vv.regoff) ||
128 ((src->flags ^ dst->flags) & INMEMORY)) {
130 /* If one of the variables resides in memory, we can eliminate
131 the register move from/to the temporary register with the
132 order of getting the destination register and the load. */
134 if (IS_INMEMORY(src->flags)) {
135 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
136 s1 = emit_load(jd, iptr, src, d);
139 s1 = emit_load(jd, iptr, src, REG_IFTMP);
140 d = codegen_reg_of_var(iptr->opc, dst, s1);
144 if (IS_FLT_DBL_TYPE(src->type))
150 emit_store(jd, iptr, dst, d);
155 /* emit_iconst *****************************************************************
159 *******************************************************************************/
161 void emit_iconst(codegendata *cd, s4 d, s4 value)
165 if ((value >= -32768) && (value <= 32767))
166 M_LDA_INTERN(d, REG_ZERO, value);
168 disp = dseg_add_s4(cd, value);
169 M_ILD(d, REG_PV, disp);
174 /* emit_lconst *****************************************************************
178 *******************************************************************************/
180 void emit_lconst(codegendata *cd, s4 d, s8 value)
184 if ((value >= -32768) && (value <= 32767))
185 M_LDA_INTERN(d, REG_ZERO, value);
187 disp = dseg_add_s8(cd, value);
188 M_LLD(d, REG_PV, disp);
193 /* emit_arrayindexoutofbounds_check ********************************************
195 Emit an ArrayIndexOutOfBoundsException check.
197 *******************************************************************************/
199 void emit_arrayindexoutofbounds_check(codegendata *cd, s4 s1, s4 s2)
202 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
203 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
204 M_BEQZ(REG_ITMP3, 0);
205 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
210 /* emit_arraystore_check *******************************************************
212 Emit an ArrayStoreException check.
214 *******************************************************************************/
216 void emit_arraystore_check(codegendata *cd, s4 reg)
219 codegen_add_arraystoreexception_ref(cd);
223 /* emit_classcast_check ********************************************************
225 Emit a ClassCastException check.
227 *******************************************************************************/
229 void emit_classcast_check(codegendata *cd, s4 condition, s4 reg, s4 s1)
232 codegen_add_classcastexception_ref(cd, s1);
236 /* emit_nullpointer_check ******************************************************
238 Emit a NullPointerException check.
240 *******************************************************************************/
242 void emit_nullpointer_check(codegendata *cd, s4 reg)
246 codegen_add_nullpointerexception_ref(cd);
251 /* emit_exception_stubs ********************************************************
253 Generates the code for the exception stubs.
255 *******************************************************************************/
257 void emit_exception_stubs(jitdata *jd)
267 /* get required compiler data */
272 /* generate exception stubs */
276 for (er = cd->exceptionrefs; er != NULL; er = er->next) {
277 /* back-patch the branch to this exception code */
279 branchmpc = er->branchpos;
280 targetmpc = cd->mcodeptr - cd->mcodebase;
282 md_codegen_patch_branch(cd, branchmpc, targetmpc);
286 /* move index register into REG_ITMP1 */
288 /* Check if the exception is an
289 ArrayIndexOutOfBoundsException. If so, move index register
293 M_MOV(er->reg, rd->argintregs[4]);
295 /* calcuate exception address */
297 M_LDA(rd->argintregs[3], REG_PV, er->branchpos - 4);
299 /* move function to call into REG_ITMP3 */
301 disp = dseg_add_functionptr(cd, er->function);
302 M_ALD(REG_ITMP3, REG_PV, disp);
304 if (targetdisp == 0) {
305 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
307 M_MOV(REG_PV, rd->argintregs[0]);
308 M_MOV(REG_SP, rd->argintregs[1]);
310 if (jd->isleafmethod)
311 M_MOV(REG_RA, rd->argintregs[2]);
313 M_ALD(rd->argintregs[2],
314 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
316 M_LDA(REG_SP, REG_SP, -2 * 8);
317 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
319 if (jd->isleafmethod)
320 M_AST(REG_RA, REG_SP, 1 * 8);
322 M_MOV(REG_ITMP3, REG_PV);
323 M_JSR(REG_RA, REG_PV);
324 disp = (s4) (cd->mcodeptr - cd->mcodebase);
325 M_LDA(REG_PV, REG_RA, -disp);
327 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
329 if (jd->isleafmethod)
330 M_ALD(REG_RA, REG_SP, 1 * 8);
332 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
333 M_LDA(REG_SP, REG_SP, 2 * 8);
335 disp = dseg_add_functionptr(cd, asm_handle_exception);
336 M_ALD(REG_ITMP3, REG_PV, disp);
337 M_JMP(REG_ZERO, REG_ITMP3);
340 disp = (((u4 *) cd->mcodebase) + targetdisp) -
341 (((u4 *) cd->mcodeptr) + 1);
349 /* emit_patcher_stubs **********************************************************
351 Generates the code for the patcher stubs.
353 *******************************************************************************/
355 void emit_patcher_stubs(jitdata *jd)
365 /* get required compiler data */
369 /* generate code patching stub call code */
373 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
374 /* check code segment size */
378 /* Get machine code which is patched back in later. The
379 call is 1 instruction word long. */
381 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
383 mcode = *((u4 *) tmpmcodeptr);
385 /* Patch in the call to call the following code (done at
388 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
389 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
391 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
392 M_BSR(REG_ITMP3, disp);
394 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
396 /* create stack frame */
398 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
400 /* move return address onto stack */
402 M_AST(REG_ITMP3, REG_SP, 5 * 8);
404 /* move pointer to java_objectheader onto stack */
406 #if defined(ENABLE_THREADS)
407 /* create a virtual java_objectheader */
409 (void) dseg_add_unique_address(cd, NULL); /* flcword */
410 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
411 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
413 M_LDA(REG_ITMP3, REG_PV, disp);
414 M_AST(REG_ITMP3, REG_SP, 4 * 8);
419 /* move machine code onto stack */
421 disp = dseg_add_s4(cd, mcode);
422 M_ILD(REG_ITMP3, REG_PV, disp);
423 M_IST(REG_ITMP3, REG_SP, 3 * 8);
425 /* move class/method/field reference onto stack */
427 disp = dseg_add_address(cd, pref->ref);
428 M_ALD(REG_ITMP3, REG_PV, disp);
429 M_AST(REG_ITMP3, REG_SP, 2 * 8);
431 /* move data segment displacement onto stack */
433 disp = dseg_add_s4(cd, pref->disp);
434 M_ILD(REG_ITMP3, REG_PV, disp);
435 M_IST(REG_ITMP3, REG_SP, 1 * 8);
437 /* move patcher function pointer onto stack */
439 disp = dseg_add_functionptr(cd, pref->patcher);
440 M_ALD(REG_ITMP3, REG_PV, disp);
441 M_AST(REG_ITMP3, REG_SP, 0 * 8);
443 if (targetdisp == 0) {
444 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
446 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
447 M_ALD(REG_ITMP3, REG_PV, disp);
448 M_JMP(REG_ZERO, REG_ITMP3);
451 disp = (((u4 *) cd->mcodebase) + targetdisp) -
452 (((u4 *) cd->mcodeptr) + 1);
460 /* emit_replacement_stubs ******************************************************
462 Generates the code for the replacement stubs.
464 *******************************************************************************/
466 void emit_replacement_stubs(jitdata *jd)
476 /* get required compiler data */
481 rplp = code->rplpoints;
483 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
484 /* check code segment size */
488 /* note start of stub code */
490 outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
492 /* make machine code for patching */
494 savedmcodeptr = cd->mcodeptr;
495 cd->mcodeptr = (u1 *) &(rplp->mcode);
497 disp = (ptrint) ((s4 *) outcode - (s4 *) rplp->pc) - 1;
500 cd->mcodeptr = savedmcodeptr;
502 /* create stack frame - 16-byte aligned */
504 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
506 /* push address of `rplpoint` struct */
508 disp = dseg_add_address(cd, rplp);
509 M_ALD(REG_ITMP3, REG_PV, disp);
510 M_AST(REG_ITMP3, REG_SP, 0 * 8);
512 /* jump to replacement function */
514 disp = dseg_add_functionptr(cd, asm_replacement_out);
515 M_ALD(REG_ITMP3, REG_PV, disp);
516 M_JMP(REG_ZERO, REG_ITMP3);
521 /* emit_verbosecall_enter ******************************************************
523 Generates the code for the call trace.
525 *******************************************************************************/
528 void emit_verbosecall_enter(jitdata *jd)
537 /* get required compiler data */
545 /* mark trace code */
549 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
550 M_AST(REG_RA, REG_SP, 1 * 8);
552 /* save argument registers */
554 for (i = 0; i < INT_ARG_CNT; i++)
555 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
557 for (i = 0; i < FLT_ARG_CNT; i++)
558 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
560 /* save temporary registers for leaf methods */
562 if (jd->isleafmethod) {
563 for (i = 0; i < INT_TMP_CNT; i++)
564 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
566 for (i = 0; i < FLT_TMP_CNT; i++)
567 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
570 /* load float arguments into integer registers */
572 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
573 t = md->paramtypes[i].type;
575 if (IS_FLT_DBL_TYPE(t)) {
576 if (IS_2_WORD_TYPE(t)) {
577 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
578 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
581 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
582 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
587 disp = dseg_add_address(cd, m);
588 M_ALD(REG_ITMP1, REG_PV, disp);
589 M_AST(REG_ITMP1, REG_SP, 0 * 8);
590 disp = dseg_add_functionptr(cd, builtin_trace_args);
591 M_ALD(REG_PV, REG_PV, disp);
592 M_JSR(REG_RA, REG_PV);
593 disp = (s4) (cd->mcodeptr - cd->mcodebase);
594 M_LDA(REG_PV, REG_RA, -disp);
595 M_ALD(REG_RA, REG_SP, 1 * 8);
597 /* restore argument registers */
599 for (i = 0; i < INT_ARG_CNT; i++)
600 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
602 for (i = 0; i < FLT_ARG_CNT; i++)
603 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
605 /* restore temporary registers for leaf methods */
607 if (jd->isleafmethod) {
608 for (i = 0; i < INT_TMP_CNT; i++)
609 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
611 for (i = 0; i < FLT_TMP_CNT; i++)
612 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
615 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
617 /* mark trace code */
621 #endif /* !defined(NDEBUG) */
624 /* emit_verbosecall_exit *******************************************************
626 Generates the code for the call trace.
628 *******************************************************************************/
631 void emit_verbosecall_exit(jitdata *jd)
638 /* get required compiler data */
644 /* mark trace code */
648 M_LDA(REG_SP, REG_SP, -4 * 8);
649 M_AST(REG_RA, REG_SP, 0 * 8);
651 M_LST(REG_RESULT, REG_SP, 1 * 8);
652 M_DST(REG_FRESULT, REG_SP, 2 * 8);
654 disp = dseg_add_address(cd, m);
655 M_ALD(rd->argintregs[0], REG_PV, disp);
657 M_MOV(REG_RESULT, rd->argintregs[1]);
658 M_FLTMOVE(REG_FRESULT, rd->argfltregs[2]);
659 M_FLTMOVE(REG_FRESULT, rd->argfltregs[3]);
661 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
662 M_ALD(REG_PV, REG_PV, disp);
663 M_JSR(REG_RA, REG_PV);
664 disp = (cd->mcodeptr - cd->mcodebase);
665 M_LDA(REG_PV, REG_RA, -disp);
667 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
668 M_LLD(REG_RESULT, REG_SP, 1 * 8);
670 M_ALD(REG_RA, REG_SP, 0 * 8);
671 M_LDA(REG_SP, REG_SP, 4 * 8);
673 /* mark trace code */
677 #endif /* !defined(NDEBUG) */
681 * These are local overrides for various environment variables in Emacs.
682 * Please do not remove this and leave it at the end of the file, where
683 * Emacs will automagically detect them.
684 * ---------------------------------------------------------------------
687 * indent-tabs-mode: t
691 * vim:noexpandtab:sw=4:ts=4: