OSDN Git Service

PR libgcj/29594:
[pf3gnuchains/gcc-fork.git] / libjava / interpret.cc
1 // interpret.cc - Code for the interpreter
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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/InternalError.h>
29 #include <java/lang/NullPointerException.h>
30 #include <java/lang/ArithmeticException.h>
31 #include <java/lang/IncompatibleClassChangeError.h>
32 #include <java/lang/InstantiationException.h>
33 #include <java/lang/Thread.h>
34 #include <java-insns.h>
35 #include <java-signal.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <execution.h>
38 #include <java/lang/reflect/Modifier.h>
39
40 #include <jvmti.h>
41 #include "jvmti-int.h"
42
43 #include <gnu/classpath/jdwp/Jdwp.h>
44 #include <gnu/gcj/jvmti/Breakpoint.h>
45 #include <gnu/gcj/jvmti/BreakpointManager.h>
46
47 #ifdef INTERPRETER
48
49 // Execution engine for interpreted code.
50 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
51
52 #include <stdlib.h>
53
54 using namespace gcj;
55
56 static void throw_internal_error (const char *msg)
57   __attribute__ ((__noreturn__));
58 static void throw_incompatible_class_change_error (jstring msg)
59   __attribute__ ((__noreturn__));
60 static void throw_null_pointer_exception ()
61   __attribute__ ((__noreturn__));
62
63 static void throw_class_format_error (jstring msg)
64         __attribute__ ((__noreturn__));
65 static void throw_class_format_error (const char *msg)
66         __attribute__ ((__noreturn__));
67
68 #ifdef DIRECT_THREADED
69 // Lock to ensure that methods are not compiled concurrently.
70 // We could use a finer-grained lock here, however it is not safe to use
71 // the Class monitor as user code in another thread could hold it.
72 static _Jv_Mutex_t compile_mutex;
73
74 void
75 _Jv_InitInterpreter()
76 {
77   _Jv_MutexInit (&compile_mutex);
78 }
79 #else
80 void _Jv_InitInterpreter() {}
81 #endif
82
83 // The breakpoint instruction. For the direct threaded case,
84 // _Jv_InterpMethod::compile will initialize breakpoint_insn
85 // the first time it is called.
86 #ifdef DIRECT_THREADED
87 insn_slot _Jv_InterpMethod::bp_insn_slot;
88 pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
89 #else
90 unsigned char _Jv_InterpMethod::bp_insn_opcode
91   = static_cast<unsigned char> (op_breakpoint);
92 pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
93 #endif
94
95 extern "C" double __ieee754_fmod (double,double);
96
97 static inline void dupx (_Jv_word *sp, int n, int x)
98 {
99   // first "slide" n+x elements n to the right
100   int top = n-1;
101   for (int i = 0; i < n+x; i++)
102     {
103       sp[(top-i)] = sp[(top-i)-n];
104     }
105   
106   // next, copy the n top elements, n+x down
107   for (int i = 0; i < n; i++)
108     {
109       sp[top-(n+x)-i] = sp[top-i];
110     }
111 }
112
113 // Used to convert from floating types to integral types.
114 template<typename TO, typename FROM>
115 static inline TO
116 convert (FROM val, TO min, TO max)
117 {
118   TO ret;
119   if (val >= (FROM) max)
120     ret = max;
121   else if (val <= (FROM) min)
122     ret = min;
123   else if (val != val)
124     ret = 0;
125   else
126     ret = (TO) val;
127   return ret;
128 }
129
130 #define PUSHA(V)  (sp++)->o = (V)
131 #define PUSHI(V)  (sp++)->i = (V)
132 #define PUSHF(V)  (sp++)->f = (V)
133 #if SIZEOF_VOID_P == 8
134 # define PUSHL(V)   (sp->l = (V), sp += 2)
135 # define PUSHD(V)   (sp->d = (V), sp += 2)
136 #else
137 # define PUSHL(V)  do { _Jv_word2 w2; w2.l=(V); \
138                         (sp++)->ia[0] = w2.ia[0]; \
139                         (sp++)->ia[0] = w2.ia[1]; } while (0)
140 # define PUSHD(V)  do { _Jv_word2 w2; w2.d=(V); \
141                         (sp++)->ia[0] = w2.ia[0]; \
142                         (sp++)->ia[0] = w2.ia[1]; } while (0)
143 #endif
144
145 #define POPA()    ((--sp)->o)
146 #define POPI()    ((jint) (--sp)->i) // cast since it may be promoted
147 #define POPF()    ((jfloat) (--sp)->f)
148 #if SIZEOF_VOID_P == 8
149 # define POPL()   (sp -= 2, (jlong) sp->l)
150 # define POPD()   (sp -= 2, (jdouble) sp->d)
151 #else
152 # define POPL()    ({ _Jv_word2 w2; \
153                      w2.ia[1] = (--sp)->ia[0]; \
154                      w2.ia[0] = (--sp)->ia[0]; w2.l; })
155 # define POPD()    ({ _Jv_word2 w2; \
156                      w2.ia[1] = (--sp)->ia[0]; \
157                      w2.ia[0] = (--sp)->ia[0]; w2.d; })
158 #endif
159
160 #define LOADA(I)  (sp++)->o = locals[I].o
161 #define LOADI(I)  (sp++)->i = locals[I].i
162 #define LOADF(I)  (sp++)->f = locals[I].f
163 #if SIZEOF_VOID_P == 8
164 # define LOADL(I)  (sp->l = locals[I].l, sp += 2)
165 # define LOADD(I)  (sp->d = locals[I].d, sp += 2)
166 #else
167 # define LOADL(I)  do { jint __idx = (I); \
168                         (sp++)->ia[0] = locals[__idx].ia[0]; \
169                         (sp++)->ia[0] = locals[__idx+1].ia[0]; \
170                    } while (0)
171 # define LOADD(I)  LOADL(I)
172 #endif
173
174 #define STOREA(I)               \
175 do {                                    \
176 DEBUG_LOCALS_INSN(I, 'o');              \
177 locals[I].o = (--sp)->o;                \
178 } while(0)
179 #define STOREI(I)               \
180 do {                                    \
181 DEBUG_LOCALS_INSN (I, 'i');             \
182 locals[I].i = (--sp)->i;                \
183 } while(0)
184 #define STOREF(I)               \
185 do {                                    \
186 DEBUG_LOCALS_INSN (I, 'f');             \
187 locals[I].f = (--sp)->f;                \
188 } while(0)
189 #if SIZEOF_VOID_P == 8
190 # define STOREL(I)                      \
191 do {                                                    \
192 DEBUG_LOCALS_INSN (I, 'l');                     \
193 (sp -= 2, locals[I].l = sp->l);         \
194 } while(0)
195 # define STORED(I)                              \
196 do {                                                    \
197 DEBUG_LOCALS_INSN (I, 'd');                     \
198 (sp -= 2, locals[I].d = sp->d);         \
199 } while(0)
200
201 #else
202 # define STOREL(I)              \
203 do { DEBUG_LOCALS_INSN(I, 'l'); \
204          jint __idx = (I);      \
205      locals[__idx+1].ia[0] = (--sp)->ia[0]; \
206      locals[__idx].ia[0] = (--sp)->ia[0];       \
207    } while (0)
208 # define STORED(I)              \
209 do { DEBUG_LOCALS_INSN(I, 'd'); \
210          jint __idx = (I);      \
211      locals[__idx+1].ia[0] = (--sp)->ia[0]; \
212      locals[__idx].ia[0] = (--sp)->ia[0];       \
213    } while (0)
214 #endif
215
216 #define PEEKI(I)  (locals+(I))->i
217 #define PEEKA(I)  (locals+(I))->o
218
219 #define POKEI(I,V)      \
220 DEBUG_LOCALS_INSN(I,'i'); \
221 ((locals+(I))->i = (V))
222
223
224 #define BINOPI(OP) { \
225    jint value2 = POPI(); \
226    jint value1 = POPI(); \
227    PUSHI(value1 OP value2); \
228 }
229
230 #define BINOPF(OP) { \
231    jfloat value2 = POPF(); \
232    jfloat value1 = POPF(); \
233    PUSHF(value1 OP value2); \
234 }
235
236 #define BINOPL(OP) { \
237    jlong value2 = POPL(); \
238    jlong value1 = POPL(); \
239    PUSHL(value1 OP value2); \
240 }
241
242 #define BINOPD(OP) { \
243    jdouble value2 = POPD(); \
244    jdouble value1 = POPD(); \
245    PUSHD(value1 OP value2); \
246 }
247
248 static inline jint get1s(unsigned char* loc) {
249   return *(signed char*)loc;
250 }
251
252 static inline jint get1u(unsigned char* loc) {
253   return *loc;
254 }
255
256 static inline jint get2s(unsigned char* loc) {
257   return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
258 }
259
260 static inline jint get2u(unsigned char* loc) {
261   return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
262 }
263
264 static jint get4(unsigned char* loc) {
265   return (((jint)(loc[0])) << 24) 
266        | (((jint)(loc[1])) << 16) 
267        | (((jint)(loc[2])) << 8) 
268        | (((jint)(loc[3])) << 0);
269 }
270
271 #define SAVE_PC() frame_desc.pc = pc
272
273 // We used to define this conditionally, depending on HANDLE_SEGV.
274 // However, that runs into a problem if a chunk in low memory is
275 // mapped and we try to look at a field near the end of a large
276 // object.  See PR 26858 for details.  It is, most likely, relatively
277 // inexpensive to simply do this check always.
278 #define NULLCHECK(X) \
279   do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
280
281 // Note that we can still conditionally define NULLARRAYCHECK, since
282 // we know that all uses of an array will first reference the length
283 // field, which is first -- and thus will trigger a SEGV.
284 #ifdef HANDLE_SEGV
285 #define NULLARRAYCHECK(X) SAVE_PC()
286 #else
287 #define NULLARRAYCHECK(X) \
288   do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
289 #endif
290
291 #define ARRAYBOUNDSCHECK(array, index)                                        \
292   do                                                                          \
293     {                                                                         \
294       if (((unsigned) index) >= (unsigned) (array->length))                   \
295         _Jv_ThrowBadArrayIndex (index);                                       \
296     }                                                                         \
297   while (0)
298
299 void
300 _Jv_InterpMethod::run_normal (ffi_cif *,
301                               void* ret,
302                               ffi_raw * args,
303                               void* __this)
304 {
305   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
306   run (ret, args, _this);
307 }
308
309 void
310 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
311                               void* ret,
312                               ffi_raw * args,
313                               void* __this)
314 {
315   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
316   run_debug (ret, args, _this);
317 }
318
319 void
320 _Jv_InterpMethod::run_synch_object (ffi_cif *,
321                                     void* ret,
322                                     ffi_raw * args,
323                                     void* __this)
324 {
325   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
326
327   jobject rcv = (jobject) args[0].ptr;
328   JvSynchronize mutex (rcv);
329
330   run (ret, args, _this);
331 }
332
333 void
334 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
335                                     void* ret,
336                                     ffi_raw * args,
337                                     void* __this)
338 {
339   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
340
341   jobject rcv = (jobject) args[0].ptr;
342   JvSynchronize mutex (rcv);
343
344   run_debug (ret, args, _this);
345 }
346
347 void
348 _Jv_InterpMethod::run_class (ffi_cif *,
349                              void* ret,
350                              ffi_raw * args,
351                              void* __this)
352 {
353   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
354   _Jv_InitClass (_this->defining_class);
355   run (ret, args, _this);
356 }
357
358 void
359 _Jv_InterpMethod::run_class_debug (ffi_cif *,
360                              void* ret,
361                              ffi_raw * args,
362                              void* __this)
363 {
364   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
365   _Jv_InitClass (_this->defining_class);
366   run_debug (ret, args, _this);
367 }
368
369 void
370 _Jv_InterpMethod::run_synch_class (ffi_cif *,
371                                    void* ret,
372                                    ffi_raw * args,
373                                    void* __this)
374 {
375   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
376
377   jclass sync = _this->defining_class;
378   _Jv_InitClass (sync);
379   JvSynchronize mutex (sync);
380
381   run (ret, args, _this);
382 }
383
384 void
385 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
386                                    void* ret,
387                                    ffi_raw * args,
388                                    void* __this)
389 {
390   _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
391
392   jclass sync = _this->defining_class;
393   _Jv_InitClass (sync);
394   JvSynchronize mutex (sync);
395
396   run_debug (ret, args, _this);
397 }
398
399 #ifdef DIRECT_THREADED
400 // "Compile" a method by turning it from bytecode to direct-threaded
401 // code.
402 void
403 _Jv_InterpMethod::compile (const void * const *insn_targets)
404 {
405   insn_slot *insns = NULL;
406   int next = 0;
407   unsigned char *codestart = bytecode ();
408   unsigned char *end = codestart + code_length;
409   _Jv_word *pool_data = defining_class->constants.data;
410
411 #define SET_ONE(Field, Value)                                                 \
412   do                                                                          \
413     {                                                                         \
414       if (first_pass)                                                         \
415         ++next;                                                               \
416       else                                                                    \
417         insns[next++].Field = Value;                                          \
418     }                                                                         \
419   while (0)
420
421 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
422 #define SET_INT(Value) SET_ONE (int_val, Value)
423 #define SET_DATUM(Value) SET_ONE (datum, Value)
424
425   // Map from bytecode PC to slot in INSNS.
426   int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
427   for (int i = 0; i < code_length; ++i)
428     pc_mapping[i] = -1;
429
430   for (int i = 0; i < 2; ++i)
431     {
432       jboolean first_pass = i == 0;
433
434       if (! first_pass)
435         {
436           insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
437           number_insn_slots = next;
438           next = 0;
439         }
440
441       unsigned char *pc = codestart;
442       while (pc < end)
443         {
444           int base_pc_val = pc - codestart;
445           if (first_pass)
446             pc_mapping[base_pc_val] = next;
447
448           java_opcode opcode = (java_opcode) *pc++;
449           // Just elide NOPs.
450           if (opcode == op_nop)
451             continue;
452           SET_INSN (insn_targets[opcode]);
453
454           switch (opcode)
455             {
456             case op_nop:
457             case op_aconst_null:
458             case op_iconst_m1:
459             case op_iconst_0:
460             case op_iconst_1:
461             case op_iconst_2:
462             case op_iconst_3:
463             case op_iconst_4:
464             case op_iconst_5:
465             case op_lconst_0:
466             case op_lconst_1:
467             case op_fconst_0:
468             case op_fconst_1:
469             case op_fconst_2:
470             case op_dconst_0:
471             case op_dconst_1:
472             case op_iload_0:
473             case op_iload_1:
474             case op_iload_2:
475             case op_iload_3:
476             case op_lload_0:
477             case op_lload_1:
478             case op_lload_2:
479             case op_lload_3:
480             case op_fload_0:
481             case op_fload_1:
482             case op_fload_2:
483             case op_fload_3:
484             case op_dload_0:
485             case op_dload_1:
486             case op_dload_2:
487             case op_dload_3:
488             case op_aload_0:
489             case op_aload_1:
490             case op_aload_2:
491             case op_aload_3:
492             case op_iaload:
493             case op_laload:
494             case op_faload:
495             case op_daload:
496             case op_aaload:
497             case op_baload:
498             case op_caload:
499             case op_saload:
500             case op_istore_0:
501             case op_istore_1:
502             case op_istore_2:
503             case op_istore_3:
504             case op_lstore_0:
505             case op_lstore_1:
506             case op_lstore_2:
507             case op_lstore_3:
508             case op_fstore_0:
509             case op_fstore_1:
510             case op_fstore_2:
511             case op_fstore_3:
512             case op_dstore_0:
513             case op_dstore_1:
514             case op_dstore_2:
515             case op_dstore_3:
516             case op_astore_0:
517             case op_astore_1:
518             case op_astore_2:
519             case op_astore_3:
520             case op_iastore:
521             case op_lastore:
522             case op_fastore:
523             case op_dastore:
524             case op_aastore:
525             case op_bastore:
526             case op_castore:
527             case op_sastore:
528             case op_pop:
529             case op_pop2:
530             case op_dup:
531             case op_dup_x1:
532             case op_dup_x2:
533             case op_dup2:
534             case op_dup2_x1:
535             case op_dup2_x2:
536             case op_swap:
537             case op_iadd:
538             case op_isub:
539             case op_imul:
540             case op_idiv:
541             case op_irem:
542             case op_ishl:
543             case op_ishr:
544             case op_iushr:
545             case op_iand:
546             case op_ior:
547             case op_ixor:
548             case op_ladd:
549             case op_lsub:
550             case op_lmul:
551             case op_ldiv:
552             case op_lrem:
553             case op_lshl:
554             case op_lshr:
555             case op_lushr:
556             case op_land:
557             case op_lor:
558             case op_lxor:
559             case op_fadd:
560             case op_fsub:
561             case op_fmul:
562             case op_fdiv:
563             case op_frem:
564             case op_dadd:
565             case op_dsub:
566             case op_dmul:
567             case op_ddiv:
568             case op_drem:
569             case op_ineg:
570             case op_i2b:
571             case op_i2c:
572             case op_i2s:
573             case op_lneg:
574             case op_fneg:
575             case op_dneg:
576             case op_i2l:
577             case op_i2f:
578             case op_i2d:
579             case op_l2i:
580             case op_l2f:
581             case op_l2d:
582             case op_f2i:
583             case op_f2l:
584             case op_f2d:
585             case op_d2i:
586             case op_d2l:
587             case op_d2f:
588             case op_lcmp:
589             case op_fcmpl:
590             case op_fcmpg:
591             case op_dcmpl:
592             case op_dcmpg:
593             case op_monitorenter:
594             case op_monitorexit:
595             case op_ireturn:
596             case op_lreturn:
597             case op_freturn:
598             case op_dreturn:
599             case op_areturn:
600             case op_return:
601             case op_athrow:
602             case op_arraylength:
603               // No argument, nothing else to do.
604               break;
605
606             case op_bipush:
607               SET_INT (get1s (pc));
608               ++pc;
609               break;
610
611             case op_ldc:
612               {
613                 int index = get1u (pc);
614                 ++pc;
615                 // For an unresolved class we want to delay resolution
616                 // until execution.
617                 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
618                   {
619                     --next;
620                     SET_INSN (insn_targets[int (op_jsr_w) + 1]);
621                     SET_INT (index);
622                   }
623                 else
624                   SET_DATUM (pool_data[index].o);
625               }
626               break;
627
628             case op_ret:
629             case op_iload:
630             case op_lload:
631             case op_fload:
632             case op_dload:
633             case op_aload:
634             case op_istore:
635             case op_lstore:
636             case op_fstore:
637             case op_dstore:
638             case op_astore:
639             case op_newarray:
640               SET_INT (get1u (pc));
641               ++pc;
642               break;
643
644             case op_iinc:
645               SET_INT (get1u (pc));
646               SET_INT (get1s (pc + 1));
647               pc += 2;
648               break;
649
650             case op_ldc_w:
651               {
652                 int index = get2u (pc);
653                 pc += 2;
654                 // For an unresolved class we want to delay resolution
655                 // until execution.
656                 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
657                   {
658                     --next;
659                     SET_INSN (insn_targets[int (op_jsr_w) + 1]);
660                     SET_INT (index);
661                   }
662                 else
663                   SET_DATUM (pool_data[index].o);
664               }
665               break;
666
667             case op_ldc2_w:
668               {
669                 int index = get2u (pc);
670                 pc += 2;
671                 SET_DATUM (&pool_data[index]);
672               }
673               break;
674
675             case op_sipush:
676               SET_INT (get2s (pc));
677               pc += 2;
678               break;
679
680             case op_new:
681             case op_getstatic:
682             case op_getfield:
683             case op_putfield:
684             case op_putstatic:
685             case op_anewarray:
686             case op_instanceof:
687             case op_checkcast:
688             case op_invokespecial:
689             case op_invokestatic:
690             case op_invokevirtual:
691               SET_INT (get2u (pc));
692               pc += 2;
693               break;
694
695             case op_multianewarray:
696               SET_INT (get2u (pc));
697               SET_INT (get1u (pc + 2));
698               pc += 3;
699               break;
700
701             case op_jsr:
702             case op_ifeq:
703             case op_ifne:
704             case op_iflt:
705             case op_ifge:
706             case op_ifgt:
707             case op_ifle:
708             case op_if_icmpeq:
709             case op_if_icmpne:
710             case op_if_icmplt:
711             case op_if_icmpge:
712             case op_if_icmpgt:
713             case op_if_icmple:
714             case op_if_acmpeq:
715             case op_if_acmpne:
716             case op_ifnull:
717             case op_ifnonnull:
718             case op_goto:
719               {
720                 int offset = get2s (pc);
721                 pc += 2;
722
723                 int new_pc = base_pc_val + offset;
724
725                 bool orig_was_goto = opcode == op_goto;
726
727                 // Thread jumps.  We limit the loop count; this lets
728                 // us avoid infinite loops if the bytecode contains
729                 // such.  `10' is arbitrary.
730                 int count = 10;
731                 while (codestart[new_pc] == op_goto && count-- > 0)
732                   new_pc += get2s (&codestart[new_pc + 1]);
733
734                 // If the jump takes us to a `return' instruction and
735                 // the original branch was an unconditional goto, then
736                 // we hoist the return.
737                 opcode = (java_opcode) codestart[new_pc];
738                 if (orig_was_goto
739                     && (opcode == op_ireturn || opcode == op_lreturn
740                         || opcode == op_freturn || opcode == op_dreturn
741                         || opcode == op_areturn || opcode == op_return))
742                   {
743                     --next;
744                     SET_INSN (insn_targets[opcode]);
745                   }
746                 else
747                   SET_DATUM (&insns[pc_mapping[new_pc]]);
748               }
749               break;
750
751             case op_tableswitch:
752               {
753                 while ((pc - codestart) % 4 != 0)
754                   ++pc;
755
756                 jint def = get4 (pc);
757                 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
758                 pc += 4;
759
760                 int low = get4 (pc);
761                 SET_INT (low);
762                 pc += 4;
763                 int high = get4 (pc);
764                 SET_INT (high);
765                 pc += 4;
766
767                 for (int i = low; i <= high; ++i)
768                   {
769                     SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
770                     pc += 4;
771                   }
772               }
773               break;
774
775             case op_lookupswitch:
776               {
777                 while ((pc - codestart) % 4 != 0)
778                   ++pc;
779
780                 jint def = get4 (pc);
781                 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
782                 pc += 4;
783
784                 jint npairs = get4 (pc);
785                 pc += 4;
786                 SET_INT (npairs);
787
788                 while (npairs-- > 0)
789                   {
790                     jint match = get4 (pc);
791                     jint offset = get4 (pc + 4);
792                     SET_INT (match);
793                     SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
794                     pc += 8;
795                   }
796               }
797               break;
798
799             case op_invokeinterface:
800               {
801                 jint index = get2u (pc);
802                 pc += 2;
803                 // We ignore the next two bytes.
804                 pc += 2;
805                 SET_INT (index);
806               }
807               break;
808
809             case op_wide:
810               {
811                 opcode = (java_opcode) get1u (pc);
812                 pc += 1;
813                 jint val = get2u (pc);
814                 pc += 2;
815
816                 // We implement narrow and wide instructions using the
817                 // same code in the interpreter.  So we rewrite the
818                 // instruction slot here.
819                 if (! first_pass)
820                   insns[next - 1].insn = (void *) insn_targets[opcode];
821                 SET_INT (val);
822
823                 if (opcode == op_iinc)
824                   {
825                     SET_INT (get2s (pc));
826                     pc += 2;
827                   }
828               }
829               break;
830
831             case op_jsr_w:
832             case op_goto_w:
833               {
834                 jint offset = get4 (pc);
835                 pc += 4;
836                 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
837               }
838               break;
839
840             // Some "can't happen" cases that we include for
841             // error-checking purposes.
842             case op_putfield_1:
843             case op_putfield_2:
844             case op_putfield_4:
845             case op_putfield_8:
846             case op_putfield_a:
847             case op_putstatic_1:
848             case op_putstatic_2:
849             case op_putstatic_4:
850             case op_putstatic_8:
851             case op_putstatic_a:
852             case op_getfield_1:
853             case op_getfield_2s:
854             case op_getfield_2u:
855             case op_getfield_4:
856             case op_getfield_8:
857             case op_getfield_a:
858             case op_getstatic_1:
859             case op_getstatic_2s:
860             case op_getstatic_2u:
861             case op_getstatic_4:
862             case op_getstatic_8:
863             case op_getstatic_a:
864             case op_breakpoint:
865             default:
866               // Fail somehow.
867               break;
868             }
869         }
870     }
871
872   // Now update exceptions.
873   _Jv_InterpException *exc = exceptions ();
874   for (int i = 0; i < exc_count; ++i)
875     {
876       exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
877       exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
878       exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
879       // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
880       // during compilation.
881       jclass handler
882         = (_Jv_Linker::resolve_pool_entry (defining_class,
883                                              exc[i].handler_type.i)).clazz;
884       exc[i].handler_type.p = handler;
885     }
886
887   // Translate entries in the LineNumberTable from bytecode PC's to direct
888   // threaded interpreter instruction values.
889   for (int i = 0; i < line_table_len; i++)
890     {
891       int byte_pc = line_table[i].bytecode_pc;
892       // It isn't worth throwing an exception if this table is
893       // corrupted, but at the same time we don't want a crash.
894       if (byte_pc < 0 || byte_pc >= code_length)
895         byte_pc = 0;
896       line_table[i].pc = &insns[pc_mapping[byte_pc]];
897     }  
898
899   prepared = insns;
900
901   if (breakpoint_insn == NULL)
902     {
903       bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
904       breakpoint_insn = &bp_insn_slot;
905     }
906 }
907 #endif /* DIRECT_THREADED */
908
909 /* Run the given method.
910    When args is NULL, don't run anything -- just compile it. */
911 void
912 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
913 {
914 #undef DEBUG
915 #undef DEBUG_LOCALS_INSN
916 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
917
918 #include "interpret-run.cc"
919 }
920
921 void
922 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
923 {
924 /* Used to keep track of local variable type
925  * 
926  * Possible Types:
927  * o object
928  * i integer
929  * f float
930  * l long 
931  * d double 
932  */
933 #define DEBUG
934 #undef DEBUG_LOCALS_INSN
935 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
936
937 #include "interpret-run.cc"
938 }
939
940 static void
941 throw_internal_error (const char *msg)
942 {
943   throw new java::lang::InternalError (JvNewStringLatin1 (msg));
944 }
945
946 static void 
947 throw_incompatible_class_change_error (jstring msg)
948 {
949   throw new java::lang::IncompatibleClassChangeError (msg);
950 }
951
952 static void 
953 throw_null_pointer_exception ()
954 {
955   throw new java::lang::NullPointerException;
956 }
957
958 /* Look up source code line number for given bytecode (or direct threaded
959    interpreter) PC. */
960 int
961 _Jv_InterpMethod::get_source_line(pc_t mpc)
962 {
963   int line = line_table_len > 0 ? line_table[0].line : -1;
964   for (int i = 1; i < line_table_len; i++)
965     if (line_table[i].pc > mpc)
966       break;
967     else
968       line = line_table[i].line;
969
970   return line;
971 }
972
973 /** Do static initialization for fields with a constant initializer */
974 void
975 _Jv_InitField (jobject obj, jclass klass, int index)
976 {
977   using namespace java::lang::reflect;
978
979   if (obj != 0 && klass == 0)
980     klass = obj->getClass ();
981
982   if (!_Jv_IsInterpretedClass (klass))
983     return;
984
985   _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
986
987   _Jv_Field * field = (&klass->fields[0]) + index;
988
989   if (index > klass->field_count)
990     throw_internal_error ("field out of range");
991
992   int init = iclass->field_initializers[index];
993   if (init == 0)
994     return;
995
996   _Jv_Constants *pool = &klass->constants;
997   int tag = pool->tags[init];
998
999   if (! field->isResolved ())
1000     throw_internal_error ("initializing unresolved field");
1001
1002   if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1003     throw_internal_error ("initializing non-static field with no object");
1004
1005   void *addr = 0;
1006
1007   if ((field->flags & Modifier::STATIC) != 0)
1008     addr = (void*) field->u.addr;
1009   else
1010     addr = (void*) (((char*)obj) + field->u.boffset);
1011
1012   switch (tag)
1013     {
1014     case JV_CONSTANT_String:
1015       {
1016         jstring str;
1017         str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1018         pool->data[init].string = str;
1019         pool->tags[init] = JV_CONSTANT_ResolvedString;
1020       }
1021       /* fall through */
1022
1023     case JV_CONSTANT_ResolvedString:
1024       if (! (field->type == &java::lang::String::class$
1025              || field->type == &java::lang::Class::class$))
1026         throw_class_format_error ("string initialiser to non-string field");
1027
1028       *(jstring*)addr = pool->data[init].string;
1029       break;
1030
1031     case JV_CONSTANT_Integer:
1032       {
1033         int value = pool->data[init].i;
1034
1035         if (field->type == JvPrimClass (boolean))
1036           *(jboolean*)addr = (jboolean)value;
1037         
1038         else if (field->type == JvPrimClass (byte))
1039           *(jbyte*)addr = (jbyte)value;
1040         
1041         else if (field->type == JvPrimClass (char))
1042           *(jchar*)addr = (jchar)value;
1043
1044         else if (field->type == JvPrimClass (short))
1045           *(jshort*)addr = (jshort)value;
1046         
1047         else if (field->type == JvPrimClass (int))
1048           *(jint*)addr = (jint)value;
1049
1050         else
1051           throw_class_format_error ("erroneous field initializer");
1052       }  
1053       break;
1054
1055     case JV_CONSTANT_Long:
1056       if (field->type != JvPrimClass (long))
1057         throw_class_format_error ("erroneous field initializer");
1058
1059       *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1060       break;
1061
1062     case JV_CONSTANT_Float:
1063       if (field->type != JvPrimClass (float))
1064         throw_class_format_error ("erroneous field initializer");
1065
1066       *(jfloat*)addr = pool->data[init].f;
1067       break;
1068
1069     case JV_CONSTANT_Double:
1070       if (field->type != JvPrimClass (double))
1071         throw_class_format_error ("erroneous field initializer");
1072
1073       *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1074       break;
1075
1076     default:
1077       throw_class_format_error ("erroneous field initializer");
1078     }
1079 }
1080
1081 inline static unsigned char*
1082 skip_one_type (unsigned char* ptr)
1083 {
1084   int ch = *ptr++;
1085
1086   while (ch == '[')
1087     { 
1088       ch = *ptr++;
1089     }
1090   
1091   if (ch == 'L')
1092     {
1093       do { ch = *ptr++; } while (ch != ';');
1094     }
1095
1096   return ptr;
1097 }
1098
1099 static ffi_type*
1100 get_ffi_type_from_signature (unsigned char* ptr)
1101 {
1102   switch (*ptr) 
1103     {
1104     case 'L':
1105     case '[':
1106       return &ffi_type_pointer;
1107       break;
1108
1109     case 'Z':
1110       // On some platforms a bool is a byte, on others an int.
1111       if (sizeof (jboolean) == sizeof (jbyte))
1112         return &ffi_type_sint8;
1113       else
1114         {
1115           JvAssert (sizeof (jbyte) == sizeof (jint));
1116           return &ffi_type_sint32;
1117         }
1118       break;
1119
1120     case 'B':
1121       return &ffi_type_sint8;
1122       break;
1123       
1124     case 'C':
1125       return &ffi_type_uint16;
1126       break;
1127           
1128     case 'S': 
1129       return &ffi_type_sint16;
1130       break;
1131           
1132     case 'I':
1133       return &ffi_type_sint32;
1134       break;
1135           
1136     case 'J':
1137       return &ffi_type_sint64;
1138       break;
1139           
1140     case 'F':
1141       return &ffi_type_float;
1142       break;
1143           
1144     case 'D':
1145       return &ffi_type_double;
1146       break;
1147
1148     case 'V':
1149       return &ffi_type_void;
1150       break;
1151     }
1152
1153   throw_internal_error ("unknown type in signature");
1154 }
1155
1156 /* this function yields the number of actual arguments, that is, if the
1157  * function is non-static, then one is added to the number of elements
1158  * found in the signature */
1159
1160 int 
1161 _Jv_count_arguments (_Jv_Utf8Const *signature,
1162                      jboolean staticp)
1163 {
1164   unsigned char *ptr = (unsigned char*) signature->chars();
1165   int arg_count = staticp ? 0 : 1;
1166
1167   /* first, count number of arguments */
1168
1169   // skip '('
1170   ptr++;
1171
1172   // count args
1173   while (*ptr != ')')
1174     {
1175       ptr = skip_one_type (ptr);
1176       arg_count += 1;
1177     }
1178
1179   return arg_count;
1180 }
1181
1182 /* This beast will build a cif, given the signature.  Memory for
1183  * the cif itself and for the argument types must be allocated by the
1184  * caller.
1185  */
1186
1187 int 
1188 _Jv_init_cif (_Jv_Utf8Const* signature,
1189               int arg_count,
1190               jboolean staticp,
1191               ffi_cif *cif,
1192               ffi_type **arg_types,
1193               ffi_type **rtype_p)
1194 {
1195   unsigned char *ptr = (unsigned char*) signature->chars();
1196
1197   int arg_index = 0;            // arg number
1198   int item_count = 0;           // stack-item count
1199
1200   // setup receiver
1201   if (!staticp)
1202     {
1203       arg_types[arg_index++] = &ffi_type_pointer;
1204       item_count += 1;
1205     }
1206
1207   // skip '('
1208   ptr++;
1209
1210   // assign arg types
1211   while (*ptr != ')')
1212     {
1213       arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1214
1215       if (*ptr == 'J' || *ptr == 'D')
1216         item_count += 2;
1217       else
1218         item_count += 1;
1219
1220       ptr = skip_one_type (ptr);
1221     }
1222
1223   // skip ')'
1224   ptr++;
1225   ffi_type *rtype = get_ffi_type_from_signature (ptr);
1226
1227   ptr = skip_one_type (ptr);
1228   if (ptr != (unsigned char*)signature->chars() + signature->len())
1229     throw_internal_error ("did not find end of signature");
1230
1231   if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1232                     arg_count, rtype, arg_types) != FFI_OK)
1233     throw_internal_error ("ffi_prep_cif failed");
1234
1235   if (rtype_p != NULL)
1236     *rtype_p = rtype;
1237
1238   return item_count;
1239 }
1240
1241 #if FFI_NATIVE_RAW_API
1242 #   define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1243 #   define FFI_RAW_SIZE ffi_raw_size
1244 #else
1245 #   define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1246 #   define FFI_RAW_SIZE ffi_java_raw_size
1247 #endif
1248
1249 /* we put this one here, and not in interpret.cc because it
1250  * calls the utility routines _Jv_count_arguments 
1251  * which are static to this module.  The following struct defines the
1252  * layout we use for the stubs, it's only used in the ncode method. */
1253
1254 typedef struct {
1255   ffi_raw_closure  closure;
1256   ffi_cif   cif;
1257   ffi_type *arg_types[0];
1258 } ncode_closure;
1259
1260 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1261
1262 void *
1263 _Jv_InterpMethod::ncode ()
1264 {
1265   using namespace java::lang::reflect;
1266
1267   if (self->ncode != 0)
1268     return self->ncode;
1269
1270   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1271   int arg_count = _Jv_count_arguments (self->signature, staticp);
1272
1273   ncode_closure *closure =
1274     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1275                                         + arg_count * sizeof (ffi_type*));
1276
1277   _Jv_init_cif (self->signature,
1278                 arg_count,
1279                 staticp,
1280                 &closure->cif,
1281                 &closure->arg_types[0],
1282                 NULL);
1283
1284   ffi_closure_fun fun;
1285
1286   args_raw_size = FFI_RAW_SIZE (&closure->cif);
1287
1288   JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1289
1290   if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1291     {
1292       if (staticp)
1293         {
1294         if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1295                   fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1296                 else
1297                   fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1298         }
1299       else
1300         {
1301               if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1302                     fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1303                   else
1304                         fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1305         } 
1306     }
1307   else
1308     {
1309       if (staticp)
1310         {
1311               if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1312                     fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1313                   else
1314                     fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1315         }
1316       else
1317         {
1318               if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1319                     fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1320                   else
1321                     fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1322         }
1323     }
1324
1325   FFI_PREP_RAW_CLOSURE (&closure->closure,
1326                         &closure->cif, 
1327                         fun,
1328                         (void*)this);
1329
1330   self->ncode = (void*)closure;
1331   return self->ncode;
1332 }
1333
1334 /* Find the index of the given insn in the array of insn slots
1335    for this method. Returns -1 if not found. */
1336 jlong
1337 _Jv_InterpMethod::insn_index (pc_t pc)
1338 {
1339   jlong left = 0;
1340 #ifdef DIRECT_THREADED
1341   jlong right = number_insn_slots;
1342   pc_t insns = prepared;
1343 #else
1344   jlong right = code_length;
1345   pc_t insns = bytecode ();
1346 #endif
1347
1348   while (right >= 0)
1349     {
1350       jlong mid = (left + right) / 2;
1351       if (&insns[mid] == pc)
1352         return mid;
1353
1354       if (pc < &insns[mid])
1355         right = mid - 1;
1356       else
1357         left = mid + 1;
1358     }
1359
1360   return -1;
1361 }
1362
1363 void
1364 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1365                                   jintArray& line_numbers,
1366                                   jlongArray& code_indices)
1367 {
1368 #ifdef DIRECT_THREADED
1369   /* For the DIRECT_THREADED case, if the method has not yet been
1370    * compiled, the linetable will change to insn slots instead of
1371    * bytecode PCs. It is probably easiest, in this case, to simply
1372    * compile the method and guarantee that we are using insn
1373    * slots.
1374    */
1375   _Jv_CompileMethod (this);
1376
1377   if (line_table_len > 0)
1378     {
1379       start = 0;
1380       end = number_insn_slots;
1381       line_numbers = JvNewIntArray (line_table_len);
1382       code_indices = JvNewLongArray (line_table_len);
1383
1384       jint* lines = elements (line_numbers);
1385       jlong* indices = elements (code_indices);
1386       for (int i = 0; i < line_table_len; ++i)
1387         {
1388           lines[i] = line_table[i].line;
1389           indices[i] = insn_index (line_table[i].pc);
1390         }
1391     }
1392 #else // !DIRECT_THREADED
1393   if (line_table_len > 0)
1394     {
1395       start = 0;
1396       end = code_length;
1397       line_numbers = JvNewIntArray (line_table_len);
1398       code_indices = JvNewLongArray (line_table_len);
1399
1400       jint* lines = elements (line_numbers);
1401       jlong* indices = elements (code_indices);
1402       for (int i = 0; i < line_table_len; ++i)
1403         {
1404           lines[i] = line_table[i].line;
1405           indices[i] = (jlong) line_table[i].bytecode_pc;
1406         }
1407     }
1408 #endif // !DIRECT_THREADED
1409 }
1410
1411 pc_t
1412 _Jv_InterpMethod::install_break (jlong index)
1413 {
1414   return set_insn (index, breakpoint_insn);
1415 }
1416
1417 pc_t
1418 _Jv_InterpMethod::get_insn (jlong index)
1419 {
1420   pc_t code;
1421
1422 #ifdef DIRECT_THREADED
1423   if (index >= number_insn_slots || index < 0)
1424     return NULL;
1425
1426   code = prepared;
1427 #else // !DIRECT_THREADED
1428   if (index >= code_length || index < 0)
1429     return NULL;
1430
1431   code = reinterpret_cast<pc_t> (bytecode ());
1432 #endif // !DIRECT_THREADED
1433
1434   return &code[index];
1435 }
1436
1437 pc_t
1438 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1439 {
1440 #ifdef DIRECT_THREADED
1441   if (index >= number_insn_slots || index < 0)
1442     return NULL;
1443
1444   pc_t code = prepared;
1445   code[index].insn = insn->insn;
1446 #else // !DIRECT_THREADED
1447   if (index >= code_length || index < 0)
1448     return NULL;
1449
1450   pc_t code = reinterpret_cast<pc_t> (bytecode ());
1451   code[index] = *insn;
1452 #endif // !DIRECT_THREADED
1453
1454   return &code[index];
1455 }
1456
1457 void *
1458 _Jv_JNIMethod::ncode ()
1459 {
1460   using namespace java::lang::reflect;
1461
1462   if (self->ncode != 0)
1463     return self->ncode;
1464
1465   jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1466   int arg_count = _Jv_count_arguments (self->signature, staticp);
1467
1468   ncode_closure *closure =
1469     (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1470                                     + arg_count * sizeof (ffi_type*));
1471
1472   ffi_type *rtype;
1473   _Jv_init_cif (self->signature,
1474                 arg_count,
1475                 staticp,
1476                 &closure->cif,
1477                 &closure->arg_types[0],
1478                 &rtype);
1479
1480   ffi_closure_fun fun;
1481
1482   args_raw_size = FFI_RAW_SIZE (&closure->cif);
1483
1484   // Initialize the argument types and CIF that represent the actual
1485   // underlying JNI function.
1486   int extra_args = 1;
1487   if ((self->accflags & Modifier::STATIC))
1488     ++extra_args;
1489   jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1490                                                 * sizeof (ffi_type *));
1491   int offset = 0;
1492   jni_arg_types[offset++] = &ffi_type_pointer;
1493   if ((self->accflags & Modifier::STATIC))
1494     jni_arg_types[offset++] = &ffi_type_pointer;
1495   memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1496           arg_count * sizeof (ffi_type *));
1497
1498   if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1499                     extra_args + arg_count, rtype,
1500                     jni_arg_types) != FFI_OK)
1501     throw_internal_error ("ffi_prep_cif failed for JNI function");
1502
1503   JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1504
1505   // FIXME: for now we assume that all native methods for
1506   // interpreted code use JNI.
1507   fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1508
1509   FFI_PREP_RAW_CLOSURE (&closure->closure,
1510                         &closure->cif, 
1511                         fun,
1512                         (void*) this);
1513
1514   self->ncode = (void *) closure;
1515   return self->ncode;
1516 }
1517
1518 static void
1519 throw_class_format_error (jstring msg)
1520 {
1521   throw (msg
1522          ? new java::lang::ClassFormatError (msg)
1523          : new java::lang::ClassFormatError);
1524 }
1525
1526 static void
1527 throw_class_format_error (const char *msg)
1528 {
1529   throw_class_format_error (JvNewStringLatin1 (msg));
1530 }
1531
1532 \f
1533
1534 void
1535 _Jv_InterpreterEngine::do_verify (jclass klass)
1536 {
1537   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1538   for (int i = 0; i < klass->method_count; i++)
1539     {
1540       using namespace java::lang::reflect;
1541       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1542       _Jv_ushort accflags = klass->methods[i].accflags;
1543       if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1544         {
1545           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1546           _Jv_VerifyMethod (im);
1547         }
1548     }
1549 }
1550
1551 void
1552 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1553 {
1554   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1555   for (int i = 0; i < klass->method_count; i++)
1556     {
1557       // Just skip abstract methods.  This is particularly important
1558       // because we don't resize the interpreted_methods array when
1559       // miranda methods are added to it.
1560       if ((klass->methods[i].accflags
1561            & java::lang::reflect::Modifier::ABSTRACT)
1562           != 0)
1563         continue;
1564
1565       _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1566
1567       if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1568           != 0)
1569         {
1570           // You might think we could use a virtual `ncode' method in
1571           // the _Jv_MethodBase and unify the native and non-native
1572           // cases.  Well, we can't, because we don't allocate these
1573           // objects using `new', and thus they don't get a vtable.
1574           _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1575           klass->methods[i].ncode = jnim->ncode ();
1576         }
1577       else if (imeth != 0)              // it could be abstract
1578         {
1579           _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1580           klass->methods[i].ncode = im->ncode ();
1581         }
1582     }
1583 }
1584
1585 void
1586 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1587                                                   int pointer_size,
1588                                                   int other_size)
1589 {
1590   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1591
1592   // Splitting the allocations here lets us scan reference fields and
1593   // avoid scanning non-reference fields.  How reference fields are
1594   // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1595   // means that this memory will be scanned conservatively (same
1596   // difference, since we know all the contents here are pointers).
1597   // Then we put pointers into this memory into the 'fields'
1598   // structure.  Most of these are interior pointers, which is ok (but
1599   // even so the pointer to the first reference field will be used and
1600   // that is not an interior pointer).  The 'fields' array is also
1601   // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1602   // be scanned.  A pointer to this array is held by Class and thus
1603   // seen by the collector.
1604   char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1605   char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1606
1607   for (int i = 0; i < klass->field_count; i++)
1608     {
1609       _Jv_Field *field = &klass->fields[i];
1610
1611       if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1612         continue;
1613
1614       char *base = field->isRef() ? reference_fields : non_reference_fields;
1615       field->u.addr  = base + field->u.boffset;
1616
1617       if (iclass->field_initializers[i] != 0)
1618         {
1619           _Jv_Linker::resolve_field (field, klass->loader);
1620           _Jv_InitField (0, klass, i);
1621         }
1622     }
1623
1624   // Now we don't need the field_initializers anymore, so let the
1625   // collector get rid of it.
1626   iclass->field_initializers = 0;
1627 }
1628
1629 _Jv_ResolvedMethod *
1630 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1631                                           jboolean staticp)
1632 {
1633   int arg_count = _Jv_count_arguments (method->signature, staticp);
1634
1635   _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1636     _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1637                     + arg_count*sizeof (ffi_type*));
1638
1639   result->stack_item_count
1640     = _Jv_init_cif (method->signature,
1641                     arg_count,
1642                     staticp,
1643                     &result->cif,
1644                     &result->arg_types[0],
1645                     NULL);
1646
1647   result->method              = method;
1648   result->klass               = klass;
1649
1650   return result;
1651 }
1652
1653 void
1654 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1655 {
1656   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1657   for (int i = 0; i < klass->method_count; i++)
1658     {
1659       // Just skip abstract methods.  This is particularly important
1660       // because we don't resize the interpreted_methods array when
1661       // miranda methods are added to it.
1662       if ((klass->methods[i].accflags
1663            & java::lang::reflect::Modifier::ABSTRACT)
1664           != 0)
1665         continue;
1666       // Miranda method additions mean that the `methods' array moves.
1667       // We cache a pointer into this array, so we have to update.
1668       iclass->interpreted_methods[i]->self = &klass->methods[i];
1669     }
1670 }
1671
1672 #ifdef DIRECT_THREADED
1673 void
1674 _Jv_CompileMethod (_Jv_InterpMethod* method)
1675 {
1676   if (method->prepared == NULL)
1677     _Jv_InterpMethod::run (NULL, NULL, method);
1678 }
1679 #endif // DIRECT_THREADED
1680
1681 #endif // INTERPRETER