OSDN Git Service

2005-09-18 Richard Guenther <rguenther@suse.de>
[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 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 void
796 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
797 {
798   using namespace java::lang::reflect;
799
800   // FRAME_DESC registers this particular invocation as the top-most
801   // interpreter frame.  This lets the stack tracing code (for
802   // Throwable) print information about the method being interpreted
803   // rather than about the interpreter itself.  FRAME_DESC has a
804   // destructor so it cleans up automatically when the interpreter
805   // returns.
806   java::lang::Thread *thread = java::lang::Thread::currentThread();
807   _Jv_InterpFrame frame_desc (meth,
808                               (_Jv_InterpFrame **) &thread->interp_frame);
809
810   _Jv_word stack[meth->max_stack];
811   _Jv_word *sp = stack;
812
813   _Jv_word locals[meth->max_locals];
814
815   /* Go straight at it!  the ffi raw format matches the internal
816      stack representation exactly.  At least, that's the idea.
817   */
818   memcpy ((void*) locals, (void*) args, meth->args_raw_size);
819
820   _Jv_word *pool_data = meth->defining_class->constants.data;
821
822   /* These three are temporaries for common code used by several
823      instructions.  */
824   void (*fun)();
825   _Jv_ResolvedMethod* rmeth;
826   int tmpval;
827
828 #define INSN_LABEL(op) &&insn_##op
829
830   static const void *const insn_target[] = 
831   {
832     INSN_LABEL(nop),
833     INSN_LABEL(aconst_null),
834     INSN_LABEL(iconst_m1),
835     INSN_LABEL(iconst_0),
836     INSN_LABEL(iconst_1),
837     INSN_LABEL(iconst_2),
838     INSN_LABEL(iconst_3),
839     INSN_LABEL(iconst_4),
840     INSN_LABEL(iconst_5),
841     INSN_LABEL(lconst_0),
842     INSN_LABEL(lconst_1),
843     INSN_LABEL(fconst_0),
844     INSN_LABEL(fconst_1),
845     INSN_LABEL(fconst_2),
846     INSN_LABEL(dconst_0),
847     INSN_LABEL(dconst_1),
848     INSN_LABEL(bipush),
849     INSN_LABEL(sipush),
850     INSN_LABEL(ldc),
851     INSN_LABEL(ldc_w),
852     INSN_LABEL(ldc2_w),
853     INSN_LABEL(iload),
854     INSN_LABEL(lload),
855     INSN_LABEL(fload),
856     INSN_LABEL(dload),
857     INSN_LABEL(aload),
858     INSN_LABEL(iload_0),
859     INSN_LABEL(iload_1),
860     INSN_LABEL(iload_2),
861     INSN_LABEL(iload_3),
862     INSN_LABEL(lload_0),
863     INSN_LABEL(lload_1),
864     INSN_LABEL(lload_2),
865     INSN_LABEL(lload_3),
866     INSN_LABEL(fload_0),
867     INSN_LABEL(fload_1),
868     INSN_LABEL(fload_2),
869     INSN_LABEL(fload_3),
870     INSN_LABEL(dload_0),
871     INSN_LABEL(dload_1),
872     INSN_LABEL(dload_2),
873     INSN_LABEL(dload_3),
874     INSN_LABEL(aload_0),
875     INSN_LABEL(aload_1),
876     INSN_LABEL(aload_2),
877     INSN_LABEL(aload_3),
878     INSN_LABEL(iaload),
879     INSN_LABEL(laload),
880     INSN_LABEL(faload),
881     INSN_LABEL(daload),
882     INSN_LABEL(aaload),
883     INSN_LABEL(baload),
884     INSN_LABEL(caload),
885     INSN_LABEL(saload),
886     INSN_LABEL(istore),
887     INSN_LABEL(lstore),
888     INSN_LABEL(fstore),
889     INSN_LABEL(dstore),
890     INSN_LABEL(astore),
891     INSN_LABEL(istore_0),
892     INSN_LABEL(istore_1),
893     INSN_LABEL(istore_2),
894     INSN_LABEL(istore_3),
895     INSN_LABEL(lstore_0),
896     INSN_LABEL(lstore_1),
897     INSN_LABEL(lstore_2),
898     INSN_LABEL(lstore_3),
899     INSN_LABEL(fstore_0),
900     INSN_LABEL(fstore_1),
901     INSN_LABEL(fstore_2),
902     INSN_LABEL(fstore_3),
903     INSN_LABEL(dstore_0),
904     INSN_LABEL(dstore_1),
905     INSN_LABEL(dstore_2),
906     INSN_LABEL(dstore_3),
907     INSN_LABEL(astore_0),
908     INSN_LABEL(astore_1),
909     INSN_LABEL(astore_2),
910     INSN_LABEL(astore_3),
911     INSN_LABEL(iastore),
912     INSN_LABEL(lastore),
913     INSN_LABEL(fastore),
914     INSN_LABEL(dastore),
915     INSN_LABEL(aastore),
916     INSN_LABEL(bastore),
917     INSN_LABEL(castore),
918     INSN_LABEL(sastore),
919     INSN_LABEL(pop),
920     INSN_LABEL(pop2),
921     INSN_LABEL(dup),
922     INSN_LABEL(dup_x1),
923     INSN_LABEL(dup_x2),
924     INSN_LABEL(dup2),
925     INSN_LABEL(dup2_x1),
926     INSN_LABEL(dup2_x2),
927     INSN_LABEL(swap),
928     INSN_LABEL(iadd),
929     INSN_LABEL(ladd),
930     INSN_LABEL(fadd),
931     INSN_LABEL(dadd),
932     INSN_LABEL(isub),
933     INSN_LABEL(lsub),
934     INSN_LABEL(fsub),
935     INSN_LABEL(dsub),
936     INSN_LABEL(imul),
937     INSN_LABEL(lmul),
938     INSN_LABEL(fmul),
939     INSN_LABEL(dmul),
940     INSN_LABEL(idiv),
941     INSN_LABEL(ldiv),
942     INSN_LABEL(fdiv),
943     INSN_LABEL(ddiv),
944     INSN_LABEL(irem),
945     INSN_LABEL(lrem),
946     INSN_LABEL(frem),
947     INSN_LABEL(drem),
948     INSN_LABEL(ineg),
949     INSN_LABEL(lneg),
950     INSN_LABEL(fneg),
951     INSN_LABEL(dneg),
952     INSN_LABEL(ishl),
953     INSN_LABEL(lshl),
954     INSN_LABEL(ishr),
955     INSN_LABEL(lshr),
956     INSN_LABEL(iushr),
957     INSN_LABEL(lushr),
958     INSN_LABEL(iand),
959     INSN_LABEL(land),
960     INSN_LABEL(ior),
961     INSN_LABEL(lor),
962     INSN_LABEL(ixor),
963     INSN_LABEL(lxor),
964     INSN_LABEL(iinc),
965     INSN_LABEL(i2l),
966     INSN_LABEL(i2f),
967     INSN_LABEL(i2d),
968     INSN_LABEL(l2i),
969     INSN_LABEL(l2f),
970     INSN_LABEL(l2d),
971     INSN_LABEL(f2i),
972     INSN_LABEL(f2l),
973     INSN_LABEL(f2d),
974     INSN_LABEL(d2i),
975     INSN_LABEL(d2l),
976     INSN_LABEL(d2f),
977     INSN_LABEL(i2b),
978     INSN_LABEL(i2c),
979     INSN_LABEL(i2s),
980     INSN_LABEL(lcmp),
981     INSN_LABEL(fcmpl),
982     INSN_LABEL(fcmpg),
983     INSN_LABEL(dcmpl),
984     INSN_LABEL(dcmpg),
985     INSN_LABEL(ifeq),
986     INSN_LABEL(ifne),
987     INSN_LABEL(iflt),
988     INSN_LABEL(ifge),
989     INSN_LABEL(ifgt),
990     INSN_LABEL(ifle),
991     INSN_LABEL(if_icmpeq),
992     INSN_LABEL(if_icmpne),
993     INSN_LABEL(if_icmplt),
994     INSN_LABEL(if_icmpge),
995     INSN_LABEL(if_icmpgt),
996     INSN_LABEL(if_icmple),
997     INSN_LABEL(if_acmpeq),
998     INSN_LABEL(if_acmpne),
999     INSN_LABEL(goto), 
1000     INSN_LABEL(jsr),
1001     INSN_LABEL(ret),
1002     INSN_LABEL(tableswitch),
1003     INSN_LABEL(lookupswitch),
1004     INSN_LABEL(ireturn),
1005     INSN_LABEL(lreturn),
1006     INSN_LABEL(freturn),
1007     INSN_LABEL(dreturn),
1008     INSN_LABEL(areturn),
1009     INSN_LABEL(return),
1010     INSN_LABEL(getstatic),
1011     INSN_LABEL(putstatic),
1012     INSN_LABEL(getfield),
1013     INSN_LABEL(putfield),
1014     INSN_LABEL(invokevirtual),
1015     INSN_LABEL(invokespecial),
1016     INSN_LABEL(invokestatic),
1017     INSN_LABEL(invokeinterface),
1018     0, /* Unused.  */
1019     INSN_LABEL(new),
1020     INSN_LABEL(newarray),
1021     INSN_LABEL(anewarray),
1022     INSN_LABEL(arraylength),
1023     INSN_LABEL(athrow),
1024     INSN_LABEL(checkcast),
1025     INSN_LABEL(instanceof),
1026     INSN_LABEL(monitorenter),
1027     INSN_LABEL(monitorexit),
1028 #ifdef DIRECT_THREADED
1029     0, // wide
1030 #else
1031     INSN_LABEL(wide),
1032 #endif
1033     INSN_LABEL(multianewarray),
1034     INSN_LABEL(ifnull),
1035     INSN_LABEL(ifnonnull),
1036     INSN_LABEL(goto_w),
1037     INSN_LABEL(jsr_w),
1038 #ifdef DIRECT_THREADED
1039     INSN_LABEL (ldc_class)
1040 #else
1041     0
1042 #endif
1043   };
1044
1045   pc_t pc;
1046
1047 #ifdef DIRECT_THREADED
1048
1049 #define NEXT_INSN goto *((pc++)->insn)
1050 #define INTVAL() ((pc++)->int_val)
1051 #define AVAL() ((pc++)->datum)
1052
1053 #define GET1S() INTVAL ()
1054 #define GET2S() INTVAL ()
1055 #define GET1U() INTVAL ()
1056 #define GET2U() INTVAL ()
1057 #define AVAL1U() AVAL ()
1058 #define AVAL2U() AVAL ()
1059 #define AVAL2UP() AVAL ()
1060 #define SKIP_GOTO ++pc
1061 #define GOTO_VAL() (insn_slot *) pc->datum
1062 #define PCVAL(unionval) unionval.p
1063 #define AMPAMP(label) &&label
1064
1065   // Compile if we must. NOTE: Double-check locking.
1066   if (meth->prepared == NULL)
1067     {
1068       _Jv_MutexLock (&compile_mutex);
1069       if (meth->prepared == NULL)
1070         meth->compile (insn_target);
1071       _Jv_MutexUnlock (&compile_mutex);
1072     }
1073   pc = (insn_slot *) meth->prepared;
1074
1075 #else
1076
1077 #define NEXT_INSN goto *(insn_target[*pc++])
1078
1079 #define GET1S() get1s (pc++)
1080 #define GET2S() (pc += 2, get2s (pc- 2))
1081 #define GET1U() get1u (pc++)
1082 #define GET2U() (pc += 2, get2u (pc - 2))
1083   // Note that these could be more efficient when not handling 'ldc
1084   // class'.
1085 #define AVAL1U()                                                \
1086   ({ int index = get1u (pc++);                                  \
1087       resolve_pool_entry (meth->defining_class, index).o; })
1088 #define AVAL2U()                                                \
1089   ({ int index = get2u (pc); pc += 2;                           \
1090       resolve_pool_entry (meth->defining_class, index).o; })
1091   // Note that we don't need to resolve the pool entry here as class
1092   // constants are never wide.
1093 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1094 #define SKIP_GOTO pc += 2
1095 #define GOTO_VAL() pc - 1 + get2s (pc)
1096 #define PCVAL(unionval) unionval.i
1097 #define AMPAMP(label) NULL
1098
1099   pc = bytecode ();
1100
1101 #endif /* DIRECT_THREADED */
1102
1103 #define TAKE_GOTO pc = GOTO_VAL ()
1104
1105   try
1106     {
1107       // We keep nop around.  It is used if we're interpreting the
1108       // bytecodes and not doing direct threading.
1109     insn_nop:
1110       NEXT_INSN;
1111
1112       /* The first few instructions here are ordered according to their
1113          frequency, in the hope that this will improve code locality a
1114          little.  */
1115
1116     insn_aload_0:               // 0x2a
1117       LOADA (0);
1118       NEXT_INSN;
1119
1120     insn_iload:         // 0x15
1121       LOADI (GET1U ());
1122       NEXT_INSN;
1123
1124     insn_iload_1:               // 0x1b
1125       LOADI (1);
1126       NEXT_INSN;
1127
1128     insn_invokevirtual: // 0xb6
1129       {
1130         int index = GET2U ();
1131
1132         /* _Jv_Linker::resolve_pool_entry returns immediately if the
1133          * value already is resolved.  If we want to clutter up the
1134          * code here to gain a little performance, then we can check
1135          * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1136          * directly.  For now, I don't think it is worth it.  */
1137
1138         SAVE_PC();
1139         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1140                                                    index)).rmethod;
1141
1142         sp -= rmeth->stack_item_count;
1143         // We don't use NULLCHECK here because we can't rely on that
1144         // working if the method is final.  So instead we do an
1145         // explicit test.
1146         if (! sp[0].o)
1147           {
1148             //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1149             throw new java::lang::NullPointerException;
1150           }
1151
1152         if (rmeth->vtable_index == -1)
1153           {
1154             // final methods do not appear in the vtable,
1155             // if it does not appear in the superclass.
1156             fun = (void (*)()) rmeth->method->ncode;
1157           }
1158         else
1159           {
1160             jobject rcv = sp[0].o;
1161             _Jv_VTable *table = *(_Jv_VTable**) rcv;
1162             fun = (void (*)()) table->get_method (rmeth->vtable_index);
1163           }
1164
1165 #ifdef DIRECT_THREADED
1166         // Rewrite instruction so that we use a faster pre-resolved
1167         // method.
1168         pc[-2].insn = &&invokevirtual_resolved;
1169         pc[-1].datum = rmeth;
1170 #endif /* DIRECT_THREADED */
1171       }
1172       goto perform_invoke;
1173
1174 #ifdef DIRECT_THREADED
1175     invokevirtual_resolved:
1176       {
1177         rmeth = (_Jv_ResolvedMethod *) AVAL ();
1178         sp -= rmeth->stack_item_count;
1179         // We don't use NULLCHECK here because we can't rely on that
1180         // working if the method is final.  So instead we do an
1181         // explicit test.
1182         if (! sp[0].o)
1183           {
1184             SAVE_PC();
1185             throw new java::lang::NullPointerException;
1186           }
1187
1188         if (rmeth->vtable_index == -1)
1189           {
1190             // final methods do not appear in the vtable,
1191             // if it does not appear in the superclass.
1192             fun = (void (*)()) rmeth->method->ncode;
1193           }
1194         else
1195           {
1196             jobject rcv = sp[0].o;
1197             _Jv_VTable *table = *(_Jv_VTable**) rcv;
1198             fun = (void (*)()) table->get_method (rmeth->vtable_index);
1199           }
1200       }
1201       goto perform_invoke;
1202 #endif /* DIRECT_THREADED */
1203
1204     perform_invoke:
1205       {
1206         SAVE_PC();
1207         
1208         /* here goes the magic again... */
1209         ffi_cif *cif = &rmeth->cif;
1210         ffi_raw *raw = (ffi_raw*) sp;
1211
1212         _Jv_value rvalue;
1213
1214 #if FFI_NATIVE_RAW_API
1215         /* We assume that this is only implemented if it's correct      */
1216         /* to use it here.  On a 64 bit machine, it never is.           */
1217         ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1218 #else
1219         ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1220 #endif
1221
1222         int rtype = cif->rtype->type;
1223
1224         /* the likelyhood of object, int, or void return is very high,
1225          * so those are checked before the switch */
1226         if (rtype == FFI_TYPE_POINTER)
1227           {
1228             PUSHA (rvalue.object_value);
1229           }
1230         else if (rtype == FFI_TYPE_SINT32)
1231           {
1232             PUSHI (rvalue.int_value);
1233           }
1234         else if (rtype == FFI_TYPE_VOID)
1235           {
1236             /* skip */
1237           }
1238         else
1239           {
1240             switch (rtype)
1241               {
1242               case FFI_TYPE_SINT8:
1243                 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1244                 break;
1245
1246               case FFI_TYPE_SINT16:
1247                 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1248                 break;
1249
1250               case FFI_TYPE_UINT16:
1251                 PUSHI (rvalue.int_value & 0xffff);
1252                 break;
1253
1254               case FFI_TYPE_FLOAT:
1255                 PUSHF (rvalue.float_value);
1256                 break;
1257
1258               case FFI_TYPE_DOUBLE:
1259                 PUSHD (rvalue.double_value);
1260                 break;
1261
1262               case FFI_TYPE_SINT64:
1263                 PUSHL (rvalue.long_value);
1264                 break;
1265
1266               default:
1267                 throw_internal_error ("unknown return type in invokeXXX");
1268               }
1269           }
1270       }
1271       NEXT_INSN;
1272
1273     insn_aconst_null:
1274       PUSHA (NULL);
1275       NEXT_INSN;
1276
1277     insn_iconst_m1:
1278       PUSHI (-1);
1279       NEXT_INSN;
1280
1281     insn_iconst_0:
1282       PUSHI (0);
1283       NEXT_INSN;
1284
1285     insn_iconst_1:
1286       PUSHI (1);
1287       NEXT_INSN;
1288
1289     insn_iconst_2:
1290       PUSHI (2);
1291       NEXT_INSN;
1292
1293     insn_iconst_3:
1294       PUSHI (3);
1295       NEXT_INSN;
1296
1297     insn_iconst_4:
1298       PUSHI (4);
1299       NEXT_INSN;
1300
1301     insn_iconst_5:
1302       PUSHI (5);
1303       NEXT_INSN;
1304
1305     insn_lconst_0:
1306       PUSHL (0);
1307       NEXT_INSN;
1308
1309     insn_lconst_1:
1310       PUSHL (1);
1311       NEXT_INSN;
1312
1313     insn_fconst_0:
1314       PUSHF (0);
1315       NEXT_INSN;
1316
1317     insn_fconst_1:
1318       PUSHF (1);
1319       NEXT_INSN;
1320
1321     insn_fconst_2:
1322       PUSHF (2);
1323       NEXT_INSN;
1324
1325     insn_dconst_0:
1326       PUSHD (0);
1327       NEXT_INSN;
1328
1329     insn_dconst_1:
1330       PUSHD (1);
1331       NEXT_INSN;
1332
1333     insn_bipush:
1334       // For direct threaded, bipush and sipush are the same.
1335 #ifndef DIRECT_THREADED
1336       PUSHI (GET1S ());
1337       NEXT_INSN;
1338 #endif /* DIRECT_THREADED */
1339     insn_sipush:
1340       PUSHI (GET2S ());
1341       NEXT_INSN;
1342
1343     insn_ldc:
1344       // For direct threaded, ldc and ldc_w are the same.
1345 #ifndef DIRECT_THREADED
1346       PUSHA ((jobject) AVAL1U ());
1347       NEXT_INSN;
1348 #endif /* DIRECT_THREADED */
1349     insn_ldc_w:
1350       PUSHA ((jobject) AVAL2U ());
1351       NEXT_INSN;
1352
1353 #ifdef DIRECT_THREADED
1354       // For direct threaded we have a separate 'ldc class' operation.
1355     insn_ldc_class:
1356       {
1357         // We could rewrite the instruction at this point.
1358         int index = INTVAL ();
1359         jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1360                                                      index)).o;
1361         PUSHA (k);
1362       }
1363       NEXT_INSN;
1364 #endif /* DIRECT_THREADED */
1365
1366     insn_ldc2_w:
1367       {
1368         void *where = AVAL2UP ();
1369         memcpy (sp, where, 2*sizeof (_Jv_word));
1370         sp += 2;
1371       }
1372       NEXT_INSN;
1373
1374     insn_lload:
1375       LOADL (GET1U ());
1376       NEXT_INSN;
1377
1378     insn_fload:
1379       LOADF (GET1U ());
1380       NEXT_INSN;
1381
1382     insn_dload:
1383       LOADD (GET1U ());
1384       NEXT_INSN;
1385
1386     insn_aload:
1387       LOADA (GET1U ());
1388       NEXT_INSN;
1389
1390     insn_iload_0:
1391       LOADI (0);
1392       NEXT_INSN;
1393
1394     insn_iload_2:
1395       LOADI (2);
1396       NEXT_INSN;
1397
1398     insn_iload_3:
1399       LOADI (3);
1400       NEXT_INSN;
1401
1402     insn_lload_0:
1403       LOADL (0);
1404       NEXT_INSN;
1405
1406     insn_lload_1:
1407       LOADL (1);
1408       NEXT_INSN;
1409
1410     insn_lload_2:
1411       LOADL (2);
1412       NEXT_INSN;
1413
1414     insn_lload_3:
1415       LOADL (3);
1416       NEXT_INSN;
1417
1418     insn_fload_0:
1419       LOADF (0);
1420       NEXT_INSN;
1421
1422     insn_fload_1:
1423       LOADF (1);
1424       NEXT_INSN;
1425
1426     insn_fload_2:
1427       LOADF (2);
1428       NEXT_INSN;
1429
1430     insn_fload_3:
1431       LOADF (3);
1432       NEXT_INSN;
1433
1434     insn_dload_0:
1435       LOADD (0);
1436       NEXT_INSN;
1437
1438     insn_dload_1:
1439       LOADD (1);
1440       NEXT_INSN;
1441
1442     insn_dload_2:
1443       LOADD (2);
1444       NEXT_INSN;
1445
1446     insn_dload_3:
1447       LOADD (3);
1448       NEXT_INSN;
1449
1450     insn_aload_1:
1451       LOADA(1);
1452       NEXT_INSN;
1453
1454     insn_aload_2:
1455       LOADA(2);
1456       NEXT_INSN;
1457
1458     insn_aload_3:
1459       LOADA(3);
1460       NEXT_INSN;
1461
1462     insn_iaload:
1463       {
1464         jint index = POPI();
1465         jintArray arr = (jintArray) POPA();
1466         NULLARRAYCHECK (arr);
1467         ARRAYBOUNDSCHECK (arr, index);
1468         PUSHI( elements(arr)[index] );
1469       }
1470       NEXT_INSN;
1471
1472     insn_laload:
1473       {
1474         jint index = POPI();
1475         jlongArray arr = (jlongArray) POPA();
1476         NULLARRAYCHECK (arr);
1477         ARRAYBOUNDSCHECK (arr, index);
1478         PUSHL( elements(arr)[index] );
1479       }
1480       NEXT_INSN;
1481
1482     insn_faload:
1483       {
1484         jint index = POPI();
1485         jfloatArray arr = (jfloatArray) POPA();
1486         NULLARRAYCHECK (arr);
1487         ARRAYBOUNDSCHECK (arr, index);
1488         PUSHF( elements(arr)[index] );
1489       }
1490       NEXT_INSN;
1491
1492     insn_daload:
1493       {
1494         jint index = POPI();
1495         jdoubleArray arr = (jdoubleArray) POPA();
1496         NULLARRAYCHECK (arr);
1497         ARRAYBOUNDSCHECK (arr, index);
1498         PUSHD( elements(arr)[index] );
1499       }
1500       NEXT_INSN;
1501
1502     insn_aaload:
1503       {
1504         jint index = POPI();
1505         jobjectArray arr = (jobjectArray) POPA();
1506         NULLARRAYCHECK (arr);
1507         ARRAYBOUNDSCHECK (arr, index);
1508         PUSHA( elements(arr)[index] );
1509       }
1510       NEXT_INSN;
1511
1512     insn_baload:
1513       {
1514         jint index = POPI();
1515         jbyteArray arr = (jbyteArray) POPA();
1516         NULLARRAYCHECK (arr);
1517         ARRAYBOUNDSCHECK (arr, index);
1518         PUSHI( elements(arr)[index] );
1519       }
1520       NEXT_INSN;
1521
1522     insn_caload:
1523       {
1524         jint index = POPI();
1525         jcharArray arr = (jcharArray) POPA();
1526         NULLARRAYCHECK (arr);
1527         ARRAYBOUNDSCHECK (arr, index);
1528         PUSHI( elements(arr)[index] );
1529       }
1530       NEXT_INSN;
1531
1532     insn_saload:
1533       {
1534         jint index = POPI();
1535         jshortArray arr = (jshortArray) POPA();
1536         NULLARRAYCHECK (arr);
1537         ARRAYBOUNDSCHECK (arr, index);
1538         PUSHI( elements(arr)[index] );
1539       }
1540       NEXT_INSN;
1541
1542     insn_istore:
1543       STOREI (GET1U ());
1544       NEXT_INSN;
1545
1546     insn_lstore:
1547       STOREL (GET1U ());
1548       NEXT_INSN;
1549
1550     insn_fstore:
1551       STOREF (GET1U ());
1552       NEXT_INSN;
1553
1554     insn_dstore:
1555       STORED (GET1U ());
1556       NEXT_INSN;
1557
1558     insn_astore:
1559       STOREA (GET1U ());
1560       NEXT_INSN;
1561
1562     insn_istore_0:
1563       STOREI (0);
1564       NEXT_INSN;
1565
1566     insn_istore_1:
1567       STOREI (1);
1568       NEXT_INSN;
1569
1570     insn_istore_2:
1571       STOREI (2);
1572       NEXT_INSN;
1573
1574     insn_istore_3:
1575       STOREI (3);
1576       NEXT_INSN;
1577
1578     insn_lstore_0:
1579       STOREL (0);
1580       NEXT_INSN;
1581
1582     insn_lstore_1:
1583       STOREL (1);
1584       NEXT_INSN;
1585
1586     insn_lstore_2:
1587       STOREL (2);
1588       NEXT_INSN;
1589
1590     insn_lstore_3:
1591       STOREL (3);
1592       NEXT_INSN;
1593
1594     insn_fstore_0:
1595       STOREF (0);
1596       NEXT_INSN;
1597
1598     insn_fstore_1:
1599       STOREF (1);
1600       NEXT_INSN;
1601
1602     insn_fstore_2:
1603       STOREF (2);
1604       NEXT_INSN;
1605
1606     insn_fstore_3:
1607       STOREF (3);
1608       NEXT_INSN;
1609
1610     insn_dstore_0:
1611       STORED (0);
1612       NEXT_INSN;
1613
1614     insn_dstore_1:
1615       STORED (1);
1616       NEXT_INSN;
1617
1618     insn_dstore_2:
1619       STORED (2);
1620       NEXT_INSN;
1621
1622     insn_dstore_3:
1623       STORED (3);
1624       NEXT_INSN;
1625
1626     insn_astore_0:
1627       STOREA(0);
1628       NEXT_INSN;
1629
1630     insn_astore_1:
1631       STOREA(1);
1632       NEXT_INSN;
1633
1634     insn_astore_2:
1635       STOREA(2);
1636       NEXT_INSN;
1637
1638     insn_astore_3:
1639       STOREA(3);
1640       NEXT_INSN;
1641
1642     insn_iastore:
1643       {
1644         jint value = POPI();
1645         jint index  = POPI();
1646         jintArray arr = (jintArray) POPA();
1647         NULLARRAYCHECK (arr);
1648         ARRAYBOUNDSCHECK (arr, index);
1649         elements(arr)[index] = value;
1650       }
1651       NEXT_INSN;
1652
1653     insn_lastore:
1654       {
1655         jlong value = POPL();
1656         jint index  = POPI();
1657         jlongArray arr = (jlongArray) POPA();
1658         NULLARRAYCHECK (arr);
1659         ARRAYBOUNDSCHECK (arr, index);
1660         elements(arr)[index] = value;
1661       }
1662       NEXT_INSN;
1663
1664     insn_fastore:
1665       {
1666         jfloat value = POPF();
1667         jint index  = POPI();
1668         jfloatArray arr = (jfloatArray) POPA();
1669         NULLARRAYCHECK (arr);
1670         ARRAYBOUNDSCHECK (arr, index);
1671         elements(arr)[index] = value;
1672       }
1673       NEXT_INSN;
1674
1675     insn_dastore:
1676       {
1677         jdouble value = POPD();
1678         jint index  = POPI();
1679         jdoubleArray arr = (jdoubleArray) POPA();
1680         NULLARRAYCHECK (arr);
1681         ARRAYBOUNDSCHECK (arr, index);
1682         elements(arr)[index] = value;
1683       }
1684       NEXT_INSN;
1685
1686     insn_aastore:
1687       {
1688         jobject value = POPA();
1689         jint index  = POPI();
1690         jobjectArray arr = (jobjectArray) POPA();
1691         NULLARRAYCHECK (arr);
1692         ARRAYBOUNDSCHECK (arr, index);
1693         _Jv_CheckArrayStore (arr, value);
1694         elements(arr)[index] = value;
1695       }
1696       NEXT_INSN;
1697
1698     insn_bastore:
1699       {
1700         jbyte value = (jbyte) POPI();
1701         jint index  = POPI();
1702         jbyteArray arr = (jbyteArray) POPA();
1703         NULLARRAYCHECK (arr);
1704         ARRAYBOUNDSCHECK (arr, index);
1705         elements(arr)[index] = value;
1706       }
1707       NEXT_INSN;
1708
1709     insn_castore:
1710       {
1711         jchar value = (jchar) POPI();
1712         jint index  = POPI();
1713         jcharArray arr = (jcharArray) POPA();
1714         NULLARRAYCHECK (arr);
1715         ARRAYBOUNDSCHECK (arr, index);
1716         elements(arr)[index] = value;
1717       }
1718       NEXT_INSN;
1719
1720     insn_sastore:
1721       {
1722         jshort value = (jshort) POPI();
1723         jint index  = POPI();
1724         jshortArray arr = (jshortArray) POPA();
1725         NULLARRAYCHECK (arr);
1726         ARRAYBOUNDSCHECK (arr, index);
1727         elements(arr)[index] = value;
1728       }
1729       NEXT_INSN;
1730
1731     insn_pop:
1732       sp -= 1;
1733       NEXT_INSN;
1734
1735     insn_pop2:
1736       sp -= 2;
1737       NEXT_INSN;
1738
1739     insn_dup:
1740       sp[0] = sp[-1];
1741       sp += 1;
1742       NEXT_INSN;
1743
1744     insn_dup_x1:
1745       dupx (sp, 1, 1); sp+=1;
1746       NEXT_INSN;
1747
1748     insn_dup_x2:
1749       dupx (sp, 1, 2); sp+=1;
1750       NEXT_INSN;
1751
1752     insn_dup2:
1753       sp[0] = sp[-2];
1754       sp[1] = sp[-1];
1755       sp += 2;
1756       NEXT_INSN;
1757
1758     insn_dup2_x1:
1759       dupx (sp, 2, 1); sp+=2;
1760       NEXT_INSN;
1761
1762     insn_dup2_x2:
1763       dupx (sp, 2, 2); sp+=2;
1764       NEXT_INSN;
1765
1766     insn_swap:
1767       {
1768         jobject tmp1 = POPA();
1769         jobject tmp2 = POPA();
1770         PUSHA (tmp1);
1771         PUSHA (tmp2);
1772       }
1773       NEXT_INSN;
1774
1775     insn_iadd:
1776       BINOPI(+);
1777       NEXT_INSN;
1778
1779     insn_ladd:
1780       BINOPL(+);
1781       NEXT_INSN;
1782
1783     insn_fadd:
1784       BINOPF(+);
1785       NEXT_INSN;
1786
1787     insn_dadd:
1788       BINOPD(+);
1789       NEXT_INSN;
1790
1791     insn_isub:
1792       BINOPI(-);
1793       NEXT_INSN;
1794
1795     insn_lsub:
1796       BINOPL(-);
1797       NEXT_INSN;
1798
1799     insn_fsub:
1800       BINOPF(-);
1801       NEXT_INSN;
1802
1803     insn_dsub:
1804       BINOPD(-);
1805       NEXT_INSN;
1806
1807     insn_imul:
1808       BINOPI(*);
1809       NEXT_INSN;
1810
1811     insn_lmul:
1812       BINOPL(*);
1813       NEXT_INSN;
1814
1815     insn_fmul:
1816       BINOPF(*);
1817       NEXT_INSN;
1818
1819     insn_dmul:
1820       BINOPD(*);
1821       NEXT_INSN;
1822
1823     insn_idiv:
1824       {
1825         jint value2 = POPI();
1826         jint value1 = POPI();
1827         jint res = _Jv_divI (value1, value2);
1828         PUSHI (res);
1829       }
1830       NEXT_INSN;
1831
1832     insn_ldiv:
1833       {
1834         jlong value2 = POPL();
1835         jlong value1 = POPL();
1836         jlong res = _Jv_divJ (value1, value2);
1837         PUSHL (res);
1838       }
1839       NEXT_INSN;
1840
1841     insn_fdiv:
1842       {
1843         jfloat value2 = POPF();
1844         jfloat value1 = POPF();
1845         jfloat res = value1 / value2;
1846         PUSHF (res);
1847       }
1848       NEXT_INSN;
1849
1850     insn_ddiv:
1851       {
1852         jdouble value2 = POPD();
1853         jdouble value1 = POPD();
1854         jdouble res = value1 / value2;
1855         PUSHD (res);
1856       }
1857       NEXT_INSN;
1858
1859     insn_irem:
1860       {
1861         jint value2 = POPI();
1862         jint value1 =  POPI();
1863         jint res = _Jv_remI (value1, value2);
1864         PUSHI (res);
1865       }
1866       NEXT_INSN;
1867
1868     insn_lrem:
1869       {
1870         jlong value2 = POPL();
1871         jlong value1 = POPL();
1872         jlong res = _Jv_remJ (value1, value2);
1873         PUSHL (res);
1874       }
1875       NEXT_INSN;
1876
1877     insn_frem:
1878       {
1879         jfloat value2 = POPF();
1880         jfloat value1 = POPF();
1881         jfloat res    = __ieee754_fmod (value1, value2);
1882         PUSHF (res);
1883       }
1884       NEXT_INSN;
1885
1886     insn_drem:
1887       {
1888         jdouble value2 = POPD();
1889         jdouble value1 = POPD();
1890         jdouble res    = __ieee754_fmod (value1, value2);
1891         PUSHD (res);
1892       }
1893       NEXT_INSN;
1894
1895     insn_ineg:
1896       {
1897         jint value = POPI();
1898         PUSHI (value * -1);
1899       }
1900       NEXT_INSN;
1901
1902     insn_lneg:
1903       {
1904         jlong value = POPL();
1905         PUSHL (value * -1);
1906       }
1907       NEXT_INSN;
1908
1909     insn_fneg:
1910       {
1911         jfloat value = POPF();
1912         PUSHF (value * -1);
1913       }
1914       NEXT_INSN;
1915
1916     insn_dneg:
1917       {
1918         jdouble value = POPD();
1919         PUSHD (value * -1);
1920       }
1921       NEXT_INSN;
1922
1923     insn_ishl:
1924       {
1925         jint shift = (POPI() & 0x1f);
1926         jint value = POPI();
1927         PUSHI (value << shift);
1928       }
1929       NEXT_INSN;
1930
1931     insn_lshl:
1932       {
1933         jint shift = (POPI() & 0x3f);
1934         jlong value = POPL();
1935         PUSHL (value << shift);
1936       }
1937       NEXT_INSN;
1938
1939     insn_ishr:
1940       {
1941         jint shift = (POPI() & 0x1f);
1942         jint value = POPI();
1943         PUSHI (value >> shift);
1944       }
1945       NEXT_INSN;
1946
1947     insn_lshr:
1948       {
1949         jint shift = (POPI() & 0x3f);
1950         jlong value = POPL();
1951         PUSHL (value >> shift);
1952       }
1953       NEXT_INSN;
1954
1955     insn_iushr:
1956       {
1957         jint shift = (POPI() & 0x1f);
1958         _Jv_uint value = (_Jv_uint) POPI();
1959         PUSHI ((jint) (value >> shift));
1960       }
1961       NEXT_INSN;
1962
1963     insn_lushr:
1964       {
1965         jint shift = (POPI() & 0x3f);
1966         _Jv_ulong value = (_Jv_ulong) POPL();
1967         PUSHL ((jlong) (value >> shift));
1968       }
1969       NEXT_INSN;
1970
1971     insn_iand:
1972       BINOPI (&);
1973       NEXT_INSN;
1974
1975     insn_land:
1976       BINOPL (&);
1977       NEXT_INSN;
1978
1979     insn_ior:
1980       BINOPI (|);
1981       NEXT_INSN;
1982
1983     insn_lor:
1984       BINOPL (|);
1985       NEXT_INSN;
1986
1987     insn_ixor:
1988       BINOPI (^);
1989       NEXT_INSN;
1990
1991     insn_lxor:
1992       BINOPL (^);
1993       NEXT_INSN;
1994
1995     insn_iinc:
1996       {
1997         jint index  = GET1U ();
1998         jint amount = GET1S ();
1999         locals[index].i += amount;
2000       }
2001       NEXT_INSN;
2002
2003     insn_i2l:
2004       {jlong value = POPI(); PUSHL (value);}
2005       NEXT_INSN;
2006
2007     insn_i2f:
2008       {jfloat value = POPI(); PUSHF (value);}
2009       NEXT_INSN;
2010
2011     insn_i2d:
2012       {jdouble value = POPI(); PUSHD (value);}
2013       NEXT_INSN;
2014
2015     insn_l2i:
2016       {jint value = POPL(); PUSHI (value);}
2017       NEXT_INSN;
2018
2019     insn_l2f:
2020       {jfloat value = POPL(); PUSHF (value);}
2021       NEXT_INSN;
2022
2023     insn_l2d:
2024       {jdouble value = POPL(); PUSHD (value);}
2025       NEXT_INSN;
2026
2027     insn_f2i:
2028       {
2029         using namespace java::lang;
2030         jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2031         PUSHI(value);
2032       }
2033       NEXT_INSN;
2034
2035     insn_f2l:
2036       {
2037         using namespace java::lang;
2038         jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2039         PUSHL(value);
2040       }
2041       NEXT_INSN;
2042
2043     insn_f2d:
2044       { jdouble value = POPF (); PUSHD(value); }
2045       NEXT_INSN;
2046
2047     insn_d2i:
2048       {
2049         using namespace java::lang;
2050         jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2051         PUSHI(value);
2052       }
2053       NEXT_INSN;
2054
2055     insn_d2l:
2056       {
2057         using namespace java::lang;
2058         jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2059         PUSHL(value);
2060       }
2061       NEXT_INSN;
2062
2063     insn_d2f:
2064       { jfloat value = POPD (); PUSHF(value); }
2065       NEXT_INSN;
2066
2067     insn_i2b:
2068       { jbyte value = POPI (); PUSHI(value); }
2069       NEXT_INSN;
2070
2071     insn_i2c:
2072       { jchar value = POPI (); PUSHI(value); }
2073       NEXT_INSN;
2074
2075     insn_i2s:
2076       { jshort value = POPI (); PUSHI(value); }
2077       NEXT_INSN;
2078
2079     insn_lcmp:
2080       {
2081         jlong value2 = POPL ();
2082         jlong value1 = POPL ();
2083         if (value1 > value2)
2084           { PUSHI (1); }
2085         else if (value1 == value2)
2086           { PUSHI (0); }
2087         else
2088           { PUSHI (-1); }
2089       }
2090       NEXT_INSN;
2091
2092     insn_fcmpl:
2093       tmpval = -1;
2094       goto fcmp;
2095
2096     insn_fcmpg:
2097       tmpval = 1;
2098
2099     fcmp:
2100       {
2101         jfloat value2 = POPF ();
2102         jfloat value1 = POPF ();
2103         if (value1 > value2)
2104           PUSHI (1);
2105         else if (value1 == value2)
2106           PUSHI (0);
2107         else if (value1 < value2)
2108           PUSHI (-1);
2109         else
2110           PUSHI (tmpval);
2111       }
2112       NEXT_INSN;
2113
2114     insn_dcmpl:
2115       tmpval = -1;
2116       goto dcmp;
2117
2118     insn_dcmpg:
2119       tmpval = 1;
2120
2121     dcmp:
2122       {
2123         jdouble value2 = POPD ();
2124         jdouble value1 = POPD ();
2125         if (value1 > value2)
2126           PUSHI (1);
2127         else if (value1 == value2)
2128           PUSHI (0);
2129         else if (value1 < value2)
2130           PUSHI (-1);
2131         else
2132           PUSHI (tmpval);
2133       }
2134       NEXT_INSN;
2135
2136     insn_ifeq:
2137       {
2138         if (POPI() == 0)
2139           TAKE_GOTO;
2140         else
2141           SKIP_GOTO;
2142       }
2143       NEXT_INSN;
2144
2145     insn_ifne:
2146       {
2147         if (POPI() != 0)
2148           TAKE_GOTO;
2149         else
2150           SKIP_GOTO;
2151       }
2152       NEXT_INSN;
2153
2154     insn_iflt:
2155       {
2156         if (POPI() < 0)
2157           TAKE_GOTO;
2158         else
2159           SKIP_GOTO;
2160       }
2161       NEXT_INSN;
2162
2163     insn_ifge:
2164       {
2165         if (POPI() >= 0)
2166           TAKE_GOTO;
2167         else
2168           SKIP_GOTO;
2169       }
2170       NEXT_INSN;
2171
2172     insn_ifgt:
2173       {
2174         if (POPI() > 0)
2175           TAKE_GOTO;
2176         else
2177           SKIP_GOTO;
2178       }
2179       NEXT_INSN;
2180
2181     insn_ifle:
2182       {
2183         if (POPI() <= 0)
2184           TAKE_GOTO;
2185         else
2186           SKIP_GOTO;
2187       }
2188       NEXT_INSN;
2189
2190     insn_if_icmpeq:
2191       {
2192         jint value2 = POPI();
2193         jint value1 = POPI();
2194         if (value1 == value2)
2195           TAKE_GOTO;
2196         else
2197           SKIP_GOTO;
2198       }
2199       NEXT_INSN;
2200
2201     insn_if_icmpne:
2202       {
2203         jint value2 = POPI();
2204         jint value1 = POPI();
2205         if (value1 != value2)
2206           TAKE_GOTO;
2207         else
2208           SKIP_GOTO;
2209       }
2210       NEXT_INSN;
2211
2212     insn_if_icmplt:
2213       {
2214         jint value2 = POPI();
2215         jint value1 = POPI();
2216         if (value1 < value2)
2217           TAKE_GOTO;
2218         else
2219           SKIP_GOTO;
2220       }
2221       NEXT_INSN;
2222
2223     insn_if_icmpge:
2224       {
2225         jint value2 = POPI();
2226         jint value1 = POPI();
2227         if (value1 >= value2)
2228           TAKE_GOTO;
2229         else
2230           SKIP_GOTO;
2231       }
2232       NEXT_INSN;
2233
2234     insn_if_icmpgt:
2235       {
2236         jint value2 = POPI();
2237         jint value1 = POPI();
2238         if (value1 > value2)
2239           TAKE_GOTO;
2240         else
2241           SKIP_GOTO;
2242       }
2243       NEXT_INSN;
2244
2245     insn_if_icmple:
2246       {
2247         jint value2 = POPI();
2248         jint value1 = POPI();
2249         if (value1 <= value2)
2250           TAKE_GOTO;
2251         else
2252           SKIP_GOTO;
2253       }
2254       NEXT_INSN;
2255
2256     insn_if_acmpeq:
2257       {
2258         jobject value2 = POPA();
2259         jobject value1 = POPA();
2260         if (value1 == value2)
2261           TAKE_GOTO;
2262         else
2263           SKIP_GOTO;
2264       }
2265       NEXT_INSN;
2266
2267     insn_if_acmpne:
2268       {
2269         jobject value2 = POPA();
2270         jobject value1 = POPA();
2271         if (value1 != value2)
2272           TAKE_GOTO;
2273         else
2274           SKIP_GOTO;
2275       }
2276       NEXT_INSN;
2277
2278     insn_goto_w:
2279 #ifndef DIRECT_THREADED
2280       // For direct threaded, goto and goto_w are the same.
2281       pc = pc - 1 + get4 (pc);
2282       NEXT_INSN;
2283 #endif /* DIRECT_THREADED */
2284     insn_goto:
2285       TAKE_GOTO;
2286       NEXT_INSN;
2287
2288     insn_jsr_w:
2289 #ifndef DIRECT_THREADED
2290       // For direct threaded, jsr and jsr_w are the same.
2291       {
2292         pc_t next = pc - 1 + get4 (pc);
2293         pc += 4;
2294         PUSHA ((jobject) pc);
2295         pc = next;
2296       }
2297       NEXT_INSN;
2298 #endif /* DIRECT_THREADED */
2299     insn_jsr:
2300       {
2301         pc_t next = GOTO_VAL();
2302         SKIP_GOTO;
2303         PUSHA ((jobject) pc);
2304         pc = next;
2305       }
2306       NEXT_INSN;
2307
2308     insn_ret:
2309       {
2310         jint index = GET1U ();
2311         pc = (pc_t) PEEKA (index);
2312       }
2313       NEXT_INSN;
2314
2315     insn_tableswitch:
2316       {
2317 #ifdef DIRECT_THREADED
2318         void *def = (pc++)->datum;
2319
2320         int index = POPI();
2321
2322         jint low = INTVAL ();
2323         jint high = INTVAL ();
2324
2325         if (index < low || index > high)
2326           pc = (insn_slot *) def;
2327         else
2328           pc = (insn_slot *) ((pc + index - low)->datum);
2329 #else
2330         pc_t base_pc = pc - 1;
2331         int index = POPI ();
2332
2333         pc_t base = (pc_t) bytecode ();
2334         while ((pc - base) % 4 != 0)
2335           ++pc;
2336
2337         jint def = get4 (pc);
2338         jint low = get4 (pc + 4);
2339         jint high = get4 (pc + 8);
2340         if (index < low || index > high)
2341           pc = base_pc + def;
2342         else
2343           pc = base_pc + get4 (pc + 4 * (index - low + 3));
2344 #endif /* DIRECT_THREADED */
2345       }
2346       NEXT_INSN;
2347
2348     insn_lookupswitch:
2349       {
2350 #ifdef DIRECT_THREADED
2351         void *def = (pc++)->insn;
2352
2353         int index = POPI();
2354
2355         jint npairs = INTVAL ();
2356
2357         int max = npairs - 1;
2358         int min = 0;
2359
2360         // Simple binary search...
2361         while (min < max)
2362           {
2363             int half = (min + max) / 2;
2364             int match = pc[2 * half].int_val;
2365
2366             if (index == match)
2367               {
2368                 // Found it.
2369                 pc = (insn_slot *) pc[2 * half + 1].datum;
2370                 NEXT_INSN;
2371               }
2372             else if (index < match)
2373               // We can use HALF - 1 here because we check again on
2374               // loop exit.
2375               max = half - 1;
2376             else
2377               // We can use HALF + 1 here because we check again on
2378               // loop exit.
2379               min = half + 1;
2380           }
2381         if (index == pc[2 * min].int_val)
2382           pc = (insn_slot *) pc[2 * min + 1].datum;
2383         else
2384           pc = (insn_slot *) def;
2385 #else
2386         unsigned char *base_pc = pc-1;
2387         int index = POPI();
2388
2389         unsigned char* base = bytecode ();
2390         while ((pc-base) % 4 != 0)
2391           ++pc;
2392
2393         jint def     = get4 (pc);
2394         jint npairs  = get4 (pc+4);
2395
2396         int max = npairs-1;
2397         int min = 0;
2398
2399         // Simple binary search...
2400         while (min < max)
2401           {
2402             int half = (min+max)/2;
2403             int match = get4 (pc+ 4*(2 + 2*half));
2404
2405             if (index == match)
2406               min = max = half;
2407             else if (index < match)
2408               // We can use HALF - 1 here because we check again on
2409               // loop exit.
2410               max = half - 1;
2411             else
2412               // We can use HALF + 1 here because we check again on
2413               // loop exit.
2414               min = half + 1;
2415           }
2416
2417         if (index == get4 (pc+ 4*(2 + 2*min)))
2418           pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2419         else
2420           pc = base_pc + def;    
2421 #endif /* DIRECT_THREADED */
2422       }
2423       NEXT_INSN;
2424
2425     insn_areturn:
2426       *(jobject *) retp = POPA ();
2427       return;
2428
2429     insn_lreturn:
2430       *(jlong *) retp = POPL ();
2431       return;
2432
2433     insn_freturn:
2434       *(jfloat *) retp = POPF ();
2435       return;
2436
2437     insn_dreturn:
2438       *(jdouble *) retp = POPD ();
2439       return;
2440
2441     insn_ireturn:
2442       *(jint *) retp = POPI ();
2443       return;
2444
2445     insn_return:
2446       return;
2447
2448     insn_getstatic:
2449       {
2450         jint fieldref_index = GET2U ();
2451         SAVE_PC(); // Constant pool resolution could throw.
2452         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2453         _Jv_Field *field = pool_data[fieldref_index].field;
2454
2455         if ((field->flags & Modifier::STATIC) == 0)
2456           throw_incompatible_class_change_error 
2457             (JvNewStringLatin1 ("field no longer static"));
2458
2459         jclass type = field->type;
2460
2461         // We rewrite the instruction once we discover what it refers
2462         // to.
2463         void *newinsn = NULL;
2464         if (type->isPrimitive ())
2465           {
2466             switch (type->size_in_bytes)
2467               {
2468               case 1:
2469                 PUSHI (*field->u.byte_addr);
2470                 newinsn = AMPAMP (getstatic_resolved_1);
2471                 break;
2472
2473               case 2:
2474                 if (type == JvPrimClass (char))
2475                   {
2476                     PUSHI (*field->u.char_addr);
2477                     newinsn = AMPAMP (getstatic_resolved_char);
2478                   }
2479                 else
2480                   {
2481                     PUSHI (*field->u.short_addr);
2482                     newinsn = AMPAMP (getstatic_resolved_short);
2483                   }
2484                 break;
2485
2486               case 4:
2487                 PUSHI(*field->u.int_addr);
2488                 newinsn = AMPAMP (getstatic_resolved_4);
2489                 break;
2490
2491               case 8:
2492                 PUSHL(*field->u.long_addr);
2493                 newinsn = AMPAMP (getstatic_resolved_8);
2494                 break;
2495               }
2496           }
2497         else
2498           {
2499             PUSHA(*field->u.object_addr);
2500             newinsn = AMPAMP (getstatic_resolved_obj);
2501           }
2502
2503 #ifdef DIRECT_THREADED
2504         pc[-2].insn = newinsn;
2505         pc[-1].datum = field->u.addr;
2506 #endif /* DIRECT_THREADED */
2507       }
2508       NEXT_INSN;
2509
2510 #ifdef DIRECT_THREADED
2511     getstatic_resolved_1:
2512       PUSHI (*(jbyte *) AVAL ());
2513       NEXT_INSN;
2514
2515     getstatic_resolved_char:
2516       PUSHI (*(jchar *) AVAL ());
2517       NEXT_INSN;
2518
2519     getstatic_resolved_short:
2520       PUSHI (*(jshort *) AVAL ());
2521       NEXT_INSN;
2522
2523     getstatic_resolved_4:
2524       PUSHI (*(jint *) AVAL ());
2525       NEXT_INSN;
2526
2527     getstatic_resolved_8:
2528       PUSHL (*(jlong *) AVAL ());
2529       NEXT_INSN;
2530
2531     getstatic_resolved_obj:
2532       PUSHA (*(jobject *) AVAL ());
2533       NEXT_INSN;
2534 #endif /* DIRECT_THREADED */
2535
2536     insn_getfield:
2537       {
2538         jint fieldref_index = GET2U ();
2539         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2540         _Jv_Field *field = pool_data[fieldref_index].field;
2541
2542         if ((field->flags & Modifier::STATIC) != 0)
2543           throw_incompatible_class_change_error 
2544             (JvNewStringLatin1 ("field is static"));
2545
2546         jclass type = field->type;
2547         jint field_offset = field->u.boffset;
2548         if (field_offset > 0xffff)
2549           throw new java::lang::VirtualMachineError;
2550
2551         jobject obj   = POPA();
2552         NULLCHECK(obj);
2553
2554         void *newinsn = NULL;
2555         _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2556         if (type->isPrimitive ())
2557           {
2558             switch (type->size_in_bytes)
2559               {
2560               case 1:
2561                 PUSHI (val->byte_value);
2562                 newinsn = AMPAMP (getfield_resolved_1);
2563                 break;
2564
2565               case 2:
2566                 if (type == JvPrimClass (char))
2567                   {
2568                     PUSHI (val->char_value);
2569                     newinsn = AMPAMP (getfield_resolved_char);
2570                   }
2571                 else
2572                   {
2573                     PUSHI (val->short_value);
2574                     newinsn = AMPAMP (getfield_resolved_short);
2575                   }
2576                 break;
2577
2578               case 4:
2579                 PUSHI (val->int_value);
2580                 newinsn = AMPAMP (getfield_resolved_4);
2581                 break;
2582
2583               case 8:
2584                 PUSHL (val->long_value);
2585                 newinsn = AMPAMP (getfield_resolved_8);
2586                 break;
2587               }
2588           }
2589         else
2590           {
2591             PUSHA (val->object_value);
2592             newinsn = AMPAMP (getfield_resolved_obj);
2593           }
2594
2595 #ifdef DIRECT_THREADED
2596         pc[-2].insn = newinsn;
2597         pc[-1].int_val = field_offset;
2598 #endif /* DIRECT_THREADED */
2599       }
2600       NEXT_INSN;
2601
2602 #ifdef DIRECT_THREADED
2603     getfield_resolved_1:
2604       {
2605         char *obj = (char *) POPA ();
2606         NULLCHECK (obj);
2607         PUSHI (*(jbyte *) (obj + INTVAL ()));
2608       }
2609       NEXT_INSN;
2610
2611     getfield_resolved_char:
2612       {
2613         char *obj = (char *) POPA ();
2614         NULLCHECK (obj);
2615         PUSHI (*(jchar *) (obj + INTVAL ()));
2616       }
2617       NEXT_INSN;
2618
2619     getfield_resolved_short:
2620       {
2621         char *obj = (char *) POPA ();
2622         NULLCHECK (obj);
2623         PUSHI (*(jshort *) (obj + INTVAL ()));
2624       }
2625       NEXT_INSN;
2626
2627     getfield_resolved_4:
2628       {
2629         char *obj = (char *) POPA ();
2630         NULLCHECK (obj);
2631         PUSHI (*(jint *) (obj + INTVAL ()));
2632       }
2633       NEXT_INSN;
2634
2635     getfield_resolved_8:
2636       {
2637         char *obj = (char *) POPA ();
2638         NULLCHECK (obj);
2639         PUSHL (*(jlong *) (obj + INTVAL ()));
2640       }
2641       NEXT_INSN;
2642
2643     getfield_resolved_obj:
2644       {
2645         char *obj = (char *) POPA ();
2646         NULLCHECK (obj);
2647         PUSHA (*(jobject *) (obj + INTVAL ()));
2648       }
2649       NEXT_INSN;
2650 #endif /* DIRECT_THREADED */
2651
2652     insn_putstatic:
2653       {
2654         jint fieldref_index = GET2U ();
2655         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2656         _Jv_Field *field = pool_data[fieldref_index].field;
2657
2658         jclass type = field->type;
2659
2660         // ResolvePoolEntry cannot check this
2661         if ((field->flags & Modifier::STATIC) == 0)
2662           throw_incompatible_class_change_error 
2663             (JvNewStringLatin1 ("field no longer static"));
2664
2665         void *newinsn = NULL;
2666         if (type->isPrimitive ())
2667           {
2668             switch (type->size_in_bytes) 
2669               {
2670               case 1:
2671                 {
2672                   jint value = POPI();
2673                   *field->u.byte_addr = value;
2674                   newinsn = AMPAMP (putstatic_resolved_1);
2675                   break;
2676                 }
2677
2678               case 2:
2679                 {
2680                   jint value = POPI();
2681                   *field->u.char_addr = value;
2682                   newinsn = AMPAMP (putstatic_resolved_2);
2683                   break;
2684                 }
2685
2686               case 4:
2687                 {
2688                   jint value = POPI();
2689                   *field->u.int_addr = value;
2690                   newinsn = AMPAMP (putstatic_resolved_4);
2691                   break;
2692                 }
2693
2694               case 8:
2695                 {
2696                   jlong value = POPL();
2697                   *field->u.long_addr = value;
2698                   newinsn = AMPAMP (putstatic_resolved_8);
2699                   break;
2700                 }
2701               }
2702           }
2703         else
2704           {
2705             jobject value = POPA();
2706             *field->u.object_addr = value;
2707             newinsn = AMPAMP (putstatic_resolved_obj);
2708           }
2709
2710 #ifdef DIRECT_THREADED
2711         pc[-2].insn = newinsn;
2712         pc[-1].datum = field->u.addr;
2713 #endif /* DIRECT_THREADED */
2714       }
2715       NEXT_INSN;
2716
2717 #ifdef DIRECT_THREADED
2718     putstatic_resolved_1:
2719       *(jbyte *) AVAL () = POPI ();
2720       NEXT_INSN;
2721
2722     putstatic_resolved_2:
2723       *(jchar *) AVAL () = POPI ();
2724       NEXT_INSN;
2725
2726     putstatic_resolved_4:
2727       *(jint *) AVAL () = POPI ();
2728       NEXT_INSN;
2729
2730     putstatic_resolved_8:
2731       *(jlong *) AVAL () = POPL ();
2732       NEXT_INSN;
2733
2734     putstatic_resolved_obj:
2735       *(jobject *) AVAL () = POPA ();
2736       NEXT_INSN;
2737 #endif /* DIRECT_THREADED */
2738
2739     insn_putfield:
2740       {
2741         jint fieldref_index = GET2U ();
2742         _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2743         _Jv_Field *field = pool_data[fieldref_index].field;
2744
2745         jclass type = field->type;
2746
2747         if ((field->flags & Modifier::STATIC) != 0)
2748           throw_incompatible_class_change_error 
2749             (JvNewStringLatin1 ("field is static"));
2750
2751         jint field_offset = field->u.boffset;
2752         if (field_offset > 0xffff)
2753           throw new java::lang::VirtualMachineError;
2754
2755         void *newinsn = NULL;
2756         if (type->isPrimitive ())
2757           {
2758             switch (type->size_in_bytes) 
2759               {
2760               case 1:
2761                 {
2762                   jint    value = POPI();
2763                   jobject obj   = POPA();
2764                   NULLCHECK(obj);
2765                   *(jbyte*) ((char*)obj + field_offset) = value;
2766                   newinsn = AMPAMP (putfield_resolved_1);
2767                   break;
2768                 }
2769
2770               case 2:
2771                 {
2772                   jint    value = POPI();
2773                   jobject obj   = POPA();
2774                   NULLCHECK(obj);
2775                   *(jchar*) ((char*)obj + field_offset) = value;
2776                   newinsn = AMPAMP (putfield_resolved_2);
2777                   break;
2778                 }
2779
2780               case 4:
2781                 {
2782                   jint    value = POPI();
2783                   jobject obj   = POPA();
2784                   NULLCHECK(obj);
2785                   *(jint*) ((char*)obj + field_offset) = value;
2786                   newinsn = AMPAMP (putfield_resolved_4);
2787                   break;
2788                 }
2789
2790               case 8:
2791                 {
2792                   jlong   value = POPL();
2793                   jobject obj   = POPA();
2794                   NULLCHECK(obj);
2795                   *(jlong*) ((char*)obj + field_offset) = value;
2796                   newinsn = AMPAMP (putfield_resolved_8);
2797                   break;
2798                 }
2799               }
2800           }
2801         else
2802           {
2803             jobject value = POPA();
2804             jobject obj   = POPA();
2805             NULLCHECK(obj);
2806             *(jobject*) ((char*)obj + field_offset) = value;
2807             newinsn = AMPAMP (putfield_resolved_obj);
2808           }
2809
2810 #ifdef DIRECT_THREADED
2811         pc[-2].insn = newinsn;
2812         pc[-1].int_val = field_offset;
2813 #endif /* DIRECT_THREADED */
2814       }
2815       NEXT_INSN;
2816
2817 #ifdef DIRECT_THREADED
2818     putfield_resolved_1:
2819       {
2820         jint val = POPI ();
2821         char *obj = (char *) POPA ();
2822         NULLCHECK (obj);
2823         *(jbyte *) (obj + INTVAL ()) = val;
2824       }
2825       NEXT_INSN;
2826
2827     putfield_resolved_2:
2828       {
2829         jint val = POPI ();
2830         char *obj = (char *) POPA ();
2831         NULLCHECK (obj);
2832         *(jchar *) (obj + INTVAL ()) = val;
2833       }
2834       NEXT_INSN;
2835
2836     putfield_resolved_4:
2837       {
2838         jint val = POPI ();
2839         char *obj = (char *) POPA ();
2840         NULLCHECK (obj);
2841         *(jint *) (obj + INTVAL ()) = val;
2842       }
2843       NEXT_INSN;
2844
2845     putfield_resolved_8:
2846       {
2847         jlong val = POPL ();
2848         char *obj = (char *) POPA ();
2849         NULLCHECK (obj);
2850         *(jlong *) (obj + INTVAL ()) = val;
2851       }
2852       NEXT_INSN;
2853
2854     putfield_resolved_obj:
2855       {
2856         jobject val = POPA ();
2857         char *obj = (char *) POPA ();
2858         NULLCHECK (obj);
2859         *(jobject *) (obj + INTVAL ()) = val;
2860       }
2861       NEXT_INSN;
2862 #endif /* DIRECT_THREADED */
2863
2864     insn_invokespecial:
2865       {
2866         int index = GET2U ();
2867
2868         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2869                                                    index)).rmethod;
2870
2871         sp -= rmeth->stack_item_count;
2872
2873         // We don't use NULLCHECK here because we can't rely on that
2874         // working for <init>.  So instead we do an explicit test.
2875         if (! sp[0].o)
2876           {
2877             SAVE_PC();
2878             throw new java::lang::NullPointerException;
2879           }
2880
2881         fun = (void (*)()) rmeth->method->ncode;
2882
2883 #ifdef DIRECT_THREADED
2884         // Rewrite instruction so that we use a faster pre-resolved
2885         // method.
2886         pc[-2].insn = &&invokespecial_resolved;
2887         pc[-1].datum = rmeth;
2888 #endif /* DIRECT_THREADED */
2889       }
2890       goto perform_invoke;
2891
2892 #ifdef DIRECT_THREADED
2893     invokespecial_resolved:
2894       {
2895         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2896         sp -= rmeth->stack_item_count;
2897         // We don't use NULLCHECK here because we can't rely on that
2898         // working for <init>.  So instead we do an explicit test.
2899         if (! sp[0].o)
2900           {
2901             SAVE_PC();
2902             throw new java::lang::NullPointerException;
2903           }
2904         fun = (void (*)()) rmeth->method->ncode;
2905       }
2906       goto perform_invoke;
2907 #endif /* DIRECT_THREADED */
2908
2909     insn_invokestatic:
2910       {
2911         int index = GET2U ();
2912
2913         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2914                                                    index)).rmethod;
2915
2916         sp -= rmeth->stack_item_count;
2917
2918         fun = (void (*)()) rmeth->method->ncode;
2919
2920 #ifdef DIRECT_THREADED
2921         // Rewrite instruction so that we use a faster pre-resolved
2922         // method.
2923         pc[-2].insn = &&invokestatic_resolved;
2924         pc[-1].datum = rmeth;
2925 #endif /* DIRECT_THREADED */
2926       }
2927       goto perform_invoke;
2928
2929 #ifdef DIRECT_THREADED
2930     invokestatic_resolved:
2931       {
2932         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2933         sp -= rmeth->stack_item_count;
2934         fun = (void (*)()) rmeth->method->ncode;
2935       }
2936       goto perform_invoke;
2937 #endif /* DIRECT_THREADED */
2938
2939     insn_invokeinterface:
2940       {
2941         int index = GET2U ();
2942
2943         rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2944                                                    index)).rmethod;
2945
2946         sp -= rmeth->stack_item_count;
2947
2948         jobject rcv = sp[0].o;
2949
2950         NULLCHECK (rcv);
2951
2952         fun = (void (*)())
2953           _Jv_LookupInterfaceMethod (rcv->getClass (),
2954                                      rmeth->method->name,
2955                                      rmeth->method->signature);
2956
2957 #ifdef DIRECT_THREADED
2958         // Rewrite instruction so that we use a faster pre-resolved
2959         // method.
2960         pc[-2].insn = &&invokeinterface_resolved;
2961         pc[-1].datum = rmeth;
2962 #else
2963         // Skip dummy bytes.
2964         pc += 2;
2965 #endif /* DIRECT_THREADED */
2966       }
2967       goto perform_invoke;
2968
2969 #ifdef DIRECT_THREADED
2970     invokeinterface_resolved:
2971       {
2972         rmeth = (_Jv_ResolvedMethod *) AVAL ();
2973         sp -= rmeth->stack_item_count;
2974         jobject rcv = sp[0].o;
2975         NULLCHECK (rcv);
2976         fun = (void (*)())
2977           _Jv_LookupInterfaceMethod (rcv->getClass (),
2978                                      rmeth->method->name,
2979                                      rmeth->method->signature);
2980       }
2981       goto perform_invoke;
2982 #endif /* DIRECT_THREADED */
2983
2984     insn_new:
2985       {
2986         int index = GET2U ();
2987         jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2988                                                           index)).clazz;
2989         /* VM spec, section 3.11.5 */
2990         if ((klass->getModifiers() & Modifier::ABSTRACT)
2991             || klass->isInterface())
2992           throw new java::lang::InstantiationException;
2993         jobject res = _Jv_AllocObject (klass);
2994         PUSHA (res);
2995
2996 #ifdef DIRECT_THREADED
2997         pc[-2].insn = &&new_resolved;
2998         pc[-1].datum = klass;
2999 #endif /* DIRECT_THREADED */
3000       }
3001       NEXT_INSN;
3002
3003 #ifdef DIRECT_THREADED
3004     new_resolved:
3005       {
3006         jclass klass = (jclass) AVAL ();
3007         jobject res = _Jv_AllocObject (klass);
3008         PUSHA (res);
3009       }
3010       NEXT_INSN;
3011 #endif /* DIRECT_THREADED */
3012
3013     insn_newarray:
3014       {
3015         int atype = GET1U ();
3016         int size  = POPI();
3017         jobject result = _Jv_NewArray (atype, size);
3018         PUSHA (result);
3019       }
3020       NEXT_INSN;
3021
3022     insn_anewarray:
3023       {
3024         int index = GET2U ();
3025         jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3026                                                           index)).clazz;
3027         int size  = POPI();
3028         jobject result = _Jv_NewObjectArray (size, klass, 0);
3029         PUSHA (result);
3030
3031 #ifdef DIRECT_THREADED
3032         pc[-2].insn = &&anewarray_resolved;
3033         pc[-1].datum = klass;
3034 #endif /* DIRECT_THREADED */
3035       }
3036       NEXT_INSN;
3037
3038 #ifdef DIRECT_THREADED
3039     anewarray_resolved:
3040       {
3041         jclass klass = (jclass) AVAL ();
3042         int size = POPI ();
3043         jobject result = _Jv_NewObjectArray (size, klass, 0);
3044         PUSHA (result);
3045       }
3046       NEXT_INSN;
3047 #endif /* DIRECT_THREADED */
3048
3049     insn_arraylength:
3050       {
3051         __JArray *arr = (__JArray*)POPA();
3052         NULLARRAYCHECK (arr);
3053         PUSHI (arr->length);
3054       }
3055       NEXT_INSN;
3056
3057     insn_athrow:
3058       {
3059         jobject value = POPA();
3060         throw static_cast<jthrowable>(value);
3061       }
3062       NEXT_INSN;
3063
3064     insn_checkcast:
3065       {
3066         SAVE_PC();
3067         jobject value = POPA();
3068         jint index = GET2U ();
3069         jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3070                                                        index)).clazz;
3071
3072         value = (jobject) _Jv_CheckCast (to, value);
3073
3074         PUSHA (value);
3075
3076 #ifdef DIRECT_THREADED
3077         pc[-2].insn = &&checkcast_resolved;
3078         pc[-1].datum = to;
3079 #endif /* DIRECT_THREADED */
3080       }
3081       NEXT_INSN;
3082
3083 #ifdef DIRECT_THREADED
3084     checkcast_resolved:
3085       {
3086         SAVE_PC();
3087         jobject value = POPA ();
3088         jclass to = (jclass) AVAL ();
3089         value = (jobject) _Jv_CheckCast (to, value);
3090         PUSHA (value);
3091       }
3092       NEXT_INSN;
3093 #endif /* DIRECT_THREADED */
3094
3095     insn_instanceof:
3096       {
3097         SAVE_PC();
3098         jobject value = POPA();
3099         jint index = GET2U ();
3100         jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3101                                                        index)).clazz;
3102         PUSHI (to->isInstance (value));
3103
3104 #ifdef DIRECT_THREADED
3105         pc[-2].insn = &&instanceof_resolved;
3106         pc[-1].datum = to;
3107 #endif /* DIRECT_THREADED */
3108       }
3109       NEXT_INSN;
3110
3111 #ifdef DIRECT_THREADED
3112     instanceof_resolved:
3113       {
3114         jobject value = POPA ();
3115         jclass to = (jclass) AVAL ();
3116         PUSHI (to->isInstance (value));
3117       }
3118       NEXT_INSN;
3119 #endif /* DIRECT_THREADED */
3120
3121     insn_monitorenter:
3122       {
3123         jobject value = POPA();
3124         NULLCHECK(value);
3125         _Jv_MonitorEnter (value);
3126       }
3127       NEXT_INSN;
3128
3129     insn_monitorexit:
3130       {
3131         jobject value = POPA();
3132         NULLCHECK(value);
3133         _Jv_MonitorExit (value);
3134       }
3135       NEXT_INSN;
3136
3137     insn_ifnull:
3138       {
3139         jobject val = POPA();
3140         if (val == NULL)
3141           TAKE_GOTO;
3142         else
3143           SKIP_GOTO;
3144       }
3145       NEXT_INSN;
3146
3147     insn_ifnonnull:
3148       {
3149         jobject val = POPA();
3150         if (val != NULL)
3151           TAKE_GOTO;
3152         else
3153           SKIP_GOTO;
3154       }
3155       NEXT_INSN;
3156
3157     insn_multianewarray:
3158       {
3159         int kind_index = GET2U ();
3160         int dim        = GET1U ();
3161
3162         jclass type    
3163           = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3164                                                kind_index)).clazz;
3165         jint *sizes    = (jint*) __builtin_alloca (sizeof (jint)*dim);
3166
3167         for (int i = dim - 1; i >= 0; i--)
3168           {
3169             sizes[i] = POPI ();
3170           }
3171
3172         jobject res    = _Jv_NewMultiArray (type,dim, sizes);
3173
3174         PUSHA (res);
3175       }
3176       NEXT_INSN;
3177
3178 #ifndef DIRECT_THREADED
3179     insn_wide:
3180       {
3181         jint the_mod_op = get1u (pc++);
3182         jint wide       = get2u (pc); pc += 2;
3183
3184         switch (the_mod_op)
3185           {
3186           case op_istore:
3187             STOREI (wide);
3188             NEXT_INSN;
3189
3190           case op_fstore:
3191             STOREF (wide);
3192             NEXT_INSN;
3193
3194           case op_astore:
3195             STOREA (wide);
3196             NEXT_INSN;
3197
3198           case op_lload:
3199             LOADL (wide);
3200             NEXT_INSN;
3201
3202           case op_dload:
3203             LOADD (wide);
3204             NEXT_INSN;
3205
3206           case op_iload:
3207             LOADI (wide);
3208             NEXT_INSN;
3209
3210           case op_fload:
3211             LOADF (wide);
3212             NEXT_INSN;
3213
3214           case op_aload:
3215             LOADA (wide);
3216             NEXT_INSN;
3217
3218           case op_lstore:
3219             STOREL (wide);
3220             NEXT_INSN;
3221
3222           case op_dstore:
3223             STORED (wide);
3224             NEXT_INSN;
3225
3226           case op_ret:
3227             pc = (unsigned char*) PEEKA (wide);
3228             NEXT_INSN;
3229
3230           case op_iinc:
3231             {
3232               jint amount = get2s (pc); pc += 2;
3233               jint value = PEEKI (wide);
3234               POKEI (wide, value+amount);
3235             }
3236             NEXT_INSN;
3237
3238           default:
3239             throw_internal_error ("illegal bytecode modified by wide");
3240           }
3241
3242       }
3243 #endif /* DIRECT_THREADED */
3244     }
3245   catch (java::lang::Throwable *ex)
3246     {
3247 #ifdef DIRECT_THREADED
3248       void *logical_pc = (void *) ((insn_slot *) pc - 1);
3249 #else
3250       int logical_pc = pc - 1 - bytecode ();
3251 #endif
3252       _Jv_InterpException *exc = meth->exceptions ();
3253       jclass exc_class = ex->getClass ();
3254
3255       for (int i = 0; i < meth->exc_count; i++)
3256         {
3257           if (PCVAL (exc[i].start_pc) <= logical_pc
3258               && logical_pc < PCVAL (exc[i].end_pc))
3259             {
3260 #ifdef DIRECT_THREADED
3261               jclass handler = (jclass) exc[i].handler_type.p;
3262 #else
3263               jclass handler = NULL;
3264               if (exc[i].handler_type.i != 0)
3265                 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3266                                                              exc[i].handler_type.i)).clazz;
3267 #endif /* DIRECT_THREADED */
3268
3269               if (handler == NULL || handler->isAssignableFrom (exc_class))
3270                 {
3271 #ifdef DIRECT_THREADED
3272                   pc = (insn_slot *) exc[i].handler_pc.p;
3273 #else
3274                   pc = bytecode () + exc[i].handler_pc.i;
3275 #endif /* DIRECT_THREADED */
3276                   sp = stack;
3277                   sp++->o = ex; // Push exception.
3278                   NEXT_INSN;
3279                 }
3280             }
3281         }
3282
3283       // No handler, so re-throw.
3284       throw ex;
3285     }
3286 }
3287
3288 static void
3289 throw_internal_error (char *msg)
3290 {
3291   throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3292 }
3293
3294 static void 
3295 throw_incompatible_class_change_error (jstring msg)
3296 {
3297   throw new java::lang::IncompatibleClassChangeError (msg);
3298 }
3299
3300 #ifndef HANDLE_SEGV
3301 static java::lang::NullPointerException *null_pointer_exc;
3302 static void 
3303 throw_null_pointer_exception ()
3304 {
3305   if (null_pointer_exc == NULL)
3306     null_pointer_exc = new java::lang::NullPointerException;
3307
3308   throw null_pointer_exc;
3309 }
3310 #endif
3311
3312 /* Look up source code line number for given bytecode (or direct threaded
3313    interpreter) PC. */
3314 int
3315 _Jv_InterpMethod::get_source_line(pc_t mpc)
3316 {
3317   int line = line_table_len > 0 ? line_table[0].line : -1;
3318   for (int i = 1; i < line_table_len; i++)
3319     if (line_table[i].pc > mpc)
3320       break;
3321     else
3322       line = line_table[i].line;
3323
3324   return line;
3325 }
3326
3327 /** Do static initialization for fields with a constant initializer */
3328 void
3329 _Jv_InitField (jobject obj, jclass klass, int index)
3330 {
3331   using namespace java::lang::reflect;
3332
3333   if (obj != 0 && klass == 0)
3334     klass = obj->getClass ();
3335
3336   if (!_Jv_IsInterpretedClass (klass))
3337     return;
3338
3339   _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3340
3341   _Jv_Field * field = (&klass->fields[0]) + index;
3342
3343   if (index > klass->field_count)
3344     throw_internal_error ("field out of range");
3345
3346   int init = iclass->field_initializers[index];
3347   if (init == 0)
3348     return;
3349
3350   _Jv_Constants *pool = &klass->constants;
3351   int tag = pool->tags[init];
3352
3353   if (! field->isResolved ())
3354     throw_internal_error ("initializing unresolved field");
3355
3356   if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3357     throw_internal_error ("initializing non-static field with no object");
3358
3359   void *addr = 0;
3360
3361   if ((field->flags & Modifier::STATIC) != 0)
3362     addr = (void*) field->u.addr;
3363   else
3364     addr = (void*) (((char*)obj) + field->u.boffset);
3365
3366   switch (tag)
3367     {
3368     case JV_CONSTANT_String:
3369       {
3370         jstring str;
3371         str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3372         pool->data[init].string = str;
3373         pool->tags[init] = JV_CONSTANT_ResolvedString;
3374       }
3375       /* fall through */
3376
3377     case JV_CONSTANT_ResolvedString:
3378       if (! (field->type == &java::lang::String::class$
3379              || field->type == &java::lang::Class::class$))
3380         throw_class_format_error ("string initialiser to non-string field");
3381
3382       *(jstring*)addr = pool->data[init].string;
3383       break;
3384
3385     case JV_CONSTANT_Integer:
3386       {
3387         int value = pool->data[init].i;
3388
3389         if (field->type == JvPrimClass (boolean))
3390           *(jboolean*)addr = (jboolean)value;
3391         
3392         else if (field->type == JvPrimClass (byte))
3393           *(jbyte*)addr = (jbyte)value;
3394         
3395         else if (field->type == JvPrimClass (char))
3396           *(jchar*)addr = (jchar)value;
3397
3398         else if (field->type == JvPrimClass (short))
3399           *(jshort*)addr = (jshort)value;
3400         
3401         else if (field->type == JvPrimClass (int))
3402           *(jint*)addr = (jint)value;
3403
3404         else
3405           throw_class_format_error ("erroneous field initializer");
3406       }  
3407       break;
3408
3409     case JV_CONSTANT_Long:
3410       if (field->type != JvPrimClass (long))
3411         throw_class_format_error ("erroneous field initializer");
3412
3413       *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3414       break;
3415
3416     case JV_CONSTANT_Float:
3417       if (field->type != JvPrimClass (float))
3418         throw_class_format_error ("erroneous field initializer");
3419
3420       *(jfloat*)addr = pool->data[init].f;
3421       break;
3422
3423     case JV_CONSTANT_Double:
3424       if (field->type != JvPrimClass (double))
3425         throw_class_format_error ("erroneous field initializer");
3426
3427       *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3428       break;
3429
3430     default:
3431       throw_class_format_error ("erroneous field initializer");
3432     }
3433 }
3434
3435 inline static unsigned char*
3436 skip_one_type (unsigned char* ptr)
3437 {
3438   int ch = *ptr++;
3439
3440   while (ch == '[')
3441     { 
3442       ch = *ptr++;
3443     }
3444   
3445   if (ch == 'L')
3446     {
3447       do { ch = *ptr++; } while (ch != ';');
3448     }
3449
3450   return ptr;
3451 }
3452
3453 static ffi_type*
3454 get_ffi_type_from_signature (unsigned char* ptr)
3455 {
3456   switch (*ptr) 
3457     {
3458     case 'L':
3459     case '[':
3460       return &ffi_type_pointer;
3461       break;
3462
3463     case 'Z':
3464       // On some platforms a bool is a byte, on others an int.
3465       if (sizeof (jboolean) == sizeof (jbyte))
3466         return &ffi_type_sint8;
3467       else
3468         {
3469           JvAssert (sizeof (jbyte) == sizeof (jint));
3470           return &ffi_type_sint32;
3471         }
3472       break;
3473
3474     case 'B':
3475       return &ffi_type_sint8;
3476       break;
3477       
3478     case 'C':
3479       return &ffi_type_uint16;
3480       break;
3481           
3482     case 'S': 
3483       return &ffi_type_sint16;
3484       break;
3485           
3486     case 'I':
3487       return &ffi_type_sint32;
3488       break;
3489           
3490     case 'J':
3491       return &ffi_type_sint64;
3492       break;
3493           
3494     case 'F':
3495       return &ffi_type_float;
3496       break;
3497           
3498     case 'D':
3499       return &ffi_type_double;
3500       break;
3501
3502     case 'V':
3503       return &ffi_type_void;
3504       break;
3505     }
3506
3507   throw_internal_error ("unknown type in signature");
3508 }
3509
3510 /* this function yields the number of actual arguments, that is, if the
3511  * function is non-static, then one is added to the number of elements
3512  * found in the signature */
3513
3514 int 
3515 _Jv_count_arguments (_Jv_Utf8Const *signature,
3516                      jboolean staticp)
3517 {
3518   unsigned char *ptr = (unsigned char*) signature->chars();
3519   int arg_count = staticp ? 0 : 1;
3520
3521   /* first, count number of arguments */
3522
3523   // skip '('
3524   ptr++;
3525
3526   // count args
3527   while (*ptr != ')')
3528     {
3529       ptr = skip_one_type (ptr);
3530       arg_count += 1;
3531     }
3532
3533   return arg_count;
3534 }
3535
3536 /* This beast will build a cif, given the signature.  Memory for
3537  * the cif itself and for the argument types must be allocated by the
3538  * caller.
3539  */
3540
3541 static int 
3542 init_cif (_Jv_Utf8Const* signature,
3543           int arg_count,
3544           jboolean staticp,
3545           ffi_cif *cif,
3546           ffi_type **arg_types,
3547           ffi_type **rtype_p)
3548 {
3549   unsigned char *ptr = (unsigned char*) signature->chars();
3550
3551   int arg_index = 0;            // arg number
3552   int item_count = 0;           // stack-item count
3553
3554   // setup receiver
3555   if (!staticp)
3556     {
3557       arg_types[arg_index++] = &ffi_type_pointer;
3558       item_count += 1;
3559     }
3560
3561   // skip '('
3562   ptr++;
3563
3564   // assign arg types
3565   while (*ptr != ')')
3566     {
3567       arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3568
3569       if (*ptr == 'J' || *ptr == 'D')
3570         item_count += 2;
3571       else
3572         item_count += 1;
3573
3574       ptr = skip_one_type (ptr);
3575     }
3576
3577   // skip ')'
3578   ptr++;
3579   ffi_type *rtype = get_ffi_type_from_signature (ptr);
3580
3581   ptr = skip_one_type (ptr);
3582   if (ptr != (unsigned char*)signature->chars() + signature->len())
3583     throw_internal_error ("did not find end of signature");
3584
3585   if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3586                     arg_count, rtype, arg_types) != FFI_OK)
3587     throw_internal_error ("ffi_prep_cif failed");
3588
3589   if (rtype_p != NULL)
3590     *rtype_p = rtype;
3591
3592   return item_count;
3593 }
3594
3595 #if FFI_NATIVE_RAW_API
3596 #   define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3597 #   define FFI_RAW_SIZE ffi_raw_size
3598 #else
3599 #   define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3600 #   define FFI_RAW_SIZE ffi_java_raw_size
3601 #endif
3602
3603 /* we put this one here, and not in interpret.cc because it
3604  * calls the utility routines _Jv_count_arguments 
3605  * which are static to this module.  The following struct defines the
3606  * layout we use for the stubs, it's only used in the ncode method. */
3607
3608 typedef struct {
3609   ffi_raw_closure  closure;
3610   ffi_cif   cif;
3611   ffi_type *arg_types[0];
3612 } ncode_closure;
3613
3614 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3615
3616 void *
3617 _Jv_InterpMethod::ncode ()
3618 {
3619   using namespace java::lang::reflect;
3620
3621   if (self->ncode != 0)
3622     return self->ncode;
3623
3624   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3625   int arg_count = _Jv_count_arguments (self->signature, staticp);
3626
3627   ncode_closure *closure =
3628     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3629                                         + arg_count * sizeof (ffi_type*));
3630
3631   init_cif (self->signature,
3632             arg_count,
3633             staticp,
3634             &closure->cif,
3635             &closure->arg_types[0],
3636             NULL);
3637
3638   ffi_closure_fun fun;
3639
3640   args_raw_size = FFI_RAW_SIZE (&closure->cif);
3641
3642   JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3643
3644   if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3645     {
3646       if (staticp)
3647         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3648       else
3649         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object; 
3650     }
3651   else
3652     {
3653       if (staticp)
3654         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3655       else
3656         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3657     }
3658
3659   FFI_PREP_RAW_CLOSURE (&closure->closure,
3660                         &closure->cif, 
3661                         fun,
3662                         (void*)this);
3663
3664   self->ncode = (void*)closure;
3665   return self->ncode;
3666 }
3667
3668 void *
3669 _Jv_JNIMethod::ncode ()
3670 {
3671   using namespace java::lang::reflect;
3672
3673   if (self->ncode != 0)
3674     return self->ncode;
3675
3676   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3677   int arg_count = _Jv_count_arguments (self->signature, staticp);
3678
3679   ncode_closure *closure =
3680     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3681                                     + arg_count * sizeof (ffi_type*));
3682
3683   ffi_type *rtype;
3684   init_cif (self->signature,
3685             arg_count,
3686             staticp,
3687             &closure->cif,
3688             &closure->arg_types[0],
3689             &rtype);
3690
3691   ffi_closure_fun fun;
3692
3693   args_raw_size = FFI_RAW_SIZE (&closure->cif);
3694
3695   // Initialize the argument types and CIF that represent the actual
3696   // underlying JNI function.
3697   int extra_args = 1;
3698   if ((self->accflags & Modifier::STATIC))
3699     ++extra_args;
3700   jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3701                                                 * sizeof (ffi_type *));
3702   int offset = 0;
3703   jni_arg_types[offset++] = &ffi_type_pointer;
3704   if ((self->accflags & Modifier::STATIC))
3705     jni_arg_types[offset++] = &ffi_type_pointer;
3706   memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3707           arg_count * sizeof (ffi_type *));
3708
3709   if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3710                     extra_args + arg_count, rtype,
3711                     jni_arg_types) != FFI_OK)
3712     throw_internal_error ("ffi_prep_cif failed for JNI function");
3713
3714   JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3715
3716   // FIXME: for now we assume that all native methods for
3717   // interpreted code use JNI.
3718   fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3719
3720   FFI_PREP_RAW_CLOSURE (&closure->closure,
3721                         &closure->cif, 
3722                         fun,
3723                         (void*) this);
3724
3725   self->ncode = (void *) closure;
3726   return self->ncode;
3727 }
3728
3729 static void
3730 throw_class_format_error (jstring msg)
3731 {
3732   throw (msg
3733          ? new java::lang::ClassFormatError (msg)
3734          : new java::lang::ClassFormatError);
3735 }
3736
3737 static void
3738 throw_class_format_error (char *msg)
3739 {
3740   throw_class_format_error (JvNewStringLatin1 (msg));
3741 }
3742
3743 \f
3744
3745 void
3746 _Jv_InterpreterEngine::do_verify (jclass klass)
3747 {
3748   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3749   for (int i = 0; i < klass->method_count; i++)
3750     {
3751       using namespace java::lang::reflect;
3752       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3753       _Jv_ushort accflags = klass->methods[i].accflags;
3754       if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3755         {
3756           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3757           _Jv_VerifyMethod (im);
3758         }
3759     }
3760 }
3761
3762 void
3763 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3764 {
3765   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3766   for (int i = 0; i < klass->method_count; i++)
3767     {
3768       // Just skip abstract methods.  This is particularly important
3769       // because we don't resize the interpreted_methods array when
3770       // miranda methods are added to it.
3771       if ((klass->methods[i].accflags
3772            & java::lang::reflect::Modifier::ABSTRACT)
3773           != 0)
3774         continue;
3775
3776       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3777
3778       if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3779           != 0)
3780         {
3781           // You might think we could use a virtual `ncode' method in
3782           // the _Jv_MethodBase and unify the native and non-native
3783           // cases.  Well, we can't, because we don't allocate these
3784           // objects using `new', and thus they don't get a vtable.
3785           _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3786           klass->methods[i].ncode = jnim->ncode ();
3787         }
3788       else if (imeth != 0)              // it could be abstract
3789         {
3790           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3791           klass->methods[i].ncode = im->ncode ();
3792         }
3793     }
3794 }
3795
3796 void
3797 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3798                                                   int static_size)
3799 {
3800   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3801
3802   char *static_data = (char *) _Jv_AllocBytes (static_size);
3803
3804   for (int i = 0; i < klass->field_count; i++)
3805     {
3806       _Jv_Field *field = &klass->fields[i];
3807
3808       if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3809         {
3810           field->u.addr  = static_data + field->u.boffset;
3811               
3812           if (iclass->field_initializers[i] != 0)
3813             {
3814               _Jv_Linker::resolve_field (field, klass->loader);
3815               _Jv_InitField (0, klass, i);
3816             }
3817         }
3818     }
3819
3820   // Now we don't need the field_initializers anymore, so let the
3821   // collector get rid of it.
3822   iclass->field_initializers = 0;
3823 }
3824
3825 _Jv_ResolvedMethod *
3826 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3827                                           jboolean staticp, jint vtable_index)
3828 {
3829   int arg_count = _Jv_count_arguments (method->signature, staticp);
3830
3831   _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3832     _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3833                     + arg_count*sizeof (ffi_type*));
3834
3835   result->stack_item_count
3836     = init_cif (method->signature,
3837                 arg_count,
3838                 staticp,
3839                 &result->cif,
3840                 &result->arg_types[0],
3841                 NULL);
3842
3843   result->vtable_index        = vtable_index;
3844   result->method              = method;
3845   result->klass               = klass;
3846
3847   return result;
3848 }
3849
3850 void
3851 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3852 {
3853   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3854   for (int i = 0; i < klass->method_count; i++)
3855     {
3856       // Just skip abstract methods.  This is particularly important
3857       // because we don't resize the interpreted_methods array when
3858       // miranda methods are added to it.
3859       if ((klass->methods[i].accflags
3860            & java::lang::reflect::Modifier::ABSTRACT)
3861           != 0)
3862         continue;
3863       // Miranda method additions mean that the `methods' array moves.
3864       // We cache a pointer into this array, so we have to update.
3865       iclass->interpreted_methods[i]->self = &klass->methods[i];
3866     }
3867 }
3868
3869 #endif // INTERPRETER