1 /* src/vm/jit/emit-common.c - common code emitter functions
3 Copyright (C) 2006, 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
39 #include "vm/jit/emit-common.h"
40 #include "vm/jit/jit.h"
42 #include "vmcore/options.h"
43 #include "vmcore/statistics.h"
46 /* emit_load_s1 ****************************************************************
48 Emits a possible load of the first source operand.
50 *******************************************************************************/
52 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
57 src = VAROP(iptr->s1);
59 reg = emit_load(jd, iptr, src, tempreg);
65 /* emit_load_s2 ****************************************************************
67 Emits a possible load of the second source operand.
69 *******************************************************************************/
71 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
76 src = VAROP(iptr->sx.s23.s2);
78 reg = emit_load(jd, iptr, src, tempreg);
84 /* emit_load_s3 ****************************************************************
86 Emits a possible load of the third source operand.
88 *******************************************************************************/
90 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
95 src = VAROP(iptr->sx.s23.s3);
97 reg = emit_load(jd, iptr, src, tempreg);
103 /* emit_load_s1_low ************************************************************
105 Emits a possible load of the low 32-bits of the first long source
108 *******************************************************************************/
110 #if SIZEOF_VOID_P == 4
111 s4 emit_load_s1_low(jitdata *jd, instruction *iptr, s4 tempreg)
116 src = VAROP(iptr->s1);
118 reg = emit_load_low(jd, iptr, src, tempreg);
125 /* emit_load_s2_low ************************************************************
127 Emits a possible load of the low 32-bits of the second long source
130 *******************************************************************************/
132 #if SIZEOF_VOID_P == 4
133 s4 emit_load_s2_low(jitdata *jd, instruction *iptr, s4 tempreg)
138 src = VAROP(iptr->sx.s23.s2);
140 reg = emit_load_low(jd, iptr, src, tempreg);
147 /* emit_load_s3_low ************************************************************
149 Emits a possible load of the low 32-bits of the third long source
152 *******************************************************************************/
154 #if SIZEOF_VOID_P == 4
155 s4 emit_load_s3_low(jitdata *jd, instruction *iptr, s4 tempreg)
160 src = VAROP(iptr->sx.s23.s3);
162 reg = emit_load_low(jd, iptr, src, tempreg);
169 /* emit_load_s1_high ***********************************************************
171 Emits a possible load of the high 32-bits of the first long source
174 *******************************************************************************/
176 #if SIZEOF_VOID_P == 4
177 s4 emit_load_s1_high(jitdata *jd, instruction *iptr, s4 tempreg)
182 src = VAROP(iptr->s1);
184 reg = emit_load_high(jd, iptr, src, tempreg);
191 /* emit_load_s2_high ***********************************************************
193 Emits a possible load of the high 32-bits of the second long source
196 *******************************************************************************/
198 #if SIZEOF_VOID_P == 4
199 s4 emit_load_s2_high(jitdata *jd, instruction *iptr, s4 tempreg)
204 src = VAROP(iptr->sx.s23.s2);
206 reg = emit_load_high(jd, iptr, src, tempreg);
213 /* emit_load_s3_high ***********************************************************
215 Emits a possible load of the high 32-bits of the third long source
218 *******************************************************************************/
220 #if SIZEOF_VOID_P == 4
221 s4 emit_load_s3_high(jitdata *jd, instruction *iptr, s4 tempreg)
226 src = VAROP(iptr->sx.s23.s3);
228 reg = emit_load_high(jd, iptr, src, tempreg);
235 /* emit_store_dst **************************************************************
237 This function generates the code to store the result of an
238 operation back into a spilled pseudo-variable. If the
239 pseudo-variable has not been spilled in the first place, this
240 function will generate nothing.
242 *******************************************************************************/
244 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
246 emit_store(jd, iptr, VAROP(iptr->dst), d);
250 /* emit_bccz *******************************************************************
252 Emit conditional and unconditional branch instructions on integer
255 *******************************************************************************/
257 void emit_bccz(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options)
262 /* Target basic block already has an PC, so we can generate the
263 branch immediately. */
265 if ((target->mpc >= 0)) {
266 STATISTICS(count_branches_resolved++);
268 /* calculate the mpc of the branch instruction */
270 branchmpc = cd->mcodeptr - cd->mcodebase;
271 disp = target->mpc - branchmpc;
273 STATISTICS(count_emit_branch++);
274 STATISTICS(if ((int8_t)disp == disp) count_emit_branch_8bit++;
275 else if ((int16_t)disp == disp) count_emit_branch_16bit++;
276 else if ((int32_t)disp == disp) count_emit_branch_32bit++;
277 #if (SIZEOF_VOID_P == 8)
278 else if ((int64_t)disp == disp) count_emit_branch_64bit++;
281 emit_branch(cd, disp, condition, reg, options);
284 /* current mcodeptr is the correct position,
285 afterwards emit the NOPs */
287 codegen_add_branch_ref(cd, target, condition, reg, options);
289 /* generate NOPs as placeholder for branch code */
296 /* emit_bcc ********************************************************************
298 Emit conditional and unconditional branch instructions on condition
301 *******************************************************************************/
303 void emit_bcc(codegendata *cd, basicblock *target, s4 condition, u4 options)
305 emit_bccz(cd, target, condition, -1, options);
309 /* emit_br *********************************************************************
311 Wrapper for unconditional branches.
313 *******************************************************************************/
315 void emit_br(codegendata *cd, basicblock *target)
317 emit_bcc(cd, target, BRANCH_UNCONDITIONAL, BRANCH_OPT_NONE);
321 /* emit_bxxz *******************************************************************
323 Wrappers for branches on one integer register.
325 *******************************************************************************/
327 #if SUPPORT_BRANCH_CONDITIONAL_ONE_INTEGER_REGISTER
329 void emit_beqz(codegendata *cd, basicblock *target, s4 reg)
331 emit_bccz(cd, target, BRANCH_EQ, reg, BRANCH_OPT_NONE);
334 void emit_bnez(codegendata *cd, basicblock *target, s4 reg)
336 emit_bccz(cd, target, BRANCH_NE, reg, BRANCH_OPT_NONE);
339 void emit_bltz(codegendata *cd, basicblock *target, s4 reg)
341 emit_bccz(cd, target, BRANCH_LT, reg, BRANCH_OPT_NONE);
344 void emit_bgez(codegendata *cd, basicblock *target, s4 reg)
346 emit_bccz(cd, target, BRANCH_GE, reg, BRANCH_OPT_NONE);
349 void emit_bgtz(codegendata *cd, basicblock *target, s4 reg)
351 emit_bccz(cd, target, BRANCH_GT, reg, BRANCH_OPT_NONE);
354 void emit_blez(codegendata *cd, basicblock *target, s4 reg)
356 emit_bccz(cd, target, BRANCH_LE, reg, BRANCH_OPT_NONE);
359 #endif /* SUPPORT_BRANCH_CONDITIONAL_ONE_INTEGER_REGISTER */
362 /* emit_bxx ********************************************************************
364 Wrappers for branches on two integer registers.
366 We use PACK_REGS here, so we don't have to change the branchref
367 data structure and the emit_bccz function.
369 *******************************************************************************/
371 #if SUPPORT_BRANCH_CONDITIONAL_TWO_INTEGER_REGISTERS
373 void emit_beq(codegendata *cd, basicblock *target, s4 s1, s4 s2)
375 emit_bccz(cd, target, BRANCH_EQ, PACK_REGS(s1, s2), BRANCH_OPT_NONE);
378 void emit_bne(codegendata *cd, basicblock *target, s4 s1, s4 s2)
380 emit_bccz(cd, target, BRANCH_NE, PACK_REGS(s1, s2), BRANCH_OPT_NONE);
383 #endif /* SUPPORT_BRANCH_CONDITIONAL_TWO_INTEGER_REGISTERS */
386 /* emit_bxx ********************************************************************
388 Wrappers for branches on condition codes.
390 *******************************************************************************/
392 #if SUPPORT_BRANCH_CONDITIONAL_CONDITION_REGISTER
394 void emit_beq(codegendata *cd, basicblock *target)
396 emit_bcc(cd, target, BRANCH_EQ, BRANCH_OPT_NONE);
399 void emit_bne(codegendata *cd, basicblock *target)
401 emit_bcc(cd, target, BRANCH_NE, BRANCH_OPT_NONE);
404 void emit_blt(codegendata *cd, basicblock *target)
406 emit_bcc(cd, target, BRANCH_LT, BRANCH_OPT_NONE);
409 void emit_bge(codegendata *cd, basicblock *target)
411 emit_bcc(cd, target, BRANCH_GE, BRANCH_OPT_NONE);
414 void emit_bgt(codegendata *cd, basicblock *target)
416 emit_bcc(cd, target, BRANCH_GT, BRANCH_OPT_NONE);
419 void emit_ble(codegendata *cd, basicblock *target)
421 emit_bcc(cd, target, BRANCH_LE, BRANCH_OPT_NONE);
424 #if SUPPORT_BRANCH_CONDITIONAL_UNSIGNED_CONDITIONS
425 void emit_bult(codegendata *cd, basicblock *target)
427 emit_bcc(cd, target, BRANCH_ULT, BRANCH_OPT_NONE);
430 void emit_bule(codegendata *cd, basicblock *target)
432 emit_bcc(cd, target, BRANCH_ULE, BRANCH_OPT_NONE);
435 void emit_buge(codegendata *cd, basicblock *target)
437 emit_bcc(cd, target, BRANCH_UGE, BRANCH_OPT_NONE);
440 void emit_bugt(codegendata *cd, basicblock *target)
442 emit_bcc(cd, target, BRANCH_UGT, BRANCH_OPT_NONE);
446 #if defined(__POWERPC__) || defined(__POWERPC64__)
447 void emit_bnan(codegendata *cd, basicblock *target)
449 emit_bcc(cd, target, BRANCH_NAN, BRANCH_OPT_NONE);
453 #endif /* SUPPORT_BRANCH_CONDITIONAL_CONDITION_REGISTER */
456 /* emit_label_bccz *************************************************************
458 Emit a branch to a label. Possibly emit the branch, if it is a
461 *******************************************************************************/
463 void emit_label_bccz(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options)
466 branch_label_ref_t *br;
470 /* get the label list */
472 list = cd->brancheslabel;
474 /* search if the label is already in the list */
476 for (br = list_first_unsynced(list); br != NULL;
477 br = list_next_unsynced(list, br)) {
478 /* is this entry the correct label? */
480 if (br->label == label)
484 /* a branch reference was found */
487 /* calculate the mpc of the branch instruction */
489 mpc = cd->mcodeptr - cd->mcodebase;
490 disp = br->mpc - mpc;
492 STATISTICS(count_emit_branch++);
493 STATISTICS(if ((int8_t)disp == disp) count_emit_branch_8bit++;
494 else if ((int16_t)disp == disp) count_emit_branch_16bit++;
495 else if ((int32_t)disp == disp) count_emit_branch_32bit++;
496 #if (SIZEOF_VOID_P == 8)
497 else if ((int64_t)disp == disp) count_emit_branch_64bit++;
500 emit_branch(cd, disp, condition, reg, options);
502 /* now remove the branch reference */
504 list_remove_unsynced(list, br);
507 /* current mcodeptr is the correct position,
508 afterwards emit the NOPs */
510 codegen_branch_label_add(cd, label, condition, reg, options);
512 /* generate NOPs as placeholder for branch code */
519 /* emit_label ******************************************************************
521 Emit a label for a branch. Possibly emit the branch, if it is a
524 *******************************************************************************/
526 void emit_label(codegendata *cd, s4 label)
529 branch_label_ref_t *br;
534 /* get the label list */
536 list = cd->brancheslabel;
538 /* search if the label is already in the list */
540 for (br = list_first_unsynced(list); br != NULL;
541 br = list_next_unsynced(list, br)) {
542 /* is this entry the correct label? */
544 if (br->label == label)
548 /* a branch reference was found */
551 /* calculate the mpc of the branch instruction */
553 mpc = cd->mcodeptr - cd->mcodebase;
554 disp = mpc - br->mpc;
556 /* temporary set the mcodeptr */
558 mcodeptr = cd->mcodeptr;
559 cd->mcodeptr = cd->mcodebase + br->mpc;
561 STATISTICS(count_emit_branch++);
562 STATISTICS(if ((int8_t)disp == disp) count_emit_branch_8bit++;
563 else if ((int16_t)disp == disp) count_emit_branch_16bit++;
564 else if ((int32_t)disp == disp) count_emit_branch_32bit++;
565 #if (SIZEOF_VOID_P == 8)
566 else if ((int64_t)disp == disp) count_emit_branch_64bit++;
569 emit_branch(cd, disp, br->condition, br->reg, br->options);
571 /* restore mcodeptr */
573 cd->mcodeptr = mcodeptr;
575 /* now remove the branch reference */
577 list_remove_unsynced(list, br);
580 /* add the label to the list (use invalid values for condition
583 codegen_branch_label_add(cd, label, -1, -1, BRANCH_OPT_NONE );
588 /* emit_label_bcc **************************************************************
590 Emit conditional and unconditional label-branch instructions on
593 *******************************************************************************/
595 void emit_label_bcc(codegendata *cd, s4 label, s4 condition, u4 options)
597 emit_label_bccz(cd, label, condition, -1, options);
601 /* emit_label_br ***************************************************************
603 Wrapper for unconditional label-branches.
605 *******************************************************************************/
607 void emit_label_br(codegendata *cd, s4 label)
609 emit_label_bcc(cd, label, BRANCH_UNCONDITIONAL, BRANCH_OPT_NONE);
613 /* emit_label_bxxz *************************************************************
615 Wrappers for label-branches on one integer register.
617 *******************************************************************************/
619 #if SUPPORT_BRANCH_CONDITIONAL_ONE_INTEGER_REGISTER
621 void emit_label_beqz(codegendata *cd, s4 label, s4 reg)
623 emit_label_bccz(cd, label, BRANCH_EQ, reg, BRANCH_OPT_NONE);
626 #endif /* SUPPORT_BRANCH_CONDITIONAL_ONE_INTEGER_REGISTER */
629 /* emit_label_bxx **************************************************************
631 Wrappers for label-branches on condition codes.
633 *******************************************************************************/
635 #if SUPPORT_BRANCH_CONDITIONAL_CONDITION_REGISTER
637 void emit_label_beq(codegendata *cd, s4 label)
639 emit_label_bcc(cd, label, BRANCH_EQ, BRANCH_OPT_NONE);
642 void emit_label_bne(codegendata *cd, s4 label)
644 emit_label_bcc(cd, label, BRANCH_NE, BRANCH_OPT_NONE);
647 void emit_label_blt(codegendata *cd, s4 label)
649 emit_label_bcc(cd, label, BRANCH_LT, BRANCH_OPT_NONE);
652 void emit_label_bge(codegendata *cd, s4 label)
654 emit_label_bcc(cd, label, BRANCH_GE, BRANCH_OPT_NONE);
657 void emit_label_bgt(codegendata *cd, s4 label)
659 emit_label_bcc(cd, label, BRANCH_GT, BRANCH_OPT_NONE);
662 void emit_label_ble(codegendata *cd, s4 label)
664 emit_label_bcc(cd, label, BRANCH_LE, BRANCH_OPT_NONE);
667 #endif /* SUPPORT_BRANCH_CONDITIONAL_CONDITION_REGISTER */
671 * These are local overrides for various environment variables in Emacs.
672 * Please do not remove this and leave it at the end of the file, where
673 * Emacs will automagically detect them.
674 * ---------------------------------------------------------------------
677 * indent-tabs-mode: t
681 * vim:noexpandtab:sw=4:ts=4: