OSDN Git Service

* prims.cc (next_property_value): Never return NULL.
[pf3gnuchains/gcc-fork.git] / libjava / interpret.cc
1 // interpret.cc - Code for the interpreter
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 /* Author: Kresten Krab Thorup <krab@gnu.org>  */
12
13 #include <config.h>
14 #include <platform.h>
15
16 #pragma implementation "java-interp.h"
17
18 #include <jvm.h>
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/VirtualMachineError.h>
29 #include <java/lang/InternalError.h>
30 #include <java/lang/NullPointerException.h>
31 #include <java/lang/ArithmeticException.h>
32 #include <java/lang/IncompatibleClassChangeError.h>
33 #include <java/lang/InstantiationException.h>
34 #include <java/lang/Thread.h>
35 #include <java-insns.h>
36 #include <java-signal.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <execution.h>
39 #include <java/lang/reflect/Modifier.h>
40
41 #ifdef INTERPRETER
42
43 // Execution engine for interpreted code.
44 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
45
46 #include <stdlib.h>
47
48 using namespace gcj;
49
50 static void throw_internal_error (char *msg)
51   __attribute__ ((__noreturn__));
52 static void throw_incompatible_class_change_error (jstring msg)
53   __attribute__ ((__noreturn__));
54 #ifndef HANDLE_SEGV
55 static void throw_null_pointer_exception ()
56   __attribute__ ((__noreturn__));
57 #endif
58
59 static void throw_class_format_error (jstring msg)
60         __attribute__ ((__noreturn__));
61 static void throw_class_format_error (char *msg)
62         __attribute__ ((__noreturn__));
63
64 #ifdef DIRECT_THREADED
65 // Lock to ensure that methods are not compiled concurrently.
66 // We could use a finer-grained lock here, however it is not safe to use
67 // the Class monitor as user code in another thread could hold it.
68 static _Jv_Mutex_t compile_mutex;
69
70 void
71 _Jv_InitInterpreter()
72 {
73   _Jv_MutexInit (&compile_mutex);
74 }
75 #else
76 void _Jv_InitInterpreter() {}
77 #endif
78
79 extern "C" double __ieee754_fmod (double,double);
80
81 static inline void dupx (_Jv_word *sp, int n, int x)
82 {
83   // first "slide" n+x elements n to the right
84   int top = n-1;
85   for (int i = 0; i < n+x; i++)
86     {
87       sp[(top-i)] = sp[(top-i)-n];
88     }
89   
90   // next, copy the n top elements, n+x down
91   for (int i = 0; i < n; i++)
92     {
93       sp[top-(n+x)-i] = sp[top-i];
94     }
95 }
96
97 // Used to convert from floating types to integral types.
98 template<typename TO, typename FROM>
99 static inline TO
100 convert (FROM val, TO min, TO max)
101 {
102   TO ret;
103   if (val >= (FROM) max)
104     ret = max;
105   else if (val <= (FROM) min)
106     ret = min;
107   else if (val != val)
108     ret = 0;
109   else
110     ret = (TO) val;
111   return ret;
112 }
113
114 #define PUSHA(V)  (sp++)->o = (V)
115 #define PUSHI(V)  (sp++)->i = (V)
116 #define PUSHF(V)  (sp++)->f = (V)
117 #if SIZEOF_VOID_P == 8
118 # define PUSHL(V)   (sp->l = (V), sp += 2)
119 # define PUSHD(V)   (sp->d = (V), sp += 2)
120 #else
121 # define PUSHL(V)  do { _Jv_word2 w2; w2.l=(V); \
122                         (sp++)->ia[0] = w2.ia[0]; \
123                         (sp++)->ia[0] = w2.ia[1]; } while (0)
124 # define PUSHD(V)  do { _Jv_word2 w2; w2.d=(V); \
125                         (sp++)->ia[0] = w2.ia[0]; \
126                         (sp++)->ia[0] = w2.ia[1]; } while (0)
127 #endif
128
129 #define POPA()    ((--sp)->o)
130 #define POPI()    ((jint) (--sp)->i) // cast since it may be promoted
131 #define POPF()    ((jfloat) (--sp)->f)
132 #if SIZEOF_VOID_P == 8
133 # define POPL()   (sp -= 2, (jlong) sp->l)
134 # define POPD()   (sp -= 2, (jdouble) sp->d)
135 #else
136 # define POPL()    ({ _Jv_word2 w2; \
137                      w2.ia[1] = (--sp)->ia[0]; \
138                      w2.ia[0] = (--sp)->ia[0]; w2.l; })
139 # define POPD()    ({ _Jv_word2 w2; \
140                      w2.ia[1] = (--sp)->ia[0]; \
141                      w2.ia[0] = (--sp)->ia[0]; w2.d; })
142 #endif
143
144 #define LOADA(I)  (sp++)->o = locals[I].o
145 #define LOADI(I)  (sp++)->i = locals[I].i
146 #define LOADF(I)  (sp++)->f = locals[I].f
147 #if SIZEOF_VOID_P == 8
148 # define LOADL(I)  (sp->l = locals[I].l, sp += 2)
149 # define LOADD(I)  (sp->d = locals[I].d, sp += 2)
150 #else
151 # define LOADL(I)  do { jint __idx = (I); \
152                         (sp++)->ia[0] = locals[__idx].ia[0]; \
153                         (sp++)->ia[0] = locals[__idx+1].ia[0]; \
154                    } while (0)
155 # define LOADD(I)  LOADL(I)
156 #endif
157
158 #define STOREA(I) locals[I].o = (--sp)->o
159 #define STOREI(I) locals[I].i = (--sp)->i
160 #define STOREF(I) locals[I].f = (--sp)->f
161 #if SIZEOF_VOID_P == 8
162 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
163 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
164 #else
165 # define STOREL(I) do { jint __idx = (I); \
166                        locals[__idx+1].ia[0] = (--sp)->ia[0]; \
167                        locals[__idx].ia[0] = (--sp)->ia[0]; \
168                    } while (0)
169 # define STORED(I) STOREL(I)
170 #endif
171
172 #define PEEKI(I)  (locals+(I))->i
173 #define PEEKA(I)  (locals+(I))->o
174
175 #define POKEI(I,V)  ((locals+(I))->i = (V))
176
177
178 #define BINOPI(OP) { \
179    jint value2 = POPI(); \
180    jint value1 = POPI(); \
181    PUSHI(value1 OP value2); \
182 }
183
184 #define BINOPF(OP) { \
185    jfloat value2 = POPF(); \
186    jfloat value1 = POPF(); \
187    PUSHF(value1 OP value2); \
188 }
189
190 #define BINOPL(OP) { \
191    jlong value2 = POPL(); \
192    jlong value1 = POPL(); \
193    PUSHL(value1 OP value2); \
194 }
195
196 #define BINOPD(OP) { \
197    jdouble value2 = POPD(); \
198    jdouble value1 = POPD(); \
199    PUSHD(value1 OP value2); \
200 }
201
202 static inline jint get1s(unsigned char* loc) {
203   return *(signed char*)loc;
204 }
205
206 static inline jint get1u(unsigned char* loc) {
207   return *loc;
208 }
209
210 static inline jint get2s(unsigned char* loc) {
211   return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
212 }
213
214 static inline jint get2u(unsigned char* loc) {
215   return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
216 }
217
218 static jint get4(unsigned char* loc) {
219   return (((jint)(loc[0])) << 24) 
220        | (((jint)(loc[1])) << 16) 
221        | (((jint)(loc[2])) << 8) 
222        | (((jint)(loc[3])) << 0);
223 }
224
225 #define SAVE_PC() frame_desc.pc = pc
226
227 #ifdef HANDLE_SEGV
228 #define NULLCHECK(X) SAVE_PC()
229 #define NULLARRAYCHECK(X) SAVE_PC()
230 #else
231 #define NULLCHECK(X) \
232   do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
233 #define NULLARRAYCHECK(X) \
234   do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
235 #endif
236
237 #define ARRAYBOUNDSCHECK(array, index)                                        \
238   do                                                                          \
239     {                                                                         \
240       if (((unsigned) index) >= (unsigned) (array->length))                   \
241         _Jv_ThrowBadArrayIndex (index);                                       \
242     }                                                                         \
243   while (0)
244
245 void
246 _Jv_InterpMethod::run_normal (ffi_cif *,
247                               void* ret,
248                               ffi_raw * args,
249                               void* __this)
250 {
251   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
252   run (ret, args, _this);
253 }
254
255 void
256 _Jv_InterpMethod::run_synch_object (ffi_cif *,
257                                     void* ret,
258                                     ffi_raw * args,
259                                     void* __this)
260 {
261   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
262
263   jobject rcv = (jobject) args[0].ptr;
264   JvSynchronize mutex (rcv);
265
266   run (ret, args, _this);
267 }
268
269 void
270 _Jv_InterpMethod::run_class (ffi_cif *,
271                              void* ret,
272                              ffi_raw * args,
273                              void* __this)
274 {
275   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
276   _Jv_InitClass (_this->defining_class);
277   run (ret, args, _this);
278 }
279
280 void
281 _Jv_InterpMethod::run_synch_class (ffi_cif *,
282                                    void* ret,
283                                    ffi_raw * args,
284                                    void* __this)
285 {
286   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
287
288   jclass sync = _this->defining_class;
289   _Jv_InitClass (sync);
290   JvSynchronize mutex (sync);
291
292   run (ret, args, _this);
293 }
294
295 #ifdef DIRECT_THREADED
296 // "Compile" a method by turning it from bytecode to direct-threaded
297 // code.
298 void
299 _Jv_InterpMethod::compile (const void * const *insn_targets)
300 {
301   insn_slot *insns = NULL;
302   int next = 0;
303   unsigned char *codestart = bytecode ();
304   unsigned char *end = codestart + code_length;
305   _Jv_word *pool_data = defining_class->constants.data;
306
307 #define SET_ONE(Field, Value)                                                 \
308   do                                                                          \
309     {                                                                         \
310       if (first_pass)                                                         \
311         ++next;                                                               \
312       else                                                                    \
313         insns[next++].Field = Value;                                          \
314     }                                                                         \
315   while (0)
316
317 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
318 #define SET_INT(Value) SET_ONE (int_val, Value)
319 #define SET_DATUM(Value) SET_ONE (datum, Value)
320
321   // Map from bytecode PC to slot in INSNS.
322   int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
323   for (int i = 0; i < code_length; ++i)
324     pc_mapping[i] = -1;
325
326   for (int i = 0; i < 2; ++i)
327     {
328       jboolean first_pass = i == 0;
329
330       if (! first_pass)
331         {
332           insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
333           next = 0;
334         }
335
336       unsigned char *pc = codestart;
337       while (pc < end)
338         {
339           int base_pc_val = pc - codestart;
340           if (first_pass)
341             pc_mapping[base_pc_val] = next;
342
343           java_opcode opcode = (java_opcode) *pc++;
344           // Just elide NOPs.
345           if (opcode == op_nop)
346             continue;
347           SET_INSN (insn_targets[opcode]);
348
349           switch (opcode)
350             {
351             case op_nop:
352             case op_aconst_null:
353             case op_iconst_m1:
354             case op_iconst_0:
355             case op_iconst_1:
356             case op_iconst_2:
357             case op_iconst_3:
358             case op_iconst_4:
359             case op_iconst_5:
360             case op_lconst_0:
361             case op_lconst_1:
362             case op_fconst_0:
363             case op_fconst_1:
364             case op_fconst_2:
365             case op_dconst_0:
366             case op_dconst_1:
367             case op_iload_0:
368             case op_iload_1:
369             case op_iload_2:
370             case op_iload_3:
371             case op_lload_0:
372             case op_lload_1:
373             case op_lload_2:
374             case op_lload_3:
375             case op_fload_0:
376             case op_fload_1:
377             case op_fload_2:
378             case op_fload_3:
379             case op_dload_0:
380             case op_dload_1:
381             case op_dload_2:
382             case op_dload_3:
383             case op_aload_0:
384             case op_aload_1:
385             case op_aload_2:
386             case op_aload_3:
387             case op_iaload:
388             case op_laload:
389             case op_faload:
390             case op_daload:
391             case op_aaload:
392             case op_baload:
393             case op_caload:
394             case op_saload:
395             case op_istore_0:
396             case op_istore_1:
397             case op_istore_2:
398             case op_istore_3:
399             case op_lstore_0:
400             case op_lstore_1:
401             case op_lstore_2:
402             case op_lstore_3:
403             case op_fstore_0:
404             case op_fstore_1:
405             case op_fstore_2:
406             case op_fstore_3:
407             case op_dstore_0:
408             case op_dstore_1:
409             case op_dstore_2:
410             case op_dstore_3:
411             case op_astore_0:
412             case op_astore_1:
413             case op_astore_2:
414             case op_astore_3:
415             case op_iastore:
416             case op_lastore:
417             case op_fastore:
418             case op_dastore:
419             case op_aastore:
420             case op_bastore:
421             case op_castore:
422             case op_sastore:
423             case op_pop:
424             case op_pop2:
425             case op_dup:
426             case op_dup_x1:
427             case op_dup_x2:
428             case op_dup2:
429             case op_dup2_x1:
430             case op_dup2_x2:
431             case op_swap:
432             case op_iadd:
433             case op_isub:
434             case op_imul:
435             case op_idiv:
436             case op_irem:
437             case op_ishl:
438             case op_ishr:
439             case op_iushr:
440             case op_iand:
441             case op_ior:
442             case op_ixor:
443             case op_ladd:
444             case op_lsub:
445             case op_lmul:
446             case op_ldiv:
447             case op_lrem:
448             case op_lshl:
449             case op_lshr:
450             case op_lushr:
451             case op_land:
452             case op_lor:
453             case op_lxor:
454             case op_fadd:
455             case op_fsub:
456             case op_fmul:
457             case op_fdiv:
458             case op_frem:
459             case op_dadd:
460             case op_dsub:
461             case op_dmul:
462             case op_ddiv:
463             case op_drem:
464             case op_ineg:
465             case op_i2b:
466             case op_i2c:
467             case op_i2s:
468             case op_lneg:
469             case op_fneg:
470             case op_dneg:
471             case op_i2l:
472             case op_i2f:
473             case op_i2d:
474             case op_l2i:
475             case op_l2f:
476             case op_l2d:
477             case op_f2i:
478             case op_f2l:
479             case op_f2d:
480             case op_d2i:
481             case op_d2l:
482             case op_d2f:
483             case op_lcmp:
484             case op_fcmpl:
485             case op_fcmpg:
486             case op_dcmpl:
487             case op_dcmpg:
488             case op_monitorenter:
489             case op_monitorexit:
490             case op_ireturn:
491             case op_lreturn:
492             case op_freturn:
493             case op_dreturn:
494             case op_areturn:
495             case op_return:
496             case op_athrow:
497             case op_arraylength:
498               // No argument, nothing else to do.
499               break;
500
501             case op_bipush:
502               SET_INT (get1s (pc));
503               ++pc;
504               break;
505
506             case op_ldc:
507               {
508                 int index = get1u (pc);
509                 ++pc;
510                 // For an unresolved class we want to delay resolution
511                 // until execution.
512                 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
513                   {
514                     --next;
515                     SET_INSN (insn_targets[int (op_jsr_w) + 1]);
516                     SET_INT (index);
517                   }
518                 else
519                   SET_DATUM (pool_data[index].o);
520               }
521               break;
522
523             case op_ret:
524             case op_iload:
525             case op_lload:
526             case op_fload:
527             case op_dload:
528             case op_aload:
529             case op_istore:
530             case op_lstore:
531             case op_fstore:
532             case op_dstore:
533             case op_astore:
534             case op_newarray:
535               SET_INT (get1u (pc));
536               ++pc;
537               break;
538
539             case op_iinc:
540               SET_INT (get1u (pc));
541               SET_INT (get1s (pc + 1));
542               pc += 2;
543               break;
544
545             case op_ldc_w:
546               {
547                 int index = get2u (pc);
548                 pc += 2;
549                 // For an unresolved class we want to delay resolution
550                 // until execution.
551                 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
552                   {
553                     --next;
554                     SET_INSN (insn_targets[int (op_jsr_w) + 1]);
555                     SET_INT (index);
556                   }
557                 else
558                   SET_DATUM (pool_data[index].o);
559               }
560               break;
561
562             case op_ldc2_w:
563               {
564                 int index = get2u (pc);
565                 pc += 2;
566                 SET_DATUM (&pool_data[index]);
567               }
568               break;
569
570             case op_sipush:
571               SET_INT (get2s (pc));
572               pc += 2;
573               break;
574
575             case op_new:
576             case op_getstatic:
577             case op_getfield:
578             case op_putfield:
579             case op_putstatic:
580             case op_anewarray:
581             case op_instanceof:
582             case op_checkcast:
583             case op_invokespecial:
584             case op_invokestatic:
585             case op_invokevirtual:
586               SET_INT (get2u (pc));
587               pc += 2;
588               break;
589
590             case op_multianewarray:
591               SET_INT (get2u (pc));
592               SET_INT (get1u (pc + 2));
593               pc += 3;
594               break;
595
596             case op_jsr:
597             case op_ifeq:
598             case op_ifne:
599             case op_iflt:
600             case op_ifge:
601             case op_ifgt:
602             case op_ifle:
603             case op_if_icmpeq:
604             case op_if_icmpne:
605             case op_if_icmplt:
606             case op_if_icmpge:
607             case op_if_icmpgt:
608             case op_if_icmple:
609             case op_if_acmpeq:
610             case op_if_acmpne:
611             case op_ifnull:
612             case op_ifnonnull:
613             case op_goto:
614               {
615                 int offset = get2s (pc);
616                 pc += 2;
617
618                 int new_pc = base_pc_val + offset;
619
620                 bool orig_was_goto = opcode == op_goto;
621
622                 // Thread jumps.  We limit the loop count; this lets
623                 // us avoid infinite loops if the bytecode contains
624                 // such.  `10' is arbitrary.
625                 int count = 10;
626                 while (codestart[new_pc] == op_goto && count-- > 0)
627                   new_pc += get2s (&codestart[new_pc + 1]);
628
629                 // If the jump takes us to a `return' instruction and
630                 // the original branch was an unconditional goto, then
631                 // we hoist the return.
632                 opcode = (java_opcode) codestart[new_pc];
633                 if (orig_was_goto
634                     && (opcode == op_ireturn || opcode == op_lreturn
635                         || opcode == op_freturn || opcode == op_dreturn
636                         || opcode == op_areturn || opcode == op_return))
637                   {
638                     --next;
639                     SET_INSN (insn_targets[opcode]);
640                   }
641                 else
642                   SET_DATUM (&insns[pc_mapping[new_pc]]);
643               }
644               break;
645
646             case op_tableswitch:
647               {
648                 while ((pc - codestart) % 4 != 0)
649                   ++pc;
650
651                 jint def = get4 (pc);
652                 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
653                 pc += 4;
654
655                 int low = get4 (pc);
656                 SET_INT (low);
657                 pc += 4;
658                 int high = get4 (pc);
659                 SET_INT (high);
660                 pc += 4;
661
662                 for (int i = low; i <= high; ++i)
663                   {
664                     SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
665                     pc += 4;
666                   }
667               }
668               break;
669
670             case op_lookupswitch:
671               {
672                 while ((pc - codestart) % 4 != 0)
673                   ++pc;
674
675                 jint def = get4 (pc);
676                 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
677                 pc += 4;
678
679                 jint npairs = get4 (pc);
680                 pc += 4;
681                 SET_INT (npairs);
682
683                 while (npairs-- > 0)
684                   {
685                     jint match = get4 (pc);
686                     jint offset = get4 (pc + 4);
687                     SET_INT (match);
688                     SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
689                     pc += 8;
690                   }
691               }
692               break;
693
694             case op_invokeinterface:
695               {
696                 jint index = get2u (pc);
697                 pc += 2;
698                 // We ignore the next two bytes.
699                 pc += 2;
700                 SET_INT (index);
701               }
702               break;
703
704             case op_wide:
705               {
706                 opcode = (java_opcode) get1u (pc);
707                 pc += 1;
708                 jint val = get2u (pc);
709                 pc += 2;
710
711                 // We implement narrow and wide instructions using the
712                 // same code in the interpreter.  So we rewrite the
713                 // instruction slot here.
714                 if (! first_pass)
715                   insns[next - 1].insn = (void *) insn_targets[opcode];
716                 SET_INT (val);
717
718                 if (opcode == op_iinc)
719                   {
720                     SET_INT (get2s (pc));
721                     pc += 2;
722                   }
723               }
724               break;
725
726             case op_jsr_w:
727             case op_goto_w:
728               {
729                 jint offset = get4 (pc);
730                 pc += 4;
731                 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
732               }
733               break;
734
735             // Some "can't happen" cases that we include for
736             // error-checking purposes.
737             case op_putfield_1:
738             case op_putfield_2:
739             case op_putfield_4:
740             case op_putfield_8:
741             case op_putfield_a:
742             case op_putstatic_1:
743             case op_putstatic_2:
744             case op_putstatic_4:
745             case op_putstatic_8:
746             case op_putstatic_a:
747             case op_getfield_1:
748             case op_getfield_2s:
749             case op_getfield_2u:
750             case op_getfield_4:
751             case op_getfield_8:
752             case op_getfield_a:
753             case op_getstatic_1:
754             case op_getstatic_2s:
755             case op_getstatic_2u:
756             case op_getstatic_4:
757             case op_getstatic_8:
758             case op_getstatic_a:
759             default:
760               // Fail somehow.
761               break;
762             }
763         }
764     }
765
766   // Now update exceptions.
767   _Jv_InterpException *exc = exceptions ();
768   for (int i = 0; i < exc_count; ++i)
769     {
770       exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
771       exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
772       exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
773       jclass handler
774         = (_Jv_Linker::resolve_pool_entry (defining_class,
775                                              exc[i].handler_type.i)).clazz;
776       exc[i].handler_type.p = handler;
777     }
778
779   // Translate entries in the LineNumberTable from bytecode PC's to direct
780   // threaded interpreter instruction values.
781   for (int i = 0; i < line_table_len; i++)
782     {
783       int byte_pc = line_table[i].bytecode_pc;
784       // It isn't worth throwing an exception if this table is
785       // corrupted, but at the same time we don't want a crash.
786       if (byte_pc < 0 || byte_pc >= code_length)
787         byte_pc = 0;
788       line_table[i].pc = &insns[pc_mapping[byte_pc]];
789     }  
790
791   prepared = insns;
792 }
793 #endif /* DIRECT_THREADED */
794
795 /* Run the given method.
796    When args is NULL, don't run anything -- just compile it. */
797 void
798 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
799 {
800   using namespace java::lang::reflect;
801
802   // FRAME_DESC registers this particular invocation as the top-most
803   // interpreter frame.  This lets the stack tracing code (for
804   // Throwable) print information about the method being interpreted
805   // rather than about the interpreter itself.  FRAME_DESC has a
806   // destructor so it cleans up automatically when the interpreter
807   // returns.
808   java::lang::Thread *thread = java::lang::Thread::currentThread();
809   _Jv_InterpFrame frame_desc (meth,
810                               (_Jv_InterpFrame **) &thread->interp_frame);
811
812   _Jv_word stack[meth->max_stack];
813   _Jv_word *sp = stack;
814
815   _Jv_word locals[meth->max_locals];
816
817 #define INSN_LABEL(op) &&insn_##op
818
819   static const void *const insn_target[] = 
820   {
821     INSN_LABEL(nop),
822     INSN_LABEL(aconst_null),
823     INSN_LABEL(iconst_m1),
824     INSN_LABEL(iconst_0),
825     INSN_LABEL(iconst_1),
826     INSN_LABEL(iconst_2),
827     INSN_LABEL(iconst_3),
828     INSN_LABEL(iconst_4),
829     INSN_LABEL(iconst_5),
830     INSN_LABEL(lconst_0),
831     INSN_LABEL(lconst_1),
832     INSN_LABEL(fconst_0),
833     INSN_LABEL(fconst_1),
834     INSN_LABEL(fconst_2),
835     INSN_LABEL(dconst_0),
836     INSN_LABEL(dconst_1),
837     INSN_LABEL(bipush),
838     INSN_LABEL(sipush),
839     INSN_LABEL(ldc),
840     INSN_LABEL(ldc_w),
841     INSN_LABEL(ldc2_w),
842     INSN_LABEL(iload),
843     INSN_LABEL(lload),
844     INSN_LABEL(fload),
845     INSN_LABEL(dload),
846     INSN_LABEL(aload),
847     INSN_LABEL(iload_0),
848     INSN_LABEL(iload_1),
849     INSN_LABEL(iload_2),
850     INSN_LABEL(iload_3),
851     INSN_LABEL(lload_0),
852     INSN_LABEL(lload_1),
853     INSN_LABEL(lload_2),
854     INSN_LABEL(lload_3),
855     INSN_LABEL(fload_0),
856     INSN_LABEL(fload_1),
857     INSN_LABEL(fload_2),
858     INSN_LABEL(fload_3),
859     INSN_LABEL(dload_0),
860     INSN_LABEL(dload_1),
861     INSN_LABEL(dload_2),
862     INSN_LABEL(dload_3),
863     INSN_LABEL(aload_0),
864     INSN_LABEL(aload_1),
865     INSN_LABEL(aload_2),
866     INSN_LABEL(aload_3),
867     INSN_LABEL(iaload),
868     INSN_LABEL(laload),
869     INSN_LABEL(faload),
870     INSN_LABEL(daload),
871     INSN_LABEL(aaload),
872     INSN_LABEL(baload),
873     INSN_LABEL(caload),
874     INSN_LABEL(saload),
875     INSN_LABEL(istore),
876     INSN_LABEL(lstore),
877     INSN_LABEL(fstore),
878     INSN_LABEL(dstore),
879     INSN_LABEL(astore),
880     INSN_LABEL(istore_0),
881     INSN_LABEL(istore_1),
882     INSN_LABEL(istore_2),
883     INSN_LABEL(istore_3),
884     INSN_LABEL(lstore_0),
885     INSN_LABEL(lstore_1),
886     INSN_LABEL(lstore_2),
887     INSN_LABEL(lstore_3),
888     INSN_LABEL(fstore_0),
889     INSN_LABEL(fstore_1),
890     INSN_LABEL(fstore_2),
891     INSN_LABEL(fstore_3),
892     INSN_LABEL(dstore_0),
893     INSN_LABEL(dstore_1),
894     INSN_LABEL(dstore_2),
895     INSN_LABEL(dstore_3),
896     INSN_LABEL(astore_0),
897     INSN_LABEL(astore_1),
898     INSN_LABEL(astore_2),
899     INSN_LABEL(astore_3),
900     INSN_LABEL(iastore),
901     INSN_LABEL(lastore),
902     INSN_LABEL(fastore),
903     INSN_LABEL(dastore),
904     INSN_LABEL(aastore),
905     INSN_LABEL(bastore),
906     INSN_LABEL(castore),
907     INSN_LABEL(sastore),
908     INSN_LABEL(pop),
909     INSN_LABEL(pop2),
910     INSN_LABEL(dup),
911     INSN_LABEL(dup_x1),
912     INSN_LABEL(dup_x2),
913     INSN_LABEL(dup2),
914     INSN_LABEL(dup2_x1),
915     INSN_LABEL(dup2_x2),
916     INSN_LABEL(swap),
917     INSN_LABEL(iadd),
918     INSN_LABEL(ladd),
919     INSN_LABEL(fadd),
920     INSN_LABEL(dadd),
921     INSN_LABEL(isub),
922     INSN_LABEL(lsub),
923     INSN_LABEL(fsub),
924     INSN_LABEL(dsub),
925     INSN_LABEL(imul),
926     INSN_LABEL(lmul),
927     INSN_LABEL(fmul),
928     INSN_LABEL(dmul),
929     INSN_LABEL(idiv),
930     INSN_LABEL(ldiv),
931     INSN_LABEL(fdiv),
932     INSN_LABEL(ddiv),
933     INSN_LABEL(irem),
934     INSN_LABEL(lrem),
935     INSN_LABEL(frem),
936     INSN_LABEL(drem),
937     INSN_LABEL(ineg),
938     INSN_LABEL(lneg),
939     INSN_LABEL(fneg),
940     INSN_LABEL(dneg),
941     INSN_LABEL(ishl),
942     INSN_LABEL(lshl),
943     INSN_LABEL(ishr),
944     INSN_LABEL(lshr),
945     INSN_LABEL(iushr),
946     INSN_LABEL(lushr),
947     INSN_LABEL(iand),
948     INSN_LABEL(land),
949     INSN_LABEL(ior),
950     INSN_LABEL(lor),
951     INSN_LABEL(ixor),
952     INSN_LABEL(lxor),
953     INSN_LABEL(iinc),
954     INSN_LABEL(i2l),
955     INSN_LABEL(i2f),
956     INSN_LABEL(i2d),
957     INSN_LABEL(l2i),
958     INSN_LABEL(l2f),
959     INSN_LABEL(l2d),
960     INSN_LABEL(f2i),
961     INSN_LABEL(f2l),
962     INSN_LABEL(f2d),
963     INSN_LABEL(d2i),
964     INSN_LABEL(d2l),
965     INSN_LABEL(d2f),
966     INSN_LABEL(i2b),
967     INSN_LABEL(i2c),
968     INSN_LABEL(i2s),
969     INSN_LABEL(lcmp),
970     INSN_LABEL(fcmpl),
971     INSN_LABEL(fcmpg),
972     INSN_LABEL(dcmpl),
973     INSN_LABEL(dcmpg),
974     INSN_LABEL(ifeq),
975     INSN_LABEL(ifne),
976     INSN_LABEL(iflt),
977     INSN_LABEL(ifge),
978     INSN_LABEL(ifgt),
979     INSN_LABEL(ifle),
980     INSN_LABEL(if_icmpeq),
981     INSN_LABEL(if_icmpne),
982     INSN_LABEL(if_icmplt),
983     INSN_LABEL(if_icmpge),
984     INSN_LABEL(if_icmpgt),
985     INSN_LABEL(if_icmple),
986     INSN_LABEL(if_acmpeq),
987     INSN_LABEL(if_acmpne),
988     INSN_LABEL(goto), 
989     INSN_LABEL(jsr),
990     INSN_LABEL(ret),
991     INSN_LABEL(tableswitch),
992     INSN_LABEL(lookupswitch),
993     INSN_LABEL(ireturn),
994     INSN_LABEL(lreturn),
995     INSN_LABEL(freturn),
996     INSN_LABEL(dreturn),
997     INSN_LABEL(areturn),
998     INSN_LABEL(return),
999     INSN_LABEL(getstatic),
1000     INSN_LABEL(putstatic),
1001     INSN_LABEL(getfield),
1002     INSN_LABEL(putfield),
1003     INSN_LABEL(invokevirtual),
1004     INSN_LABEL(invokespecial),
1005     INSN_LABEL(invokestatic),
1006     INSN_LABEL(invokeinterface),
1007     0, /* Unused.  */
1008     INSN_LABEL(new),
1009     INSN_LABEL(newarray),
1010     INSN_LABEL(anewarray),
1011     INSN_LABEL(arraylength),
1012     INSN_LABEL(athrow),
1013     INSN_LABEL(checkcast),
1014     INSN_LABEL(instanceof),
1015     INSN_LABEL(monitorenter),
1016     INSN_LABEL(monitorexit),
1017 #ifdef DIRECT_THREADED
1018     0, // wide
1019 #else
1020     INSN_LABEL(wide),
1021 #endif
1022     INSN_LABEL(multianewarray),
1023     INSN_LABEL(ifnull),
1024     INSN_LABEL(ifnonnull),
1025     INSN_LABEL(goto_w),
1026     INSN_LABEL(jsr_w),
1027 #ifdef DIRECT_THREADED
1028     INSN_LABEL (ldc_class)
1029 #else
1030     0
1031 #endif
1032   };
1033
1034   pc_t pc;
1035
1036 #ifdef DIRECT_THREADED
1037
1038 #define NEXT_INSN goto *((pc++)->insn)
1039 #define INTVAL() ((pc++)->int_val)
1040 #define AVAL() ((pc++)->datum)
1041
1042 #define GET1S() INTVAL ()
1043 #define GET2S() INTVAL ()
1044 #define GET1U() INTVAL ()
1045 #define GET2U() INTVAL ()
1046 #define AVAL1U() AVAL ()
1047 #define AVAL2U() AVAL ()
1048 #define AVAL2UP() AVAL ()
1049 #define SKIP_GOTO ++pc
1050 #define GOTO_VAL() (insn_slot *) pc->datum
1051 #define PCVAL(unionval) unionval.p
1052 #define AMPAMP(label) &&label
1053
1054   // Compile if we must. NOTE: Double-check locking.
1055   if (meth->prepared == NULL)
1056     {
1057       _Jv_MutexLock (&compile_mutex);
1058       if (meth->prepared == NULL)
1059         meth->compile (insn_target);
1060       _Jv_MutexUnlock (&compile_mutex);
1061     }
1062
1063   // If we're only compiling, stop here
1064   if (args == NULL)
1065     return;
1066
1067   pc = (insn_slot *) meth->prepared;
1068
1069 #else
1070
1071 #define NEXT_INSN goto *(insn_target[*pc++])
1072
1073 #define GET1S() get1s (pc++)
1074 #define GET2S() (pc += 2, get2s (pc- 2))
1075 #define GET1U() get1u (pc++)
1076 #define GET2U() (pc += 2, get2u (pc - 2))
1077   // Note that these could be more efficient when not handling 'ldc
1078   // class'.
1079 #define AVAL1U()                                                \
1080   ({ int index = get1u (pc++);                                  \
1081       resolve_pool_entry (meth->defining_class, index).o; })
1082 #define AVAL2U()                                                \
1083   ({ int index = get2u (pc); pc += 2;                           \
1084       resolve_pool_entry (meth->defining_class, index).o; })
1085   // Note that we don't need to resolve the pool entry here as class
1086   // constants are never wide.
1087 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1088 #define SKIP_GOTO pc += 2
1089 #define GOTO_VAL() pc - 1 + get2s (pc)
1090 #define PCVAL(unionval) unionval.i
1091 #define AMPAMP(label) NULL
1092
1093   pc = bytecode ();
1094
1095 #endif /* DIRECT_THREADED */
1096
1097 #define TAKE_GOTO pc = GOTO_VAL ()
1098
1099   /* Go straight at it!  the ffi raw format matches the internal
1100      stack representation exactly.  At least, that's the idea.
1101   */
1102   memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1103
1104   _Jv_word *pool_data = meth->defining_class->constants.data;
1105
1106   /* These three are temporaries for common code used by several
1107      instructions.  */
1108   void (*fun)();
1109   _Jv_ResolvedMethod* rmeth;
1110   int tmpval;
1111
1112   try
1113     {
1114       // We keep nop around.  It is used if we're interpreting the
1115       // bytecodes and not doing direct threading.
1116     insn_nop:
1117       NEXT_INSN;
1118
1119       /* The first few instructions here are ordered according to their
1120          frequency, in the hope that this will improve code locality a
1121          little.  */
1122
1123     insn_aload_0:               // 0x2a
1124       LOADA (0);
1125       NEXT_INSN;
1126
1127     insn_iload:         // 0x15
1128       LOADI (GET1U ());
1129       NEXT_INSN;
1130
1131     insn_iload_1:               // 0x1b
1132       LOADI (1);
1133       NEXT_INSN;
1134
1135     insn_invokevirtual: // 0xb6
1136       {
1137         int index = GET2U ();
1138
1139         /* _Jv_Linker::resolve_pool_entry returns immediately if the
1140          * value already is resolved.  If we want to clutter up the
1141          * code here to gain a little performance, then we can check
1142          * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1143          * directly.  For now, I don't think it is worth it.  */
1144
1145         SAVE_PC();
1146         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1147                                                    index)).rmethod;
1148
1149         sp -= rmeth->stack_item_count;
1150         // We don't use NULLCHECK here because we can't rely on that
1151         // working if the method is final.  So instead we do an
1152         // explicit test.
1153         if (! sp[0].o)
1154           {
1155             //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1156             throw new java::lang::NullPointerException;
1157           }
1158
1159         if (rmeth->vtable_index == -1)
1160           {
1161             // final methods do not appear in the vtable,
1162             // if it does not appear in the superclass.
1163             fun = (void (*)()) rmeth->method->ncode;
1164           }
1165         else
1166           {
1167             jobject rcv = sp[0].o;
1168             _Jv_VTable *table = *(_Jv_VTable**) rcv;
1169             fun = (void (*)()) table->get_method (rmeth->vtable_index);
1170           }
1171
1172 #ifdef DIRECT_THREADED
1173         // Rewrite instruction so that we use a faster pre-resolved
1174         // method.
1175         pc[-2].insn = &&invokevirtual_resolved;
1176         pc[-1].datum = rmeth;
1177 #endif /* DIRECT_THREADED */
1178       }
1179       goto perform_invoke;
1180
1181 #ifdef DIRECT_THREADED
1182     invokevirtual_resolved:
1183       {
1184         rmeth = (_Jv_ResolvedMethod *) AVAL ();
1185         sp -= rmeth->stack_item_count;
1186         // We don't use NULLCHECK here because we can't rely on that
1187         // working if the method is final.  So instead we do an
1188         // explicit test.
1189         if (! sp[0].o)
1190           {
1191             SAVE_PC();
1192             throw new java::lang::NullPointerException;
1193           }
1194
1195         if (rmeth->vtable_index == -1)
1196           {
1197             // final methods do not appear in the vtable,
1198             // if it does not appear in the superclass.
1199             fun = (void (*)()) rmeth->method->ncode;
1200           }
1201         else
1202           {
1203             jobject rcv = sp[0].o;
1204             _Jv_VTable *table = *(_Jv_VTable**) rcv;
1205             fun = (void (*)()) table->get_method (rmeth->vtable_index);
1206           }
1207       }
1208       goto perform_invoke;
1209 #endif /* DIRECT_THREADED */
1210
1211     perform_invoke:
1212       {
1213         SAVE_PC();
1214         
1215         /* here goes the magic again... */
1216         ffi_cif *cif = &rmeth->cif;
1217         ffi_raw *raw = (ffi_raw*) sp;
1218
1219         _Jv_value rvalue;
1220
1221 #if FFI_NATIVE_RAW_API
1222         /* We assume that this is only implemented if it's correct      */
1223         /* to use it here.  On a 64 bit machine, it never is.           */
1224         ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1225 #else
1226         ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1227 #endif
1228
1229         int rtype = cif->rtype->type;
1230
1231         /* the likelyhood of object, int, or void return is very high,
1232          * so those are checked before the switch */
1233         if (rtype == FFI_TYPE_POINTER)
1234           {
1235             PUSHA (rvalue.object_value);
1236           }
1237         else if (rtype == FFI_TYPE_SINT32)
1238           {
1239             PUSHI (rvalue.int_value);
1240           }
1241         else if (rtype == FFI_TYPE_VOID)
1242           {
1243             /* skip */
1244           }
1245         else
1246           {
1247             switch (rtype)
1248               {
1249               case FFI_TYPE_SINT8:
1250                 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1251                 break;
1252
1253               case FFI_TYPE_SINT16:
1254                 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1255                 break;
1256
1257               case FFI_TYPE_UINT16:
1258                 PUSHI (rvalue.int_value & 0xffff);
1259                 break;
1260
1261               case FFI_TYPE_FLOAT:
1262                 PUSHF (rvalue.float_value);
1263                 break;
1264
1265               case FFI_TYPE_DOUBLE:
1266                 PUSHD (rvalue.double_value);
1267                 break;
1268
1269               case FFI_TYPE_SINT64:
1270                 PUSHL (rvalue.long_value);
1271                 break;
1272
1273               default:
1274                 throw_internal_error ("unknown return type in invokeXXX");
1275               }
1276           }
1277       }
1278       NEXT_INSN;
1279
1280     insn_aconst_null:
1281       PUSHA (NULL);
1282       NEXT_INSN;
1283
1284     insn_iconst_m1:
1285       PUSHI (-1);
1286       NEXT_INSN;
1287
1288     insn_iconst_0:
1289       PUSHI (0);
1290       NEXT_INSN;
1291
1292     insn_iconst_1:
1293       PUSHI (1);
1294       NEXT_INSN;
1295
1296     insn_iconst_2:
1297       PUSHI (2);
1298       NEXT_INSN;
1299
1300     insn_iconst_3:
1301       PUSHI (3);
1302       NEXT_INSN;
1303
1304     insn_iconst_4:
1305       PUSHI (4);
1306       NEXT_INSN;
1307
1308     insn_iconst_5:
1309       PUSHI (5);
1310       NEXT_INSN;
1311
1312     insn_lconst_0:
1313       PUSHL (0);
1314       NEXT_INSN;
1315
1316     insn_lconst_1:
1317       PUSHL (1);
1318       NEXT_INSN;
1319
1320     insn_fconst_0:
1321       PUSHF (0);
1322       NEXT_INSN;
1323
1324     insn_fconst_1:
1325       PUSHF (1);
1326       NEXT_INSN;
1327
1328     insn_fconst_2:
1329       PUSHF (2);
1330       NEXT_INSN;
1331
1332     insn_dconst_0:
1333       PUSHD (0);
1334       NEXT_INSN;
1335
1336     insn_dconst_1:
1337       PUSHD (1);
1338       NEXT_INSN;
1339
1340     insn_bipush:
1341       // For direct threaded, bipush and sipush are the same.
1342 #ifndef DIRECT_THREADED
1343       PUSHI (GET1S ());
1344       NEXT_INSN;
1345 #endif /* DIRECT_THREADED */
1346     insn_sipush:
1347       PUSHI (GET2S ());
1348       NEXT_INSN;
1349
1350     insn_ldc:
1351       // For direct threaded, ldc and ldc_w are the same.
1352 #ifndef DIRECT_THREADED
1353       PUSHA ((jobject) AVAL1U ());
1354       NEXT_INSN;
1355 #endif /* DIRECT_THREADED */
1356     insn_ldc_w:
1357       PUSHA ((jobject) AVAL2U ());
1358       NEXT_INSN;
1359
1360 #ifdef DIRECT_THREADED
1361       // For direct threaded we have a separate 'ldc class' operation.
1362     insn_ldc_class:
1363       {
1364         // We could rewrite the instruction at this point.
1365         int index = INTVAL ();
1366         jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1367                                                      index)).o;
1368         PUSHA (k);
1369       }
1370       NEXT_INSN;
1371 #endif /* DIRECT_THREADED */
1372
1373     insn_ldc2_w:
1374       {
1375         void *where = AVAL2UP ();
1376         memcpy (sp, where, 2*sizeof (_Jv_word));
1377         sp += 2;
1378       }
1379       NEXT_INSN;
1380
1381     insn_lload:
1382       LOADL (GET1U ());
1383       NEXT_INSN;
1384
1385     insn_fload:
1386       LOADF (GET1U ());
1387       NEXT_INSN;
1388
1389     insn_dload:
1390       LOADD (GET1U ());
1391       NEXT_INSN;
1392
1393     insn_aload:
1394       LOADA (GET1U ());
1395       NEXT_INSN;
1396
1397     insn_iload_0:
1398       LOADI (0);
1399       NEXT_INSN;
1400
1401     insn_iload_2:
1402       LOADI (2);
1403       NEXT_INSN;
1404
1405     insn_iload_3:
1406       LOADI (3);
1407       NEXT_INSN;
1408
1409     insn_lload_0:
1410       LOADL (0);
1411       NEXT_INSN;
1412
1413     insn_lload_1:
1414       LOADL (1);
1415       NEXT_INSN;
1416
1417     insn_lload_2:
1418       LOADL (2);
1419       NEXT_INSN;
1420
1421     insn_lload_3:
1422       LOADL (3);
1423       NEXT_INSN;
1424
1425     insn_fload_0:
1426       LOADF (0);
1427       NEXT_INSN;
1428
1429     insn_fload_1:
1430       LOADF (1);
1431       NEXT_INSN;
1432
1433     insn_fload_2:
1434       LOADF (2);
1435       NEXT_INSN;
1436
1437     insn_fload_3:
1438       LOADF (3);
1439       NEXT_INSN;
1440
1441     insn_dload_0:
1442       LOADD (0);
1443       NEXT_INSN;
1444
1445     insn_dload_1:
1446       LOADD (1);
1447       NEXT_INSN;
1448
1449     insn_dload_2:
1450       LOADD (2);
1451       NEXT_INSN;
1452
1453     insn_dload_3:
1454       LOADD (3);
1455       NEXT_INSN;
1456
1457     insn_aload_1:
1458       LOADA(1);
1459       NEXT_INSN;
1460
1461     insn_aload_2:
1462       LOADA(2);
1463       NEXT_INSN;
1464
1465     insn_aload_3:
1466       LOADA(3);
1467       NEXT_INSN;
1468
1469     insn_iaload:
1470       {
1471         jint index = POPI();
1472         jintArray arr = (jintArray) POPA();
1473         NULLARRAYCHECK (arr);
1474         ARRAYBOUNDSCHECK (arr, index);
1475         PUSHI( elements(arr)[index] );
1476       }
1477       NEXT_INSN;
1478
1479     insn_laload:
1480       {
1481         jint index = POPI();
1482         jlongArray arr = (jlongArray) POPA();
1483         NULLARRAYCHECK (arr);
1484         ARRAYBOUNDSCHECK (arr, index);
1485         PUSHL( elements(arr)[index] );
1486       }
1487       NEXT_INSN;
1488
1489     insn_faload:
1490       {
1491         jint index = POPI();
1492         jfloatArray arr = (jfloatArray) POPA();
1493         NULLARRAYCHECK (arr);
1494         ARRAYBOUNDSCHECK (arr, index);
1495         PUSHF( elements(arr)[index] );
1496       }
1497       NEXT_INSN;
1498
1499     insn_daload:
1500       {
1501         jint index = POPI();
1502         jdoubleArray arr = (jdoubleArray) POPA();
1503         NULLARRAYCHECK (arr);
1504         ARRAYBOUNDSCHECK (arr, index);
1505         PUSHD( elements(arr)[index] );
1506       }
1507       NEXT_INSN;
1508
1509     insn_aaload:
1510       {
1511         jint index = POPI();
1512         jobjectArray arr = (jobjectArray) POPA();
1513         NULLARRAYCHECK (arr);
1514         ARRAYBOUNDSCHECK (arr, index);
1515         PUSHA( elements(arr)[index] );
1516       }
1517       NEXT_INSN;
1518
1519     insn_baload:
1520       {
1521         jint index = POPI();
1522         jbyteArray arr = (jbyteArray) POPA();
1523         NULLARRAYCHECK (arr);
1524         ARRAYBOUNDSCHECK (arr, index);
1525         PUSHI( elements(arr)[index] );
1526       }
1527       NEXT_INSN;
1528
1529     insn_caload:
1530       {
1531         jint index = POPI();
1532         jcharArray arr = (jcharArray) POPA();
1533         NULLARRAYCHECK (arr);
1534         ARRAYBOUNDSCHECK (arr, index);
1535         PUSHI( elements(arr)[index] );
1536       }
1537       NEXT_INSN;
1538
1539     insn_saload:
1540       {
1541         jint index = POPI();
1542         jshortArray arr = (jshortArray) POPA();
1543         NULLARRAYCHECK (arr);
1544         ARRAYBOUNDSCHECK (arr, index);
1545         PUSHI( elements(arr)[index] );
1546       }
1547       NEXT_INSN;
1548
1549     insn_istore:
1550       STOREI (GET1U ());
1551       NEXT_INSN;
1552
1553     insn_lstore:
1554       STOREL (GET1U ());
1555       NEXT_INSN;
1556
1557     insn_fstore:
1558       STOREF (GET1U ());
1559       NEXT_INSN;
1560
1561     insn_dstore:
1562       STORED (GET1U ());
1563       NEXT_INSN;
1564
1565     insn_astore:
1566       STOREA (GET1U ());
1567       NEXT_INSN;
1568
1569     insn_istore_0:
1570       STOREI (0);
1571       NEXT_INSN;
1572
1573     insn_istore_1:
1574       STOREI (1);
1575       NEXT_INSN;
1576
1577     insn_istore_2:
1578       STOREI (2);
1579       NEXT_INSN;
1580
1581     insn_istore_3:
1582       STOREI (3);
1583       NEXT_INSN;
1584
1585     insn_lstore_0:
1586       STOREL (0);
1587       NEXT_INSN;
1588
1589     insn_lstore_1:
1590       STOREL (1);
1591       NEXT_INSN;
1592
1593     insn_lstore_2:
1594       STOREL (2);
1595       NEXT_INSN;
1596
1597     insn_lstore_3:
1598       STOREL (3);
1599       NEXT_INSN;
1600
1601     insn_fstore_0:
1602       STOREF (0);
1603       NEXT_INSN;
1604
1605     insn_fstore_1:
1606       STOREF (1);
1607       NEXT_INSN;
1608
1609     insn_fstore_2:
1610       STOREF (2);
1611       NEXT_INSN;
1612
1613     insn_fstore_3:
1614       STOREF (3);
1615       NEXT_INSN;
1616
1617     insn_dstore_0:
1618       STORED (0);
1619       NEXT_INSN;
1620
1621     insn_dstore_1:
1622       STORED (1);
1623       NEXT_INSN;
1624
1625     insn_dstore_2:
1626       STORED (2);
1627       NEXT_INSN;
1628
1629     insn_dstore_3:
1630       STORED (3);
1631       NEXT_INSN;
1632
1633     insn_astore_0:
1634       STOREA(0);
1635       NEXT_INSN;
1636
1637     insn_astore_1:
1638       STOREA(1);
1639       NEXT_INSN;
1640
1641     insn_astore_2:
1642       STOREA(2);
1643       NEXT_INSN;
1644
1645     insn_astore_3:
1646       STOREA(3);
1647       NEXT_INSN;
1648
1649     insn_iastore:
1650       {
1651         jint value = POPI();
1652         jint index  = POPI();
1653         jintArray arr = (jintArray) POPA();
1654         NULLARRAYCHECK (arr);
1655         ARRAYBOUNDSCHECK (arr, index);
1656         elements(arr)[index] = value;
1657       }
1658       NEXT_INSN;
1659
1660     insn_lastore:
1661       {
1662         jlong value = POPL();
1663         jint index  = POPI();
1664         jlongArray arr = (jlongArray) POPA();
1665         NULLARRAYCHECK (arr);
1666         ARRAYBOUNDSCHECK (arr, index);
1667         elements(arr)[index] = value;
1668       }
1669       NEXT_INSN;
1670
1671     insn_fastore:
1672       {
1673         jfloat value = POPF();
1674         jint index  = POPI();
1675         jfloatArray arr = (jfloatArray) POPA();
1676         NULLARRAYCHECK (arr);
1677         ARRAYBOUNDSCHECK (arr, index);
1678         elements(arr)[index] = value;
1679       }
1680       NEXT_INSN;
1681
1682     insn_dastore:
1683       {
1684         jdouble value = POPD();
1685         jint index  = POPI();
1686         jdoubleArray arr = (jdoubleArray) POPA();
1687         NULLARRAYCHECK (arr);
1688         ARRAYBOUNDSCHECK (arr, index);
1689         elements(arr)[index] = value;
1690       }
1691       NEXT_INSN;
1692
1693     insn_aastore:
1694       {
1695         jobject value = POPA();
1696         jint index  = POPI();
1697         jobjectArray arr = (jobjectArray) POPA();
1698         NULLARRAYCHECK (arr);
1699         ARRAYBOUNDSCHECK (arr, index);
1700         _Jv_CheckArrayStore (arr, value);
1701         elements(arr)[index] = value;
1702       }
1703       NEXT_INSN;
1704
1705     insn_bastore:
1706       {
1707         jbyte value = (jbyte) POPI();
1708         jint index  = POPI();
1709         jbyteArray arr = (jbyteArray) POPA();
1710         NULLARRAYCHECK (arr);
1711         ARRAYBOUNDSCHECK (arr, index);
1712         elements(arr)[index] = value;
1713       }
1714       NEXT_INSN;
1715
1716     insn_castore:
1717       {
1718         jchar value = (jchar) POPI();
1719         jint index  = POPI();
1720         jcharArray arr = (jcharArray) POPA();
1721         NULLARRAYCHECK (arr);
1722         ARRAYBOUNDSCHECK (arr, index);
1723         elements(arr)[index] = value;
1724       }
1725       NEXT_INSN;
1726
1727     insn_sastore:
1728       {
1729         jshort value = (jshort) POPI();
1730         jint index  = POPI();
1731         jshortArray arr = (jshortArray) POPA();
1732         NULLARRAYCHECK (arr);
1733         ARRAYBOUNDSCHECK (arr, index);
1734         elements(arr)[index] = value;
1735       }
1736       NEXT_INSN;
1737
1738     insn_pop:
1739       sp -= 1;
1740       NEXT_INSN;
1741
1742     insn_pop2:
1743       sp -= 2;
1744       NEXT_INSN;
1745
1746     insn_dup:
1747       sp[0] = sp[-1];
1748       sp += 1;
1749       NEXT_INSN;
1750
1751     insn_dup_x1:
1752       dupx (sp, 1, 1); sp+=1;
1753       NEXT_INSN;
1754
1755     insn_dup_x2:
1756       dupx (sp, 1, 2); sp+=1;
1757       NEXT_INSN;
1758
1759     insn_dup2:
1760       sp[0] = sp[-2];
1761       sp[1] = sp[-1];
1762       sp += 2;
1763       NEXT_INSN;
1764
1765     insn_dup2_x1:
1766       dupx (sp, 2, 1); sp+=2;
1767       NEXT_INSN;
1768
1769     insn_dup2_x2:
1770       dupx (sp, 2, 2); sp+=2;
1771       NEXT_INSN;
1772
1773     insn_swap:
1774       {
1775         jobject tmp1 = POPA();
1776         jobject tmp2 = POPA();
1777         PUSHA (tmp1);
1778         PUSHA (tmp2);
1779       }
1780       NEXT_INSN;
1781
1782     insn_iadd:
1783       BINOPI(+);
1784       NEXT_INSN;
1785
1786     insn_ladd:
1787       BINOPL(+);
1788       NEXT_INSN;
1789
1790     insn_fadd:
1791       BINOPF(+);
1792       NEXT_INSN;
1793
1794     insn_dadd:
1795       BINOPD(+);
1796       NEXT_INSN;
1797
1798     insn_isub:
1799       BINOPI(-);
1800       NEXT_INSN;
1801
1802     insn_lsub:
1803       BINOPL(-);
1804       NEXT_INSN;
1805
1806     insn_fsub:
1807       BINOPF(-);
1808       NEXT_INSN;
1809
1810     insn_dsub:
1811       BINOPD(-);
1812       NEXT_INSN;
1813
1814     insn_imul:
1815       BINOPI(*);
1816       NEXT_INSN;
1817
1818     insn_lmul:
1819       BINOPL(*);
1820       NEXT_INSN;
1821
1822     insn_fmul:
1823       BINOPF(*);
1824       NEXT_INSN;
1825
1826     insn_dmul:
1827       BINOPD(*);
1828       NEXT_INSN;
1829
1830     insn_idiv:
1831       {
1832         jint value2 = POPI();
1833         jint value1 = POPI();
1834         jint res = _Jv_divI (value1, value2);
1835         PUSHI (res);
1836       }
1837       NEXT_INSN;
1838
1839     insn_ldiv:
1840       {
1841         jlong value2 = POPL();
1842         jlong value1 = POPL();
1843         jlong res = _Jv_divJ (value1, value2);
1844         PUSHL (res);
1845       }
1846       NEXT_INSN;
1847
1848     insn_fdiv:
1849       {
1850         jfloat value2 = POPF();
1851         jfloat value1 = POPF();
1852         jfloat res = value1 / value2;
1853         PUSHF (res);
1854       }
1855       NEXT_INSN;
1856
1857     insn_ddiv:
1858       {
1859         jdouble value2 = POPD();
1860         jdouble value1 = POPD();
1861         jdouble res = value1 / value2;
1862         PUSHD (res);
1863       }
1864       NEXT_INSN;
1865
1866     insn_irem:
1867       {
1868         jint value2 = POPI();
1869         jint value1 =  POPI();
1870         jint res = _Jv_remI (value1, value2);
1871         PUSHI (res);
1872       }
1873       NEXT_INSN;
1874
1875     insn_lrem:
1876       {
1877         jlong value2 = POPL();
1878         jlong value1 = POPL();
1879         jlong res = _Jv_remJ (value1, value2);
1880         PUSHL (res);
1881       }
1882       NEXT_INSN;
1883
1884     insn_frem:
1885       {
1886         jfloat value2 = POPF();
1887         jfloat value1 = POPF();
1888         jfloat res    = __ieee754_fmod (value1, value2);
1889         PUSHF (res);
1890       }
1891       NEXT_INSN;
1892
1893     insn_drem:
1894       {
1895         jdouble value2 = POPD();
1896         jdouble value1 = POPD();
1897         jdouble res    = __ieee754_fmod (value1, value2);
1898         PUSHD (res);
1899       }
1900       NEXT_INSN;
1901
1902     insn_ineg:
1903       {
1904         jint value = POPI();
1905         PUSHI (value * -1);
1906       }
1907       NEXT_INSN;
1908
1909     insn_lneg:
1910       {
1911         jlong value = POPL();
1912         PUSHL (value * -1);
1913       }
1914       NEXT_INSN;
1915
1916     insn_fneg:
1917       {
1918         jfloat value = POPF();
1919         PUSHF (value * -1);
1920       }
1921       NEXT_INSN;
1922
1923     insn_dneg:
1924       {
1925         jdouble value = POPD();
1926         PUSHD (value * -1);
1927       }
1928       NEXT_INSN;
1929
1930     insn_ishl:
1931       {
1932         jint shift = (POPI() & 0x1f);
1933         jint value = POPI();
1934         PUSHI (value << shift);
1935       }
1936       NEXT_INSN;
1937
1938     insn_lshl:
1939       {
1940         jint shift = (POPI() & 0x3f);
1941         jlong value = POPL();
1942         PUSHL (value << shift);
1943       }
1944       NEXT_INSN;
1945
1946     insn_ishr:
1947       {
1948         jint shift = (POPI() & 0x1f);
1949         jint value = POPI();
1950         PUSHI (value >> shift);
1951       }
1952       NEXT_INSN;
1953
1954     insn_lshr:
1955       {
1956         jint shift = (POPI() & 0x3f);
1957         jlong value = POPL();
1958         PUSHL (value >> shift);
1959       }
1960       NEXT_INSN;
1961
1962     insn_iushr:
1963       {
1964         jint shift = (POPI() & 0x1f);
1965         _Jv_uint value = (_Jv_uint) POPI();
1966         PUSHI ((jint) (value >> shift));
1967       }
1968       NEXT_INSN;
1969
1970     insn_lushr:
1971       {
1972         jint shift = (POPI() & 0x3f);
1973         _Jv_ulong value = (_Jv_ulong) POPL();
1974         PUSHL ((jlong) (value >> shift));
1975       }
1976       NEXT_INSN;
1977
1978     insn_iand:
1979       BINOPI (&);
1980       NEXT_INSN;
1981
1982     insn_land:
1983       BINOPL (&);
1984       NEXT_INSN;
1985
1986     insn_ior:
1987       BINOPI (|);
1988       NEXT_INSN;
1989
1990     insn_lor:
1991       BINOPL (|);
1992       NEXT_INSN;
1993
1994     insn_ixor:
1995       BINOPI (^);
1996       NEXT_INSN;
1997
1998     insn_lxor:
1999       BINOPL (^);
2000       NEXT_INSN;
2001
2002     insn_iinc:
2003       {
2004         jint index  = GET1U ();
2005         jint amount = GET1S ();
2006         locals[index].i += amount;
2007       }
2008       NEXT_INSN;
2009
2010     insn_i2l:
2011       {jlong value = POPI(); PUSHL (value);}
2012       NEXT_INSN;
2013
2014     insn_i2f:
2015       {jfloat value = POPI(); PUSHF (value);}
2016       NEXT_INSN;
2017
2018     insn_i2d:
2019       {jdouble value = POPI(); PUSHD (value);}
2020       NEXT_INSN;
2021
2022     insn_l2i:
2023       {jint value = POPL(); PUSHI (value);}
2024       NEXT_INSN;
2025
2026     insn_l2f:
2027       {jfloat value = POPL(); PUSHF (value);}
2028       NEXT_INSN;
2029
2030     insn_l2d:
2031       {jdouble value = POPL(); PUSHD (value);}
2032       NEXT_INSN;
2033
2034     insn_f2i:
2035       {
2036         using namespace java::lang;
2037         jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2038         PUSHI(value);
2039       }
2040       NEXT_INSN;
2041
2042     insn_f2l:
2043       {
2044         using namespace java::lang;
2045         jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2046         PUSHL(value);
2047       }
2048       NEXT_INSN;
2049
2050     insn_f2d:
2051       { jdouble value = POPF (); PUSHD(value); }
2052       NEXT_INSN;
2053
2054     insn_d2i:
2055       {
2056         using namespace java::lang;
2057         jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2058         PUSHI(value);
2059       }
2060       NEXT_INSN;
2061
2062     insn_d2l:
2063       {
2064         using namespace java::lang;
2065         jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2066         PUSHL(value);
2067       }
2068       NEXT_INSN;
2069
2070     insn_d2f:
2071       { jfloat value = POPD (); PUSHF(value); }
2072       NEXT_INSN;
2073
2074     insn_i2b:
2075       { jbyte value = POPI (); PUSHI(value); }
2076       NEXT_INSN;
2077
2078     insn_i2c:
2079       { jchar value = POPI (); PUSHI(value); }
2080       NEXT_INSN;
2081
2082     insn_i2s:
2083       { jshort value = POPI (); PUSHI(value); }
2084       NEXT_INSN;
2085
2086     insn_lcmp:
2087       {
2088         jlong value2 = POPL ();
2089         jlong value1 = POPL ();
2090         if (value1 > value2)
2091           { PUSHI (1); }
2092         else if (value1 == value2)
2093           { PUSHI (0); }
2094         else
2095           { PUSHI (-1); }
2096       }
2097       NEXT_INSN;
2098
2099     insn_fcmpl:
2100       tmpval = -1;
2101       goto fcmp;
2102
2103     insn_fcmpg:
2104       tmpval = 1;
2105
2106     fcmp:
2107       {
2108         jfloat value2 = POPF ();
2109         jfloat value1 = POPF ();
2110         if (value1 > value2)
2111           PUSHI (1);
2112         else if (value1 == value2)
2113           PUSHI (0);
2114         else if (value1 < value2)
2115           PUSHI (-1);
2116         else
2117           PUSHI (tmpval);
2118       }
2119       NEXT_INSN;
2120
2121     insn_dcmpl:
2122       tmpval = -1;
2123       goto dcmp;
2124
2125     insn_dcmpg:
2126       tmpval = 1;
2127
2128     dcmp:
2129       {
2130         jdouble value2 = POPD ();
2131         jdouble value1 = POPD ();
2132         if (value1 > value2)
2133           PUSHI (1);
2134         else if (value1 == value2)
2135           PUSHI (0);
2136         else if (value1 < value2)
2137           PUSHI (-1);
2138         else
2139           PUSHI (tmpval);
2140       }
2141       NEXT_INSN;
2142
2143     insn_ifeq:
2144       {
2145         if (POPI() == 0)
2146           TAKE_GOTO;
2147         else
2148           SKIP_GOTO;
2149       }
2150       NEXT_INSN;
2151
2152     insn_ifne:
2153       {
2154         if (POPI() != 0)
2155           TAKE_GOTO;
2156         else
2157           SKIP_GOTO;
2158       }
2159       NEXT_INSN;
2160
2161     insn_iflt:
2162       {
2163         if (POPI() < 0)
2164           TAKE_GOTO;
2165         else
2166           SKIP_GOTO;
2167       }
2168       NEXT_INSN;
2169
2170     insn_ifge:
2171       {
2172         if (POPI() >= 0)
2173           TAKE_GOTO;
2174         else
2175           SKIP_GOTO;
2176       }
2177       NEXT_INSN;
2178
2179     insn_ifgt:
2180       {
2181         if (POPI() > 0)
2182           TAKE_GOTO;
2183         else
2184           SKIP_GOTO;
2185       }
2186       NEXT_INSN;
2187
2188     insn_ifle:
2189       {
2190         if (POPI() <= 0)
2191           TAKE_GOTO;
2192         else
2193           SKIP_GOTO;
2194       }
2195       NEXT_INSN;
2196
2197     insn_if_icmpeq:
2198       {
2199         jint value2 = POPI();
2200         jint value1 = POPI();
2201         if (value1 == value2)
2202           TAKE_GOTO;
2203         else
2204           SKIP_GOTO;
2205       }
2206       NEXT_INSN;
2207
2208     insn_if_icmpne:
2209       {
2210         jint value2 = POPI();
2211         jint value1 = POPI();
2212         if (value1 != value2)
2213           TAKE_GOTO;
2214         else
2215           SKIP_GOTO;
2216       }
2217       NEXT_INSN;
2218
2219     insn_if_icmplt:
2220       {
2221         jint value2 = POPI();
2222         jint value1 = POPI();
2223         if (value1 < value2)
2224           TAKE_GOTO;
2225         else
2226           SKIP_GOTO;
2227       }
2228       NEXT_INSN;
2229
2230     insn_if_icmpge:
2231       {
2232         jint value2 = POPI();
2233         jint value1 = POPI();
2234         if (value1 >= value2)
2235           TAKE_GOTO;
2236         else
2237           SKIP_GOTO;
2238       }
2239       NEXT_INSN;
2240
2241     insn_if_icmpgt:
2242       {
2243         jint value2 = POPI();
2244         jint value1 = POPI();
2245         if (value1 > value2)
2246           TAKE_GOTO;
2247         else
2248           SKIP_GOTO;
2249       }
2250       NEXT_INSN;
2251
2252     insn_if_icmple:
2253       {
2254         jint value2 = POPI();
2255         jint value1 = POPI();
2256         if (value1 <= value2)
2257           TAKE_GOTO;
2258         else
2259           SKIP_GOTO;
2260       }
2261       NEXT_INSN;
2262
2263     insn_if_acmpeq:
2264       {
2265         jobject value2 = POPA();
2266         jobject value1 = POPA();
2267         if (value1 == value2)
2268           TAKE_GOTO;
2269         else
2270           SKIP_GOTO;
2271       }
2272       NEXT_INSN;
2273
2274     insn_if_acmpne:
2275       {
2276         jobject value2 = POPA();
2277         jobject value1 = POPA();
2278         if (value1 != value2)
2279           TAKE_GOTO;
2280         else
2281           SKIP_GOTO;
2282       }
2283       NEXT_INSN;
2284
2285     insn_goto_w:
2286 #ifndef DIRECT_THREADED
2287       // For direct threaded, goto and goto_w are the same.
2288       pc = pc - 1 + get4 (pc);
2289       NEXT_INSN;
2290 #endif /* DIRECT_THREADED */
2291     insn_goto:
2292       TAKE_GOTO;
2293       NEXT_INSN;
2294
2295     insn_jsr_w:
2296 #ifndef DIRECT_THREADED
2297       // For direct threaded, jsr and jsr_w are the same.
2298       {
2299         pc_t next = pc - 1 + get4 (pc);
2300         pc += 4;
2301         PUSHA ((jobject) pc);
2302         pc = next;
2303       }
2304       NEXT_INSN;
2305 #endif /* DIRECT_THREADED */
2306     insn_jsr:
2307       {
2308         pc_t next = GOTO_VAL();
2309         SKIP_GOTO;
2310         PUSHA ((jobject) pc);
2311         pc = next;
2312       }
2313       NEXT_INSN;
2314
2315     insn_ret:
2316       {
2317         jint index = GET1U ();
2318         pc = (pc_t) PEEKA (index);
2319       }
2320       NEXT_INSN;
2321
2322     insn_tableswitch:
2323       {
2324 #ifdef DIRECT_THREADED
2325         void *def = (pc++)->datum;
2326
2327         int index = POPI();
2328
2329         jint low = INTVAL ();
2330         jint high = INTVAL ();
2331
2332         if (index < low || index > high)
2333           pc = (insn_slot *) def;
2334         else
2335           pc = (insn_slot *) ((pc + index - low)->datum);
2336 #else
2337         pc_t base_pc = pc - 1;
2338         int index = POPI ();
2339
2340         pc_t base = (pc_t) bytecode ();
2341         while ((pc - base) % 4 != 0)
2342           ++pc;
2343
2344         jint def = get4 (pc);
2345         jint low = get4 (pc + 4);
2346         jint high = get4 (pc + 8);
2347         if (index < low || index > high)
2348           pc = base_pc + def;
2349         else
2350           pc = base_pc + get4 (pc + 4 * (index - low + 3));
2351 #endif /* DIRECT_THREADED */
2352       }
2353       NEXT_INSN;
2354
2355     insn_lookupswitch:
2356       {
2357 #ifdef DIRECT_THREADED
2358         void *def = (pc++)->insn;
2359
2360         int index = POPI();
2361
2362         jint npairs = INTVAL ();
2363
2364         int max = npairs - 1;
2365         int min = 0;
2366
2367         // Simple binary search...
2368         while (min < max)
2369           {
2370             int half = (min + max) / 2;
2371             int match = pc[2 * half].int_val;
2372
2373             if (index == match)
2374               {
2375                 // Found it.
2376                 pc = (insn_slot *) pc[2 * half + 1].datum;
2377                 NEXT_INSN;
2378               }
2379             else if (index < match)
2380               // We can use HALF - 1 here because we check again on
2381               // loop exit.
2382               max = half - 1;
2383             else
2384               // We can use HALF + 1 here because we check again on
2385               // loop exit.
2386               min = half + 1;
2387           }
2388         if (index == pc[2 * min].int_val)
2389           pc = (insn_slot *) pc[2 * min + 1].datum;
2390         else
2391           pc = (insn_slot *) def;
2392 #else
2393         unsigned char *base_pc = pc-1;
2394         int index = POPI();
2395
2396         unsigned char* base = bytecode ();
2397         while ((pc-base) % 4 != 0)
2398           ++pc;
2399
2400         jint def     = get4 (pc);
2401         jint npairs  = get4 (pc+4);
2402
2403         int max = npairs-1;
2404         int min = 0;
2405
2406         // Simple binary search...
2407         while (min < max)
2408           {
2409             int half = (min+max)/2;
2410             int match = get4 (pc+ 4*(2 + 2*half));
2411
2412             if (index == match)
2413               min = max = half;
2414             else if (index < match)
2415               // We can use HALF - 1 here because we check again on
2416               // loop exit.
2417               max = half - 1;
2418             else
2419               // We can use HALF + 1 here because we check again on
2420               // loop exit.
2421               min = half + 1;
2422           }
2423
2424         if (index == get4 (pc+ 4*(2 + 2*min)))
2425           pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2426         else
2427           pc = base_pc + def;    
2428 #endif /* DIRECT_THREADED */
2429       }
2430       NEXT_INSN;
2431
2432     insn_areturn:
2433       *(jobject *) retp = POPA ();
2434       return;
2435
2436     insn_lreturn:
2437       *(jlong *) retp = POPL ();
2438       return;
2439
2440     insn_freturn:
2441       *(jfloat *) retp = POPF ();
2442       return;
2443
2444     insn_dreturn:
2445       *(jdouble *) retp = POPD ();
2446       return;
2447
2448     insn_ireturn:
2449       *(jint *) retp = POPI ();
2450       return;
2451
2452     insn_return:
2453       return;
2454
2455     insn_getstatic:
2456       {
2457         jint fieldref_index = GET2U ();
2458         SAVE_PC(); // Constant pool resolution could throw.
2459         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2460         _Jv_Field *field = pool_data[fieldref_index].field;
2461
2462         if ((field->flags & Modifier::STATIC) == 0)
2463           throw_incompatible_class_change_error 
2464             (JvNewStringLatin1 ("field no longer static"));
2465
2466         jclass type = field->type;
2467
2468         // We rewrite the instruction once we discover what it refers
2469         // to.
2470         void *newinsn = NULL;
2471         if (type->isPrimitive ())
2472           {
2473             switch (type->size_in_bytes)
2474               {
2475               case 1:
2476                 PUSHI (*field->u.byte_addr);
2477                 newinsn = AMPAMP (getstatic_resolved_1);
2478                 break;
2479
2480               case 2:
2481                 if (type == JvPrimClass (char))
2482                   {
2483                     PUSHI (*field->u.char_addr);
2484                     newinsn = AMPAMP (getstatic_resolved_char);
2485                   }
2486                 else
2487                   {
2488                     PUSHI (*field->u.short_addr);
2489                     newinsn = AMPAMP (getstatic_resolved_short);
2490                   }
2491                 break;
2492
2493               case 4:
2494                 PUSHI(*field->u.int_addr);
2495                 newinsn = AMPAMP (getstatic_resolved_4);
2496                 break;
2497
2498               case 8:
2499                 PUSHL(*field->u.long_addr);
2500                 newinsn = AMPAMP (getstatic_resolved_8);
2501                 break;
2502               }
2503           }
2504         else
2505           {
2506             PUSHA(*field->u.object_addr);
2507             newinsn = AMPAMP (getstatic_resolved_obj);
2508           }
2509
2510 #ifdef DIRECT_THREADED
2511         pc[-2].insn = newinsn;
2512         pc[-1].datum = field->u.addr;
2513 #endif /* DIRECT_THREADED */
2514       }
2515       NEXT_INSN;
2516
2517 #ifdef DIRECT_THREADED
2518     getstatic_resolved_1:
2519       PUSHI (*(jbyte *) AVAL ());
2520       NEXT_INSN;
2521
2522     getstatic_resolved_char:
2523       PUSHI (*(jchar *) AVAL ());
2524       NEXT_INSN;
2525
2526     getstatic_resolved_short:
2527       PUSHI (*(jshort *) AVAL ());
2528       NEXT_INSN;
2529
2530     getstatic_resolved_4:
2531       PUSHI (*(jint *) AVAL ());
2532       NEXT_INSN;
2533
2534     getstatic_resolved_8:
2535       PUSHL (*(jlong *) AVAL ());
2536       NEXT_INSN;
2537
2538     getstatic_resolved_obj:
2539       PUSHA (*(jobject *) AVAL ());
2540       NEXT_INSN;
2541 #endif /* DIRECT_THREADED */
2542
2543     insn_getfield:
2544       {
2545         jint fieldref_index = GET2U ();
2546         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2547         _Jv_Field *field = pool_data[fieldref_index].field;
2548
2549         if ((field->flags & Modifier::STATIC) != 0)
2550           throw_incompatible_class_change_error 
2551             (JvNewStringLatin1 ("field is static"));
2552
2553         jclass type = field->type;
2554         jint field_offset = field->u.boffset;
2555         if (field_offset > 0xffff)
2556           throw new java::lang::VirtualMachineError;
2557
2558         jobject obj   = POPA();
2559         NULLCHECK(obj);
2560
2561         void *newinsn = NULL;
2562         _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2563         if (type->isPrimitive ())
2564           {
2565             switch (type->size_in_bytes)
2566               {
2567               case 1:
2568                 PUSHI (val->byte_value);
2569                 newinsn = AMPAMP (getfield_resolved_1);
2570                 break;
2571
2572               case 2:
2573                 if (type == JvPrimClass (char))
2574                   {
2575                     PUSHI (val->char_value);
2576                     newinsn = AMPAMP (getfield_resolved_char);
2577                   }
2578                 else
2579                   {
2580                     PUSHI (val->short_value);
2581                     newinsn = AMPAMP (getfield_resolved_short);
2582                   }
2583                 break;
2584
2585               case 4:
2586                 PUSHI (val->int_value);
2587                 newinsn = AMPAMP (getfield_resolved_4);
2588                 break;
2589
2590               case 8:
2591                 PUSHL (val->long_value);
2592                 newinsn = AMPAMP (getfield_resolved_8);
2593                 break;
2594               }
2595           }
2596         else
2597           {
2598             PUSHA (val->object_value);
2599             newinsn = AMPAMP (getfield_resolved_obj);
2600           }
2601
2602 #ifdef DIRECT_THREADED
2603         pc[-2].insn = newinsn;
2604         pc[-1].int_val = field_offset;
2605 #endif /* DIRECT_THREADED */
2606       }
2607       NEXT_INSN;
2608
2609 #ifdef DIRECT_THREADED
2610     getfield_resolved_1:
2611       {
2612         char *obj = (char *) POPA ();
2613         NULLCHECK (obj);
2614         PUSHI (*(jbyte *) (obj + INTVAL ()));
2615       }
2616       NEXT_INSN;
2617
2618     getfield_resolved_char:
2619       {
2620         char *obj = (char *) POPA ();
2621         NULLCHECK (obj);
2622         PUSHI (*(jchar *) (obj + INTVAL ()));
2623       }
2624       NEXT_INSN;
2625
2626     getfield_resolved_short:
2627       {
2628         char *obj = (char *) POPA ();
2629         NULLCHECK (obj);
2630         PUSHI (*(jshort *) (obj + INTVAL ()));
2631       }
2632       NEXT_INSN;
2633
2634     getfield_resolved_4:
2635       {
2636         char *obj = (char *) POPA ();
2637         NULLCHECK (obj);
2638         PUSHI (*(jint *) (obj + INTVAL ()));
2639       }
2640       NEXT_INSN;
2641
2642     getfield_resolved_8:
2643       {
2644         char *obj = (char *) POPA ();
2645         NULLCHECK (obj);
2646         PUSHL (*(jlong *) (obj + INTVAL ()));
2647       }
2648       NEXT_INSN;
2649
2650     getfield_resolved_obj:
2651       {
2652         char *obj = (char *) POPA ();
2653         NULLCHECK (obj);
2654         PUSHA (*(jobject *) (obj + INTVAL ()));
2655       }
2656       NEXT_INSN;
2657 #endif /* DIRECT_THREADED */
2658
2659     insn_putstatic:
2660       {
2661         jint fieldref_index = GET2U ();
2662         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2663         _Jv_Field *field = pool_data[fieldref_index].field;
2664
2665         jclass type = field->type;
2666
2667         // ResolvePoolEntry cannot check this
2668         if ((field->flags & Modifier::STATIC) == 0)
2669           throw_incompatible_class_change_error 
2670             (JvNewStringLatin1 ("field no longer static"));
2671
2672         void *newinsn = NULL;
2673         if (type->isPrimitive ())
2674           {
2675             switch (type->size_in_bytes) 
2676               {
2677               case 1:
2678                 {
2679                   jint value = POPI();
2680                   *field->u.byte_addr = value;
2681                   newinsn = AMPAMP (putstatic_resolved_1);
2682                   break;
2683                 }
2684
2685               case 2:
2686                 {
2687                   jint value = POPI();
2688                   *field->u.char_addr = value;
2689                   newinsn = AMPAMP (putstatic_resolved_2);
2690                   break;
2691                 }
2692
2693               case 4:
2694                 {
2695                   jint value = POPI();
2696                   *field->u.int_addr = value;
2697                   newinsn = AMPAMP (putstatic_resolved_4);
2698                   break;
2699                 }
2700
2701               case 8:
2702                 {
2703                   jlong value = POPL();
2704                   *field->u.long_addr = value;
2705                   newinsn = AMPAMP (putstatic_resolved_8);
2706                   break;
2707                 }
2708               }
2709           }
2710         else
2711           {
2712             jobject value = POPA();
2713             *field->u.object_addr = value;
2714             newinsn = AMPAMP (putstatic_resolved_obj);
2715           }
2716
2717 #ifdef DIRECT_THREADED
2718         pc[-2].insn = newinsn;
2719         pc[-1].datum = field->u.addr;
2720 #endif /* DIRECT_THREADED */
2721       }
2722       NEXT_INSN;
2723
2724 #ifdef DIRECT_THREADED
2725     putstatic_resolved_1:
2726       *(jbyte *) AVAL () = POPI ();
2727       NEXT_INSN;
2728
2729     putstatic_resolved_2:
2730       *(jchar *) AVAL () = POPI ();
2731       NEXT_INSN;
2732
2733     putstatic_resolved_4:
2734       *(jint *) AVAL () = POPI ();
2735       NEXT_INSN;
2736
2737     putstatic_resolved_8:
2738       *(jlong *) AVAL () = POPL ();
2739       NEXT_INSN;
2740
2741     putstatic_resolved_obj:
2742       *(jobject *) AVAL () = POPA ();
2743       NEXT_INSN;
2744 #endif /* DIRECT_THREADED */
2745
2746     insn_putfield:
2747       {
2748         jint fieldref_index = GET2U ();
2749         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2750         _Jv_Field *field = pool_data[fieldref_index].field;
2751
2752         jclass type = field->type;
2753
2754         if ((field->flags & Modifier::STATIC) != 0)
2755           throw_incompatible_class_change_error 
2756             (JvNewStringLatin1 ("field is static"));
2757
2758         jint field_offset = field->u.boffset;
2759         if (field_offset > 0xffff)
2760           throw new java::lang::VirtualMachineError;
2761
2762         void *newinsn = NULL;
2763         if (type->isPrimitive ())
2764           {
2765             switch (type->size_in_bytes) 
2766               {
2767               case 1:
2768                 {
2769                   jint    value = POPI();
2770                   jobject obj   = POPA();
2771                   NULLCHECK(obj);
2772                   *(jbyte*) ((char*)obj + field_offset) = value;
2773                   newinsn = AMPAMP (putfield_resolved_1);
2774                   break;
2775                 }
2776
2777               case 2:
2778                 {
2779                   jint    value = POPI();
2780                   jobject obj   = POPA();
2781                   NULLCHECK(obj);
2782                   *(jchar*) ((char*)obj + field_offset) = value;
2783                   newinsn = AMPAMP (putfield_resolved_2);
2784                   break;
2785                 }
2786
2787               case 4:
2788                 {
2789                   jint    value = POPI();
2790                   jobject obj   = POPA();
2791                   NULLCHECK(obj);
2792                   *(jint*) ((char*)obj + field_offset) = value;
2793                   newinsn = AMPAMP (putfield_resolved_4);
2794                   break;
2795                 }
2796
2797               case 8:
2798                 {
2799                   jlong   value = POPL();
2800                   jobject obj   = POPA();
2801                   NULLCHECK(obj);
2802                   *(jlong*) ((char*)obj + field_offset) = value;
2803                   newinsn = AMPAMP (putfield_resolved_8);
2804                   break;
2805                 }
2806               }
2807           }
2808         else
2809           {
2810             jobject value = POPA();
2811             jobject obj   = POPA();
2812             NULLCHECK(obj);
2813             *(jobject*) ((char*)obj + field_offset) = value;
2814             newinsn = AMPAMP (putfield_resolved_obj);
2815           }
2816
2817 #ifdef DIRECT_THREADED
2818         pc[-2].insn = newinsn;
2819         pc[-1].int_val = field_offset;
2820 #endif /* DIRECT_THREADED */
2821       }
2822       NEXT_INSN;
2823
2824 #ifdef DIRECT_THREADED
2825     putfield_resolved_1:
2826       {
2827         jint val = POPI ();
2828         char *obj = (char *) POPA ();
2829         NULLCHECK (obj);
2830         *(jbyte *) (obj + INTVAL ()) = val;
2831       }
2832       NEXT_INSN;
2833
2834     putfield_resolved_2:
2835       {
2836         jint val = POPI ();
2837         char *obj = (char *) POPA ();
2838         NULLCHECK (obj);
2839         *(jchar *) (obj + INTVAL ()) = val;
2840       }
2841       NEXT_INSN;
2842
2843     putfield_resolved_4:
2844       {
2845         jint val = POPI ();
2846         char *obj = (char *) POPA ();
2847         NULLCHECK (obj);
2848         *(jint *) (obj + INTVAL ()) = val;
2849       }
2850       NEXT_INSN;
2851
2852     putfield_resolved_8:
2853       {
2854         jlong val = POPL ();
2855         char *obj = (char *) POPA ();
2856         NULLCHECK (obj);
2857         *(jlong *) (obj + INTVAL ()) = val;
2858       }
2859       NEXT_INSN;
2860
2861     putfield_resolved_obj:
2862       {
2863         jobject val = POPA ();
2864         char *obj = (char *) POPA ();
2865         NULLCHECK (obj);
2866         *(jobject *) (obj + INTVAL ()) = val;
2867       }
2868       NEXT_INSN;
2869 #endif /* DIRECT_THREADED */
2870
2871     insn_invokespecial:
2872       {
2873         int index = GET2U ();
2874
2875         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2876                                                    index)).rmethod;
2877
2878         sp -= rmeth->stack_item_count;
2879
2880         // We don't use NULLCHECK here because we can't rely on that
2881         // working for <init>.  So instead we do an explicit test.
2882         if (! sp[0].o)
2883           {
2884             SAVE_PC();
2885             throw new java::lang::NullPointerException;
2886           }
2887
2888         fun = (void (*)()) rmeth->method->ncode;
2889
2890 #ifdef DIRECT_THREADED
2891         // Rewrite instruction so that we use a faster pre-resolved
2892         // method.
2893         pc[-2].insn = &&invokespecial_resolved;
2894         pc[-1].datum = rmeth;
2895 #endif /* DIRECT_THREADED */
2896       }
2897       goto perform_invoke;
2898
2899 #ifdef DIRECT_THREADED
2900     invokespecial_resolved:
2901       {
2902         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2903         sp -= rmeth->stack_item_count;
2904         // We don't use NULLCHECK here because we can't rely on that
2905         // working for <init>.  So instead we do an explicit test.
2906         if (! sp[0].o)
2907           {
2908             SAVE_PC();
2909             throw new java::lang::NullPointerException;
2910           }
2911         fun = (void (*)()) rmeth->method->ncode;
2912       }
2913       goto perform_invoke;
2914 #endif /* DIRECT_THREADED */
2915
2916     insn_invokestatic:
2917       {
2918         int index = GET2U ();
2919
2920         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2921                                                    index)).rmethod;
2922
2923         sp -= rmeth->stack_item_count;
2924
2925         fun = (void (*)()) rmeth->method->ncode;
2926
2927 #ifdef DIRECT_THREADED
2928         // Rewrite instruction so that we use a faster pre-resolved
2929         // method.
2930         pc[-2].insn = &&invokestatic_resolved;
2931         pc[-1].datum = rmeth;
2932 #endif /* DIRECT_THREADED */
2933       }
2934       goto perform_invoke;
2935
2936 #ifdef DIRECT_THREADED
2937     invokestatic_resolved:
2938       {
2939         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2940         sp -= rmeth->stack_item_count;
2941         fun = (void (*)()) rmeth->method->ncode;
2942       }
2943       goto perform_invoke;
2944 #endif /* DIRECT_THREADED */
2945
2946     insn_invokeinterface:
2947       {
2948         int index = GET2U ();
2949
2950         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2951                                                    index)).rmethod;
2952
2953         sp -= rmeth->stack_item_count;
2954
2955         jobject rcv = sp[0].o;
2956
2957         NULLCHECK (rcv);
2958
2959         fun = (void (*)())
2960           _Jv_LookupInterfaceMethod (rcv->getClass (),
2961                                      rmeth->method->name,
2962                                      rmeth->method->signature);
2963
2964 #ifdef DIRECT_THREADED
2965         // Rewrite instruction so that we use a faster pre-resolved
2966         // method.
2967         pc[-2].insn = &&invokeinterface_resolved;
2968         pc[-1].datum = rmeth;
2969 #else
2970         // Skip dummy bytes.
2971         pc += 2;
2972 #endif /* DIRECT_THREADED */
2973       }
2974       goto perform_invoke;
2975
2976 #ifdef DIRECT_THREADED
2977     invokeinterface_resolved:
2978       {
2979         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2980         sp -= rmeth->stack_item_count;
2981         jobject rcv = sp[0].o;
2982         NULLCHECK (rcv);
2983         fun = (void (*)())
2984           _Jv_LookupInterfaceMethod (rcv->getClass (),
2985                                      rmeth->method->name,
2986                                      rmeth->method->signature);
2987       }
2988       goto perform_invoke;
2989 #endif /* DIRECT_THREADED */
2990
2991     insn_new:
2992       {
2993         int index = GET2U ();
2994         jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2995                                                           index)).clazz;
2996         /* VM spec, section 3.11.5 */
2997         if ((klass->getModifiers() & Modifier::ABSTRACT)
2998             || klass->isInterface())
2999           throw new java::lang::InstantiationException;
3000         jobject res = _Jv_AllocObject (klass);
3001         PUSHA (res);
3002
3003 #ifdef DIRECT_THREADED
3004         pc[-2].insn = &&new_resolved;
3005         pc[-1].datum = klass;
3006 #endif /* DIRECT_THREADED */
3007       }
3008       NEXT_INSN;
3009
3010 #ifdef DIRECT_THREADED
3011     new_resolved:
3012       {
3013         jclass klass = (jclass) AVAL ();
3014         jobject res = _Jv_AllocObject (klass);
3015         PUSHA (res);
3016       }
3017       NEXT_INSN;
3018 #endif /* DIRECT_THREADED */
3019
3020     insn_newarray:
3021       {
3022         int atype = GET1U ();
3023         int size  = POPI();
3024         jobject result = _Jv_NewArray (atype, size);
3025         PUSHA (result);
3026       }
3027       NEXT_INSN;
3028
3029     insn_anewarray:
3030       {
3031         int index = GET2U ();
3032         jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3033                                                           index)).clazz;
3034         int size  = POPI();
3035         jobject result = _Jv_NewObjectArray (size, klass, 0);
3036         PUSHA (result);
3037
3038 #ifdef DIRECT_THREADED
3039         pc[-2].insn = &&anewarray_resolved;
3040         pc[-1].datum = klass;
3041 #endif /* DIRECT_THREADED */
3042       }
3043       NEXT_INSN;
3044
3045 #ifdef DIRECT_THREADED
3046     anewarray_resolved:
3047       {
3048         jclass klass = (jclass) AVAL ();
3049         int size = POPI ();
3050         jobject result = _Jv_NewObjectArray (size, klass, 0);
3051         PUSHA (result);
3052       }
3053       NEXT_INSN;
3054 #endif /* DIRECT_THREADED */
3055
3056     insn_arraylength:
3057       {
3058         __JArray *arr = (__JArray*)POPA();
3059         NULLARRAYCHECK (arr);
3060         PUSHI (arr->length);
3061       }
3062       NEXT_INSN;
3063
3064     insn_athrow:
3065       {
3066         jobject value = POPA();
3067         throw static_cast<jthrowable>(value);
3068       }
3069       NEXT_INSN;
3070
3071     insn_checkcast:
3072       {
3073         SAVE_PC();
3074         jobject value = POPA();
3075         jint index = GET2U ();
3076         jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3077                                                        index)).clazz;
3078
3079         value = (jobject) _Jv_CheckCast (to, value);
3080
3081         PUSHA (value);
3082
3083 #ifdef DIRECT_THREADED
3084         pc[-2].insn = &&checkcast_resolved;
3085         pc[-1].datum = to;
3086 #endif /* DIRECT_THREADED */
3087       }
3088       NEXT_INSN;
3089
3090 #ifdef DIRECT_THREADED
3091     checkcast_resolved:
3092       {
3093         SAVE_PC();
3094         jobject value = POPA ();
3095         jclass to = (jclass) AVAL ();
3096         value = (jobject) _Jv_CheckCast (to, value);
3097         PUSHA (value);
3098       }
3099       NEXT_INSN;
3100 #endif /* DIRECT_THREADED */
3101
3102     insn_instanceof:
3103       {
3104         SAVE_PC();
3105         jobject value = POPA();
3106         jint index = GET2U ();
3107         jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3108                                                        index)).clazz;
3109         PUSHI (to->isInstance (value));
3110
3111 #ifdef DIRECT_THREADED
3112         pc[-2].insn = &&instanceof_resolved;
3113         pc[-1].datum = to;
3114 #endif /* DIRECT_THREADED */
3115       }
3116       NEXT_INSN;
3117
3118 #ifdef DIRECT_THREADED
3119     instanceof_resolved:
3120       {
3121         jobject value = POPA ();
3122         jclass to = (jclass) AVAL ();
3123         PUSHI (to->isInstance (value));
3124       }
3125       NEXT_INSN;
3126 #endif /* DIRECT_THREADED */
3127
3128     insn_monitorenter:
3129       {
3130         jobject value = POPA();
3131         NULLCHECK(value);
3132         _Jv_MonitorEnter (value);
3133       }
3134       NEXT_INSN;
3135
3136     insn_monitorexit:
3137       {
3138         jobject value = POPA();
3139         NULLCHECK(value);
3140         _Jv_MonitorExit (value);
3141       }
3142       NEXT_INSN;
3143
3144     insn_ifnull:
3145       {
3146         jobject val = POPA();
3147         if (val == NULL)
3148           TAKE_GOTO;
3149         else
3150           SKIP_GOTO;
3151       }
3152       NEXT_INSN;
3153
3154     insn_ifnonnull:
3155       {
3156         jobject val = POPA();
3157         if (val != NULL)
3158           TAKE_GOTO;
3159         else
3160           SKIP_GOTO;
3161       }
3162       NEXT_INSN;
3163
3164     insn_multianewarray:
3165       {
3166         int kind_index = GET2U ();
3167         int dim        = GET1U ();
3168
3169         jclass type    
3170           = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3171                                                kind_index)).clazz;
3172         jint *sizes    = (jint*) __builtin_alloca (sizeof (jint)*dim);
3173
3174         for (int i = dim - 1; i >= 0; i--)
3175           {
3176             sizes[i] = POPI ();
3177           }
3178
3179         jobject res    = _Jv_NewMultiArray (type,dim, sizes);
3180
3181         PUSHA (res);
3182       }
3183       NEXT_INSN;
3184
3185 #ifndef DIRECT_THREADED
3186     insn_wide:
3187       {
3188         jint the_mod_op = get1u (pc++);
3189         jint wide       = get2u (pc); pc += 2;
3190
3191         switch (the_mod_op)
3192           {
3193           case op_istore:
3194             STOREI (wide);
3195             NEXT_INSN;
3196
3197           case op_fstore:
3198             STOREF (wide);
3199             NEXT_INSN;
3200
3201           case op_astore:
3202             STOREA (wide);
3203             NEXT_INSN;
3204
3205           case op_lload:
3206             LOADL (wide);
3207             NEXT_INSN;
3208
3209           case op_dload:
3210             LOADD (wide);
3211             NEXT_INSN;
3212
3213           case op_iload:
3214             LOADI (wide);
3215             NEXT_INSN;
3216
3217           case op_fload:
3218             LOADF (wide);
3219             NEXT_INSN;
3220
3221           case op_aload:
3222             LOADA (wide);
3223             NEXT_INSN;
3224
3225           case op_lstore:
3226             STOREL (wide);
3227             NEXT_INSN;
3228
3229           case op_dstore:
3230             STORED (wide);
3231             NEXT_INSN;
3232
3233           case op_ret:
3234             pc = (unsigned char*) PEEKA (wide);
3235             NEXT_INSN;
3236
3237           case op_iinc:
3238             {
3239               jint amount = get2s (pc); pc += 2;
3240               jint value = PEEKI (wide);
3241               POKEI (wide, value+amount);
3242             }
3243             NEXT_INSN;
3244
3245           default:
3246             throw_internal_error ("illegal bytecode modified by wide");
3247           }
3248
3249       }
3250 #endif /* DIRECT_THREADED */
3251     }
3252   catch (java::lang::Throwable *ex)
3253     {
3254 #ifdef DIRECT_THREADED
3255       void *logical_pc = (void *) ((insn_slot *) pc - 1);
3256 #else
3257       int logical_pc = pc - 1 - bytecode ();
3258 #endif
3259       _Jv_InterpException *exc = meth->exceptions ();
3260       jclass exc_class = ex->getClass ();
3261
3262       for (int i = 0; i < meth->exc_count; i++)
3263         {
3264           if (PCVAL (exc[i].start_pc) <= logical_pc
3265               && logical_pc < PCVAL (exc[i].end_pc))
3266             {
3267 #ifdef DIRECT_THREADED
3268               jclass handler = (jclass) exc[i].handler_type.p;
3269 #else
3270               jclass handler = NULL;
3271               if (exc[i].handler_type.i != 0)
3272                 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3273                                                              exc[i].handler_type.i)).clazz;
3274 #endif /* DIRECT_THREADED */
3275
3276               if (handler == NULL || handler->isAssignableFrom (exc_class))
3277                 {
3278 #ifdef DIRECT_THREADED
3279                   pc = (insn_slot *) exc[i].handler_pc.p;
3280 #else
3281                   pc = bytecode () + exc[i].handler_pc.i;
3282 #endif /* DIRECT_THREADED */
3283                   sp = stack;
3284                   sp++->o = ex; // Push exception.
3285                   NEXT_INSN;
3286                 }
3287             }
3288         }
3289
3290       // No handler, so re-throw.
3291       throw ex;
3292     }
3293 }
3294
3295 static void
3296 throw_internal_error (char *msg)
3297 {
3298   throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3299 }
3300
3301 static void 
3302 throw_incompatible_class_change_error (jstring msg)
3303 {
3304   throw new java::lang::IncompatibleClassChangeError (msg);
3305 }
3306
3307 #ifndef HANDLE_SEGV
3308 static java::lang::NullPointerException *null_pointer_exc;
3309 static void 
3310 throw_null_pointer_exception ()
3311 {
3312   if (null_pointer_exc == NULL)
3313     null_pointer_exc = new java::lang::NullPointerException;
3314
3315   throw null_pointer_exc;
3316 }
3317 #endif
3318
3319 /* Look up source code line number for given bytecode (or direct threaded
3320    interpreter) PC. */
3321 int
3322 _Jv_InterpMethod::get_source_line(pc_t mpc)
3323 {
3324   int line = line_table_len > 0 ? line_table[0].line : -1;
3325   for (int i = 1; i < line_table_len; i++)
3326     if (line_table[i].pc > mpc)
3327       break;
3328     else
3329       line = line_table[i].line;
3330
3331   return line;
3332 }
3333
3334 /** Do static initialization for fields with a constant initializer */
3335 void
3336 _Jv_InitField (jobject obj, jclass klass, int index)
3337 {
3338   using namespace java::lang::reflect;
3339
3340   if (obj != 0 && klass == 0)
3341     klass = obj->getClass ();
3342
3343   if (!_Jv_IsInterpretedClass (klass))
3344     return;
3345
3346   _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3347
3348   _Jv_Field * field = (&klass->fields[0]) + index;
3349
3350   if (index > klass->field_count)
3351     throw_internal_error ("field out of range");
3352
3353   int init = iclass->field_initializers[index];
3354   if (init == 0)
3355     return;
3356
3357   _Jv_Constants *pool = &klass->constants;
3358   int tag = pool->tags[init];
3359
3360   if (! field->isResolved ())
3361     throw_internal_error ("initializing unresolved field");
3362
3363   if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3364     throw_internal_error ("initializing non-static field with no object");
3365
3366   void *addr = 0;
3367
3368   if ((field->flags & Modifier::STATIC) != 0)
3369     addr = (void*) field->u.addr;
3370   else
3371     addr = (void*) (((char*)obj) + field->u.boffset);
3372
3373   switch (tag)
3374     {
3375     case JV_CONSTANT_String:
3376       {
3377         jstring str;
3378         str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3379         pool->data[init].string = str;
3380         pool->tags[init] = JV_CONSTANT_ResolvedString;
3381       }
3382       /* fall through */
3383
3384     case JV_CONSTANT_ResolvedString:
3385       if (! (field->type == &java::lang::String::class$
3386              || field->type == &java::lang::Class::class$))
3387         throw_class_format_error ("string initialiser to non-string field");
3388
3389       *(jstring*)addr = pool->data[init].string;
3390       break;
3391
3392     case JV_CONSTANT_Integer:
3393       {
3394         int value = pool->data[init].i;
3395
3396         if (field->type == JvPrimClass (boolean))
3397           *(jboolean*)addr = (jboolean)value;
3398         
3399         else if (field->type == JvPrimClass (byte))
3400           *(jbyte*)addr = (jbyte)value;
3401         
3402         else if (field->type == JvPrimClass (char))
3403           *(jchar*)addr = (jchar)value;
3404
3405         else if (field->type == JvPrimClass (short))
3406           *(jshort*)addr = (jshort)value;
3407         
3408         else if (field->type == JvPrimClass (int))
3409           *(jint*)addr = (jint)value;
3410
3411         else
3412           throw_class_format_error ("erroneous field initializer");
3413       }  
3414       break;
3415
3416     case JV_CONSTANT_Long:
3417       if (field->type != JvPrimClass (long))
3418         throw_class_format_error ("erroneous field initializer");
3419
3420       *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3421       break;
3422
3423     case JV_CONSTANT_Float:
3424       if (field->type != JvPrimClass (float))
3425         throw_class_format_error ("erroneous field initializer");
3426
3427       *(jfloat*)addr = pool->data[init].f;
3428       break;
3429
3430     case JV_CONSTANT_Double:
3431       if (field->type != JvPrimClass (double))
3432         throw_class_format_error ("erroneous field initializer");
3433
3434       *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3435       break;
3436
3437     default:
3438       throw_class_format_error ("erroneous field initializer");
3439     }
3440 }
3441
3442 inline static unsigned char*
3443 skip_one_type (unsigned char* ptr)
3444 {
3445   int ch = *ptr++;
3446
3447   while (ch == '[')
3448     { 
3449       ch = *ptr++;
3450     }
3451   
3452   if (ch == 'L')
3453     {
3454       do { ch = *ptr++; } while (ch != ';');
3455     }
3456
3457   return ptr;
3458 }
3459
3460 static ffi_type*
3461 get_ffi_type_from_signature (unsigned char* ptr)
3462 {
3463   switch (*ptr) 
3464     {
3465     case 'L':
3466     case '[':
3467       return &ffi_type_pointer;
3468       break;
3469
3470     case 'Z':
3471       // On some platforms a bool is a byte, on others an int.
3472       if (sizeof (jboolean) == sizeof (jbyte))
3473         return &ffi_type_sint8;
3474       else
3475         {
3476           JvAssert (sizeof (jbyte) == sizeof (jint));
3477           return &ffi_type_sint32;
3478         }
3479       break;
3480
3481     case 'B':
3482       return &ffi_type_sint8;
3483       break;
3484       
3485     case 'C':
3486       return &ffi_type_uint16;
3487       break;
3488           
3489     case 'S': 
3490       return &ffi_type_sint16;
3491       break;
3492           
3493     case 'I':
3494       return &ffi_type_sint32;
3495       break;
3496           
3497     case 'J':
3498       return &ffi_type_sint64;
3499       break;
3500           
3501     case 'F':
3502       return &ffi_type_float;
3503       break;
3504           
3505     case 'D':
3506       return &ffi_type_double;
3507       break;
3508
3509     case 'V':
3510       return &ffi_type_void;
3511       break;
3512     }
3513
3514   throw_internal_error ("unknown type in signature");
3515 }
3516
3517 /* this function yields the number of actual arguments, that is, if the
3518  * function is non-static, then one is added to the number of elements
3519  * found in the signature */
3520
3521 int 
3522 _Jv_count_arguments (_Jv_Utf8Const *signature,
3523                      jboolean staticp)
3524 {
3525   unsigned char *ptr = (unsigned char*) signature->chars();
3526   int arg_count = staticp ? 0 : 1;
3527
3528   /* first, count number of arguments */
3529
3530   // skip '('
3531   ptr++;
3532
3533   // count args
3534   while (*ptr != ')')
3535     {
3536       ptr = skip_one_type (ptr);
3537       arg_count += 1;
3538     }
3539
3540   return arg_count;
3541 }
3542
3543 /* This beast will build a cif, given the signature.  Memory for
3544  * the cif itself and for the argument types must be allocated by the
3545  * caller.
3546  */
3547
3548 static int 
3549 init_cif (_Jv_Utf8Const* signature,
3550           int arg_count,
3551           jboolean staticp,
3552           ffi_cif *cif,
3553           ffi_type **arg_types,
3554           ffi_type **rtype_p)
3555 {
3556   unsigned char *ptr = (unsigned char*) signature->chars();
3557
3558   int arg_index = 0;            // arg number
3559   int item_count = 0;           // stack-item count
3560
3561   // setup receiver
3562   if (!staticp)
3563     {
3564       arg_types[arg_index++] = &ffi_type_pointer;
3565       item_count += 1;
3566     }
3567
3568   // skip '('
3569   ptr++;
3570
3571   // assign arg types
3572   while (*ptr != ')')
3573     {
3574       arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3575
3576       if (*ptr == 'J' || *ptr == 'D')
3577         item_count += 2;
3578       else
3579         item_count += 1;
3580
3581       ptr = skip_one_type (ptr);
3582     }
3583
3584   // skip ')'
3585   ptr++;
3586   ffi_type *rtype = get_ffi_type_from_signature (ptr);
3587
3588   ptr = skip_one_type (ptr);
3589   if (ptr != (unsigned char*)signature->chars() + signature->len())
3590     throw_internal_error ("did not find end of signature");
3591
3592   if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3593                     arg_count, rtype, arg_types) != FFI_OK)
3594     throw_internal_error ("ffi_prep_cif failed");
3595
3596   if (rtype_p != NULL)
3597     *rtype_p = rtype;
3598
3599   return item_count;
3600 }
3601
3602 #if FFI_NATIVE_RAW_API
3603 #   define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3604 #   define FFI_RAW_SIZE ffi_raw_size
3605 #else
3606 #   define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3607 #   define FFI_RAW_SIZE ffi_java_raw_size
3608 #endif
3609
3610 /* we put this one here, and not in interpret.cc because it
3611  * calls the utility routines _Jv_count_arguments 
3612  * which are static to this module.  The following struct defines the
3613  * layout we use for the stubs, it's only used in the ncode method. */
3614
3615 typedef struct {
3616   ffi_raw_closure  closure;
3617   ffi_cif   cif;
3618   ffi_type *arg_types[0];
3619 } ncode_closure;
3620
3621 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3622
3623 void *
3624 _Jv_InterpMethod::ncode ()
3625 {
3626   using namespace java::lang::reflect;
3627
3628   if (self->ncode != 0)
3629     return self->ncode;
3630
3631   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3632   int arg_count = _Jv_count_arguments (self->signature, staticp);
3633
3634   ncode_closure *closure =
3635     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3636                                         + arg_count * sizeof (ffi_type*));
3637
3638   init_cif (self->signature,
3639             arg_count,
3640             staticp,
3641             &closure->cif,
3642             &closure->arg_types[0],
3643             NULL);
3644
3645   ffi_closure_fun fun;
3646
3647   args_raw_size = FFI_RAW_SIZE (&closure->cif);
3648
3649   JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3650
3651   if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3652     {
3653       if (staticp)
3654         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3655       else
3656         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object; 
3657     }
3658   else
3659     {
3660       if (staticp)
3661         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3662       else
3663         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3664     }
3665
3666   FFI_PREP_RAW_CLOSURE (&closure->closure,
3667                         &closure->cif, 
3668                         fun,
3669                         (void*)this);
3670
3671   self->ncode = (void*)closure;
3672   return self->ncode;
3673 }
3674
3675 void *
3676 _Jv_JNIMethod::ncode ()
3677 {
3678   using namespace java::lang::reflect;
3679
3680   if (self->ncode != 0)
3681     return self->ncode;
3682
3683   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3684   int arg_count = _Jv_count_arguments (self->signature, staticp);
3685
3686   ncode_closure *closure =
3687     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3688                                     + arg_count * sizeof (ffi_type*));
3689
3690   ffi_type *rtype;
3691   init_cif (self->signature,
3692             arg_count,
3693             staticp,
3694             &closure->cif,
3695             &closure->arg_types[0],
3696             &rtype);
3697
3698   ffi_closure_fun fun;
3699
3700   args_raw_size = FFI_RAW_SIZE (&closure->cif);
3701
3702   // Initialize the argument types and CIF that represent the actual
3703   // underlying JNI function.
3704   int extra_args = 1;
3705   if ((self->accflags & Modifier::STATIC))
3706     ++extra_args;
3707   jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3708                                                 * sizeof (ffi_type *));
3709   int offset = 0;
3710   jni_arg_types[offset++] = &ffi_type_pointer;
3711   if ((self->accflags & Modifier::STATIC))
3712     jni_arg_types[offset++] = &ffi_type_pointer;
3713   memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3714           arg_count * sizeof (ffi_type *));
3715
3716   if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3717                     extra_args + arg_count, rtype,
3718                     jni_arg_types) != FFI_OK)
3719     throw_internal_error ("ffi_prep_cif failed for JNI function");
3720
3721   JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3722
3723   // FIXME: for now we assume that all native methods for
3724   // interpreted code use JNI.
3725   fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3726
3727   FFI_PREP_RAW_CLOSURE (&closure->closure,
3728                         &closure->cif, 
3729                         fun,
3730                         (void*) this);
3731
3732   self->ncode = (void *) closure;
3733   return self->ncode;
3734 }
3735
3736 static void
3737 throw_class_format_error (jstring msg)
3738 {
3739   throw (msg
3740          ? new java::lang::ClassFormatError (msg)
3741          : new java::lang::ClassFormatError);
3742 }
3743
3744 static void
3745 throw_class_format_error (char *msg)
3746 {
3747   throw_class_format_error (JvNewStringLatin1 (msg));
3748 }
3749
3750 \f
3751
3752 void
3753 _Jv_InterpreterEngine::do_verify (jclass klass)
3754 {
3755   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3756   for (int i = 0; i < klass->method_count; i++)
3757     {
3758       using namespace java::lang::reflect;
3759       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3760       _Jv_ushort accflags = klass->methods[i].accflags;
3761       if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3762         {
3763           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3764           _Jv_VerifyMethod (im);
3765         }
3766     }
3767 }
3768
3769 void
3770 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3771 {
3772   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3773   for (int i = 0; i < klass->method_count; i++)
3774     {
3775       // Just skip abstract methods.  This is particularly important
3776       // because we don't resize the interpreted_methods array when
3777       // miranda methods are added to it.
3778       if ((klass->methods[i].accflags
3779            & java::lang::reflect::Modifier::ABSTRACT)
3780           != 0)
3781         continue;
3782
3783       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3784
3785       if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3786           != 0)
3787         {
3788           // You might think we could use a virtual `ncode' method in
3789           // the _Jv_MethodBase and unify the native and non-native
3790           // cases.  Well, we can't, because we don't allocate these
3791           // objects using `new', and thus they don't get a vtable.
3792           _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3793           klass->methods[i].ncode = jnim->ncode ();
3794         }
3795       else if (imeth != 0)              // it could be abstract
3796         {
3797           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3798           klass->methods[i].ncode = im->ncode ();
3799         }
3800     }
3801 }
3802
3803 void
3804 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3805                                                   int static_size)
3806 {
3807   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3808
3809   char *static_data = (char *) _Jv_AllocBytes (static_size);
3810
3811   for (int i = 0; i < klass->field_count; i++)
3812     {
3813       _Jv_Field *field = &klass->fields[i];
3814
3815       if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3816         {
3817           field->u.addr  = static_data + field->u.boffset;
3818               
3819           if (iclass->field_initializers[i] != 0)
3820             {
3821               _Jv_Linker::resolve_field (field, klass->loader);
3822               _Jv_InitField (0, klass, i);
3823             }
3824         }
3825     }
3826
3827   // Now we don't need the field_initializers anymore, so let the
3828   // collector get rid of it.
3829   iclass->field_initializers = 0;
3830 }
3831
3832 _Jv_ResolvedMethod *
3833 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3834                                           jboolean staticp, jint vtable_index)
3835 {
3836   int arg_count = _Jv_count_arguments (method->signature, staticp);
3837
3838   _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3839     _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3840                     + arg_count*sizeof (ffi_type*));
3841
3842   result->stack_item_count
3843     = init_cif (method->signature,
3844                 arg_count,
3845                 staticp,
3846                 &result->cif,
3847                 &result->arg_types[0],
3848                 NULL);
3849
3850   result->vtable_index        = vtable_index;
3851   result->method              = method;
3852   result->klass               = klass;
3853
3854   return result;
3855 }
3856
3857 void
3858 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3859 {
3860   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3861   for (int i = 0; i < klass->method_count; i++)
3862     {
3863       // Just skip abstract methods.  This is particularly important
3864       // because we don't resize the interpreted_methods array when
3865       // miranda methods are added to it.
3866       if ((klass->methods[i].accflags
3867            & java::lang::reflect::Modifier::ABSTRACT)
3868           != 0)
3869         continue;
3870       // Miranda method additions mean that the `methods' array moves.
3871       // We cache a pointer into this array, so we have to update.
3872       iclass->interpreted_methods[i]->self = &klass->methods[i];
3873     }
3874 }
3875
3876 #ifdef DIRECT_THREADED
3877 void
3878 _Jv_CompileMethod (_Jv_InterpMethod* method)
3879 {
3880   if (method->prepared == NULL)
3881     _Jv_InterpMethod::run (NULL, NULL, method);
3882 }
3883 #endif // DIRECT_THREADED
3884
3885 #endif // INTERPRETER