* src/vm/jit/show.h: New file.
[cacao.git] / src / vm / jit / inline / inline_debug.inc
1 #include <ctype.h>
2
3 #define DEBUG_SLOT(slot)  ((int)((slot) ? ((slot) - iln->ctx->n_debug_stackbase) : (-1)))
4
5 #if 0
6                 printf("linenumbertable_entry %p: pc=%p line=%08x, lntsize=%d, looking for pc=%p\n",
7                                 (void*)lntentry,(void*)lntentry->pc,lntentry->line,lntsize,(void*)pc);
8 #endif
9
10 #if 0
11                                                 printf("inline entry %p: pc=%p line=%08x, lntsize=%d, looking for pc=%p\n",
12                                                                 (void*)lntinline,(void*)lntinline->pc,lntinline->line,lntsize,(void*)pc);
13                                                 printf("\trecurse %p into %p ",(void*)lntinline,(void*)lntinline->pc); method_println((methodinfo *)lntinline->pc);
14 #endif
15
16 #if 0
17         if (oa) {
18             int i;
19
20             for (i=0; i<oa->header.size; ++i) {
21                 printf("\t%i: %p ",i,(void*)oa->data[i]);
22                 if (oa->data[i])
23                     class_println((classinfo *)oa->data[i]);
24                 else
25                     printf("\n");
26             }
27         }
28         printf("GOT CLASS: %p\n",c);
29         if (c) {
30             class_println(c);
31         }
32         printf("GOT CLASSLOADER: %p\n",cl);
33         if (cl) {
34             class_println(cl->vftbl->class);
35         }
36 #endif
37
38 static void debug_dump_inline_context(inline_node *iln)
39 {
40         inline_stack_translation *tr;
41         
42         printf("inline_context @%p: stackbase=%p transstart=%p translationlimit=%p\n",
43                         (void*)iln->ctx,(void*)iln->ctx->n_debug_stackbase,
44                         (void*)iln->ctx->stacktranslationstart,(void*)iln->ctx->o_translationlimit);
45         tr = iln->ctx->stacktranslationstart;
46         while (tr >= iln->ctx->stacktranslation) {
47                 printf("\ttranslate %p -> %d (%p)\n",
48                                 (void*)tr->o_sp,DEBUG_SLOT(tr->n_sp),(void*)tr->n_sp);
49                 tr--;
50         }
51 }
52
53 static void debug_dump_stack(stackptr sp)
54 {
55         while (sp) {
56                 printf("%p (%d) (%01d:%02d:%01x) -> ",(void*)sp,sp->type,sp->varkind,sp->varnum,sp->flags);
57                 sp = sp->prev;
58         }
59         printf("%p",(void*)NULL);
60 }
61
62 static void dump_inline_tree(inline_node *iln)
63 {
64         char indent[100];
65         int i;
66         inline_node *child;
67
68         if (!iln) {
69                 printf("(inline_node *)null\n");
70                 return;
71         }
72
73         for (i=0; i<iln->depth; ++i)
74                 indent[i] = '\t';
75         indent[i] = 0;
76
77         assert(iln->m);
78         if (iln->depth) {
79                 if (!iln->parent) {
80                         printf("parent unset");
81                 }
82                 else {
83                         printf("%s[%d] L%03d %d-%d (ins %d,st %d) (sd=%d,cs=%p,lofs=%d) cum(ins %d,st %d,bb %d) ",
84                                         indent,iln->depth,iln->callerblock->debug_nr,
85                                         iln->callerpc,(int)(iln->callerblock->iinstr - iln->parent->m->basicblocks->iinstr),
86                                         iln->instructioncount,iln->stackcount,iln->n_callerstackdepth,
87                                         (void*)iln->n_callerstack,
88                                         iln->localsoffset,
89                                         iln->cumul_instructioncount,iln->cumul_stackcount,iln->cumul_basicblockcount);
90                 }
91         }
92         else {
93                 printf("%s[%d] MAIN (ins %d,st %d) (cs=%p) cum(ins %d,st %d,bb %d) ",indent,iln->depth,
94                                 iln->instructioncount,iln->stackcount,
95                                 (void*)iln->n_callerstack,
96                                 iln->cumul_instructioncount,iln->cumul_stackcount,iln->cumul_basicblockcount);
97         }
98         method_println(iln->m);
99
100         child = iln->children;
101         if (child) {
102                 do {
103                         dump_inline_tree(child);
104                 }
105                 while ((child = child->next) != iln->children);
106         }
107 }
108
109
110
111 static stackptr first_stackslot_of_block(basicblock *block)
112 {
113         int len;
114         instruction *iptr;
115         stackptr sp;
116         
117         assert(block);
118         if (block->instack)
119                 return block->instack - (block->indepth-1);
120         
121         len = block->icount;
122         iptr = block->iinstr;
123         while (len--) {
124                 if (iptr->dst) {
125                         sp = iptr->dst;
126                         while (sp->prev) {
127                                 sp = sp->prev;
128                         }
129                         return sp;
130                 }
131                 iptr++;
132         }
133
134         return NULL;
135 }
136
137 static void debug_print_stack(inline_node *iln,stackptr sp,int validstackmin,int validstackmax)
138 {
139         int i;
140         int idx;
141         char typechar;
142         char kindchar;
143         
144         printf("[");
145         for (i=0; i<iln->cumul_maxstack; ++i) {
146                 if (sp) {
147                         idx = sp - iln->n_inlined_stack;
148                         switch (sp->type) {
149                                 case TYPE_ADR: typechar = 'a'; break;
150                                 case TYPE_INT: typechar = 'i'; break;
151                                 case TYPE_LNG: typechar = 'l'; break;
152                                 case TYPE_FLT: typechar = 'f'; break;
153                                 case TYPE_DBL: typechar = 'd'; break;
154                                 default:       typechar = '?';
155                         }
156             switch (sp->varkind) {
157                 case STACKVAR: kindchar = 's'; break;
158                 case LOCALVAR: kindchar = 'l'; break;
159                 case TEMPVAR : kindchar = 't'; break;
160                 case UNDEFVAR: kindchar = 'u'; break;
161                 case ARGVAR  : kindchar = 'a'; break;
162                 default:       kindchar = '_'; break;
163             }
164             if (sp->flags & SAVEDVAR)
165                 kindchar = toupper(kindchar);
166                         printf("%c%-3d(%c%-2d:%01x) ",typechar,idx,kindchar,sp->varnum,sp->flags);
167                         sp = sp->prev;
168
169                         assert(idx >= validstackmin);
170                         assert(idx <= validstackmax);
171 #if 0
172                         if (idx < validstackmin || idx > validstackmax) {
173                                 printf("INVALID STACK INDEX: %d\n",idx);
174                         }
175 #endif
176                 }
177                 else {
178                         printf("----        ");
179                 }
180         }
181         printf("] ");
182 }
183
184 static void debug_dump_inlined_code(inline_node *iln,methodinfo *newmethod,codegendata *cd,registerdata *rd)
185 {
186         basicblock *bptr;
187         instruction *iptr;
188         stackptr curstack;
189         stackptr dst;
190         basicblock *nextblock;
191         int len;
192         int validstackmin;
193         int validstackmax;
194         int i;
195         int type;
196
197         printf("INLINED CODE: maxstack=%d maxlocals=%d leafmethod=%d\n",
198                         newmethod->maxstack,newmethod->maxlocals,newmethod->isleafmethod);
199
200         for (i=0; i<newmethod->maxlocals; ++i) {
201             for (type=0; type<5; ++type) {
202                 if (rd->locals[i][type].type < 0)
203                     continue;
204                 printf("\tlocal %d type %d: flags %01x\n",i,type,rd->locals[i][type].flags);
205             }
206         }
207
208         for (i=0; i<newmethod->maxstack; ++i) {
209             for (type=0; type<5; ++type) {
210                 if (rd->interfaces[i][type].type < 0)
211                     continue;
212                 printf("\tinterface %d type %d: flags %01x\n",i,type,rd->interfaces[i][type].flags);
213             }
214         }
215
216         printf("registerdata:\n");
217         printf("\tmemuse = %d\n",rd->memuse);
218         printf("\targintreguse = %d\n",rd->argintreguse);
219         printf("\targfltreguse = %d\n",rd->argfltreguse);
220
221         validstackmin = 0;
222
223         bptr = iln->inlined_basicblocks;
224         for (; bptr; bptr = bptr->next) {
225                 curstack = bptr->instack;
226                 iptr = bptr->iinstr;
227                 len = bptr->icount;
228
229                 nextblock = bptr->next;
230 find_stackmax:
231                 if (nextblock) {
232                         dst = first_stackslot_of_block(nextblock);
233                         if (dst) {
234                                 validstackmax = (dst - iln->n_inlined_stack) - 1;
235                         }
236                         else {
237                                 nextblock = nextblock->next;
238                                 goto find_stackmax;
239                         }
240                 }
241                 else {
242                         validstackmax = 10000; /* XXX */
243                 }
244
245                 debug_print_stack(iln,curstack,validstackmin,validstackmax);
246                 printf("L%03d BLOCK %p indepth=%d outdepth=%d icount=%d stack=[%d,%d] type=%d flags=%d\n",
247                                 bptr->debug_nr,(void*)bptr,bptr->indepth,bptr->outdepth,bptr->icount,
248                                 validstackmin,validstackmax,
249                 bptr->type,bptr->flags);
250
251                 while (len--) {
252                         dst = iptr->dst;
253
254                         debug_print_stack(iln,dst,validstackmin,validstackmax);
255                         printf("     ");
256                         show_icmd(iptr,false); printf("\n");
257
258                         curstack = dst;
259                         iptr++;
260                 }
261                 printf("\n");
262
263                 /* next basic block */
264                 validstackmin = validstackmax + 1;
265         }
266 }