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 utf_display_printable_ascii_classname(ex->catchtype.ref->name);
179 utf_display_printable_ascii_classname(ex->catchtype.cls->name);
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) {
284 printf("Stack slots: (memuse=%d)\n", rd->memuse);
285 for (i=0; i<rd->memuse; ++i) {
286 printf(" M%02d: ", i);
287 for (j=0; j<jd->varcount; ++j) {
288 varinfo *v = jd->var + j;
289 if ((v->flags & INMEMORY) && (v->regoff == i)) {
290 show_variable(jd, j, stage);
301 if (code->rplpoints) {
302 printf("Replacement Points:\n");
303 replace_show_replacement_points(code);
307 #if defined(ENABLE_DISASSEMBLER)
308 /* show code before first basic block */
310 if ((stage >= SHOW_CODE) && JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
311 u1ptr = (u1 *) ((ptrint) code->mcode + cd->dseglen);
313 for (; u1ptr < (u1 *) ((ptrint) code->mcode + cd->dseglen + jd->new_basicblocks[0].mpc);)
320 /* show code of all basic blocks */
322 for (bptr = jd->new_basicblocks; bptr != NULL; bptr = bptr->next)
323 new_show_basicblock(jd, bptr, stage);
325 #if defined(ENABLE_DISASSEMBLER)
326 /* show stubs code */
328 if (stage >= SHOW_CODE && opt_showdisassemble && opt_showexceptionstubs) {
329 printf("\nStubs code:\n");
330 printf("Length: %d\n\n", (s4) (code->mcodelength -
331 ((ptrint) cd->dseglen + lastbptr->mpc)));
333 u1ptr = (u1 *) ((ptrint) code->mcode + cd->dseglen + lastbptr->mpc);
335 for (; (ptrint) u1ptr < ((ptrint) code->mcode + code->mcodelength);)
342 LOCK_MONITOR_EXIT(show_global_lock);
344 /* finally flush the output */
348 #endif /* !defined(NDEBUG) */
351 /* show_basicblock *************************************************************
353 Print the intermediate representation of a basic block.
355 NOTE: Currently this function may only be called after register allocation!
357 *******************************************************************************/
360 void new_show_basicblock(jitdata *jd, basicblock *bptr, int stage)
370 /* get required compiler data */
376 if (bptr->flags != BBDELETED) {
377 deadcode = bptr->flags <= BBREACHED;
379 printf("======== %sL%03d ======== (flags: %d, bitflags: %01x, next: %d, type: ",
380 (bptr->bitflags & BBFLAG_REPLACEMENT) ? "<REPLACE> " : "",
381 bptr->nr, bptr->flags, bptr->bitflags,
382 (bptr->next) ? (bptr->next->nr) : -1);
384 switch (bptr->type) {
396 printf(", instruction count: %d, predecessors: %d [ ",
397 bptr->icount, bptr->predecessorcount);
399 for (i = 0; i < bptr->predecessorcount; i++)
400 printf("%d ", bptr->predecessors[i]->nr);
404 if (stage >= SHOW_STACK) {
406 new_show_variable_array(jd, bptr->invars, bptr->indepth, stage);
412 for (i = 0; i < bptr->icount; i++, iptr++) {
413 printf("%4d: ", iptr->line);
415 new_show_icmd(jd, iptr, deadcode, stage);
419 if (stage >= SHOW_STACK) {
421 new_show_variable_array(jd, bptr->outvars, bptr->outdepth, stage);
425 #if defined(ENABLE_DISASSEMBLER)
426 if ((stage >= SHOW_CODE) && JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd) &&
430 u1ptr = (u1 *) (code->mcode + cd->dseglen + bptr->mpc);
432 if (bptr->next != NULL) {
433 for (; u1ptr < (u1 *) (code->mcode + cd->dseglen + bptr->next->mpc);)
438 for (; u1ptr < (u1 *) (code->mcode + code->mcodelength);)
446 #endif /* !defined(NDEBUG) */
449 /* show_icmd *******************************************************************
451 Print the intermediate representation of an instruction.
453 NOTE: Currently this function may only be called after register allocation!
455 *******************************************************************************/
459 #define SHOW_TARGET(target) \
460 if (stage >= SHOW_STACK) { \
461 printf("--> L%03d ", (target).block->nr); \
463 else if (stage >= SHOW_PARSE) { \
464 printf("--> insindex %d (L%03d) ", (target).insindex, \
465 jd->new_basicblocks[jd->new_basicblockindex[ \
466 (target).insindex]].nr); \
469 printf("--> insindex %d ", (target).insindex); \
472 #define SHOW_INT_CONST(val) \
473 if (stage >= SHOW_PARSE) { \
474 printf("%ld ", (long) (val)); \
480 #define SHOW_LNG_CONST(val) \
481 if (stage >= SHOW_PARSE) { \
482 printf("%lld ", (long long)(val)); \
488 #define SHOW_FLT_CONST(val) \
489 if (stage >= SHOW_PARSE) { \
490 printf("%g ", (val)); \
496 #define SHOW_DBL_CONST(val) \
497 if (stage >= SHOW_PARSE) { \
498 printf("%g ", (val)); \
504 #define SHOW_INDEX(index) \
505 if (stage >= SHOW_PARSE) { \
506 printf("%d ", index); \
512 #define SHOW_STRING(val) \
513 if (stage >= SHOW_PARSE) { \
515 utf_display_printable_ascii( \
516 javastring_toutf((java_lang_String *)(val), false)); \
523 #define SHOW_CLASSREF_OR_CLASSINFO(c) \
524 if (stage >= SHOW_PARSE) { \
525 if (IS_CLASSREF(c)) \
526 class_classref_print(c.ref); \
528 class_print(c.cls); \
535 #define SHOW_FIELD(fmiref) \
536 if (stage >= SHOW_PARSE) { \
537 field_fieldref_print(fmiref); \
544 #define SHOW_VARIABLE(v) \
545 show_variable(jd, (v), stage)
547 #define SHOW_S1(iptr) \
548 if (stage >= SHOW_STACK) { \
549 SHOW_VARIABLE(iptr->s1.varindex); \
552 #define SHOW_S2(iptr) \
553 if (stage >= SHOW_STACK) { \
554 SHOW_VARIABLE(iptr->sx.s23.s2.varindex); \
557 #define SHOW_S3(iptr) \
558 if (stage >= SHOW_STACK) { \
559 SHOW_VARIABLE(iptr->sx.s23.s3.varindex); \
562 #define SHOW_DST(iptr) \
563 if (stage >= SHOW_STACK) { \
565 SHOW_VARIABLE(iptr->dst.varindex); \
568 #define SHOW_S1_LOCAL(iptr) \
569 if (stage >= SHOW_STACK) { \
570 printf("L%d ", iptr->s1.varindex); \
573 #define SHOW_DST_LOCAL(iptr) \
574 if (stage >= SHOW_STACK) { \
575 printf("=> L%d ", iptr->dst.varindex); \
578 static void show_allocation(s4 type, s4 flags, s4 regoff)
580 if (flags & INMEMORY) {
581 printf("M%02d", regoff);
585 #ifdef HAS_ADDRESS_REGISTER_FILE
586 if (type == TYPE_ADR) {
587 printf("R%02d", regoff);
592 if (IS_FLT_DBL_TYPE(type)) {
593 printf("F%02d", regoff);
597 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
598 if (IS_2_WORD_TYPE(type)) {
599 # if defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER)
600 # if defined(ENABLE_INTRP)
602 printf("%3d/%3d", GET_LOW_REG(regoff),
603 GET_HIGH_REG(regoff));
606 printf("%3s/%3s", regs[GET_LOW_REG(regoff)],
607 regs[GET_HIGH_REG(regoff)]);
609 printf("%3d/%3d", GET_LOW_REG(regoff),
610 GET_HIGH_REG(regoff));
614 #endif /* defined(SUPPORT_COMBINE_INTEGER_REGISTERS) */
616 #if defined(ENABLE_JIT) && defined(ENABLE_DISASSEMBLER)
617 # if defined(ENABLE_INTRP)
619 printf("%3d", regoff);
622 printf("%3s", regs[regoff]);
624 printf("%3d", regoff);
628 static void show_variable(jitdata *jd, s4 index, int stage)
634 v = &(jd->var[index]);
637 case TYPE_INT: type = 'i'; break;
638 case TYPE_LNG: type = 'l'; break;
639 case TYPE_FLT: type = 'f'; break;
640 case TYPE_DBL: type = 'd'; break;
641 case TYPE_ADR: type = 'a'; break;
645 if (index < jd->localcount) {
647 if (v->flags & (PREALLOC | OUTVAR))
648 printf("<INVALID FLAGS!>");
651 if (v->flags & PREALLOC) {
653 if (v->flags & OUTVAR)
654 printf("<INVALID FLAGS!>");
656 else if (v->flags & OUTVAR)
662 printf("%c%c%d", kind, type, index);
664 if (v->flags & SAVEDVAR)
667 if (stage >= SHOW_REGS) {
669 show_allocation(v->type, v->flags, v->regoff);
676 static void new_show_variable_array(jitdata *jd, s4 *vars, int n, int stage)
681 for (i=0; i<n; ++i) {
684 show_variable(jd, vars[i], stage);
689 void new_show_icmd(jitdata *jd, instruction *iptr, bool deadcode, int stage)
692 branch_target_t *table;
693 lookup_target_t *lookup;
694 constant_FMIref *fmiref;
698 /* get the opcode and the condition */
702 printf("%s ", icmd_names[opcode]);
704 if (stage < SHOW_PARSE)
710 /* Print the condition for conditional instructions. */
712 /* XXX print condition from flags */
714 if (iptr->flags.bits & INS_FLAG_UNRESOLVED)
715 printf("(UNRESOLVED) ");
721 case ICMD_CHECKNULL_POP:
726 case ICMD_ARRAYLENGTH:
793 /* binary/const INT */
805 case ICMD_IUSHRCONST:
808 case ICMD_LUSHRCONST:
810 SHOW_INT_CONST(iptr->sx.val.i);
814 /* ?ASTORECONST (trinary/const INT) */
815 case ICMD_IASTORECONST:
816 case ICMD_BASTORECONST:
817 case ICMD_CASTORECONST:
818 case ICMD_SASTORECONST:
821 SHOW_INT_CONST(iptr->sx.s23.s3.constval);
826 SHOW_INT_CONST(iptr->sx.val.i);
830 /* binary/const LNG */
841 SHOW_LNG_CONST(iptr->sx.val.l);
845 /* trinary/const LNG (<= pointer size) */
846 case ICMD_LASTORECONST:
849 SHOW_LNG_CONST(iptr->sx.s23.s3.constval);
854 SHOW_LNG_CONST(iptr->sx.val.l);
860 SHOW_FLT_CONST(iptr->sx.val.f);
866 SHOW_DBL_CONST(iptr->sx.val.d);
872 if (iptr->flags.bits & INS_FLAG_CLASS) {
873 SHOW_CLASSREF_OR_CLASSINFO(iptr->sx.val.c);
875 else if (iptr->sx.val.anyptr == NULL) {
879 SHOW_STRING(iptr->sx.val.stringconst);
884 case ICMD_AASTORECONST:
887 printf("%p ", (void*) iptr->sx.s23.s3.constval);
890 case ICMD_GETFIELD: /* 1 -> 1 */
891 case ICMD_PUTFIELD: /* 2 -> 0 */
892 case ICMD_PUTSTATIC: /* 1 -> 0 */
893 case ICMD_GETSTATIC: /* 0 -> 1 */
894 case ICMD_PUTSTATICCONST: /* 0 -> 0 */
895 case ICMD_PUTFIELDCONST: /* 1 -> 0 */
896 if (opcode != ICMD_GETSTATIC && opcode != ICMD_PUTSTATICCONST) {
898 if (opcode == ICMD_PUTFIELD) {
902 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
905 if (opcode == ICMD_GETSTATIC || opcode == ICMD_GETFIELD) {
912 SHOW_INT_CONST(iptr->sx.val.i);
913 SHOW_DST_LOCAL(iptr);
961 SHOW_DST_LOCAL(iptr);
976 case ICMD_MULTIANEWARRAY:
977 if (stage >= SHOW_STACK) {
978 argp = iptr->sx.s23.s2.args;
979 i = iptr->s1.argcount;
981 SHOW_VARIABLE(*(argp++));
985 printf("argcount=%d ", iptr->s1.argcount);
995 case ICMD_INSTANCEOF:
1000 case ICMD_INLINE_START:
1001 case ICMD_INLINE_END:
1005 if (stage >= SHOW_STACK) {
1006 argp = iptr->sx.s23.s2.args;
1007 i = iptr->s1.argcount;
1009 if ((iptr->s1.argcount - 1 - i) == iptr->sx.s23.s3.bte->md->paramcount)
1010 printf(" pass-through: ");
1011 SHOW_VARIABLE(*(argp++));
1014 printf("%s ", iptr->sx.s23.s3.bte->cname);
1015 if (iptr->sx.s23.s3.bte->md->returntype.type != TYPE_VOID) {
1020 case ICMD_INVOKEVIRTUAL:
1021 case ICMD_INVOKESPECIAL:
1022 case ICMD_INVOKESTATIC:
1023 case ICMD_INVOKEINTERFACE:
1024 if (stage >= SHOW_STACK) {
1026 INSTRUCTION_GET_METHODDESC(iptr, md);
1027 argp = iptr->sx.s23.s2.args;
1028 i = iptr->s1.argcount;
1030 if ((iptr->s1.argcount - 1 - i) == md->paramcount)
1031 printf(" pass-through: ");
1032 SHOW_VARIABLE(*(argp++));
1035 INSTRUCTION_GET_METHODREF(iptr, fmiref);
1036 method_methodref_print(fmiref);
1037 if (fmiref->parseddesc.md->returntype.type != TYPE_VOID) {
1050 SHOW_TARGET(iptr->dst);
1060 SHOW_TARGET(iptr->dst);
1064 case ICMD_INLINE_GOTO:
1065 SHOW_TARGET(iptr->dst);
1069 SHOW_TARGET(iptr->sx.s23.s3.jsrtarget);
1074 case ICMD_IFNONNULL:
1076 SHOW_TARGET(iptr->dst);
1079 case ICMD_IF_ICMPEQ:
1080 case ICMD_IF_ICMPNE:
1081 case ICMD_IF_ICMPLT:
1082 case ICMD_IF_ICMPGE:
1083 case ICMD_IF_ICMPGT:
1084 case ICMD_IF_ICMPLE:
1086 case ICMD_IF_LCMPEQ:
1087 case ICMD_IF_LCMPNE:
1088 case ICMD_IF_LCMPLT:
1089 case ICMD_IF_LCMPGE:
1090 case ICMD_IF_LCMPGT:
1091 case ICMD_IF_LCMPLE:
1093 case ICMD_IF_FCMPEQ:
1094 case ICMD_IF_FCMPNE:
1096 case ICMD_IF_FCMPL_LT:
1097 case ICMD_IF_FCMPL_GE:
1098 case ICMD_IF_FCMPL_GT:
1099 case ICMD_IF_FCMPL_LE:
1101 case ICMD_IF_FCMPG_LT:
1102 case ICMD_IF_FCMPG_GE:
1103 case ICMD_IF_FCMPG_GT:
1104 case ICMD_IF_FCMPG_LE:
1106 case ICMD_IF_DCMPEQ:
1107 case ICMD_IF_DCMPNE:
1109 case ICMD_IF_DCMPL_LT:
1110 case ICMD_IF_DCMPL_GE:
1111 case ICMD_IF_DCMPL_GT:
1112 case ICMD_IF_DCMPL_LE:
1114 case ICMD_IF_DCMPG_LT:
1115 case ICMD_IF_DCMPG_GE:
1116 case ICMD_IF_DCMPG_GT:
1117 case ICMD_IF_DCMPG_LE:
1119 case ICMD_IF_ACMPEQ:
1120 case ICMD_IF_ACMPNE:
1123 SHOW_TARGET(iptr->dst);
1126 case ICMD_TABLESWITCH:
1128 table = iptr->dst.table;
1130 i = iptr->sx.s23.s3.tablehigh
1131 - iptr->sx.s23.s2.tablelow + 1;
1133 printf("high=%d low=%d count=%d\n", iptr->sx.s23.s3.tablehigh, iptr->sx.s23.s2.tablelow, i);
1135 printf("\t\t%d --> ", table - iptr->dst.table);
1136 if (stage >= SHOW_STACK) {
1137 printf("L%03d\n", table->block->nr);
1140 printf("insindex %d (L%03d)\n", table->insindex, BLOCK_OF(table->insindex)->nr);
1147 case ICMD_LOOKUPSWITCH:
1150 printf("count=%d, default=", iptr->sx.s23.s2.lookupcount);
1151 if (stage >= SHOW_STACK) {
1152 printf("L%03d\n", iptr->sx.s23.s3.lookupdefault.block->nr);
1155 printf("insindex %d (L%03d)\n", iptr->sx.s23.s3.lookupdefault.insindex, BLOCK_OF(iptr->sx.s23.s3.lookupdefault.insindex)->nr);
1158 lookup = iptr->dst.lookup;
1159 i = iptr->sx.s23.s2.lookupcount;
1161 printf("\t\t%d --> ", lookup->value);
1162 if (stage >= SHOW_STACK) {
1163 printf("L%03d\n", lookup->target.block->nr);
1166 printf("insindex %d (L%03d)\n", lookup->target.insindex, BLOCK_OF(lookup->target.insindex)->nr);
1188 #endif /* !defined(NDEBUG) */
1192 * These are local overrides for various environment variables in Emacs.
1193 * Please do not remove this and leave it at the end of the file, where
1194 * Emacs will automagically detect them.
1195 * ---------------------------------------------------------------------
1198 * indent-tabs-mode: t
1202 * vim:noexpandtab:sw=4:ts=4: