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
31 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
41 #include "vm/jit/alpha/codegen.h"
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/lock.h"
47 #include "vm/builtin.h"
48 #include "vm/jit/abi-asm.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/emit-common.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
56 /* emit_load *******************************************************************
58 Emits a possible load of an operand.
60 *******************************************************************************/
62 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
67 /* get required compiler data */
71 if (IS_INMEMORY(src->flags)) {
74 if (IS_FLT_DBL_TYPE(src->type))
75 M_DLD(tempreg, REG_SP, src->vv.regoff * 8);
77 M_LLD(tempreg, REG_SP, src->vv.regoff * 8);
88 /* emit_store ******************************************************************
90 Emit a possible store for the given variable.
92 *******************************************************************************/
94 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
98 /* get required compiler data */
102 if (IS_INMEMORY(dst->flags)) {
105 if (IS_FLT_DBL_TYPE(dst->type))
106 M_DST(d, REG_SP, dst->vv.regoff * 8);
108 M_LST(d, REG_SP, dst->vv.regoff * 8);
113 /* emit_copy *******************************************************************
115 Generates a register/memory to register/memory copy.
117 *******************************************************************************/
119 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
124 /* get required compiler data */
128 if ((src->vv.regoff != dst->vv.regoff) ||
129 ((src->flags ^ dst->flags) & INMEMORY)) {
131 /* If one of the variables resides in memory, we can eliminate
132 the register move from/to the temporary register with the
133 order of getting the destination register and the load. */
135 if (IS_INMEMORY(src->flags)) {
136 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
137 s1 = emit_load(jd, iptr, src, d);
140 s1 = emit_load(jd, iptr, src, REG_IFTMP);
141 d = codegen_reg_of_var(iptr->opc, dst, s1);
145 if (IS_FLT_DBL_TYPE(src->type))
151 emit_store(jd, iptr, dst, d);
156 /* emit_iconst *****************************************************************
160 *******************************************************************************/
162 void emit_iconst(codegendata *cd, s4 d, s4 value)
166 if ((value >= -32768) && (value <= 32767))
167 M_LDA_INTERN(d, REG_ZERO, value);
169 disp = dseg_adds4(cd, value);
170 M_ILD(d, REG_PV, disp);
175 /* emit_lconst *****************************************************************
179 *******************************************************************************/
181 void emit_lconst(codegendata *cd, s4 d, s8 value)
185 if ((value >= -32768) && (value <= 32767))
186 M_LDA_INTERN(d, REG_ZERO, value);
188 disp = dseg_adds8(cd, value);
189 M_LLD(d, REG_PV, disp);
194 /* emit_exception_stubs ********************************************************
196 Generates the code for the exception stubs.
198 *******************************************************************************/
200 void emit_exception_stubs(jitdata *jd)
208 /* get required compiler data */
213 /* generate exception stubs */
217 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
218 gen_resolvebranch(cd->mcodebase + eref->branchpos,
219 eref->branchpos, cd->mcodeptr - cd->mcodebase);
223 /* move index register into REG_ITMP1 */
225 /* Check if the exception is an
226 ArrayIndexOutOfBoundsException. If so, move index register
230 M_MOV(eref->reg, rd->argintregs[4]);
232 /* calcuate exception address */
234 M_LDA(rd->argintregs[3], REG_PV, eref->branchpos - 4);
236 /* move function to call into REG_ITMP3 */
238 disp = dseg_add_functionptr(cd, eref->function);
239 M_ALD(REG_ITMP3, REG_PV, disp);
241 if (targetdisp == 0) {
242 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
244 M_MOV(REG_PV, rd->argintregs[0]);
245 M_MOV(REG_SP, rd->argintregs[1]);
247 if (jd->isleafmethod)
248 M_MOV(REG_RA, rd->argintregs[2]);
250 M_ALD(rd->argintregs[2],
251 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
253 M_LDA(REG_SP, REG_SP, -2 * 8);
254 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
256 if (jd->isleafmethod)
257 M_AST(REG_RA, REG_SP, 1 * 8);
259 M_MOV(REG_ITMP3, REG_PV);
260 M_JSR(REG_RA, REG_PV);
261 disp = (s4) (cd->mcodeptr - cd->mcodebase);
262 M_LDA(REG_PV, REG_RA, -disp);
264 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
266 if (jd->isleafmethod)
267 M_ALD(REG_RA, REG_SP, 1 * 8);
269 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
270 M_LDA(REG_SP, REG_SP, 2 * 8);
272 disp = dseg_add_functionptr(cd, asm_handle_exception);
273 M_ALD(REG_ITMP3, REG_PV, disp);
274 M_JMP(REG_ZERO, REG_ITMP3);
277 disp = (((u4 *) cd->mcodebase) + targetdisp) -
278 (((u4 *) cd->mcodeptr) + 1);
286 /* emit_patcher_stubs **********************************************************
288 Generates the code for the patcher stubs.
290 *******************************************************************************/
292 void emit_patcher_stubs(jitdata *jd)
302 /* get required compiler data */
306 /* generate code patching stub call code */
310 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
311 /* check code segment size */
315 /* Get machine code which is patched back in later. The
316 call is 1 instruction word long. */
318 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
320 mcode = *((u4 *) tmpmcodeptr);
322 /* Patch in the call to call the following code (done at
325 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
326 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
328 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
329 M_BSR(REG_ITMP3, disp);
331 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
333 /* create stack frame */
335 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
337 /* move return address onto stack */
339 M_AST(REG_ITMP3, REG_SP, 5 * 8);
341 /* move pointer to java_objectheader onto stack */
343 #if defined(ENABLE_THREADS)
344 /* create a virtual java_objectheader */
346 (void) dseg_add_unique_address(cd, NULL); /* flcword */
347 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
348 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
350 M_LDA(REG_ITMP3, REG_PV, disp);
351 M_AST(REG_ITMP3, REG_SP, 4 * 8);
356 /* move machine code onto stack */
358 disp = dseg_add_s4(cd, mcode);
359 M_ILD(REG_ITMP3, REG_PV, disp);
360 M_IST(REG_ITMP3, REG_SP, 3 * 8);
362 /* move class/method/field reference onto stack */
364 disp = dseg_add_address(cd, pref->ref);
365 M_ALD(REG_ITMP3, REG_PV, disp);
366 M_AST(REG_ITMP3, REG_SP, 2 * 8);
368 /* move data segment displacement onto stack */
370 disp = dseg_add_s4(cd, pref->disp);
371 M_ILD(REG_ITMP3, REG_PV, disp);
372 M_IST(REG_ITMP3, REG_SP, 1 * 8);
374 /* move patcher function pointer onto stack */
376 disp = dseg_add_functionptr(cd, pref->patcher);
377 M_ALD(REG_ITMP3, REG_PV, disp);
378 M_AST(REG_ITMP3, REG_SP, 0 * 8);
380 if (targetdisp == 0) {
381 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
383 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
384 M_ALD(REG_ITMP3, REG_PV, disp);
385 M_JMP(REG_ZERO, REG_ITMP3);
388 disp = (((u4 *) cd->mcodebase) + targetdisp) -
389 (((u4 *) cd->mcodeptr) + 1);
397 /* emit_replacement_stubs ******************************************************
399 Generates the code for the replacement stubs.
401 *******************************************************************************/
403 void emit_replacement_stubs(jitdata *jd)
412 /* get required compiler data */
417 rplp = code->rplpoints;
419 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
420 /* check code segment size */
424 /* note start of stub code */
426 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
428 /* make machine code for patching */
430 savedmcodeptr = cd->mcodeptr;
431 cd->mcodeptr = (u1 *) &(rplp->mcode);
433 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
436 cd->mcodeptr = savedmcodeptr;
438 /* create stack frame - 16-byte aligned */
440 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
442 /* push address of `rplpoint` struct */
444 disp = dseg_add_address(cd, rplp);
445 M_ALD(REG_ITMP3, REG_PV, disp);
446 M_AST(REG_ITMP3, REG_SP, 0 * 8);
448 /* jump to replacement function */
450 disp = dseg_add_functionptr(cd, asm_replacement_out);
451 M_ALD(REG_ITMP3, REG_PV, disp);
452 M_JMP(REG_ZERO, REG_ITMP3);
457 /* emit_verbosecall_enter ******************************************************
459 Generates the code for the call trace.
461 *******************************************************************************/
464 void emit_verbosecall_enter(jitdata *jd)
473 /* get required compiler data */
481 /* mark trace code */
485 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
486 M_AST(REG_RA, REG_SP, 1 * 8);
488 /* save argument registers */
490 for (i = 0; i < INT_ARG_CNT; i++)
491 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
493 for (i = 0; i < FLT_ARG_CNT; i++)
494 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
496 /* save temporary registers for leaf methods */
498 if (jd->isleafmethod) {
499 for (i = 0; i < INT_TMP_CNT; i++)
500 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
502 for (i = 0; i < FLT_TMP_CNT; i++)
503 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
506 /* load float arguments into integer registers */
508 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
509 t = md->paramtypes[i].type;
511 if (IS_FLT_DBL_TYPE(t)) {
512 if (IS_2_WORD_TYPE(t)) {
513 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
514 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
517 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
518 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
523 disp = dseg_add_address(cd, m);
524 M_ALD(REG_ITMP1, REG_PV, disp);
525 M_AST(REG_ITMP1, REG_SP, 0 * 8);
526 disp = dseg_add_functionptr(cd, builtin_trace_args);
527 M_ALD(REG_PV, REG_PV, disp);
528 M_JSR(REG_RA, REG_PV);
529 disp = (s4) (cd->mcodeptr - cd->mcodebase);
530 M_LDA(REG_PV, REG_RA, -disp);
531 M_ALD(REG_RA, REG_SP, 1 * 8);
533 /* restore argument registers */
535 for (i = 0; i < INT_ARG_CNT; i++)
536 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
538 for (i = 0; i < FLT_ARG_CNT; i++)
539 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
541 /* restore temporary registers for leaf methods */
543 if (jd->isleafmethod) {
544 for (i = 0; i < INT_TMP_CNT; i++)
545 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
547 for (i = 0; i < FLT_TMP_CNT; i++)
548 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
551 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
553 /* mark trace code */
557 #endif /* !defined(NDEBUG) */
560 /* emit_verbosecall_exit *******************************************************
562 Generates the code for the call trace.
564 *******************************************************************************/
567 void emit_verbosecall_exit(jitdata *jd)
574 /* get required compiler data */
580 /* mark trace code */
584 M_LDA(REG_SP, REG_SP, -4 * 8);
585 M_AST(REG_RA, REG_SP, 0 * 8);
587 M_LST(REG_RESULT, REG_SP, 1 * 8);
588 M_DST(REG_FRESULT, REG_SP, 2 * 8);
590 disp = dseg_add_address(cd, m);
591 M_ALD(rd->argintregs[0], REG_PV, disp);
593 M_MOV(REG_RESULT, rd->argintregs[1]);
594 M_FLTMOVE(REG_FRESULT, rd->argfltregs[2]);
595 M_FLTMOVE(REG_FRESULT, rd->argfltregs[3]);
597 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
598 M_ALD(REG_PV, REG_PV, disp);
599 M_JSR(REG_RA, REG_PV);
600 disp = (cd->mcodeptr - cd->mcodebase);
601 M_LDA(REG_PV, REG_RA, -disp);
603 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
604 M_LLD(REG_RESULT, REG_SP, 1 * 8);
606 M_ALD(REG_RA, REG_SP, 0 * 8);
607 M_LDA(REG_SP, REG_SP, 4 * 8);
609 /* mark trace code */
613 #endif /* !defined(NDEBUG) */
617 * These are local overrides for various environment variables in Emacs.
618 * Please do not remove this and leave it at the end of the file, where
619 * Emacs will automagically detect them.
620 * ---------------------------------------------------------------------
623 * indent-tabs-mode: t
627 * vim:noexpandtab:sw=4:ts=4: