OSDN Git Service

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