- moved <clinit> call back into internal function
[cacao.git] / jit / typecheck.c
1 /* jit/typecheck.c - typechecking (part of bytecode verification)
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Edwin Steiner
28
29    $Id: typecheck.c 1348 2004-07-22 09:57:51Z twisti $
30
31 */
32
33 #include "global.h" /* must be here because of CACAO_TYPECHECK */
34
35 #ifdef CACAO_TYPECHECK
36
37 #include <string.h>
38 #include "main.h"
39 #include "builtin.h"
40 #include "tables.h"
41 #include "loader.h"
42 #include "native.h"
43 #include "types.h"
44 #include "jit/jit.h"
45 #include "jit/stack.h"
46 #include "toolbox/logging.h"
47 #include "toolbox/memory.h"
48
49 /****************************************************************************/
50 /* DEBUG HELPERS                                                            */
51 /****************************************************************************/
52
53 #ifdef TYPECHECK_VERBOSE_OPT
54 bool typecheckverbose = false;
55 #define DOLOG(action)  do { if (typecheckverbose) {action;} } while(0)
56 #else
57 #define DOLOG(action)
58 #endif
59
60 #ifdef TYPECHECK_VERBOSE
61 #define TYPECHECK_VERBOSE_IMPORTANT
62 #define LOG(str)           DOLOG(log_text(str))
63 #define LOG1(str,a)        DOLOG(dolog(str,a))
64 #define LOG2(str,a,b)      DOLOG(dolog(str,a,b))
65 #define LOG3(str,a,b,c)    DOLOG(dolog(str,a,b,c))
66 #define LOGIF(cond,str)    DOLOG(do {if (cond) log_text(str);} while(0))
67 #define LOGINFO(info)      DOLOG(do {typeinfo_print_short(get_logfile(),(info));log_plain("\n");} while(0))
68 #define LOGFLUSH           DOLOG(fflush(get_logfile()))
69 #define LOGNL              DOLOG(log_plain("\n"))
70 #define LOGSTR(str)        DOLOG(log_plain(str))
71 #define LOGSTR1(str,a)     DOLOG(dolog_plain(str,a))
72 #define LOGSTR2(str,a,b)   DOLOG(dolog_plain(str,a,b))
73 #define LOGSTR3(str,a,b,c) DOLOG(dolog_plain(str,a,b,c))
74 #define LOGSTRu(utf)       DOLOG(log_plain_utf(utf))
75 #else
76 #define LOG(str)
77 #define LOG1(str,a)
78 #define LOG2(str,a,b)
79 #define LOG3(str,a,b,c)
80 #define LOGIF(cond,str)
81 #define LOGINFO(info)
82 #define LOGFLUSH
83 #define LOGNL
84 #define LOGSTR(str)
85 #define LOGSTR1(str,a)
86 #define LOGSTR2(str,a,b)
87 #define LOGSTR3(str,a,b,c)
88 #define LOGSTRu(utf)
89 #endif
90
91 #ifdef TYPECHECK_VERBOSE_IMPORTANT
92 #define LOGimp(str)     DOLOG(log_text(str))
93 #define LOGimpSTR(str)  DOLOG(log_plain(str))
94 #define LOGimpSTRu(utf) DOLOG(log_plain_utf(utf))
95 #else
96 #define LOGimp(str)
97 #define LOGimpSTR(str)
98 #define LOGimpSTRu(utf)
99 #endif
100
101 #if defined(TYPECHECK_VERBOSE) || defined(TYPECHECK_VERBOSE_IMPORTANT)
102
103 #include <stdio.h>
104
105 static
106 void
107 typestack_print(FILE *file,stackptr stack)
108 {
109     while (stack) {
110         typeinfo_print_stacktype(file,stack->type,&stack->typeinfo);
111         stack = stack->prev;
112         if (stack) fprintf(file," ");
113     }
114 }
115
116 static
117 void
118 typestate_print(FILE *file,stackptr instack,typevector *localset,int size)
119 {
120     fprintf(file,"Stack: ");
121     typestack_print(file,instack);
122     fprintf(file," Locals:");
123     typevectorset_print(file,localset,size);
124 }
125
126 #endif
127
128 /****************************************************************************/
129 /* STATISTICS                                                               */
130 /****************************************************************************/
131
132 #ifdef TYPECHECK_DEBUG
133 /*#define TYPECHECK_STATISTICS*/
134 #endif
135
136 #ifdef TYPECHECK_STATISTICS
137 #define STAT_ITERATIONS  10
138 #define STAT_BLOCKS      10
139 #define STAT_LOCALS      16
140
141 static int stat_typechecked = 0;
142 static int stat_typechecked_jsr = 0;
143 static int stat_iterations[STAT_ITERATIONS+1] = { 0 };
144 static int stat_reached = 0;
145 static int stat_copied = 0;
146 static int stat_merged = 0;
147 static int stat_merging_changed = 0;
148 static int stat_backwards = 0;
149 static int stat_blocks[STAT_BLOCKS+1] = { 0 };
150 static int stat_locals[STAT_LOCALS+1] = { 0 };
151 static int stat_ins = 0;
152 static int stat_ins_field = 0;
153 static int stat_ins_invoke = 0;
154 static int stat_ins_primload = 0;
155 static int stat_ins_aload = 0;
156 static int stat_ins_builtin = 0;
157 static int stat_ins_builtin_gen = 0;
158 static int stat_ins_branch = 0;
159 static int stat_ins_switch = 0;
160 static int stat_ins_unchecked = 0;
161 static int stat_handlers_reached = 0;
162 static int stat_savedstack = 0;
163
164 #define TYPECHECK_COUNT(cnt)  (cnt)++
165 #define TYPECHECK_COUNTIF(cond,cnt)  do{if(cond) (cnt)++;} while(0)
166 #define TYPECHECK_COUNT_FREQ(array,val,limit) \
167         do {                                                                      \
168                 if ((val) < (limit)) (array)[val]++;  \
169                 else (array)[limit]++;                            \
170         } while (0)
171
172 static void print_freq(FILE *file,int *array,int limit)
173 {
174         int i;
175         for (i=0; i<limit; ++i)
176                 fprintf(file,"      %3d: %8d\n",i,array[i]);
177         fprintf(file,"    =>%3d: %8d\n",limit,array[limit]);
178 }
179
180 void typecheck_print_statistics(FILE *file) {
181         fprintf(file,"typechecked methods: %8d\n",stat_typechecked);
182         fprintf(file,"methods with JSR   : %8d\n",stat_typechecked_jsr);
183         fprintf(file,"reached blocks     : %8d\n",stat_reached);
184         fprintf(file,"copied states      : %8d\n",stat_copied);
185         fprintf(file,"merged states      : %8d\n",stat_merged);
186         fprintf(file,"merging changed    : %8d\n",stat_merging_changed);
187         fprintf(file,"backwards branches : %8d\n",stat_backwards);
188         fprintf(file,"handlers reached   : %8d\n",stat_handlers_reached);
189         fprintf(file,"saved stack (times): %8d\n",stat_savedstack);
190         fprintf(file,"instructions       : %8d\n",stat_ins);
191         fprintf(file,"    field access   : %8d\n",stat_ins_field);
192         fprintf(file,"    invocations    : %8d\n",stat_ins_invoke);
193         fprintf(file,"    load primitive : %8d\n",stat_ins_primload);
194         fprintf(file,"    load address   : %8d\n",stat_ins_aload);
195         fprintf(file,"    builtins       : %8d\n",stat_ins_builtin);
196         fprintf(file,"        generic    : %8d\n",stat_ins_builtin_gen);
197         fprintf(file,"    unchecked      : %8d\n",stat_ins_unchecked);
198         fprintf(file,"    branches       : %8d\n",stat_ins_branch);
199         fprintf(file,"    switches       : %8d\n",stat_ins_switch);
200         fprintf(file,"iterations used:\n");
201         print_freq(file,stat_iterations,STAT_ITERATIONS);
202         fprintf(file,"basic blocks per method / 10:\n");
203         print_freq(file,stat_blocks,STAT_BLOCKS);
204         fprintf(file,"locals:\n");
205         print_freq(file,stat_locals,STAT_LOCALS);
206 }
207                                                    
208 #else
209                                                    
210 #define TYPECHECK_COUNT(cnt)
211 #define TYPECHECK_COUNTIF(cond,cnt)
212 #define TYPECHECK_COUNT_FREQ(array,val,limit)
213 #endif
214
215 /****************************************************************************/
216 /* TYPESTACK FUNCTIONS                                                      */
217 /****************************************************************************/
218
219 #define TYPESTACK_IS_RETURNADDRESS(sptr) \
220             TYPE_IS_RETURNADDRESS((sptr)->type,(sptr)->typeinfo)
221
222 #define TYPESTACK_IS_REFERENCE(sptr) \
223             TYPE_IS_REFERENCE((sptr)->type,(sptr)->typeinfo)
224
225 #define TYPESTACK_RETURNADDRESSSET(sptr) \
226             ((typeinfo_retaddr_set*)TYPEINFO_RETURNADDRESS((sptr)->typeinfo))
227
228 #define RETURNADDRESSSET_SEEK(set,pos) \
229             do {int i; for (i=pos;i--;) set=set->alt;} while(0)
230
231 #define TYPESTACK_COPY(sp,copy)                                                                 \
232                 do {for(; sp; sp=sp->prev, copy=copy->prev) {           \
233                                         copy->type = sp->type;                                          \
234                                         TYPEINFO_COPY(sp->typeinfo,copy->typeinfo);     \
235                                 }} while (0)                                                                    \
236
237 static void
238 typestack_copy(stackptr dst,stackptr y,typevector *selected)
239 {
240         typevector *sel;
241         typeinfo_retaddr_set *sety;
242         typeinfo_retaddr_set *new;
243         typeinfo_retaddr_set **next;
244         int k;
245         
246         for (;dst; dst=dst->prev, y=y->prev) {
247                 if (!y) panic("Stack depth mismatch 1");
248                 if (dst->type != y->type)
249                         panic("Stack type mismatch 1");
250                 LOG3("copy %p -> %p (type %d)",y,dst,dst->type);
251                 if (dst->type == TYPE_ADDRESS) {
252                         if (TYPEINFO_IS_PRIMITIVE(y->typeinfo)) {
253                                 /* We copy the returnAddresses from the selected
254                                  * states only. */
255
256                                 LOG("copying returnAddress");
257                                 sety = TYPESTACK_RETURNADDRESSSET(y);
258                                 next = &new;
259                                 for (k=0,sel=selected; sel; sel=sel->alt) {
260                                         LOG1("selected k=%d",sel->k);
261                                         while (k<sel->k) {
262                                                 sety = sety->alt;
263                                                 k++;
264                                         }
265                                         *next = DNEW(typeinfo_retaddr_set);
266                                         (*next)->addr = sety->addr;
267                                         next = &((*next)->alt);
268                                 }
269                                 *next = NULL;
270                                 TYPEINFO_INIT_RETURNADDRESS(dst->typeinfo,new);
271                         }
272                         else {
273                                 TYPEINFO_CLONE(y->typeinfo,dst->typeinfo);
274                         }
275                 }
276         }
277         if (y) panic("Stack depth mismatch 2");
278 }
279
280 static void
281 typestack_put_retaddr(stackptr dst,void *retaddr,typevector *loc)
282 {
283 #ifdef TYPECHECK_DEBUG
284         if (dst->type != TYPE_ADDRESS)
285                 panic("Internal error: Storing returnAddress in non-address slot");
286 #endif
287         
288         TYPEINFO_INIT_RETURNADDRESS(dst->typeinfo,NULL);
289         for (;loc; loc=loc->alt) {
290                 typeinfo_retaddr_set *set = DNEW(typeinfo_retaddr_set);
291                 set->addr = retaddr;
292                 set->alt = TYPESTACK_RETURNADDRESSSET(dst);
293                 TYPEINFO_INIT_RETURNADDRESS(dst->typeinfo,set);
294         }
295 }
296
297 static void
298 typestack_collapse(stackptr dst)
299 {
300         for (; dst; dst = dst->prev) {
301                 if (TYPESTACK_IS_RETURNADDRESS(dst))
302                         TYPESTACK_RETURNADDRESSSET(dst)->alt = NULL;
303         }
304 }
305
306 static bool
307 typestack_merge(stackptr dst,stackptr y)
308 {
309         bool changed = false;
310         for (; dst; dst = dst->prev, y=y->prev) {
311                 if (!y)
312                         panic("Stack depth mismatch 3");
313                 if (dst->type != y->type) panic("Stack type mismatch 2");
314                 if (dst->type == TYPE_ADDRESS) {
315                         if (TYPEINFO_IS_PRIMITIVE(dst->typeinfo)) {
316                                 /* dst has returnAddress type */
317                                 if (!TYPEINFO_IS_PRIMITIVE(y->typeinfo))
318                                         panic("Merging returnAddress with reference");
319                         }
320                         else {
321                                 /* dst has reference type */
322                                 if (TYPEINFO_IS_PRIMITIVE(y->typeinfo))
323                                         panic("Merging reference with returnAddress");
324                                 changed |= typeinfo_merge(&(dst->typeinfo),&(y->typeinfo));
325                         }
326                 }
327         }
328         if (y) panic("Stack depth mismatch 4");
329         return changed;
330 }
331
332 static void
333 typestack_add(stackptr dst,stackptr y,int ky)
334 {
335         typeinfo_retaddr_set *setd;
336         typeinfo_retaddr_set *sety;
337         
338         for (; dst; dst = dst->prev, y=y->prev) {
339                 if (TYPESTACK_IS_RETURNADDRESS(dst)) {
340                         setd = TYPESTACK_RETURNADDRESSSET(dst);
341                         sety = TYPESTACK_RETURNADDRESSSET(y);
342                         RETURNADDRESSSET_SEEK(sety,ky);
343                         while (setd->alt)
344                                 setd=setd->alt;
345                         setd->alt = DNEW(typeinfo_retaddr_set);
346                         setd->alt->addr = sety->addr;
347                         setd->alt->alt = NULL;
348                 }
349         }
350 }
351
352 /* 'a' and 'b' are assumed to have passed typestack_canmerge! */
353 static bool
354 typestack_separable_with(stackptr a,stackptr b,int kb)
355 {
356         typeinfo_retaddr_set *seta;
357         typeinfo_retaddr_set *setb;
358         
359         for (; a; a = a->prev, b = b->prev) {
360 #ifdef TYPECHECK_DEBUG
361                 if (!b) panic("Internal error: typestack_separable_from: different depth");
362 #endif
363                 if (TYPESTACK_IS_RETURNADDRESS(a)) {
364 #ifdef TYPECHECK_DEBUG
365                         if (!TYPESTACK_IS_RETURNADDRESS(b))
366                                 panic("Internal error: typestack_separable_from: unmergable stacks");
367 #endif
368                         seta = TYPESTACK_RETURNADDRESSSET(a);
369                         setb = TYPESTACK_RETURNADDRESSSET(b);
370                         RETURNADDRESSSET_SEEK(setb,kb);
371
372                         for (;seta;seta=seta->alt)
373                                 if (seta->addr != setb->addr) return true;
374                 }
375         }
376 #ifdef TYPECHECK_DEBUG
377         if (b) panic("Internal error: typestack_separable_from: different depth");
378 #endif
379         return false;
380 }
381
382 /* 'a' and 'b' are assumed to have passed typestack_canmerge! */
383 static bool
384 typestack_separable_from(stackptr a,int ka,stackptr b,int kb)
385 {
386         typeinfo_retaddr_set *seta;
387         typeinfo_retaddr_set *setb;
388
389         for (; a; a = a->prev, b = b->prev) {
390 #ifdef TYPECHECK_DEBUG
391                 if (!b) panic("Internal error: typestack_separable_from: different depth");
392 #endif
393                 if (TYPESTACK_IS_RETURNADDRESS(a)) {
394 #ifdef TYPECHECK_DEBUG
395                         if (!TYPESTACK_IS_RETURNADDRESS(b))
396                                 panic("Internal error: typestack_separable_from: unmergable stacks");
397 #endif
398                         seta = TYPESTACK_RETURNADDRESSSET(a);
399                         setb = TYPESTACK_RETURNADDRESSSET(b);
400                         RETURNADDRESSSET_SEEK(seta,ka);
401                         RETURNADDRESSSET_SEEK(setb,kb);
402
403                         if (seta->addr != setb->addr) return true;
404                 }
405         }
406 #ifdef TYPECHECK_DEBUG
407         if (b) panic("Internal error: typestack_separable_from: different depth");
408 #endif
409         return false;
410 }
411
412 /****************************************************************************/
413 /* TYPESTATE FUNCTIONS                                                      */
414 /****************************************************************************/
415
416 static bool
417 typestate_merge(stackptr deststack,typevector *destloc,
418                                 stackptr ystack,typevector *yloc,
419                                 int locsize,bool jsrencountered)
420 {
421         typevector *dvec,*yvec;
422         int kd,ky;
423         bool changed = false;
424         
425         LOG("merge:");
426         LOGSTR("dstack: "); DOLOG(typestack_print(get_logfile(),deststack)); LOGNL;
427         LOGSTR("ystack: "); DOLOG(typestack_print(get_logfile(),ystack)); LOGNL;
428         LOGSTR("dloc  : "); DOLOG(typevectorset_print(get_logfile(),destloc,locsize)); LOGNL;
429         LOGSTR("yloc  : "); DOLOG(typevectorset_print(get_logfile(),yloc,locsize)); LOGNL;
430         LOGFLUSH;
431
432         /* The stack is always merged. If there are returnAddresses on
433          * the stack they are ignored in this step. */
434
435         changed |= typestack_merge(deststack,ystack);
436
437         if (!jsrencountered)
438                 return typevector_merge(destloc,yloc,locsize);
439
440         for (yvec=yloc; yvec; yvec=yvec->alt) {
441                 ky = yvec->k;
442
443                 /* Check if the typestates (deststack,destloc) will be
444                  * separable when (ystack,yvec) is added. */
445
446                 if (!typestack_separable_with(deststack,ystack,ky)
447                         && !typevectorset_separable_with(destloc,yvec,locsize))
448                 {
449                         /* No, the resulting set won't be separable, thus we
450                          * may merge all states in (deststack,destloc) and
451                          * (ystack,yvec). */
452
453                         typestack_collapse(deststack);
454                         typevectorset_collapse(destloc,locsize);
455                         typevector_merge(destloc,yvec,locsize);
456                 }
457                 else {
458                         /* Yes, the resulting set will be separable. Thus we check
459                          * if we may merge (ystack,yvec) with a single state in
460                          * (deststack,destloc). */
461                 
462                         for (dvec=destloc,kd=0; dvec; dvec=dvec->alt, kd++) {
463                                 if (!typestack_separable_from(ystack,ky,deststack,kd)
464                                         && !typevector_separable_from(yvec,dvec,locsize))
465                                 {
466                                         /* The typestate (ystack,yvec) is not separable from
467                                          * (deststack,dvec) by any returnAddress. Thus we may
468                                          * merge the states. */
469                                         
470                                         changed |= typevector_merge(dvec,yvec,locsize);
471                                         
472                                         goto merged;
473                                 }
474                         }
475
476                         /* The typestate (ystack,yvec) is separable from all typestates
477                          * (deststack,destloc). Thus we must add this state to the
478                          * result set. */
479
480                         typestack_add(deststack,ystack,ky);
481                         typevectorset_add(destloc,yvec,locsize);
482                         changed = true;
483                 }
484                    
485         merged:
486                 ;
487         }
488         
489         LOG("result:");
490         LOGSTR("dstack: "); DOLOG(typestack_print(get_logfile(),deststack)); LOGNL;
491         LOGSTR("dloc  : "); DOLOG(typevectorset_print(get_logfile(),destloc,locsize)); LOGNL;
492         LOGFLUSH;
493         
494         return changed;
495 }
496
497
498 static bool
499 typestate_reach(methodinfo *m, void *localbuf,
500                                 basicblock *current,
501                                 basicblock *destblock,
502                                 stackptr ystack,typevector *yloc,
503                                 int locsize,bool jsrencountered)
504 {
505         typevector *destloc;
506         int destidx;
507         bool changed = false;
508
509         LOG1("reaching block L%03d",destblock->debug_nr);
510         TYPECHECK_COUNT(stat_reached);
511         
512         destidx = destblock - m->basicblocks;
513         destloc = MGET_TYPEVECTOR(localbuf,destidx,locsize);
514
515         /* When branching backwards we have to check for uninitialized objects */
516         
517         if (destblock <= current) {
518                 stackptr sp;
519                 int i;
520                 
521                 TYPECHECK_COUNT(stat_backwards);
522         LOG("BACKWARDS!");
523         for (sp = ystack; sp; sp=sp->prev)
524             if (sp->type == TYPE_ADR &&
525                 TYPEINFO_IS_NEWOBJECT(sp->typeinfo))
526                 panic("Branching backwards with uninitialized object on stack");
527
528         for (i=0; i<locsize; ++i)
529             if (yloc->td[i].type == TYPE_ADR &&
530                 TYPEINFO_IS_NEWOBJECT(yloc->td[i].info))
531                 panic("Branching backwards with uninitialized object in local variable");
532     }
533         
534         if (destblock->flags == BBTYPECHECK_UNDEF) {
535                 /* The destblock has never been reached before */
536
537                 TYPECHECK_COUNT(stat_copied);
538                 LOG1("block (index %04d) reached first time",destidx);
539                 
540                 typestack_copy(destblock->instack,ystack,yloc);
541                 COPY_TYPEVECTORSET(yloc,destloc,locsize);
542                 changed = true;
543         }
544         else {
545                 /* The destblock has already been reached before */
546                 
547                 TYPECHECK_COUNT(stat_merged);
548                 LOG1("block (index %04d) reached before",destidx);
549                 
550                 changed = typestate_merge(destblock->instack,destloc,
551                                                                   ystack,yloc,locsize,
552                                                                   jsrencountered);
553                 TYPECHECK_COUNTIF(changed,stat_merging_changed);
554         }
555
556         if (changed) {
557                 LOG("changed!");
558                 destblock->flags = BBTYPECHECK_REACHED;
559                 if (destblock <= current) {LOG("REPEAT!"); return true;}
560         }
561         return false;
562 }
563
564
565 static bool
566 typestate_ret(methodinfo *m, void *localbuf,
567                           basicblock *current,
568                           stackptr ystack,typevector *yloc,
569                           int retindex,int locsize)
570 {
571         typevector *yvec;
572         typevector *selected;
573         basicblock *destblock;
574         bool repeat = false;
575
576         for (yvec=yloc; yvec; ) {
577                 if (!TYPEDESC_IS_RETURNADDRESS(yvec->td[retindex]))
578                         panic("Illegal instruction: RET on non-returnAddress");
579
580                 destblock = (basicblock*) TYPEINFO_RETURNADDRESS(yvec->td[retindex].info);
581
582                 selected = typevectorset_select(&yvec,retindex,destblock);
583                 
584                 repeat |= typestate_reach(m, localbuf,current,destblock,
585                                                                   ystack,selected,locsize,true);
586         }
587         return repeat;
588 }
589
590 /****************************************************************************/
591 /* HELPER FUNCTIONS                                                         */
592 /****************************************************************************/
593
594 /* If a field is checked, definingclass == implementingclass */
595 static bool
596 is_accessible(int flags,classinfo *definingclass,classinfo *implementingclass, classinfo *methodclass,
597                           typeinfo *instance)
598 {
599         /* check access rights */
600         if (methodclass != definingclass) {
601                 switch (flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED)) {
602                   case ACC_PUBLIC:
603                           break;
604                           
605                           /* In the cases below, definingclass cannot be an interface */
606                           
607                   case 0:
608                           if (definingclass->packagename != methodclass->packagename)
609                                   return false;
610                           break;
611                   case ACC_PROTECTED:
612                           if (definingclass->packagename != methodclass->packagename) {
613                                   if (!builtin_isanysubclass(methodclass,implementingclass))
614                                           return false;
615                                   
616                                   /* For protected access of super class members in another
617                                    * package the instance must be a subclass of or the same
618                                    * as the current class. */
619                                   LOG("protected access into other package");
620                                   implementingclass = methodclass;
621                           }
622                           break;
623                   case ACC_PRIVATE:
624                           if (definingclass != methodclass) {
625                                   LOG("private access");
626                                   return false;
627                           }
628                           break;
629                   default:
630                           panic("Invalid access flags");
631                 }
632         }
633
634         if (instance) {
635                 if ((flags & ACC_STATIC) != 0) {
636                         LOG("accessing STATIC member with instance");
637                         return false;
638                 }
639                 
640                 if (implementingclass
641                         && !TYPEINFO_IS_NULLTYPE(*instance)
642                         && !TYPEINFO_IS_NEWOBJECT(*instance))
643                 {
644                         if (!typeinfo_is_assignable_to_classinfo(instance,
645                                                                                                          implementingclass))
646                         {
647                                 LOG("instance not assignable");
648                                 LOGINFO(instance);
649                                 LOGSTRu(implementingclass->name); LOGNL; LOGFLUSH;
650                                 return false;
651                         }
652                 }
653         }
654         else {
655                 if ((flags & ACC_STATIC) == 0) {
656                         LOG("accessing non-STATIC member without instance");
657                         return false;
658                 }
659         }
660         
661         return true;
662 }
663
664 /****************************************************************************/
665 /* MACROS FOR LOCAL VARIABLE CHECKING                                       */
666 /****************************************************************************/
667
668 #define INDEX_ONEWORD(num)                                                                              \
669         do { if((num)<0 || (num)>=validlocals)                                          \
670                         panic("Invalid local variable index"); } while (0)
671 #define INDEX_TWOWORD(num)                                                                              \
672         do { if((num)<0 || ((num)+1)>=validlocals)                                      \
673                         panic("Invalid local variable index"); } while (0)
674
675 #define STORE_ONEWORD(num,type)                                                                 \
676         do {typevectorset_store(localset,num,type,NULL);} while(0)
677
678 #define STORE_TWOWORD(num,type)                                                                         \
679         do {typevectorset_store_twoword(localset,num,type);} while(0)
680
681 #define CHECK_ONEWORD(num,tp)                                                                                   \
682         do {TYPECHECK_COUNT(stat_ins_primload);                                                         \
683                 if (jsrencountered) {                                                                                   \
684                         if (!typevectorset_checktype(localset,num,tp))                          \
685                                 panic("Variable type mismatch");                                                \
686                 }                                                                                                                               \
687                 else {                                                                                                                  \
688                         if (localset->td[num].type != tp)                                                       \
689                                 panic("Variable type mismatch");                                                \
690                 }                                                                                                                               \
691                 } while(0)
692
693 #define CHECK_TWOWORD(num,type)                                                                                 \
694         do {TYPECHECK_COUNT(stat_ins_primload);                                                         \
695                 if (!typevectorset_checktype(localset,num,type))                \
696             panic("Variable type mismatch");                            \
697                 } while(0)
698
699 /****************************************************************************/
700 /* MACROS FOR STACK TYPE CHECKING                                           */
701 /****************************************************************************/
702
703 /* These macros are for basic typechecks which were not done in stack.c */
704
705 #define TYPECHECK_STACK(sp,tp)                                                          \
706         do { if ((sp)->type != (tp))                                                    \
707                         panic("Wrong data type on stack"); } while(0)
708
709 #define TYPECHECK_ADR(sp)  TYPECHECK_STACK(sp,TYPE_ADR)
710 #define TYPECHECK_INT(sp)  TYPECHECK_STACK(sp,TYPE_INT)
711 #define TYPECHECK_LNG(sp)  TYPECHECK_STACK(sp,TYPE_LNG)
712 #define TYPECHECK_FLT(sp)  TYPECHECK_STACK(sp,TYPE_FLT)
713 #define TYPECHECK_DBL(sp)  TYPECHECK_STACK(sp,TYPE_DBL)
714
715 #define TYPECHECK_ARGS1(t1)                                                                 \
716         do {TYPECHECK_STACK(curstack,t1);} while (0)
717 #define TYPECHECK_ARGS2(t1,t2)                                                      \
718         do {TYPECHECK_ARGS1(t1);                                                                        \
719                 TYPECHECK_STACK(curstack->prev,t2);} while (0)
720 #define TYPECHECK_ARGS3(t1,t2,t3)                                                               \
721         do {TYPECHECK_ARGS2(t1,t2);                                                                     \
722                 TYPECHECK_STACK(curstack->prev->prev,t3);} while (0)
723
724 /****************************************************************************/
725 /* MISC MACROS                                                              */
726 /****************************************************************************/
727
728 #define COPYTYPE(source,dest)   \
729         {if ((source)->type == TYPE_ADR)                                                                \
730                         TYPEINFO_COPY((source)->typeinfo,(dest)->typeinfo);}
731
732 #define ISBUILTIN(v)   (iptr->val.a == (functionptr)(v))
733
734 /* TYPECHECK_REACH: executed, when the target block (tbptr) can be reached
735  *     from the current block (bptr). The types of local variables and
736  *     stack slots are propagated to the target block.
737  * Input:
738  *     bptr.......current block
739  *     tbptr......target block
740  *     dst........current output stack pointer
741  *     numlocals..number of local variables
742  *     localset...current local variable vectorset
743  *     localbuf...local variable vectorset buffer
744  *     jsrencountered...true if a JSR has been seen
745  * Output:
746  *     repeat.....changed to true if a block before the current
747  *                block has changed
748  */
749 #define TYPECHECK_REACH                                                 \
750     do {                                                                \
751     repeat |= typestate_reach(m, localbuf,bptr,tbptr,dst,               \
752                                                           localset,numlocals,jsrencountered);       \
753     LOG("done.");                                                       \
754     } while (0)
755
756 /* TYPECHECK_LEAVE: executed when the method is exited non-abruptly
757  * Input:
758  *     class........class of the current method
759  *     numlocals....number of local variables
760  *     localset.....current local variable vectorset
761  *     initmethod...true if this is an <init> method
762  */
763 #define TYPECHECK_LEAVE                                                 \
764     do {                                                                \
765         if (initmethod && m->class != class_java_lang_Object) {         \
766             /* check the marker variable */                             \
767             LOG("Checking <init> marker");                              \
768             if (!typevectorset_checktype(localset,numlocals-1,TYPE_INT))\
769                 panic("<init> method does not initialize 'this'");      \
770         }                                                               \
771     } while (0)
772
773 /****************************************************************************/
774 /* typecheck()                                                              */
775 /****************************************************************************/
776
777 #define MAXPARAMS 255
778
779 /* typecheck is called directly after analyse_stack */
780
781 methodinfo *typecheck(methodinfo *m)
782 {
783     int b_count, b_index;
784     stackptr curstack;      /* input stack top for current instruction */
785     stackptr srcstack;         /* source stack for copying and merging */
786     int opcode;                                      /* current opcode */
787     int i;                                        /* temporary counter */
788     int len;                        /* for counting instructions, etc. */
789     bool superblockend;        /* true if no fallthrough to next block */
790     bool repeat;            /* if true, blocks are iterated over again */
791     instruction *iptr;               /* pointer to current instruction */
792     basicblock *bptr;                /* pointer to current basic block */
793     basicblock *tbptr;                   /* temporary for target block */
794         
795     int numlocals;                        /* number of local variables */
796         int validlocals;         /* number of valid local variable indices */
797         void *localbuf;       /* local variable types for each block start */
798         typevector *localset;        /* typevector set for local variables */
799         typevector *lset;                             /* temporary pointer */
800         typedescriptor *td;                           /* temporary pointer */
801
802         stackptr savedstackbuf = NULL;      /* buffer for saving the stack */
803         stackptr savedstack = NULL;      /* saved instack of current block */
804
805         stackelement excstack;           /* instack for exception handlers */
806                                                                                                           
807         typedescriptor returntype;        /* return type of current method */
808     u1 *ptype;                     /* parameter types of called method */
809     typeinfo *pinfo;           /* parameter typeinfos of called method */
810     int rtype;                         /* return type of called method */
811     typeinfo rinfo;       /* typeinfo for return type of called method */
812         
813     stackptr dst;               /* output stack of current instruction */
814     basicblock **tptr;    /* pointer into target list of switch instr. */
815     exceptiontable **handlers;            /* active exception handlers */
816     classinfo *cls;                                       /* temporary */
817     bool maythrow;               /* true if this instruction may throw */
818     static utf *name_init;                                 /* "<init>" */
819     bool initmethod;             /* true if this is an "<init>" method */
820         builtin_descriptor *builtindesc;    /* temp. descriptor of builtin */
821         bool jsrencountered = false;         /* true if we there was a JSR */
822
823     classinfo *myclass;
824
825 #ifdef TYPECHECK_STATISTICS
826         int count_iterations = 0;
827         TYPECHECK_COUNT(stat_typechecked);
828         TYPECHECK_COUNT_FREQ(stat_locals,m->maxlocals,STAT_LOCALS);
829         TYPECHECK_COUNT_FREQ(stat_blocks,m->basicblockcount/10,STAT_BLOCKS);
830 #endif
831
832     LOGSTR("\n==============================================================================\n");
833     DOLOG(show_icmd_method());
834     LOGSTR("\n==============================================================================\n");
835     LOGimpSTR("Entering typecheck: ");
836     LOGimpSTRu(method->name);
837     LOGimpSTR("    ");
838     LOGimpSTRu(method->descriptor);
839     LOGimpSTR("    (class ");
840     LOGimpSTRu(method->class->name);
841     LOGimpSTR(")\n");
842         LOGFLUSH;
843
844         if (!name_init)
845                 name_init = utf_new_char("<init>");
846     initmethod = (m->name == name_init);
847
848         /* Allocate buffer for method arguments */
849         
850     ptype = DMNEW(u1,MAXPARAMS);
851     pinfo = DMNEW(typeinfo,MAXPARAMS);
852     
853     LOG("Buffer allocated.\n");
854
855     /* reset all BBFINISHED blocks to BBTYPECHECK_UNDEF. */
856     b_count = m->basicblockcount;
857     bptr = m->basicblocks;
858     while (--b_count >= 0) {
859 #ifdef TYPECHECK_DEBUG
860         if (bptr->flags != BBFINISHED && bptr->flags != BBDELETED
861             && bptr->flags != BBUNDEF)
862         {
863             show_icmd_method();
864             LOGSTR1("block flags: %d\n",bptr->flags); LOGFLUSH;
865             panic("Internal error: Unexpected block flags in typecheck()");
866         }
867 #endif
868         if (bptr->flags >= BBFINISHED) {
869             bptr->flags = BBTYPECHECK_UNDEF;
870         }
871         bptr++;
872     }
873
874     /* The first block is always reached */
875     if (m->basicblockcount && m->basicblocks[0].flags == BBTYPECHECK_UNDEF)
876         m->basicblocks[0].flags = BBTYPECHECK_REACHED;
877
878     LOG("Blocks reset.\n");
879
880     /* number of local variables */
881     
882     /* In <init> methods we use an extra local variable to signal if
883      * the 'this' reference has been initialized. */
884     numlocals = m->maxlocals;
885         validlocals = numlocals;
886     if (initmethod) numlocals++;
887
888     /* allocate the buffers for local variables */
889         localbuf = DMNEW_TYPEVECTOR(m->basicblockcount+1, numlocals);
890         localset = MGET_TYPEVECTOR(localbuf,m->basicblockcount,numlocals);
891
892     LOG("Variable buffer allocated.\n");
893
894     /* allocate the buffer of active exception handlers */
895     handlers = DMNEW(exceptiontable*, m->exceptiontablelength + 1);
896
897     /* initialize the variable types of the first block */
898     /* to the types of the arguments */
899         lset = MGET_TYPEVECTOR(localbuf,0,numlocals);
900         lset->k = 0;
901         lset->alt = NULL;
902         td = lset->td;
903         i = validlocals;
904
905     /* if this is an instance method initialize the "this" ref type */
906     if (!(m->flags & ACC_STATIC)) {
907                 if (!i)
908                         panic("Not enough local variables for method arguments");
909         td->type = TYPE_ADDRESS;
910         if (initmethod)
911             TYPEINFO_INIT_NEWOBJECT(td->info,NULL);
912         else
913             TYPEINFO_INIT_CLASSINFO(td->info, m->class);
914         td++;
915                 i--;
916     }
917
918     LOG("'this' argument set.\n");
919
920     /* the rest of the arguments and the return type */
921     i = typedescriptors_init_from_method_args(td, m->descriptor,
922                                                                                           i,
923                                                                                           true, /* two word types use two slots */
924                                                                                           &returntype);
925         td += i;
926         i = numlocals - (td - lset->td);
927         while (i--) {
928                 td->type = TYPE_VOID;
929                 td++;
930         }
931
932     LOG("Arguments set.\n");
933
934     /* initialize the input stack of exception handlers */
935         excstack.prev = NULL;
936         excstack.type = TYPE_ADR;
937         TYPEINFO_INIT_CLASSINFO(excstack.typeinfo,
938                                                         class_java_lang_Throwable); /* changed later */
939
940     LOG("Exception handler stacks set.\n");
941
942     /* loop while there are still blocks to be checked */
943     do {
944                 TYPECHECK_COUNT(count_iterations);
945
946         repeat = false;
947         
948         b_count = m->basicblockcount;
949         bptr = m->basicblocks;
950
951         while (--b_count >= 0) {
952             LOGSTR1("---- BLOCK %04d, ",bptr-block);
953             LOGSTR1("blockflags: %d\n",bptr->flags);
954             LOGFLUSH;
955                 
956             if (bptr->flags == BBTYPECHECK_REACHED) {
957                 LOGSTR1("\n---- BLOCK %04d ------------------------------------------------\n",bptr-block);
958                 LOGFLUSH;
959                 
960                 superblockend = false;
961                 bptr->flags = BBFINISHED;
962                 b_index = bptr - m->basicblocks;
963
964                 /* init stack at the start of this block */
965                 curstack = bptr->instack;
966                                         
967                 /* determine the active exception handlers for this block */
968                 /* XXX could use a faster algorithm with sorted lists or
969                  * something? */
970                 len = 0;
971                 for (i = 0; i < m->exceptiontablelength; ++i) {
972                     if ((m->exceptiontable[i].start <= bptr) && (m->exceptiontable[i].end > bptr)) {
973                         LOG1("active handler L%03d", m->exceptiontable[i].handler->debug_nr);
974                         handlers[len++] = m->exceptiontable + i;
975                     }
976                 }
977                 handlers[len] = NULL;
978                                         
979                 /* init variable types at the start of this block */
980                                 COPY_TYPEVECTORSET(MGET_TYPEVECTOR(localbuf,b_index,numlocals),
981                                                                    localset,numlocals);
982                                 if (handlers[0])
983                                         for (i=0; i<numlocals; ++i)
984                                                 if (localset->td[i].type == TYPE_ADR
985                                                         && TYPEINFO_IS_NEWOBJECT(localset->td[i].info))
986                                                         panic("Uninitialized object in local variable inside try block");
987
988                                 DOLOG(typestate_print(get_logfile(),curstack,localset,numlocals));
989                                 LOGNL; LOGFLUSH;
990
991                 /* loop over the instructions */
992                 len = bptr->icount;
993                 iptr = bptr->iinstr;
994                 while (--len >= 0)  {
995                                         TYPECHECK_COUNT(stat_ins);
996                     DOLOG(show_icmd(iptr,false)); LOGNL; LOGFLUSH;
997                         
998                     opcode = iptr->opc;
999                     myclass = iptr->method->class;
1000                     dst = iptr->dst;
1001                     maythrow = false;
1002                                                 
1003                     switch (opcode) {
1004
1005                         /****************************************/
1006                         /* STACK MANIPULATIONS                  */
1007
1008                         /* We just need to copy the typeinfo */
1009                         /* for slots containing addresses.   */
1010
1011                         /* XXX We assume that the destination stack
1012                          * slots were continuously allocated in
1013                          * memory.  (The current implementation in
1014                          * stack.c)
1015                          */
1016
1017                       case ICMD_DUP:
1018                           COPYTYPE(curstack,dst);
1019                           break;
1020                                                           
1021                       case ICMD_DUP_X1:
1022                           COPYTYPE(curstack,dst);
1023                           COPYTYPE(curstack,dst-2);
1024                           COPYTYPE(curstack->prev,dst-1);
1025                           break;
1026                                                           
1027                       case ICMD_DUP_X2:
1028                           COPYTYPE(curstack,dst);
1029                           COPYTYPE(curstack,dst-3);
1030                           COPYTYPE(curstack->prev,dst-1);
1031                           COPYTYPE(curstack->prev->prev,dst-2);
1032                           break;
1033                                                           
1034                       case ICMD_DUP2:
1035                           COPYTYPE(curstack,dst);
1036                           COPYTYPE(curstack->prev,dst-1);
1037                           break;
1038
1039                       case ICMD_DUP2_X1:
1040                           COPYTYPE(curstack,dst);
1041                           COPYTYPE(curstack->prev,dst-1);
1042                           COPYTYPE(curstack,dst-3);
1043                           COPYTYPE(curstack->prev,dst-4);
1044                           COPYTYPE(curstack->prev->prev,dst-2);
1045                           break;
1046                                                           
1047                       case ICMD_DUP2_X2:
1048                           COPYTYPE(curstack,dst);
1049                           COPYTYPE(curstack->prev,dst-1);
1050                           COPYTYPE(curstack,dst-4);
1051                           COPYTYPE(curstack->prev,dst-5);
1052                           COPYTYPE(curstack->prev->prev,dst-2);
1053                           COPYTYPE(curstack->prev->prev->prev,dst-3);
1054                           break;
1055                                                           
1056                       case ICMD_SWAP:
1057                           COPYTYPE(curstack,dst-1);
1058                           COPYTYPE(curstack->prev,dst);
1059                           break;
1060
1061                           /****************************************/
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071                           /* PRIMITIVE VARIABLE ACCESS            */
1072
1073                       case ICMD_ILOAD: CHECK_ONEWORD(iptr->op1,TYPE_INT); break;
1074                       case ICMD_FLOAD: CHECK_ONEWORD(iptr->op1,TYPE_FLOAT); break;
1075                       case ICMD_IINC:  CHECK_ONEWORD(iptr->op1,TYPE_INT); break;
1076                       case ICMD_LLOAD: CHECK_TWOWORD(iptr->op1,TYPE_LONG); break;
1077                       case ICMD_DLOAD: CHECK_TWOWORD(iptr->op1,TYPE_DOUBLE); break;
1078                           
1079                       case ICMD_FSTORE: STORE_ONEWORD(iptr->op1,TYPE_FLOAT); break;
1080                       case ICMD_ISTORE: STORE_ONEWORD(iptr->op1,TYPE_INT); break;
1081                       case ICMD_LSTORE: STORE_TWOWORD(iptr->op1,TYPE_LONG); break;
1082                       case ICMD_DSTORE: STORE_TWOWORD(iptr->op1,TYPE_DOUBLE); break;
1083                           
1084                           /****************************************/
1085                           /* LOADING ADDRESS FROM VARIABLE        */
1086
1087                       case ICMD_ALOAD:
1088                                                   TYPECHECK_COUNT(stat_ins_aload);
1089                           
1090                           /* loading a returnAddress is not allowed */
1091                                                   if (jsrencountered) {
1092                                                           if (!typevectorset_checkreference(localset,iptr->op1))
1093                                                                   panic("illegal instruction: ALOAD loading non-reference");
1094
1095                                                           typevectorset_copymergedtype(localset,iptr->op1,&(dst->typeinfo));
1096                                                   }
1097                                                   else {
1098                                                           if (!TYPEDESC_IS_REFERENCE(localset->td[iptr->op1]))
1099                                                                   panic("illegal instruction: ALOAD loading non-reference");
1100                                                           TYPEINFO_COPY(localset->td[iptr->op1].info,dst->typeinfo);
1101                                                   }
1102                           break;
1103                                                         
1104                           /****************************************/
1105                           /* STORING ADDRESS TO VARIABLE          */
1106
1107                       case ICMD_ASTORE:
1108                           if (handlers[0] &&
1109                               TYPEINFO_IS_NEWOBJECT(curstack->typeinfo))
1110                               panic("Storing uninitialized object in local variable inside try block");
1111
1112                                                   if (TYPESTACK_IS_RETURNADDRESS(curstack))
1113                                                           typevectorset_store_retaddr(localset,iptr->op1,&(curstack->typeinfo));
1114                                                   else
1115                                                           typevectorset_store(localset,iptr->op1,TYPE_ADDRESS,
1116                                                                                                   &(curstack->typeinfo));
1117                           break;
1118                           
1119                           /****************************************/
1120                           /* LOADING ADDRESS FROM ARRAY           */
1121
1122                       case ICMD_AALOAD:
1123                           if (!TYPEINFO_MAYBE_ARRAY_OF_REFS(curstack->prev->typeinfo))
1124                               panic("illegal instruction: AALOAD on non-reference array");
1125
1126                           typeinfo_init_component(&curstack->prev->typeinfo,&dst->typeinfo);
1127                           maythrow = true;
1128                           break;
1129                                                           
1130                           /****************************************/
1131                           /* FIELD ACCESS                         */
1132
1133                       case ICMD_PUTFIELD:
1134                                                   TYPECHECK_COUNT(stat_ins_field);
1135                           if (!TYPEINFO_IS_REFERENCE(curstack->prev->typeinfo))
1136                               panic("illegal instruction: PUTFIELD on non-reference");
1137                           if (TYPEINFO_IS_ARRAY(curstack->prev->typeinfo))
1138                               panic("illegal instruction: PUTFIELD on array");
1139
1140                           /* check if the value is assignable to the field */
1141                                                   {
1142                                                           fieldinfo *fi = (fieldinfo*) iptr[0].val.a;
1143
1144                                                           if (TYPEINFO_IS_NEWOBJECT(curstack->prev->typeinfo)) {
1145                                                                   if (initmethod
1146                                                                           && !TYPEINFO_NEWOBJECT_INSTRUCTION(curstack->prev->typeinfo))
1147                                                                   {
1148                                                                           /* uninitialized "this" instance */
1149                                                                           if (fi->class != m->class || (fi->flags & ACC_STATIC) != 0)
1150                                                                                   panic("Setting unaccessible field in uninitialized object");
1151                                                                   }
1152                                                                   else {
1153                                                                           panic("PUTFIELD on uninitialized object");
1154                                                                   }
1155                                                           }
1156                                                           else {
1157                                                                   if (!is_accessible(fi->flags,fi->class,fi->class, myclass,
1158                                                                                                          &(curstack->prev->typeinfo)))
1159                                                                           panic("PUTFIELD: field is not accessible");
1160                                                           }
1161
1162                                                           if (curstack->type != fi->type)
1163                                                                   panic("PUTFIELD type mismatch");
1164                                                           if (fi->type == TYPE_ADR) {
1165                                                                   TYPEINFO_INIT_FROM_FIELDINFO(rinfo,fi);
1166                                                                   if (!typeinfo_is_assignable(&(curstack->typeinfo),
1167                                                                                                                           &rinfo))
1168                                                                           panic("PUTFIELD reference type not assignable");
1169                                                           }
1170                                                   }
1171                           maythrow = true;
1172                           break;
1173
1174                       case ICMD_PUTSTATIC:
1175                                                   TYPECHECK_COUNT(stat_ins_field);
1176                           /* check if the value is assignable to the field */
1177                                                   {
1178                                                           fieldinfo *fi = (fieldinfo*) iptr[0].val.a;
1179
1180                                                           if (!is_accessible(fi->flags,fi->class,fi->class,myclass,NULL))
1181                                                                   panic("PUTSTATIC: field is not accessible");
1182
1183                                                           if (curstack->type != fi->type)
1184                                                                   panic("PUTSTATIC type mismatch");
1185                                                           if (fi->type == TYPE_ADR) {
1186                                                                   TYPEINFO_INIT_FROM_FIELDINFO(rinfo,fi);
1187                                                                   if (!typeinfo_is_assignable(&(curstack->typeinfo),
1188                                                                                                                           &rinfo))
1189                                                                           panic("PUTSTATIC reference type not assignable");
1190                                                           }
1191                                                   }
1192                           maythrow = true;
1193                           break;
1194
1195                       case ICMD_GETFIELD:
1196                                                   TYPECHECK_COUNT(stat_ins_field);
1197                           if (!TYPEINFO_IS_REFERENCE(curstack->typeinfo))
1198                               panic("illegal instruction: GETFIELD on non-reference");
1199                           if (TYPEINFO_IS_ARRAY(curstack->typeinfo))
1200                               panic("illegal instruction: GETFIELD on array");
1201                           
1202                           {
1203                               fieldinfo *fi = (fieldinfo *)(iptr->val.a);
1204
1205                                                           if (!is_accessible(fi->flags,fi->class,fi->class,myclass,
1206                                                                                                  &(curstack->typeinfo)))
1207                                                                   panic("GETFIELD: field is not accessible");
1208                                                           
1209                               if (dst->type == TYPE_ADR) {
1210                                   TYPEINFO_INIT_FROM_FIELDINFO(dst->typeinfo,fi);
1211                               }
1212                           }
1213                           maythrow = true;
1214                           break;
1215
1216                       case ICMD_GETSTATIC:
1217                                                   TYPECHECK_COUNT(stat_ins_field);
1218                           {
1219                               fieldinfo *fi = (fieldinfo *)(iptr->val.a);
1220                                                           
1221                                                           if (!is_accessible(fi->flags,fi->class,fi->class,myclass,NULL)) {
1222                                                                 printf("---------\n");
1223                                                                   utf_display(fi->class->name);
1224                                                                 printf("\n");
1225                                                                   utf_display(myclass->name);
1226                                                                 printf("\n");
1227
1228
1229                                                                   panic("GETSTATIC: field is not accessible");
1230                                                         }
1231
1232                               if (dst->type == TYPE_ADR) {
1233                                   TYPEINFO_INIT_FROM_FIELDINFO(dst->typeinfo,fi);
1234                               }
1235                           }
1236                           maythrow = true;
1237                           break;
1238
1239                           /****************************************/
1240                           /* PRIMITIVE ARRAY ACCESS               */
1241
1242                       case ICMD_ARRAYLENGTH:
1243                           if (!TYPEINFO_MAYBE_ARRAY(curstack->typeinfo)
1244                                                           && curstack->typeinfo.typeclass != pseudo_class_Arraystub)
1245                               panic("illegal instruction: ARRAYLENGTH on non-array");
1246                           maythrow = true;
1247                           break;
1248
1249                       case ICMD_BALOAD:
1250                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_BOOLEAN)
1251                               && !TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_BYTE))
1252                               panic("Array type mismatch");
1253                           maythrow = true;
1254                           break;
1255                       case ICMD_CALOAD:
1256                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_CHAR))
1257                               panic("Array type mismatch");
1258                           maythrow = true;
1259                           break;
1260                       case ICMD_DALOAD:
1261                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_DOUBLE))
1262                               panic("Array type mismatch");
1263                           maythrow = true;
1264                           break;
1265                       case ICMD_FALOAD:
1266                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_FLOAT))
1267                               panic("Array type mismatch");
1268                           maythrow = true;
1269                           break;
1270                       case ICMD_IALOAD:
1271                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_INT))
1272                               panic("Array type mismatch");
1273                           maythrow = true;
1274                           break;
1275                       case ICMD_SALOAD:
1276                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_SHORT))
1277                               panic("Array type mismatch");
1278                           maythrow = true;
1279                           break;
1280                       case ICMD_LALOAD:
1281                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo,ARRAYTYPE_LONG))
1282                               panic("Array type mismatch");
1283                           maythrow = true;
1284                           break;
1285
1286                       case ICMD_BASTORE:
1287                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_BOOLEAN)
1288                               && !TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_BYTE))
1289                               panic("Array type mismatch");
1290                           maythrow = true;
1291                           break;
1292                       case ICMD_CASTORE:
1293                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_CHAR))
1294                               panic("Array type mismatch");
1295                           maythrow = true;
1296                           break;
1297                       case ICMD_DASTORE:
1298                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_DOUBLE))
1299                               panic("Array type mismatch");
1300                           maythrow = true;
1301                           break;
1302                       case ICMD_FASTORE:
1303                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_FLOAT))
1304                               panic("Array type mismatch");
1305                           maythrow = true;
1306                           break;
1307                       case ICMD_IASTORE:
1308                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_INT))
1309                               panic("Array type mismatch");
1310                           maythrow = true;
1311                           break;
1312                       case ICMD_SASTORE:
1313                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_SHORT))
1314                               panic("Array type mismatch");
1315                           maythrow = true;
1316                           break;
1317                       case ICMD_LASTORE:
1318                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->prev->typeinfo,ARRAYTYPE_LONG))
1319                               panic("Array type mismatch");
1320                           maythrow = true;
1321                           break;
1322
1323                       case ICMD_IASTORECONST:
1324                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_INT))
1325                               panic("Array type mismatch");
1326                           maythrow = true;
1327                           break;
1328
1329                       case ICMD_LASTORECONST:
1330                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_LONG))
1331                               panic("Array type mismatch");
1332                           maythrow = true;
1333                           break;
1334
1335                       case ICMD_BASTORECONST:
1336                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_BOOLEAN)
1337                               && !TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_BYTE))
1338                               panic("Array type mismatch");
1339                           maythrow = true;
1340                           break;
1341
1342                       case ICMD_CASTORECONST:
1343                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_CHAR))
1344                               panic("Array type mismatch");
1345                           maythrow = true;
1346                           break;
1347
1348                       case ICMD_SASTORECONST:
1349                           if (!TYPEINFO_MAYBE_PRIMITIVE_ARRAY(curstack->prev->typeinfo, ARRAYTYPE_SHORT))
1350                               panic("Array type mismatch");
1351                           maythrow = true;
1352                           break;
1353
1354
1355                           /****************************************/
1356                           /* ADDRESS CONSTANTS                    */
1357
1358                       case ICMD_ACONST:
1359                           if (iptr->val.a == NULL)
1360                               TYPEINFO_INIT_NULLTYPE(dst->typeinfo);
1361                           else
1362                               /* string constants (or constant for builtin function) */
1363                               TYPEINFO_INIT_CLASSINFO(dst->typeinfo,class_java_lang_String);
1364                           break;
1365
1366                           /****************************************/
1367                           /* CHECKCAST AND INSTANCEOF             */
1368
1369                       case ICMD_CHECKCAST:
1370                                                   TYPECHECK_ADR(curstack);
1371                           /* returnAddress is not allowed */
1372                           if (!TYPEINFO_IS_REFERENCE(curstack->typeinfo))
1373                               panic("Illegal instruction: CHECKCAST on non-reference");
1374
1375                           TYPEINFO_INIT_CLASSINFO(dst->typeinfo,(classinfo *)iptr[0].val.a);
1376                           maythrow = true;
1377                           break;
1378
1379                       case ICMD_INSTANCEOF:
1380                                                   TYPECHECK_ADR(curstack);
1381                           /* returnAddress is not allowed */
1382                           if (!TYPEINFO_IS_REFERENCE(curstack->typeinfo))
1383                               panic("Illegal instruction: INSTANCEOF on non-reference");
1384                           break;
1385                           
1386                           /****************************************/
1387                           /* BRANCH INSTRUCTIONS                  */
1388
1389                       case ICMD_GOTO:
1390                           superblockend = true;
1391                           /* FALLTHROUGH! */
1392                       case ICMD_IFNULL:
1393                       case ICMD_IFNONNULL:
1394                       case ICMD_IFEQ:
1395                       case ICMD_IFNE:
1396                       case ICMD_IFLT:
1397                       case ICMD_IFGE:
1398                       case ICMD_IFGT:
1399                       case ICMD_IFLE:
1400                       case ICMD_IF_ICMPEQ:
1401                       case ICMD_IF_ICMPNE:
1402                       case ICMD_IF_ICMPLT:
1403                       case ICMD_IF_ICMPGE:
1404                       case ICMD_IF_ICMPGT:
1405                       case ICMD_IF_ICMPLE:
1406                       case ICMD_IF_ACMPEQ:
1407                       case ICMD_IF_ACMPNE:
1408                       case ICMD_IF_LEQ:
1409                       case ICMD_IF_LNE:
1410                       case ICMD_IF_LLT:
1411                       case ICMD_IF_LGE:
1412                       case ICMD_IF_LGT:
1413                       case ICMD_IF_LLE:
1414                       case ICMD_IF_LCMPEQ:
1415                       case ICMD_IF_LCMPNE:
1416                       case ICMD_IF_LCMPLT:
1417                       case ICMD_IF_LCMPGE:
1418                       case ICMD_IF_LCMPGT:
1419                       case ICMD_IF_LCMPLE:
1420                                                   TYPECHECK_COUNT(stat_ins_branch);
1421                           tbptr = (basicblock *) iptr->target;
1422
1423                           /* propagate stack and variables to the target block */
1424                           TYPECHECK_REACH;
1425                           break;
1426
1427                           /****************************************/
1428                           /* SWITCHES                             */
1429                           
1430                       case ICMD_TABLESWITCH:
1431                                                   TYPECHECK_COUNT(stat_ins_switch);
1432                           {
1433                               s4 *s4ptr = iptr->val.a;
1434                               s4ptr++;              /* skip default */
1435                               i = *s4ptr++;         /* low */
1436                               i = *s4ptr++ - i + 2; /* +1 for default target */
1437                           }
1438                           goto switch_instruction_tail;
1439                           
1440                       case ICMD_LOOKUPSWITCH:
1441                                                   TYPECHECK_COUNT(stat_ins_switch);
1442                           {
1443                               s4 *s4ptr = iptr->val.a;
1444                               s4ptr++;              /* skip default */
1445                               i = *s4ptr++ + 1;     /* count +1 for default */
1446                           }
1447                     switch_instruction_tail:
1448                           tptr = (basicblock **)iptr->target;
1449                           
1450                           while (--i >= 0) {
1451                               tbptr = *tptr++;
1452                               LOG2("target %d is block %04d",(tptr-(basicblock **)iptr->target)-1,tbptr-block);
1453                               TYPECHECK_REACH;
1454                           }
1455                           LOG("switch done");
1456                           superblockend = true;
1457                           break;
1458
1459                           /****************************************/
1460                           /* RETURNS AND THROW                    */
1461
1462                       case ICMD_ATHROW:
1463                           if (!typeinfo_is_assignable_to_classinfo(
1464                                    &curstack->typeinfo,class_java_lang_Throwable))
1465                               panic("illegal instruction: ATHROW on non-Throwable");
1466                           superblockend = true;
1467                           maythrow = true;
1468                           break;
1469
1470                       case ICMD_ARETURN:
1471                           if (!TYPEINFO_IS_REFERENCE(curstack->typeinfo))
1472                               panic("illegal instruction: ARETURN on non-reference");
1473
1474                           if (returntype.type != TYPE_ADDRESS
1475                               || !typeinfo_is_assignable(&curstack->typeinfo,&(returntype.info)))
1476                               panic("Return type mismatch");
1477                                                   goto return_tail;
1478                                                   
1479                       case ICMD_IRETURN:
1480                           if (returntype.type != TYPE_INT) panic("Return type mismatch");
1481                                                   goto return_tail;
1482                                                   
1483                       case ICMD_LRETURN:
1484                           if (returntype.type != TYPE_LONG) panic("Return type mismatch");
1485                                                   goto return_tail;
1486                                                   
1487                       case ICMD_FRETURN:
1488                           if (returntype.type != TYPE_FLOAT) panic("Return type mismatch");
1489                                                   goto return_tail;
1490                                                   
1491                       case ICMD_DRETURN:
1492                           if (returntype.type != TYPE_DOUBLE) panic("Return type mismatch");
1493                                                   goto return_tail;
1494                                                   
1495                       case ICMD_RETURN:
1496                           if (returntype.type != TYPE_VOID) panic("Return type mismatch");
1497                                           return_tail:
1498                           TYPECHECK_LEAVE;
1499                           superblockend = true;
1500                           maythrow = true;
1501                           break;
1502                                                     
1503                           /****************************************/
1504                           /* SUBROUTINE INSTRUCTIONS              */
1505
1506                       case ICMD_JSR:
1507                           LOG("jsr");
1508                                                   jsrencountered = true;
1509
1510                           /* This is a dirty hack. It is needed
1511                            * because of the special handling of
1512                            * ICMD_JSR in stack.c
1513                            */
1514                           dst = (stackptr) iptr->val.a;
1515                           
1516                           tbptr = (basicblock *) iptr->target;
1517                                                   if (bptr + 1 == (m->basicblocks + m->basicblockcount + 1))
1518                                                           panic("Illegal instruction: JSR at end of bytecode");
1519                                                   typestack_put_retaddr(dst,bptr+1,localset);
1520                                                   repeat |= typestate_reach(m, localbuf,bptr,tbptr,dst,
1521                                                                                                         localset,numlocals,true);
1522
1523                                                   superblockend = true;
1524                           break;
1525                           
1526                       case ICMD_RET:
1527                           /* check returnAddress variable */
1528                                                   if (!typevectorset_checkretaddr(localset,iptr->op1))
1529                               panic("illegal instruction: RET using non-returnAddress variable");
1530
1531                                                   repeat |= typestate_ret(m, localbuf,bptr,curstack,
1532                                                                                                   localset,iptr->op1,numlocals);
1533
1534                           superblockend = true;
1535                           break;
1536                                                           
1537                           /****************************************/
1538                           /* INVOKATIONS                          */
1539
1540                       case ICMD_INVOKEVIRTUAL:
1541                       case ICMD_INVOKESPECIAL:
1542                       case ICMD_INVOKESTATIC:
1543                       case ICMD_INVOKEINTERFACE:
1544                                                   TYPECHECK_COUNT(stat_ins_invoke);
1545                           {
1546                               methodinfo *mi = (methodinfo*) iptr->val.a;
1547                                                           bool specialmethod = (mi->name->text[0] == '<');
1548                               bool callinginit = (opcode == ICMD_INVOKESPECIAL && mi->name == name_init);
1549                               instruction *ins;
1550                               classinfo *initclass;
1551
1552                                                           if (specialmethod && !callinginit)
1553                                                                   panic("Invalid invocation of special method");
1554
1555                                                           if (opcode == ICMD_INVOKESPECIAL) {
1556                                                                   /* XXX for INVOKESPECIAL: check if the invokation is done at all */
1557                                                                   
1558                                                                   /* (If callinginit the class is checked later.) */
1559                                                                   if (!callinginit) { 
1560                                                                           if (!builtin_isanysubclass(myclass,mi->class)) 
1561                                                                                   panic("Illegal instruction: INVOKESPECIAL calling non-superclass method"); 
1562                                                                   } 
1563                                                           }
1564
1565                               /* fetch parameter types and return type */
1566                               i = 0;
1567                               if (opcode != ICMD_INVOKESTATIC) {
1568                                   ptype[0] = TYPE_ADR;
1569                                   TYPEINFO_INIT_CLASSINFO(pinfo[0],mi->class);
1570                                   i++;
1571                               }
1572                               typeinfo_init_from_method_args(mi->descriptor,ptype+i,pinfo+i,
1573                                                              MAXPARAMS-i,false,
1574                                                              &rtype,&rinfo);
1575
1576                               /* check parameter types */
1577                               srcstack = curstack;
1578                               i = mi->paramcount; /* number of parameters including 'this'*/
1579                               while (--i >= 0) {
1580                                   LOG1("param %d",i);
1581                                   if (srcstack->type != ptype[i])
1582                                       panic("Parameter type mismatch in method invocation");
1583                                   if (srcstack->type == TYPE_ADR) {
1584                                       LOGINFO(&(srcstack->typeinfo));
1585                                       LOGINFO(pinfo + i);
1586                                       if (i==0 && callinginit)
1587                                       {
1588                                           /* first argument to <init> method */
1589                                           if (!TYPEINFO_IS_NEWOBJECT(srcstack->typeinfo))
1590                                               panic("Calling <init> on initialized object");
1591                                           
1592                                           /* get the address of the NEW instruction */
1593                                           LOGINFO(&(srcstack->typeinfo));
1594                                           ins = (instruction*)TYPEINFO_NEWOBJECT_INSTRUCTION(srcstack->typeinfo);
1595                                           initclass = (ins) ? (classinfo*)ins[-1].val.a : m->class;
1596                                           LOGSTR("class: "); LOGSTRu(initclass->name); LOGNL;
1597
1598                                                                                   /* check type */
1599                                                                                   /* (This is checked below.) */
1600 /*                                                                                TYPEINFO_INIT_CLASSINFO(tempinfo,initclass); */
1601 /*                                           if (!typeinfo_is_assignable(&tempinfo,pinfo+0)) */
1602 /*                                               panic("Parameter reference type mismatch in <init> invocation"); */
1603                                       }
1604                                       else {
1605                                           if (!typeinfo_is_assignable(&(srcstack->typeinfo),pinfo+i))
1606                                               panic("Parameter reference type mismatch in method invocation");
1607                                       }
1608                                   }
1609                                   LOG("ok");
1610
1611                                   if (i) srcstack = srcstack->prev;
1612                               }
1613
1614                                                           /* XXX We should resolve the method and pass its
1615                                                            * class as implementingclass to is_accessible. */
1616                                                           if (!is_accessible(mi->flags,mi->class,NULL, myclass,
1617                                                                                                  (opcode == ICMD_INVOKESTATIC) ? NULL
1618                                                                                                  : &(srcstack->typeinfo)))
1619                                                                   panic("Invoking unaccessible method");
1620
1621                                                           LOG("checking return type");
1622                               if (rtype != TYPE_VOID) {
1623                                   if (rtype != dst->type)
1624                                       panic("Return type mismatch in method invocation");
1625                                   TYPEINFO_COPY(rinfo,dst->typeinfo);
1626                               }
1627
1628                               if (callinginit) {
1629                                                                   LOG("replacing uninitialized object");
1630                                   /* replace uninitialized object type on stack */
1631                                   srcstack = dst;
1632                                   while (srcstack) {
1633                                       if (srcstack->type == TYPE_ADR
1634                                           && TYPEINFO_IS_NEWOBJECT(srcstack->typeinfo)
1635                                           && TYPEINFO_NEWOBJECT_INSTRUCTION(srcstack->typeinfo) == ins)
1636                                       {
1637                                           LOG("replacing uninitialized type on stack");
1638
1639                                                                                   /* If this stackslot is in the instack of
1640                                                                                    * this basic block we must save the type(s)
1641                                                                                    * we are going to replace.
1642                                                                                    */
1643                                                                                   if (srcstack <= bptr->instack && !savedstack)
1644                                                                                   {
1645                                                                                           stackptr sp;
1646                                                                                           stackptr copy;
1647                                                                                           LOG("saving input stack types");
1648                                                                                           if (!savedstackbuf) {
1649                                                                                                   LOG("allocating savedstack buffer");
1650                                                                                                   savedstackbuf = DMNEW(stackelement,m->maxstack);
1651                                                                                                   savedstackbuf->prev = NULL;
1652                                                                                                   for (i=1; i<m->maxstack; ++i)
1653                                                                                                           savedstackbuf[i].prev = savedstackbuf+(i-1);
1654                                                                                           }
1655                                                                                           sp = savedstack = bptr->instack;
1656                                                                                           copy = bptr->instack = savedstackbuf + (bptr->indepth-1);
1657                                                                                           TYPESTACK_COPY(sp,copy);
1658                                                                                   }
1659                                                                                   
1660                                           TYPEINFO_INIT_CLASSINFO(srcstack->typeinfo,initclass);
1661                                       }
1662                                       srcstack = srcstack->prev;
1663                                   }
1664                                   /* replace uninitialized object type in locals */
1665                                                                   typevectorset_init_object(localset,ins,initclass,numlocals);
1666
1667                                   /* initializing the 'this' reference? */
1668                                   if (!ins) {
1669 #ifdef TYPECHECK_DEBUG
1670                                                                           if (!initmethod)
1671                                                                                   panic("Internal error: calling <init> on this in non-<init> method.");
1672 #endif
1673                                                                           /* must be <init> of current class or direct superclass */
1674                                                                           if (mi->class != m->class && mi->class != m->class->super)
1675                                                                                   panic("<init> calling <init> of the wrong class");
1676                                                                           
1677                                       /* set our marker variable to type int */
1678                                       LOG("setting <init> marker");
1679                                                                           typevectorset_store(localset,numlocals-1,TYPE_INT,NULL);
1680                                   }
1681                                                                   else {
1682                                                                           /* initializing an instance created with NEW */
1683                                                                           /* XXX is this strictness ok? */
1684                                                                           if (mi->class != initclass)
1685                                                                                   panic("Calling <init> method of the wrong class");
1686                                                                   }
1687                               }
1688                           }
1689                           maythrow = true;
1690                           break;
1691                           
1692                       case ICMD_MULTIANEWARRAY:
1693                                                   {
1694                                                           vftbl_t *arrayvftbl;
1695                                                           arraydescriptor *desc;
1696                                                           
1697                                                           /* check the array lengths on the stack */
1698                                                           i = iptr[0].op1;
1699                                                           if (i<1) panic("MULTIANEWARRAY with dimensions < 1");
1700                                                           srcstack = curstack;
1701                                                           while (i--) {
1702                                                                   if (!srcstack)
1703                                                                           panic("MULTIANEWARRAY missing array length");
1704                                                                   if (srcstack->type != TYPE_INT)
1705                                                                           panic("MULTIANEWARRAY using non-int as array length");
1706                                                                   srcstack = srcstack->prev;
1707                                                           }
1708                                                           
1709                                                           /* check array descriptor */
1710                                                           arrayvftbl = (vftbl_t*) iptr[0].val.a;
1711                                                           if (!arrayvftbl)
1712                                                                   panic("MULTIANEWARRAY with unlinked class");
1713                                                           if ((desc = arrayvftbl->arraydesc) == NULL)
1714                                                                   panic("MULTIANEWARRAY with non-array class");
1715                                                           if (desc->dimension < iptr[0].op1)
1716                                                                   panic("MULTIANEWARRAY dimension to high");
1717                                                           
1718                                                           /* set the array type of the result */
1719                                                           TYPEINFO_INIT_CLASSINFO(dst->typeinfo,arrayvftbl->class);
1720                                                   }
1721                           maythrow = true;
1722                           break;
1723                           
1724                       case ICMD_BUILTIN3:
1725                                                   TYPECHECK_COUNT(stat_ins_builtin);
1726                           if (ISBUILTIN(BUILTIN_aastore)) {
1727                                                           TYPECHECK_ADR(curstack);
1728                                                           TYPECHECK_INT(curstack->prev);
1729                                                           TYPECHECK_ADR(curstack->prev->prev);
1730                               if (!TYPEINFO_MAYBE_ARRAY_OF_REFS(curstack->prev->prev->typeinfo))
1731                                   panic("illegal instruction: AASTORE to non-reference array");
1732                           }
1733                                                   else {
1734                                                           /* XXX put these checks in a function */
1735                                                           TYPECHECK_COUNT(stat_ins_builtin_gen);
1736                                                           builtindesc = builtin_desc;
1737                                                           while (builtindesc->opcode && builtindesc->builtin
1738                                                                          != (functionptr) iptr->val.a) builtindesc++;
1739                                                           if (!builtindesc->opcode) {
1740                                                                   dolog("Builtin not in table: %s",icmd_builtin_name((functionptr) iptr->val.a));
1741                                                                   panic("Internal error: builtin not found in table");
1742                                                           }
1743                                                           TYPECHECK_ARGS3(builtindesc->type_s3,builtindesc->type_s2,builtindesc->type_s1);
1744                                                   }
1745                           maythrow = true;
1746                           break;
1747                           
1748                       case ICMD_BUILTIN2:
1749                                                   TYPECHECK_COUNT(stat_ins_builtin);
1750                           if (ISBUILTIN(BUILTIN_newarray))
1751                           {
1752                                                           vftbl_t *vft;
1753                                                           TYPECHECK_INT(curstack->prev);
1754                               if (iptr[-1].opc != ICMD_ACONST)
1755                                   panic("illegal instruction: builtin_newarray without classinfo");
1756                                                           vft = (vftbl_t *)iptr[-1].val.a;
1757                                                           if (!vft)
1758                                                                   panic("ANEWARRAY with unlinked class");
1759                                                           if (!vft->arraydesc)
1760                                                                   panic("ANEWARRAY with non-array class");
1761                               TYPEINFO_INIT_CLASSINFO(dst->typeinfo,vft->class);
1762                           }
1763                           else if (ISBUILTIN(BUILTIN_arrayinstanceof))
1764                           {
1765                                                           vftbl_t *vft;
1766                                                           TYPECHECK_ADR(curstack->prev);
1767                               if (iptr[-1].opc != ICMD_ACONST)
1768                                   panic("illegal instruction: builtin_arrayinstanceof without classinfo");
1769                                                           vft = (vftbl_t *)iptr[-1].val.a;
1770                                                           if (!vft)
1771                                                                   panic("INSTANCEOF with unlinked class");
1772                                                           if (!vft->arraydesc)
1773                                                                   panic("internal error: builtin_arrayinstanceof with non-array class");
1774                                                   }
1775                           else if (ISBUILTIN(BUILTIN_checkarraycast)) {
1776                                                           vftbl_t *vft;
1777                                                           TYPECHECK_ADR(curstack->prev);
1778                               if (iptr[-1].opc != ICMD_ACONST)
1779                                   panic("illegal instruction: BUILTIN_checkarraycast without classinfo");
1780                                                           vft = (vftbl_t *)iptr[-1].val.a;
1781                                                           if (!vft)
1782                                                                   panic("CHECKCAST with unlinked class");
1783                                                           if (!vft->arraydesc)
1784                                                                   panic("internal error: builtin_checkarraycast with non-array class");
1785                               TYPEINFO_INIT_CLASSINFO(dst->typeinfo,vft->class);
1786                           }
1787                                                   else {
1788                                                           TYPECHECK_COUNT(stat_ins_builtin_gen);
1789                                                           builtindesc = builtin_desc;
1790                                                           while (builtindesc->opcode && builtindesc->builtin
1791                                                                          != (functionptr) iptr->val.a) builtindesc++;
1792                                                           if (!builtindesc->opcode) {
1793                                                                   dolog("Builtin not in table: %s",icmd_builtin_name((functionptr) iptr->val.a));
1794                                                                   panic("Internal error: builtin not found in table");
1795                                                           }
1796                                                           TYPECHECK_ARGS2(builtindesc->type_s2,builtindesc->type_s1);
1797                                                   }
1798                           maythrow = true;
1799                           break;
1800                           
1801                       case ICMD_BUILTIN1:
1802                                                   TYPECHECK_COUNT(stat_ins_builtin);
1803                           if (ISBUILTIN(BUILTIN_new)) {
1804                                                           
1805                               if (iptr[-1].opc != ICMD_ACONST)
1806                                   panic("illegal instruction: builtin_new without classinfo");
1807                                                           cls = (classinfo *) iptr[-1].val.a;
1808                                                           if (!cls->linked)
1809                                                                   panic("Internal error: NEW with unlinked class");
1810                                                           /* The following check also forbids array classes and interfaces: */
1811                                                           if ((cls->flags & ACC_ABSTRACT) != 0)
1812                                                                   panic("Invalid instruction: NEW creating instance of abstract class");
1813                               TYPEINFO_INIT_NEWOBJECT(dst->typeinfo,iptr);
1814                           }
1815                           else if (ISBUILTIN(BUILTIN_newarray_boolean)) {
1816                                                           TYPECHECK_INT(curstack);
1817                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_BOOLEAN);
1818                           }
1819                           else if (ISBUILTIN(BUILTIN_newarray_char)) {
1820                                                           TYPECHECK_INT(curstack);
1821                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_CHAR);
1822                           }
1823                           else if (ISBUILTIN(BUILTIN_newarray_float)) {
1824                                                           TYPECHECK_INT(curstack);
1825                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_FLOAT);
1826                           }
1827                           else if (ISBUILTIN(BUILTIN_newarray_double)) {
1828                                                           TYPECHECK_INT(curstack);
1829                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_DOUBLE);
1830                           }
1831                           else if (ISBUILTIN(BUILTIN_newarray_byte)) {
1832                                                           TYPECHECK_INT(curstack);
1833                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_BYTE);
1834                           }
1835                           else if (ISBUILTIN(BUILTIN_newarray_short)) {
1836                                                           TYPECHECK_INT(curstack);
1837                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_SHORT);
1838                           }
1839                           else if (ISBUILTIN(BUILTIN_newarray_int)) {
1840                                                           TYPECHECK_INT(curstack);
1841                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_INT);
1842                           }
1843                           else if (ISBUILTIN(BUILTIN_newarray_long)) {
1844                                                           TYPECHECK_INT(curstack);
1845                               TYPEINFO_INIT_PRIMITIVE_ARRAY(dst->typeinfo,ARRAYTYPE_LONG);
1846                           }
1847                                                   else {
1848                                                           TYPECHECK_COUNT(stat_ins_builtin_gen);
1849                                                           builtindesc = builtin_desc;
1850                                                           while (builtindesc->opcode && builtindesc->builtin
1851                                                                          != (functionptr) iptr->val.a) builtindesc++;
1852                                                           if (!builtindesc->opcode) {
1853                                                                   dolog("Builtin not in table: %s",icmd_builtin_name((functionptr) iptr->val.a));
1854                                                                   panic("Internal error: builtin not found in table");
1855                                                           }
1856                                                           TYPECHECK_ARGS1(builtindesc->type_s1);
1857                                                   }
1858                           maythrow = true;
1859                           break;
1860                                                      
1861                           /****************************************/
1862                           /* SIMPLE EXCEPTION THROWING TESTS      */
1863
1864                       case ICMD_CHECKASIZE:
1865                                                   /* The argument to CHECKASIZE is typechecked by
1866                                                    * typechecking the array creation instructions. */
1867
1868                                                   /* FALLTHROUGH! */
1869                       case ICMD_NULLCHECKPOP:
1870                                                   /* NULLCHECKPOP just requires that the stack top
1871                                                    * is an address. This is checked in stack.c */
1872                                                   
1873                           maythrow = true;
1874                           break;
1875
1876                           /****************************************/
1877                           /* INSTRUCTIONS WHICH SHOULD HAVE BEEN  */
1878                           /* REPLACED BY OTHER OPCODES            */
1879
1880 #ifdef TYPECHECK_DEBUG
1881                       case ICMD_NEW:
1882                       case ICMD_NEWARRAY:
1883                       case ICMD_ANEWARRAY:
1884                       case ICMD_MONITORENTER:
1885                       case ICMD_MONITOREXIT:
1886                       case ICMD_AASTORE:
1887                           LOG2("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
1888                                                   LOG("Should have been converted to builtin function call.");
1889                           panic("Internal error: unexpected instruction encountered");
1890                           break;
1891                                                      
1892                       case ICMD_READONLY_ARG:
1893                       case ICMD_CLEAR_ARGREN:
1894                           LOG2("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
1895                                                   LOG("Should have been replaced in stack.c.");
1896                           panic("Internal error: unexpected pseudo instruction encountered");
1897                           break;
1898 #endif
1899                                                   
1900                           /****************************************/
1901                           /* UNCHECKED OPERATIONS                 */
1902
1903                                                   /*********************************************
1904                                                    * Instructions below...
1905                                                    *     *) don't operate on local variables,
1906                                                    *     *) don't operate on references,
1907                                                    *     *) don't operate on returnAddresses.
1908                                                    *
1909                                                    * (These instructions are typechecked in
1910                                                    *  analyse_stack.)
1911                                                    ********************************************/
1912
1913                           /* Instructions which may throw a runtime exception: */
1914                           
1915                       case ICMD_IDIV:
1916                       case ICMD_IREM:
1917                       case ICMD_LDIV:
1918                       case ICMD_LREM:
1919                           
1920                           maythrow = true;
1921                           break;
1922                           
1923                           /* Instructions which never throw a runtime exception: */
1924 #if defined(TYPECHECK_DEBUG) || defined(TYPECHECK_STATISTICS)
1925                       case ICMD_NOP:
1926                       case ICMD_POP:
1927                       case ICMD_POP2:
1928
1929                       case ICMD_ICONST:
1930                       case ICMD_LCONST:
1931                       case ICMD_FCONST:
1932                       case ICMD_DCONST:
1933
1934                       case ICMD_IFEQ_ICONST:
1935                       case ICMD_IFNE_ICONST:
1936                       case ICMD_IFLT_ICONST:
1937                       case ICMD_IFGE_ICONST:
1938                       case ICMD_IFGT_ICONST:
1939                       case ICMD_IFLE_ICONST:
1940                       case ICMD_ELSE_ICONST:
1941
1942                       case ICMD_IADD:
1943                       case ICMD_ISUB:
1944                       case ICMD_IMUL:
1945                       case ICMD_INEG:
1946                       case ICMD_IAND:
1947                       case ICMD_IOR:
1948                       case ICMD_IXOR:
1949                       case ICMD_ISHL:
1950                       case ICMD_ISHR:
1951                       case ICMD_IUSHR:
1952                       case ICMD_LADD:
1953                       case ICMD_LSUB:
1954                       case ICMD_LMUL:
1955                       case ICMD_LNEG:
1956                       case ICMD_LAND:
1957                       case ICMD_LOR:
1958                       case ICMD_LXOR:
1959                       case ICMD_LSHL:
1960                       case ICMD_LSHR:
1961                       case ICMD_LUSHR:
1962                       case ICMD_IREM0X10001:
1963                       case ICMD_LREM0X10001:
1964                       case ICMD_IDIVPOW2:
1965                       case ICMD_LDIVPOW2:
1966                       case ICMD_IADDCONST:
1967                       case ICMD_ISUBCONST:
1968                       case ICMD_IMULCONST:
1969                       case ICMD_IANDCONST:
1970                       case ICMD_IORCONST:
1971                       case ICMD_IXORCONST:
1972                       case ICMD_ISHLCONST:
1973                       case ICMD_ISHRCONST:
1974                       case ICMD_IUSHRCONST:
1975                       case ICMD_IREMPOW2:
1976                       case ICMD_LADDCONST:
1977                       case ICMD_LSUBCONST:
1978                       case ICMD_LMULCONST:
1979                       case ICMD_LANDCONST:
1980                       case ICMD_LORCONST:
1981                       case ICMD_LXORCONST:
1982                       case ICMD_LSHLCONST:
1983                       case ICMD_LSHRCONST:
1984                       case ICMD_LUSHRCONST:
1985                       case ICMD_LREMPOW2:
1986                           
1987                       case ICMD_I2L:
1988                       case ICMD_I2F:
1989                       case ICMD_I2D:
1990                       case ICMD_L2I:
1991                       case ICMD_L2F:
1992                       case ICMD_L2D:
1993                       case ICMD_F2I:
1994                       case ICMD_F2L:
1995                       case ICMD_F2D:
1996                       case ICMD_D2I:
1997                       case ICMD_D2L:
1998                       case ICMD_D2F:
1999                       case ICMD_INT2BYTE:
2000                       case ICMD_INT2CHAR:
2001                       case ICMD_INT2SHORT:
2002
2003                       case ICMD_LCMP:
2004                       case ICMD_LCMPCONST:
2005                       case ICMD_FCMPL:
2006                       case ICMD_FCMPG:
2007                       case ICMD_DCMPL:
2008                       case ICMD_DCMPG:
2009                           
2010                       case ICMD_FADD:
2011                       case ICMD_DADD:
2012                       case ICMD_FSUB:
2013                       case ICMD_DSUB:
2014                       case ICMD_FMUL:
2015                       case ICMD_DMUL:
2016                       case ICMD_FDIV:
2017                       case ICMD_DDIV:
2018                       case ICMD_FREM:
2019                       case ICMD_DREM:
2020                       case ICMD_FNEG:
2021                       case ICMD_DNEG:
2022
2023                                                   TYPECHECK_COUNT(stat_ins_unchecked);
2024                           break;
2025                           
2026                           /****************************************/
2027
2028                       default:
2029                           LOG2("ICMD %d at %d\n", iptr->opc, (int)(iptr-instr));
2030                           panic("Missing ICMD code during typecheck");
2031 #endif
2032                                         }
2033
2034                     /* the output of this instruction becomes the current stack */
2035                     curstack = dst;
2036                     
2037                     /* reach exception handlers for this instruction */
2038                     if (maythrow) {
2039                         LOG("reaching exception handlers");
2040                         i = 0;
2041                         while (handlers[i]) {
2042                                                         TYPECHECK_COUNT(stat_handlers_reached);
2043                                                         cls = handlers[i]->catchtype;
2044                                                         excstack.typeinfo.typeclass = (cls) ? cls
2045                                                                 : class_java_lang_Throwable;
2046                                                         repeat |= typestate_reach(m, localbuf,bptr,
2047                                                                                                           handlers[i]->handler,
2048                                                                                                           &excstack,localset,
2049                                                                                                           numlocals,
2050                                                                                                           jsrencountered);
2051                             i++;
2052                         }
2053                     }
2054
2055                     iptr++;
2056                 } /* while instructions */
2057
2058                 LOG("instructions done");
2059                 LOGSTR("RESULT=> ");
2060                 DOLOG(typestate_print(get_logfile(),curstack,localset,numlocals));
2061                 LOGNL; LOGFLUSH;
2062                 
2063                 /* propagate stack and variables to the following block */
2064                 if (!superblockend) {
2065                     LOG("reaching following block");
2066                     tbptr = bptr + 1;
2067                     while (tbptr->flags == BBDELETED) {
2068                         tbptr++;
2069 #ifdef TYPECHECK_DEBUG
2070                         if ((tbptr-block) >= m->basicblockcount)
2071                             panic("Control flow falls off the last block");
2072 #endif
2073                     }
2074                     TYPECHECK_REACH;
2075                 }
2076
2077                                 /* We may have to restore the types of the instack slots. They
2078                                  * have been saved if an <init> call inside the block has
2079                                  * modified the instack types. (see INVOKESPECIAL) */
2080                                 
2081                                 if (savedstack) {
2082                                         stackptr sp = bptr->instack;
2083                                         stackptr copy = savedstack;
2084                                         TYPECHECK_COUNT(stat_savedstack);
2085                                         LOG("restoring saved instack");
2086                                         TYPESTACK_COPY(sp,copy);
2087                                         bptr->instack = savedstack;
2088                                         savedstack = NULL;
2089                                 }
2090                 
2091             } /* if block has to be checked */
2092             bptr++;
2093         } /* while blocks */
2094
2095         LOGIF(repeat,"repeat=true");
2096     } while (repeat);
2097
2098 #ifdef TYPECHECK_STATISTICS
2099         dolog("Typechecker did %4d iterations",count_iterations);
2100         TYPECHECK_COUNT_FREQ(stat_iterations,count_iterations,STAT_ITERATIONS);
2101         TYPECHECK_COUNTIF(jsrencountered,stat_typechecked_jsr);
2102 #endif
2103
2104 #ifdef TYPECHECK_DEBUG
2105         for (i=0; i<m->basicblockcount; ++i) {
2106                 if (m->basicblocks[i].flags != BBDELETED
2107                         && m->basicblocks[i].flags != BBUNDEF
2108                         && m->basicblocks[i].flags != BBFINISHED
2109                         && m->basicblocks[i].flags != BBTYPECHECK_UNDEF) /* typecheck may never reach
2110                                                                                                          * some exception handlers,
2111                                                                                                          * that's ok. */
2112                 {
2113                         LOG2("block L%03d has invalid flags after typecheck: %d",
2114                                  m->basicblocks[i].debug_nr,m->basicblocks[i].flags);
2115                         panic("Invalid block flags after typecheck");
2116                 }
2117         }
2118 #endif
2119         
2120         /* Reset blocks we never reached */
2121         for (i=0; i<m->basicblockcount; ++i) {
2122                 if (m->basicblocks[i].flags == BBTYPECHECK_UNDEF)
2123                         m->basicblocks[i].flags = BBFINISHED;
2124         }
2125                 
2126     LOGimp("exiting typecheck");
2127
2128         /* just return methodinfo* to signal everything was ok */
2129
2130         return m;
2131 }
2132
2133 #undef COPYTYPE
2134
2135 #endif /* CACAO_TYPECHECK */
2136
2137 /*
2138  * These are local overrides for various environment variables in Emacs.
2139  * Please do not remove this and leave it at the end of the file, where
2140  * Emacs will automagically detect them.
2141  * ---------------------------------------------------------------------
2142  * Local variables:
2143  * mode: c
2144  * indent-tabs-mode: t
2145  * c-basic-offset: 4
2146  * tab-width: 4
2147  * End:
2148  */