OSDN Git Service

* verify.cc (_Jv_BytecodeVerifier::get_type_val_for_signature):
[pf3gnuchains/gcc-fork.git] / libjava / verify.cc
1 // defineclass.cc - defining a class from .class format.
2
3 /* Copyright (C) 2001  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 // Writte by Tom Tromey <tromey@redhat.com>
12
13 #include <config.h>
14
15 #include <jvm.h>
16 #include <gcj/cni.h>
17 #include <java-insns.h>
18 #include <java-interp.h>
19
20 #ifdef INTERPRETER
21
22 #include <java/lang/Class.h>
23 #include <java/lang/VerifyError.h>
24 #include <java/lang/Throwable.h>
25 #include <java/lang/reflect/Modifier.h>
26 #include <java/lang/StringBuffer.h>
27
28
29 // TO DO
30 // * read more about when classes must be loaded
31 // * there are bugs with boolean arrays?
32 // * class loader madness
33 // * Lots and lots of debugging and testing
34 // * type representation is still ugly.  look for the big switches
35 // * at least one GC problem :-(
36
37
38 // This is global because __attribute__ doesn't seem to work on static
39 // methods.
40 static void verify_fail (char *msg, jint pc = -1)
41   __attribute__ ((__noreturn__));
42
43 class _Jv_BytecodeVerifier
44 {
45 private:
46
47   static const int FLAG_INSN_START = 1;
48   static const int FLAG_BRANCH_TARGET = 2;
49   static const int FLAG_JSR_TARGET = 4;
50
51   struct state;
52   struct type;
53   struct subr_info;
54
55   // The current PC.
56   int PC;
57   // The PC corresponding to the start of the current instruction.
58   int start_PC;
59
60   // The current state of the stack, locals, etc.
61   state *current_state;
62
63   // We store the state at branch targets, for merging.  This holds
64   // such states.
65   state **states;
66
67   // We keep a linked list of all the PCs which we must reverify.
68   // The link is done using the PC values.  This is the head of the
69   // list.
70   int next_verify_pc;
71
72   // We keep some flags for each instruction.  The values are the
73   // FLAG_* constants defined above.
74   char *flags;
75
76   // We need to keep track of which instructions can call a given
77   // subroutine.  FIXME: this is inefficient.  We keep a linked list
78   // of all calling `jsr's at at each jsr target.
79   subr_info **jsr_ptrs;
80
81   // The current top of the stack, in terms of slots.
82   int stacktop;
83   // The current depth of the stack.  This will be larger than
84   // STACKTOP when wide types are on the stack.
85   int stackdepth;
86
87   // The bytecode itself.
88   unsigned char *bytecode;
89   // The exceptions.
90   _Jv_InterpException *exception;
91
92   // Defining class.
93   jclass current_class;
94   // This method.
95   _Jv_InterpMethod *current_method;
96
97   // This enum holds a list of tags for all the different types we
98   // need to handle.  Reference types are treated specially by the
99   // type class.
100   enum type_val
101   {
102     void_type,
103
104     // The values for primitive types are chosen to correspond to values
105     // specified to newarray.
106     boolean_type = 4,
107     char_type = 5,
108     float_type = 6,
109     double_type = 7,
110     byte_type = 8,
111     short_type = 9,
112     int_type = 10,
113     long_type = 11,
114
115     // Used when overwriting second word of a double or long in the
116     // local variables.  Also used after merging local variable states
117     // to indicate an unusable value.
118     unsuitable_type,
119     return_address_type,
120     continuation_type,
121
122     // Everything after `reference_type' must be a reference type.
123     reference_type,
124     null_type,
125     unresolved_reference_type,
126     uninitialized_reference_type,
127     uninitialized_unresolved_reference_type
128   };
129
130   // Return the type_val corresponding to a primitive signature
131   // character.  For instance `I' returns `int.class'.
132   static type_val get_type_val_for_signature (jchar sig)
133   {
134     type_val rt;
135     switch (sig)
136       {
137       case 'Z':
138         rt = boolean_type;
139         break;
140       case 'B':
141         rt = byte_type;
142         break;
143       case 'C':
144         rt = char_type;
145         break;
146       case 'S':
147         rt = short_type;
148         break;
149       case 'I':
150         rt = int_type;
151         break;
152       case 'J':
153         rt = long_type;
154         break;
155       case 'F':
156         rt = float_type;
157         break;
158       case 'D':
159         rt = double_type;
160         break;
161       case 'V':
162         rt = void_type;
163         break;
164       default:
165         verify_fail ("invalid signature");
166       }
167     return rt;
168   }
169
170   // Return the type_val corresponding to a primitive class.
171   static type_val get_type_val_for_signature (jclass k)
172   {
173     return get_type_val_for_signature ((jchar) k->method_count);
174   }
175
176   // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
177   // TARGET haven't been prepared.
178   static bool is_assignable_from_slow (jclass target, jclass source)
179   {
180     // This will terminate when SOURCE==Object.
181     while (true)
182       {
183         if (source == target)
184           return true;
185
186         if (target->isPrimitive () || source->isPrimitive ())
187           return false;
188
189         // _Jv_IsAssignableFrom can handle a target which is an
190         // interface even if it hasn't been prepared.
191         if ((target->state > JV_STATE_LINKED || target->isInterface ())
192             && source->state > JV_STATE_LINKED)
193           return _Jv_IsAssignableFrom (target, source);
194
195         if (target->isArray ())
196           {
197             if (! source->isArray ())
198               return false;
199             target = target->getComponentType ();
200             source = source->getComponentType ();
201           }
202         else if (target->isInterface ())
203           {
204             for (int i = 0; i < source->interface_count; ++i)
205               {
206                 // We use a recursive call because we also need to
207                 // check superinterfaces.
208                 if (is_assignable_from_slow (target, source->interfaces[i]))
209                     return true;
210               }
211             return false;
212           }
213         else if (target == &java::lang::Object::class$)
214           return true;
215         else if (source->isInterface ()
216                  || source == &java::lang::Object::class$)
217           return false;
218         else
219           source = source->getSuperclass ();
220       }
221   }
222
223   // This is used to keep track of which `jsr's correspond to a given
224   // jsr target.
225   struct subr_info
226   {
227     // PC of the instruction just after the jsr.
228     int pc;
229     // Link.
230     subr_info *next;
231   };
232
233   // The `type' class is used to represent a single type in the
234   // verifier.
235   struct type
236   {
237     // The type.
238     type_val key;
239     // Some associated data.
240     union
241     {
242       // For a resolved reference type, this is a pointer to the class.
243       jclass klass;
244       // For other reference types, this it the name of the class.
245       _Jv_Utf8Const *name;
246     } data;
247     // This is used when constructing a new object.  It is the PC of the
248     // `new' instruction which created the object.  We use the special
249     // value -2 to mean that this is uninitialized, and the special
250     // value -1 for the case where the current method is itself the
251     // <init> method.
252     int pc;
253
254     static const int UNINIT = -2;
255     static const int SELF = -1;
256
257     // Basic constructor.
258     type ()
259     {
260       key = unsuitable_type;
261       data.klass = NULL;
262       pc = UNINIT;
263     }
264
265     // Make a new instance given the type tag.  We assume a generic
266     // `reference_type' means Object.
267     type (type_val k)
268     {
269       key = k;
270       data.klass = NULL;
271       if (key == reference_type)
272         data.klass = &java::lang::Object::class$;
273       pc = UNINIT;
274     }
275
276     // Make a new instance given a class.
277     type (jclass klass)
278     {
279       key = reference_type;
280       data.klass = klass;
281       pc = UNINIT;
282     }
283
284     // Make a new instance given the name of a class.
285     type (_Jv_Utf8Const *n)
286     {
287       key = unresolved_reference_type;
288       data.name = n;
289       pc = UNINIT;
290     }
291
292     // Copy constructor.
293     type (const type &t)
294     {
295       key = t.key;
296       data = t.data;
297       pc = t.pc;
298     }
299
300     // These operators are required because libgcj can't link in
301     // -lstdc++.
302     void *operator new[] (size_t bytes)
303     {
304       return _Jv_Malloc (bytes);
305     }
306
307     void operator delete[] (void *mem)
308     {
309       _Jv_Free (mem);
310     }
311
312     type& operator= (type_val k)
313     {
314       key = k;
315       data.klass = NULL;
316       pc = UNINIT;
317       return *this;
318     }
319
320     type& operator= (const type& t)
321     {
322       key = t.key;
323       data = t.data;
324       pc = t.pc;
325       return *this;
326     }
327
328     // Promote a numeric type.
329     type &promote ()
330     {
331       if (key == boolean_type || key == char_type
332           || key == byte_type || key == short_type)
333         key = int_type;
334       return *this;
335     }
336
337     // If *THIS is an unresolved reference type, resolve it.
338     void resolve ()
339     {
340       if (key != unresolved_reference_type
341           && key != uninitialized_unresolved_reference_type)
342         return;
343
344       // FIXME: class loader
345       using namespace java::lang;
346       // We might see either kind of name.  Sigh.
347       if (data.name->data[0] == 'L'
348           && data.name->data[data.name->length - 1] == ';')
349         data.klass = _Jv_FindClassFromSignature (data.name->data, NULL);
350       else
351         data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
352                                      false, NULL);
353       key = (key == unresolved_reference_type
354              ? reference_type
355              : uninitialized_reference_type);
356     }
357
358     // Mark this type as the uninitialized result of `new'.
359     void set_uninitialized (int npc)
360     {
361       if (key == reference_type)
362         key = uninitialized_reference_type;
363       else if (key == unresolved_reference_type)
364         key = uninitialized_unresolved_reference_type;
365       else
366         verify_fail ("internal error in type::uninitialized");
367       pc = npc;
368     }
369
370     // Mark this type as now initialized.
371     void set_initialized (int npc)
372     {
373       if (npc != UNINIT && pc == npc
374           && (key == uninitialized_reference_type
375               || key == uninitialized_unresolved_reference_type))
376         {
377           key = (key == uninitialized_reference_type
378                  ? reference_type
379                  : unresolved_reference_type);
380           pc = UNINIT;
381         }
382     }
383
384
385     // Return true if an object of type K can be assigned to a variable
386     // of type *THIS.  Handle various special cases too.  Might modify
387     // *THIS or K.  Note however that this does not perform numeric
388     // promotion.
389     bool compatible (type &k)
390     {
391       // Any type is compatible with the unsuitable type.
392       if (key == unsuitable_type)
393         return true;
394
395       if (key < reference_type || k.key < reference_type)
396         return key == k.key;
397
398       // The `null' type is convertible to any reference type.
399       // FIXME: is this correct for THIS?
400       if (key == null_type || k.key == null_type)
401         return true;
402
403       // Any reference type is convertible to Object.  This is a special
404       // case so we don't need to unnecessarily resolve a class.
405       if (key == reference_type
406           && data.klass == &java::lang::Object::class$)
407         return true;
408
409       // An initialized type and an uninitialized type are not
410       // compatible.
411       if (isinitialized () != k.isinitialized ())
412         return false;
413
414       // Two uninitialized objects are compatible if either:
415       // * The PCs are identical, or
416       // * One PC is UNINIT.
417       if (! isinitialized ())
418         {
419           if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
420             return false;
421         }
422
423       // Two unresolved types are equal if their names are the same.
424       if (! isresolved ()
425           && ! k.isresolved ()
426           && _Jv_equalUtf8Consts (data.name, k.data.name))
427         return true;
428
429       // We must resolve both types and check assignability.
430       resolve ();
431       k.resolve ();
432       return is_assignable_from_slow (data.klass, k.data.klass);
433     }
434
435     bool isvoid () const
436     {
437       return key == void_type;
438     }
439
440     bool iswide () const
441     {
442       return key == long_type || key == double_type;
443     }
444
445     // Return number of stack or local variable slots taken by this
446     // type.
447     int depth () const
448     {
449       return iswide () ? 2 : 1;
450     }
451
452     bool isarray () const
453     {
454       // We treat null_type as not an array.  This is ok based on the
455       // current uses of this method.
456       if (key == reference_type)
457         return data.klass->isArray ();
458       else if (key == unresolved_reference_type)
459         return data.name->data[0] == '[';
460       return false;
461     }
462
463     bool isinterface ()
464     {
465       resolve ();
466       if (key != reference_type)
467         return false;
468       return data.klass->isInterface ();
469     }
470
471     bool isabstract ()
472     {
473       resolve ();
474       if (key != reference_type)
475         return false;
476       using namespace java::lang::reflect;
477       return Modifier::isAbstract (data.klass->getModifiers ());
478     }
479
480     // Return the element type of an array.
481     type element_type ()
482     {
483       // FIXME: maybe should do string manipulation here.
484       resolve ();
485       if (key != reference_type)
486         verify_fail ("programmer error in type::element_type()");
487
488       jclass k = data.klass->getComponentType ();
489       if (k->isPrimitive ())
490         return type (get_type_val_for_signature (k));
491       return type (k);
492     }
493
494     bool isreference () const
495     {
496       return key >= reference_type;
497     }
498
499     int get_pc () const
500     {
501       return pc;
502     }
503
504     bool isinitialized () const
505     {
506       return (key == reference_type
507               || key == null_type
508               || key == unresolved_reference_type);
509     }
510
511     bool isresolved () const
512     {
513       return (key == reference_type
514               || key == null_type
515               || key == uninitialized_reference_type);
516     }
517
518     void verify_dimensions (int ndims)
519     {
520       // The way this is written, we don't need to check isarray().
521       if (key == reference_type)
522         {
523           jclass k = data.klass;
524           while (k->isArray () && ndims > 0)
525             {
526               k = k->getComponentType ();
527               --ndims;
528             }
529         }
530       else
531         {
532           // We know KEY == unresolved_reference_type.
533           char *p = data.name->data;
534           while (*p++ == '[' && ndims-- > 0)
535             ;
536         }
537
538       if (ndims > 0)
539         verify_fail ("array type has fewer dimensions than required");
540     }
541
542     // Merge OLD_TYPE into this.  On error throw exception.
543     bool merge (type& old_type, bool local_semantics = false)
544     {
545       bool changed = false;
546       bool refo = old_type.isreference ();
547       bool refn = isreference ();
548       if (refo && refn)
549         {
550           if (old_type.key == null_type)
551             ;
552           else if (key == null_type)
553             {
554               *this = old_type;
555               changed = true;
556             }
557           else if (isinitialized () != old_type.isinitialized ())
558             verify_fail ("merging initialized and uninitialized types");
559           else
560             {
561               if (! isinitialized ())
562                 {
563                   if (pc == UNINIT)
564                     pc = old_type.pc;
565                   else if (old_type.pc == UNINIT)
566                     ;
567                   else if (pc != old_type.pc)
568                     verify_fail ("merging different uninitialized types");
569                 }
570
571               if (! isresolved ()
572                   && ! old_type.isresolved ()
573                   && _Jv_equalUtf8Consts (data.name, old_type.data.name))
574                 {
575                   // Types are identical.
576                 }
577               else
578                 {
579                   resolve ();
580                   old_type.resolve ();
581
582                   jclass k = data.klass;
583                   jclass oldk = old_type.data.klass;
584
585                   int arraycount = 0;
586                   while (k->isArray () && oldk->isArray ())
587                     {
588                       ++arraycount;
589                       k = k->getComponentType ();
590                       oldk = oldk->getComponentType ();
591                     }
592
593                   // This loop will end when we hit Object.
594                   while (true)
595                     {
596                       if (is_assignable_from_slow (k, oldk))
597                         break;
598                       k = k->getSuperclass ();
599                       changed = true;
600                     }
601
602                   if (changed)
603                     {
604                       while (arraycount > 0)
605                         {
606                           // FIXME: Class loader.
607                           k = _Jv_GetArrayClass (k, NULL);
608                           --arraycount;
609                         }
610                       data.klass = k;
611                     }
612                 }
613             }
614         }
615       else if (refo || refn || key != old_type.key)
616         {
617           if (local_semantics)
618             {
619               key = unsuitable_type;
620               changed = true;
621             }
622           else
623             verify_fail ("unmergeable type");
624         }
625       return changed;
626     }
627   };
628
629   // This class holds all the state information we need for a given
630   // location.
631   struct state
632   {
633     // Current top of stack.
634     int stacktop;
635     // Current stack depth.  This is like the top of stack but it
636     // includes wide variable information.
637     int stackdepth;
638     // The stack.
639     type *stack;
640     // The local variables.
641     type *locals;
642     // This is used in subroutines to keep track of which local
643     // variables have been accessed.
644     bool *local_changed;
645     // If not 0, then we are in a subroutine.  The value is the PC of
646     // the subroutine's entry point.  We can use 0 as an exceptional
647     // value because PC=0 can never be a subroutine.
648     int subroutine;
649     // This is used to keep a linked list of all the states which
650     // require re-verification.  We use the PC to keep track.
651     int next;
652
653     // INVALID marks a state which is not on the linked list of states
654     // requiring reverification.
655     static const int INVALID = -1;
656     // NO_NEXT marks the state at the end of the reverification list.
657     static const int NO_NEXT = -2;
658
659     state ()
660     {
661       stack = NULL;
662       locals = NULL;
663       local_changed = NULL;
664     }
665
666     state (int max_stack, int max_locals)
667     {
668       stacktop = 0;
669       stackdepth = 0;
670       stack = new type[max_stack];
671       for (int i = 0; i < max_stack; ++i)
672         stack[i] = unsuitable_type;
673       locals = new type[max_locals];
674       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
675       for (int i = 0; i < max_locals; ++i)
676         {
677           locals[i] = unsuitable_type;
678           local_changed[i] = false;
679         }
680       next = INVALID;
681       subroutine = 0;
682     }
683
684     state (const state *copy, int max_stack, int max_locals)
685     {
686       stack = new type[max_stack];
687       locals = new type[max_locals];
688       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
689       *this = *copy;
690       next = INVALID;
691     }
692
693     ~state ()
694     {
695       if (stack)
696         delete[] stack;
697       if (locals)
698         delete[] locals;
699       if (local_changed)
700         _Jv_Free (local_changed);
701     }
702
703     void *operator new[] (size_t bytes)
704     {
705       return _Jv_Malloc (bytes);
706     }
707
708     void operator delete[] (void *mem)
709     {
710       _Jv_Free (mem);
711     }
712
713     void *operator new (size_t bytes)
714     {
715       return _Jv_Malloc (bytes);
716     }
717
718     void operator delete (void *mem)
719     {
720       _Jv_Free (mem);
721     }
722
723     void copy (const state *copy, int max_stack, int max_locals)
724     {
725       stacktop = copy->stacktop;
726       stackdepth = copy->stackdepth;
727       subroutine = copy->subroutine;
728       for (int i = 0; i < max_stack; ++i)
729         stack[i] = copy->stack[i];
730       for (int i = 0; i < max_locals; ++i)
731         {
732           locals[i] = copy->locals[i];
733           local_changed[i] = copy->local_changed[i];
734         }
735       // Don't modify `next'.
736     }
737
738     // Modify this state to reflect entry to an exception handler.
739     void set_exception (type t, int max_stack)
740     {
741       stackdepth = 1;
742       stacktop = 1;
743       stack[0] = t;
744       for (int i = stacktop; i < max_stack; ++i)
745         stack[i] = unsuitable_type;
746
747       // FIXME: subroutine handling?
748     }
749
750     // Merge STATE into this state.  Destructively modifies this state.
751     // Returns true if the new state was in fact changed.  Will throw an
752     // exception if the states are not mergeable.
753     bool merge (state *state_old, bool ret_semantics,
754                 int max_locals)
755     {
756       bool changed = false;
757
758       // Merge subroutine states.  *THIS and *STATE_OLD must be in the
759       // same subroutine.  Also, recursive subroutine calls must be
760       // avoided.
761       if (subroutine == state_old->subroutine)
762         {
763           // Nothing.
764         }
765       else if (subroutine == 0)
766         {
767           subroutine = state_old->subroutine;
768           changed = true;
769         }
770       else
771         verify_fail ("subroutines merged");
772
773       // Merge stacks.
774       if (state_old->stacktop != stacktop)
775         verify_fail ("stack sizes differ");
776       for (int i = 0; i < state_old->stacktop; ++i)
777         {
778           if (stack[i].merge (state_old->stack[i]))
779             changed = true;
780         }
781
782       // Merge local variables.
783       for (int i = 0; i < max_locals; ++i)
784         {
785           if (! ret_semantics || local_changed[i])
786             {
787               if (locals[i].merge (state_old->locals[i], true))
788                 {
789                   changed = true;
790                   note_variable (i);
791                 }
792             }
793
794           // If we're in a subroutine, we must compute the union of
795           // all the changed local variables.
796           if (state_old->local_changed[i])
797             note_variable (i);
798         }
799
800       return changed;
801     }
802
803     // Throw an exception if there is an uninitialized object on the
804     // stack or in a local variable.  EXCEPTION_SEMANTICS controls
805     // whether we're using backwards-branch or exception-handing
806     // semantics.
807     void check_no_uninitialized_objects (int max_locals,
808                                          bool exception_semantics = false)
809     {
810       if (! exception_semantics)
811         {
812           for (int i = 0; i < stacktop; ++i)
813             if (stack[i].isreference () && ! stack[i].isinitialized ())
814               verify_fail ("uninitialized object on stack");
815         }
816
817       for (int i = 0; i < max_locals; ++i)
818         if (locals[i].isreference () && ! locals[i].isinitialized ())
819           verify_fail ("uninitialized object in local variable");
820     }
821
822     // Note that a local variable was accessed or modified.
823     void note_variable (int index)
824     {
825       if (subroutine > 0)
826         local_changed[index] = true;
827     }
828
829     // Mark each `new'd object we know of that was allocated at PC as
830     // initialized.
831     void set_initialized (int pc, int max_locals)
832     {
833       for (int i = 0; i < stacktop; ++i)
834         stack[i].set_initialized (pc);
835       for (int i = 0; i < max_locals; ++i)
836         locals[i].set_initialized (pc);
837     }
838   };
839
840   type pop_raw ()
841   {
842     if (current_state->stacktop <= 0)
843       verify_fail ("stack empty", start_PC);
844     type r = current_state->stack[--current_state->stacktop];
845     current_state->stackdepth -= r.depth ();
846     if (current_state->stackdepth < 0)
847       verify_fail ("stack empty", start_PC);
848     return r;
849   }
850
851   type pop32 ()
852   {
853     type r = pop_raw ();
854     if (r.iswide ())
855       verify_fail ("narrow pop of wide type", start_PC);
856     return r;
857   }
858
859   type pop64 ()
860   {
861     type r = pop_raw ();
862     if (! r.iswide ())
863       verify_fail ("wide pop of narrow type", start_PC);
864     return r;
865   }
866
867   type pop_type (type match)
868   {
869     match.promote ();
870     type t = pop_raw ();
871     if (! match.compatible (t))
872       verify_fail ("incompatible type on stack", start_PC);
873     return t;
874   }
875
876   void push_type (type t)
877   {
878     // If T is a numeric type like short, promote it to int.
879     t.promote ();
880
881     int depth = t.depth ();
882     if (current_state->stackdepth + depth > current_method->max_stack)
883       verify_fail ("stack overflow");
884     current_state->stack[current_state->stacktop++] = t;
885     current_state->stackdepth += depth;
886   }
887
888   void set_variable (int index, type t)
889   {
890     // If T is a numeric type like short, promote it to int.
891     t.promote ();
892
893     int depth = t.depth ();
894     if (index > current_method->max_locals - depth)
895       verify_fail ("invalid local variable");
896     current_state->locals[index] = t;
897     current_state->note_variable (index);
898
899     if (depth == 2)
900       {
901         current_state->locals[index + 1] = continuation_type;
902         current_state->note_variable (index + 1);
903       }
904     if (index > 0 && current_state->locals[index - 1].iswide ())
905       {
906         current_state->locals[index - 1] = unsuitable_type;
907         // There's no need to call note_variable here.
908       }
909   }
910
911   type get_variable (int index, type t)
912   {
913     int depth = t.depth ();
914     if (index > current_method->max_locals - depth)
915       verify_fail ("invalid local variable", start_PC);
916     if (! t.compatible (current_state->locals[index]))
917       verify_fail ("incompatible type in local variable", start_PC);
918     if (depth == 2)
919       {
920         type t (continuation_type);
921         if (! current_state->locals[index + 1].compatible (t))
922           verify_fail ("invalid local variable", start_PC);
923       }
924     current_state->note_variable (index);
925     return current_state->locals[index];
926   }
927
928   // Make sure ARRAY is an array type and that its elements are
929   // compatible with type ELEMENT.  Returns the actual element type.
930   type require_array_type (type array, type element)
931   {
932     if (! array.isarray ())
933       verify_fail ("array required");
934
935     type t = array.element_type ();
936     if (! element.compatible (t))
937       verify_fail ("incompatible array element type");
938
939     // Return T and not ELEMENT, because T might be specialized.
940     return t;
941   }
942
943   jint get_byte ()
944   {
945     if (PC >= current_method->code_length)
946       verify_fail ("premature end of bytecode");
947     return (jint) bytecode[PC++] & 0xff;
948   }
949
950   jint get_ushort ()
951   {
952     jint b1 = get_byte ();
953     jint b2 = get_byte ();
954     return (jint) ((b1 << 8) | b2) & 0xffff;
955   }
956
957   jint get_short ()
958   {
959     jint b1 = get_byte ();
960     jint b2 = get_byte ();
961     jshort s = (b1 << 8) | b2;
962     return (jint) s;
963   }
964
965   jint get_int ()
966   {
967     jint b1 = get_byte ();
968     jint b2 = get_byte ();
969     jint b3 = get_byte ();
970     jint b4 = get_byte ();
971     return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
972   }
973
974   int compute_jump (int offset)
975   {
976     int npc = start_PC + offset;
977     if (npc < 0 || npc >= current_method->code_length)
978       verify_fail ("branch out of range");
979     return npc;
980   }
981
982   // Merge the indicated state into a new state and schedule a new PC if
983   // there is a change.  If RET_SEMANTICS is true, then we are merging
984   // from a `ret' instruction into the instruction after a `jsr'.  This
985   // is a special case with its own modified semantics.
986   void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
987   {
988     bool changed = true;
989     if (states[npc] == NULL)
990       {
991         // FIXME: what if we reach this code from a `ret'?
992         
993         states[npc] = new state (nstate, current_method->max_stack,
994                                  current_method->max_locals);
995       }
996     else
997       changed = nstate->merge (states[npc], ret_semantics,
998                                current_method->max_stack);
999
1000     if (changed && states[npc]->next == state::INVALID)
1001       {
1002         // The merge changed the state, and the new PC isn't yet on our
1003         // list of PCs to re-verify.
1004         states[npc]->next = next_verify_pc;
1005         next_verify_pc = npc;
1006       }
1007   }
1008
1009   void push_jump (int offset)
1010   {
1011     int npc = compute_jump (offset);
1012     if (npc < PC)
1013       current_state->check_no_uninitialized_objects (current_method->max_stack);
1014     push_jump_merge (npc, current_state);
1015   }
1016
1017   void push_exception_jump (type t, int pc)
1018   {
1019     current_state->check_no_uninitialized_objects (current_method->max_stack,
1020                                                   true);
1021     state s (current_state, current_method->max_stack,
1022              current_method->max_locals);
1023     s.set_exception (t, current_method->max_stack);
1024     push_jump_merge (pc, &s);
1025   }
1026
1027   int pop_jump ()
1028   {
1029     int npc = next_verify_pc;
1030     if (npc != state::NO_NEXT)
1031       {
1032         next_verify_pc = states[npc]->next;
1033         states[npc]->next = state::INVALID;
1034       }
1035     return npc;
1036   }
1037
1038   void invalidate_pc ()
1039   {
1040     PC = state::NO_NEXT;
1041   }
1042
1043   void note_branch_target (int pc, bool is_jsr_target = false)
1044   {
1045     if (pc <= PC && ! (flags[pc] & FLAG_INSN_START))
1046       verify_fail ("branch not to instruction start");
1047     flags[pc] |= FLAG_BRANCH_TARGET;
1048     if (is_jsr_target)
1049       {
1050         // Record the jsr which called this instruction.
1051         subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1052         info->pc = PC;
1053         info->next = jsr_ptrs[pc];
1054         jsr_ptrs[pc] = info;
1055         flags[pc] |= FLAG_JSR_TARGET;
1056       }
1057   }
1058
1059   void skip_padding ()
1060   {
1061     while ((PC % 4) > 0)
1062       if (get_byte () != 0)
1063         verify_fail ("found nonzero padding byte");
1064   }
1065
1066   // Return the subroutine to which the instruction at PC belongs.
1067   int get_subroutine (int pc)
1068   {
1069     if (states[pc] == NULL)
1070       return 0;
1071     return states[pc]->subroutine;
1072   }
1073
1074   // Do the work for a `ret' instruction.  INDEX is the index into the
1075   // local variables.
1076   void handle_ret_insn (int index)
1077   {
1078     get_variable (index, return_address_type);
1079
1080     int csub = current_state->subroutine;
1081     if (csub == 0)
1082       verify_fail ("no subroutine");
1083
1084     for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1085       {
1086         // Temporarily modify the current state so it looks like we're
1087         // in the enclosing context.
1088         current_state->subroutine = get_subroutine (subr->pc);
1089         if (subr->pc < PC)
1090           current_state->check_no_uninitialized_objects (current_method->max_stack);
1091         push_jump_merge (subr->pc, current_state, true);
1092       }
1093
1094     current_state->subroutine = csub;
1095     invalidate_pc ();
1096   }
1097
1098   // We're in the subroutine SUB, calling a subroutine at DEST.  Make
1099   // sure this subroutine isn't already on the stack.
1100   void check_nonrecursive_call (int sub, int dest)
1101   {
1102     if (sub == 0)
1103       return;
1104     if (sub == dest)
1105       verify_fail ("recursive subroutine call");
1106     for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1107       check_nonrecursive_call (get_subroutine (info->pc), dest);
1108   }
1109
1110   void handle_jsr_insn (int offset)
1111   {
1112     int npc = compute_jump (offset);
1113
1114     if (npc < PC)
1115       current_state->check_no_uninitialized_objects (current_method->max_stack);
1116     check_nonrecursive_call (current_state->subroutine, npc);
1117
1118     // Temporarily modify the current state so that it looks like we are
1119     // in the subroutine.
1120     push_type (return_address_type);
1121     int save = current_state->subroutine;
1122     current_state->subroutine = npc;
1123
1124     // Merge into the subroutine.
1125     push_jump_merge (npc, current_state);
1126
1127     // Undo our modifications.
1128     current_state->subroutine = save;
1129     pop_type (return_address_type);
1130   }
1131
1132   jclass construct_primitive_array_type (type_val prim)
1133   {
1134     jclass k = NULL;
1135     switch (prim)
1136       {
1137       case boolean_type:
1138         k = JvPrimClass (boolean);
1139         break;
1140       case char_type:
1141         k = JvPrimClass (char);
1142         break;
1143       case float_type:
1144         k = JvPrimClass (float);
1145         break;
1146       case double_type:
1147         k = JvPrimClass (double);
1148         break;
1149       case byte_type:
1150         k = JvPrimClass (byte);
1151         break;
1152       case short_type:
1153         k = JvPrimClass (short);
1154         break;
1155       case int_type:
1156         k = JvPrimClass (int);
1157         break;
1158       case long_type:
1159         k = JvPrimClass (long);
1160         break;
1161       default:
1162         verify_fail ("unknown type in construct_primitive_array_type");
1163       }
1164     k = _Jv_GetArrayClass (k, NULL);
1165     return k;
1166   }
1167
1168   // This pass computes the location of branch targets and also
1169   // instruction starts.
1170   void branch_prepass ()
1171   {
1172     flags = (char *) _Jv_Malloc (current_method->code_length);
1173     jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1174                                           * current_method->code_length);
1175
1176     for (int i = 0; i < current_method->code_length; ++i)
1177       {
1178         flags[i] = 0;
1179         jsr_ptrs[i] = NULL;
1180       }
1181
1182     bool last_was_jsr = false;
1183
1184     PC = 0;
1185     while (PC < current_method->code_length)
1186       {
1187         flags[PC] |= FLAG_INSN_START;
1188
1189         // If the previous instruction was a jsr, then the next
1190         // instruction is a branch target -- the branch being the
1191         // corresponding `ret'.
1192         if (last_was_jsr)
1193           note_branch_target (PC);
1194         last_was_jsr = false;
1195
1196         start_PC = PC;
1197         unsigned char opcode = bytecode[PC++];
1198         switch (opcode)
1199           {
1200           case op_nop:
1201           case op_aconst_null:
1202           case op_iconst_m1:
1203           case op_iconst_0:
1204           case op_iconst_1:
1205           case op_iconst_2:
1206           case op_iconst_3:
1207           case op_iconst_4:
1208           case op_iconst_5:
1209           case op_lconst_0:
1210           case op_lconst_1:
1211           case op_fconst_0:
1212           case op_fconst_1:
1213           case op_fconst_2:
1214           case op_dconst_0:
1215           case op_dconst_1:
1216           case op_iload_0:
1217           case op_iload_1:
1218           case op_iload_2:
1219           case op_iload_3:
1220           case op_lload_0:
1221           case op_lload_1:
1222           case op_lload_2:
1223           case op_lload_3:
1224           case op_fload_0:
1225           case op_fload_1:
1226           case op_fload_2:
1227           case op_fload_3:
1228           case op_dload_0:
1229           case op_dload_1:
1230           case op_dload_2:
1231           case op_dload_3:
1232           case op_aload_0:
1233           case op_aload_1:
1234           case op_aload_2:
1235           case op_aload_3:
1236           case op_iaload:
1237           case op_laload:
1238           case op_faload:
1239           case op_daload:
1240           case op_aaload:
1241           case op_baload:
1242           case op_caload:
1243           case op_saload:
1244           case op_istore_0:
1245           case op_istore_1:
1246           case op_istore_2:
1247           case op_istore_3:
1248           case op_lstore_0:
1249           case op_lstore_1:
1250           case op_lstore_2:
1251           case op_lstore_3:
1252           case op_fstore_0:
1253           case op_fstore_1:
1254           case op_fstore_2:
1255           case op_fstore_3:
1256           case op_dstore_0:
1257           case op_dstore_1:
1258           case op_dstore_2:
1259           case op_dstore_3:
1260           case op_astore_0:
1261           case op_astore_1:
1262           case op_astore_2:
1263           case op_astore_3:
1264           case op_iastore:
1265           case op_lastore:
1266           case op_fastore:
1267           case op_dastore:
1268           case op_aastore:
1269           case op_bastore:
1270           case op_castore:
1271           case op_sastore:
1272           case op_pop:
1273           case op_pop2:
1274           case op_dup:
1275           case op_dup_x1:
1276           case op_dup_x2:
1277           case op_dup2:
1278           case op_dup2_x1:
1279           case op_dup2_x2:
1280           case op_swap:
1281           case op_iadd:
1282           case op_isub:
1283           case op_imul:
1284           case op_idiv:
1285           case op_irem:
1286           case op_ishl:
1287           case op_ishr:
1288           case op_iushr:
1289           case op_iand:
1290           case op_ior:
1291           case op_ixor:
1292           case op_ladd:
1293           case op_lsub:
1294           case op_lmul:
1295           case op_ldiv:
1296           case op_lrem:
1297           case op_lshl:
1298           case op_lshr:
1299           case op_lushr:
1300           case op_land:
1301           case op_lor:
1302           case op_lxor:
1303           case op_fadd:
1304           case op_fsub:
1305           case op_fmul:
1306           case op_fdiv:
1307           case op_frem:
1308           case op_dadd:
1309           case op_dsub:
1310           case op_dmul:
1311           case op_ddiv:
1312           case op_drem:
1313           case op_ineg:
1314           case op_i2b:
1315           case op_i2c:
1316           case op_i2s:
1317           case op_lneg:
1318           case op_fneg:
1319           case op_dneg:
1320           case op_iinc:
1321           case op_i2l:
1322           case op_i2f:
1323           case op_i2d:
1324           case op_l2i:
1325           case op_l2f:
1326           case op_l2d:
1327           case op_f2i:
1328           case op_f2l:
1329           case op_f2d:
1330           case op_d2i:
1331           case op_d2l:
1332           case op_d2f:
1333           case op_lcmp:
1334           case op_fcmpl:
1335           case op_fcmpg:
1336           case op_dcmpl:
1337           case op_dcmpg:
1338           case op_monitorenter:
1339           case op_monitorexit:
1340           case op_ireturn:
1341           case op_lreturn:
1342           case op_freturn:
1343           case op_dreturn:
1344           case op_areturn:
1345           case op_return:
1346           case op_athrow:
1347             break;
1348
1349           case op_bipush:
1350           case op_sipush:
1351           case op_ldc:
1352           case op_iload:
1353           case op_lload:
1354           case op_fload:
1355           case op_dload:
1356           case op_aload:
1357           case op_istore:
1358           case op_lstore:
1359           case op_fstore:
1360           case op_dstore:
1361           case op_astore:
1362           case op_arraylength:
1363           case op_ret:
1364             get_byte ();
1365             break;
1366
1367           case op_ldc_w:
1368           case op_ldc2_w:
1369           case op_getstatic:
1370           case op_getfield:
1371           case op_putfield:
1372           case op_putstatic:
1373           case op_new:
1374           case op_newarray:
1375           case op_anewarray:
1376           case op_instanceof:
1377           case op_checkcast:
1378           case op_invokespecial:
1379           case op_invokestatic:
1380           case op_invokevirtual:
1381             get_short ();
1382             break;
1383
1384           case op_multianewarray:
1385             get_short ();
1386             get_byte ();
1387             break;
1388
1389           case op_jsr:
1390             last_was_jsr = true;
1391             // Fall through.
1392           case op_ifeq:
1393           case op_ifne:
1394           case op_iflt:
1395           case op_ifge:
1396           case op_ifgt:
1397           case op_ifle:
1398           case op_if_icmpeq:
1399           case op_if_icmpne:
1400           case op_if_icmplt:
1401           case op_if_icmpge:
1402           case op_if_icmpgt:
1403           case op_if_icmple:
1404           case op_if_acmpeq:
1405           case op_if_acmpne:
1406           case op_ifnull:
1407           case op_ifnonnull:
1408           case op_goto:
1409             note_branch_target (compute_jump (get_short ()), last_was_jsr);
1410             break;
1411
1412           case op_tableswitch:
1413             {
1414               skip_padding ();
1415               note_branch_target (compute_jump (get_int ()));
1416               jint low = get_int ();
1417               jint hi = get_int ();
1418               if (low > hi)
1419                 verify_fail ("invalid tableswitch", start_PC);
1420               for (int i = low; i <= hi; ++i)
1421                 note_branch_target (compute_jump (get_int ()));
1422             }
1423             break;
1424
1425           case op_lookupswitch:
1426             {
1427               skip_padding ();
1428               note_branch_target (compute_jump (get_int ()));
1429               int npairs = get_int ();
1430               if (npairs < 0)
1431                 verify_fail ("too few pairs in lookupswitch", start_PC);
1432               while (npairs-- > 0)
1433                 {
1434                   get_int ();
1435                   note_branch_target (compute_jump (get_int ()));
1436                 }
1437             }
1438             break;
1439
1440           case op_invokeinterface:
1441             get_short ();
1442             get_byte ();
1443             get_byte ();
1444             break;
1445
1446           case op_wide:
1447             {
1448               opcode = get_byte ();
1449               get_short ();
1450               if (opcode == (unsigned char) op_iinc)
1451                 get_short ();
1452             }
1453             break;
1454
1455           case op_jsr_w:
1456             last_was_jsr = true;
1457             // Fall through.
1458           case op_goto_w:
1459             note_branch_target (compute_jump (get_int ()), last_was_jsr);
1460             break;
1461
1462           default:
1463             verify_fail ("unrecognized instruction in branch_prepass",
1464                          start_PC);
1465           }
1466
1467         // See if any previous branch tried to branch to the middle of
1468         // this instruction.
1469         for (int pc = start_PC + 1; pc < PC; ++pc)
1470           {
1471             if ((flags[pc] & FLAG_BRANCH_TARGET))
1472               verify_fail ("branch to middle of instruction", pc);
1473           }
1474       }
1475
1476     // Verify exception handlers.
1477     for (int i = 0; i < current_method->exc_count; ++i)
1478       {
1479         if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1480           verify_fail ("exception handler not at instruction start",
1481                        exception[i].handler_pc);
1482         if (exception[i].start_pc > exception[i].end_pc)
1483           verify_fail ("exception range inverted");
1484         if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
1485           verify_fail ("exception start not at instruction start",
1486                        exception[i].start_pc);
1487         else if (! (flags[exception[i].end_pc] & FLAG_INSN_START))
1488           verify_fail ("exception end not at instruction start",
1489                        exception[i].end_pc);
1490
1491         flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1492       }
1493   }
1494
1495   void check_pool_index (int index)
1496   {
1497     if (index < 0 || index >= current_class->constants.size)
1498       verify_fail ("constant pool index out of range", start_PC);
1499   }
1500
1501   type check_class_constant (int index)
1502   {
1503     check_pool_index (index);
1504     _Jv_Constants *pool = &current_class->constants;
1505     if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1506       return type (pool->data[index].clazz);
1507     else if (pool->tags[index] == JV_CONSTANT_Class)
1508       return type (pool->data[index].utf8);
1509     verify_fail ("expected class constant", start_PC);
1510   }
1511
1512   type check_constant (int index)
1513   {
1514     check_pool_index (index);
1515     _Jv_Constants *pool = &current_class->constants;
1516     if (pool->tags[index] == JV_CONSTANT_ResolvedString
1517         || pool->tags[index] == JV_CONSTANT_String)
1518       return type (&java::lang::String::class$);
1519     else if (pool->tags[index] == JV_CONSTANT_Integer)
1520       return type (int_type);
1521     else if (pool->tags[index] == JV_CONSTANT_Float)
1522       return type (float_type);
1523     verify_fail ("String, int, or float constant expected", start_PC);
1524   }
1525
1526   type check_wide_constant (int index)
1527   {
1528     check_pool_index (index);
1529     _Jv_Constants *pool = &current_class->constants;
1530     if (pool->tags[index] == JV_CONSTANT_Long)
1531       return type (long_type);
1532     else if (pool->tags[index] == JV_CONSTANT_Double)
1533       return type (double_type);
1534     verify_fail ("long or double constant expected", start_PC);
1535   }
1536
1537   // Helper for both field and method.  These are laid out the same in
1538   // the constant pool.
1539   type handle_field_or_method (int index, int expected,
1540                                _Jv_Utf8Const **name,
1541                                _Jv_Utf8Const **fmtype)
1542   {
1543     check_pool_index (index);
1544     _Jv_Constants *pool = &current_class->constants;
1545     if (pool->tags[index] != expected)
1546       verify_fail ("didn't see expected constant", start_PC);
1547     // Once we know we have a Fieldref or Methodref we assume that it
1548     // is correctly laid out in the constant pool.  I think the code
1549     // in defineclass.cc guarantees this.
1550     _Jv_ushort class_index, name_and_type_index;
1551     _Jv_loadIndexes (&pool->data[index],
1552                      class_index,
1553                      name_and_type_index);
1554     _Jv_ushort name_index, desc_index;
1555     _Jv_loadIndexes (&pool->data[name_and_type_index],
1556                      name_index, desc_index);
1557
1558     *name = pool->data[name_index].utf8;
1559     *fmtype = pool->data[desc_index].utf8;
1560
1561     return check_class_constant (class_index);
1562   }
1563
1564   // Return field's type, compute class' type if requested.
1565   type check_field_constant (int index, type *class_type = NULL)
1566   {
1567     _Jv_Utf8Const *name, *field_type;
1568     type ct = handle_field_or_method (index,
1569                                       JV_CONSTANT_Fieldref,
1570                                       &name, &field_type);
1571     if (class_type)
1572       *class_type = ct;
1573     if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1574       return type (field_type);
1575     return get_type_val_for_signature (field_type->data[0]);
1576   }
1577
1578   type check_method_constant (int index, bool is_interface,
1579                               _Jv_Utf8Const **method_name,
1580                               _Jv_Utf8Const **method_signature)
1581   {
1582     return handle_field_or_method (index,
1583                                    (is_interface
1584                                     ? JV_CONSTANT_InterfaceMethodref
1585                                     : JV_CONSTANT_Methodref),
1586                                    method_name, method_signature);
1587   }
1588
1589   type get_one_type (char *&p)
1590   {
1591     char *start = p;
1592
1593     int arraycount = 0;
1594     while (*p == '[')
1595       {
1596         ++arraycount;
1597         ++p;
1598       }
1599
1600     char v = *p++;
1601
1602     if (v == 'L')
1603       {
1604         while (*p != ';')
1605           ++p;
1606         ++p;
1607         // FIXME!  This will get collected!
1608         _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1609         return type (name);
1610       }
1611
1612     // Casting to jchar here is ok since we are looking at an ASCII
1613     // character.
1614     type_val rt = get_type_val_for_signature (jchar (v));
1615
1616     if (arraycount == 0)
1617       {
1618         // Callers of this function eventually push their arguments on
1619         // the stack.  So, promote them here.
1620         return type (rt).promote ();
1621       }
1622
1623     jclass k = construct_primitive_array_type (rt);
1624     while (--arraycount > 0)
1625       k = _Jv_GetArrayClass (k, NULL);
1626     return type (k);
1627   }
1628
1629   void compute_argument_types (_Jv_Utf8Const *signature,
1630                                type *types)
1631   {
1632     char *p = signature->data;
1633     // Skip `('.
1634     ++p;
1635
1636     int i = 0;
1637     while (*p != ')')
1638       types[i++] = get_one_type (p);
1639   }
1640
1641   type compute_return_type (_Jv_Utf8Const *signature)
1642   {
1643     char *p = signature->data;
1644     while (*p != ')')
1645       ++p;
1646     ++p;
1647     return get_one_type (p);
1648   }
1649
1650   void check_return_type (type onstack)
1651   {
1652     type rt = compute_return_type (current_method->self->signature);
1653     if (! rt.compatible (onstack))
1654       verify_fail ("incompatible return type", start_PC);
1655   }
1656
1657   void verify_instructions_0 ()
1658   {
1659     current_state = new state (current_method->max_stack,
1660                                current_method->max_locals);
1661
1662     PC = 0;
1663     start_PC = 0;
1664
1665     {
1666       int var = 0;
1667
1668       using namespace java::lang::reflect;
1669       if (! Modifier::isStatic (current_method->self->accflags))
1670         {
1671           type kurr (current_class);
1672           if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1673             kurr.set_uninitialized (type::SELF);
1674           set_variable (0, kurr);
1675           ++var;
1676         }
1677
1678       // We have to handle wide arguments specially here.
1679       int arg_count = _Jv_count_arguments (current_method->self->signature);
1680       type arg_types[arg_count];
1681       compute_argument_types (current_method->self->signature, arg_types);
1682       for (int i = 0; i < arg_count; ++i)
1683         {
1684           set_variable (var, arg_types[i]);
1685           ++var;
1686           if (arg_types[i].iswide ())
1687             ++var;
1688         }
1689     }
1690
1691     states = (state **) _Jv_Malloc (sizeof (state *)
1692                                     * current_method->code_length);
1693     for (int i = 0; i < current_method->code_length; ++i)
1694       states[i] = NULL;
1695
1696     next_verify_pc = state::NO_NEXT;
1697
1698     while (true)
1699       {
1700         // If the PC was invalidated, get a new one from the work list.
1701         if (PC == state::NO_NEXT)
1702           {
1703             PC = pop_jump ();
1704             if (PC == state::INVALID)
1705               verify_fail ("saw state::INVALID", start_PC);
1706             if (PC == state::NO_NEXT)
1707               break;
1708             // Set up the current state.
1709             *current_state = *states[PC];
1710           }
1711
1712         // Control can't fall off the end of the bytecode.
1713         if (PC >= current_method->code_length)
1714           verify_fail ("fell off end");
1715
1716         if (states[PC] != NULL)
1717           {
1718             // We've already visited this instruction.  So merge the
1719             // states together.  If this yields no change then we don't
1720             // have to re-verify.
1721             if (! current_state->merge (states[PC], false,
1722                                         current_method->max_stack))
1723               {
1724                 invalidate_pc ();
1725                 continue;
1726               }
1727             // Save a copy of it for later.
1728             states[PC]->copy (current_state, current_method->max_stack,
1729                               current_method->max_locals);
1730           }
1731         else if ((flags[PC] & FLAG_BRANCH_TARGET))
1732           {
1733             // We only have to keep saved state at branch targets.
1734             states[PC] = new state (current_state, current_method->max_stack,
1735                                     current_method->max_locals);
1736           }
1737
1738         // Update states for all active exception handlers.  Ordinarily
1739         // there are not many exception handlers.  So we simply run
1740         // through them all.
1741         for (int i = 0; i < current_method->exc_count; ++i)
1742           {
1743             if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
1744               {
1745                 type handler = reference_type;
1746                 if (exception[i].handler_type != 0)
1747                   handler = check_class_constant (exception[i].handler_type);
1748                 push_exception_jump (handler, exception[i].handler_pc);
1749               }
1750           }
1751
1752         start_PC = PC;
1753         unsigned char opcode = bytecode[PC++];
1754         switch (opcode)
1755           {
1756           case op_nop:
1757             break;
1758
1759           case op_aconst_null:
1760             push_type (null_type);
1761             break;
1762
1763           case op_iconst_m1:
1764           case op_iconst_0:
1765           case op_iconst_1:
1766           case op_iconst_2:
1767           case op_iconst_3:
1768           case op_iconst_4:
1769           case op_iconst_5:
1770             push_type (int_type);
1771             break;
1772
1773           case op_lconst_0:
1774           case op_lconst_1:
1775             push_type (long_type);
1776             break;
1777
1778           case op_fconst_0:
1779           case op_fconst_1:
1780           case op_fconst_2:
1781             push_type (float_type);
1782             break;
1783
1784           case op_dconst_0:
1785           case op_dconst_1:
1786             push_type (double_type);
1787             break;
1788
1789           case op_bipush:
1790             get_byte ();
1791             push_type (int_type);
1792             break;
1793
1794           case op_sipush:
1795             get_short ();
1796             push_type (int_type);
1797             break;
1798
1799           case op_ldc:
1800             push_type (check_constant (get_byte ()));
1801             break;
1802           case op_ldc_w:
1803             push_type (check_constant (get_ushort ()));
1804             break;
1805           case op_ldc2_w:
1806             push_type (check_wide_constant (get_ushort ()));
1807             break;
1808
1809           case op_iload:
1810             push_type (get_variable (get_byte (), int_type));
1811             break;
1812           case op_lload:
1813             push_type (get_variable (get_byte (), long_type));
1814             break;
1815           case op_fload:
1816             push_type (get_variable (get_byte (), float_type));
1817             break;
1818           case op_dload:
1819             push_type (get_variable (get_byte (), double_type));
1820             break;
1821           case op_aload:
1822             push_type (get_variable (get_byte (), reference_type));
1823             break;
1824
1825           case op_iload_0:
1826           case op_iload_1:
1827           case op_iload_2:
1828           case op_iload_3:
1829             push_type (get_variable (opcode - op_iload_0, int_type));
1830             break;
1831           case op_lload_0:
1832           case op_lload_1:
1833           case op_lload_2:
1834           case op_lload_3:
1835             push_type (get_variable (opcode - op_lload_0, long_type));
1836             break;
1837           case op_fload_0:
1838           case op_fload_1:
1839           case op_fload_2:
1840           case op_fload_3:
1841             push_type (get_variable (opcode - op_fload_0, float_type));
1842             break;
1843           case op_dload_0:
1844           case op_dload_1:
1845           case op_dload_2:
1846           case op_dload_3:
1847             push_type (get_variable (opcode - op_dload_0, double_type));
1848             break;
1849           case op_aload_0:
1850           case op_aload_1:
1851           case op_aload_2:
1852           case op_aload_3:
1853             push_type (get_variable (opcode - op_aload_0, reference_type));
1854             break;
1855           case op_iaload:
1856             pop_type (int_type);
1857             push_type (require_array_type (pop_type (reference_type),
1858                                            int_type));
1859             break;
1860           case op_laload:
1861             pop_type (int_type);
1862             push_type (require_array_type (pop_type (reference_type),
1863                                            long_type));
1864             break;
1865           case op_faload:
1866             pop_type (int_type);
1867             push_type (require_array_type (pop_type (reference_type),
1868                                            float_type));
1869             break;
1870           case op_daload:
1871             pop_type (int_type);
1872             push_type (require_array_type (pop_type (reference_type),
1873                                            double_type));
1874             break;
1875           case op_aaload:
1876             pop_type (int_type);
1877             push_type (require_array_type (pop_type (reference_type),
1878                                            reference_type));
1879             break;
1880           case op_baload:
1881             pop_type (int_type);
1882             require_array_type (pop_type (reference_type), byte_type);
1883             push_type (int_type);
1884             break;
1885           case op_caload:
1886             pop_type (int_type);
1887             require_array_type (pop_type (reference_type), char_type);
1888             push_type (int_type);
1889             break;
1890           case op_saload:
1891             pop_type (int_type);
1892             require_array_type (pop_type (reference_type), short_type);
1893             push_type (int_type);
1894             break;
1895           case op_istore:
1896             set_variable (get_byte (), pop_type (int_type));
1897             break;
1898           case op_lstore:
1899             set_variable (get_byte (), pop_type (long_type));
1900             break;
1901           case op_fstore:
1902             set_variable (get_byte (), pop_type (float_type));
1903             break;
1904           case op_dstore:
1905             set_variable (get_byte (), pop_type (double_type));
1906             break;
1907           case op_astore:
1908             set_variable (get_byte (), pop_type (reference_type));
1909             break;
1910           case op_istore_0:
1911           case op_istore_1:
1912           case op_istore_2:
1913           case op_istore_3:
1914             set_variable (opcode - op_istore_0, pop_type (int_type));
1915             break;
1916           case op_lstore_0:
1917           case op_lstore_1:
1918           case op_lstore_2:
1919           case op_lstore_3:
1920             set_variable (opcode - op_lstore_0, pop_type (long_type));
1921             break;
1922           case op_fstore_0:
1923           case op_fstore_1:
1924           case op_fstore_2:
1925           case op_fstore_3:
1926             set_variable (opcode - op_fstore_0, pop_type (float_type));
1927             break;
1928           case op_dstore_0:
1929           case op_dstore_1:
1930           case op_dstore_2:
1931           case op_dstore_3:
1932             set_variable (opcode - op_dstore_0, pop_type (double_type));
1933             break;
1934           case op_astore_0:
1935           case op_astore_1:
1936           case op_astore_2:
1937           case op_astore_3:
1938             set_variable (opcode - op_astore_0, pop_type (reference_type));
1939             break;
1940           case op_iastore:
1941             pop_type (int_type);
1942             pop_type (int_type);
1943             require_array_type (pop_type (reference_type), int_type);
1944             break;
1945           case op_lastore:
1946             pop_type (long_type);
1947             pop_type (int_type);
1948             require_array_type (pop_type (reference_type), long_type);
1949             break;
1950           case op_fastore:
1951             pop_type (float_type);
1952             pop_type (int_type);
1953             require_array_type (pop_type (reference_type), float_type);
1954             break;
1955           case op_dastore:
1956             pop_type (double_type);
1957             pop_type (int_type);
1958             require_array_type (pop_type (reference_type), double_type);
1959             break;
1960           case op_aastore:
1961             pop_type (reference_type);
1962             pop_type (int_type);
1963             require_array_type (pop_type (reference_type), reference_type);
1964             break;
1965           case op_bastore:
1966             pop_type (int_type);
1967             pop_type (int_type);
1968             require_array_type (pop_type (reference_type), byte_type);
1969             break;
1970           case op_castore:
1971             pop_type (int_type);
1972             pop_type (int_type);
1973             require_array_type (pop_type (reference_type), char_type);
1974             break;
1975           case op_sastore:
1976             pop_type (int_type);
1977             pop_type (int_type);
1978             require_array_type (pop_type (reference_type), short_type);
1979             break;
1980           case op_pop:
1981             pop32 ();
1982             break;
1983           case op_pop2:
1984             pop64 ();
1985             break;
1986           case op_dup:
1987             {
1988               type t = pop32 ();
1989               push_type (t);
1990               push_type (t);
1991             }
1992             break;
1993           case op_dup_x1:
1994             {
1995               type t1 = pop32 ();
1996               type t2 = pop32 ();
1997               push_type (t1);
1998               push_type (t2);
1999               push_type (t1);
2000             }
2001             break;
2002           case op_dup_x2:
2003             {
2004               type t1 = pop32 ();
2005               type t2 = pop_raw ();
2006               if (! t2.iswide ())
2007                 {
2008                   type t3 = pop32 ();
2009                   push_type (t1);
2010                   push_type (t3);
2011                 }
2012               else
2013                 push_type (t1);
2014               push_type (t2);
2015               push_type (t1);
2016             }
2017             break;
2018           case op_dup2:
2019             {
2020               type t = pop_raw ();
2021               if (! t.iswide ())
2022                 {
2023                   type t2 = pop32 ();
2024                   push_type (t2);
2025                   push_type (t);
2026                   push_type (t2);
2027                 }
2028               push_type (t);
2029             }
2030             break;
2031           case op_dup2_x1:
2032             {
2033               type t1 = pop_raw ();
2034               type t2 = pop32 ();
2035               if (! t1.iswide ())
2036                 {
2037                   type t3 = pop32 ();
2038                   push_type (t2);
2039                   push_type (t1);
2040                   push_type (t3);
2041                 }
2042               else
2043                 push_type (t1);
2044               push_type (t2);
2045               push_type (t1);
2046             }
2047             break;
2048           case op_dup2_x2:
2049             {
2050               // FIXME
2051               type t1 = pop_raw ();
2052               if (t1.iswide ())
2053                 {
2054                   type t2 = pop_raw ();
2055                   if (t2.iswide ())
2056                     {
2057                       push_type (t1);
2058                       push_type (t2);
2059                     }
2060                   else
2061                     {
2062                       type t3 = pop32 ();
2063                       push_type (t1);
2064                       push_type (t3);
2065                       push_type (t2);
2066                     }
2067                   push_type (t1);
2068                 }
2069               else
2070                 {
2071                   type t2 = pop32 ();
2072                   type t3 = pop_raw ();
2073                   if (t3.iswide ())
2074                     {
2075                       push_type (t2);
2076                       push_type (t1);
2077                     }
2078                   else
2079                     {
2080                       type t4 = pop32 ();
2081                       push_type (t2);
2082                       push_type (t1);
2083                       push_type (t4);
2084                     }
2085                   push_type (t3);
2086                   push_type (t2);
2087                   push_type (t1);
2088                 }
2089             }
2090             break;
2091           case op_swap:
2092             {
2093               type t1 = pop32 ();
2094               type t2 = pop32 ();
2095               push_type (t1);
2096               push_type (t2);
2097             }
2098             break;
2099           case op_iadd:
2100           case op_isub:
2101           case op_imul:
2102           case op_idiv:
2103           case op_irem:
2104           case op_ishl:
2105           case op_ishr:
2106           case op_iushr:
2107           case op_iand:
2108           case op_ior:
2109           case op_ixor:
2110             pop_type (int_type);
2111             push_type (pop_type (int_type));
2112             break;
2113           case op_ladd:
2114           case op_lsub:
2115           case op_lmul:
2116           case op_ldiv:
2117           case op_lrem:
2118           case op_lshl:
2119           case op_lshr:
2120           case op_lushr:
2121           case op_land:
2122           case op_lor:
2123           case op_lxor:
2124             pop_type (long_type);
2125             push_type (pop_type (long_type));
2126             break;
2127           case op_fadd:
2128           case op_fsub:
2129           case op_fmul:
2130           case op_fdiv:
2131           case op_frem:
2132             pop_type (float_type);
2133             push_type (pop_type (float_type));
2134             break;
2135           case op_dadd:
2136           case op_dsub:
2137           case op_dmul:
2138           case op_ddiv:
2139           case op_drem:
2140             pop_type (double_type);
2141             push_type (pop_type (double_type));
2142             break;
2143           case op_ineg:
2144           case op_i2b:
2145           case op_i2c:
2146           case op_i2s:
2147             push_type (pop_type (int_type));
2148             break;
2149           case op_lneg:
2150             push_type (pop_type (long_type));
2151             break;
2152           case op_fneg:
2153             push_type (pop_type (float_type));
2154             break;
2155           case op_dneg:
2156             push_type (pop_type (double_type));
2157             break;
2158           case op_iinc:
2159             get_variable (get_byte (), int_type);
2160             get_byte ();
2161             break;
2162           case op_i2l:
2163             pop_type (int_type);
2164             push_type (long_type);
2165             break;
2166           case op_i2f:
2167             pop_type (int_type);
2168             push_type (float_type);
2169             break;
2170           case op_i2d:
2171             pop_type (int_type);
2172             push_type (double_type);
2173             break;
2174           case op_l2i:
2175             pop_type (long_type);
2176             push_type (int_type);
2177             break;
2178           case op_l2f:
2179             pop_type (long_type);
2180             push_type (float_type);
2181             break;
2182           case op_l2d:
2183             pop_type (long_type);
2184             push_type (double_type);
2185             break;
2186           case op_f2i:
2187             pop_type (float_type);
2188             push_type (int_type);
2189             break;
2190           case op_f2l:
2191             pop_type (float_type);
2192             push_type (long_type);
2193             break;
2194           case op_f2d:
2195             pop_type (float_type);
2196             push_type (double_type);
2197             break;
2198           case op_d2i:
2199             pop_type (double_type);
2200             push_type (int_type);
2201             break;
2202           case op_d2l:
2203             pop_type (double_type);
2204             push_type (long_type);
2205             break;
2206           case op_d2f:
2207             pop_type (double_type);
2208             push_type (float_type);
2209             break;
2210           case op_lcmp:
2211             pop_type (long_type);
2212             pop_type (long_type);
2213             push_type (int_type);
2214             break;
2215           case op_fcmpl:
2216           case op_fcmpg:
2217             pop_type (float_type);
2218             pop_type (float_type);
2219             push_type (int_type);
2220             break;
2221           case op_dcmpl:
2222           case op_dcmpg:
2223             pop_type (double_type);
2224             pop_type (double_type);
2225             push_type (int_type);
2226             break;
2227           case op_ifeq:
2228           case op_ifne:
2229           case op_iflt:
2230           case op_ifge:
2231           case op_ifgt:
2232           case op_ifle:
2233             pop_type (int_type);
2234             push_jump (get_short ());
2235             break;
2236           case op_if_icmpeq:
2237           case op_if_icmpne:
2238           case op_if_icmplt:
2239           case op_if_icmpge:
2240           case op_if_icmpgt:
2241           case op_if_icmple:
2242             pop_type (int_type);
2243             pop_type (int_type);
2244             push_jump (get_short ());
2245             break;
2246           case op_if_acmpeq:
2247           case op_if_acmpne:
2248             pop_type (reference_type);
2249             pop_type (reference_type);
2250             push_jump (get_short ());
2251             break;
2252           case op_goto:
2253             push_jump (get_short ());
2254             invalidate_pc ();
2255             break;
2256           case op_jsr:
2257             handle_jsr_insn (get_short ());
2258             break;
2259           case op_ret:
2260             handle_ret_insn (get_byte ());
2261             break;
2262           case op_tableswitch:
2263             {
2264               pop_type (int_type);
2265               skip_padding ();
2266               push_jump (get_int ());
2267               jint low = get_int ();
2268               jint high = get_int ();
2269               // Already checked LOW -vs- HIGH.
2270               for (int i = low; i <= high; ++i)
2271                 push_jump (get_int ());
2272               invalidate_pc ();
2273             }
2274             break;
2275
2276           case op_lookupswitch:
2277             {
2278               pop_type (int_type);
2279               skip_padding ();
2280               push_jump (get_int ());
2281               jint npairs = get_int ();
2282               // Already checked NPAIRS >= 0.
2283               jint lastkey = 0;
2284               for (int i = 0; i < npairs; ++i)
2285                 {
2286                   jint key = get_int ();
2287                   if (i > 0 && key <= lastkey)
2288                     verify_fail ("lookupswitch pairs unsorted", start_PC);
2289                   lastkey = key;
2290                   push_jump (get_int ());
2291                 }
2292               invalidate_pc ();
2293             }
2294             break;
2295           case op_ireturn:
2296             check_return_type (pop_type (int_type));
2297             invalidate_pc ();
2298             break;
2299           case op_lreturn:
2300             check_return_type (pop_type (long_type));
2301             invalidate_pc ();
2302             break;
2303           case op_freturn:
2304             check_return_type (pop_type (float_type));
2305             invalidate_pc ();
2306             break;
2307           case op_dreturn:
2308             check_return_type (pop_type (double_type));
2309             invalidate_pc ();
2310             break;
2311           case op_areturn:
2312             check_return_type (pop_type (reference_type));
2313             invalidate_pc ();
2314             break;
2315           case op_return:
2316             check_return_type (void_type);
2317             invalidate_pc ();
2318             break;
2319           case op_getstatic:
2320             push_type (check_field_constant (get_ushort ()));
2321             break;
2322           case op_putstatic:
2323             pop_type (check_field_constant (get_ushort ()));
2324             break;
2325           case op_getfield:
2326             {
2327               type klass;
2328               type field = check_field_constant (get_ushort (), &klass);
2329               pop_type (klass);
2330               push_type (field);
2331             }
2332             break;
2333           case op_putfield:
2334             {
2335               type klass;
2336               type field = check_field_constant (get_ushort (), &klass);
2337               pop_type (field);
2338               pop_type (klass);
2339             }
2340             break;
2341
2342           case op_invokevirtual:
2343           case op_invokespecial:
2344           case op_invokestatic:
2345           case op_invokeinterface:
2346             {
2347               _Jv_Utf8Const *method_name, *method_signature;
2348               type class_type
2349                 = check_method_constant (get_ushort (),
2350                                          opcode == (unsigned char) op_invokeinterface,
2351                                          &method_name,
2352                                          &method_signature);
2353               int arg_count = _Jv_count_arguments (method_signature);
2354               if (opcode == (unsigned char) op_invokeinterface)
2355                 {
2356                   int nargs = get_byte ();
2357                   if (nargs == 0)
2358                     verify_fail ("too few arguments to invokeinterface",
2359                                  start_PC);
2360                   if (get_byte () != 0)
2361                     verify_fail ("invokeinterface dummy byte is wrong",
2362                                  start_PC);
2363                   if (nargs - 1 != arg_count)
2364                     verify_fail ("wrong argument count for invokeinterface",
2365                                  start_PC);
2366                 }
2367
2368               bool is_init = false;
2369               if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2370                 {
2371                   is_init = true;
2372                   if (opcode != (unsigned char) op_invokespecial)
2373                     verify_fail ("can't invoke <init>", start_PC);
2374                 }
2375               else if (method_name->data[0] == '<')
2376                 verify_fail ("can't invoke method starting with `<'",
2377                              start_PC);
2378
2379               // Pop arguments and check types.
2380               type arg_types[arg_count];
2381               compute_argument_types (method_signature, arg_types);
2382               for (int i = arg_count - 1; i >= 0; --i)
2383                 pop_type (arg_types[i]);
2384
2385               if (opcode != (unsigned char) op_invokestatic)
2386                 {
2387                   type t = class_type;
2388                   if (is_init)
2389                     {
2390                       // In this case the PC doesn't matter.
2391                       t.set_uninitialized (type::UNINIT);
2392                     }
2393                   t = pop_type (t);
2394                   if (is_init)
2395                     current_state->set_initialized (t.get_pc (),
2396                                                     current_method->max_locals);
2397                 }
2398
2399               type rt = compute_return_type (method_signature);
2400               if (! rt.isvoid ())
2401                 push_type (rt);
2402             }
2403             break;
2404
2405           case op_new:
2406             {
2407               type t = check_class_constant (get_ushort ());
2408               if (t.isarray () || t.isinterface () || t.isabstract ())
2409                 verify_fail ("type is array, interface, or abstract",
2410                              start_PC);
2411               t.set_uninitialized (start_PC);
2412               push_type (t);
2413             }
2414             break;
2415
2416           case op_newarray:
2417             {
2418               int atype = get_byte ();
2419               // We intentionally have chosen constants to make this
2420               // valid.
2421               if (atype < boolean_type || atype > long_type)
2422                 verify_fail ("type not primitive", start_PC);
2423               pop_type (int_type);
2424               push_type (construct_primitive_array_type (type_val (atype)));
2425             }
2426             break;
2427           case op_anewarray:
2428             pop_type (int_type);
2429             push_type (check_class_constant (get_ushort ()));
2430             break;
2431           case op_arraylength:
2432             {
2433               type t = pop_type (reference_type);
2434               if (! t.isarray ())
2435                 verify_fail ("array type expected", start_PC);
2436               push_type (int_type);
2437             }
2438             break;
2439           case op_athrow:
2440             pop_type (type (&java::lang::Throwable::class$));
2441             invalidate_pc ();
2442             break;
2443           case op_checkcast:
2444             pop_type (reference_type);
2445             push_type (check_class_constant (get_ushort ()));
2446             break;
2447           case op_instanceof:
2448             pop_type (reference_type);
2449             check_class_constant (get_ushort ());
2450             push_type (int_type);
2451             break;
2452           case op_monitorenter:
2453             pop_type (reference_type);
2454             break;
2455           case op_monitorexit:
2456             pop_type (reference_type);
2457             break;
2458           case op_wide:
2459             {
2460               switch (get_byte ())
2461                 {
2462                 case op_iload:
2463                   push_type (get_variable (get_ushort (), int_type));
2464                   break;
2465                 case op_lload:
2466                   push_type (get_variable (get_ushort (), long_type));
2467                   break;
2468                 case op_fload:
2469                   push_type (get_variable (get_ushort (), float_type));
2470                   break;
2471                 case op_dload:
2472                   push_type (get_variable (get_ushort (), double_type));
2473                   break;
2474                 case op_aload:
2475                   push_type (get_variable (get_ushort (), reference_type));
2476                   break;
2477                 case op_istore:
2478                   set_variable (get_ushort (), pop_type (int_type));
2479                   break;
2480                 case op_lstore:
2481                   set_variable (get_ushort (), pop_type (long_type));
2482                   break;
2483                 case op_fstore:
2484                   set_variable (get_ushort (), pop_type (float_type));
2485                   break;
2486                 case op_dstore:
2487                   set_variable (get_ushort (), pop_type (double_type));
2488                   break;
2489                 case op_astore:
2490                   set_variable (get_ushort (), pop_type (reference_type));
2491                   break;
2492                 case op_ret:
2493                   handle_ret_insn (get_short ());
2494                   break;
2495                 case op_iinc:
2496                   get_variable (get_ushort (), int_type);
2497                   get_short ();
2498                   break;
2499                 default:
2500                   verify_fail ("unrecognized wide instruction", start_PC);
2501                 }
2502             }
2503             break;
2504           case op_multianewarray:
2505             {
2506               type atype = check_class_constant (get_ushort ());
2507               int dim = get_byte ();
2508               if (dim < 1)
2509                 verify_fail ("too few dimensions to multianewarray", start_PC);
2510               atype.verify_dimensions (dim);
2511               for (int i = 0; i < dim; ++i)
2512                 pop_type (int_type);
2513               push_type (atype);
2514             }
2515             break;
2516           case op_ifnull:
2517           case op_ifnonnull:
2518             pop_type (reference_type);
2519             push_jump (get_short ());
2520             break;
2521           case op_goto_w:
2522             push_jump (get_int ());
2523             invalidate_pc ();
2524             break;
2525           case op_jsr_w:
2526             handle_jsr_insn (get_int ());
2527             break;
2528
2529           default:
2530             // Unrecognized opcode.
2531             verify_fail ("unrecognized instruction in verify_instructions_0",
2532                          start_PC);
2533           }
2534       }
2535   }
2536
2537 public:
2538
2539   void verify_instructions ()
2540   {
2541     branch_prepass ();
2542     verify_instructions_0 ();
2543   }
2544
2545   _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2546   {
2547     current_method = m;
2548     bytecode = m->bytecode ();
2549     exception = m->exceptions ();
2550     current_class = m->defining_class;
2551
2552     states = NULL;
2553     flags = NULL;
2554     jsr_ptrs = NULL;
2555   }
2556
2557   ~_Jv_BytecodeVerifier ()
2558   {
2559     if (states)
2560       _Jv_Free (states);
2561     if (flags)
2562       _Jv_Free (flags);
2563     if (jsr_ptrs)
2564       _Jv_Free (jsr_ptrs);
2565   }
2566 };
2567
2568 void
2569 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2570 {
2571   _Jv_BytecodeVerifier v (meth);
2572   v.verify_instructions ();
2573 }
2574
2575 // FIXME: add more info, like PC, when required.
2576 static void
2577 verify_fail (char *s, jint pc)
2578 {
2579   using namespace java::lang;
2580   StringBuffer *buf = new StringBuffer ();
2581
2582   buf->append (JvNewStringLatin1 ("verification failed"));
2583   if (pc != -1)
2584     {
2585       buf->append (JvNewStringLatin1 (" at PC "));
2586       buf->append (pc);
2587     }
2588   buf->append (JvNewStringLatin1 (": "));
2589   buf->append (JvNewStringLatin1 (s));
2590   throw new java::lang::VerifyError (buf->toString ());
2591 }
2592
2593 #endif  /* INTERPRETER */