1 /* src/vm/jit/show.c - showing the intermediate representation
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: Andreas Krall
29 Changes: Edwin Steiner
43 #include "mm/memory.h"
45 #if defined(ENABLE_THREADS)
46 # include "threads/native/lock.h"
48 # include "threads/none/lock.h"
51 #include "vm/global.h"
52 #include "vm/options.h"
53 #include "vm/builtin.h"
54 #include "vm/stringlocal.h"
55 #include "vm/jit/jit.h"
56 #include "vm/jit/show.h"
57 #include "vm/jit/disass.h"
58 #include "vm/jit/stack.h"
61 /* global variables ***********************************************************/
63 #if defined(ENABLE_THREADS) && !defined(NDEBUG)
64 static java_objectheader *show_global_lock;
68 /* forward declarations *******************************************************/
71 static void new_show_variable_array(jitdata *jd, s4 *vars, int n, int stage);
72 static void show_allocation(s4 type, s4 flags, s4 regoff);
73 static void show_variable(jitdata *jd, s4 index, int stage);
77 /* show_init *******************************************************************
79 Initialized the show subsystem (called by jit_init).
81 *******************************************************************************/
86 #if defined(ENABLE_THREADS)
87 /* initialize the show lock */
89 show_global_lock = NEW(java_objectheader);
91 lock_init_object_lock(show_global_lock);
102 static char *jit_type[] = {
112 /* show_method *****************************************************************
114 Print the intermediate representation of a method.
116 NOTE: Currently this function may only be called after register allocation!
118 *******************************************************************************/
121 void new_show_method(jitdata *jd, int stage)
128 basicblock *lastbptr;
133 /* get required compiler data */
140 /* We need to enter a lock here, since the binutils disassembler
141 is not reentrant-able and we could not read functions printed
144 LOCK_MONITOR_ENTER(show_global_lock);
146 /* get the last basic block */
148 for (lastbptr = jd->new_basicblocks; lastbptr->next != NULL; lastbptr = lastbptr->next);
154 printf("\n(NEW INSTRUCTION FORMAT)\n");
155 if (jd->isleafmethod)
156 printf("LEAFMETHOD\n");
157 printf("\nBasic blocks: %d\n", jd->new_basicblockcount);
158 if (stage >= SHOW_CODE) {
159 printf("Code length: %d\n", (lastbptr->mpc - jd->new_basicblocks[0].mpc));
160 printf("Data length: %d\n", cd->dseglen);
161 printf("Stub length: %d\n", (s4) (code->mcodelength -
162 ((ptrint) cd->dseglen + lastbptr->mpc)));
164 printf("Max locals: %d\n", cd->maxlocals);
165 printf("Max stack: %d\n", cd->maxstack);
166 printf("Line number table length: %d\n", m->linenumbercount);
168 if (stage >= SHOW_PARSE) {
169 printf("Exceptions (Number: %d):\n", cd->exceptiontablelength);
170 for (ex = cd->exceptiontable; ex != NULL; ex = ex->down) {
171 printf(" L%03d ... ", ex->start->nr );
172 printf("L%03d = ", ex->end->nr);
173 printf("L%03d", ex->handler->nr);
174 printf(" (catchtype: ");
175 if (ex->catchtype.any)
176 if (IS_CLASSREF(ex->catchtype))
177 class_classref_print(ex->catchtype.ref);
179 class_print(ex->catchtype.ref);
186 if (stage >= SHOW_PARSE && rd && jd->localcount > 0) {
187 printf("Local Table:\n");
188 for (i = 0; i < jd->localcount; i++) {
191 #if defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER)
192 # if defined(ENABLE_INTRP)
195 printf(" (%s) ", jit_type[jd->var[i].type]);
196 show_allocation(jd->var[i].type, jd->var[i].flags, jd->var[i].regoff);
198 # if defined(ENABLE_INTRP)
201 #endif /* defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER) */
206 if (cd->maxlocals > 0) {
207 printf("Local Map:\n");
209 for (j = 0; j < cd->maxlocals; j++) {
213 for (i = 0; i < 5; i++) {
214 printf(" %5s ",jit_type[i]);
215 for (j = 0; j < cd->maxlocals; j++) {
216 if (jd->local_map[j*5+i] == UNUSED)
219 printf("%4i ",jd->local_map[j*5+i]);
226 if (cd->maxstack > 0 && jd->interface_map && stage >= SHOW_STACK) {
228 interface_info *mapptr = jd->interface_map;
230 /* look if there exist any IN/OUTVARS */
231 for (i = 0; (i < (5 * cd->maxstack)) && !exist; i++, mapptr++)
232 exist = (mapptr->flags != UNUSED);
235 printf("Interface Table: (In/Outvars)\n");
237 for (j = 0; j < cd->maxstack; j++) {
242 for (i = 0; i < 5; i++) {
243 printf(" %5s ",jit_type[i]);
244 for (j = 0; j < cd->maxstack; j++) {
245 s4 flags = jd->interface_map[j*5+i].flags;
246 s4 regoff = jd->interface_map[j*5+i].regoff;
252 if (stage >= SHOW_REGS) {
253 if (flags & SAVEDVAR) {
254 if (flags & INMEMORY)
260 if (flags & INMEMORY)
265 printf("%c%03d(", ch, regoff);
266 show_allocation(i, flags, regoff);
270 if (flags & SAVEDVAR)
283 if (rd->memuse && stage >= SHOW_REGS) {
287 printf("Stack slots: (memuse=%d", rd->memuse);
288 if (stage >= SHOW_CODE) {
289 printf(", stackframesize=%d", cd->stackframesize);
290 max = cd->stackframesize;
293 for (i=0; i<max; ++i) {
294 #if defined(HAS_4BYTE_STACKSLOT)
295 printf(" M%02d = 0x%02x(sp): ", i, i * 4);
297 printf(" M%02d = 0x%02x(sp): ", i, i * 8);
299 for (j=0; j<jd->varcount; ++j) {
300 varinfo *v = jd->var + j;
301 if ((v->flags & INMEMORY) && (v->regoff == i)) {
302 show_variable(jd, j, stage);
313 if (code->rplpoints) {
314 printf("Replacement Points:\n");
315 replace_show_replacement_points(code);
319 #if defined(ENABLE_DISASSEMBLER)
320 /* show code before first basic block */
322 if ((stage >= SHOW_CODE) && JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
323 u1ptr = (u1 *) ((ptrint) code->mcode + cd->dseglen);
325 for (; u1ptr < (u1 *) ((ptrint) code->mcode + cd->dseglen + jd->new_basicblocks[0].mpc);)
332 /* show code of all basic blocks */
334 for (bptr = jd->new_basicblocks; bptr != NULL; bptr = bptr->next)
335 new_show_basicblock(jd, bptr, stage);
337 #if defined(ENABLE_DISASSEMBLER)
338 /* show stubs code */
340 if (stage >= SHOW_CODE && opt_showdisassemble && opt_showexceptionstubs) {
341 printf("\nStubs code:\n");
342 printf("Length: %d\n\n", (s4) (code->mcodelength -
343 ((ptrint) cd->dseglen + lastbptr->mpc)));
345 u1ptr = (u1 *) ((ptrint) code->mcode + cd->dseglen + lastbptr->mpc);
347 for (; (ptrint) u1ptr < ((ptrint) code->mcode + code->mcodelength);)
354 LOCK_MONITOR_EXIT(show_global_lock);
356 /* finally flush the output */
360 #endif /* !defined(NDEBUG) */
363 /* show_basicblock *************************************************************
365 Print the intermediate representation of a basic block.
367 NOTE: Currently this function may only be called after register allocation!
369 *******************************************************************************/
372 void new_show_basicblock(jitdata *jd, basicblock *bptr, int stage)
382 /* get required compiler data */
388 if (bptr->flags != BBDELETED) {
389 deadcode = bptr->flags <= BBREACHED;
391 printf("======== %sL%03d ======== (flags: %d, bitflags: %01x, next: %d, type: ",
392 (bptr->bitflags & BBFLAG_REPLACEMENT) ? "<REPLACE> " : "",
393 bptr->nr, bptr->flags, bptr->bitflags,
394 (bptr->next) ? (bptr->next->nr) : -1);
396 switch (bptr->type) {
408 printf(", instruction count: %d, predecessors: %d [ ",
409 bptr->icount, bptr->predecessorcount);
411 for (i = 0; i < bptr->predecessorcount; i++)
412 printf("%d ", bptr->predecessors[i]->nr);
416 if (stage >= SHOW_STACK) {
418 new_show_variable_array(jd, bptr->invars, bptr->indepth, stage);
424 for (i = 0; i < bptr->icount; i++, iptr++) {
425 printf("%4d: ", iptr->line);
427 new_show_icmd(jd, iptr, deadcode, stage);
431 if (stage >= SHOW_STACK) {
433 new_show_variable_array(jd, bptr->outvars, bptr->outdepth, stage);
437 #if defined(ENABLE_DISASSEMBLER)
438 if ((stage >= SHOW_CODE) && JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) &&
442 u1ptr = (u1 *) (code->mcode + cd->dseglen + bptr->mpc);
444 if (bptr->next != NULL) {
445 for (; u1ptr < (u1 *) (code->mcode + cd->dseglen + bptr->next->mpc);)
450 for (; u1ptr < (u1 *) (code->mcode + code->mcodelength);)
458 #endif /* !defined(NDEBUG) */
461 /* show_icmd *******************************************************************
463 Print the intermediate representation of an instruction.
465 NOTE: Currently this function may only be called after register allocation!
467 *******************************************************************************/
471 #define SHOW_TARGET(target) \
472 if (stage >= SHOW_STACK) { \
473 printf("--> L%03d ", (target).block->nr); \
475 else if (stage >= SHOW_PARSE) { \
476 printf("--> insindex %d (L%03d) ", (target).insindex, \
477 jd->new_basicblocks[jd->new_basicblockindex[ \
478 (target).insindex]].nr); \
481 printf("--> insindex %d ", (target).insindex); \
484 #define SHOW_INT_CONST(val) \
485 if (stage >= SHOW_PARSE) { \
486 printf("%ld ", (long) (val)); \
492 #define SHOW_LNG_CONST(val) \
493 if (stage >= SHOW_PARSE) { \
494 printf("%lld ", (long long)(val)); \
500 #define SHOW_FLT_CONST(val) \
501 if (stage >= SHOW_PARSE) { \
502 printf("%g ", (val)); \
508 #define SHOW_DBL_CONST(val) \
509 if (stage >= SHOW_PARSE) { \
510 printf("%g ", (val)); \
516 #define SHOW_INDEX(index) \
517 if (stage >= SHOW_PARSE) { \
518 printf("%d ", index); \
524 #define SHOW_STRING(val) \
525 if (stage >= SHOW_PARSE) { \
527 utf_display_printable_ascii( \
528 javastring_toutf((java_lang_String *)(val), false)); \
535 #define SHOW_CLASSREF_OR_CLASSINFO(c) \
536 if (stage >= SHOW_PARSE) { \
537 if (IS_CLASSREF(c)) \
538 class_classref_print(c.ref); \
540 class_print(c.cls); \
547 #define SHOW_FIELD(fmiref) \
548 if (stage >= SHOW_PARSE) { \
549 field_fieldref_print(fmiref); \
556 #define SHOW_VARIABLE(v) \
557 show_variable(jd, (v), stage)
559 #define SHOW_S1(iptr) \
560 if (stage >= SHOW_STACK) { \
561 SHOW_VARIABLE(iptr->s1.varindex); \
564 #define SHOW_S2(iptr) \
565 if (stage >= SHOW_STACK) { \
566 SHOW_VARIABLE(iptr->sx.s23.s2.varindex); \
569 #define SHOW_S3(iptr) \
570 if (stage >= SHOW_STACK) { \
571 SHOW_VARIABLE(iptr->sx.s23.s3.varindex); \
574 #define SHOW_DST(iptr) \
575 if (stage >= SHOW_STACK) { \
577 SHOW_VARIABLE(iptr->dst.varindex); \
580 #define SHOW_S1_LOCAL(iptr) \
581 if (stage >= SHOW_STACK) { \
582 printf("L%d ", iptr->s1.varindex); \
585 #define SHOW_DST_LOCAL(iptr) \
586 if (stage >= SHOW_STACK) { \
587 printf("=> L%d ", iptr->dst.varindex); \
590 static void show_allocation(s4 type, s4 flags, s4 regoff)
592 if (flags & INMEMORY) {
593 printf("M%02d", regoff);
597 #ifdef HAS_ADDRESS_REGISTER_FILE
598 if (type == TYPE_ADR) {
599 printf("R%02d", regoff);
604 if (IS_FLT_DBL_TYPE(type)) {
605 printf("F%02d", regoff);
609 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
610 if (IS_2_WORD_TYPE(type)) {
611 # if defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER)
612 # if defined(ENABLE_INTRP)
614 printf("%3d/%3d", GET_LOW_REG(regoff),
615 GET_HIGH_REG(regoff));
618 printf("%3s/%3s", regs[GET_LOW_REG(regoff)],
619 regs[GET_HIGH_REG(regoff)]);
621 printf("%3d/%3d", GET_LOW_REG(regoff),
622 GET_HIGH_REG(regoff));
626 #endif /* defined(SUPPORT_COMBINE_INTEGER_REGISTERS) */
628 #if defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER)
629 # if defined(ENABLE_INTRP)
631 printf("%3d", regoff);
634 printf("%3s", regs[regoff]);
636 printf("%3d", regoff);
640 static void show_variable(jitdata *jd, s4 index, int stage)
646 v = &(jd->var[index]);
649 case TYPE_INT: type = 'i'; break;
650 case TYPE_LNG: type = 'l'; break;
651 case TYPE_FLT: type = 'f'; break;
652 case TYPE_DBL: type = 'd'; break;
653 case TYPE_ADR: type = 'a'; break;
657 if (index < jd->localcount) {
659 if (v->flags & (PREALLOC | OUTVAR))
660 printf("<INVALID FLAGS!>");
663 if (v->flags & PREALLOC) {
665 if (v->flags & OUTVAR)
666 printf("<INVALID FLAGS!>");
668 else if (v->flags & OUTVAR)
674 printf("%c%c%d", kind, type, index);
676 if (v->flags & SAVEDVAR)
679 if (stage >= SHOW_REGS) {
681 show_allocation(v->type, v->flags, v->regoff);
688 static void new_show_variable_array(jitdata *jd, s4 *vars, int n, int stage)
693 for (i=0; i<n; ++i) {
696 show_variable(jd, vars[i], stage);
701 void new_show_icmd(jitdata *jd, instruction *iptr, bool deadcode, int stage)
704 branch_target_t *table;
705 lookup_target_t *lookup;
706 constant_FMIref *fmiref;
710 /* get the opcode and the condition */
714 printf("%s ", icmd_names[opcode]);
716 if (stage < SHOW_PARSE)
722 /* Print the condition for conditional instructions. */
724 /* XXX print condition from flags */
726 if (iptr->flags.bits & INS_FLAG_UNRESOLVED)
727 printf("(UNRESOLVED) ");
733 case ICMD_CHECKNULL_POP:
738 case ICMD_ARRAYLENGTH:
805 /* binary/const INT */
817 case ICMD_IUSHRCONST:
820 case ICMD_LUSHRCONST:
822 SHOW_INT_CONST(iptr->sx.val.i);
826 /* ?ASTORECONST (trinary/const INT) */
827 case ICMD_IASTORECONST:
828 case ICMD_BASTORECONST:
829 case ICMD_CASTORECONST:
830 case ICMD_SASTORECONST:
833 SHOW_INT_CONST(iptr->sx.s23.s3.constval);
838 SHOW_INT_CONST(iptr->sx.val.i);
842 /* binary/const LNG */
853 SHOW_LNG_CONST(iptr->sx.val.l);
857 /* trinary/const LNG (<= pointer size) */
858 case ICMD_LASTORECONST:
861 SHOW_LNG_CONST(iptr->sx.s23.s3.constval);
866 SHOW_LNG_CONST(iptr->sx.val.l);
872 SHOW_FLT_CONST(iptr->sx.val.f);
878 SHOW_DBL_CONST(iptr->sx.val.d);
884 if (iptr->flags.bits & INS_FLAG_CLASS) {
885 SHOW_CLASSREF_OR_CLASSINFO(iptr->sx.val.c);
887 else if (iptr->sx.val.anyptr == NULL) {
891 SHOW_STRING(iptr->sx.val.stringconst);
896 case ICMD_AASTORECONST:
899 printf("%p ", (void*) iptr->sx.s23.s3.constval);
902 case ICMD_GETFIELD: /* 1 -> 1 */
903 case ICMD_PUTFIELD: /* 2 -> 0 */
904 case ICMD_PUTSTATIC: /* 1 -> 0 */
905 case ICMD_GETSTATIC: /* 0 -> 1 */
906 case ICMD_PUTSTATICCONST: /* 0 -> 0 */
907 case ICMD_PUTFIELDCONST: /* 1 -> 0 */
908 if (opcode != ICMD_GETSTATIC && opcode != ICMD_PUTSTATICCONST) {
910 if (opcode == ICMD_PUTFIELD) {
914 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
917 if (opcode == ICMD_GETSTATIC || opcode == ICMD_GETFIELD) {
924 SHOW_INT_CONST(iptr->sx.val.i);
925 SHOW_DST_LOCAL(iptr);
973 SHOW_DST_LOCAL(iptr);
988 case ICMD_MULTIANEWARRAY:
989 if (stage >= SHOW_STACK) {
990 argp = iptr->sx.s23.s2.args;
991 i = iptr->s1.argcount;
993 SHOW_VARIABLE(*(argp++));
997 printf("argcount=%d ", iptr->s1.argcount);
1002 case ICMD_CHECKCAST:
1007 case ICMD_INSTANCEOF:
1012 case ICMD_INLINE_START:
1013 case ICMD_INLINE_END:
1017 if (stage >= SHOW_STACK) {
1018 argp = iptr->sx.s23.s2.args;
1019 i = iptr->s1.argcount;
1021 if ((iptr->s1.argcount - 1 - i) == iptr->sx.s23.s3.bte->md->paramcount)
1022 printf(" pass-through: ");
1023 SHOW_VARIABLE(*(argp++));
1026 printf("%s ", iptr->sx.s23.s3.bte->cname);
1027 if (iptr->sx.s23.s3.bte->md->returntype.type != TYPE_VOID) {
1032 case ICMD_INVOKEVIRTUAL:
1033 case ICMD_INVOKESPECIAL:
1034 case ICMD_INVOKESTATIC:
1035 case ICMD_INVOKEINTERFACE:
1036 if (stage >= SHOW_STACK) {
1038 INSTRUCTION_GET_METHODDESC(iptr, md);
1039 argp = iptr->sx.s23.s2.args;
1040 i = iptr->s1.argcount;
1042 if ((iptr->s1.argcount - 1 - i) == md->paramcount)
1043 printf(" pass-through: ");
1044 SHOW_VARIABLE(*(argp++));
1047 INSTRUCTION_GET_METHODREF(iptr, fmiref);
1048 method_methodref_print(fmiref);
1049 if (fmiref->parseddesc.md->returntype.type != TYPE_VOID) {
1062 SHOW_TARGET(iptr->dst);
1072 SHOW_TARGET(iptr->dst);
1076 case ICMD_INLINE_GOTO:
1077 SHOW_TARGET(iptr->dst);
1081 SHOW_TARGET(iptr->sx.s23.s3.jsrtarget);
1086 case ICMD_IFNONNULL:
1088 SHOW_TARGET(iptr->dst);
1091 case ICMD_IF_ICMPEQ:
1092 case ICMD_IF_ICMPNE:
1093 case ICMD_IF_ICMPLT:
1094 case ICMD_IF_ICMPGE:
1095 case ICMD_IF_ICMPGT:
1096 case ICMD_IF_ICMPLE:
1098 case ICMD_IF_LCMPEQ:
1099 case ICMD_IF_LCMPNE:
1100 case ICMD_IF_LCMPLT:
1101 case ICMD_IF_LCMPGE:
1102 case ICMD_IF_LCMPGT:
1103 case ICMD_IF_LCMPLE:
1105 case ICMD_IF_FCMPEQ:
1106 case ICMD_IF_FCMPNE:
1108 case ICMD_IF_FCMPL_LT:
1109 case ICMD_IF_FCMPL_GE:
1110 case ICMD_IF_FCMPL_GT:
1111 case ICMD_IF_FCMPL_LE:
1113 case ICMD_IF_FCMPG_LT:
1114 case ICMD_IF_FCMPG_GE:
1115 case ICMD_IF_FCMPG_GT:
1116 case ICMD_IF_FCMPG_LE:
1118 case ICMD_IF_DCMPEQ:
1119 case ICMD_IF_DCMPNE:
1121 case ICMD_IF_DCMPL_LT:
1122 case ICMD_IF_DCMPL_GE:
1123 case ICMD_IF_DCMPL_GT:
1124 case ICMD_IF_DCMPL_LE:
1126 case ICMD_IF_DCMPG_LT:
1127 case ICMD_IF_DCMPG_GE:
1128 case ICMD_IF_DCMPG_GT:
1129 case ICMD_IF_DCMPG_LE:
1131 case ICMD_IF_ACMPEQ:
1132 case ICMD_IF_ACMPNE:
1135 SHOW_TARGET(iptr->dst);
1138 case ICMD_TABLESWITCH:
1140 table = iptr->dst.table;
1142 i = iptr->sx.s23.s3.tablehigh
1143 - iptr->sx.s23.s2.tablelow + 1;
1145 printf("high=%d low=%d count=%d\n", iptr->sx.s23.s3.tablehigh, iptr->sx.s23.s2.tablelow, i);
1147 printf("\t\t%d --> ", table - iptr->dst.table);
1148 if (stage >= SHOW_STACK) {
1149 printf("L%03d\n", table->block->nr);
1152 printf("insindex %d (L%03d)\n", table->insindex, BLOCK_OF(table->insindex)->nr);
1159 case ICMD_LOOKUPSWITCH:
1162 printf("count=%d, default=", iptr->sx.s23.s2.lookupcount);
1163 if (stage >= SHOW_STACK) {
1164 printf("L%03d\n", iptr->sx.s23.s3.lookupdefault.block->nr);
1167 printf("insindex %d (L%03d)\n", iptr->sx.s23.s3.lookupdefault.insindex, BLOCK_OF(iptr->sx.s23.s3.lookupdefault.insindex)->nr);
1170 lookup = iptr->dst.lookup;
1171 i = iptr->sx.s23.s2.lookupcount;
1173 printf("\t\t%d --> ", lookup->value);
1174 if (stage >= SHOW_STACK) {
1175 printf("L%03d\n", lookup->target.block->nr);
1178 printf("insindex %d (L%03d)\n", lookup->target.insindex, BLOCK_OF(lookup->target.insindex)->nr);
1200 #endif /* !defined(NDEBUG) */
1204 * These are local overrides for various environment variables in Emacs.
1205 * Please do not remove this and leave it at the end of the file, where
1206 * Emacs will automagically detect them.
1207 * ---------------------------------------------------------------------
1210 * indent-tabs-mode: t
1214 * vim:noexpandtab:sw=4:ts=4: