OSDN Git Service

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