Revert emutls patch.
[official-gcc.git] / libjava / interpret.cc
blob1c4e21e6b76e5a03411722ecde2e89d86ea018df
1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
13 #include <config.h>
14 #include <platform.h>
16 #pragma implementation "java-interp.h"
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>
40 #include <gnu/classpath/jdwp/Jdwp.h>
42 #ifdef INTERPRETER
44 // Execution engine for interpreted code.
45 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
47 #include <stdlib.h>
49 using namespace gcj;
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__));
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__));
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;
69 void
70 _Jv_InitInterpreter()
72 _Jv_MutexInit (&compile_mutex);
74 #else
75 void _Jv_InitInterpreter() {}
76 #endif
78 extern "C" double __ieee754_fmod (double,double);
80 static inline void dupx (_Jv_word *sp, int n, int x)
82 // first "slide" n+x elements n to the right
83 int top = n-1;
84 for (int i = 0; i < n+x; i++)
86 sp[(top-i)] = sp[(top-i)-n];
89 // next, copy the n top elements, n+x down
90 for (int i = 0; i < n; i++)
92 sp[top-(n+x)-i] = sp[top-i];
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)
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;
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
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
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
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)
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
199 #define PEEKI(I) (locals+(I))->i
200 #define PEEKA(I) (locals+(I))->o
202 #define POKEI(I,V) \
203 DEBUG_LOCALS_INSN(I,'i'); \
204 ((locals+(I))->i = (V))
207 #define BINOPI(OP) { \
208 jint value2 = POPI(); \
209 jint value1 = POPI(); \
210 PUSHI(value1 OP value2); \
213 #define BINOPF(OP) { \
214 jfloat value2 = POPF(); \
215 jfloat value1 = POPF(); \
216 PUSHF(value1 OP value2); \
219 #define BINOPL(OP) { \
220 jlong value2 = POPL(); \
221 jlong value1 = POPL(); \
222 PUSHL(value1 OP value2); \
225 #define BINOPD(OP) { \
226 jdouble value2 = POPD(); \
227 jdouble value1 = POPD(); \
228 PUSHD(value1 OP value2); \
231 static inline jint get1s(unsigned char* loc) {
232 return *(signed char*)loc;
235 static inline jint get1u(unsigned char* loc) {
236 return *loc;
239 static inline jint get2s(unsigned char* loc) {
240 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
243 static inline jint get2u(unsigned char* loc) {
244 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
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);
254 #define SAVE_PC() frame_desc.pc = pc
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)
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
274 #define ARRAYBOUNDSCHECK(array, index) \
275 do \
277 if (((unsigned) index) >= (unsigned) (array->length)) \
278 _Jv_ThrowBadArrayIndex (index); \
280 while (0)
282 void
283 _Jv_InterpMethod::run_normal (ffi_cif *,
284 void* ret,
285 ffi_raw * args,
286 void* __this)
288 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
289 run (ret, args, _this);
292 void
293 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
294 void* ret,
295 ffi_raw * args,
296 void* __this)
298 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
299 run_debug (ret, args, _this);
302 void
303 _Jv_InterpMethod::run_synch_object (ffi_cif *,
304 void* ret,
305 ffi_raw * args,
306 void* __this)
308 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
310 jobject rcv = (jobject) args[0].ptr;
311 JvSynchronize mutex (rcv);
313 run (ret, args, _this);
316 void
317 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
318 void* ret,
319 ffi_raw * args,
320 void* __this)
322 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
324 jobject rcv = (jobject) args[0].ptr;
325 JvSynchronize mutex (rcv);
327 run_debug (ret, args, _this);
330 void
331 _Jv_InterpMethod::run_class (ffi_cif *,
332 void* ret,
333 ffi_raw * args,
334 void* __this)
336 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
337 _Jv_InitClass (_this->defining_class);
338 run (ret, args, _this);
341 void
342 _Jv_InterpMethod::run_class_debug (ffi_cif *,
343 void* ret,
344 ffi_raw * args,
345 void* __this)
347 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
348 _Jv_InitClass (_this->defining_class);
349 run_debug (ret, args, _this);
352 void
353 _Jv_InterpMethod::run_synch_class (ffi_cif *,
354 void* ret,
355 ffi_raw * args,
356 void* __this)
358 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
360 jclass sync = _this->defining_class;
361 _Jv_InitClass (sync);
362 JvSynchronize mutex (sync);
364 run (ret, args, _this);
367 void
368 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
369 void* ret,
370 ffi_raw * args,
371 void* __this)
373 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
375 jclass sync = _this->defining_class;
376 _Jv_InitClass (sync);
377 JvSynchronize mutex (sync);
379 run_debug (ret, args, _this);
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)
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;
394 #define SET_ONE(Field, Value) \
395 do \
397 if (first_pass) \
398 ++next; \
399 else \
400 insns[next++].Field = Value; \
402 while (0)
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)
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;
413 for (int i = 0; i < 2; ++i)
415 jboolean first_pass = i == 0;
417 if (! first_pass)
419 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
420 number_insn_slots = next;
421 next = 0;
424 unsigned char *pc = codestart;
425 while (pc < end)
427 int base_pc_val = pc - codestart;
428 if (first_pass)
429 pc_mapping[base_pc_val] = next;
431 java_opcode opcode = (java_opcode) *pc++;
432 // Just elide NOPs.
433 if (opcode == op_nop)
434 continue;
435 SET_INSN (insn_targets[opcode]);
437 switch (opcode)
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;
589 case op_bipush:
590 SET_INT (get1s (pc));
591 ++pc;
592 break;
594 case op_ldc:
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)
602 --next;
603 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
604 SET_INT (index);
606 else
607 SET_DATUM (pool_data[index].o);
609 break;
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;
627 case op_iinc:
628 SET_INT (get1u (pc));
629 SET_INT (get1s (pc + 1));
630 pc += 2;
631 break;
633 case op_ldc_w:
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)
641 --next;
642 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
643 SET_INT (index);
645 else
646 SET_DATUM (pool_data[index].o);
648 break;
650 case op_ldc2_w:
652 int index = get2u (pc);
653 pc += 2;
654 SET_DATUM (&pool_data[index]);
656 break;
658 case op_sipush:
659 SET_INT (get2s (pc));
660 pc += 2;
661 break;
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;
678 case op_multianewarray:
679 SET_INT (get2u (pc));
680 SET_INT (get1u (pc + 2));
681 pc += 3;
682 break;
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:
703 int offset = get2s (pc);
704 pc += 2;
706 int new_pc = base_pc_val + offset;
708 bool orig_was_goto = opcode == op_goto;
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]);
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))
726 --next;
727 SET_INSN (insn_targets[opcode]);
729 else
730 SET_DATUM (&insns[pc_mapping[new_pc]]);
732 break;
734 case op_tableswitch:
736 while ((pc - codestart) % 4 != 0)
737 ++pc;
739 jint def = get4 (pc);
740 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
741 pc += 4;
743 int low = get4 (pc);
744 SET_INT (low);
745 pc += 4;
746 int high = get4 (pc);
747 SET_INT (high);
748 pc += 4;
750 for (int i = low; i <= high; ++i)
752 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
753 pc += 4;
756 break;
758 case op_lookupswitch:
760 while ((pc - codestart) % 4 != 0)
761 ++pc;
763 jint def = get4 (pc);
764 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
765 pc += 4;
767 jint npairs = get4 (pc);
768 pc += 4;
769 SET_INT (npairs);
771 while (npairs-- > 0)
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;
780 break;
782 case op_invokeinterface:
784 jint index = get2u (pc);
785 pc += 2;
786 // We ignore the next two bytes.
787 pc += 2;
788 SET_INT (index);
790 break;
792 case op_wide:
794 opcode = (java_opcode) get1u (pc);
795 pc += 1;
796 jint val = get2u (pc);
797 pc += 2;
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);
806 if (opcode == op_iinc)
808 SET_INT (get2s (pc));
809 pc += 2;
812 break;
814 case op_jsr_w:
815 case op_goto_w:
817 jint offset = get4 (pc);
818 pc += 4;
819 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
821 break;
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;
854 // Now update exceptions.
855 _Jv_InterpException *exc = exceptions ();
856 for (int i = 0; i < exc_count; ++i)
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;
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++)
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]];
881 prepared = insns;
883 #endif /* DIRECT_THREADED */
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)
890 #undef DEBUG
891 #undef DEBUG_LOCALS_INSN
892 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
894 #include "interpret-run.cc"
897 void
898 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
900 /* Used to keep track of local variable type
902 * Possible Types:
903 * o object
904 * i integer
905 * f float
906 * l long
907 * d double
909 #define DEBUG
910 #undef DEBUG_LOCALS_INSN
911 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
913 #include "interpret-run.cc"
916 static void
917 throw_internal_error (const char *msg)
919 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
922 static void
923 throw_incompatible_class_change_error (jstring msg)
925 throw new java::lang::IncompatibleClassChangeError (msg);
928 static void
929 throw_null_pointer_exception ()
931 throw new java::lang::NullPointerException;
934 /* Look up source code line number for given bytecode (or direct threaded
935 interpreter) PC. */
937 _Jv_InterpMethod::get_source_line(pc_t mpc)
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;
946 return line;
949 /** Do static initialization for fields with a constant initializer */
950 void
951 _Jv_InitField (jobject obj, jclass klass, int index)
953 using namespace java::lang::reflect;
955 if (obj != 0 && klass == 0)
956 klass = obj->getClass ();
958 if (!_Jv_IsInterpretedClass (klass))
959 return;
961 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
963 _Jv_Field * field = (&klass->fields[0]) + index;
965 if (index > klass->field_count)
966 throw_internal_error ("field out of range");
968 int init = iclass->field_initializers[index];
969 if (init == 0)
970 return;
972 _Jv_Constants *pool = &klass->constants;
973 int tag = pool->tags[init];
975 if (! field->isResolved ())
976 throw_internal_error ("initializing unresolved field");
978 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
979 throw_internal_error ("initializing non-static field with no object");
981 void *addr = 0;
983 if ((field->flags & Modifier::STATIC) != 0)
984 addr = (void*) field->u.addr;
985 else
986 addr = (void*) (((char*)obj) + field->u.boffset);
988 switch (tag)
990 case JV_CONSTANT_String:
992 jstring str;
993 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
994 pool->data[init].string = str;
995 pool->tags[init] = JV_CONSTANT_ResolvedString;
997 /* fall through */
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");
1004 *(jstring*)addr = pool->data[init].string;
1005 break;
1007 case JV_CONSTANT_Integer:
1009 int value = pool->data[init].i;
1011 if (field->type == JvPrimClass (boolean))
1012 *(jboolean*)addr = (jboolean)value;
1014 else if (field->type == JvPrimClass (byte))
1015 *(jbyte*)addr = (jbyte)value;
1017 else if (field->type == JvPrimClass (char))
1018 *(jchar*)addr = (jchar)value;
1020 else if (field->type == JvPrimClass (short))
1021 *(jshort*)addr = (jshort)value;
1023 else if (field->type == JvPrimClass (int))
1024 *(jint*)addr = (jint)value;
1026 else
1027 throw_class_format_error ("erroneous field initializer");
1029 break;
1031 case JV_CONSTANT_Long:
1032 if (field->type != JvPrimClass (long))
1033 throw_class_format_error ("erroneous field initializer");
1035 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1036 break;
1038 case JV_CONSTANT_Float:
1039 if (field->type != JvPrimClass (float))
1040 throw_class_format_error ("erroneous field initializer");
1042 *(jfloat*)addr = pool->data[init].f;
1043 break;
1045 case JV_CONSTANT_Double:
1046 if (field->type != JvPrimClass (double))
1047 throw_class_format_error ("erroneous field initializer");
1049 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1050 break;
1052 default:
1053 throw_class_format_error ("erroneous field initializer");
1057 inline static unsigned char*
1058 skip_one_type (unsigned char* ptr)
1060 int ch = *ptr++;
1062 while (ch == '[')
1064 ch = *ptr++;
1067 if (ch == 'L')
1069 do { ch = *ptr++; } while (ch != ';');
1072 return ptr;
1075 static ffi_type*
1076 get_ffi_type_from_signature (unsigned char* ptr)
1078 switch (*ptr)
1080 case 'L':
1081 case '[':
1082 return &ffi_type_pointer;
1083 break;
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
1091 JvAssert (sizeof (jbyte) == sizeof (jint));
1092 return &ffi_type_sint32;
1094 break;
1096 case 'B':
1097 return &ffi_type_sint8;
1098 break;
1100 case 'C':
1101 return &ffi_type_uint16;
1102 break;
1104 case 'S':
1105 return &ffi_type_sint16;
1106 break;
1108 case 'I':
1109 return &ffi_type_sint32;
1110 break;
1112 case 'J':
1113 return &ffi_type_sint64;
1114 break;
1116 case 'F':
1117 return &ffi_type_float;
1118 break;
1120 case 'D':
1121 return &ffi_type_double;
1122 break;
1124 case 'V':
1125 return &ffi_type_void;
1126 break;
1129 throw_internal_error ("unknown type in signature");
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 */
1136 int
1137 _Jv_count_arguments (_Jv_Utf8Const *signature,
1138 jboolean staticp)
1140 unsigned char *ptr = (unsigned char*) signature->chars();
1141 int arg_count = staticp ? 0 : 1;
1143 /* first, count number of arguments */
1145 // skip '('
1146 ptr++;
1148 // count args
1149 while (*ptr != ')')
1151 ptr = skip_one_type (ptr);
1152 arg_count += 1;
1155 return arg_count;
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.
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)
1171 unsigned char *ptr = (unsigned char*) signature->chars();
1173 int arg_index = 0; // arg number
1174 int item_count = 0; // stack-item count
1176 // setup receiver
1177 if (!staticp)
1179 arg_types[arg_index++] = &ffi_type_pointer;
1180 item_count += 1;
1183 // skip '('
1184 ptr++;
1186 // assign arg types
1187 while (*ptr != ')')
1189 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1191 if (*ptr == 'J' || *ptr == 'D')
1192 item_count += 2;
1193 else
1194 item_count += 1;
1196 ptr = skip_one_type (ptr);
1199 // skip ')'
1200 ptr++;
1201 ffi_type *rtype = get_ffi_type_from_signature (ptr);
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");
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");
1211 if (rtype_p != NULL)
1212 *rtype_p = rtype;
1214 return item_count;
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
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. */
1230 typedef struct {
1231 ffi_raw_closure closure;
1232 ffi_cif cif;
1233 ffi_type *arg_types[0];
1234 } ncode_closure;
1236 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1238 void *
1239 _Jv_InterpMethod::ncode ()
1241 using namespace java::lang::reflect;
1243 if (self->ncode != 0)
1244 return self->ncode;
1246 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1247 int arg_count = _Jv_count_arguments (self->signature, staticp);
1249 ncode_closure *closure =
1250 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1251 + arg_count * sizeof (ffi_type*));
1253 init_cif (self->signature,
1254 arg_count,
1255 staticp,
1256 &closure->cif,
1257 &closure->arg_types[0],
1258 NULL);
1260 ffi_closure_fun fun;
1262 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1264 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1266 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1268 if (staticp)
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;
1275 else
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;
1283 else
1285 if (staticp)
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;
1292 else
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;
1301 FFI_PREP_RAW_CLOSURE (&closure->closure,
1302 &closure->cif,
1303 fun,
1304 (void*)this);
1306 self->ncode = (void*)closure;
1307 return self->ncode;
1310 /* Find the index of the given insn in the array of insn slots
1311 for this method. Returns -1 if not found. */
1312 jlong
1313 _Jv_InterpMethod::insn_index (pc_t pc)
1315 jlong left = 0;
1316 #ifdef DIRECT_THREADED
1317 jlong right = number_insn_slots;
1318 pc_t insns = prepared;
1319 #else
1320 jlong right = code_length;
1321 pc_t insns = bytecode ();
1322 #endif
1324 while (right >= 0)
1326 jlong mid = (left + right) / 2;
1327 if (&insns[mid] == pc)
1328 return mid;
1330 if (pc < &insns[mid])
1331 right = mid - 1;
1332 else
1333 left = mid + 1;
1336 return -1;
1339 void
1340 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1341 jintArray& line_numbers,
1342 jlongArray& code_indices)
1344 #ifdef DIRECT_THREADED
1345 /* For the DIRECT_THREADED case, if the method has not yet been
1346 * compiled, the linetable will change to insn slots instead of
1347 * bytecode PCs. It is probably easiest, in this case, to simply
1348 * compile the method and guarantee that we are using insn
1349 * slots.
1351 _Jv_CompileMethod (this);
1353 if (line_table_len > 0)
1355 start = 0;
1356 end = number_insn_slots;
1357 line_numbers = JvNewIntArray (line_table_len);
1358 code_indices = JvNewLongArray (line_table_len);
1360 jint* lines = elements (line_numbers);
1361 jlong* indices = elements (code_indices);
1362 for (int i = 0; i < line_table_len; ++i)
1364 lines[i] = line_table[i].line;
1365 indices[i] = insn_index (line_table[i].pc);
1368 #else // !DIRECT_THREADED
1369 if (line_table_len > 0)
1371 start = 0;
1372 end = code_length;
1373 line_numbers = JvNewIntArray (line_table_len);
1374 code_indices = JvNewLongArray (line_table_len);
1376 jint* lines = elements (line_numbers);
1377 jlong* indices = elements (code_indices);
1378 for (int i = 0; i < line_table_len; ++i)
1380 lines[i] = line_table[i].line;
1381 indices[i] = (jlong) line_table[i].bytecode_pc;
1384 #endif // !DIRECT_THREADED
1387 void *
1388 _Jv_JNIMethod::ncode ()
1390 using namespace java::lang::reflect;
1392 if (self->ncode != 0)
1393 return self->ncode;
1395 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1396 int arg_count = _Jv_count_arguments (self->signature, staticp);
1398 ncode_closure *closure =
1399 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1400 + arg_count * sizeof (ffi_type*));
1402 ffi_type *rtype;
1403 init_cif (self->signature,
1404 arg_count,
1405 staticp,
1406 &closure->cif,
1407 &closure->arg_types[0],
1408 &rtype);
1410 ffi_closure_fun fun;
1412 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1414 // Initialize the argument types and CIF that represent the actual
1415 // underlying JNI function.
1416 int extra_args = 1;
1417 if ((self->accflags & Modifier::STATIC))
1418 ++extra_args;
1419 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1420 * sizeof (ffi_type *));
1421 int offset = 0;
1422 jni_arg_types[offset++] = &ffi_type_pointer;
1423 if ((self->accflags & Modifier::STATIC))
1424 jni_arg_types[offset++] = &ffi_type_pointer;
1425 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1426 arg_count * sizeof (ffi_type *));
1428 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1429 extra_args + arg_count, rtype,
1430 jni_arg_types) != FFI_OK)
1431 throw_internal_error ("ffi_prep_cif failed for JNI function");
1433 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1435 // FIXME: for now we assume that all native methods for
1436 // interpreted code use JNI.
1437 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1439 FFI_PREP_RAW_CLOSURE (&closure->closure,
1440 &closure->cif,
1441 fun,
1442 (void*) this);
1444 self->ncode = (void *) closure;
1445 return self->ncode;
1448 static void
1449 throw_class_format_error (jstring msg)
1451 throw (msg
1452 ? new java::lang::ClassFormatError (msg)
1453 : new java::lang::ClassFormatError);
1456 static void
1457 throw_class_format_error (const char *msg)
1459 throw_class_format_error (JvNewStringLatin1 (msg));
1464 void
1465 _Jv_InterpreterEngine::do_verify (jclass klass)
1467 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1468 for (int i = 0; i < klass->method_count; i++)
1470 using namespace java::lang::reflect;
1471 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1472 _Jv_ushort accflags = klass->methods[i].accflags;
1473 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1475 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1476 _Jv_VerifyMethod (im);
1481 void
1482 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1484 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1485 for (int i = 0; i < klass->method_count; i++)
1487 // Just skip abstract methods. This is particularly important
1488 // because we don't resize the interpreted_methods array when
1489 // miranda methods are added to it.
1490 if ((klass->methods[i].accflags
1491 & java::lang::reflect::Modifier::ABSTRACT)
1492 != 0)
1493 continue;
1495 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1497 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1498 != 0)
1500 // You might think we could use a virtual `ncode' method in
1501 // the _Jv_MethodBase and unify the native and non-native
1502 // cases. Well, we can't, because we don't allocate these
1503 // objects using `new', and thus they don't get a vtable.
1504 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1505 klass->methods[i].ncode = jnim->ncode ();
1507 else if (imeth != 0) // it could be abstract
1509 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1510 klass->methods[i].ncode = im->ncode ();
1515 void
1516 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1517 int pointer_size,
1518 int other_size)
1520 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1522 // Splitting the allocations here lets us scan reference fields and
1523 // avoid scanning non-reference fields. How reference fields are
1524 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1525 // means that this memory will be scanned conservatively (same
1526 // difference, since we know all the contents here are pointers).
1527 // Then we put pointers into this memory into the 'fields'
1528 // structure. Most of these are interior pointers, which is ok (but
1529 // even so the pointer to the first reference field will be used and
1530 // that is not an interior pointer). The 'fields' array is also
1531 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1532 // be scanned. A pointer to this array is held by Class and thus
1533 // seen by the collector.
1534 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1535 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1537 for (int i = 0; i < klass->field_count; i++)
1539 _Jv_Field *field = &klass->fields[i];
1541 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1542 continue;
1544 char *base = field->isRef() ? reference_fields : non_reference_fields;
1545 field->u.addr = base + field->u.boffset;
1547 if (iclass->field_initializers[i] != 0)
1549 _Jv_Linker::resolve_field (field, klass->loader);
1550 _Jv_InitField (0, klass, i);
1554 // Now we don't need the field_initializers anymore, so let the
1555 // collector get rid of it.
1556 iclass->field_initializers = 0;
1559 _Jv_ResolvedMethod *
1560 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1561 jboolean staticp)
1563 int arg_count = _Jv_count_arguments (method->signature, staticp);
1565 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1566 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1567 + arg_count*sizeof (ffi_type*));
1569 result->stack_item_count
1570 = init_cif (method->signature,
1571 arg_count,
1572 staticp,
1573 &result->cif,
1574 &result->arg_types[0],
1575 NULL);
1577 result->method = method;
1578 result->klass = klass;
1580 return result;
1583 void
1584 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1586 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1587 for (int i = 0; i < klass->method_count; i++)
1589 // Just skip abstract methods. This is particularly important
1590 // because we don't resize the interpreted_methods array when
1591 // miranda methods are added to it.
1592 if ((klass->methods[i].accflags
1593 & java::lang::reflect::Modifier::ABSTRACT)
1594 != 0)
1595 continue;
1596 // Miranda method additions mean that the `methods' array moves.
1597 // We cache a pointer into this array, so we have to update.
1598 iclass->interpreted_methods[i]->self = &klass->methods[i];
1602 #ifdef DIRECT_THREADED
1603 void
1604 _Jv_CompileMethod (_Jv_InterpMethod* method)
1606 if (method->prepared == NULL)
1607 _Jv_InterpMethod::run (NULL, NULL, method);
1609 #endif // DIRECT_THREADED
1611 #endif // INTERPRETER