OSDN Git Service

2006-07-13 Bryce McKinlay <mckinlay@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / interpret.cc
index e5c40cf..b9abb01 100644 (file)
@@ -1,6 +1,6 @@
 // interpret.cc - Code for the interpreter
 
-/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
+/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
 
    This file is part of libgcj.
 
@@ -11,18 +11,13 @@ details.  */
 /* Author: Kresten Krab Thorup <krab@gnu.org>  */
 
 #include <config.h>
-
-// Define this to get the direct-threaded interpreter.  If undefined,
-// we revert to a basic bytecode interpreter.  The former is faster
-// but uses more memory.
-#define DIRECT_THREADED
+#include <platform.h>
 
 #pragma implementation "java-interp.h"
 
 #include <jvm.h>
 #include <java-cpool.h>
 #include <java-interp.h>
-// #include <java/lang/fdlibm.h>
 #include <java/lang/System.h>
 #include <java/lang/String.h>
 #include <java/lang/Integer.h>
@@ -30,52 +25,56 @@ details.  */
 #include <java/lang/StringBuffer.h>
 #include <java/lang/Class.h>
 #include <java/lang/reflect/Modifier.h>
-#include <java/lang/ClassCastException.h>
-#include <java/lang/VirtualMachineError.h>
 #include <java/lang/InternalError.h>
 #include <java/lang/NullPointerException.h>
 #include <java/lang/ArithmeticException.h>
 #include <java/lang/IncompatibleClassChangeError.h>
+#include <java/lang/InstantiationException.h>
+#include <java/lang/Thread.h>
 #include <java-insns.h>
 #include <java-signal.h>
+#include <java/lang/ClassFormatError.h>
+#include <execution.h>
+#include <java/lang/reflect/Modifier.h>
 
 #ifdef INTERPRETER
 
+// Execution engine for interpreted code.
+_Jv_InterpreterEngine _Jv_soleInterpreterEngine;
+
 #include <stdlib.h>
 
 using namespace gcj;
 
-static void throw_internal_error (char *msg)
+static void throw_internal_error (const char *msg)
   __attribute__ ((__noreturn__));
 static void throw_incompatible_class_change_error (jstring msg)
   __attribute__ ((__noreturn__));
-#ifndef HANDLE_SEGV
 static void throw_null_pointer_exception ()
   __attribute__ ((__noreturn__));
-#endif
 
-extern "C" double __ieee754_fmod (double,double);
+static void throw_class_format_error (jstring msg)
+       __attribute__ ((__noreturn__));
+static void throw_class_format_error (const char *msg)
+       __attribute__ ((__noreturn__));
 
-// This represents a single slot in the "compiled" form of the
-// bytecode.
-union insn_slot
-{
-  // Address of code.
-  void *insn;
-  // An integer value used by an instruction.
-  jint int_val;
-  // A pointer value used by an instruction.
-  void *datum;
-};
-
-// The type of the PC depends on whether we're doing direct threading
-// or a more ordinary bytecode interpreter.
 #ifdef DIRECT_THREADED
-typedef insn_slot *pc_t;
+// Lock to ensure that methods are not compiled concurrently.
+// We could use a finer-grained lock here, however it is not safe to use
+// the Class monitor as user code in another thread could hold it.
+static _Jv_Mutex_t compile_mutex;
+
+void
+_Jv_InitInterpreter()
+{
+  _Jv_MutexInit (&compile_mutex);
+}
 #else
-typedef unsigned char *pc_t;
+void _Jv_InitInterpreter() {}
 #endif
 
+extern "C" double __ieee754_fmod (double,double);
+
 static inline void dupx (_Jv_word *sp, int n, int x)
 {
   // first "slide" n+x elements n to the right
@@ -90,8 +89,7 @@ static inline void dupx (_Jv_word *sp, int n, int x)
     {
       sp[top-(n+x)-i] = sp[top-i];
     }
-  
-};
+}
 
 // Used to convert from floating types to integral types.
 template<typename TO, typename FROM>
@@ -221,15 +219,24 @@ static jint get4(unsigned char* loc) {
        | (((jint)(loc[3])) << 0);
 }
 
+#define SAVE_PC() frame_desc.pc = pc
 
+// We used to define this conditionally, depending on HANDLE_SEGV.
+// However, that runs into a problem if a chunk in low memory is
+// mapped and we try to look at a field near the end of a large
+// object.  See PR 26858 for details.  It is, most likely, relatively
+// inexpensive to simply do this check always.
+#define NULLCHECK(X) \
+  do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
+
+// Note that we can still conditionally define NULLARRAYCHECK, since
+// we know that all uses of an array will first reference the length
+// field, which is first -- and thus will trigger a SEGV.
 #ifdef HANDLE_SEGV
-#define NULLCHECK(X) 
-#define NULLARRAYCHECK(X)
+#define NULLARRAYCHECK(X) SAVE_PC()
 #else
-#define NULLCHECK(X) \
-  do { if ((X)==NULL) throw_null_pointer_exception (); } while (0)
 #define NULLARRAYCHECK(X) \
-  do { if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
+  do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
 #endif
 
 #define ARRAYBOUNDSCHECK(array, index)                                       \
@@ -240,39 +247,54 @@ static jint get4(unsigned char* loc) {
     }                                                                        \
   while (0)
 
-void _Jv_InterpMethod::run_normal (ffi_cif *,
-                                  void* ret,
-                                  ffi_raw * args,
-                                  void* __this)
+void
+_Jv_InterpMethod::run_normal (ffi_cif *,
+                             void* ret,
+                             ffi_raw * args,
+                             void* __this)
 {
   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
-  _this->run (ret, args);
+  run (ret, args, _this);
 }
 
-void _Jv_InterpMethod::run_synch_object (ffi_cif *,
-                                        void* ret,
-                                        ffi_raw * args,
-                                        void* __this)
+void
+_Jv_InterpMethod::run_synch_object (ffi_cif *,
+                                   void* ret,
+                                   ffi_raw * args,
+                                   void* __this)
 {
   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
 
   jobject rcv = (jobject) args[0].ptr;
   JvSynchronize mutex (rcv);
 
-  _this->run (ret, args);
+  run (ret, args, _this);
 }
 
-void _Jv_InterpMethod::run_synch_class (ffi_cif *,
-                                       void* ret,
-                                       ffi_raw * args,
-                                       void* __this)
+void
+_Jv_InterpMethod::run_class (ffi_cif *,
+                            void* ret,
+                            ffi_raw * args,
+                            void* __this)
+{
+  _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
+  _Jv_InitClass (_this->defining_class);
+  run (ret, args, _this);
+}
+
+void
+_Jv_InterpMethod::run_synch_class (ffi_cif *,
+                                  void* ret,
+                                  ffi_raw * args,
+                                  void* __this)
 {
   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
 
   jclass sync = _this->defining_class;
+  _Jv_InitClass (sync);
   JvSynchronize mutex (sync);
 
-  _this->run (ret, args);
+  run (ret, args, _this);
 }
 
 #ifdef DIRECT_THREADED
@@ -312,7 +334,8 @@ _Jv_InterpMethod::compile (const void * const *insn_targets)
 
       if (! first_pass)
        {
-         insns = (insn_slot *) _Jv_Malloc (sizeof (insn_slot) * next);
+         insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
+         number_insn_slots = next;
          next = 0;
        }
 
@@ -490,7 +513,16 @@ _Jv_InterpMethod::compile (const void * const *insn_targets)
              {
                int index = get1u (pc);
                ++pc;
-               SET_DATUM (pool_data[index].o);
+               // For an unresolved class we want to delay resolution
+               // until execution.
+               if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
+                 {
+                   --next;
+                   SET_INSN (insn_targets[int (op_jsr_w) + 1]);
+                   SET_INT (index);
+                 }
+               else
+                 SET_DATUM (pool_data[index].o);
              }
              break;
 
@@ -520,7 +552,16 @@ _Jv_InterpMethod::compile (const void * const *insn_targets)
              {
                int index = get2u (pc);
                pc += 2;
-               SET_DATUM (pool_data[index].o);
+               // For an unresolved class we want to delay resolution
+               // until execution.
+               if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
+                 {
+                   --next;
+                   SET_INSN (insn_targets[int (op_jsr_w) + 1]);
+                   SET_INT (index);
+                 }
+               else
+                 SET_DATUM (pool_data[index].o);
              }
              break;
 
@@ -735,37 +776,50 @@ _Jv_InterpMethod::compile (const void * const *insn_targets)
       exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
       exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
       exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
-      jclass handler = (_Jv_ResolvePoolEntry (defining_class,
-                                             exc[i].handler_type.i)).clazz;
+      // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
+      // during compilation.
+      jclass handler
+       = (_Jv_Linker::resolve_pool_entry (defining_class,
+                                            exc[i].handler_type.i)).clazz;
       exc[i].handler_type.p = handler;
     }
 
+  // Translate entries in the LineNumberTable from bytecode PC's to direct
+  // threaded interpreter instruction values.
+  for (int i = 0; i < line_table_len; i++)
+    {
+      int byte_pc = line_table[i].bytecode_pc;
+      // It isn't worth throwing an exception if this table is
+      // corrupted, but at the same time we don't want a crash.
+      if (byte_pc < 0 || byte_pc >= code_length)
+       byte_pc = 0;
+      line_table[i].pc = &insns[pc_mapping[byte_pc]];
+    }  
+
   prepared = insns;
 }
 #endif /* DIRECT_THREADED */
 
+/* Run the given method.
+   When args is NULL, don't run anything -- just compile it. */
 void
-_Jv_InterpMethod::run (void *retp, ffi_raw *args)
+_Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 {
   using namespace java::lang::reflect;
 
-  _Jv_word stack[max_stack];
-  _Jv_word *sp = stack;
-
-  _Jv_word locals[max_locals];
-
-  /* Go straight at it!  the ffi raw format matches the internal
-     stack representation exactly.  At least, that's the idea.
-  */
-  memcpy ((void*) locals, (void*) args, args_raw_size);
+  // FRAME_DESC registers this particular invocation as the top-most
+  // interpreter frame.  This lets the stack tracing code (for
+  // Throwable) print information about the method being interpreted
+  // rather than about the interpreter itself.  FRAME_DESC has a
+  // destructor so it cleans up automatically when the interpreter
+  // returns.
+  java::lang::Thread *thread = java::lang::Thread::currentThread();
+  _Jv_InterpFrame frame_desc (meth, thread);
 
-  _Jv_word *pool_data = defining_class->constants.data;
+  _Jv_word stack[meth->max_stack];
+  _Jv_word *sp = stack;
 
-  /* These three are temporaries for common code used by several
-     instructions.  */
-  void (*fun)();
-  _Jv_ResolvedMethod* rmeth;
-  int tmpval;
+  _Jv_word locals[meth->max_locals];
 
 #define INSN_LABEL(op) &&insn_##op
 
@@ -977,7 +1031,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
     INSN_LABEL(ifnonnull),
     INSN_LABEL(goto_w),
     INSN_LABEL(jsr_w),
+#ifdef DIRECT_THREADED
+    INSN_LABEL (ldc_class)
+#else
     0
+#endif
   };
 
   pc_t pc;
@@ -1000,10 +1058,20 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #define PCVAL(unionval) unionval.p
 #define AMPAMP(label) &&label
 
-  // Compile if we must.
-  if (prepared == NULL)
-    compile (insn_target);
-  pc = (insn_slot *) prepared;
+  // Compile if we must. NOTE: Double-check locking.
+  if (meth->prepared == NULL)
+    {
+      _Jv_MutexLock (&compile_mutex);
+      if (meth->prepared == NULL)
+       meth->compile (insn_target);
+      _Jv_MutexUnlock (&compile_mutex);
+    }
+
+  // If we're only compiling, stop here
+  if (args == NULL)
+    return;
+
+  pc = (insn_slot *) meth->prepared;
 
 #else
 
@@ -1013,8 +1081,16 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #define GET2S() (pc += 2, get2s (pc- 2))
 #define GET1U() get1u (pc++)
 #define GET2U() (pc += 2, get2u (pc - 2))
-#define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
-#define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
+  // Note that these could be more efficient when not handling 'ldc
+  // class'.
+#define AVAL1U()                                               \
+  ({ int index = get1u (pc++);                                 \
+      resolve_pool_entry (meth->defining_class, index).o; })
+#define AVAL2U()                                               \
+  ({ int index = get2u (pc); pc += 2;                          \
+      resolve_pool_entry (meth->defining_class, index).o; })
+  // Note that we don't need to resolve the pool entry here as class
+  // constants are never wide.
 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
 #define SKIP_GOTO pc += 2
 #define GOTO_VAL() pc - 1 + get2s (pc)
@@ -1027,6 +1103,19 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
 #define TAKE_GOTO pc = GOTO_VAL ()
 
+  /* Go straight at it!  the ffi raw format matches the internal
+     stack representation exactly.  At least, that's the idea.
+  */
+  memcpy ((void*) locals, (void*) args, meth->args_raw_size);
+
+  _Jv_word *pool_data = meth->defining_class->constants.data;
+
+  /* These three are temporaries for common code used by several
+     instructions.  */
+  void (*fun)();
+  _Jv_ResolvedMethod* rmeth;
+  int tmpval;
+
   try
     {
       // We keep nop around.  It is used if we're interpreting the
@@ -1052,34 +1141,35 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_invokevirtual:        // 0xb6
       {
+       SAVE_PC();
        int index = GET2U ();
 
-       /* _Jv_ResolvePoolEntry returns immediately if the value already
-        * is resolved.  If we want to clutter up the code here to gain
-        * a little performance, then we can check the corresponding bit
-        * JV_CONSTANT_ResolvedFlag in the tag directly.  For now, I
-        * don't think it is worth it.  */
+       /* _Jv_Linker::resolve_pool_entry returns immediately if the
+        * value already is resolved.  If we want to clutter up the
+        * code here to gain a little performance, then we can check
+        * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
+        * directly.  For now, I don't think it is worth it.  */
 
-       rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
+       rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                  index)).rmethod;
 
        sp -= rmeth->stack_item_count;
-       // We don't use NULLCHECK here because we can't rely on that
-       // working if the method is final.  So instead we do an
-       // explicit test.
-       if (! sp[0].o)
-         throw new java::lang::NullPointerException;
 
-       if (rmeth->vtable_index == -1)
+       if (rmeth->method->accflags & Modifier::FINAL)
          {
-           // final methods do not appear in the vtable,
-           // if it does not appear in the superclass.
+           // We can't rely on NULLCHECK working if the method is final.
+           if (! sp[0].o)
+             throw_null_pointer_exception ();
+
+           // Final methods might not appear in the vtable.
            fun = (void (*)()) rmeth->method->ncode;
          }
        else
          {
+           NULLCHECK (sp[0].o);
            jobject rcv = sp[0].o;
            _Jv_VTable *table = *(_Jv_VTable**) rcv;
-           fun = (void (*)()) table->get_method (rmeth->vtable_index);
+           fun = (void (*)()) table->get_method (rmeth->method->index);
          }
 
 #ifdef DIRECT_THREADED
@@ -1094,25 +1184,24 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #ifdef DIRECT_THREADED
     invokevirtual_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
-       // We don't use NULLCHECK here because we can't rely on that
-       // working if the method is final.  So instead we do an
-       // explicit test.
-       if (! sp[0].o)
-         throw new java::lang::NullPointerException;
 
-       if (rmeth->vtable_index == -1)
+       if (rmeth->method->accflags & Modifier::FINAL)
          {
-           // final methods do not appear in the vtable,
-           // if it does not appear in the superclass.
+           // We can't rely on NULLCHECK working if the method is final.
+           if (! sp[0].o)
+             throw_null_pointer_exception ();
+
+           // Final methods might not appear in the vtable.
            fun = (void (*)()) rmeth->method->ncode;
          }
        else
          {
            jobject rcv = sp[0].o;
            _Jv_VTable *table = *(_Jv_VTable**) rcv;
-           fun = (void (*)()) table->get_method (rmeth->vtable_index);
+           fun = (void (*)()) table->get_method (rmeth->method->index);
          }
       }
       goto perform_invoke;
@@ -1124,7 +1213,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
        ffi_cif *cif = &rmeth->cif;
        ffi_raw *raw = (ffi_raw*) sp;
 
-       jdouble rvalue;
+       _Jv_value rvalue;
 
 #if FFI_NATIVE_RAW_API
        /* We assume that this is only implemented if it's correct      */
@@ -1140,11 +1229,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
         * so those are checked before the switch */
        if (rtype == FFI_TYPE_POINTER)
          {
-           PUSHA (*(jobject*)&rvalue);
+           PUSHA (rvalue.object_value);
          }
        else if (rtype == FFI_TYPE_SINT32)
          {
-           PUSHI (*(jint*)&rvalue);
+           PUSHI (rvalue.int_value);
          }
        else if (rtype == FFI_TYPE_VOID)
          {
@@ -1155,36 +1244,27 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
            switch (rtype)
              {
              case FFI_TYPE_SINT8:
-               {
-                 jbyte value = (*(jint*)&rvalue) & 0xff;
-                 PUSHI (value);
-               }
+               PUSHI ((jbyte)(rvalue.int_value & 0xff));
                break;
 
              case FFI_TYPE_SINT16:
-               {
-                 jshort value = (*(jint*)&rvalue) & 0xffff;
-                 PUSHI (value);
-               }
+               PUSHI ((jshort)(rvalue.int_value & 0xffff));
                break;
 
              case FFI_TYPE_UINT16:
-               {
-                 jint value = (*(jint*)&rvalue) & 0xffff;
-                 PUSHI (value);
-               }
+               PUSHI (rvalue.int_value & 0xffff);
                break;
 
              case FFI_TYPE_FLOAT:
-               PUSHF (*(jfloat*)&rvalue);
+               PUSHF (rvalue.float_value);
                break;
 
              case FFI_TYPE_DOUBLE:
-               PUSHD (rvalue);
+               PUSHD (rvalue.double_value);
                break;
 
              case FFI_TYPE_SINT64:
-               PUSHL (*(jlong*)&rvalue);
+               PUSHL (rvalue.long_value);
                break;
 
              default:
@@ -1274,6 +1354,20 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
       PUSHA ((jobject) AVAL2U ());
       NEXT_INSN;
 
+#ifdef DIRECT_THREADED
+      // For direct threaded we have a separate 'ldc class' operation.
+    insn_ldc_class:
+      {
+       SAVE_PC();
+       // We could rewrite the instruction at this point.
+       int index = INTVAL ();
+       jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                    index)).o;
+       PUSHA (k);
+      }
+      NEXT_INSN;
+#endif /* DIRECT_THREADED */
+
     insn_ldc2_w:
       {
        void *where = AVAL2UP ();
@@ -1733,6 +1827,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_idiv:
       {
+       SAVE_PC();
        jint value2 = POPI();
        jint value1 = POPI();
        jint res = _Jv_divI (value1, value2);
@@ -1742,6 +1837,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_ldiv:
       {
+       SAVE_PC();
        jlong value2 = POPL();
        jlong value1 = POPL();
        jlong res = _Jv_divJ (value1, value2);
@@ -1751,6 +1847,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_fdiv:
       {
+       SAVE_PC();
        jfloat value2 = POPF();
        jfloat value1 = POPF();
        jfloat res = value1 / value2;
@@ -1769,6 +1866,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_irem:
       {
+       SAVE_PC();
        jint value2 = POPI();
        jint value1 =  POPI();
        jint res = _Jv_remI (value1, value2);
@@ -1778,6 +1876,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_lrem:
       {
+       SAVE_PC();
        jlong value2 = POPL();
        jlong value1 = POPL();
        jlong res = _Jv_remJ (value1, value2);
@@ -1866,7 +1965,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
     insn_iushr:
       {
        jint shift = (POPI() & 0x1f);
-       unsigned long value = POPI();
+       _Jv_uint value = (_Jv_uint) POPI();
        PUSHI ((jint) (value >> shift));
       }
       NEXT_INSN;
@@ -1874,8 +1973,8 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
     insn_lushr:
       {
        jint shift = (POPI() & 0x3f);
-       UINT64 value = (UINT64) POPL();
-       PUSHL ((value >> shift));
+       _Jv_ulong value = (_Jv_ulong) POPL();
+       PUSHL ((jlong) (value >> shift));
       }
       NEXT_INSN;
 
@@ -2023,11 +2122,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
       NEXT_INSN;
 
     insn_dcmpl:
-      tmpval = 1;
+      tmpval = -1;
       goto dcmp;
 
     insn_dcmpg:
-      tmpval = -1;
+      tmpval = 1;
 
     dcmp:
       {
@@ -2359,7 +2458,8 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
     insn_getstatic:
       {
        jint fieldref_index = GET2U ();
-       _Jv_ResolvePoolEntry (defining_class, fieldref_index);
+        SAVE_PC(); // Constant pool resolution could throw.
+       _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
 
        if ((field->flags & Modifier::STATIC) == 0)
@@ -2376,37 +2476,37 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
            switch (type->size_in_bytes)
              {
              case 1:
-               PUSHI (*(jbyte*) (field->u.addr));
+               PUSHI (*field->u.byte_addr);
                newinsn = AMPAMP (getstatic_resolved_1);
                break;
 
              case 2:
                if (type == JvPrimClass (char))
                  {
-                   PUSHI(*(jchar*) (field->u.addr));
+                   PUSHI (*field->u.char_addr);
                    newinsn = AMPAMP (getstatic_resolved_char);
                  }
                else
                  {
-                   PUSHI(*(jshort*) (field->u.addr));
+                   PUSHI (*field->u.short_addr);
                    newinsn = AMPAMP (getstatic_resolved_short);
                  }
                break;
 
              case 4:
-               PUSHI(*(jint*) (field->u.addr));
+               PUSHI(*field->u.int_addr);
                newinsn = AMPAMP (getstatic_resolved_4);
                break;
 
              case 8:
-               PUSHL(*(jlong*) (field->u.addr));
+               PUSHL(*field->u.long_addr);
                newinsn = AMPAMP (getstatic_resolved_8);
                break;
              }
          }
        else
          {
-           PUSHA(*(jobject*) (field->u.addr));
+           PUSHA(*field->u.object_addr);
            newinsn = AMPAMP (getstatic_resolved_obj);
          }
 
@@ -2445,8 +2545,9 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_getfield:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
-       _Jv_ResolvePoolEntry (defining_class, fieldref_index);
+       _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
 
        if ((field->flags & Modifier::STATIC) != 0)
@@ -2455,49 +2556,48 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
        jclass type = field->type;
        jint field_offset = field->u.boffset;
-       if (field_offset > 0xffff)
-         throw new java::lang::VirtualMachineError;
 
        jobject obj   = POPA();
        NULLCHECK(obj);
 
        void *newinsn = NULL;
+       _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
        if (type->isPrimitive ())
          {
            switch (type->size_in_bytes)
              {
              case 1:
-               PUSHI (*(jbyte*) ((char*)obj + field_offset));
+               PUSHI (val->byte_value);
                newinsn = AMPAMP (getfield_resolved_1);
                break;
 
              case 2:
                if (type == JvPrimClass (char))
                  {
-                   PUSHI (*(jchar*) ((char*)obj + field_offset));
+                   PUSHI (val->char_value);
                    newinsn = AMPAMP (getfield_resolved_char);
                  }
                else
                  {
-                   PUSHI (*(jshort*) ((char*)obj + field_offset));
+                   PUSHI (val->short_value);
                    newinsn = AMPAMP (getfield_resolved_short);
                  }
                break;
 
              case 4:
-               PUSHI (*(jint*) ((char*)obj + field_offset));
+               PUSHI (val->int_value);
                newinsn = AMPAMP (getfield_resolved_4);
                break;
 
              case 8:
-               PUSHL(*(jlong*) ((char*)obj + field_offset));
+               PUSHL (val->long_value);
                newinsn = AMPAMP (getfield_resolved_8);
                break;
              }
          }
        else
          {
-           PUSHA(*(jobject*) ((char*)obj + field_offset));
+           PUSHA (val->object_value);
            newinsn = AMPAMP (getfield_resolved_obj);
          }
 
@@ -2560,8 +2660,9 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_putstatic:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
-       _Jv_ResolvePoolEntry (defining_class, fieldref_index);
+       _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
 
        jclass type = field->type;
@@ -2579,7 +2680,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
              case 1:
                {
                  jint value = POPI();
-                 *(jbyte*) (field->u.addr) = value;
+                 *field->u.byte_addr = value;
                  newinsn = AMPAMP (putstatic_resolved_1);
                  break;
                }
@@ -2587,7 +2688,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
              case 2:
                {
                  jint value = POPI();
-                 *(jchar*) (field->u.addr) = value;
+                 *field->u.char_addr = value;
                  newinsn = AMPAMP (putstatic_resolved_2);
                  break;
                }
@@ -2595,7 +2696,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
              case 4:
                {
                  jint value = POPI();
-                 *(jint*) (field->u.addr) = value;
+                 *field->u.int_addr = value;
                  newinsn = AMPAMP (putstatic_resolved_4);
                  break;
                }
@@ -2603,7 +2704,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
              case 8:
                {
                  jlong value = POPL();
-                 *(jlong*) (field->u.addr) = value;
+                 *field->u.long_addr = value;
                  newinsn = AMPAMP (putstatic_resolved_8);
                  break;
                }
@@ -2612,7 +2713,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
        else
          {
            jobject value = POPA();
-           *(jobject*) (field->u.addr) = value;
+           *field->u.object_addr = value;
            newinsn = AMPAMP (putstatic_resolved_obj);
          }
 
@@ -2647,8 +2748,9 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_putfield:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
-       _Jv_ResolvePoolEntry (defining_class, fieldref_index);
+       _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
 
        jclass type = field->type;
@@ -2658,8 +2760,6 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
            (JvNewStringLatin1 ("field is static"));
 
        jint field_offset = field->u.boffset;
-       if (field_offset > 0xffff)
-         throw new java::lang::VirtualMachineError;
 
        void *newinsn = NULL;
        if (type->isPrimitive ())
@@ -2772,13 +2872,21 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_invokespecial:
       {
+       SAVE_PC();
        int index = GET2U ();
 
-       rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
+       rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                  index)).rmethod;
 
        sp -= rmeth->stack_item_count;
 
-       NULLCHECK (sp[0].o);
+       // We don't use NULLCHECK here because we can't rely on that
+       // working for <init>.  So instead we do an explicit test.
+       if (! sp[0].o)
+         {
+           SAVE_PC();
+           throw_null_pointer_exception ();
+         }
 
        fun = (void (*)()) rmeth->method->ncode;
 
@@ -2794,9 +2902,15 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #ifdef DIRECT_THREADED
     invokespecial_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
-       NULLCHECK (sp[0].o);
+       // We don't use NULLCHECK here because we can't rely on that
+       // working for <init>.  So instead we do an explicit test.
+       if (! sp[0].o)
+         {
+           throw_null_pointer_exception ();
+         }
        fun = (void (*)()) rmeth->method->ncode;
       }
       goto perform_invoke;
@@ -2804,13 +2918,14 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_invokestatic:
       {
+       SAVE_PC();
        int index = GET2U ();
 
-       rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
+       rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                  index)).rmethod;
 
        sp -= rmeth->stack_item_count;
 
-       _Jv_InitClass (rmeth->klass);
        fun = (void (*)()) rmeth->method->ncode;
 
 #ifdef DIRECT_THREADED
@@ -2825,6 +2940,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #ifdef DIRECT_THREADED
     invokestatic_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
        fun = (void (*)()) rmeth->method->ncode;
@@ -2834,9 +2950,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_invokeinterface:
       {
+       SAVE_PC();
        int index = GET2U ();
 
-       rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
+       rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                  index)).rmethod;
 
        sp -= rmeth->stack_item_count;
 
@@ -2864,6 +2982,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #ifdef DIRECT_THREADED
     invokeinterface_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
        jobject rcv = sp[0].o;
@@ -2878,10 +2997,15 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_new:
       {
+       SAVE_PC();
        int index = GET2U ();
-       jclass klass = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
-       _Jv_InitClass (klass);
-       jobject res = _Jv_AllocObject (klass, klass->size_in_bytes);
+       jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                         index)).clazz;
+       /* VM spec, section 3.11.5 */
+       if ((klass->getModifiers() & Modifier::ABSTRACT)
+           || klass->isInterface())
+         throw new java::lang::InstantiationException;
+       jobject res = _Jv_AllocObject (klass);
        PUSHA (res);
 
 #ifdef DIRECT_THREADED
@@ -2895,7 +3019,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
     new_resolved:
       {
        jclass klass = (jclass) AVAL ();
-       jobject res = _Jv_AllocObject (klass, klass->size_in_bytes);
+       jobject res = _Jv_AllocObject (klass);
        PUSHA (res);
       }
       NEXT_INSN;
@@ -2912,10 +3036,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_anewarray:
       {
+       SAVE_PC();
        int index = GET2U ();
-       jclass klass = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
+       jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                         index)).clazz;
        int size  = POPI();
-       _Jv_InitClass (klass);
        jobject result = _Jv_NewObjectArray (size, klass, 0);
        PUSHA (result);
 
@@ -2954,12 +3079,13 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_checkcast:
       {
+        SAVE_PC();
        jobject value = POPA();
        jint index = GET2U ();
-       jclass to = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
+       jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                      index)).clazz;
 
-       if (value != NULL && ! to->isInstance (value))
-         throw new java::lang::ClassCastException (to->getName());
+       value = (jobject) _Jv_CheckCast (to, value);
 
        PUSHA (value);
 
@@ -2973,10 +3099,10 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #ifdef DIRECT_THREADED
     checkcast_resolved:
       {
+        SAVE_PC();
        jobject value = POPA ();
        jclass to = (jclass) AVAL ();
-       if (value != NULL && ! to->isInstance (value))
-         throw new java::lang::ClassCastException (to->getName());
+       value = (jobject) _Jv_CheckCast (to, value);
        PUSHA (value);
       }
       NEXT_INSN;
@@ -2984,9 +3110,11 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_instanceof:
       {
+        SAVE_PC();
        jobject value = POPA();
        jint index = GET2U ();
-       jclass to = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
+       jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                                      index)).clazz;
        PUSHI (to->isInstance (value));
 
 #ifdef DIRECT_THREADED
@@ -3044,12 +3172,13 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 
     insn_multianewarray:
       {
+       SAVE_PC();
        int kind_index = GET2U ();
        int dim        = GET1U ();
 
        jclass type    
-         = (_Jv_ResolvePoolEntry (defining_class, kind_index)).clazz;
-       _Jv_InitClass (type);
+         = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
+                                              kind_index)).clazz;
        jint *sizes    = (jint*) __builtin_alloca (sizeof (jint)*dim);
 
        for (int i = dim - 1; i >= 0; i--)
@@ -3095,6 +3224,10 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
            LOADI (wide);
            NEXT_INSN;
 
+         case op_fload:
+           LOADF (wide);
+           NEXT_INSN;
+
          case op_aload:
            LOADA (wide);
            NEXT_INSN;
@@ -3133,10 +3266,10 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #else
       int logical_pc = pc - 1 - bytecode ();
 #endif
-      _Jv_InterpException *exc = exceptions ();
+      _Jv_InterpException *exc = meth->exceptions ();
       jclass exc_class = ex->getClass ();
 
-      for (int i = 0; i < exc_count; i++)
+      for (int i = 0; i < meth->exc_count; i++)
        {
          if (PCVAL (exc[i].start_pc) <= logical_pc
              && logical_pc < PCVAL (exc[i].end_pc))
@@ -3146,8 +3279,8 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 #else
              jclass handler = NULL;
              if (exc[i].handler_type.i != 0)
-               handler = (_Jv_ResolvePoolEntry (defining_class,
-                                                exc[i].handler_type.i)).clazz;
+               handler = (_Jv_Linker::resolve_pool_entry (defining_class,
+                                                            exc[i].handler_type.i)).clazz;
 #endif /* DIRECT_THREADED */
 
              if (handler == NULL || handler->isAssignableFrom (exc_class))
@@ -3170,7 +3303,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args)
 }
 
 static void
-throw_internal_error (char *msg)
+throw_internal_error (const char *msg)
 {
   throw new java::lang::InternalError (JvNewStringLatin1 (msg));
 }
@@ -3181,16 +3314,664 @@ throw_incompatible_class_change_error (jstring msg)
   throw new java::lang::IncompatibleClassChangeError (msg);
 }
 
-#ifndef HANDLE_SEGV
-static java::lang::NullPointerException *null_pointer_exc;
 static void 
 throw_null_pointer_exception ()
 {
-  if (null_pointer_exc == NULL)
-    null_pointer_exc = new java::lang::NullPointerException;
+  throw new java::lang::NullPointerException;
+}
+
+/* Look up source code line number for given bytecode (or direct threaded
+   interpreter) PC. */
+int
+_Jv_InterpMethod::get_source_line(pc_t mpc)
+{
+  int line = line_table_len > 0 ? line_table[0].line : -1;
+  for (int i = 1; i < line_table_len; i++)
+    if (line_table[i].pc > mpc)
+      break;
+    else
+      line = line_table[i].line;
+
+  return line;
+}
+
+/** Do static initialization for fields with a constant initializer */
+void
+_Jv_InitField (jobject obj, jclass klass, int index)
+{
+  using namespace java::lang::reflect;
+
+  if (obj != 0 && klass == 0)
+    klass = obj->getClass ();
+
+  if (!_Jv_IsInterpretedClass (klass))
+    return;
+
+  _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
+
+  _Jv_Field * field = (&klass->fields[0]) + index;
+
+  if (index > klass->field_count)
+    throw_internal_error ("field out of range");
+
+  int init = iclass->field_initializers[index];
+  if (init == 0)
+    return;
+
+  _Jv_Constants *pool = &klass->constants;
+  int tag = pool->tags[init];
+
+  if (! field->isResolved ())
+    throw_internal_error ("initializing unresolved field");
+
+  if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
+    throw_internal_error ("initializing non-static field with no object");
+
+  void *addr = 0;
+
+  if ((field->flags & Modifier::STATIC) != 0)
+    addr = (void*) field->u.addr;
+  else
+    addr = (void*) (((char*)obj) + field->u.boffset);
+
+  switch (tag)
+    {
+    case JV_CONSTANT_String:
+      {
+       jstring str;
+       str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
+       pool->data[init].string = str;
+       pool->tags[init] = JV_CONSTANT_ResolvedString;
+      }
+      /* fall through */
+
+    case JV_CONSTANT_ResolvedString:
+      if (! (field->type == &java::lang::String::class$
+            || field->type == &java::lang::Class::class$))
+       throw_class_format_error ("string initialiser to non-string field");
+
+      *(jstring*)addr = pool->data[init].string;
+      break;
+
+    case JV_CONSTANT_Integer:
+      {
+       int value = pool->data[init].i;
+
+       if (field->type == JvPrimClass (boolean))
+         *(jboolean*)addr = (jboolean)value;
+       
+       else if (field->type == JvPrimClass (byte))
+         *(jbyte*)addr = (jbyte)value;
+       
+       else if (field->type == JvPrimClass (char))
+         *(jchar*)addr = (jchar)value;
+
+       else if (field->type == JvPrimClass (short))
+         *(jshort*)addr = (jshort)value;
+       
+       else if (field->type == JvPrimClass (int))
+         *(jint*)addr = (jint)value;
+
+       else
+         throw_class_format_error ("erroneous field initializer");
+      }  
+      break;
+
+    case JV_CONSTANT_Long:
+      if (field->type != JvPrimClass (long))
+       throw_class_format_error ("erroneous field initializer");
+
+      *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
+      break;
+
+    case JV_CONSTANT_Float:
+      if (field->type != JvPrimClass (float))
+       throw_class_format_error ("erroneous field initializer");
+
+      *(jfloat*)addr = pool->data[init].f;
+      break;
+
+    case JV_CONSTANT_Double:
+      if (field->type != JvPrimClass (double))
+       throw_class_format_error ("erroneous field initializer");
+
+      *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
+      break;
+
+    default:
+      throw_class_format_error ("erroneous field initializer");
+    }
+}
+
+inline static unsigned char*
+skip_one_type (unsigned char* ptr)
+{
+  int ch = *ptr++;
+
+  while (ch == '[')
+    { 
+      ch = *ptr++;
+    }
+  
+  if (ch == 'L')
+    {
+      do { ch = *ptr++; } while (ch != ';');
+    }
+
+  return ptr;
+}
+
+static ffi_type*
+get_ffi_type_from_signature (unsigned char* ptr)
+{
+  switch (*ptr) 
+    {
+    case 'L':
+    case '[':
+      return &ffi_type_pointer;
+      break;
+
+    case 'Z':
+      // On some platforms a bool is a byte, on others an int.
+      if (sizeof (jboolean) == sizeof (jbyte))
+       return &ffi_type_sint8;
+      else
+       {
+         JvAssert (sizeof (jbyte) == sizeof (jint));
+         return &ffi_type_sint32;
+       }
+      break;
+
+    case 'B':
+      return &ffi_type_sint8;
+      break;
+      
+    case 'C':
+      return &ffi_type_uint16;
+      break;
+         
+    case 'S': 
+      return &ffi_type_sint16;
+      break;
+         
+    case 'I':
+      return &ffi_type_sint32;
+      break;
+         
+    case 'J':
+      return &ffi_type_sint64;
+      break;
+         
+    case 'F':
+      return &ffi_type_float;
+      break;
+         
+    case 'D':
+      return &ffi_type_double;
+      break;
+
+    case 'V':
+      return &ffi_type_void;
+      break;
+    }
+
+  throw_internal_error ("unknown type in signature");
+}
+
+/* this function yields the number of actual arguments, that is, if the
+ * function is non-static, then one is added to the number of elements
+ * found in the signature */
+
+int 
+_Jv_count_arguments (_Jv_Utf8Const *signature,
+                    jboolean staticp)
+{
+  unsigned char *ptr = (unsigned char*) signature->chars();
+  int arg_count = staticp ? 0 : 1;
+
+  /* first, count number of arguments */
+
+  // skip '('
+  ptr++;
+
+  // count args
+  while (*ptr != ')')
+    {
+      ptr = skip_one_type (ptr);
+      arg_count += 1;
+    }
+
+  return arg_count;
+}
+
+/* This beast will build a cif, given the signature.  Memory for
+ * the cif itself and for the argument types must be allocated by the
+ * caller.
+ */
+
+static int 
+init_cif (_Jv_Utf8Const* signature,
+         int arg_count,
+         jboolean staticp,
+         ffi_cif *cif,
+         ffi_type **arg_types,
+         ffi_type **rtype_p)
+{
+  unsigned char *ptr = (unsigned char*) signature->chars();
+
+  int arg_index = 0;           // arg number
+  int item_count = 0;          // stack-item count
+
+  // setup receiver
+  if (!staticp)
+    {
+      arg_types[arg_index++] = &ffi_type_pointer;
+      item_count += 1;
+    }
+
+  // skip '('
+  ptr++;
+
+  // assign arg types
+  while (*ptr != ')')
+    {
+      arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
+
+      if (*ptr == 'J' || *ptr == 'D')
+       item_count += 2;
+      else
+       item_count += 1;
+
+      ptr = skip_one_type (ptr);
+    }
+
+  // skip ')'
+  ptr++;
+  ffi_type *rtype = get_ffi_type_from_signature (ptr);
 
-  throw null_pointer_exc;
+  ptr = skip_one_type (ptr);
+  if (ptr != (unsigned char*)signature->chars() + signature->len())
+    throw_internal_error ("did not find end of signature");
+
+  if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
+                   arg_count, rtype, arg_types) != FFI_OK)
+    throw_internal_error ("ffi_prep_cif failed");
+
+  if (rtype_p != NULL)
+    *rtype_p = rtype;
+
+  return item_count;
 }
+
+#if FFI_NATIVE_RAW_API
+#   define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
+#   define FFI_RAW_SIZE ffi_raw_size
+#else
+#   define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
+#   define FFI_RAW_SIZE ffi_java_raw_size
 #endif
 
+/* we put this one here, and not in interpret.cc because it
+ * calls the utility routines _Jv_count_arguments 
+ * which are static to this module.  The following struct defines the
+ * layout we use for the stubs, it's only used in the ncode method. */
+
+typedef struct {
+  ffi_raw_closure  closure;
+  ffi_cif   cif;
+  ffi_type *arg_types[0];
+} ncode_closure;
+
+typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
+
+void *
+_Jv_InterpMethod::ncode ()
+{
+  using namespace java::lang::reflect;
+
+  if (self->ncode != 0)
+    return self->ncode;
+
+  jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
+  int arg_count = _Jv_count_arguments (self->signature, staticp);
+
+  ncode_closure *closure =
+    (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
+                                       + arg_count * sizeof (ffi_type*));
+
+  init_cif (self->signature,
+           arg_count,
+           staticp,
+           &closure->cif,
+           &closure->arg_types[0],
+           NULL);
+
+  ffi_closure_fun fun;
+
+  args_raw_size = FFI_RAW_SIZE (&closure->cif);
+
+  JvAssert ((self->accflags & Modifier::NATIVE) == 0);
+
+  if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
+    {
+      if (staticp)
+       fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
+      else
+       fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object; 
+    }
+  else
+    {
+      if (staticp)
+       fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
+      else
+       fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
+    }
+
+  FFI_PREP_RAW_CLOSURE (&closure->closure,
+                       &closure->cif, 
+                       fun,
+                       (void*)this);
+
+  self->ncode = (void*)closure;
+  return self->ncode;
+}
+
+#ifdef DIRECT_THREADED
+/* Find the index of the given insn in the array of insn slots
+   for this method. Returns -1 if not found. */
+jlong
+_Jv_InterpMethod::insn_index (pc_t pc)
+{
+  jlong left = 0;
+  jlong right = number_insn_slots;
+  insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
+
+  while (right >= 0)
+    {
+      jlong mid = (left + right) / 2;
+      if (&slots[mid] == pc)
+       return mid;
+
+      if (pc < &slots[mid])
+       right = mid - 1;
+      else
+        left = mid + 1;
+    }
+
+  return -1;
+}
+#endif // DIRECT_THREADED
+
+void
+_Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
+                                 jintArray& line_numbers,
+                                 jlongArray& code_indices)
+{
+#ifdef DIRECT_THREADED
+  /* For the DIRECT_THREADED case, if the method has not yet been
+   * compiled, the linetable will change to insn slots instead of
+   * bytecode PCs. It is probably easiest, in this case, to simply
+   * compile the method and guarantee that we are using insn
+   * slots.
+   */
+  _Jv_CompileMethod (this);
+
+  if (line_table_len > 0)
+    {
+      start = 0;
+      end = number_insn_slots;
+      line_numbers = JvNewIntArray (line_table_len);
+      code_indices = JvNewLongArray (line_table_len);
+
+      jint* lines = elements (line_numbers);
+      jlong* indices = elements (code_indices);
+      for (int i = 0; i < line_table_len; ++i)
+       {
+         lines[i] = line_table[i].line;
+         indices[i] = insn_index (line_table[i].pc);
+       }
+    }
+#else // !DIRECT_THREADED
+  if (line_table_len > 0)
+    {
+      start = 0;
+      end = code_length;
+      line_numbers = JvNewIntArray (line_table_len);
+      code_indices = JvNewLongArray (line_table_len);
+
+      jint* lines = elements (line_numbers);
+      jlong* indices = elements (code_indices);
+      for (int i = 0; i < line_table_len; ++i)
+       {
+         lines[i] = line_table[i].line;
+         indices[i] = (jlong) line_table[i].bytecode_pc;
+       }
+    }
+#endif // !DIRECT_THREADED
+}
+
+void *
+_Jv_JNIMethod::ncode ()
+{
+  using namespace java::lang::reflect;
+
+  if (self->ncode != 0)
+    return self->ncode;
+
+  jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
+  int arg_count = _Jv_count_arguments (self->signature, staticp);
+
+  ncode_closure *closure =
+    (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
+                                   + arg_count * sizeof (ffi_type*));
+
+  ffi_type *rtype;
+  init_cif (self->signature,
+           arg_count,
+           staticp,
+           &closure->cif,
+           &closure->arg_types[0],
+           &rtype);
+
+  ffi_closure_fun fun;
+
+  args_raw_size = FFI_RAW_SIZE (&closure->cif);
+
+  // Initialize the argument types and CIF that represent the actual
+  // underlying JNI function.
+  int extra_args = 1;
+  if ((self->accflags & Modifier::STATIC))
+    ++extra_args;
+  jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
+                                               * sizeof (ffi_type *));
+  int offset = 0;
+  jni_arg_types[offset++] = &ffi_type_pointer;
+  if ((self->accflags & Modifier::STATIC))
+    jni_arg_types[offset++] = &ffi_type_pointer;
+  memcpy (&jni_arg_types[offset], &closure->arg_types[0],
+         arg_count * sizeof (ffi_type *));
+
+  if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
+                   extra_args + arg_count, rtype,
+                   jni_arg_types) != FFI_OK)
+    throw_internal_error ("ffi_prep_cif failed for JNI function");
+
+  JvAssert ((self->accflags & Modifier::NATIVE) != 0);
+
+  // FIXME: for now we assume that all native methods for
+  // interpreted code use JNI.
+  fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
+
+  FFI_PREP_RAW_CLOSURE (&closure->closure,
+                       &closure->cif, 
+                       fun,
+                       (void*) this);
+
+  self->ncode = (void *) closure;
+  return self->ncode;
+}
+
+static void
+throw_class_format_error (jstring msg)
+{
+  throw (msg
+        ? new java::lang::ClassFormatError (msg)
+        : new java::lang::ClassFormatError);
+}
+
+static void
+throw_class_format_error (const char *msg)
+{
+  throw_class_format_error (JvNewStringLatin1 (msg));
+}
+
+\f
+
+void
+_Jv_InterpreterEngine::do_verify (jclass klass)
+{
+  _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+  for (int i = 0; i < klass->method_count; i++)
+    {
+      using namespace java::lang::reflect;
+      _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
+      _Jv_ushort accflags = klass->methods[i].accflags;
+      if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
+       {
+         _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
+         _Jv_VerifyMethod (im);
+       }
+    }
+}
+
+void
+_Jv_InterpreterEngine::do_create_ncode (jclass klass)
+{
+  _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+  for (int i = 0; i < klass->method_count; i++)
+    {
+      // Just skip abstract methods.  This is particularly important
+      // because we don't resize the interpreted_methods array when
+      // miranda methods are added to it.
+      if ((klass->methods[i].accflags
+          & java::lang::reflect::Modifier::ABSTRACT)
+         != 0)
+       continue;
+
+      _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
+
+      if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
+         != 0)
+       {
+         // You might think we could use a virtual `ncode' method in
+         // the _Jv_MethodBase and unify the native and non-native
+         // cases.  Well, we can't, because we don't allocate these
+         // objects using `new', and thus they don't get a vtable.
+         _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
+         klass->methods[i].ncode = jnim->ncode ();
+       }
+      else if (imeth != 0)             // it could be abstract
+       {
+         _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
+         klass->methods[i].ncode = im->ncode ();
+       }
+    }
+}
+
+void
+_Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
+                                                 int pointer_size,
+                                                 int other_size)
+{
+  _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+
+  // Splitting the allocations here lets us scan reference fields and
+  // avoid scanning non-reference fields.  How reference fields are
+  // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
+  // means that this memory will be scanned conservatively (same
+  // difference, since we know all the contents here are pointers).
+  // Then we put pointers into this memory into the 'fields'
+  // structure.  Most of these are interior pointers, which is ok (but
+  // even so the pointer to the first reference field will be used and
+  // that is not an interior pointer).  The 'fields' array is also
+  // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
+  // be scanned.  A pointer to this array is held by Class and thus
+  // seen by the collector.
+  char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
+  char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
+
+  for (int i = 0; i < klass->field_count; i++)
+    {
+      _Jv_Field *field = &klass->fields[i];
+
+      if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
+       continue;
+
+      char *base = field->isRef() ? reference_fields : non_reference_fields;
+      field->u.addr  = base + field->u.boffset;
+
+      if (iclass->field_initializers[i] != 0)
+       {
+         _Jv_Linker::resolve_field (field, klass->loader);
+         _Jv_InitField (0, klass, i);
+       }
+    }
+
+  // Now we don't need the field_initializers anymore, so let the
+  // collector get rid of it.
+  iclass->field_initializers = 0;
+}
+
+_Jv_ResolvedMethod *
+_Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
+                                         jboolean staticp)
+{
+  int arg_count = _Jv_count_arguments (method->signature, staticp);
+
+  _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
+    _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
+                   + arg_count*sizeof (ffi_type*));
+
+  result->stack_item_count
+    = init_cif (method->signature,
+               arg_count,
+               staticp,
+               &result->cif,
+               &result->arg_types[0],
+               NULL);
+
+  result->method              = method;
+  result->klass               = klass;
+
+  return result;
+}
+
+void
+_Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
+{
+  _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+  for (int i = 0; i < klass->method_count; i++)
+    {
+      // Just skip abstract methods.  This is particularly important
+      // because we don't resize the interpreted_methods array when
+      // miranda methods are added to it.
+      if ((klass->methods[i].accflags
+          & java::lang::reflect::Modifier::ABSTRACT)
+         != 0)
+       continue;
+      // Miranda method additions mean that the `methods' array moves.
+      // We cache a pointer into this array, so we have to update.
+      iclass->interpreted_methods[i]->self = &klass->methods[i];
+    }
+}
+
+#ifdef DIRECT_THREADED
+void
+_Jv_CompileMethod (_Jv_InterpMethod* method)
+{
+  if (method->prepared == NULL)
+    _Jv_InterpMethod::run (NULL, NULL, method);
+}
+#endif // DIRECT_THREADED
+
 #endif // INTERPRETER