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 8318 2007-08-16 10:05:34Z michi $
38 #include "vm/jit/alpha/codegen.h"
40 #include "mm/memory.h"
42 #include "threads/lock-common.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
47 #include "vm/jit/abi.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/patcher-common.h"
54 #include "vm/jit/replace.h"
56 #include "vmcore/options.h"
59 /* emit_load *******************************************************************
61 Emits a possible load of an operand.
63 *******************************************************************************/
65 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
71 /* get required compiler data */
75 if (IS_INMEMORY(src->flags)) {
78 disp = src->vv.regoff;
84 M_LLD(tempreg, REG_SP, disp);
88 M_DLD(tempreg, REG_SP, disp);
91 vm_abort("emit_load: unknown type %d", src->type);
103 /* emit_store ******************************************************************
105 Emit a possible store for the given variable.
107 *******************************************************************************/
109 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
114 /* get required compiler data */
118 if (IS_INMEMORY(dst->flags)) {
121 disp = dst->vv.regoff;
127 M_LST(d, REG_SP, disp);
131 M_DST(d, REG_SP, disp);
134 vm_abort("emit_store: unknown type %d", dst->type);
140 /* emit_copy *******************************************************************
142 Generates a register/memory to register/memory copy.
144 *******************************************************************************/
146 void emit_copy(jitdata *jd, instruction *iptr)
153 /* get required compiler data */
157 /* get source and destination variables */
159 src = VAROP(iptr->s1);
160 dst = VAROP(iptr->dst);
162 if ((src->vv.regoff != dst->vv.regoff) ||
163 ((src->flags ^ dst->flags) & INMEMORY)) {
165 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
166 /* emit nothing, as the value won't be used anyway */
170 /* If one of the variables resides in memory, we can eliminate
171 the register move from/to the temporary register with the
172 order of getting the destination register and the load. */
174 if (IS_INMEMORY(src->flags)) {
175 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
176 s1 = emit_load(jd, iptr, src, d);
179 s1 = emit_load(jd, iptr, src, REG_IFTMP);
180 d = codegen_reg_of_var(iptr->opc, dst, s1);
195 vm_abort("emit_copy: unknown type %d", src->type);
199 emit_store(jd, iptr, dst, d);
204 /* emit_iconst *****************************************************************
208 *******************************************************************************/
210 void emit_iconst(codegendata *cd, s4 d, s4 value)
214 if ((value >= -32768) && (value <= 32767))
215 M_LDA_INTERN(d, REG_ZERO, value);
217 disp = dseg_add_s4(cd, value);
218 M_ILD(d, REG_PV, disp);
223 /* emit_lconst *****************************************************************
227 *******************************************************************************/
229 void emit_lconst(codegendata *cd, s4 d, s8 value)
233 if ((value >= -32768) && (value <= 32767))
234 M_LDA_INTERN(d, REG_ZERO, value);
236 disp = dseg_add_s8(cd, value);
237 M_LLD(d, REG_PV, disp);
242 /* emit_branch *****************************************************************
244 Emits the code for conditional and unconditional branchs.
246 *******************************************************************************/
248 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
253 /* calculate the different displacements */
255 checkdisp = (disp - 4);
256 branchdisp = (disp - 4) >> 2;
258 /* check which branch to generate */
260 if (condition == BRANCH_UNCONDITIONAL) {
261 /* check displacement for overflow */
263 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
264 /* if the long-branches flag isn't set yet, do it */
266 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
267 log_println("setting error");
268 cd->flags |= (CODEGENDATA_FLAG_ERROR |
269 CODEGENDATA_FLAG_LONGBRANCHES);
272 vm_abort("emit_branch: emit unconditional long-branch code");
279 /* and displacement for overflow */
281 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
282 /* if the long-branches flag isn't set yet, do it */
284 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
285 log_println("setting error");
286 cd->flags |= (CODEGENDATA_FLAG_ERROR |
287 CODEGENDATA_FLAG_LONGBRANCHES);
290 vm_abort("emit_branch: emit conditional long-branch code");
295 M_BEQZ(reg, branchdisp);
298 M_BNEZ(reg, branchdisp);
301 M_BLTZ(reg, branchdisp);
304 M_BGEZ(reg, branchdisp);
307 M_BGTZ(reg, branchdisp);
310 M_BLEZ(reg, branchdisp);
313 vm_abort("emit_branch: unknown condition %d", condition);
320 /* emit_arithmetic_check *******************************************************
322 Emit an ArithmeticException check.
324 *******************************************************************************/
326 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
328 if (INSTRUCTION_MUST_CHECK(iptr)) {
330 /* Destination register must not be REG_ZERO, because then no
331 SIGSEGV is thrown. */
332 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
337 /* emit_arrayindexoutofbounds_check ********************************************
339 Emit an ArrayIndexOutOfBoundsException check.
341 *******************************************************************************/
343 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
345 if (INSTRUCTION_MUST_CHECK(iptr)) {
346 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
347 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
348 M_BNEZ(REG_ITMP3, 1);
349 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
354 /* emit_classcast_check ********************************************************
356 Emit a ClassCastException check.
358 *******************************************************************************/
360 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
362 if (INSTRUCTION_MUST_CHECK(iptr)) {
371 vm_abort("emit_classcast_check: unknown condition %d", condition);
373 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
378 /* emit_nullpointer_check ******************************************************
380 Emit a NullPointerException check.
382 *******************************************************************************/
384 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
386 if (INSTRUCTION_MUST_CHECK(iptr)) {
388 /* Destination register must not be REG_ZERO, because then no
389 SIGSEGV is thrown. */
390 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
395 /* emit_exception_check ********************************************************
397 Emit an Exception check.
399 *******************************************************************************/
401 void emit_exception_check(codegendata *cd, instruction *iptr)
403 if (INSTRUCTION_MUST_CHECK(iptr)) {
404 M_BNEZ(REG_RESULT, 1);
405 /* Destination register must not be REG_ZERO, because then no
406 SIGSEGV is thrown. */
407 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
412 /* emit_trap *******************************************************************
414 Emit a trap instruction and return the original machine code.
416 *******************************************************************************/
418 uint32_t emit_trap(codegendata *cd)
422 /* Get machine code which is patched back in later. The
423 trap is 1 instruction word long. */
425 mcode = *((u4 *) cd->mcodeptr);
427 /* Destination register must not be REG_ZERO, because then no
428 SIGSEGV is thrown. */
429 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
435 /* emit_verbosecall_enter ******************************************************
437 Generates the code for the call trace.
439 *******************************************************************************/
442 void emit_verbosecall_enter(jitdata *jd)
451 /* get required compiler data */
459 /* mark trace code */
463 M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
464 M_AST(REG_RA, REG_SP, 1 * 8);
466 /* save argument registers */
468 for (i = 0; i < INT_ARG_CNT; i++)
469 M_LST(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
471 for (i = 0; i < FLT_ARG_CNT; i++)
472 M_DST(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
474 /* save temporary registers for leaf methods */
476 if (jd->isleafmethod) {
477 for (i = 0; i < INT_TMP_CNT; i++)
478 M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
480 for (i = 0; i < FLT_TMP_CNT; i++)
481 M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
484 /* load float arguments into integer registers */
486 for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
487 t = md->paramtypes[i].type;
489 if (IS_FLT_DBL_TYPE(t)) {
490 if (IS_2_WORD_TYPE(t)) {
491 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
492 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
495 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
496 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
501 disp = dseg_add_address(cd, m);
502 M_ALD(REG_ITMP1, REG_PV, disp);
503 M_AST(REG_ITMP1, REG_SP, 0 * 8);
504 disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
505 M_ALD(REG_PV, REG_PV, disp);
506 M_JSR(REG_RA, REG_PV);
507 disp = (s4) (cd->mcodeptr - cd->mcodebase);
508 M_LDA(REG_PV, REG_RA, -disp);
509 M_ALD(REG_RA, REG_SP, 1 * 8);
511 /* restore argument registers */
513 for (i = 0; i < INT_ARG_CNT; i++)
514 M_LLD(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
516 for (i = 0; i < FLT_ARG_CNT; i++)
517 M_DLD(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
519 /* restore temporary registers for leaf methods */
521 if (jd->isleafmethod) {
522 for (i = 0; i < INT_TMP_CNT; i++)
523 M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
525 for (i = 0; i < FLT_TMP_CNT; i++)
526 M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
529 M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
531 /* mark trace code */
535 #endif /* !defined(NDEBUG) */
538 /* emit_verbosecall_exit *******************************************************
540 Generates the code for the call trace.
542 void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
544 *******************************************************************************/
547 void emit_verbosecall_exit(jitdata *jd)
554 /* get required compiler data */
560 /* mark trace code */
564 M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
565 M_AST(REG_RA, REG_SP, 0 * 8);
567 M_LST(REG_RESULT, REG_SP, 1 * 8);
568 M_DST(REG_FRESULT, REG_SP, 2 * 8);
570 M_MOV(REG_RESULT, REG_A0);
571 M_FMOV(REG_FRESULT, REG_FA1);
572 M_FMOV(REG_FRESULT, REG_FA2);
574 disp = dseg_add_address(cd, m);
575 M_ALD(REG_A3, REG_PV, disp);
577 disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
578 M_ALD(REG_PV, REG_PV, disp);
579 M_JSR(REG_RA, REG_PV);
580 disp = (cd->mcodeptr - cd->mcodebase);
581 M_LDA(REG_PV, REG_RA, -disp);
583 M_DLD(REG_FRESULT, REG_SP, 2 * 8);
584 M_LLD(REG_RESULT, REG_SP, 1 * 8);
586 M_ALD(REG_RA, REG_SP, 0 * 8);
587 M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
589 /* mark trace code */
593 #endif /* !defined(NDEBUG) */
597 * These are local overrides for various environment variables in Emacs.
598 * Please do not remove this and leave it at the end of the file, where
599 * Emacs will automagically detect them.
600 * ---------------------------------------------------------------------
603 * indent-tabs-mode: t
607 * vim:noexpandtab:sw=4:ts=4: