OSDN Git Service

PR libgcj/10582:
[pf3gnuchains/gcc-fork.git] / libjava / verify.cc
1 // defineclass.cc - defining a class from .class format.
2
3 /* Copyright (C) 2001, 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 // Written by Tom Tromey <tromey@redhat.com>
12
13 // Define VERIFY_DEBUG to enable debugging output.
14
15 #include <config.h>
16
17 #include <jvm.h>
18 #include <gcj/cni.h>
19 #include <java-insns.h>
20 #include <java-interp.h>
21
22 #ifdef INTERPRETER
23
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
29
30 #ifdef VERIFY_DEBUG
31 #include <stdio.h>
32 #endif /* VERIFY_DEBUG */
33
34
35 static void debug_print (const char *fmt, ...)
36   __attribute__ ((format (printf, 1, 2)));
37
38 static inline void
39 debug_print (const char *fmt, ...)
40 {
41 #ifdef VERIFY_DEBUG
42   va_list ap;
43   va_start (ap, fmt);
44   vfprintf (stderr, fmt, ap);
45   va_end (ap);
46 #endif /* VERIFY_DEBUG */
47 }
48
49 class _Jv_BytecodeVerifier
50 {
51 private:
52
53   static const int FLAG_INSN_START = 1;
54   static const int FLAG_BRANCH_TARGET = 2;
55
56   struct state;
57   struct type;
58   struct subr_info;
59   struct subr_entry_info;
60   struct linked_utf8;
61
62   // The current PC.
63   int PC;
64   // The PC corresponding to the start of the current instruction.
65   int start_PC;
66
67   // The current state of the stack, locals, etc.
68   state *current_state;
69
70   // We store the state at branch targets, for merging.  This holds
71   // such states.
72   state **states;
73
74   // We keep a linked list of all the PCs which we must reverify.
75   // The link is done using the PC values.  This is the head of the
76   // list.
77   int next_verify_pc;
78
79   // We keep some flags for each instruction.  The values are the
80   // FLAG_* constants defined above.
81   char *flags;
82
83   // We need to keep track of which instructions can call a given
84   // subroutine.  FIXME: this is inefficient.  We keep a linked list
85   // of all calling `jsr's at at each jsr target.
86   subr_info **jsr_ptrs;
87
88   // We keep a linked list of entries which map each `ret' instruction
89   // to its unique subroutine entry point.  We expect that there won't
90   // be many `ret' instructions, so a linked list is ok.
91   subr_entry_info *entry_points;
92
93   // The bytecode itself.
94   unsigned char *bytecode;
95   // The exceptions.
96   _Jv_InterpException *exception;
97
98   // Defining class.
99   jclass current_class;
100   // This method.
101   _Jv_InterpMethod *current_method;
102
103   // A linked list of utf8 objects we allocate.  This is really ugly,
104   // but without this our utf8 objects would be collected.
105   linked_utf8 *utf8_list;
106
107   struct linked_utf8
108   {
109     _Jv_Utf8Const *val;
110     linked_utf8 *next;
111   };
112
113   _Jv_Utf8Const *make_utf8_const (char *s, int len)
114   {
115     _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
116     _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
117                                                      + val->length
118                                                      + 1);
119     r->length = val->length;
120     r->hash = val->hash;
121     memcpy (r->data, val->data, val->length + 1);
122
123     linked_utf8 *lu = (linked_utf8 *) _Jv_Malloc (sizeof (linked_utf8));
124     lu->val = r;
125     lu->next = utf8_list;
126     utf8_list = lu;
127
128     return r;
129   }
130
131   __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
132   {
133     using namespace java::lang;
134     StringBuffer *buf = new StringBuffer ();
135
136     buf->append (JvNewStringLatin1 ("verification failed"));
137     if (pc == -1)
138       pc = start_PC;
139     if (pc != -1)
140       {
141         buf->append (JvNewStringLatin1 (" at PC "));
142         buf->append (pc);
143       }
144
145     _Jv_InterpMethod *method = current_method;
146     buf->append (JvNewStringLatin1 (" in "));
147     buf->append (current_class->getName());
148     buf->append ((jchar) ':');
149     buf->append (JvNewStringUTF (method->get_method()->name->data));
150     buf->append ((jchar) '(');
151     buf->append (JvNewStringUTF (method->get_method()->signature->data));
152     buf->append ((jchar) ')');
153
154     buf->append (JvNewStringLatin1 (": "));
155     buf->append (JvNewStringLatin1 (s));
156     throw new java::lang::VerifyError (buf->toString ());
157   }
158
159   // This enum holds a list of tags for all the different types we
160   // need to handle.  Reference types are treated specially by the
161   // type class.
162   enum type_val
163   {
164     void_type,
165
166     // The values for primitive types are chosen to correspond to values
167     // specified to newarray.
168     boolean_type = 4,
169     char_type = 5,
170     float_type = 6,
171     double_type = 7,
172     byte_type = 8,
173     short_type = 9,
174     int_type = 10,
175     long_type = 11,
176
177     // Used when overwriting second word of a double or long in the
178     // local variables.  Also used after merging local variable states
179     // to indicate an unusable value.
180     unsuitable_type,
181     return_address_type,
182     continuation_type,
183
184     // There is an obscure special case which requires us to note when
185     // a local variable has not been used by a subroutine.  See
186     // push_jump_merge for more information.
187     unused_by_subroutine_type,
188
189     // Everything after `reference_type' must be a reference type.
190     reference_type,
191     null_type,
192     unresolved_reference_type,
193     uninitialized_reference_type,
194     uninitialized_unresolved_reference_type
195   };
196
197   // Return the type_val corresponding to a primitive signature
198   // character.  For instance `I' returns `int.class'.
199   type_val get_type_val_for_signature (jchar sig)
200   {
201     type_val rt;
202     switch (sig)
203       {
204       case 'Z':
205         rt = boolean_type;
206         break;
207       case 'B':
208         rt = byte_type;
209         break;
210       case 'C':
211         rt = char_type;
212         break;
213       case 'S':
214         rt = short_type;
215         break;
216       case 'I':
217         rt = int_type;
218         break;
219       case 'J':
220         rt = long_type;
221         break;
222       case 'F':
223         rt = float_type;
224         break;
225       case 'D':
226         rt = double_type;
227         break;
228       case 'V':
229         rt = void_type;
230         break;
231       default:
232         verify_fail ("invalid signature");
233       }
234     return rt;
235   }
236
237   // Return the type_val corresponding to a primitive class.
238   type_val get_type_val_for_signature (jclass k)
239   {
240     return get_type_val_for_signature ((jchar) k->method_count);
241   }
242
243   // This is used to keep track of which `jsr's correspond to a given
244   // jsr target.
245   struct subr_info
246   {
247     // PC of the instruction just after the jsr.
248     int pc;
249     // Link.
250     subr_info *next;
251   };
252
253   // This is used to keep track of which subroutine entry point
254   // corresponds to which `ret' instruction.
255   struct subr_entry_info
256   {
257     // PC of the subroutine entry point.
258     int pc;
259     // PC of the `ret' instruction.
260     int ret_pc;
261     // Link.
262     subr_entry_info *next;
263   };
264
265   // The `type' class is used to represent a single type in the
266   // verifier.
267   struct type
268   {
269     // The type.
270     type_val key;
271     // Some associated data.
272     union
273     {
274       // For a resolved reference type, this is a pointer to the class.
275       jclass klass;
276       // For other reference types, this it the name of the class.
277       _Jv_Utf8Const *name;
278     } data;
279     // This is used when constructing a new object.  It is the PC of the
280     // `new' instruction which created the object.  We use the special
281     // value -2 to mean that this is uninitialized, and the special
282     // value -1 for the case where the current method is itself the
283     // <init> method.
284     int pc;
285
286     static const int UNINIT = -2;
287     static const int SELF = -1;
288
289     // Basic constructor.
290     type ()
291     {
292       key = unsuitable_type;
293       data.klass = NULL;
294       pc = UNINIT;
295     }
296
297     // Make a new instance given the type tag.  We assume a generic
298     // `reference_type' means Object.
299     type (type_val k)
300     {
301       key = k;
302       data.klass = NULL;
303       if (key == reference_type)
304         data.klass = &java::lang::Object::class$;
305       pc = UNINIT;
306     }
307
308     // Make a new instance given a class.
309     type (jclass klass)
310     {
311       key = reference_type;
312       data.klass = klass;
313       pc = UNINIT;
314     }
315
316     // Make a new instance given the name of a class.
317     type (_Jv_Utf8Const *n)
318     {
319       key = unresolved_reference_type;
320       data.name = n;
321       pc = UNINIT;
322     }
323
324     // Copy constructor.
325     type (const type &t)
326     {
327       key = t.key;
328       data = t.data;
329       pc = t.pc;
330     }
331
332     // These operators are required because libgcj can't link in
333     // -lstdc++.
334     void *operator new[] (size_t bytes)
335     {
336       return _Jv_Malloc (bytes);
337     }
338
339     void operator delete[] (void *mem)
340     {
341       _Jv_Free (mem);
342     }
343
344     type& operator= (type_val k)
345     {
346       key = k;
347       data.klass = NULL;
348       pc = UNINIT;
349       return *this;
350     }
351
352     type& operator= (const type& t)
353     {
354       key = t.key;
355       data = t.data;
356       pc = t.pc;
357       return *this;
358     }
359
360     // Promote a numeric type.
361     type &promote ()
362     {
363       if (key == boolean_type || key == char_type
364           || key == byte_type || key == short_type)
365         key = int_type;
366       return *this;
367     }
368
369     // If *THIS is an unresolved reference type, resolve it.
370     void resolve (_Jv_BytecodeVerifier *verifier)
371     {
372       if (key != unresolved_reference_type
373           && key != uninitialized_unresolved_reference_type)
374         return;
375
376       using namespace java::lang;
377       java::lang::ClassLoader *loader
378         = verifier->current_class->getClassLoaderInternal();
379       // We might see either kind of name.  Sigh.
380       if (data.name->data[0] == 'L'
381           && data.name->data[data.name->length - 1] == ';')
382         data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
383       else
384         data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
385                                      false, loader);
386       key = (key == unresolved_reference_type
387              ? reference_type
388              : uninitialized_reference_type);
389     }
390
391     // Mark this type as the uninitialized result of `new'.
392     void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
393     {
394       if (key == reference_type)
395         key = uninitialized_reference_type;
396       else if (key == unresolved_reference_type)
397         key = uninitialized_unresolved_reference_type;
398       else
399         verifier->verify_fail ("internal error in type::uninitialized");
400       pc = npc;
401     }
402
403     // Mark this type as now initialized.
404     void set_initialized (int npc)
405     {
406       if (npc != UNINIT && pc == npc
407           && (key == uninitialized_reference_type
408               || key == uninitialized_unresolved_reference_type))
409         {
410           key = (key == uninitialized_reference_type
411                  ? reference_type
412                  : unresolved_reference_type);
413           pc = UNINIT;
414         }
415     }
416
417
418     // Return true if an object of type K can be assigned to a variable
419     // of type *THIS.  Handle various special cases too.  Might modify
420     // *THIS or K.  Note however that this does not perform numeric
421     // promotion.
422     bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
423     {
424       // Any type is compatible with the unsuitable type.
425       if (key == unsuitable_type)
426         return true;
427
428       if (key < reference_type || k.key < reference_type)
429         return key == k.key;
430
431       // The `null' type is convertible to any initialized reference
432       // type.
433       if (key == null_type || k.key == null_type)
434         return true;
435
436       // Any reference type is convertible to Object.  This is a special
437       // case so we don't need to unnecessarily resolve a class.
438       if (key == reference_type
439           && data.klass == &java::lang::Object::class$)
440         return true;
441
442       // An initialized type and an uninitialized type are not
443       // compatible.
444       if (isinitialized () != k.isinitialized ())
445         return false;
446
447       // Two uninitialized objects are compatible if either:
448       // * The PCs are identical, or
449       // * One PC is UNINIT.
450       if (! isinitialized ())
451         {
452           if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
453             return false;
454         }
455
456       // Two unresolved types are equal if their names are the same.
457       if (! isresolved ()
458           && ! k.isresolved ()
459           && _Jv_equalUtf8Consts (data.name, k.data.name))
460         return true;
461
462       // We must resolve both types and check assignability.
463       resolve (verifier);
464       k.resolve (verifier);
465       return _Jv_IsAssignableFrom (data.klass, k.data.klass);
466     }
467
468     bool isvoid () const
469     {
470       return key == void_type;
471     }
472
473     bool iswide () const
474     {
475       return key == long_type || key == double_type;
476     }
477
478     // Return number of stack or local variable slots taken by this
479     // type.
480     int depth () const
481     {
482       return iswide () ? 2 : 1;
483     }
484
485     bool isarray () const
486     {
487       // We treat null_type as not an array.  This is ok based on the
488       // current uses of this method.
489       if (key == reference_type)
490         return data.klass->isArray ();
491       else if (key == unresolved_reference_type)
492         return data.name->data[0] == '[';
493       return false;
494     }
495
496     bool isnull () const
497     {
498       return key == null_type;
499     }
500
501     bool isinterface (_Jv_BytecodeVerifier *verifier)
502     {
503       resolve (verifier);
504       if (key != reference_type)
505         return false;
506       return data.klass->isInterface ();
507     }
508
509     bool isabstract (_Jv_BytecodeVerifier *verifier)
510     {
511       resolve (verifier);
512       if (key != reference_type)
513         return false;
514       using namespace java::lang::reflect;
515       return Modifier::isAbstract (data.klass->getModifiers ());
516     }
517
518     // Return the element type of an array.
519     type element_type (_Jv_BytecodeVerifier *verifier)
520     {
521       // FIXME: maybe should do string manipulation here.
522       resolve (verifier);
523       if (key != reference_type)
524         verifier->verify_fail ("programmer error in type::element_type()", -1);
525
526       jclass k = data.klass->getComponentType ();
527       if (k->isPrimitive ())
528         return type (verifier->get_type_val_for_signature (k));
529       return type (k);
530     }
531
532     // Return the array type corresponding to an initialized
533     // reference.  We could expand this to work for other kinds of
534     // types, but currently we don't need to.
535     type to_array (_Jv_BytecodeVerifier *verifier)
536     {
537       // Resolving isn't ideal, because it might force us to load
538       // another class, but it's easy.  FIXME?
539       if (key == unresolved_reference_type)
540         resolve (verifier);
541
542       if (key == reference_type)
543         return type (_Jv_GetArrayClass (data.klass,
544                                         data.klass->getClassLoaderInternal()));
545       else
546         verifier->verify_fail ("internal error in type::to_array()");
547     }
548
549     bool isreference () const
550     {
551       return key >= reference_type;
552     }
553
554     int get_pc () const
555     {
556       return pc;
557     }
558
559     bool isinitialized () const
560     {
561       return (key == reference_type
562               || key == null_type
563               || key == unresolved_reference_type);
564     }
565
566     bool isresolved () const
567     {
568       return (key == reference_type
569               || key == null_type
570               || key == uninitialized_reference_type);
571     }
572
573     void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
574     {
575       // The way this is written, we don't need to check isarray().
576       if (key == reference_type)
577         {
578           jclass k = data.klass;
579           while (k->isArray () && ndims > 0)
580             {
581               k = k->getComponentType ();
582               --ndims;
583             }
584         }
585       else
586         {
587           // We know KEY == unresolved_reference_type.
588           char *p = data.name->data;
589           while (*p++ == '[' && ndims-- > 0)
590             ;
591         }
592
593       if (ndims > 0)
594         verifier->verify_fail ("array type has fewer dimensions than required");
595     }
596
597     // Merge OLD_TYPE into this.  On error throw exception.
598     bool merge (type& old_type, bool local_semantics,
599                 _Jv_BytecodeVerifier *verifier)
600     {
601       bool changed = false;
602       bool refo = old_type.isreference ();
603       bool refn = isreference ();
604       if (refo && refn)
605         {
606           if (old_type.key == null_type)
607             ;
608           else if (key == null_type)
609             {
610               *this = old_type;
611               changed = true;
612             }
613           else if (isinitialized () != old_type.isinitialized ())
614             verifier->verify_fail ("merging initialized and uninitialized types");
615           else
616             {
617               if (! isinitialized ())
618                 {
619                   if (pc == UNINIT)
620                     pc = old_type.pc;
621                   else if (old_type.pc == UNINIT)
622                     ;
623                   else if (pc != old_type.pc)
624                     verifier->verify_fail ("merging different uninitialized types");
625                 }
626
627               if (! isresolved ()
628                   && ! old_type.isresolved ()
629                   && _Jv_equalUtf8Consts (data.name, old_type.data.name))
630                 {
631                   // Types are identical.
632                 }
633               else
634                 {
635                   resolve (verifier);
636                   old_type.resolve (verifier);
637
638                   jclass k = data.klass;
639                   jclass oldk = old_type.data.klass;
640
641                   int arraycount = 0;
642                   while (k->isArray () && oldk->isArray ())
643                     {
644                       ++arraycount;
645                       k = k->getComponentType ();
646                       oldk = oldk->getComponentType ();
647                     }
648
649                   // Ordinarily this terminates when we hit Object...
650                   while (k != NULL)
651                     {
652                       if (_Jv_IsAssignableFrom (k, oldk))
653                         break;
654                       k = k->getSuperclass ();
655                       changed = true;
656                     }
657                   // ... but K could have been an interface, in which
658                   // case we'll end up here.  We just convert this
659                   // into Object.
660                   if (k == NULL)
661                     k = &java::lang::Object::class$;
662
663                   if (changed)
664                     {
665                       while (arraycount > 0)
666                         {
667                           java::lang::ClassLoader *loader
668                             = verifier->current_class->getClassLoaderInternal();
669                           k = _Jv_GetArrayClass (k, loader);
670                           --arraycount;
671                         }
672                       data.klass = k;
673                     }
674                 }
675             }
676         }
677       else if (refo || refn || key != old_type.key)
678         {
679           if (local_semantics)
680             {
681               // If we're merging into an "unused" slot, then we
682               // simply accept whatever we're merging from.
683               if (key == unused_by_subroutine_type)
684                 {
685                   *this = old_type;
686                   changed = true;
687                 }
688               else if (old_type.key == unused_by_subroutine_type)
689                 {
690                   // Do nothing.
691                 }
692               // If we already have an `unsuitable' type, then we
693               // don't need to change again.
694               else if (key != unsuitable_type)
695                 {
696                   key = unsuitable_type;
697                   changed = true;
698                 }
699             }
700           else
701             verifier->verify_fail ("unmergeable type");
702         }
703       return changed;
704     }
705
706 #ifdef VERIFY_DEBUG
707     void print (void) const
708     {
709       char c = '?';
710       switch (key)
711         {
712         case boolean_type: c = 'Z'; break;
713         case byte_type: c = 'B'; break;
714         case char_type: c = 'C'; break;
715         case short_type: c = 'S'; break;
716         case int_type: c = 'I'; break;
717         case long_type: c = 'J'; break;
718         case float_type: c = 'F'; break;
719         case double_type: c = 'D'; break;
720         case void_type: c = 'V'; break;
721         case unsuitable_type: c = '-'; break;
722         case return_address_type: c = 'r'; break;
723         case continuation_type: c = '+'; break;
724         case unused_by_subroutine_type: c = '_'; break;
725         case reference_type: c = 'L'; break;
726         case null_type: c = '@'; break;
727         case unresolved_reference_type: c = 'l'; break;
728         case uninitialized_reference_type: c = 'U'; break;
729         case uninitialized_unresolved_reference_type: c = 'u'; break;
730         }
731       debug_print ("%c", c);
732     }
733 #endif /* VERIFY_DEBUG */
734   };
735
736   // This class holds all the state information we need for a given
737   // location.
738   struct state
739   {
740     // The current top of the stack, in terms of slots.
741     int stacktop;
742     // The current depth of the stack.  This will be larger than
743     // STACKTOP when wide types are on the stack.
744     int stackdepth;
745     // The stack.
746     type *stack;
747     // The local variables.
748     type *locals;
749     // This is used in subroutines to keep track of which local
750     // variables have been accessed.
751     bool *local_changed;
752     // If not 0, then we are in a subroutine.  The value is the PC of
753     // the subroutine's entry point.  We can use 0 as an exceptional
754     // value because PC=0 can never be a subroutine.
755     int subroutine;
756     // This is used to keep a linked list of all the states which
757     // require re-verification.  We use the PC to keep track.
758     int next;
759     // We keep track of the type of `this' specially.  This is used to
760     // ensure that an instance initializer invokes another initializer
761     // on `this' before returning.  We must keep track of this
762     // specially because otherwise we might be confused by code which
763     // assigns to locals[0] (overwriting `this') and then returns
764     // without really initializing.
765     type this_type;
766     // This is a list of all subroutines that have been seen at this
767     // point.  Ordinarily this is NULL; it is only allocated and used
768     // in relatively weird situations involving non-ret exit from a
769     // subroutine.  We have to keep track of this in this way to avoid
770     // endless recursion in these cases.
771     subr_info *seen_subrs;
772
773     // INVALID marks a state which is not on the linked list of states
774     // requiring reverification.
775     static const int INVALID = -1;
776     // NO_NEXT marks the state at the end of the reverification list.
777     static const int NO_NEXT = -2;
778
779     // This is used to mark the stack depth at the instruction just
780     // after a `jsr' when we haven't yet processed the corresponding
781     // `ret'.  See handle_jsr_insn for more information.
782     static const int NO_STACK = -1;
783
784     state ()
785       : this_type ()
786     {
787       stack = NULL;
788       locals = NULL;
789       local_changed = NULL;
790       seen_subrs = NULL;
791     }
792
793     state (int max_stack, int max_locals)
794       : this_type ()
795     {
796       stacktop = 0;
797       stackdepth = 0;
798       stack = new type[max_stack];
799       for (int i = 0; i < max_stack; ++i)
800         stack[i] = unsuitable_type;
801       locals = new type[max_locals];
802       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
803       seen_subrs = NULL;
804       for (int i = 0; i < max_locals; ++i)
805         {
806           locals[i] = unsuitable_type;
807           local_changed[i] = false;
808         }
809       next = INVALID;
810       subroutine = 0;
811     }
812
813     state (const state *orig, int max_stack, int max_locals,
814            bool ret_semantics = false)
815     {
816       stack = new type[max_stack];
817       locals = new type[max_locals];
818       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
819       seen_subrs = NULL;
820       copy (orig, max_stack, max_locals, ret_semantics);
821       next = INVALID;
822     }
823
824     ~state ()
825     {
826       if (stack)
827         delete[] stack;
828       if (locals)
829         delete[] locals;
830       if (local_changed)
831         _Jv_Free (local_changed);
832       clean_subrs ();
833     }
834
835     void *operator new[] (size_t bytes)
836     {
837       return _Jv_Malloc (bytes);
838     }
839
840     void operator delete[] (void *mem)
841     {
842       _Jv_Free (mem);
843     }
844
845     void *operator new (size_t bytes)
846     {
847       return _Jv_Malloc (bytes);
848     }
849
850     void operator delete (void *mem)
851     {
852       _Jv_Free (mem);
853     }
854
855     void clean_subrs ()
856     {
857       subr_info *info = seen_subrs;
858       while (info != NULL)
859         {
860           subr_info *next = info->next;
861           _Jv_Free (info);
862           info = next;
863         }
864     }
865
866     void copy (const state *copy, int max_stack, int max_locals,
867                bool ret_semantics = false)
868     {
869       stacktop = copy->stacktop;
870       stackdepth = copy->stackdepth;
871       subroutine = copy->subroutine;
872       for (int i = 0; i < max_stack; ++i)
873         stack[i] = copy->stack[i];
874       for (int i = 0; i < max_locals; ++i)
875         {
876           // See push_jump_merge to understand this case.
877           if (ret_semantics)
878             locals[i] = type (copy->local_changed[i]
879                               ? unsuitable_type
880                               : unused_by_subroutine_type);
881           else
882             locals[i] = copy->locals[i];
883           local_changed[i] = copy->local_changed[i];
884         }
885
886       clean_subrs ();
887       if (copy->seen_subrs)
888         {
889           for (subr_info *info = seen_subrs; info != NULL; info = info->next)
890             add_subr (info->pc);
891         }
892       else
893         seen_subrs = NULL;
894
895       this_type = copy->this_type;
896       // Don't modify `next'.
897     }
898
899     // Modify this state to reflect entry to an exception handler.
900     void set_exception (type t, int max_stack)
901     {
902       stackdepth = 1;
903       stacktop = 1;
904       stack[0] = t;
905       for (int i = stacktop; i < max_stack; ++i)
906         stack[i] = unsuitable_type;
907     }
908
909     // Modify this state to reflect entry into a subroutine.
910     void enter_subroutine (int npc, int max_locals)
911     {
912       subroutine = npc;
913       // Mark all items as unchanged.  Each subroutine needs to keep
914       // track of its `changed' state independently.  In the case of
915       // nested subroutines, this information will be merged back into
916       // parent by the `ret'.
917       for (int i = 0; i < max_locals; ++i)
918         local_changed[i] = false;
919     }
920
921     // Indicate that we've been in this this subroutine.
922     void add_subr (int pc)
923     {
924       subr_info *n = (subr_info *) _Jv_Malloc (sizeof (subr_info));
925       n->pc = pc;
926       n->next = seen_subrs;
927       seen_subrs = n;
928     }
929
930     // Merge STATE_OLD into this state.  Destructively modifies this
931     // state.  Returns true if the new state was in fact changed.
932     // Will throw an exception if the states are not mergeable.
933     bool merge (state *state_old, bool ret_semantics,
934                 int max_locals, _Jv_BytecodeVerifier *verifier)
935     {
936       bool changed = false;
937
938       // Special handling for `this'.  If one or the other is
939       // uninitialized, then the merge is uninitialized.
940       if (this_type.isinitialized ())
941         this_type = state_old->this_type;
942
943       // Merge subroutine states.  Here we just keep track of what
944       // subroutine we think we're in.  We only check for a merge
945       // (which is invalid) when we see a `ret'.
946       if (subroutine == state_old->subroutine)
947         {
948           // Nothing.
949         }
950       else if (subroutine == 0)
951         {
952           subroutine = state_old->subroutine;
953           changed = true;
954         }
955       else
956         {
957           // If the subroutines differ, and we haven't seen this
958           // subroutine before, indicate that the state changed.  This
959           // is needed to detect when subroutines have merged.
960           bool found = false;
961           for (subr_info *info = seen_subrs; info != NULL; info = info->next)
962             {
963               if (info->pc == state_old->subroutine)
964                 {
965                   found = true;
966                   break;
967                 }
968             }
969           if (! found)
970             {
971               add_subr (state_old->subroutine);
972               changed = true;
973             }
974         }
975
976       // Merge stacks.  Special handling for NO_STACK case.
977       if (state_old->stacktop == NO_STACK)
978         {
979           // Nothing to do in this case; we don't care about modifying
980           // the old state.
981         }
982       else if (stacktop == NO_STACK)
983         {
984           stacktop = state_old->stacktop;
985           stackdepth = state_old->stackdepth;
986           for (int i = 0; i < stacktop; ++i)
987             stack[i] = state_old->stack[i];
988           changed = true;
989         }
990       else if (state_old->stacktop != stacktop)
991         verifier->verify_fail ("stack sizes differ");
992       else
993         {
994           for (int i = 0; i < state_old->stacktop; ++i)
995             {
996               if (stack[i].merge (state_old->stack[i], false, verifier))
997                 changed = true;
998             }
999         }
1000
1001       // Merge local variables.
1002       for (int i = 0; i < max_locals; ++i)
1003         {
1004           // If we're not processing a `ret', then we merge every
1005           // local variable.  If we are processing a `ret', then we
1006           // only merge locals which changed in the subroutine.  When
1007           // processing a `ret', STATE_OLD is the state at the point
1008           // of the `ret', and THIS is the state just after the `jsr'.
1009           if (! ret_semantics || state_old->local_changed[i])
1010             {
1011               if (locals[i].merge (state_old->locals[i], true, verifier))
1012                 {
1013                   // Note that we don't call `note_variable' here.
1014                   // This change doesn't represent a real change to a
1015                   // local, but rather a merge artifact.  If we're in
1016                   // a subroutine which is called with two
1017                   // incompatible types in a slot that is unused by
1018                   // the subroutine, then we don't want to mark that
1019                   // variable as having been modified.
1020                   changed = true;
1021                 }
1022             }
1023
1024           // If we're in a subroutine, we must compute the union of
1025           // all the changed local variables.
1026           if (state_old->local_changed[i])
1027             note_variable (i);
1028         }
1029
1030       return changed;
1031     }
1032
1033     // Throw an exception if there is an uninitialized object on the
1034     // stack or in a local variable.  EXCEPTION_SEMANTICS controls
1035     // whether we're using backwards-branch or exception-handing
1036     // semantics.
1037     void check_no_uninitialized_objects (int max_locals,
1038                                          _Jv_BytecodeVerifier *verifier,
1039                                          bool exception_semantics = false)
1040     {
1041       if (! exception_semantics)
1042         {
1043           for (int i = 0; i < stacktop; ++i)
1044             if (stack[i].isreference () && ! stack[i].isinitialized ())
1045               verifier->verify_fail ("uninitialized object on stack");
1046         }
1047
1048       for (int i = 0; i < max_locals; ++i)
1049         if (locals[i].isreference () && ! locals[i].isinitialized ())
1050           verifier->verify_fail ("uninitialized object in local variable");
1051
1052       check_this_initialized (verifier);
1053     }
1054
1055     // Ensure that `this' has been initialized.
1056     void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1057     {
1058       if (this_type.isreference () && ! this_type.isinitialized ())
1059         verifier->verify_fail ("`this' is uninitialized");
1060     }
1061
1062     // Set type of `this'.
1063     void set_this_type (const type &k)
1064     {
1065       this_type = k;
1066     }
1067
1068     // Note that a local variable was modified.
1069     void note_variable (int index)
1070     {
1071       if (subroutine > 0)
1072         local_changed[index] = true;
1073     }
1074
1075     // Mark each `new'd object we know of that was allocated at PC as
1076     // initialized.
1077     void set_initialized (int pc, int max_locals)
1078     {
1079       for (int i = 0; i < stacktop; ++i)
1080         stack[i].set_initialized (pc);
1081       for (int i = 0; i < max_locals; ++i)
1082         locals[i].set_initialized (pc);
1083       this_type.set_initialized (pc);
1084     }
1085
1086     // Return true if this state is the unmerged result of a `ret'.
1087     bool is_unmerged_ret_state (int max_locals) const
1088     {
1089       if (stacktop == NO_STACK)
1090         return true;
1091       for (int i = 0; i < max_locals; ++i)
1092         {
1093           if (locals[i].key == unused_by_subroutine_type)
1094             return true;
1095         }
1096       return false;
1097     }
1098
1099 #ifdef VERIFY_DEBUG
1100     void print (const char *leader, int pc,
1101                 int max_stack, int max_locals) const
1102     {
1103       debug_print ("%s [%4d]:   [stack] ", leader, pc);
1104       int i;
1105       for (i = 0; i < stacktop; ++i)
1106         stack[i].print ();
1107       for (; i < max_stack; ++i)
1108         debug_print (".");
1109       debug_print ("    [local] ");
1110       for (i = 0; i < max_locals; ++i)
1111         {
1112           locals[i].print ();
1113           debug_print (local_changed[i] ? "+" : " ");
1114         }
1115       if (subroutine == 0)
1116         debug_print ("   | None");
1117       else
1118         debug_print ("   | %4d", subroutine);
1119       debug_print (" | %p\n", this);
1120     }
1121 #else
1122     inline void print (const char *, int, int, int) const
1123     {
1124     }
1125 #endif /* VERIFY_DEBUG */
1126   };
1127
1128   type pop_raw ()
1129   {
1130     if (current_state->stacktop <= 0)
1131       verify_fail ("stack empty");
1132     type r = current_state->stack[--current_state->stacktop];
1133     current_state->stackdepth -= r.depth ();
1134     if (current_state->stackdepth < 0)
1135       verify_fail ("stack empty", start_PC);
1136     return r;
1137   }
1138
1139   type pop32 ()
1140   {
1141     type r = pop_raw ();
1142     if (r.iswide ())
1143       verify_fail ("narrow pop of wide type");
1144     return r;
1145   }
1146
1147   type pop_type (type match)
1148   {
1149     match.promote ();
1150     type t = pop_raw ();
1151     if (! match.compatible (t, this))
1152       verify_fail ("incompatible type on stack");
1153     return t;
1154   }
1155
1156   // Pop a reference which is guaranteed to be initialized.  MATCH
1157   // doesn't have to be a reference type; in this case this acts like
1158   // pop_type.
1159   type pop_init_ref (type match)
1160   {
1161     type t = pop_raw ();
1162     if (t.isreference () && ! t.isinitialized ())
1163       verify_fail ("initialized reference required");
1164     else if (! match.compatible (t, this))
1165       verify_fail ("incompatible type on stack");
1166     return t;
1167   }
1168
1169   // Pop a reference type or a return address.
1170   type pop_ref_or_return ()
1171   {
1172     type t = pop_raw ();
1173     if (! t.isreference () && t.key != return_address_type)
1174       verify_fail ("expected reference or return address on stack");
1175     return t;
1176   }
1177
1178   void push_type (type t)
1179   {
1180     // If T is a numeric type like short, promote it to int.
1181     t.promote ();
1182
1183     int depth = t.depth ();
1184     if (current_state->stackdepth + depth > current_method->max_stack)
1185       verify_fail ("stack overflow");
1186     current_state->stack[current_state->stacktop++] = t;
1187     current_state->stackdepth += depth;
1188   }
1189
1190   void set_variable (int index, type t)
1191   {
1192     // If T is a numeric type like short, promote it to int.
1193     t.promote ();
1194
1195     int depth = t.depth ();
1196     if (index > current_method->max_locals - depth)
1197       verify_fail ("invalid local variable");
1198     current_state->locals[index] = t;
1199     current_state->note_variable (index);
1200
1201     if (depth == 2)
1202       {
1203         current_state->locals[index + 1] = continuation_type;
1204         current_state->note_variable (index + 1);
1205       }
1206     if (index > 0 && current_state->locals[index - 1].iswide ())
1207       {
1208         current_state->locals[index - 1] = unsuitable_type;
1209         // There's no need to call note_variable here.
1210       }
1211   }
1212
1213   type get_variable (int index, type t)
1214   {
1215     int depth = t.depth ();
1216     if (index > current_method->max_locals - depth)
1217       verify_fail ("invalid local variable");
1218     if (! t.compatible (current_state->locals[index], this))
1219       verify_fail ("incompatible type in local variable");
1220     if (depth == 2)
1221       {
1222         type t (continuation_type);
1223         if (! current_state->locals[index + 1].compatible (t, this))
1224           verify_fail ("invalid local variable");
1225       }
1226     return current_state->locals[index];
1227   }
1228
1229   // Make sure ARRAY is an array type and that its elements are
1230   // compatible with type ELEMENT.  Returns the actual element type.
1231   type require_array_type (type array, type element)
1232   {
1233     // An odd case.  Here we just pretend that everything went ok.  If
1234     // the requested element type is some kind of reference, return
1235     // the null type instead.
1236     if (array.isnull ())
1237       return element.isreference () ? type (null_type) : element;
1238
1239     if (! array.isarray ())
1240       verify_fail ("array required");
1241
1242     type t = array.element_type (this);
1243     if (! element.compatible (t, this))
1244       {
1245         // Special case for byte arrays, which must also be boolean
1246         // arrays.
1247         bool ok = true;
1248         if (element.key == byte_type)
1249           {
1250             type e2 (boolean_type);
1251             ok = e2.compatible (t, this);
1252           }
1253         if (! ok)
1254           verify_fail ("incompatible array element type");
1255       }
1256
1257     // Return T and not ELEMENT, because T might be specialized.
1258     return t;
1259   }
1260
1261   jint get_byte ()
1262   {
1263     if (PC >= current_method->code_length)
1264       verify_fail ("premature end of bytecode");
1265     return (jint) bytecode[PC++] & 0xff;
1266   }
1267
1268   jint get_ushort ()
1269   {
1270     jint b1 = get_byte ();
1271     jint b2 = get_byte ();
1272     return (jint) ((b1 << 8) | b2) & 0xffff;
1273   }
1274
1275   jint get_short ()
1276   {
1277     jint b1 = get_byte ();
1278     jint b2 = get_byte ();
1279     jshort s = (b1 << 8) | b2;
1280     return (jint) s;
1281   }
1282
1283   jint get_int ()
1284   {
1285     jint b1 = get_byte ();
1286     jint b2 = get_byte ();
1287     jint b3 = get_byte ();
1288     jint b4 = get_byte ();
1289     return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1290   }
1291
1292   int compute_jump (int offset)
1293   {
1294     int npc = start_PC + offset;
1295     if (npc < 0 || npc >= current_method->code_length)
1296       verify_fail ("branch out of range", start_PC);
1297     return npc;
1298   }
1299
1300   // Merge the indicated state into the state at the branch target and
1301   // schedule a new PC if there is a change.  If RET_SEMANTICS is
1302   // true, then we are merging from a `ret' instruction into the
1303   // instruction after a `jsr'.  This is a special case with its own
1304   // modified semantics.
1305   void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1306   {
1307     bool changed = true;
1308     if (states[npc] == NULL)
1309       {
1310         // There's a weird situation here.  If are examining the
1311         // branch that results from a `ret', and there is not yet a
1312         // state available at the branch target (the instruction just
1313         // after the `jsr'), then we have to construct a special kind
1314         // of state at that point for future merging.  This special
1315         // state has the type `unused_by_subroutine_type' in each slot
1316         // which was not modified by the subroutine.
1317         states[npc] = new state (nstate, current_method->max_stack,
1318                                  current_method->max_locals, ret_semantics);
1319         debug_print ("== New state in push_jump_merge\n");
1320         states[npc]->print ("New", npc, current_method->max_stack,
1321                             current_method->max_locals);
1322       }
1323     else
1324       {
1325         debug_print ("== Merge states in push_jump_merge\n");
1326         nstate->print ("Frm", start_PC, current_method->max_stack,
1327                        current_method->max_locals);
1328         states[npc]->print (" To", npc, current_method->max_stack,
1329                             current_method->max_locals);
1330         changed = states[npc]->merge (nstate, ret_semantics,
1331                                       current_method->max_locals, this);
1332         states[npc]->print ("New", npc, current_method->max_stack,
1333                             current_method->max_locals);
1334       }
1335
1336     if (changed && states[npc]->next == state::INVALID)
1337       {
1338         // The merge changed the state, and the new PC isn't yet on our
1339         // list of PCs to re-verify.
1340         states[npc]->next = next_verify_pc;
1341         next_verify_pc = npc;
1342       }
1343   }
1344
1345   void push_jump (int offset)
1346   {
1347     int npc = compute_jump (offset);
1348     if (npc < PC)
1349       current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1350     push_jump_merge (npc, current_state);
1351   }
1352
1353   void push_exception_jump (type t, int pc)
1354   {
1355     current_state->check_no_uninitialized_objects (current_method->max_locals,
1356                                                    this, true);
1357     state s (current_state, current_method->max_stack,
1358              current_method->max_locals);
1359     if (current_method->max_stack < 1)
1360       verify_fail ("stack overflow at exception handler");
1361     s.set_exception (t, current_method->max_stack);
1362     push_jump_merge (pc, &s);
1363   }
1364
1365   int pop_jump ()
1366   {
1367     int *prev_loc = &next_verify_pc;
1368     int npc = next_verify_pc;
1369
1370     while (npc != state::NO_NEXT)
1371       {
1372         // If the next available PC is an unmerged `ret' state, then
1373         // we aren't yet ready to handle it.  That's because we would
1374         // need all kind of special cases to do so.  So instead we
1375         // defer this jump until after we've processed it via a
1376         // fall-through.  This has to happen because the instruction
1377         // before this one must be a `jsr'.
1378         if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1379           {
1380             *prev_loc = states[npc]->next;
1381             states[npc]->next = state::INVALID;
1382             return npc;
1383           }
1384
1385         prev_loc = &states[npc]->next;
1386         npc = states[npc]->next;
1387       }
1388
1389     // Note that we might have gotten here even when there are
1390     // remaining states to process.  That can happen if we find a
1391     // `jsr' without a `ret'.
1392     return state::NO_NEXT;
1393   }
1394
1395   void invalidate_pc ()
1396   {
1397     PC = state::NO_NEXT;
1398   }
1399
1400   void note_branch_target (int pc, bool is_jsr_target = false)
1401   {
1402     // Don't check `pc <= PC', because we've advanced PC after
1403     // fetching the target and we haven't yet checked the next
1404     // instruction.
1405     if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1406       verify_fail ("branch not to instruction start", start_PC);
1407     flags[pc] |= FLAG_BRANCH_TARGET;
1408     if (is_jsr_target)
1409       {
1410         // Record the jsr which called this instruction.
1411         subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1412         info->pc = PC;
1413         info->next = jsr_ptrs[pc];
1414         jsr_ptrs[pc] = info;
1415       }
1416   }
1417
1418   void skip_padding ()
1419   {
1420     while ((PC % 4) > 0)
1421       if (get_byte () != 0)
1422         verify_fail ("found nonzero padding byte");
1423   }
1424
1425   // Return the subroutine to which the instruction at PC belongs.
1426   int get_subroutine (int pc)
1427   {
1428     if (states[pc] == NULL)
1429       return 0;
1430     return states[pc]->subroutine;
1431   }
1432
1433   // Do the work for a `ret' instruction.  INDEX is the index into the
1434   // local variables.
1435   void handle_ret_insn (int index)
1436   {
1437     get_variable (index, return_address_type);
1438
1439     int csub = current_state->subroutine;
1440     if (csub == 0)
1441       verify_fail ("no subroutine");
1442
1443     // Check to see if we've merged subroutines.
1444     subr_entry_info *entry;
1445     for (entry = entry_points; entry != NULL; entry = entry->next)
1446       {
1447         if (entry->ret_pc == start_PC)
1448           break;
1449       }
1450     if (entry == NULL)
1451       {
1452         entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1453         entry->pc = csub;
1454         entry->ret_pc = start_PC;
1455         entry->next = entry_points;
1456         entry_points = entry;
1457       }
1458     else if (entry->pc != csub)
1459       verify_fail ("subroutines merged");
1460
1461     for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1462       {
1463         // We might be returning to a `jsr' that is at the end of the
1464         // bytecode.  This is ok if we never return from the called
1465         // subroutine, but if we see this here it is an error.
1466         if (subr->pc >= current_method->code_length)
1467           verify_fail ("fell off end");
1468
1469         // Temporarily modify the current state so it looks like we're
1470         // in the enclosing context.
1471         current_state->subroutine = get_subroutine (subr->pc);
1472         if (subr->pc < PC)
1473           current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1474         push_jump_merge (subr->pc, current_state, true);
1475       }
1476
1477     current_state->subroutine = csub;
1478     invalidate_pc ();
1479   }
1480
1481   // We're in the subroutine SUB, calling a subroutine at DEST.  Make
1482   // sure this subroutine isn't already on the stack.
1483   void check_nonrecursive_call (int sub, int dest)
1484   {
1485     if (sub == 0)
1486       return;
1487     if (sub == dest)
1488       verify_fail ("recursive subroutine call");
1489     for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1490       check_nonrecursive_call (get_subroutine (info->pc), dest);
1491   }
1492
1493   void handle_jsr_insn (int offset)
1494   {
1495     int npc = compute_jump (offset);
1496
1497     if (npc < PC)
1498       current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1499     check_nonrecursive_call (current_state->subroutine, npc);
1500
1501     // Modify our state as appropriate for entry into a subroutine.
1502     push_type (return_address_type);
1503     push_jump_merge (npc, current_state);
1504     // Clean up.
1505     pop_type (return_address_type);
1506
1507     // On entry to the subroutine, the subroutine number must be set
1508     // and the locals must be marked as cleared.  We do this after
1509     // merging state so that we don't erroneously "notice" a variable
1510     // change merely on entry.
1511     states[npc]->enter_subroutine (npc, current_method->max_locals);
1512
1513     // Indicate that we don't know the stack depth of the instruction
1514     // following the `jsr'.  The idea here is that we need to merge
1515     // the local variable state across the jsr, but the subroutine
1516     // might change the stack depth, so we can't make any assumptions
1517     // about it.  So we have yet another special case.  We know that
1518     // at this point PC points to the instruction after the jsr.  Note
1519     // that it is ok to have a `jsr' at the end of the bytecode,
1520     // provided that the called subroutine never returns.  So, we have
1521     // a special case here and another one when we handle the ret.
1522     if (PC < current_method->code_length)
1523       {
1524         current_state->stacktop = state::NO_STACK;
1525         push_jump_merge (PC, current_state);
1526       }
1527     invalidate_pc ();
1528   }
1529
1530   jclass construct_primitive_array_type (type_val prim)
1531   {
1532     jclass k = NULL;
1533     switch (prim)
1534       {
1535       case boolean_type:
1536         k = JvPrimClass (boolean);
1537         break;
1538       case char_type:
1539         k = JvPrimClass (char);
1540         break;
1541       case float_type:
1542         k = JvPrimClass (float);
1543         break;
1544       case double_type:
1545         k = JvPrimClass (double);
1546         break;
1547       case byte_type:
1548         k = JvPrimClass (byte);
1549         break;
1550       case short_type:
1551         k = JvPrimClass (short);
1552         break;
1553       case int_type:
1554         k = JvPrimClass (int);
1555         break;
1556       case long_type:
1557         k = JvPrimClass (long);
1558         break;
1559
1560       // These aren't used here but we call them out to avoid
1561       // warnings.
1562       case void_type:
1563       case unsuitable_type:
1564       case return_address_type:
1565       case continuation_type:
1566       case unused_by_subroutine_type:
1567       case reference_type:
1568       case null_type:
1569       case unresolved_reference_type:
1570       case uninitialized_reference_type:
1571       case uninitialized_unresolved_reference_type:
1572       default:
1573         verify_fail ("unknown type in construct_primitive_array_type");
1574       }
1575     k = _Jv_GetArrayClass (k, NULL);
1576     return k;
1577   }
1578
1579   // This pass computes the location of branch targets and also
1580   // instruction starts.
1581   void branch_prepass ()
1582   {
1583     flags = (char *) _Jv_Malloc (current_method->code_length);
1584     jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1585                                           * current_method->code_length);
1586
1587     for (int i = 0; i < current_method->code_length; ++i)
1588       {
1589         flags[i] = 0;
1590         jsr_ptrs[i] = NULL;
1591       }
1592
1593     bool last_was_jsr = false;
1594
1595     PC = 0;
1596     while (PC < current_method->code_length)
1597       {
1598         // Set `start_PC' early so that error checking can have the
1599         // correct value.
1600         start_PC = PC;
1601         flags[PC] |= FLAG_INSN_START;
1602
1603         // If the previous instruction was a jsr, then the next
1604         // instruction is a branch target -- the branch being the
1605         // corresponding `ret'.
1606         if (last_was_jsr)
1607           note_branch_target (PC);
1608         last_was_jsr = false;
1609
1610         java_opcode opcode = (java_opcode) bytecode[PC++];
1611         switch (opcode)
1612           {
1613           case op_nop:
1614           case op_aconst_null:
1615           case op_iconst_m1:
1616           case op_iconst_0:
1617           case op_iconst_1:
1618           case op_iconst_2:
1619           case op_iconst_3:
1620           case op_iconst_4:
1621           case op_iconst_5:
1622           case op_lconst_0:
1623           case op_lconst_1:
1624           case op_fconst_0:
1625           case op_fconst_1:
1626           case op_fconst_2:
1627           case op_dconst_0:
1628           case op_dconst_1:
1629           case op_iload_0:
1630           case op_iload_1:
1631           case op_iload_2:
1632           case op_iload_3:
1633           case op_lload_0:
1634           case op_lload_1:
1635           case op_lload_2:
1636           case op_lload_3:
1637           case op_fload_0:
1638           case op_fload_1:
1639           case op_fload_2:
1640           case op_fload_3:
1641           case op_dload_0:
1642           case op_dload_1:
1643           case op_dload_2:
1644           case op_dload_3:
1645           case op_aload_0:
1646           case op_aload_1:
1647           case op_aload_2:
1648           case op_aload_3:
1649           case op_iaload:
1650           case op_laload:
1651           case op_faload:
1652           case op_daload:
1653           case op_aaload:
1654           case op_baload:
1655           case op_caload:
1656           case op_saload:
1657           case op_istore_0:
1658           case op_istore_1:
1659           case op_istore_2:
1660           case op_istore_3:
1661           case op_lstore_0:
1662           case op_lstore_1:
1663           case op_lstore_2:
1664           case op_lstore_3:
1665           case op_fstore_0:
1666           case op_fstore_1:
1667           case op_fstore_2:
1668           case op_fstore_3:
1669           case op_dstore_0:
1670           case op_dstore_1:
1671           case op_dstore_2:
1672           case op_dstore_3:
1673           case op_astore_0:
1674           case op_astore_1:
1675           case op_astore_2:
1676           case op_astore_3:
1677           case op_iastore:
1678           case op_lastore:
1679           case op_fastore:
1680           case op_dastore:
1681           case op_aastore:
1682           case op_bastore:
1683           case op_castore:
1684           case op_sastore:
1685           case op_pop:
1686           case op_pop2:
1687           case op_dup:
1688           case op_dup_x1:
1689           case op_dup_x2:
1690           case op_dup2:
1691           case op_dup2_x1:
1692           case op_dup2_x2:
1693           case op_swap:
1694           case op_iadd:
1695           case op_isub:
1696           case op_imul:
1697           case op_idiv:
1698           case op_irem:
1699           case op_ishl:
1700           case op_ishr:
1701           case op_iushr:
1702           case op_iand:
1703           case op_ior:
1704           case op_ixor:
1705           case op_ladd:
1706           case op_lsub:
1707           case op_lmul:
1708           case op_ldiv:
1709           case op_lrem:
1710           case op_lshl:
1711           case op_lshr:
1712           case op_lushr:
1713           case op_land:
1714           case op_lor:
1715           case op_lxor:
1716           case op_fadd:
1717           case op_fsub:
1718           case op_fmul:
1719           case op_fdiv:
1720           case op_frem:
1721           case op_dadd:
1722           case op_dsub:
1723           case op_dmul:
1724           case op_ddiv:
1725           case op_drem:
1726           case op_ineg:
1727           case op_i2b:
1728           case op_i2c:
1729           case op_i2s:
1730           case op_lneg:
1731           case op_fneg:
1732           case op_dneg:
1733           case op_i2l:
1734           case op_i2f:
1735           case op_i2d:
1736           case op_l2i:
1737           case op_l2f:
1738           case op_l2d:
1739           case op_f2i:
1740           case op_f2l:
1741           case op_f2d:
1742           case op_d2i:
1743           case op_d2l:
1744           case op_d2f:
1745           case op_lcmp:
1746           case op_fcmpl:
1747           case op_fcmpg:
1748           case op_dcmpl:
1749           case op_dcmpg:
1750           case op_monitorenter:
1751           case op_monitorexit:
1752           case op_ireturn:
1753           case op_lreturn:
1754           case op_freturn:
1755           case op_dreturn:
1756           case op_areturn:
1757           case op_return:
1758           case op_athrow:
1759           case op_arraylength:
1760             break;
1761
1762           case op_bipush:
1763           case op_ldc:
1764           case op_iload:
1765           case op_lload:
1766           case op_fload:
1767           case op_dload:
1768           case op_aload:
1769           case op_istore:
1770           case op_lstore:
1771           case op_fstore:
1772           case op_dstore:
1773           case op_astore:
1774           case op_ret:
1775           case op_newarray:
1776             get_byte ();
1777             break;
1778
1779           case op_iinc:
1780           case op_sipush:
1781           case op_ldc_w:
1782           case op_ldc2_w:
1783           case op_getstatic:
1784           case op_getfield:
1785           case op_putfield:
1786           case op_putstatic:
1787           case op_new:
1788           case op_anewarray:
1789           case op_instanceof:
1790           case op_checkcast:
1791           case op_invokespecial:
1792           case op_invokestatic:
1793           case op_invokevirtual:
1794             get_short ();
1795             break;
1796
1797           case op_multianewarray:
1798             get_short ();
1799             get_byte ();
1800             break;
1801
1802           case op_jsr:
1803             last_was_jsr = true;
1804             // Fall through.
1805           case op_ifeq:
1806           case op_ifne:
1807           case op_iflt:
1808           case op_ifge:
1809           case op_ifgt:
1810           case op_ifle:
1811           case op_if_icmpeq:
1812           case op_if_icmpne:
1813           case op_if_icmplt:
1814           case op_if_icmpge:
1815           case op_if_icmpgt:
1816           case op_if_icmple:
1817           case op_if_acmpeq:
1818           case op_if_acmpne:
1819           case op_ifnull:
1820           case op_ifnonnull:
1821           case op_goto:
1822             note_branch_target (compute_jump (get_short ()), last_was_jsr);
1823             break;
1824
1825           case op_tableswitch:
1826             {
1827               skip_padding ();
1828               note_branch_target (compute_jump (get_int ()));
1829               jint low = get_int ();
1830               jint hi = get_int ();
1831               if (low > hi)
1832                 verify_fail ("invalid tableswitch", start_PC);
1833               for (int i = low; i <= hi; ++i)
1834                 note_branch_target (compute_jump (get_int ()));
1835             }
1836             break;
1837
1838           case op_lookupswitch:
1839             {
1840               skip_padding ();
1841               note_branch_target (compute_jump (get_int ()));
1842               int npairs = get_int ();
1843               if (npairs < 0)
1844                 verify_fail ("too few pairs in lookupswitch", start_PC);
1845               while (npairs-- > 0)
1846                 {
1847                   get_int ();
1848                   note_branch_target (compute_jump (get_int ()));
1849                 }
1850             }
1851             break;
1852
1853           case op_invokeinterface:
1854             get_short ();
1855             get_byte ();
1856             get_byte ();
1857             break;
1858
1859           case op_wide:
1860             {
1861               opcode = (java_opcode) get_byte ();
1862               get_short ();
1863               if (opcode == op_iinc)
1864                 get_short ();
1865             }
1866             break;
1867
1868           case op_jsr_w:
1869             last_was_jsr = true;
1870             // Fall through.
1871           case op_goto_w:
1872             note_branch_target (compute_jump (get_int ()), last_was_jsr);
1873             break;
1874
1875           // These are unused here, but we call them out explicitly
1876           // so that -Wswitch-enum doesn't complain.
1877           case op_putfield_1:
1878           case op_putfield_2:
1879           case op_putfield_4:
1880           case op_putfield_8:
1881           case op_putfield_a:
1882           case op_putstatic_1:
1883           case op_putstatic_2:
1884           case op_putstatic_4:
1885           case op_putstatic_8:
1886           case op_putstatic_a:
1887           case op_getfield_1:
1888           case op_getfield_2s:
1889           case op_getfield_2u:
1890           case op_getfield_4:
1891           case op_getfield_8:
1892           case op_getfield_a:
1893           case op_getstatic_1:
1894           case op_getstatic_2s:
1895           case op_getstatic_2u:
1896           case op_getstatic_4:
1897           case op_getstatic_8:
1898           case op_getstatic_a:
1899           default:
1900             verify_fail ("unrecognized instruction in branch_prepass",
1901                          start_PC);
1902           }
1903
1904         // See if any previous branch tried to branch to the middle of
1905         // this instruction.
1906         for (int pc = start_PC + 1; pc < PC; ++pc)
1907           {
1908             if ((flags[pc] & FLAG_BRANCH_TARGET))
1909               verify_fail ("branch to middle of instruction", pc);
1910           }
1911       }
1912
1913     // Verify exception handlers.
1914     for (int i = 0; i < current_method->exc_count; ++i)
1915       {
1916         if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1917           verify_fail ("exception handler not at instruction start",
1918                        exception[i].handler_pc.i);
1919         if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1920           verify_fail ("exception start not at instruction start",
1921                        exception[i].start_pc.i);
1922         if (exception[i].end_pc.i != current_method->code_length
1923             && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1924           verify_fail ("exception end not at instruction start",
1925                        exception[i].end_pc.i);
1926
1927         flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1928       }
1929   }
1930
1931   void check_pool_index (int index)
1932   {
1933     if (index < 0 || index >= current_class->constants.size)
1934       verify_fail ("constant pool index out of range", start_PC);
1935   }
1936
1937   type check_class_constant (int index)
1938   {
1939     check_pool_index (index);
1940     _Jv_Constants *pool = &current_class->constants;
1941     if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1942       return type (pool->data[index].clazz);
1943     else if (pool->tags[index] == JV_CONSTANT_Class)
1944       return type (pool->data[index].utf8);
1945     verify_fail ("expected class constant", start_PC);
1946   }
1947
1948   type check_constant (int index)
1949   {
1950     check_pool_index (index);
1951     _Jv_Constants *pool = &current_class->constants;
1952     if (pool->tags[index] == JV_CONSTANT_ResolvedString
1953         || pool->tags[index] == JV_CONSTANT_String)
1954       return type (&java::lang::String::class$);
1955     else if (pool->tags[index] == JV_CONSTANT_Integer)
1956       return type (int_type);
1957     else if (pool->tags[index] == JV_CONSTANT_Float)
1958       return type (float_type);
1959     verify_fail ("String, int, or float constant expected", start_PC);
1960   }
1961
1962   type check_wide_constant (int index)
1963   {
1964     check_pool_index (index);
1965     _Jv_Constants *pool = &current_class->constants;
1966     if (pool->tags[index] == JV_CONSTANT_Long)
1967       return type (long_type);
1968     else if (pool->tags[index] == JV_CONSTANT_Double)
1969       return type (double_type);
1970     verify_fail ("long or double constant expected", start_PC);
1971   }
1972
1973   // Helper for both field and method.  These are laid out the same in
1974   // the constant pool.
1975   type handle_field_or_method (int index, int expected,
1976                                _Jv_Utf8Const **name,
1977                                _Jv_Utf8Const **fmtype)
1978   {
1979     check_pool_index (index);
1980     _Jv_Constants *pool = &current_class->constants;
1981     if (pool->tags[index] != expected)
1982       verify_fail ("didn't see expected constant", start_PC);
1983     // Once we know we have a Fieldref or Methodref we assume that it
1984     // is correctly laid out in the constant pool.  I think the code
1985     // in defineclass.cc guarantees this.
1986     _Jv_ushort class_index, name_and_type_index;
1987     _Jv_loadIndexes (&pool->data[index],
1988                      class_index,
1989                      name_and_type_index);
1990     _Jv_ushort name_index, desc_index;
1991     _Jv_loadIndexes (&pool->data[name_and_type_index],
1992                      name_index, desc_index);
1993
1994     *name = pool->data[name_index].utf8;
1995     *fmtype = pool->data[desc_index].utf8;
1996
1997     return check_class_constant (class_index);
1998   }
1999
2000   // Return field's type, compute class' type if requested.
2001   type check_field_constant (int index, type *class_type = NULL)
2002   {
2003     _Jv_Utf8Const *name, *field_type;
2004     type ct = handle_field_or_method (index,
2005                                       JV_CONSTANT_Fieldref,
2006                                       &name, &field_type);
2007     if (class_type)
2008       *class_type = ct;
2009     if (field_type->data[0] == '[' || field_type->data[0] == 'L')
2010       return type (field_type);
2011     return get_type_val_for_signature (field_type->data[0]);
2012   }
2013
2014   type check_method_constant (int index, bool is_interface,
2015                               _Jv_Utf8Const **method_name,
2016                               _Jv_Utf8Const **method_signature)
2017   {
2018     return handle_field_or_method (index,
2019                                    (is_interface
2020                                     ? JV_CONSTANT_InterfaceMethodref
2021                                     : JV_CONSTANT_Methodref),
2022                                    method_name, method_signature);
2023   }
2024
2025   type get_one_type (char *&p)
2026   {
2027     char *start = p;
2028
2029     int arraycount = 0;
2030     while (*p == '[')
2031       {
2032         ++arraycount;
2033         ++p;
2034       }
2035
2036     char v = *p++;
2037
2038     if (v == 'L')
2039       {
2040         while (*p != ';')
2041           ++p;
2042         ++p;
2043         _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2044         return type (name);
2045       }
2046
2047     // Casting to jchar here is ok since we are looking at an ASCII
2048     // character.
2049     type_val rt = get_type_val_for_signature (jchar (v));
2050
2051     if (arraycount == 0)
2052       {
2053         // Callers of this function eventually push their arguments on
2054         // the stack.  So, promote them here.
2055         return type (rt).promote ();
2056       }
2057
2058     jclass k = construct_primitive_array_type (rt);
2059     while (--arraycount > 0)
2060       k = _Jv_GetArrayClass (k, NULL);
2061     return type (k);
2062   }
2063
2064   void compute_argument_types (_Jv_Utf8Const *signature,
2065                                type *types)
2066   {
2067     char *p = signature->data;
2068     // Skip `('.
2069     ++p;
2070
2071     int i = 0;
2072     while (*p != ')')
2073       types[i++] = get_one_type (p);
2074   }
2075
2076   type compute_return_type (_Jv_Utf8Const *signature)
2077   {
2078     char *p = signature->data;
2079     while (*p != ')')
2080       ++p;
2081     ++p;
2082     return get_one_type (p);
2083   }
2084
2085   void check_return_type (type onstack)
2086   {
2087     type rt = compute_return_type (current_method->self->signature);
2088     if (! rt.compatible (onstack, this))
2089       verify_fail ("incompatible return type");
2090   }
2091
2092   // Initialize the stack for the new method.  Returns true if this
2093   // method is an instance initializer.
2094   bool initialize_stack ()
2095   {
2096     int var = 0;
2097     bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
2098                                         gcj::init_name);
2099     bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
2100                                           gcj::clinit_name);
2101
2102     using namespace java::lang::reflect;
2103     if (! Modifier::isStatic (current_method->self->accflags))
2104       {
2105         type kurr (current_class);
2106         if (is_init)
2107           {
2108             kurr.set_uninitialized (type::SELF, this);
2109             is_init = true;
2110           }
2111         else if (is_clinit)
2112           verify_fail ("<clinit> method must be static");
2113         set_variable (0, kurr);
2114         current_state->set_this_type (kurr);
2115         ++var;
2116       }
2117     else
2118       {
2119         if (is_init)
2120           verify_fail ("<init> method must be non-static");
2121       }
2122
2123     // We have to handle wide arguments specially here.
2124     int arg_count = _Jv_count_arguments (current_method->self->signature);
2125     type arg_types[arg_count];
2126     compute_argument_types (current_method->self->signature, arg_types);
2127     for (int i = 0; i < arg_count; ++i)
2128       {
2129         set_variable (var, arg_types[i]);
2130         ++var;
2131         if (arg_types[i].iswide ())
2132           ++var;
2133       }
2134
2135     return is_init;
2136   }
2137
2138   void verify_instructions_0 ()
2139   {
2140     current_state = new state (current_method->max_stack,
2141                                current_method->max_locals);
2142
2143     PC = 0;
2144     start_PC = 0;
2145
2146     // True if we are verifying an instance initializer.
2147     bool this_is_init = initialize_stack ();
2148
2149     states = (state **) _Jv_Malloc (sizeof (state *)
2150                                     * current_method->code_length);
2151     for (int i = 0; i < current_method->code_length; ++i)
2152       states[i] = NULL;
2153
2154     next_verify_pc = state::NO_NEXT;
2155
2156     while (true)
2157       {
2158         // If the PC was invalidated, get a new one from the work list.
2159         if (PC == state::NO_NEXT)
2160           {
2161             PC = pop_jump ();
2162             if (PC == state::INVALID)
2163               verify_fail ("can't happen: saw state::INVALID");
2164             if (PC == state::NO_NEXT)
2165               break;
2166             debug_print ("== State pop from pending list\n");
2167             // Set up the current state.
2168             current_state->copy (states[PC], current_method->max_stack,
2169                                  current_method->max_locals);
2170           }
2171         else
2172           {
2173             // Control can't fall off the end of the bytecode.  We
2174             // only need to check this in the fall-through case,
2175             // because branch bounds are checked when they are
2176             // pushed.
2177             if (PC >= current_method->code_length)
2178               verify_fail ("fell off end");
2179
2180             // We only have to do this checking in the situation where
2181             // control flow falls through from the previous
2182             // instruction.  Otherwise merging is done at the time we
2183             // push the branch.
2184             if (states[PC] != NULL)
2185               {
2186                 // We've already visited this instruction.  So merge
2187                 // the states together.  If this yields no change then
2188                 // we don't have to re-verify.  However, if the new
2189                 // state is an the result of an unmerged `ret', we
2190                 // must continue through it.
2191                 debug_print ("== Fall through merge\n");
2192                 states[PC]->print ("Old", PC, current_method->max_stack,
2193                                    current_method->max_locals);
2194                 current_state->print ("Cur", PC, current_method->max_stack,
2195                                       current_method->max_locals);
2196                 if (! current_state->merge (states[PC], false,
2197                                             current_method->max_locals, this)
2198                     && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
2199                   {
2200                     debug_print ("== Fall through optimization\n");
2201                     invalidate_pc ();
2202                     continue;
2203                   }
2204                 // Save a copy of it for later.
2205                 states[PC]->copy (current_state, current_method->max_stack,
2206                                   current_method->max_locals);
2207                 current_state->print ("New", PC, current_method->max_stack,
2208                                       current_method->max_locals);
2209               }
2210           }
2211
2212         // We only have to keep saved state at branch targets.  If
2213         // we're at a branch target and the state here hasn't been set
2214         // yet, we set it now.
2215         if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2216           {
2217             states[PC] = new state (current_state, current_method->max_stack,
2218                                     current_method->max_locals);
2219           }
2220
2221         // Set this before handling exceptions so that debug output is
2222         // sane.
2223         start_PC = PC;
2224
2225         // Update states for all active exception handlers.  Ordinarily
2226         // there are not many exception handlers.  So we simply run
2227         // through them all.
2228         for (int i = 0; i < current_method->exc_count; ++i)
2229           {
2230             if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2231               {
2232                 type handler (&java::lang::Throwable::class$);
2233                 if (exception[i].handler_type.i != 0)
2234                   handler = check_class_constant (exception[i].handler_type.i);
2235                 push_exception_jump (handler, exception[i].handler_pc.i);
2236               }
2237           }
2238
2239         current_state->print ("   ", PC, current_method->max_stack,
2240                               current_method->max_locals);
2241         java_opcode opcode = (java_opcode) bytecode[PC++];
2242         switch (opcode)
2243           {
2244           case op_nop:
2245             break;
2246
2247           case op_aconst_null:
2248             push_type (null_type);
2249             break;
2250
2251           case op_iconst_m1:
2252           case op_iconst_0:
2253           case op_iconst_1:
2254           case op_iconst_2:
2255           case op_iconst_3:
2256           case op_iconst_4:
2257           case op_iconst_5:
2258             push_type (int_type);
2259             break;
2260
2261           case op_lconst_0:
2262           case op_lconst_1:
2263             push_type (long_type);
2264             break;
2265
2266           case op_fconst_0:
2267           case op_fconst_1:
2268           case op_fconst_2:
2269             push_type (float_type);
2270             break;
2271
2272           case op_dconst_0:
2273           case op_dconst_1:
2274             push_type (double_type);
2275             break;
2276
2277           case op_bipush:
2278             get_byte ();
2279             push_type (int_type);
2280             break;
2281
2282           case op_sipush:
2283             get_short ();
2284             push_type (int_type);
2285             break;
2286
2287           case op_ldc:
2288             push_type (check_constant (get_byte ()));
2289             break;
2290           case op_ldc_w:
2291             push_type (check_constant (get_ushort ()));
2292             break;
2293           case op_ldc2_w:
2294             push_type (check_wide_constant (get_ushort ()));
2295             break;
2296
2297           case op_iload:
2298             push_type (get_variable (get_byte (), int_type));
2299             break;
2300           case op_lload:
2301             push_type (get_variable (get_byte (), long_type));
2302             break;
2303           case op_fload:
2304             push_type (get_variable (get_byte (), float_type));
2305             break;
2306           case op_dload:
2307             push_type (get_variable (get_byte (), double_type));
2308             break;
2309           case op_aload:
2310             push_type (get_variable (get_byte (), reference_type));
2311             break;
2312
2313           case op_iload_0:
2314           case op_iload_1:
2315           case op_iload_2:
2316           case op_iload_3:
2317             push_type (get_variable (opcode - op_iload_0, int_type));
2318             break;
2319           case op_lload_0:
2320           case op_lload_1:
2321           case op_lload_2:
2322           case op_lload_3:
2323             push_type (get_variable (opcode - op_lload_0, long_type));
2324             break;
2325           case op_fload_0:
2326           case op_fload_1:
2327           case op_fload_2:
2328           case op_fload_3:
2329             push_type (get_variable (opcode - op_fload_0, float_type));
2330             break;
2331           case op_dload_0:
2332           case op_dload_1:
2333           case op_dload_2:
2334           case op_dload_3:
2335             push_type (get_variable (opcode - op_dload_0, double_type));
2336             break;
2337           case op_aload_0:
2338           case op_aload_1:
2339           case op_aload_2:
2340           case op_aload_3:
2341             push_type (get_variable (opcode - op_aload_0, reference_type));
2342             break;
2343           case op_iaload:
2344             pop_type (int_type);
2345             push_type (require_array_type (pop_init_ref (reference_type),
2346                                            int_type));
2347             break;
2348           case op_laload:
2349             pop_type (int_type);
2350             push_type (require_array_type (pop_init_ref (reference_type),
2351                                            long_type));
2352             break;
2353           case op_faload:
2354             pop_type (int_type);
2355             push_type (require_array_type (pop_init_ref (reference_type),
2356                                            float_type));
2357             break;
2358           case op_daload:
2359             pop_type (int_type);
2360             push_type (require_array_type (pop_init_ref (reference_type),
2361                                            double_type));
2362             break;
2363           case op_aaload:
2364             pop_type (int_type);
2365             push_type (require_array_type (pop_init_ref (reference_type),
2366                                            reference_type));
2367             break;
2368           case op_baload:
2369             pop_type (int_type);
2370             require_array_type (pop_init_ref (reference_type), byte_type);
2371             push_type (int_type);
2372             break;
2373           case op_caload:
2374             pop_type (int_type);
2375             require_array_type (pop_init_ref (reference_type), char_type);
2376             push_type (int_type);
2377             break;
2378           case op_saload:
2379             pop_type (int_type);
2380             require_array_type (pop_init_ref (reference_type), short_type);
2381             push_type (int_type);
2382             break;
2383           case op_istore:
2384             set_variable (get_byte (), pop_type (int_type));
2385             break;
2386           case op_lstore:
2387             set_variable (get_byte (), pop_type (long_type));
2388             break;
2389           case op_fstore:
2390             set_variable (get_byte (), pop_type (float_type));
2391             break;
2392           case op_dstore:
2393             set_variable (get_byte (), pop_type (double_type));
2394             break;
2395           case op_astore:
2396             set_variable (get_byte (), pop_ref_or_return ());
2397             break;
2398           case op_istore_0:
2399           case op_istore_1:
2400           case op_istore_2:
2401           case op_istore_3:
2402             set_variable (opcode - op_istore_0, pop_type (int_type));
2403             break;
2404           case op_lstore_0:
2405           case op_lstore_1:
2406           case op_lstore_2:
2407           case op_lstore_3:
2408             set_variable (opcode - op_lstore_0, pop_type (long_type));
2409             break;
2410           case op_fstore_0:
2411           case op_fstore_1:
2412           case op_fstore_2:
2413           case op_fstore_3:
2414             set_variable (opcode - op_fstore_0, pop_type (float_type));
2415             break;
2416           case op_dstore_0:
2417           case op_dstore_1:
2418           case op_dstore_2:
2419           case op_dstore_3:
2420             set_variable (opcode - op_dstore_0, pop_type (double_type));
2421             break;
2422           case op_astore_0:
2423           case op_astore_1:
2424           case op_astore_2:
2425           case op_astore_3:
2426             set_variable (opcode - op_astore_0, pop_ref_or_return ());
2427             break;
2428           case op_iastore:
2429             pop_type (int_type);
2430             pop_type (int_type);
2431             require_array_type (pop_init_ref (reference_type), int_type);
2432             break;
2433           case op_lastore:
2434             pop_type (long_type);
2435             pop_type (int_type);
2436             require_array_type (pop_init_ref (reference_type), long_type);
2437             break;
2438           case op_fastore:
2439             pop_type (float_type);
2440             pop_type (int_type);
2441             require_array_type (pop_init_ref (reference_type), float_type);
2442             break;
2443           case op_dastore:
2444             pop_type (double_type);
2445             pop_type (int_type);
2446             require_array_type (pop_init_ref (reference_type), double_type);
2447             break;
2448           case op_aastore:
2449             pop_type (reference_type);
2450             pop_type (int_type);
2451             require_array_type (pop_init_ref (reference_type), reference_type);
2452             break;
2453           case op_bastore:
2454             pop_type (int_type);
2455             pop_type (int_type);
2456             require_array_type (pop_init_ref (reference_type), byte_type);
2457             break;
2458           case op_castore:
2459             pop_type (int_type);
2460             pop_type (int_type);
2461             require_array_type (pop_init_ref (reference_type), char_type);
2462             break;
2463           case op_sastore:
2464             pop_type (int_type);
2465             pop_type (int_type);
2466             require_array_type (pop_init_ref (reference_type), short_type);
2467             break;
2468           case op_pop:
2469             pop32 ();
2470             break;
2471           case op_pop2:
2472             {
2473               type t = pop_raw ();
2474               if (! t.iswide ())
2475                 pop32 ();
2476             }
2477             break;
2478           case op_dup:
2479             {
2480               type t = pop32 ();
2481               push_type (t);
2482               push_type (t);
2483             }
2484             break;
2485           case op_dup_x1:
2486             {
2487               type t1 = pop32 ();
2488               type t2 = pop32 ();
2489               push_type (t1);
2490               push_type (t2);
2491               push_type (t1);
2492             }
2493             break;
2494           case op_dup_x2:
2495             {
2496               type t1 = pop32 ();
2497               type t2 = pop_raw ();
2498               if (! t2.iswide ())
2499                 {
2500                   type t3 = pop32 ();
2501                   push_type (t1);
2502                   push_type (t3);
2503                 }
2504               else
2505                 push_type (t1);
2506               push_type (t2);
2507               push_type (t1);
2508             }
2509             break;
2510           case op_dup2:
2511             {
2512               type t = pop_raw ();
2513               if (! t.iswide ())
2514                 {
2515                   type t2 = pop32 ();
2516                   push_type (t2);
2517                   push_type (t);
2518                   push_type (t2);
2519                 }
2520               else
2521                 push_type (t);
2522               push_type (t);
2523             }
2524             break;
2525           case op_dup2_x1:
2526             {
2527               type t1 = pop_raw ();
2528               type t2 = pop32 ();
2529               if (! t1.iswide ())
2530                 {
2531                   type t3 = pop32 ();
2532                   push_type (t2);
2533                   push_type (t1);
2534                   push_type (t3);
2535                 }
2536               else
2537                 push_type (t1);
2538               push_type (t2);
2539               push_type (t1);
2540             }
2541             break;
2542           case op_dup2_x2:
2543             {
2544               type t1 = pop_raw ();
2545               if (t1.iswide ())
2546                 {
2547                   type t2 = pop_raw ();
2548                   if (t2.iswide ())
2549                     {
2550                       push_type (t1);
2551                       push_type (t2);
2552                     }
2553                   else
2554                     {
2555                       type t3 = pop32 ();
2556                       push_type (t1);
2557                       push_type (t3);
2558                       push_type (t2);
2559                     }
2560                   push_type (t1);
2561                 }
2562               else
2563                 {
2564                   type t2 = pop32 ();
2565                   type t3 = pop_raw ();
2566                   if (t3.iswide ())
2567                     {
2568                       push_type (t2);
2569                       push_type (t1);
2570                     }
2571                   else
2572                     {
2573                       type t4 = pop32 ();
2574                       push_type (t2);
2575                       push_type (t1);
2576                       push_type (t4);
2577                     }
2578                   push_type (t3);
2579                   push_type (t2);
2580                   push_type (t1);
2581                 }
2582             }
2583             break;
2584           case op_swap:
2585             {
2586               type t1 = pop32 ();
2587               type t2 = pop32 ();
2588               push_type (t1);
2589               push_type (t2);
2590             }
2591             break;
2592           case op_iadd:
2593           case op_isub:
2594           case op_imul:
2595           case op_idiv:
2596           case op_irem:
2597           case op_ishl:
2598           case op_ishr:
2599           case op_iushr:
2600           case op_iand:
2601           case op_ior:
2602           case op_ixor:
2603             pop_type (int_type);
2604             push_type (pop_type (int_type));
2605             break;
2606           case op_ladd:
2607           case op_lsub:
2608           case op_lmul:
2609           case op_ldiv:
2610           case op_lrem:
2611           case op_land:
2612           case op_lor:
2613           case op_lxor:
2614             pop_type (long_type);
2615             push_type (pop_type (long_type));
2616             break;
2617           case op_lshl:
2618           case op_lshr:
2619           case op_lushr:
2620             pop_type (int_type);
2621             push_type (pop_type (long_type));
2622             break;
2623           case op_fadd:
2624           case op_fsub:
2625           case op_fmul:
2626           case op_fdiv:
2627           case op_frem:
2628             pop_type (float_type);
2629             push_type (pop_type (float_type));
2630             break;
2631           case op_dadd:
2632           case op_dsub:
2633           case op_dmul:
2634           case op_ddiv:
2635           case op_drem:
2636             pop_type (double_type);
2637             push_type (pop_type (double_type));
2638             break;
2639           case op_ineg:
2640           case op_i2b:
2641           case op_i2c:
2642           case op_i2s:
2643             push_type (pop_type (int_type));
2644             break;
2645           case op_lneg:
2646             push_type (pop_type (long_type));
2647             break;
2648           case op_fneg:
2649             push_type (pop_type (float_type));
2650             break;
2651           case op_dneg:
2652             push_type (pop_type (double_type));
2653             break;
2654           case op_iinc:
2655             get_variable (get_byte (), int_type);
2656             get_byte ();
2657             break;
2658           case op_i2l:
2659             pop_type (int_type);
2660             push_type (long_type);
2661             break;
2662           case op_i2f:
2663             pop_type (int_type);
2664             push_type (float_type);
2665             break;
2666           case op_i2d:
2667             pop_type (int_type);
2668             push_type (double_type);
2669             break;
2670           case op_l2i:
2671             pop_type (long_type);
2672             push_type (int_type);
2673             break;
2674           case op_l2f:
2675             pop_type (long_type);
2676             push_type (float_type);
2677             break;
2678           case op_l2d:
2679             pop_type (long_type);
2680             push_type (double_type);
2681             break;
2682           case op_f2i:
2683             pop_type (float_type);
2684             push_type (int_type);
2685             break;
2686           case op_f2l:
2687             pop_type (float_type);
2688             push_type (long_type);
2689             break;
2690           case op_f2d:
2691             pop_type (float_type);
2692             push_type (double_type);
2693             break;
2694           case op_d2i:
2695             pop_type (double_type);
2696             push_type (int_type);
2697             break;
2698           case op_d2l:
2699             pop_type (double_type);
2700             push_type (long_type);
2701             break;
2702           case op_d2f:
2703             pop_type (double_type);
2704             push_type (float_type);
2705             break;
2706           case op_lcmp:
2707             pop_type (long_type);
2708             pop_type (long_type);
2709             push_type (int_type);
2710             break;
2711           case op_fcmpl:
2712           case op_fcmpg:
2713             pop_type (float_type);
2714             pop_type (float_type);
2715             push_type (int_type);
2716             break;
2717           case op_dcmpl:
2718           case op_dcmpg:
2719             pop_type (double_type);
2720             pop_type (double_type);
2721             push_type (int_type);
2722             break;
2723           case op_ifeq:
2724           case op_ifne:
2725           case op_iflt:
2726           case op_ifge:
2727           case op_ifgt:
2728           case op_ifle:
2729             pop_type (int_type);
2730             push_jump (get_short ());
2731             break;
2732           case op_if_icmpeq:
2733           case op_if_icmpne:
2734           case op_if_icmplt:
2735           case op_if_icmpge:
2736           case op_if_icmpgt:
2737           case op_if_icmple:
2738             pop_type (int_type);
2739             pop_type (int_type);
2740             push_jump (get_short ());
2741             break;
2742           case op_if_acmpeq:
2743           case op_if_acmpne:
2744             pop_type (reference_type);
2745             pop_type (reference_type);
2746             push_jump (get_short ());
2747             break;
2748           case op_goto:
2749             push_jump (get_short ());
2750             invalidate_pc ();
2751             break;
2752           case op_jsr:
2753             handle_jsr_insn (get_short ());
2754             break;
2755           case op_ret:
2756             handle_ret_insn (get_byte ());
2757             break;
2758           case op_tableswitch:
2759             {
2760               pop_type (int_type);
2761               skip_padding ();
2762               push_jump (get_int ());
2763               jint low = get_int ();
2764               jint high = get_int ();
2765               // Already checked LOW -vs- HIGH.
2766               for (int i = low; i <= high; ++i)
2767                 push_jump (get_int ());
2768               invalidate_pc ();
2769             }
2770             break;
2771
2772           case op_lookupswitch:
2773             {
2774               pop_type (int_type);
2775               skip_padding ();
2776               push_jump (get_int ());
2777               jint npairs = get_int ();
2778               // Already checked NPAIRS >= 0.
2779               jint lastkey = 0;
2780               for (int i = 0; i < npairs; ++i)
2781                 {
2782                   jint key = get_int ();
2783                   if (i > 0 && key <= lastkey)
2784                     verify_fail ("lookupswitch pairs unsorted", start_PC);
2785                   lastkey = key;
2786                   push_jump (get_int ());
2787                 }
2788               invalidate_pc ();
2789             }
2790             break;
2791           case op_ireturn:
2792             check_return_type (pop_type (int_type));
2793             invalidate_pc ();
2794             break;
2795           case op_lreturn:
2796             check_return_type (pop_type (long_type));
2797             invalidate_pc ();
2798             break;
2799           case op_freturn:
2800             check_return_type (pop_type (float_type));
2801             invalidate_pc ();
2802             break;
2803           case op_dreturn:
2804             check_return_type (pop_type (double_type));
2805             invalidate_pc ();
2806             break;
2807           case op_areturn:
2808             check_return_type (pop_init_ref (reference_type));
2809             invalidate_pc ();
2810             break;
2811           case op_return:
2812             // We only need to check this when the return type is
2813             // void, because all instance initializers return void.
2814             if (this_is_init)
2815               current_state->check_this_initialized (this);
2816             check_return_type (void_type);
2817             invalidate_pc ();
2818             break;
2819           case op_getstatic:
2820             push_type (check_field_constant (get_ushort ()));
2821             break;
2822           case op_putstatic:
2823             pop_type (check_field_constant (get_ushort ()));
2824             break;
2825           case op_getfield:
2826             {
2827               type klass;
2828               type field = check_field_constant (get_ushort (), &klass);
2829               pop_type (klass);
2830               push_type (field);
2831             }
2832             break;
2833           case op_putfield:
2834             {
2835               type klass;
2836               type field = check_field_constant (get_ushort (), &klass);
2837               pop_type (field);
2838
2839               // We have an obscure special case here: we can use
2840               // `putfield' on a field declared in this class, even if
2841               // `this' has not yet been initialized.
2842               if (! current_state->this_type.isinitialized ()
2843                   && current_state->this_type.pc == type::SELF)
2844                 klass.set_uninitialized (type::SELF, this);
2845               pop_type (klass);
2846             }
2847             break;
2848
2849           case op_invokevirtual:
2850           case op_invokespecial:
2851           case op_invokestatic:
2852           case op_invokeinterface:
2853             {
2854               _Jv_Utf8Const *method_name, *method_signature;
2855               type class_type
2856                 = check_method_constant (get_ushort (),
2857                                          opcode == op_invokeinterface,
2858                                          &method_name,
2859                                          &method_signature);
2860               // NARGS is only used when we're processing
2861               // invokeinterface.  It is simplest for us to compute it
2862               // here and then verify it later.
2863               int nargs = 0;
2864               if (opcode == op_invokeinterface)
2865                 {
2866                   nargs = get_byte ();
2867                   if (get_byte () != 0)
2868                     verify_fail ("invokeinterface dummy byte is wrong");
2869                 }
2870
2871               bool is_init = false;
2872               if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2873                 {
2874                   is_init = true;
2875                   if (opcode != op_invokespecial)
2876                     verify_fail ("can't invoke <init>");
2877                 }
2878               else if (method_name->data[0] == '<')
2879                 verify_fail ("can't invoke method starting with `<'");
2880
2881               // Pop arguments and check types.
2882               int arg_count = _Jv_count_arguments (method_signature);
2883               type arg_types[arg_count];
2884               compute_argument_types (method_signature, arg_types);
2885               for (int i = arg_count - 1; i >= 0; --i)
2886                 {
2887                   // This is only used for verifying the byte for
2888                   // invokeinterface.
2889                   nargs -= arg_types[i].depth ();
2890                   pop_init_ref (arg_types[i]);
2891                 }
2892
2893               if (opcode == op_invokeinterface
2894                   && nargs != 1)
2895                 verify_fail ("wrong argument count for invokeinterface");
2896
2897               if (opcode != op_invokestatic)
2898                 {
2899                   type t = class_type;
2900                   if (is_init)
2901                     {
2902                       // In this case the PC doesn't matter.
2903                       t.set_uninitialized (type::UNINIT, this);
2904                     }
2905                   type raw = pop_raw ();
2906                   bool ok = false;
2907                   if (! is_init && ! raw.isinitialized ())
2908                     {
2909                       // This is a failure.
2910                     }
2911                   else if (is_init && raw.isnull ())
2912                     {
2913                       // Another failure.
2914                     }
2915                   else if (t.compatible (raw, this))
2916                     {
2917                       ok = true;
2918                     }
2919                   else if (opcode == op_invokeinterface)
2920                     {
2921                       // This is a hack.  We might have merged two
2922                       // items and gotten `Object'.  This can happen
2923                       // because we don't keep track of where merges
2924                       // come from.  This is safe as long as the
2925                       // interpreter checks interfaces at runtime.
2926                       type obj (&java::lang::Object::class$);
2927                       ok = raw.compatible (obj, this);
2928                     }
2929
2930                   if (! ok)
2931                     verify_fail ("incompatible type on stack");
2932
2933                   if (is_init)
2934                     current_state->set_initialized (raw.get_pc (),
2935                                                     current_method->max_locals);
2936                 }
2937
2938               type rt = compute_return_type (method_signature);
2939               if (! rt.isvoid ())
2940                 push_type (rt);
2941             }
2942             break;
2943
2944           case op_new:
2945             {
2946               type t = check_class_constant (get_ushort ());
2947               if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2948                 verify_fail ("type is array, interface, or abstract");
2949               t.set_uninitialized (start_PC, this);
2950               push_type (t);
2951             }
2952             break;
2953
2954           case op_newarray:
2955             {
2956               int atype = get_byte ();
2957               // We intentionally have chosen constants to make this
2958               // valid.
2959               if (atype < boolean_type || atype > long_type)
2960                 verify_fail ("type not primitive", start_PC);
2961               pop_type (int_type);
2962               push_type (construct_primitive_array_type (type_val (atype)));
2963             }
2964             break;
2965           case op_anewarray:
2966             pop_type (int_type);
2967             push_type (check_class_constant (get_ushort ()).to_array (this));
2968             break;
2969           case op_arraylength:
2970             {
2971               type t = pop_init_ref (reference_type);
2972               if (! t.isarray () && ! t.isnull ())
2973                 verify_fail ("array type expected");
2974               push_type (int_type);
2975             }
2976             break;
2977           case op_athrow:
2978             pop_type (type (&java::lang::Throwable::class$));
2979             invalidate_pc ();
2980             break;
2981           case op_checkcast:
2982             pop_init_ref (reference_type);
2983             push_type (check_class_constant (get_ushort ()));
2984             break;
2985           case op_instanceof:
2986             pop_init_ref (reference_type);
2987             check_class_constant (get_ushort ());
2988             push_type (int_type);
2989             break;
2990           case op_monitorenter:
2991             pop_init_ref (reference_type);
2992             break;
2993           case op_monitorexit:
2994             pop_init_ref (reference_type);
2995             break;
2996           case op_wide:
2997             {
2998               switch (get_byte ())
2999                 {
3000                 case op_iload:
3001                   push_type (get_variable (get_ushort (), int_type));
3002                   break;
3003                 case op_lload:
3004                   push_type (get_variable (get_ushort (), long_type));
3005                   break;
3006                 case op_fload:
3007                   push_type (get_variable (get_ushort (), float_type));
3008                   break;
3009                 case op_dload:
3010                   push_type (get_variable (get_ushort (), double_type));
3011                   break;
3012                 case op_aload:
3013                   push_type (get_variable (get_ushort (), reference_type));
3014                   break;
3015                 case op_istore:
3016                   set_variable (get_ushort (), pop_type (int_type));
3017                   break;
3018                 case op_lstore:
3019                   set_variable (get_ushort (), pop_type (long_type));
3020                   break;
3021                 case op_fstore:
3022                   set_variable (get_ushort (), pop_type (float_type));
3023                   break;
3024                 case op_dstore:
3025                   set_variable (get_ushort (), pop_type (double_type));
3026                   break;
3027                 case op_astore:
3028                   set_variable (get_ushort (), pop_init_ref (reference_type));
3029                   break;
3030                 case op_ret:
3031                   handle_ret_insn (get_short ());
3032                   break;
3033                 case op_iinc:
3034                   get_variable (get_ushort (), int_type);
3035                   get_short ();
3036                   break;
3037                 default:
3038                   verify_fail ("unrecognized wide instruction", start_PC);
3039                 }
3040             }
3041             break;
3042           case op_multianewarray:
3043             {
3044               type atype = check_class_constant (get_ushort ());
3045               int dim = get_byte ();
3046               if (dim < 1)
3047                 verify_fail ("too few dimensions to multianewarray", start_PC);
3048               atype.verify_dimensions (dim, this);
3049               for (int i = 0; i < dim; ++i)
3050                 pop_type (int_type);
3051               push_type (atype);
3052             }
3053             break;
3054           case op_ifnull:
3055           case op_ifnonnull:
3056             pop_type (reference_type);
3057             push_jump (get_short ());
3058             break;
3059           case op_goto_w:
3060             push_jump (get_int ());
3061             invalidate_pc ();
3062             break;
3063           case op_jsr_w:
3064             handle_jsr_insn (get_int ());
3065             break;
3066
3067           // These are unused here, but we call them out explicitly
3068           // so that -Wswitch-enum doesn't complain.
3069           case op_putfield_1:
3070           case op_putfield_2:
3071           case op_putfield_4:
3072           case op_putfield_8:
3073           case op_putfield_a:
3074           case op_putstatic_1:
3075           case op_putstatic_2:
3076           case op_putstatic_4:
3077           case op_putstatic_8:
3078           case op_putstatic_a:
3079           case op_getfield_1:
3080           case op_getfield_2s:
3081           case op_getfield_2u:
3082           case op_getfield_4:
3083           case op_getfield_8:
3084           case op_getfield_a:
3085           case op_getstatic_1:
3086           case op_getstatic_2s:
3087           case op_getstatic_2u:
3088           case op_getstatic_4:
3089           case op_getstatic_8:
3090           case op_getstatic_a:
3091           default:
3092             // Unrecognized opcode.
3093             verify_fail ("unrecognized instruction in verify_instructions_0",
3094                          start_PC);
3095           }
3096       }
3097   }
3098
3099 public:
3100
3101   void verify_instructions ()
3102   {
3103     branch_prepass ();
3104     verify_instructions_0 ();
3105   }
3106
3107   _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3108   {
3109     // We just print the text as utf-8.  This is just for debugging
3110     // anyway.
3111     debug_print ("--------------------------------\n");
3112     debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3113
3114     current_method = m;
3115     bytecode = m->bytecode ();
3116     exception = m->exceptions ();
3117     current_class = m->defining_class;
3118
3119     states = NULL;
3120     flags = NULL;
3121     jsr_ptrs = NULL;
3122     utf8_list = NULL;
3123     entry_points = NULL;
3124   }
3125
3126   ~_Jv_BytecodeVerifier ()
3127   {
3128     if (states)
3129       _Jv_Free (states);
3130     if (flags)
3131       _Jv_Free (flags);
3132
3133     if (jsr_ptrs)
3134       {
3135         for (int i = 0; i < current_method->code_length; ++i)
3136           {
3137             if (jsr_ptrs[i] != NULL)
3138               {
3139                 subr_info *info = jsr_ptrs[i];
3140                 while (info != NULL)
3141                   {
3142                     subr_info *next = info->next;
3143                     _Jv_Free (info);
3144                     info = next;
3145                   }
3146               }
3147           }
3148         _Jv_Free (jsr_ptrs);
3149       }
3150
3151     while (utf8_list != NULL)
3152       {
3153         linked_utf8 *n = utf8_list->next;
3154         _Jv_Free (utf8_list->val);
3155         _Jv_Free (utf8_list);
3156         utf8_list = n;
3157       }
3158
3159     while (entry_points != NULL)
3160       {
3161         subr_entry_info *next = entry_points->next;
3162         _Jv_Free (entry_points);
3163         entry_points = next;
3164       }
3165   }
3166 };
3167
3168 void
3169 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3170 {
3171   _Jv_BytecodeVerifier v (meth);
3172   v.verify_instructions ();
3173 }
3174 #endif  /* INTERPRETER */