OSDN Git Service

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