Initial revision
[cacao.git] / ncomp / nstack.c
1 /****************************** ncomp/nstack.c *********************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Parser for JavaVM to intermediate code translation
8         
9         Authors: Andreas  Krall      EMAIL: cacao@complang.tuwien.ac.at
10
11         Last Change: 1997/11/18
12
13 *******************************************************************************/
14
15
16 #ifdef STATISTICS
17 #define COUNT(cnt) cnt++
18 #else
19 #define COUNT(cnt)
20 #endif
21
22 #define STACKRESET {curstack=0;stackdepth=0;}
23
24 #define TYPEPANIC  {show_icmd_method();panic("Stack type mismatch");}
25 #define CURKIND    curstack->varkind
26 #define CURTYPE    curstack->type
27
28 #define NEWSTACK(s,v,n) {new->prev=curstack;new->type=s;new->flags=0;\
29                         new->varkind=v;new->varnum=n;curstack=new;new++;}
30 #define NEWSTACKn(s,n)  NEWSTACK(s,UNDEFVAR,n)
31 #define NEWSTACK0(s)    NEWSTACK(s,UNDEFVAR,0)
32 #define NEWXSTACK   {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
33
34 #define SETDST      {iptr->dst=curstack;}
35 #define POP(s)      {if(s!=curstack->type){TYPEPANIC;}\
36                      if(curstack->varkind==UNDEFVAR)curstack->varkind=TEMPVAR;\
37                      curstack=curstack->prev;}
38 #define POPANY      {if(curstack->varkind==UNDEFVAR)curstack->varkind=TEMPVAR;\
39                      curstack=curstack->prev;}
40 #define COPY(s,d)   {(d)->flags=0;(d)->type=(s)->type;\
41                      (d)->varkind=(s)->varkind;(d)->varnum=(s)->varnum;}
42
43 #define CONST(s)    {NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
44 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
45 #define STORE(s)    {POP(s);SETDST;stackdepth--;}
46 #define OP1_0(s)    {POP(s);SETDST;stackdepth--;}
47 #define OP1_0ANY    {POPANY;SETDST;stackdepth--;}
48 #define OP0_1(s)    {NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
49 #define OP1_1(s,d)  {POP(s);NEWSTACKn(d,stackdepth-1);SETDST;}
50 #define OP2_0(s)    {POP(s);POP(s);SETDST;stackdepth-=2;}
51 #define OPTT2_0(t,b){POP(t);POP(b);SETDST;stackdepth-=2;}
52 #define OP2_1(s)    {POP(s);POP(s);NEWSTACKn(s,stackdepth-2);SETDST;stackdepth--;}
53 #define OP2IAT_1(s) {POP(TYPE_INT);POP(TYPE_ADR);NEWSTACKn(s,stackdepth-2);\
54                      SETDST;stackdepth--;}
55 #define OP2IT_1(s)  {POP(TYPE_INT);POP(s);NEWSTACKn(s,stackdepth-2);\
56                      SETDST;stackdepth--;}
57 #define OPTT2_1(s,d){POP(s);POP(s);NEWSTACKn(d,stackdepth-2);SETDST;stackdepth--;}
58 #define OP2_2(s)    {POP(s);POP(s);NEWSTACKn(s,stackdepth-2);\
59                      NEWSTACKn(s,stackdepth-1);SETDST;}
60 #define OP3TIA_0(s) {POP(s);POP(TYPE_INT);POP(TYPE_ADR);SETDST;stackdepth-=3;}
61 #define OP3_0(s)    {POP(s);POP(s);POP(s);SETDST;stackdepth-=3;}
62 #define POPMANY(i)  {stackdepth-=i;while(--i>=0){POPANY;}SETDST;}
63 #define DUP         {NEWSTACK(CURTYPE,CURKIND,curstack->varnum);SETDST;\
64                     stackdepth++;}
65 #define SWAP        {COPY(curstack,new);POPANY;COPY(curstack,new+1);POPANY;\
66                     new[0].prev=curstack;new[1].prev=new;\
67                     curstack=new+1;new+=2;SETDST;}
68 #define DUP_X1      {COPY(curstack,new);COPY(curstack,new+2);POPANY;\
69                     COPY(curstack,new+1);POPANY;new[0].prev=curstack;\
70                     new[1].prev=new;new[2].prev=new+1;\
71                     curstack=new+2;new+=3;SETDST;stackdepth++;}
72 #define DUP2_X1     {COPY(curstack,new+1);COPY(curstack,new+4);POPANY;\
73                     COPY(curstack,new);COPY(curstack,new+3);POPANY;\
74                     COPY(curstack,new+2);POPANY;new[0].prev=curstack;\
75                     new[1].prev=new;new[2].prev=new+1;\
76                     new[3].prev=new+2;new[4].prev=new+3;\
77                     curstack=new+4;new+=5;SETDST;stackdepth+=2;}
78 #define DUP_X2      {COPY(curstack,new);COPY(curstack,new+3);POPANY;\
79                     COPY(curstack,new+2);POPANY;COPY(curstack,new+1);POPANY;\
80                     new[0].prev=curstack;new[1].prev=new;\
81                     new[2].prev=new+1;new[3].prev=new+2;\
82                     curstack=new+3;new+=4;SETDST;stackdepth++;}
83 #define DUP2_X2     {COPY(curstack,new+1);COPY(curstack,new+5);POPANY;\
84                     COPY(curstack,new);COPY(curstack,new+4);POPANY;\
85                     COPY(curstack,new+3);POPANY;COPY(curstack,new+2);POPANY;\
86                     new[0].prev=curstack;new[1].prev=new;\
87                     new[2].prev=new+1;new[3].prev=new+2;\
88                     new[4].prev=new+3;new[5].prev=new+4;\
89                     curstack=new+5;new+=6;SETDST;stackdepth+=2;}
90
91 #define COPYCURSTACK(copy) {\
92         int d;\
93         stackptr s;\
94         if(curstack){\
95                 s=curstack;\
96                 new+=stackdepth;\
97                 d=stackdepth;\
98                 copy=new;\
99                 while(s){\
100                         copy--;d--;\
101                         copy->prev=copy-1;\
102                         copy->type=s->type;\
103                         copy->flags=0;\
104                         copy->varkind=STACKVAR;\
105                         copy->varnum=d;\
106                         s=s->prev;\
107                         }\
108                 copy->prev=NULL;\
109                 copy=new-1;\
110                 }\
111         else\
112                 copy=NULL;\
113 }
114
115
116 #define BBEND(s,i){\
117         i=stackdepth-1;\
118         copy=s;\
119         while(copy){\
120                 if((copy->varkind==STACKVAR)&&(copy->varnum>i))\
121                         copy->varkind=TEMPVAR;\
122                 else {\
123                         copy->varkind=STACKVAR;\
124                         copy->varnum=i;\
125                         }\
126                 interfaces[i][copy->type].type = copy->type;\
127                 interfaces[i][copy->type].flags |= copy->flags;\
128                 i--;copy=copy->prev;\
129                 }\
130         i=bptr->indepth-1;\
131         copy=bptr->instack;\
132         while(copy){\
133                 interfaces[i][copy->type].type = copy->type;\
134                 if(copy->varkind==STACKVAR){\
135                         if (copy->flags & SAVEDVAR)\
136                                 interfaces[i][copy->type].flags |= SAVEDVAR;\
137                         }\
138                 i--;copy=copy->prev;\
139                 }\
140 }
141
142         
143 #define MARKREACHED(b,c) {\
144         if(b->flags<0)\
145                 {COPYCURSTACK(c);b->flags=0;b->instack=c;b->indepth=stackdepth;}\
146         else {stackptr s=curstack;stackptr t=b->instack;\
147                 if(b->indepth!=stackdepth)\
148                         {show_icmd_method();panic("Stack depth mismatch");}\
149                 while(s){if (s->type!=t->type)\
150                                 TYPEPANIC\
151                         s=s->prev;t=t->prev;\
152                         }\
153                 }\
154 }
155
156
157 static void show_icmd_method();
158
159 static void analyse_stack()
160 {
161         int b_count;
162         int stackdepth;
163         stackptr curstack, new, copy;
164         int opcode, i, len, loops;
165         int superblockend, repeat, deadcode;
166         instruction *iptr = instr;
167         basicblock *bptr, *tbptr;
168         s4  *s4ptr;
169         
170         arguments_num = 0;
171         new = stack;
172         loops = 0;
173         block[0].flags = BBREACHED;
174         block[0].instack = 0;
175         block[0].indepth = 0;
176
177         for (i = 0; i < exceptiontablelength; i++) {
178                 bptr = &block[block_index[extable[i].handlerpc]];
179                 bptr->flags = BBREACHED;
180                 bptr->type = BBTYPE_EXH;
181                 bptr->instack = new;
182                 bptr->indepth = 1;
183                 STACKRESET;
184                 NEWXSTACK;
185                 }
186
187         do {
188                 loops++;
189                 b_count = block_count;
190                 bptr = block;
191                 superblockend = true;
192                 repeat = false;
193                 STACKRESET;
194                 deadcode = true;
195                 while (--b_count >= 0) {
196                         if (superblockend && (bptr->flags < BBREACHED))
197                                 repeat = true;
198                         else if (bptr->flags <= BBREACHED) {
199                                 if (superblockend)
200                                         stackdepth = bptr->indepth;
201                                 else if (bptr->flags < BBREACHED) {
202                                         COPYCURSTACK(copy);
203                                         bptr->instack = copy;
204                                         bptr->indepth = stackdepth;
205                                         }
206                                 else if (bptr->indepth != stackdepth) {
207                                         show_icmd_method();
208                                         panic("Stack depth mismatch");
209                                         
210                                         }
211                                 curstack = bptr->instack;
212                                 deadcode = false;
213                                 superblockend = false;
214                                 bptr->flags = BBFINISHED;
215                                 i = bptr[0].ipc;
216                                 len = bptr[1].ipc - i;
217                                 iptr = &instr[i];
218                                 while (--len >= 0)  {
219                                         opcode = iptr->opc;
220                                         switch (opcode) {
221
222                                                 /* pop 0 push 0 */
223
224                                                 case ICMD_NOP:
225                                                 case ICMD_CHECKASIZE:
226                                                         SETDST;
227                                                         break;
228                                                 case ICMD_RET:
229                                                         locals[iptr->op1][TYPE_ADR].type = TYPE_ADR;
230                                                 case ICMD_RETURN:
231                                                         COUNT(count_pcmd_return);
232                                                         SETDST;
233                                                         superblockend = true;
234                                                         break;
235
236                                                 /* pop 0 push 1 const */
237                                                 
238                                                 case ICMD_ICONST:
239                                                         COUNT(count_pcmd_load);
240                                                         if (len > 0) {
241                                                                 switch (iptr[1].opc) {
242                                                                         case ICMD_IADD:
243                                                                                 iptr[0].opc = ICMD_IADDCONST;
244 icmd_iconst_tail:
245                                                                                 iptr[1].opc = ICMD_NOP;
246                                                                                 OP1_1(TYPE_INT,TYPE_INT);
247                                                                                 COUNT(count_pcmd_op);
248                                                                                 break;
249                                                                         case ICMD_ISUB:
250                                                                                 iptr[0].opc = ICMD_ISUBCONST;
251                                                                                 goto icmd_iconst_tail;
252                                                                         case ICMD_IMUL:
253                                                                                 iptr[0].opc = ICMD_IMULCONST;
254                                                                                 goto icmd_iconst_tail;
255                                                                         case ICMD_IAND:
256                                                                                 iptr[0].opc = ICMD_IANDCONST;
257                                                                                 goto icmd_iconst_tail;
258                                                                         case ICMD_IOR:
259                                                                                 iptr[0].opc = ICMD_IORCONST;
260                                                                                 goto icmd_iconst_tail;
261                                                                         case ICMD_IXOR:
262                                                                                 iptr[0].opc = ICMD_IXORCONST;
263                                                                                 goto icmd_iconst_tail;
264                                                                         case ICMD_ISHL:
265                                                                                 iptr[0].opc = ICMD_ISHLCONST;
266                                                                                 goto icmd_iconst_tail;
267                                                                         case ICMD_ISHR:
268                                                                                 iptr[0].opc = ICMD_ISHRCONST;
269                                                                                 goto icmd_iconst_tail;
270                                                                         case ICMD_IUSHR:
271                                                                                 iptr[0].opc = ICMD_IUSHRCONST;
272                                                                                 goto icmd_iconst_tail;
273                                                                         case ICMD_IF_ICMPEQ:
274                                                                                 iptr[0].opc = ICMD_IFEQ;
275 icmd_if_icmp_tail:
276                                                                                 iptr[0].op1 = iptr[1].op1;
277                                                                                 iptr[1].opc = ICMD_NOP;
278                                                                                 OP1_0(TYPE_INT);
279                                                                                 tbptr = block + block_index[iptr->op1];
280                                                                                 MARKREACHED(tbptr, copy);
281                                                                                 COUNT(count_pcmd_bra);
282                                                                                 break;
283                                                                         case ICMD_IF_ICMPLT:
284                                                                                 iptr[0].opc = ICMD_IFLT;
285                                                                                 goto icmd_if_icmp_tail;
286                                                                         case ICMD_IF_ICMPLE:
287                                                                                 iptr[0].opc = ICMD_IFLE;
288                                                                                 goto icmd_if_icmp_tail;
289                                                                         case ICMD_IF_ICMPNE:
290                                                                                 iptr[0].opc = ICMD_IFNE;
291                                                                                 goto icmd_if_icmp_tail;
292                                                                         case ICMD_IF_ICMPGT:
293                                                                                 iptr[0].opc = ICMD_IFGT;
294                                                                                 goto icmd_if_icmp_tail;
295                                                                         case ICMD_IF_ICMPGE:
296                                                                                 iptr[0].opc = ICMD_IFGE;
297                                                                                 goto icmd_if_icmp_tail;
298                                                                         default:
299                                                                                 CONST(TYPE_INT);
300                                                                         }
301                                                                 }
302                                                         else
303                                                                 CONST(TYPE_INT);
304                                                         break;
305                                                 case ICMD_LCONST:
306                                                         COUNT(count_pcmd_load);
307                                                         if (len > 0) {
308                                                                 switch (iptr[1].opc) {
309                                                                         case ICMD_LADD:
310                                                                                 iptr[0].opc = ICMD_LADDCONST;
311 icmd_lconst_tail:
312                                                                                 iptr[1].opc = ICMD_NOP;
313                                                                                 OP1_1(TYPE_LNG,TYPE_LNG);
314                                                                                 COUNT(count_pcmd_op);
315                                                                                 break;
316                                                                         case ICMD_LSUB:
317                                                                                 iptr[0].opc = ICMD_LSUBCONST;
318                                                                                 goto icmd_lconst_tail;
319                                                                         case ICMD_LMUL:
320                                                                                 iptr[0].opc = ICMD_LMULCONST;
321                                                                                 goto icmd_lconst_tail;
322                                                                         case ICMD_LAND:
323                                                                                 iptr[0].opc = ICMD_LANDCONST;
324                                                                                 goto icmd_lconst_tail;
325                                                                         case ICMD_LOR:
326                                                                                 iptr[0].opc = ICMD_LORCONST;
327                                                                                 goto icmd_lconst_tail;
328                                                                         case ICMD_LXOR:
329                                                                                 iptr[0].opc = ICMD_LXORCONST;
330                                                                                 goto icmd_lconst_tail;
331                                                                         case ICMD_LSHL:
332                                                                                 iptr[0].opc = ICMD_LSHLCONST;
333                                                                                 goto icmd_lconst_tail;
334                                                                         case ICMD_LSHR:
335                                                                                 iptr[0].opc = ICMD_LSHRCONST;
336                                                                                 goto icmd_lconst_tail;
337                                                                         case ICMD_LUSHR:
338                                                                                 iptr[0].opc = ICMD_LUSHRCONST;
339                                                                                 goto icmd_lconst_tail;
340                                                                         case ICMD_LCMP:
341                                                                                 if ((len > 1) && (iptr[2].val.i == 0)) {
342                                                                                         switch (iptr[2].opc) {
343                                                                                         case ICMD_IFEQ:
344                                                                                                 iptr[0].opc = ICMD_IF_LEQ;
345 icmd_lconst_lcmp_tail:
346                                                                                                 iptr[0].op1 = iptr[2].op1;
347                                                                                                 iptr[1].opc = ICMD_NOP;
348                                                                                                 iptr[2].opc = ICMD_NOP;
349                                                                                                 OP1_0(TYPE_LNG);
350                                                                                                 tbptr = block + block_index[iptr->op1];
351                                                                                                 MARKREACHED(tbptr, copy);
352                                                                                                 COUNT(count_pcmd_bra);
353                                                                                                 COUNT(count_pcmd_op);
354                                                                                                 break;
355                                                                                         case ICMD_IFNE:
356                                                                                                 iptr[0].opc = ICMD_IF_LNE;
357                                                                                                 goto icmd_lconst_lcmp_tail;
358                                                                                         case ICMD_IFLT:
359                                                                                                 iptr[0].opc = ICMD_IF_LLT;
360                                                                                                 goto icmd_lconst_lcmp_tail;
361                                                                                         case ICMD_IFGT:
362                                                                                                 iptr[0].opc = ICMD_IF_LGT;
363                                                                                                 goto icmd_lconst_lcmp_tail;
364                                                                                         case ICMD_IFLE:
365                                                                                                 iptr[0].opc = ICMD_IF_LLE;
366                                                                                                 goto icmd_lconst_lcmp_tail;
367                                                                                         case ICMD_IFGE:
368                                                                                                 iptr[0].opc = ICMD_IF_LGE;
369                                                                                                 goto icmd_lconst_lcmp_tail;
370                                                                                         default:
371                                                                                                 CONST(TYPE_LNG);
372                                                                                         } /* switch (iptr[2].opc) */
373                                                                                         } /* if (iptr[2].val.i == 0) */
374                                                                                 else
375                                                                                         CONST(TYPE_LNG);
376                                                                                 break;
377                                                                         default:
378                                                                                 CONST(TYPE_LNG);
379                                                                         }
380                                                                 }
381                                                         else
382                                                                 CONST(TYPE_LNG);
383                                                         break;
384                                                 case ICMD_FCONST:
385                                                         COUNT(count_pcmd_load);
386                                                         CONST(TYPE_FLT);
387                                                         break;
388                                                 case ICMD_DCONST:
389                                                         COUNT(count_pcmd_load);
390                                                         CONST(TYPE_DBL);
391                                                         break;
392                                                 case ICMD_ACONST:
393                                                         COUNT(count_pcmd_load);
394                                                         CONST(TYPE_ADR);
395                                                         break;
396
397                                                 /* pop 0 push 1 load */
398                                                 
399                                                 case ICMD_ILOAD:
400                                                 case ICMD_LLOAD:
401                                                 case ICMD_FLOAD:
402                                                 case ICMD_DLOAD:
403                                                 case ICMD_ALOAD:
404                                                         COUNT(count_load_instruction);
405                                                         i = opcode-ICMD_ILOAD;
406                                                         locals[iptr->op1][i].type = i;
407                                                         LOAD(i, LOCALVAR, iptr->op1);
408                                                         break;
409
410                                                 /* pop 2 push 1 */
411
412                                                 case ICMD_IALOAD:
413                                                 case ICMD_LALOAD:
414                                                 case ICMD_FALOAD:
415                                                 case ICMD_DALOAD:
416                                                 case ICMD_AALOAD:
417                                                         COUNT(count_check_null);
418                                                         COUNT(count_check_bound);
419                                                         COUNT(count_pcmd_mem);
420                                                         OP2IAT_1(opcode-ICMD_IALOAD);
421                                                         break;
422
423                                                 case ICMD_BALOAD:
424                                                 case ICMD_CALOAD:
425                                                 case ICMD_SALOAD:
426                                                         COUNT(count_check_null);
427                                                         COUNT(count_check_bound);
428                                                         COUNT(count_pcmd_mem);
429                                                         OP2IAT_1(TYPE_INT);
430                                                         break;
431
432                                                 /* pop 0 push 0 iinc */
433
434                                                 case ICMD_IINC:
435 #ifdef STATISTICS
436                                                         i = stackdepth;
437                                                         if (i >= 10)
438                                                                 count_store_depth[10]++;
439                                                         else
440                                                                 count_store_depth[i]++;
441 #endif
442                                                         copy = curstack;
443                                                         i = stackdepth - 1;
444                                                         while (copy) {
445                                                                 if ((copy->varkind == LOCALVAR) &&
446                                                                     (copy->varnum == curstack->varnum)) {
447                                                                         copy->varkind = TEMPVAR;
448                                                                         copy->varnum = i;
449                                                                         }
450                                                                 i--;
451                                                                 copy = copy->prev;
452                                                                 }
453                                                         SETDST;
454                                                         break;
455
456                                                 /* pop 1 push 0 store */
457
458                                                 case ICMD_ISTORE:
459                                                 case ICMD_LSTORE:
460                                                 case ICMD_FSTORE:
461                                                 case ICMD_DSTORE:
462                                                 case ICMD_ASTORE:
463                                                         i = opcode-ICMD_ISTORE;
464                                                         locals[iptr->op1][i].type = i;
465 #ifdef STATISTICS
466                                                         count_pcmd_store++;
467                                                         i = new - curstack;
468                                                         if (i >= 20)
469                                                                 count_store_length[20]++;
470                                                         else
471                                                                 count_store_length[i]++;
472                                                         i = stackdepth - 1;
473                                                         if (i >= 10)
474                                                                 count_store_depth[10]++;
475                                                         else
476                                                                 count_store_depth[i]++;
477 #endif
478                                                         copy = curstack->prev;
479                                                         i = stackdepth - 2;
480                                                         while (copy) {
481                                                                 if ((copy->varkind == LOCALVAR) &&
482                                                                     (copy->varnum == curstack->varnum)) {
483                                                                         copy->varkind = TEMPVAR;
484                                                                         copy->varnum = i;
485                                                                         }
486                                                                 i--;
487                                                                 copy = copy->prev;
488                                                                 }
489                                                         if ((new - curstack) == 1) {
490                                                                 curstack->varkind = LOCALVAR;
491                                                                 curstack->varnum = iptr->op1;
492                                                                 };
493                                                         STORE(opcode-ICMD_ISTORE);
494                                                         break;
495
496                                                 /* pop 3 push 0 */
497
498                                                 case ICMD_IASTORE:
499                                                 case ICMD_LASTORE:
500                                                 case ICMD_FASTORE:
501                                                 case ICMD_DASTORE:
502                                                 case ICMD_AASTORE:
503                                                         COUNT(count_check_null);
504                                                         COUNT(count_check_bound);
505                                                         COUNT(count_pcmd_mem);
506                                                         OP3TIA_0(opcode-ICMD_IASTORE);
507                                                         break;
508                                                 case ICMD_BASTORE:
509                                                 case ICMD_CASTORE:
510                                                 case ICMD_SASTORE:
511                                                         COUNT(count_check_null);
512                                                         COUNT(count_check_bound);
513                                                         COUNT(count_pcmd_mem);
514                                                         OP3TIA_0(TYPE_INT);
515                                                         break;
516
517                                                 /* pop 1 push 0 */
518
519                                                 case ICMD_POP:
520                                                         OP1_0ANY;
521                                                         break;
522
523                                                 case ICMD_IRETURN:
524                                                 case ICMD_LRETURN:
525                                                 case ICMD_FRETURN:
526                                                 case ICMD_DRETURN:
527                                                 case ICMD_ARETURN:
528                                                         COUNT(count_pcmd_return);
529                                                         OP1_0(opcode-ICMD_IRETURN);
530                                                         superblockend = true;
531                                                         break;
532
533                                                 case ICMD_ATHROW:
534                                                         COUNT(count_check_null);
535                                                         OP1_0(TYPE_ADR);
536                                                         STACKRESET;
537                                                         SETDST;
538                                                         superblockend = true;
539                                                         break;
540
541                                                 case ICMD_PUTSTATIC:
542                                                         COUNT(count_pcmd_mem);
543                                                         OP1_0(iptr->op1);
544                                                         break;
545
546                                                 /* pop 1 push 0 branch */
547
548                                                 case ICMD_IFNULL:
549                                                 case ICMD_IFNONNULL:
550                                                         COUNT(count_pcmd_bra);
551                                                         OP1_0(TYPE_ADR);
552                                                         tbptr = block + block_index[iptr->op1];
553                                                         MARKREACHED(tbptr, copy);
554                                                         break;
555
556                                                 case ICMD_IFEQ:
557                                                 case ICMD_IFNE:
558                                                 case ICMD_IFLT:
559                                                 case ICMD_IFGE:
560                                                 case ICMD_IFGT:
561                                                 case ICMD_IFLE:
562                                                         COUNT(count_pcmd_bra);
563                                                         OP1_0(TYPE_INT);
564                                                         tbptr = block + block_index[iptr->op1];
565                                                         MARKREACHED(tbptr, copy);
566                                                         break;
567
568                                                 /* pop 0 push 0 branch */
569
570                                                 case ICMD_GOTO:
571                                                         COUNT(count_pcmd_bra);
572                                                         tbptr = block + block_index[iptr->op1];
573                                                         MARKREACHED(tbptr, copy);
574                                                         SETDST;
575                                                         superblockend = true;
576                                                         break;
577
578                                                 /* pop 1 push 0 table branch */
579
580                                                 case ICMD_TABLESWITCH:
581                                                         COUNT(count_pcmd_table);
582                                                         OP1_0(TYPE_INT);
583                                                         s4ptr = iptr->val.a;
584                                                         tbptr = block + block_index[*s4ptr++]; /* default */
585                                                         MARKREACHED(tbptr, copy);
586                                                         i = *s4ptr++;                          /* low     */
587                                                         i = *s4ptr++ - i + 1;                  /* high    */
588                                                         while (--i >= 0) {
589                                                                 tbptr = block + block_index[*s4ptr++];
590                                                                 MARKREACHED(tbptr, copy);
591                                                                 }
592                                                         SETDST;
593                                                         superblockend = true;
594                                                         break;
595                                                         
596                                                 /* pop 1 push 0 table branch */
597
598                                                 case ICMD_LOOKUPSWITCH:
599                                                         COUNT(count_pcmd_table);
600                                                         OP1_0(TYPE_INT);
601                                                         s4ptr = iptr->val.a;
602                                                         tbptr = block + block_index[*s4ptr++]; /* default */
603                                                         MARKREACHED(tbptr, copy);
604                                                         i = *s4ptr++;                          /* count   */
605                                                         while (--i >= 0) {
606                                                                 tbptr = block + block_index[s4ptr[1]];
607                                                                 MARKREACHED(tbptr, copy);
608                                                                 s4ptr += 2;
609                                                                 }
610                                                         SETDST;
611                                                         superblockend = true;
612                                                         break;
613
614                                                 case ICMD_NULLCHECKPOP:
615                                                 case ICMD_MONITORENTER:
616                                                         COUNT(count_check_null);
617                                                 case ICMD_MONITOREXIT:
618                                                         OP1_0(TYPE_ADR);
619                                                         break;
620
621                                                 /* pop 2 push 0 branch */
622
623                                                 case ICMD_IF_ICMPEQ:
624                                                 case ICMD_IF_ICMPNE:
625                                                 case ICMD_IF_ICMPLT:
626                                                 case ICMD_IF_ICMPGE:
627                                                 case ICMD_IF_ICMPGT:
628                                                 case ICMD_IF_ICMPLE:
629                                                         COUNT(count_pcmd_bra);
630                                                         OP2_0(TYPE_INT);
631                                                         tbptr = block + block_index[iptr->op1];
632                                                         MARKREACHED(tbptr, copy);
633                                                         break;
634
635                                                 case ICMD_IF_ACMPEQ:
636                                                 case ICMD_IF_ACMPNE:
637                                                         COUNT(count_pcmd_bra);
638                                                         OP2_0(TYPE_ADR);
639                                                         tbptr = block + block_index[iptr->op1];
640                                                         MARKREACHED(tbptr, copy);
641                                                         break;
642
643                                                 /* pop 2 push 0 */
644
645                                                 case ICMD_PUTFIELD:
646                                                         COUNT(count_check_null);
647                                                         COUNT(count_pcmd_mem);
648                                                         OPTT2_0(iptr->op1,TYPE_ADR);
649                                                         break;
650
651                                                 case ICMD_POP2:
652                                                         if (! IS_2_WORD_TYPE(curstack->type)) {
653                                                                 OP1_0ANY;                /* second pop */
654                                                                 }
655                                                         else
656                                                                 iptr->opc = ICMD_POP;
657                                                         OP1_0ANY;
658                                                         break;
659
660                                                 /* pop 0 push 1 dup */
661                                                 
662                                                 case ICMD_DUP:
663                                                         COUNT(count_dup_instruction);
664                                                         DUP;
665                                                         break;
666
667                                                 case ICMD_DUP2:
668                                                         if (IS_2_WORD_TYPE(curstack->type)) {
669                                                                 iptr->opc = ICMD_DUP;
670                                                                 DUP;
671                                                                 }
672                                                         else {
673                                                                 copy = curstack;
674                                                                 NEWSTACK(copy[-1].type, copy[-1].varkind,
675                                                                          copy[-1].varnum);
676                                                                 NEWSTACK(copy[ 0].type, copy[ 0].varkind,
677                                                                          copy[ 0].varnum);
678                                                                 SETDST;
679                                                                 stackdepth+=2;
680                                                                 }
681                                                         break;
682
683                                                 /* pop 2 push 3 dup */
684                                                 
685                                                 case ICMD_DUP_X1:
686                                                         DUP_X1;
687                                                         break;
688
689                                                 case ICMD_DUP2_X1:
690                                                         if (IS_2_WORD_TYPE(curstack->type)) {
691                                                                 iptr->opc = ICMD_DUP_X1;
692                                                                 DUP_X1;
693                                                                 }
694                                                         else {
695                                                                 DUP2_X1;
696                                                                 }
697                                                         break;
698
699                                                 /* pop 3 push 4 dup */
700                                                 
701                                                 case ICMD_DUP_X2:
702                                                         if (IS_2_WORD_TYPE(curstack[-1].type)) {
703                                                                 iptr->opc = ICMD_DUP_X1;
704                                                                 DUP_X1;
705                                                                 }
706                                                         else {
707                                                                 DUP_X2;
708                                                                 }
709                                                         break;
710
711                                                 case ICMD_DUP2_X2:
712                                                         if (IS_2_WORD_TYPE(curstack->type)) {
713                                                                 if (IS_2_WORD_TYPE(curstack[-1].type)) {
714                                                                         iptr->opc = ICMD_DUP_X1;
715                                                                         DUP_X1;
716                                                                         }
717                                                                 else {
718                                                                         iptr->opc = ICMD_DUP_X2;
719                                                                         DUP_X2;
720                                                                         }
721                                                                 }
722                                                         else
723                                                                 if (IS_2_WORD_TYPE(curstack[-2].type)) {
724                                                                         iptr->opc = ICMD_DUP2_X1;
725                                                                         DUP2_X1;
726                                                                         }
727                                                                 else {
728                                                                         DUP2_X2;
729                                                                         }
730                                                         break;
731
732                                                 /* pop 2 push 2 swap */
733                                                 
734                                                 case ICMD_SWAP:
735                                                         SWAP;
736                                                         break;
737
738                                                 /* pop 2 push 1 */
739                                                 
740                                                 case ICMD_IADD:
741                                                 case ICMD_ISUB:
742                                                 case ICMD_IMUL:
743                                                 case ICMD_IDIV:
744                                                 case ICMD_IREM:
745
746                                                 case ICMD_ISHL:
747                                                 case ICMD_ISHR:
748                                                 case ICMD_IUSHR:
749                                                 case ICMD_IAND:
750                                                 case ICMD_IOR:
751                                                 case ICMD_IXOR:
752                                                         COUNT(count_pcmd_op);
753                                                         OP2_1(TYPE_INT);
754                                                         break;
755
756                                                 case ICMD_LADD:
757                                                 case ICMD_LSUB:
758                                                 case ICMD_LMUL:
759                                                 case ICMD_LDIV:
760                                                 case ICMD_LREM:
761
762                                                 case ICMD_LOR:
763                                                 case ICMD_LAND:
764                                                 case ICMD_LXOR:
765                                                         COUNT(count_pcmd_op);
766                                                         OP2_1(TYPE_LNG);
767                                                         break;
768
769                                                 case ICMD_LSHL:
770                                                 case ICMD_LSHR:
771                                                 case ICMD_LUSHR:
772                                                         COUNT(count_pcmd_op);
773                                                         OP2IT_1(TYPE_LNG);
774                                                         break;
775
776                                                 case ICMD_FADD:
777                                                 case ICMD_FSUB:
778                                                 case ICMD_FMUL:
779                                                 case ICMD_FDIV:
780                                                 case ICMD_FREM:
781                                                         COUNT(count_pcmd_op);
782                                                         OP2_1(TYPE_FLT);
783                                                         break;
784
785                                                 case ICMD_DADD:
786                                                 case ICMD_DSUB:
787                                                 case ICMD_DMUL:
788                                                 case ICMD_DDIV:
789                                                 case ICMD_DREM:
790                                                         COUNT(count_pcmd_op);
791                                                         OP2_1(TYPE_DBL);
792                                                         break;
793
794                                                 case ICMD_LCMP:
795                                                         COUNT(count_pcmd_op);
796                                                         if ((len > 0) && (iptr[1].val.i == 0)) {
797                                                                 switch (iptr[1].opc) {
798                                                                         case ICMD_IFEQ:
799                                                                                 iptr[0].opc = ICMD_IF_LCMPEQ;
800 icmd_lcmp_if_tail:
801                                                                                 iptr[0].op1 = iptr[1].op1;
802                                                                                 iptr[1].opc = ICMD_NOP;
803                                                                                 OP2_0(TYPE_LNG);
804                                                                                 tbptr = block + block_index[iptr->op1];
805                                                                                 MARKREACHED(tbptr, copy);
806                                                                                 COUNT(count_pcmd_bra);
807                                                                                 break;
808                                                                         case ICMD_IFNE:
809                                                                                 iptr[0].opc = ICMD_IF_LCMPNE;
810                                                                                 goto icmd_lcmp_if_tail;
811                                                                         case ICMD_IFLT:
812                                                                                 iptr[0].opc = ICMD_IF_LCMPLT;
813                                                                                 goto icmd_lcmp_if_tail;
814                                                                         case ICMD_IFGT:
815                                                                                 iptr[0].opc = ICMD_IF_LCMPGT;
816                                                                                 goto icmd_lcmp_if_tail;
817                                                                         case ICMD_IFLE:
818                                                                                 iptr[0].opc = ICMD_IF_LCMPLE;
819                                                                                 goto icmd_lcmp_if_tail;
820                                                                         case ICMD_IFGE:
821                                                                                 iptr[0].opc = ICMD_IF_LCMPGE;
822                                                                                 goto icmd_lcmp_if_tail;
823                                                                         default:
824                                                                                 OPTT2_1(TYPE_LNG, TYPE_INT);
825                                                                         }
826                                                                 }
827                                                         else
828                                                                 OPTT2_1(TYPE_LNG, TYPE_INT);
829                                                         break;
830                                                 case ICMD_FCMPL:
831                                                 case ICMD_FCMPG:
832                                                         COUNT(count_pcmd_op);
833                                                         OPTT2_1(TYPE_FLT, TYPE_INT);
834                                                         break;
835                                                 case ICMD_DCMPL:
836                                                 case ICMD_DCMPG:
837                                                         COUNT(count_pcmd_op);
838                                                         OPTT2_1(TYPE_DBL, TYPE_INT);
839                                                         break;
840
841                                                 /* pop 1 push 1 */
842                                                 
843                                                 case ICMD_INEG:
844                                                 case ICMD_INT2BYTE:
845                                                 case ICMD_INT2CHAR:
846                                                 case ICMD_INT2SHORT:
847                                                         COUNT(count_pcmd_op);
848                                                         OP1_1(TYPE_INT, TYPE_INT);
849                                                         break;
850                                                 case ICMD_LNEG:
851                                                         COUNT(count_pcmd_op);
852                                                         OP1_1(TYPE_LNG, TYPE_LNG);
853                                                         break;
854                                                 case ICMD_FNEG:
855                                                         COUNT(count_pcmd_op);
856                                                         OP1_1(TYPE_FLT, TYPE_FLT);
857                                                         break;
858                                                 case ICMD_DNEG:
859                                                         COUNT(count_pcmd_op);
860                                                         OP1_1(TYPE_DBL, TYPE_DBL);
861                                                         break;
862
863                                                 case ICMD_I2L:
864                                                         COUNT(count_pcmd_op);
865                                                         OP1_1(TYPE_INT, TYPE_LNG);
866                                                         break;
867                                                 case ICMD_I2F:
868                                                         COUNT(count_pcmd_op);
869                                                         OP1_1(TYPE_INT, TYPE_FLT);
870                                                         break;
871                                                 case ICMD_I2D:
872                                                         COUNT(count_pcmd_op);
873                                                         OP1_1(TYPE_INT, TYPE_DBL);
874                                                         break;
875                                                 case ICMD_L2I:
876                                                         COUNT(count_pcmd_op);
877                                                         OP1_1(TYPE_LNG, TYPE_INT);
878                                                         break;
879                                                 case ICMD_L2F:
880                                                         COUNT(count_pcmd_op);
881                                                         OP1_1(TYPE_LNG, TYPE_FLT);
882                                                         break;
883                                                 case ICMD_L2D:
884                                                         COUNT(count_pcmd_op);
885                                                         OP1_1(TYPE_LNG, TYPE_DBL);
886                                                         break;
887                                                 case ICMD_F2I:
888                                                         COUNT(count_pcmd_op);
889                                                         OP1_1(TYPE_FLT, TYPE_INT);
890                                                         break;
891                                                 case ICMD_F2L:
892                                                         COUNT(count_pcmd_op);
893                                                         OP1_1(TYPE_FLT, TYPE_LNG);
894                                                         break;
895                                                 case ICMD_F2D:
896                                                         COUNT(count_pcmd_op);
897                                                         OP1_1(TYPE_FLT, TYPE_DBL);
898                                                         break;
899                                                 case ICMD_D2I:
900                                                         COUNT(count_pcmd_op);
901                                                         OP1_1(TYPE_DBL, TYPE_INT);
902                                                         break;
903                                                 case ICMD_D2L:
904                                                         COUNT(count_pcmd_op);
905                                                         OP1_1(TYPE_DBL, TYPE_LNG);
906                                                         break;
907                                                 case ICMD_D2F:
908                                                         COUNT(count_pcmd_op);
909                                                         OP1_1(TYPE_DBL, TYPE_FLT);
910                                                         break;
911
912                                                 case ICMD_CHECKCAST:
913                                                         OP1_1(TYPE_ADR, TYPE_ADR);
914                                                         break;
915
916                                                 case ICMD_ARRAYLENGTH:
917                                                 case ICMD_INSTANCEOF:
918                                                         OP1_1(TYPE_ADR, TYPE_INT);
919                                                         break;
920
921                                                 case ICMD_NEWARRAY:
922                                                 case ICMD_ANEWARRAY:
923                                                         OP1_1(TYPE_INT, TYPE_ADR);
924                                                         break;
925
926                                                 case ICMD_GETFIELD:
927                                                         COUNT(count_check_null);
928                                                         COUNT(count_pcmd_mem);
929                                                         OP1_1(TYPE_ADR, iptr->op1);
930                                                         break;
931
932                                                 /* pop 0 push 1 */
933                                                 
934                                                 case ICMD_GETSTATIC:
935                                                         COUNT(count_pcmd_mem);
936                                                         OP0_1(iptr->op1);
937                                                         break;
938
939                                                 case ICMD_NEW:
940                                                         OP0_1(TYPE_ADR);
941                                                         break;
942
943                                                 case ICMD_JSR:
944                                                         OP0_1(TYPE_ADR);
945                                                         tbptr = block + block_index[iptr->op1];
946                                                         tbptr->type=BBTYPE_SBR;
947                                                         MARKREACHED(tbptr, copy);
948                                                         OP1_0ANY;
949                                                         break;
950
951                                                 /* pop many push any */
952                                                 
953                                                 case ICMD_INVOKEVIRTUAL:
954                                                 case ICMD_INVOKESPECIAL:
955                                                 case ICMD_INVOKEINTERFACE:
956                                                 case ICMD_INVOKESTATIC:
957                                                         COUNT(count_pcmd_met);
958                                                         {
959                                                         methodinfo *m = iptr->val.a;
960                                                         if (m->flags & ACC_STATIC)
961                                                                 {COUNT(count_check_null);}
962                                                         i = iptr->op1;
963                                                         if (i > arguments_num)
964                                                                 arguments_num = i;
965                                                         copy = curstack;
966                                                         while (--i >= 0) {
967                                                                 if (! (copy->flags & SAVEDVAR)) {
968                                                                         copy->varkind = ARGVAR;
969                                                                         copy->varnum = i;
970                                                                         }
971                                                                 copy = copy->prev;
972                                                                 }
973                                                         while (copy) {
974                                                                 copy->flags |= SAVEDVAR;
975                                                                 copy = copy->prev;
976                                                                 }
977                                                         i = iptr->op1;
978                                                         POPMANY(i);
979                                                         if (m->returntype != TYPE_VOID) {
980                                                                 OP0_1(m->returntype);
981                                                                 }
982                                                         break;
983                                                         }
984
985                                                 case ICMD_BUILTIN3:
986                                                         if (! (curstack->flags & SAVEDVAR)) {
987                                                                 curstack->varkind = ARGVAR;
988                                                                 curstack->varnum = 2;
989                                                                 }
990                                                         OP1_0ANY;
991                                                 case ICMD_BUILTIN2:
992                                                         if (! (curstack->flags & SAVEDVAR)) {
993                                                                 curstack->varkind = ARGVAR;
994                                                                 curstack->varnum = 1;
995                                                                 }
996                                                         OP1_0ANY;
997                                                 case ICMD_BUILTIN1:
998                                                         if (! (curstack->flags & SAVEDVAR)) {
999                                                                 curstack->varkind = ARGVAR;
1000                                                                 curstack->varnum = 0;
1001                                                                 }
1002                                                         OP1_0ANY;
1003                                                         copy = curstack;
1004                                                         while (copy) {
1005                                                                 copy->flags |= SAVEDVAR;
1006                                                                 copy = copy->prev;
1007                                                                 }
1008                                                         if (iptr->op1 != TYPE_VOID)
1009                                                                 OP0_1(iptr->op1);
1010                                                         break;
1011
1012                                                 case ICMD_MULTIANEWARRAY:
1013                                                         i = iptr->op1;
1014                                                         if ((i + intreg_argnum) > arguments_num)
1015                                                                 arguments_num = i + intreg_argnum;
1016                                                         copy = curstack;
1017                                                         while (--i >= 0) {
1018                                                                 if (! (copy->flags & SAVEDVAR)) {
1019                                                                         copy->varkind = ARGVAR;
1020                                                                         copy->varnum = i + intreg_argnum;
1021                                                                         }
1022                                                                 copy = copy->prev;
1023                                                                 }
1024                                                         while (copy) {
1025                                                                 copy->flags |= SAVEDVAR;
1026                                                                 copy = copy->prev;
1027                                                                 }
1028                                                         i = iptr->op1;
1029                                                         POPMANY(i);
1030                                                         OP0_1(TYPE_ADR);
1031                                                         break;
1032
1033                                                 default:
1034                                                         printf("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
1035                                                         panic("Missing ICMD code during stack analysis");
1036                                                 } /* switch */
1037                                         iptr++;
1038                                         } /* while instructions */
1039                                 bptr->outstack = curstack;
1040                                 bptr->outdepth = stackdepth;
1041                                 BBEND(curstack, i);
1042                                 } /* if */
1043                         else
1044                                 superblockend = true;
1045                         bptr++;
1046                 } /* while blocks */
1047         } while (repeat && ! deadcode);
1048
1049 #ifdef STATISTICS
1050         if (block_count > count_max_basic_blocks)
1051                 count_max_basic_blocks = block_count;
1052         count_basic_blocks += block_count;
1053         if (block[block_count].ipc > count_max_javainstr)
1054                 count_max_javainstr = block[block_count].ipc;
1055         count_javainstr += block[block_count].ipc;
1056         if (stack_count > count_upper_bound_new_stack)
1057                 count_upper_bound_new_stack = stack_count;
1058         if ((new - stack) > count_max_new_stack)
1059                 count_max_new_stack = (new - stack);
1060
1061         b_count = block_count;
1062         bptr = block;
1063         while (--b_count >= 0) {
1064                 if (bptr->flags > BBREACHED) {
1065                         if (bptr->indepth >= 10)
1066                                 count_block_stack[10]++;
1067                         else
1068                                 count_block_stack[bptr->indepth]++;
1069                         len = bptr[1].ipc - bptr[0].ipc;
1070                         if (len <= 10) 
1071                                 count_block_size_distribution[len - 1]++;
1072                         else if (len <= 12)
1073                                 count_block_size_distribution[10]++;
1074                         else if (len <= 14)
1075                                 count_block_size_distribution[11]++;
1076                         else if (len <= 16)
1077                                 count_block_size_distribution[12]++;
1078                         else if (len <= 18)
1079                                 count_block_size_distribution[13]++;
1080                         else if (len <= 20)
1081                                 count_block_size_distribution[14]++;
1082                         else if (len <= 25)
1083                                 count_block_size_distribution[15]++;
1084                         else if (len <= 30)
1085                                 count_block_size_distribution[16]++;
1086                         else
1087                                 count_block_size_distribution[17]++;
1088                         }
1089                 bptr++;
1090                 }
1091
1092         if (loops == 1)
1093                 count_analyse_iterations[0]++;
1094         else if (loops == 2)
1095                 count_analyse_iterations[1]++;
1096         else if (loops == 3)
1097                 count_analyse_iterations[2]++;
1098         else if (loops == 4)
1099                 count_analyse_iterations[3]++;
1100         else
1101                 count_analyse_iterations[4]++;
1102
1103         if (block_count <= 5)
1104                 count_method_bb_distribution[0]++;
1105         else if (block_count <= 10)
1106                 count_method_bb_distribution[1]++;
1107         else if (block_count <= 15)
1108                 count_method_bb_distribution[2]++;
1109         else if (block_count <= 20)
1110                 count_method_bb_distribution[3]++;
1111         else if (block_count <= 30)
1112                 count_method_bb_distribution[4]++;
1113         else if (block_count <= 40)
1114                 count_method_bb_distribution[5]++;
1115         else if (block_count <= 50)
1116                 count_method_bb_distribution[6]++;
1117         else if (block_count <= 75)
1118                 count_method_bb_distribution[7]++;
1119         else
1120                 count_method_bb_distribution[8]++;
1121 #endif
1122 }
1123
1124
1125 static void print_stack(stackptr s) {
1126         int i, j;
1127         stackptr t;
1128
1129         i = maxstack;
1130         t = s;
1131         
1132         while (t) {
1133                 i--;
1134                 t = t->prev;
1135                 }
1136         j = maxstack - i;
1137         while (--i >= 0)
1138                 printf("    ");
1139         while (s) {
1140                 j--;
1141                 if (s->flags & SAVEDVAR)
1142                         switch (s->varkind) {
1143                                 case TEMPVAR:
1144                                         if (s->flags & INMEMORY)
1145                                                 printf(" m%02d", s->regoff);
1146                                         else
1147                                                 printf(" r%02d", s->regoff);
1148                                         break;
1149                                 case STACKVAR:
1150                                         printf(" s%02d", s->varnum);
1151                                         break;
1152                                 case LOCALVAR:
1153                                         printf(" l%02d", s->varnum);
1154                                         break;
1155                                 case ARGVAR:
1156                                         printf(" a%02d", s->varnum);
1157                                         break;
1158                                 default:
1159                                         printf(" !%02d", j);
1160                                 }
1161                 else
1162                         switch (s->varkind) {
1163                                 case TEMPVAR:
1164                                         if (s->flags & INMEMORY)
1165                                                 printf(" M%02d", s->regoff);
1166                                         else
1167                                                 printf(" R%02d", s->regoff);
1168                                         break;
1169                                 case STACKVAR:
1170                                         printf(" S%02d", s->varnum);
1171                                         break;
1172                                 case LOCALVAR:
1173                                         printf(" L%02d", s->varnum);
1174                                         break;
1175                                 case ARGVAR:
1176                                         printf(" A%02d", s->varnum);
1177                                         break;
1178                                 default:
1179                                         printf(" ?%02d", j);
1180                                 }
1181                 s = s->prev;
1182                 }
1183 }
1184
1185
1186 static void print_reg(stackptr s) {
1187         if (s) {
1188                 if (s->flags & SAVEDVAR)
1189                         switch (s->varkind) {
1190                                 case TEMPVAR:
1191                                         if (s->flags & INMEMORY)
1192                                                 printf(" tm%02d", s->regoff);
1193                                         else
1194                                                 printf(" tr%02d", s->regoff);
1195                                         break;
1196                                 case STACKVAR:
1197                                         printf(" s %02d", s->varnum);
1198                                         break;
1199                                 case LOCALVAR:
1200                                         printf(" l %02d", s->varnum);
1201                                         break;
1202                                 case ARGVAR:
1203                                         printf(" a %02d", s->varnum);
1204                                         break;
1205                                 default:
1206                                         printf(" ! %02d", s->varnum);
1207                                 }
1208                 else
1209                         switch (s->varkind) {
1210                                 case TEMPVAR:
1211                                         if (s->flags & INMEMORY)
1212                                                 printf(" Tm%02d", s->regoff);
1213                                         else
1214                                                 printf(" Tr%02d", s->regoff);
1215                                         break;
1216                                 case STACKVAR:
1217                                         printf(" S %02d", s->varnum);
1218                                         break;
1219                                 case LOCALVAR:
1220                                         printf(" L %02d", s->varnum);
1221                                         break;
1222                                 case ARGVAR:
1223                                         printf(" A %02d", s->varnum);
1224                                         break;
1225                                 default:
1226                                         printf(" ? %02d", s->varnum);
1227                                 }
1228                 }
1229         else
1230                 printf("     ");
1231                 
1232 }
1233
1234
1235 static char *builtin_name(functionptr bptr)
1236 {
1237         builtin_descriptor *bdesc = builtin_desc;
1238         while ((bdesc->bptr != NULL) && (bdesc->bptr != bptr))
1239                 bdesc++;
1240         return bdesc->name;
1241 }
1242
1243
1244 static void show_icmd_method()
1245 {
1246         int b, i, j;
1247         int deadcode;
1248         s4  *s4ptr;
1249         
1250         printf("\n");
1251         unicode_fprint(stdout, class->name);
1252         printf(".");
1253         unicode_fprint(stdout, method->name);
1254         printf(" ");
1255         unicode_fprint(stdout, method->descriptor);
1256         printf ("\n\nMax locals: %d\n", (int) maxlocals);
1257         printf ("Max stack:  %d\n", (int) maxstack);
1258
1259         printf ("Exceptions:\n");
1260         for (i = 0; i < exceptiontablelength; i++) {
1261                 printf("    L%03d ... ", block_index[extable[i].startpc]);
1262                 printf("L%03d = ", block_index[extable[i].endpc]);
1263                 printf("L%03d\n", block_index[extable[i].handlerpc]);
1264                 }
1265         
1266         printf ("Local Table:\n");
1267         for (i = 0; i < maxlocals; i++) {
1268                 printf("   %3d: ", i);
1269                 for (j = TYPE_INT; j <= TYPE_ADR; j++)
1270                         if (locals[i][j].type >= 0) {
1271                                 printf("   (%d) ", j);
1272                                 if (locals[i][j].flags)
1273                                         printf("m");
1274                                 else
1275                                         printf("r");
1276                                 printf("%2d", locals[i][j].regoff);
1277                                 }
1278                 printf("\n");
1279                 }
1280         printf("\n");
1281
1282         printf ("Interface Table:\n");
1283         for (i = 0; i < maxstack; i++) {
1284                 if ((interfaces[i][0].type >= 0) || (interfaces[i][1].type >= 0) ||
1285                     (interfaces[i][2].type >= 0) || (interfaces[i][3].type >= 0) ||
1286                     (interfaces[i][4].type >= 0)) {
1287                         printf("   %3d: ", i);
1288                         for (j = TYPE_INT; j <= TYPE_ADR; j++)
1289                                 if (interfaces[i][j].type >= 0) {
1290                                         printf("   (%d) ", j);
1291                                         if (interfaces[i][j].flags & SAVEDVAR)
1292                                                 printf("s");
1293                                         else
1294                                                 printf("t");
1295                                         if (interfaces[i][j].flags & INMEMORY)
1296                                                 printf("m");
1297                                         else
1298                                                 printf("r");
1299                                         printf("%2d", interfaces[i][j].regoff);
1300                                         }
1301                         printf("\n");
1302                         }
1303                 }
1304         printf("\n");
1305         for (b = 0; b < block_count; b++) {
1306                 deadcode = block[b].flags <= BBREACHED;
1307                 printf("[");
1308                 if (deadcode)
1309                         for (j = maxstack; j > 0; j--)
1310                                 printf(" ?  ");
1311                 else
1312                         print_stack(block[b].instack);
1313                 printf("] L%03d:\n", b);
1314                 for (i = block[b].ipc; i < block[b + 1].ipc; i++) {
1315                         printf("[");
1316                         if (deadcode) {
1317                                 for (j = maxstack; j > 0; j--)
1318                                         printf(" ?  ");
1319                                 }
1320                         else
1321                                 print_stack(instr[i].dst);
1322                         printf("]     %4d  %s", i, icmd_names[instr[i].opc]);
1323                         switch ((int) instr[i].opc) {
1324                                 case ICMD_IADDCONST:
1325                                 case ICMD_ISUBCONST:
1326                                 case ICMD_IMULCONST:
1327                                 case ICMD_IANDCONST:
1328                                 case ICMD_IORCONST:
1329                                 case ICMD_IXORCONST:
1330                                 case ICMD_ISHLCONST:
1331                                 case ICMD_ISHRCONST:
1332                                 case ICMD_IUSHRCONST:
1333                                 case ICMD_ICONST:
1334                                 case ICMD_GETFIELD:
1335                                 case ICMD_PUTFIELD:
1336                                         printf(" %d", instr[i].val.i);
1337                                         break;
1338                                 case ICMD_LADDCONST:
1339                                 case ICMD_LSUBCONST:
1340                                 case ICMD_LMULCONST:
1341                                 case ICMD_LANDCONST:
1342                                 case ICMD_LORCONST:
1343                                 case ICMD_LXORCONST:
1344                                 case ICMD_LSHLCONST:
1345                                 case ICMD_LSHRCONST:
1346                                 case ICMD_LUSHRCONST:
1347                                 case ICMD_LCONST:
1348                                         printf(" %ld", instr[i].val.l);
1349                                         break;
1350                                 case ICMD_FCONST:
1351                                         printf(" %f", instr[i].val.f);
1352                                         break;
1353                                 case ICMD_DCONST:
1354                                         printf(" %f", instr[i].val.d);
1355                                         break;
1356                                 case ICMD_ACONST:
1357                                 case ICMD_PUTSTATIC:
1358                                 case ICMD_GETSTATIC:
1359                                         printf(" %p", instr[i].val.a);
1360                                         break;
1361                                 case ICMD_IINC:
1362                                         printf(" %d + %d", instr[i].op1, instr[i].val.i);
1363                                         break;
1364                                 case ICMD_RET:
1365                                 case ICMD_ILOAD:
1366                                 case ICMD_LLOAD:
1367                                 case ICMD_FLOAD:
1368                                 case ICMD_DLOAD:
1369                                 case ICMD_ALOAD:
1370                                 case ICMD_ISTORE:
1371                                 case ICMD_LSTORE:
1372                                 case ICMD_FSTORE:
1373                                 case ICMD_DSTORE:
1374                                 case ICMD_ASTORE:
1375                                         printf(" %d", instr[i].op1);
1376                                         break;
1377                                 case ICMD_NEW:
1378                                         printf(" ");
1379                                         unicode_fprint(stdout,
1380                                                        ((classinfo *) instr[i].val.a)->name);
1381                                         break;
1382                                 case ICMD_NEWARRAY:
1383                                         switch (instr[i].op1) {
1384                                                 case 4:
1385                                                         printf(" boolean");
1386                                                         break;
1387                                                 case 5:
1388                                                         printf(" char");
1389                                                         break;
1390                                                 case 6:
1391                                                         printf(" float");
1392                                                         break;
1393                                                 case 7:
1394                                                         printf(" double");
1395                                                         break;
1396                                                 case 8:
1397                                                         printf(" byte");
1398                                                         break;
1399                                                 case 9:
1400                                                         printf(" short");
1401                                                         break;
1402                                                 case 10:
1403                                                         printf(" int");
1404                                                         break;
1405                                                 case 11:
1406                                                         printf(" long");
1407                                                         break;
1408                                                 }
1409                                         break;
1410                                 case ICMD_ANEWARRAY:
1411                                         if (instr[i].op1) {
1412                                                 printf(" ");
1413                                                 unicode_fprint(stdout,
1414                                                                ((classinfo *) instr[i].val.a)->name);
1415                                                 }
1416                                         break;
1417                                 case ICMD_CHECKCAST:
1418                                 case ICMD_INSTANCEOF:
1419                                         if (instr[i].op1) {
1420                                                 printf(" ");
1421                                                 unicode_fprint(stdout,
1422                                                                ((classinfo *) instr[i].val.a)->name);
1423                                                 }
1424                                         break;
1425                                 case ICMD_BUILTIN3:
1426                                 case ICMD_BUILTIN2:
1427                                 case ICMD_BUILTIN1:
1428                                         printf(" %s", builtin_name((functionptr) instr[i].val.a));
1429                                         break;
1430                                 case ICMD_INVOKEVIRTUAL:
1431                                 case ICMD_INVOKESPECIAL:
1432                                 case ICMD_INVOKESTATIC:
1433                                 case ICMD_INVOKEINTERFACE:
1434                                         printf(" ");
1435                                         unicode_fprint(stdout,
1436                                                        ((methodinfo *) instr[i].val.a)->class->name);
1437                                         printf(".");
1438                                         unicode_fprint(stdout,
1439                                                        ((methodinfo *) instr[i].val.a)->name);
1440                                         break;
1441                                 case ICMD_IFEQ:
1442                                 case ICMD_IFNE:
1443                                 case ICMD_IFLT:
1444                                 case ICMD_IFGE:
1445                                 case ICMD_IFGT:
1446                                 case ICMD_IFLE:
1447                                 case ICMD_IF_LEQ:
1448                                 case ICMD_IF_LNE:
1449                                 case ICMD_IF_LLT:
1450                                 case ICMD_IF_LGE:
1451                                 case ICMD_IF_LGT:
1452                                 case ICMD_IF_LLE:
1453                                         printf("(%d) L%03d", instr[i].val.i, block_index[instr[i].op1]);
1454                                         break;
1455                                 case ICMD_JSR:
1456                                 case ICMD_GOTO:
1457                                 case ICMD_IFNULL:
1458                                 case ICMD_IFNONNULL:
1459                                 case ICMD_IF_ICMPEQ:
1460                                 case ICMD_IF_ICMPNE:
1461                                 case ICMD_IF_ICMPLT:
1462                                 case ICMD_IF_ICMPGE:
1463                                 case ICMD_IF_ICMPGT:
1464                                 case ICMD_IF_ICMPLE:
1465                                 case ICMD_IF_LCMPEQ:
1466                                 case ICMD_IF_LCMPNE:
1467                                 case ICMD_IF_LCMPLT:
1468                                 case ICMD_IF_LCMPGE:
1469                                 case ICMD_IF_LCMPGT:
1470                                 case ICMD_IF_LCMPLE:
1471                                 case ICMD_IF_ACMPEQ:
1472                                 case ICMD_IF_ACMPNE:
1473                                         printf(" L%03d", block_index[instr[i].op1]);
1474                                         break;
1475                                 case ICMD_TABLESWITCH:
1476                                         s4ptr = instr[i].val.a;
1477                                         printf(" L%03d;", block_index[*s4ptr++]); /* default */
1478                                         j = *s4ptr++;                               /* low     */
1479                                         j = *s4ptr++ - j;                           /* high    */
1480                                         while (j >= 0) {
1481                                                 printf(" L%03d", block_index[*s4ptr++]);
1482                                                 j--;
1483                                                 }
1484                                         break;
1485                                 case ICMD_LOOKUPSWITCH:
1486                                         s4ptr = instr[i].val.a;
1487                                         printf(" L%d", block_index[*s4ptr++]);   /* default */
1488                                         j = *s4ptr++;                               /* count   */
1489                                         while (--j >= 0) {
1490                                                 printf(" L%03d", block_index[s4ptr[1]]);
1491                                                 s4ptr += 2;
1492                                                 }
1493                                         break;
1494                                 }
1495                         printf("\n");
1496                         }
1497         }
1498 }