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.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
56 /* code generation functions **************************************************/
58 /* emit_load_s1 ****************************************************************
60 Emits a possible load of the first source operand.
62 *******************************************************************************/
64 s4 emit_load_s1(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
69 /* get required compiler data */
73 if (src->flags & INMEMORY) {
76 if (IS_FLT_DBL_TYPE(src->type))
77 M_DLD(tempreg, REG_SP, src->regoff * 8);
79 M_LLD(tempreg, REG_SP, src->regoff * 8);
89 /* emit_load_s2 ****************************************************************
91 Emits a possible load of the second source operand.
93 *******************************************************************************/
95 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
100 /* get required compiler data */
104 if (src->flags & INMEMORY) {
107 if (IS_FLT_DBL_TYPE(src->type))
108 M_DLD(tempreg, REG_SP, src->regoff * 8);
110 M_LLD(tempreg, REG_SP, src->regoff * 8);
120 /* emit_load_s3 ****************************************************************
122 Emits a possible load of the third source operand.
124 *******************************************************************************/
126 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
131 /* get required compiler data */
135 if (src->flags & INMEMORY) {
138 if (IS_FLT_DBL_TYPE(src->type))
139 M_DLD(tempreg, REG_SP, src->regoff * 8);
141 M_LLD(tempreg, REG_SP, src->regoff * 8);
151 /* emit_store ******************************************************************
155 *******************************************************************************/
157 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
161 /* get required compiler data */
165 if (dst->flags & INMEMORY) {
168 if (IS_FLT_DBL_TYPE(dst->type))
169 M_DST(d, REG_SP, dst->regoff * 8);
171 M_LST(d, REG_SP, dst->regoff * 8);
176 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
182 /* get required compiler data */
187 if ((src->regoff != dst->regoff) ||
188 ((src->flags ^ dst->flags) & INMEMORY)) {
189 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
190 s1 = emit_load_s1(jd, iptr, src, d);
192 if (IS_FLT_DBL_TYPE(src->type))
197 emit_store(jd, iptr, dst, d);
202 void emit_iconst(codegendata *cd, s4 d, s4 value)
206 if ((value >= -32768) && (value <= 32767)) {
207 M_LDA_INTERN(d, REG_ZERO, value);
209 disp = dseg_adds4(cd, value);
210 M_ILD(d, REG_PV, disp);
215 void emit_lconst(codegendata *cd, s4 d, s8 value)
219 if ((value >= -32768) && (value <= 32767)) {
220 M_LDA_INTERN(d, REG_ZERO, value);
222 disp = dseg_adds8(cd, value);
223 M_LLD(d, REG_PV, disp);
228 /* emit_exception_stubs ********************************************************
230 Generates the code for the exception stubs.
232 *******************************************************************************/
234 void emit_exception_stubs(jitdata *jd)
242 /* get required compiler data */
247 /* generate exception stubs */
251 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
252 gen_resolvebranch(cd->mcodebase + eref->branchpos,
253 eref->branchpos, cd->mcodeptr - cd->mcodebase);
257 /* move index register into REG_ITMP1 */
259 /* Check if the exception is an
260 ArrayIndexOutOfBoundsException. If so, move index register
264 M_MOV(eref->reg, rd->argintregs[4]);
266 /* calcuate exception address */
268 M_LDA(rd->argintregs[3], REG_PV, eref->branchpos - 4);
270 /* move function to call into REG_ITMP3 */
272 disp = dseg_add_functionptr(cd, eref->function);
273 M_ALD(REG_ITMP3, REG_PV, disp);
275 if (targetdisp == 0) {
276 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
278 M_MOV(REG_PV, rd->argintregs[0]);
279 M_MOV(REG_SP, rd->argintregs[1]);
281 if (jd->isleafmethod)
282 M_MOV(REG_RA, rd->argintregs[2]);
284 M_ALD(rd->argintregs[2],
285 REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
287 M_LDA(REG_SP, REG_SP, -2 * 8);
288 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
290 if (jd->isleafmethod)
291 M_AST(REG_RA, REG_SP, 1 * 8);
293 M_MOV(REG_ITMP3, REG_PV);
294 M_JSR(REG_RA, REG_PV);
295 disp = (s4) (cd->mcodeptr - cd->mcodebase);
296 M_LDA(REG_PV, REG_RA, -disp);
298 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
300 if (jd->isleafmethod)
301 M_ALD(REG_RA, REG_SP, 1 * 8);
303 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
304 M_LDA(REG_SP, REG_SP, 2 * 8);
306 disp = dseg_add_functionptr(cd, asm_handle_exception);
307 M_ALD(REG_ITMP3, REG_PV, disp);
308 M_JMP(REG_ZERO, REG_ITMP3);
311 disp = (((u4 *) cd->mcodebase) + targetdisp) -
312 (((u4 *) cd->mcodeptr) + 1);
320 /* emit_patcher_stubs **********************************************************
322 Generates the code for the patcher stubs.
324 *******************************************************************************/
326 void emit_patcher_stubs(jitdata *jd)
336 /* get required compiler data */
340 /* generate code patching stub call code */
344 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
345 /* check code segment size */
349 /* Get machine code which is patched back in later. The
350 call is 1 instruction word long. */
352 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
354 mcode = *((u4 *) tmpmcodeptr);
356 /* Patch in the call to call the following code (done at
359 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
360 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
362 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
363 M_BSR(REG_ITMP3, disp);
365 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
367 /* create stack frame */
369 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
371 /* move return address onto stack */
373 M_AST(REG_ITMP3, REG_SP, 5 * 8);
375 /* move pointer to java_objectheader onto stack */
377 #if defined(ENABLE_THREADS)
378 /* create a virtual java_objectheader */
380 (void) dseg_add_unique_address(cd, NULL); /* flcword */
381 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
382 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
384 M_LDA(REG_ITMP3, REG_PV, disp);
385 M_AST(REG_ITMP3, REG_SP, 4 * 8);
390 /* move machine code onto stack */
392 disp = dseg_add_s4(cd, mcode);
393 M_ILD(REG_ITMP3, REG_PV, disp);
394 M_IST(REG_ITMP3, REG_SP, 3 * 8);
396 /* move class/method/field reference onto stack */
398 disp = dseg_add_address(cd, pref->ref);
399 M_ALD(REG_ITMP3, REG_PV, disp);
400 M_AST(REG_ITMP3, REG_SP, 2 * 8);
402 /* move data segment displacement onto stack */
404 disp = dseg_add_s4(cd, pref->disp);
405 M_ILD(REG_ITMP3, REG_PV, disp);
406 M_IST(REG_ITMP3, REG_SP, 1 * 8);
408 /* move patcher function pointer onto stack */
410 disp = dseg_add_functionptr(cd, pref->patcher);
411 M_ALD(REG_ITMP3, REG_PV, disp);
412 M_AST(REG_ITMP3, REG_SP, 0 * 8);
414 if (targetdisp == 0) {
415 targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
417 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
418 M_ALD(REG_ITMP3, REG_PV, disp);
419 M_JMP(REG_ZERO, REG_ITMP3);
422 disp = (((u4 *) cd->mcodebase) + targetdisp) -
423 (((u4 *) cd->mcodeptr) + 1);
431 /* emit_replacement_stubs ******************************************************
433 Generates the code for the replacement stubs.
435 *******************************************************************************/
437 void emit_replacement_stubs(jitdata *jd)
446 /* get required compiler data */
451 rplp = code->rplpoints;
453 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
454 /* check code segment size */
458 /* note start of stub code */
460 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
462 /* make machine code for patching */
464 savedmcodeptr = cd->mcodeptr;
465 cd->mcodeptr = (u1 *) &(rplp->mcode);
467 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
470 cd->mcodeptr = savedmcodeptr;
472 /* create stack frame - 16-byte aligned */
474 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
476 /* push address of `rplpoint` struct */
478 disp = dseg_add_address(cd, rplp);
479 M_ALD(REG_ITMP3, REG_PV, disp);
480 M_AST(REG_ITMP3, REG_SP, 0 * 8);
482 /* jump to replacement function */
484 disp = dseg_add_functionptr(cd, asm_replacement_out);
485 M_ALD(REG_ITMP3, REG_PV, disp);
486 M_JMP(REG_ZERO, REG_ITMP3);
491 /* emit_verbosecall_enter ******************************************************
493 Generates the code for the call trace.
495 *******************************************************************************/
498 void emit_verbosecall_enter(jitdata *jd)
507 /* get required compiler data */
515 /* mark trace code */
519 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
520 M_AST(REG_RA, REG_SP, 1 * 8);
522 /* save argument registers */
524 for (i = 0; i < INT_ARG_CNT; i++)
525 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
527 for (i = 0; i < FLT_ARG_CNT; i++)
528 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
530 /* save temporary registers for leaf methods */
532 if (jd->isleafmethod) {
533 for (i = 0; i < INT_TMP_CNT; i++)
534 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
536 for (i = 0; i < FLT_TMP_CNT; i++)
537 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
540 /* load float arguments into integer registers */
542 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
543 t = md->paramtypes[i].type;
545 if (IS_FLT_DBL_TYPE(t)) {
546 if (IS_2_WORD_TYPE(t)) {
547 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
548 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
551 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
552 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
557 disp = dseg_add_address(cd, m);
558 M_ALD(REG_ITMP1, REG_PV, disp);
559 M_AST(REG_ITMP1, REG_SP, 0 * 8);
560 disp = dseg_add_functionptr(cd, builtin_trace_args);
561 M_ALD(REG_PV, REG_PV, disp);
562 M_JSR(REG_RA, REG_PV);
563 disp = (s4) (cd->mcodeptr - cd->mcodebase);
564 M_LDA(REG_PV, REG_RA, -disp);
565 M_ALD(REG_RA, REG_SP, 1 * 8);
567 /* restore argument registers */
569 for (i = 0; i < INT_ARG_CNT; i++)
570 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
572 for (i = 0; i < FLT_ARG_CNT; i++)
573 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
575 /* restore temporary registers for leaf methods */
577 if (jd->isleafmethod) {
578 for (i = 0; i < INT_TMP_CNT; i++)
579 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
581 for (i = 0; i < FLT_TMP_CNT; i++)
582 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
585 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
587 /* mark trace code */
591 #endif /* !defined(NDEBUG) */
594 /* emit_verbosecall_exit *******************************************************
596 Generates the code for the call trace.
598 *******************************************************************************/
601 void emit_verbosecall_exit(jitdata *jd)
608 /* get required compiler data */
614 /* mark trace code */
618 M_LDA(REG_SP, REG_SP, -4 * 8);
619 M_AST(REG_RA, REG_SP, 0 * 8);
621 M_LST(REG_RESULT, REG_SP, 1 * 8);
622 M_DST(REG_FRESULT, REG_SP, 2 * 8);
624 disp = dseg_add_address(cd, m);
625 M_ALD(rd->argintregs[0], REG_PV, disp);
627 M_MOV(REG_RESULT, rd->argintregs[1]);
628 M_FLTMOVE(REG_FRESULT, rd->argfltregs[2]);
629 M_FLTMOVE(REG_FRESULT, rd->argfltregs[3]);
631 disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
632 M_ALD(REG_PV, REG_PV, disp);
633 M_JSR(REG_RA, REG_PV);
634 disp = (cd->mcodeptr - cd->mcodebase);
635 M_LDA(REG_PV, REG_RA, -disp);
637 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
638 M_LLD(REG_RESULT, REG_SP, 1 * 8);
640 M_ALD(REG_RA, REG_SP, 0 * 8);
641 M_LDA(REG_SP, REG_SP, 4 * 8);
643 /* mark trace code */
647 #endif /* !defined(NDEBUG) */
651 * These are local overrides for various environment variables in Emacs.
652 * Please do not remove this and leave it at the end of the file, where
653 * Emacs will automagically detect them.
654 * ---------------------------------------------------------------------
657 * indent-tabs-mode: t
661 * vim:noexpandtab:sw=4:ts=4: