1 // interpret-run.cc - Code to interpret bytecode
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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
11 /* This file is meant only to be included in interpret.cc, it should not be
12 * compiled directly. */
14 using namespace java::lang::reflect;
18 // FRAME_DESC registers this particular invocation as the top-most
19 // interpreter frame. This lets the stack tracing code (for
20 // Throwable) print information about the method being interpreted
21 // rather than about the interpreter itself. FRAME_DESC has a
22 // destructor so it cleans up automatically when the interpreter
24 java::lang::Thread *thread = java::lang::Thread::currentThread();
27 _Jv_InterpFrame frame_desc (meth, thread, NULL, &pc);
29 _Jv_InterpFrame frame_desc (meth, thread);
32 _Jv_word stack[meth->max_stack];
35 _Jv_word locals[meth->max_locals];
38 // This is the information needed to get and set local variables with
39 // proper type checking.
40 frame_desc.locals = locals;
41 char locals_type[meth->max_locals];
42 frame_desc.locals_type = locals_type;
44 // Set all slots as invalid until they are written to.
45 memset (locals_type, 'x', meth->max_locals);
47 // We need to set the local variable types for the method arguments since
48 // they are valid at invocation.
50 _Jv_Method *method = meth->get_method ();
53 // If the method is non-static, we need to set the type for the "this" pointer.
54 if ((method->accflags & java::lang::reflect::Modifier::STATIC) == 0)
58 // Set the "this" pointer for this frame.
59 _Jv_word *this_ptr = reinterpret_cast<_Jv_word *> (args);
60 frame_desc.obj_ptr = this_ptr[0].o;
63 frame_desc.locals_type[0] = 'o';
67 // Now parse the method signature to set the types of the other arguments.
68 int sig_len = method->signature->len ();
69 char *signature = method->signature->chars ();
70 for (int i = 1; signature[i] != ')' && i <= sig_len; i++)
72 if (signature[i] == 'Z' || signature[i] == 'B' || signature[i] == 'C'
73 || signature[i] == 'S' || signature[i] == 'I')
75 frame_desc.locals_type[type_ctr] = 'i';
79 else if (signature[i] == 'F')
81 frame_desc.locals_type[type_ctr] = 'f';
85 else if (signature[i] == 'J')
87 frame_desc.locals_type[type_ctr] = 'l';
88 frame_desc.locals_type[type_ctr+1] = 'x';
92 else if (signature[i] == 'D')
94 frame_desc.locals_type[type_ctr] = 'd';
95 frame_desc.locals_type[type_ctr+1] = 'x';
99 else if (signature[i] == 'L')
101 frame_desc.locals_type[type_ctr] = 'o';
103 while (signature[i] != ';')
107 else if (signature[i] == '[')
109 frame_desc.locals_type[type_ctr] = 'o';
112 // Ignore multi-dimensional arrays.
113 while (signature[i] == '[')
116 // Check for an object array
117 if (signature[i] == 'L')
119 while (signature[i] != ';')
127 #define INSN_LABEL(op) &&insn_##op
129 static const void *const insn_target[] =
132 INSN_LABEL(aconst_null),
133 INSN_LABEL(iconst_m1),
134 INSN_LABEL(iconst_0),
135 INSN_LABEL(iconst_1),
136 INSN_LABEL(iconst_2),
137 INSN_LABEL(iconst_3),
138 INSN_LABEL(iconst_4),
139 INSN_LABEL(iconst_5),
140 INSN_LABEL(lconst_0),
141 INSN_LABEL(lconst_1),
142 INSN_LABEL(fconst_0),
143 INSN_LABEL(fconst_1),
144 INSN_LABEL(fconst_2),
145 INSN_LABEL(dconst_0),
146 INSN_LABEL(dconst_1),
190 INSN_LABEL(istore_0),
191 INSN_LABEL(istore_1),
192 INSN_LABEL(istore_2),
193 INSN_LABEL(istore_3),
194 INSN_LABEL(lstore_0),
195 INSN_LABEL(lstore_1),
196 INSN_LABEL(lstore_2),
197 INSN_LABEL(lstore_3),
198 INSN_LABEL(fstore_0),
199 INSN_LABEL(fstore_1),
200 INSN_LABEL(fstore_2),
201 INSN_LABEL(fstore_3),
202 INSN_LABEL(dstore_0),
203 INSN_LABEL(dstore_1),
204 INSN_LABEL(dstore_2),
205 INSN_LABEL(dstore_3),
206 INSN_LABEL(astore_0),
207 INSN_LABEL(astore_1),
208 INSN_LABEL(astore_2),
209 INSN_LABEL(astore_3),
290 INSN_LABEL(if_icmpeq),
291 INSN_LABEL(if_icmpne),
292 INSN_LABEL(if_icmplt),
293 INSN_LABEL(if_icmpge),
294 INSN_LABEL(if_icmpgt),
295 INSN_LABEL(if_icmple),
296 INSN_LABEL(if_acmpeq),
297 INSN_LABEL(if_acmpne),
301 INSN_LABEL(tableswitch),
302 INSN_LABEL(lookupswitch),
309 INSN_LABEL(getstatic),
310 INSN_LABEL(putstatic),
311 INSN_LABEL(getfield),
312 INSN_LABEL(putfield),
313 INSN_LABEL(invokevirtual),
314 INSN_LABEL(invokespecial),
315 INSN_LABEL(invokestatic),
316 INSN_LABEL(invokeinterface),
317 INSN_LABEL(breakpoint),
319 INSN_LABEL(newarray),
320 INSN_LABEL(anewarray),
321 INSN_LABEL(arraylength),
323 INSN_LABEL(checkcast),
324 INSN_LABEL(instanceof),
325 INSN_LABEL(monitorenter),
326 INSN_LABEL(monitorexit),
327 #ifdef DIRECT_THREADED
332 INSN_LABEL(multianewarray),
334 INSN_LABEL(ifnonnull),
337 #ifdef DIRECT_THREADED
338 INSN_LABEL (ldc_class)
344 #ifdef DIRECT_THREADED
352 if (JVMTI_REQUESTED_EVENT (SingleStep)) \
354 JNIEnv *env = _Jv_GetCurrentJNIEnv (); \
355 jmethodID method = meth->self; \
356 jlocation loc = meth->insn_index (insn); \
357 _Jv_JVMTI_PostEvent (JVMTI_EVENT_SINGLE_STEP, thread, \
360 goto *(insn->insn); \
365 #define REWRITE_INSN(INSN,SLOT,VALUE) \
367 if (pc[-2].insn == breakpoint_insn->insn) \
369 using namespace ::gnu::gcj::jvmti; \
370 jlocation location = meth->insn_index (pc - 2); \
371 _Jv_RewriteBreakpointInsn (meth->self, location, (pc_t) INSN); \
374 pc[-2].insn = INSN; \
376 pc[-1].SLOT = VALUE; \
380 #undef INTERP_REPORT_EXCEPTION
381 #define INTERP_REPORT_EXCEPTION(Jthrowable) REPORT_EXCEPTION (Jthrowable)
384 #define NEXT_INSN goto *((pc++)->insn)
385 #define REWRITE_INSN(INSN,SLOT,VALUE) \
387 pc[-2].insn = INSN; \
388 pc[-1].SLOT = VALUE; \
392 #undef INTERP_REPORT_EXCEPTION
393 #define INTERP_REPORT_EXCEPTION(Jthrowable) /* not needed when not debugging */
396 #define INTVAL() ((pc++)->int_val)
397 #define AVAL() ((pc++)->datum)
399 #define GET1S() INTVAL ()
400 #define GET2S() INTVAL ()
401 #define GET1U() INTVAL ()
402 #define GET2U() INTVAL ()
403 #define AVAL1U() AVAL ()
404 #define AVAL2U() AVAL ()
405 #define AVAL2UP() AVAL ()
406 #define SKIP_GOTO ++pc
407 #define GOTO_VAL() (insn_slot *) pc->datum
408 #define PCVAL(unionval) unionval.p
409 #define AMPAMP(label) &&label
411 // Compile if we must. NOTE: Double-check locking.
412 if (meth->prepared == NULL)
414 _Jv_MutexLock (&compile_mutex);
415 if (meth->prepared == NULL)
416 meth->compile (insn_target);
417 _Jv_MutexUnlock (&compile_mutex);
420 // If we're only compiling, stop here
424 pc = (insn_slot *) meth->prepared;
432 if (JVMTI_REQUESTED_EVENT (SingleStep)) \
434 JNIEnv *env = _Jv_GetCurrentJNIEnv (); \
435 jmethodID method = meth->self; \
436 jlocation loc = meth->insn_index (pc); \
437 _Jv_JVMTI_PostEvent (JVMTI_EVENT_SINGLE_STEP, thread, \
440 goto *(insn_target[*pc++])
442 #define NEXT_INSN goto *(insn_target[*pc++])
445 #define GET1S() get1s (pc++)
446 #define GET2S() (pc += 2, get2s (pc- 2))
447 #define GET1U() get1u (pc++)
448 #define GET2U() (pc += 2, get2u (pc - 2))
449 // Note that these could be more efficient when not handling 'ldc
452 ({ int index = get1u (pc++); \
453 _Jv_Linker::resolve_pool_entry (meth->defining_class, index).o; })
455 ({ int index = get2u (pc); pc += 2; \
456 _Jv_Linker::resolve_pool_entry (meth->defining_class, index).o; })
457 // Note that we don't need to resolve the pool entry here as class
458 // constants are never wide.
459 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
460 #define SKIP_GOTO pc += 2
461 #define GOTO_VAL() pc - 1 + get2s (pc)
462 #define PCVAL(unionval) unionval.i
463 #define AMPAMP(label) NULL
465 pc = meth->bytecode ();
467 #endif /* DIRECT_THREADED */
469 #define TAKE_GOTO pc = GOTO_VAL ()
471 /* Go straight at it! the ffi raw format matches the internal
472 stack representation exactly. At least, that's the idea.
474 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
476 _Jv_word *pool_data = meth->defining_class->constants.data;
478 /* These three are temporaries for common code used by several
481 _Jv_ResolvedMethod* rmeth;
486 // We keep nop around. It is used if we're interpreting the
487 // bytecodes and not doing direct threading.
491 /* The first few instructions here are ordered according to their
492 frequency, in the hope that this will improve code locality a
495 insn_aload_0: // 0x2a
503 insn_iload_1: // 0x1b
507 insn_invokevirtual: // 0xb6
510 int index = GET2U ();
512 /* _Jv_Linker::resolve_pool_entry returns immediately if the
513 * value already is resolved. If we want to clutter up the
514 * code here to gain a little performance, then we can check
515 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
516 * directly. For now, I don't think it is worth it. */
518 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
521 sp -= rmeth->stack_item_count;
523 if (rmeth->method->accflags & Modifier::FINAL)
525 // We can't rely on NULLCHECK working if the method is final.
527 throw_null_pointer_exception ();
529 // Final methods might not appear in the vtable.
530 fun = (void (*)()) rmeth->method->ncode;
535 jobject rcv = sp[0].o;
536 _Jv_VTable *table = *(_Jv_VTable**) rcv;
537 fun = (void (*)()) table->get_method (rmeth->method->index);
540 #ifdef DIRECT_THREADED
541 // Rewrite instruction so that we use a faster pre-resolved
543 REWRITE_INSN (&&invokevirtual_resolved, datum, rmeth);
544 #endif /* DIRECT_THREADED */
548 #ifdef DIRECT_THREADED
549 invokevirtual_resolved:
552 rmeth = (_Jv_ResolvedMethod *) AVAL ();
553 sp -= rmeth->stack_item_count;
555 if (rmeth->method->accflags & Modifier::FINAL)
557 // We can't rely on NULLCHECK working if the method is final.
559 throw_null_pointer_exception ();
561 // Final methods might not appear in the vtable.
562 fun = (void (*)()) rmeth->method->ncode;
566 jobject rcv = sp[0].o;
567 _Jv_VTable *table = *(_Jv_VTable**) rcv;
568 fun = (void (*)()) table->get_method (rmeth->method->index);
572 #endif /* DIRECT_THREADED */
576 /* here goes the magic again... */
577 ffi_cif *cif = &rmeth->cif;
578 ffi_raw *raw = (ffi_raw*) sp;
582 #if FFI_NATIVE_RAW_API
583 /* We assume that this is only implemented if it's correct */
584 /* to use it here. On a 64 bit machine, it never is. */
585 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
587 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
590 int rtype = cif->rtype->type;
592 /* the likelyhood of object, int, or void return is very high,
593 * so those are checked before the switch */
594 if (rtype == FFI_TYPE_POINTER)
596 PUSHA (rvalue.object_value);
598 else if (rtype == FFI_TYPE_SINT32)
600 PUSHI (rvalue.int_value);
602 else if (rtype == FFI_TYPE_VOID)
611 PUSHI ((jbyte)(rvalue.int_value & 0xff));
614 case FFI_TYPE_SINT16:
615 PUSHI ((jshort)(rvalue.int_value & 0xffff));
618 case FFI_TYPE_UINT16:
619 PUSHI (rvalue.int_value & 0xffff);
623 PUSHF (rvalue.float_value);
626 case FFI_TYPE_DOUBLE:
627 PUSHD (rvalue.double_value);
630 case FFI_TYPE_SINT64:
631 PUSHL (rvalue.long_value);
635 throw_internal_error ("unknown return type in invokeXXX");
702 // For direct threaded, bipush and sipush are the same.
703 #ifndef DIRECT_THREADED
706 #endif /* DIRECT_THREADED */
712 // For direct threaded, ldc and ldc_w are the same.
713 #ifndef DIRECT_THREADED
714 PUSHA ((jobject) AVAL1U ());
716 #endif /* DIRECT_THREADED */
718 PUSHA ((jobject) AVAL2U ());
721 #ifdef DIRECT_THREADED
722 // For direct threaded we have a separate 'ldc class' operation.
726 // We could rewrite the instruction at this point.
727 int index = INTVAL ();
728 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
733 #endif /* DIRECT_THREADED */
737 void *where = AVAL2UP ();
738 memcpy (sp, where, 2*sizeof (_Jv_word));
834 jintArray arr = (jintArray) POPA();
835 NULLARRAYCHECK (arr);
836 ARRAYBOUNDSCHECK (arr, index);
837 PUSHI( elements(arr)[index] );
844 jlongArray arr = (jlongArray) POPA();
845 NULLARRAYCHECK (arr);
846 ARRAYBOUNDSCHECK (arr, index);
847 PUSHL( elements(arr)[index] );
854 jfloatArray arr = (jfloatArray) POPA();
855 NULLARRAYCHECK (arr);
856 ARRAYBOUNDSCHECK (arr, index);
857 PUSHF( elements(arr)[index] );
864 jdoubleArray arr = (jdoubleArray) POPA();
865 NULLARRAYCHECK (arr);
866 ARRAYBOUNDSCHECK (arr, index);
867 PUSHD( elements(arr)[index] );
874 jobjectArray arr = (jobjectArray) POPA();
875 NULLARRAYCHECK (arr);
876 ARRAYBOUNDSCHECK (arr, index);
877 PUSHA( elements(arr)[index] );
884 jbyteArray arr = (jbyteArray) POPA();
885 NULLARRAYCHECK (arr);
886 ARRAYBOUNDSCHECK (arr, index);
887 PUSHI( elements(arr)[index] );
894 jcharArray arr = (jcharArray) POPA();
895 NULLARRAYCHECK (arr);
896 ARRAYBOUNDSCHECK (arr, index);
897 PUSHI( elements(arr)[index] );
904 jshortArray arr = (jshortArray) POPA();
905 NULLARRAYCHECK (arr);
906 ARRAYBOUNDSCHECK (arr, index);
907 PUSHI( elements(arr)[index] );
1013 jint value = POPI();
1014 jint index = POPI();
1015 jintArray arr = (jintArray) POPA();
1016 NULLARRAYCHECK (arr);
1017 ARRAYBOUNDSCHECK (arr, index);
1018 elements(arr)[index] = value;
1024 jlong value = POPL();
1025 jint index = POPI();
1026 jlongArray arr = (jlongArray) POPA();
1027 NULLARRAYCHECK (arr);
1028 ARRAYBOUNDSCHECK (arr, index);
1029 elements(arr)[index] = value;
1035 jfloat value = POPF();
1036 jint index = POPI();
1037 jfloatArray arr = (jfloatArray) POPA();
1038 NULLARRAYCHECK (arr);
1039 ARRAYBOUNDSCHECK (arr, index);
1040 elements(arr)[index] = value;
1046 jdouble value = POPD();
1047 jint index = POPI();
1048 jdoubleArray arr = (jdoubleArray) POPA();
1049 NULLARRAYCHECK (arr);
1050 ARRAYBOUNDSCHECK (arr, index);
1051 elements(arr)[index] = value;
1057 jobject value = POPA();
1058 jint index = POPI();
1059 jobjectArray arr = (jobjectArray) POPA();
1060 NULLARRAYCHECK (arr);
1061 ARRAYBOUNDSCHECK (arr, index);
1062 _Jv_CheckArrayStore (arr, value);
1063 elements(arr)[index] = value;
1069 jbyte value = (jbyte) POPI();
1070 jint index = POPI();
1071 jbyteArray arr = (jbyteArray) POPA();
1072 NULLARRAYCHECK (arr);
1073 ARRAYBOUNDSCHECK (arr, index);
1074 elements(arr)[index] = value;
1080 jchar value = (jchar) POPI();
1081 jint index = POPI();
1082 jcharArray arr = (jcharArray) POPA();
1083 NULLARRAYCHECK (arr);
1084 ARRAYBOUNDSCHECK (arr, index);
1085 elements(arr)[index] = value;
1091 jshort value = (jshort) POPI();
1092 jint index = POPI();
1093 jshortArray arr = (jshortArray) POPA();
1094 NULLARRAYCHECK (arr);
1095 ARRAYBOUNDSCHECK (arr, index);
1096 elements(arr)[index] = value;
1114 dupx (sp, 1, 1); sp+=1;
1118 dupx (sp, 1, 2); sp+=1;
1128 dupx (sp, 2, 1); sp+=2;
1132 dupx (sp, 2, 2); sp+=2;
1137 jobject tmp1 = POPA();
1138 jobject tmp2 = POPA();
1195 jint value2 = POPI();
1196 jint value1 = POPI();
1197 jint res = _Jv_divI (value1, value2);
1205 jlong value2 = POPL();
1206 jlong value1 = POPL();
1207 jlong res = _Jv_divJ (value1, value2);
1214 jfloat value2 = POPF();
1215 jfloat value1 = POPF();
1216 jfloat res = value1 / value2;
1223 jdouble value2 = POPD();
1224 jdouble value1 = POPD();
1225 jdouble res = value1 / value2;
1233 jint value2 = POPI();
1234 jint value1 = POPI();
1235 jint res = _Jv_remI (value1, value2);
1243 jlong value2 = POPL();
1244 jlong value1 = POPL();
1245 jlong res = _Jv_remJ (value1, value2);
1252 jfloat value2 = POPF();
1253 jfloat value1 = POPF();
1254 jfloat res = __ieee754_fmod (value1, value2);
1261 jdouble value2 = POPD();
1262 jdouble value1 = POPD();
1263 jdouble res = __ieee754_fmod (value1, value2);
1270 jint value = POPI();
1277 jlong value = POPL();
1284 jfloat value = POPF();
1291 jdouble value = POPD();
1298 jint shift = (POPI() & 0x1f);
1299 jint value = POPI();
1300 PUSHI (value << shift);
1306 jint shift = (POPI() & 0x3f);
1307 jlong value = POPL();
1308 PUSHL (value << shift);
1314 jint shift = (POPI() & 0x1f);
1315 jint value = POPI();
1316 PUSHI (value >> shift);
1322 jint shift = (POPI() & 0x3f);
1323 jlong value = POPL();
1324 PUSHL (value >> shift);
1330 jint shift = (POPI() & 0x1f);
1331 _Jv_uint value = (_Jv_uint) POPI();
1332 PUSHI ((jint) (value >> shift));
1338 jint shift = (POPI() & 0x3f);
1339 _Jv_ulong value = (_Jv_ulong) POPL();
1340 PUSHL ((jlong) (value >> shift));
1370 jint index = GET1U ();
1371 jint amount = GET1S ();
1372 locals[index].i += amount;
1377 {jlong value = POPI(); PUSHL (value);}
1381 {jfloat value = POPI(); PUSHF (value);}
1385 {jdouble value = POPI(); PUSHD (value);}
1389 {jint value = POPL(); PUSHI (value);}
1393 {jfloat value = POPL(); PUSHF (value);}
1397 {jdouble value = POPL(); PUSHD (value);}
1402 using namespace java::lang;
1403 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1410 using namespace java::lang;
1411 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
1417 { jdouble value = POPF (); PUSHD(value); }
1422 using namespace java::lang;
1423 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1430 using namespace java::lang;
1431 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
1437 { jfloat value = POPD (); PUSHF(value); }
1441 { jbyte value = POPI (); PUSHI(value); }
1445 { jchar value = POPI (); PUSHI(value); }
1449 { jshort value = POPI (); PUSHI(value); }
1454 jlong value2 = POPL ();
1455 jlong value1 = POPL ();
1456 if (value1 > value2)
1458 else if (value1 == value2)
1474 jfloat value2 = POPF ();
1475 jfloat value1 = POPF ();
1476 if (value1 > value2)
1478 else if (value1 == value2)
1480 else if (value1 < value2)
1496 jdouble value2 = POPD ();
1497 jdouble value1 = POPD ();
1498 if (value1 > value2)
1500 else if (value1 == value2)
1502 else if (value1 < value2)
1565 jint value2 = POPI();
1566 jint value1 = POPI();
1567 if (value1 == value2)
1576 jint value2 = POPI();
1577 jint value1 = POPI();
1578 if (value1 != value2)
1587 jint value2 = POPI();
1588 jint value1 = POPI();
1589 if (value1 < value2)
1598 jint value2 = POPI();
1599 jint value1 = POPI();
1600 if (value1 >= value2)
1609 jint value2 = POPI();
1610 jint value1 = POPI();
1611 if (value1 > value2)
1620 jint value2 = POPI();
1621 jint value1 = POPI();
1622 if (value1 <= value2)
1631 jobject value2 = POPA();
1632 jobject value1 = POPA();
1633 if (value1 == value2)
1642 jobject value2 = POPA();
1643 jobject value1 = POPA();
1644 if (value1 != value2)
1652 #ifndef DIRECT_THREADED
1653 // For direct threaded, goto and goto_w are the same.
1654 pc = pc - 1 + get4 (pc);
1656 #endif /* DIRECT_THREADED */
1662 #ifndef DIRECT_THREADED
1663 // For direct threaded, jsr and jsr_w are the same.
1665 pc_t next = pc - 1 + get4 (pc);
1667 PUSHA ((jobject) pc);
1671 #endif /* DIRECT_THREADED */
1674 pc_t next = GOTO_VAL();
1676 PUSHA ((jobject) pc);
1683 jint index = GET1U ();
1684 pc = (pc_t) PEEKA (index);
1690 #ifdef DIRECT_THREADED
1691 void *def = (pc++)->datum;
1695 jint low = INTVAL ();
1696 jint high = INTVAL ();
1698 if (index < low || index > high)
1699 pc = (insn_slot *) def;
1701 pc = (insn_slot *) ((pc + index - low)->datum);
1703 pc_t base_pc = pc - 1;
1704 int index = POPI ();
1706 pc_t base = (pc_t) meth->bytecode ();
1707 while ((pc - base) % 4 != 0)
1710 jint def = get4 (pc);
1711 jint low = get4 (pc + 4);
1712 jint high = get4 (pc + 8);
1713 if (index < low || index > high)
1716 pc = base_pc + get4 (pc + 4 * (index - low + 3));
1717 #endif /* DIRECT_THREADED */
1723 #ifdef DIRECT_THREADED
1724 void *def = (pc++)->insn;
1728 jint npairs = INTVAL ();
1730 int max = npairs - 1;
1733 // Simple binary search...
1736 int half = (min + max) / 2;
1737 int match = pc[2 * half].int_val;
1742 pc = (insn_slot *) pc[2 * half + 1].datum;
1745 else if (index < match)
1746 // We can use HALF - 1 here because we check again on
1750 // We can use HALF + 1 here because we check again on
1754 if (index == pc[2 * min].int_val)
1755 pc = (insn_slot *) pc[2 * min + 1].datum;
1757 pc = (insn_slot *) def;
1759 unsigned char *base_pc = pc-1;
1762 unsigned char* base = meth->bytecode ();
1763 while ((pc-base) % 4 != 0)
1766 jint def = get4 (pc);
1767 jint npairs = get4 (pc+4);
1772 // Simple binary search...
1775 int half = (min+max)/2;
1776 int match = get4 (pc+ 4*(2 + 2*half));
1780 else if (index < match)
1781 // We can use HALF - 1 here because we check again on
1785 // We can use HALF + 1 here because we check again on
1790 if (index == get4 (pc+ 4*(2 + 2*min)))
1791 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
1794 #endif /* DIRECT_THREADED */
1799 *(jobject *) retp = POPA ();
1803 *(jlong *) retp = POPL ();
1807 *(jfloat *) retp = POPF ();
1811 *(jdouble *) retp = POPD ();
1815 *(jint *) retp = POPI ();
1823 jint fieldref_index = GET2U ();
1824 SAVE_PC(); // Constant pool resolution could throw.
1825 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
1826 _Jv_Field *field = pool_data[fieldref_index].field;
1828 if ((field->flags & Modifier::STATIC) == 0)
1829 throw_incompatible_class_change_error
1830 (JvNewStringLatin1 ("field no longer static"));
1832 jclass type = field->type;
1834 // We rewrite the instruction once we discover what it refers
1836 void *newinsn = NULL;
1837 if (type->isPrimitive ())
1839 switch (type->size_in_bytes)
1842 PUSHI (*field->u.byte_addr);
1843 newinsn = AMPAMP (getstatic_resolved_1);
1847 if (type == JvPrimClass (char))
1849 PUSHI (*field->u.char_addr);
1850 newinsn = AMPAMP (getstatic_resolved_char);
1854 PUSHI (*field->u.short_addr);
1855 newinsn = AMPAMP (getstatic_resolved_short);
1860 PUSHI(*field->u.int_addr);
1861 newinsn = AMPAMP (getstatic_resolved_4);
1865 PUSHL(*field->u.long_addr);
1866 newinsn = AMPAMP (getstatic_resolved_8);
1872 PUSHA(*field->u.object_addr);
1873 newinsn = AMPAMP (getstatic_resolved_obj);
1876 #ifdef DIRECT_THREADED
1877 REWRITE_INSN (newinsn, datum, field->u.addr);
1878 #endif /* DIRECT_THREADED */
1882 #ifdef DIRECT_THREADED
1883 getstatic_resolved_1:
1884 PUSHI (*(jbyte *) AVAL ());
1887 getstatic_resolved_char:
1888 PUSHI (*(jchar *) AVAL ());
1891 getstatic_resolved_short:
1892 PUSHI (*(jshort *) AVAL ());
1895 getstatic_resolved_4:
1896 PUSHI (*(jint *) AVAL ());
1899 getstatic_resolved_8:
1900 PUSHL (*(jlong *) AVAL ());
1903 getstatic_resolved_obj:
1904 PUSHA (*(jobject *) AVAL ());
1906 #endif /* DIRECT_THREADED */
1911 jint fieldref_index = GET2U ();
1912 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
1913 _Jv_Field *field = pool_data[fieldref_index].field;
1915 if ((field->flags & Modifier::STATIC) != 0)
1916 throw_incompatible_class_change_error
1917 (JvNewStringLatin1 ("field is static"));
1919 jclass type = field->type;
1920 jint field_offset = field->u.boffset;
1922 jobject obj = POPA();
1925 void *newinsn = NULL;
1926 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
1927 if (type->isPrimitive ())
1929 switch (type->size_in_bytes)
1932 PUSHI (val->byte_value);
1933 newinsn = AMPAMP (getfield_resolved_1);
1937 if (type == JvPrimClass (char))
1939 PUSHI (val->char_value);
1940 newinsn = AMPAMP (getfield_resolved_char);
1944 PUSHI (val->short_value);
1945 newinsn = AMPAMP (getfield_resolved_short);
1950 PUSHI (val->int_value);
1951 newinsn = AMPAMP (getfield_resolved_4);
1955 PUSHL (val->long_value);
1956 newinsn = AMPAMP (getfield_resolved_8);
1962 PUSHA (val->object_value);
1963 newinsn = AMPAMP (getfield_resolved_obj);
1966 #ifdef DIRECT_THREADED
1967 REWRITE_INSN (newinsn, int_val, field_offset);
1968 #endif /* DIRECT_THREADED */
1972 #ifdef DIRECT_THREADED
1973 getfield_resolved_1:
1975 char *obj = (char *) POPA ();
1977 PUSHI (*(jbyte *) (obj + INTVAL ()));
1981 getfield_resolved_char:
1983 char *obj = (char *) POPA ();
1985 PUSHI (*(jchar *) (obj + INTVAL ()));
1989 getfield_resolved_short:
1991 char *obj = (char *) POPA ();
1993 PUSHI (*(jshort *) (obj + INTVAL ()));
1997 getfield_resolved_4:
1999 char *obj = (char *) POPA ();
2001 PUSHI (*(jint *) (obj + INTVAL ()));
2005 getfield_resolved_8:
2007 char *obj = (char *) POPA ();
2009 PUSHL (*(jlong *) (obj + INTVAL ()));
2013 getfield_resolved_obj:
2015 char *obj = (char *) POPA ();
2017 PUSHA (*(jobject *) (obj + INTVAL ()));
2020 #endif /* DIRECT_THREADED */
2025 jint fieldref_index = GET2U ();
2026 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2027 _Jv_Field *field = pool_data[fieldref_index].field;
2029 jclass type = field->type;
2031 // ResolvePoolEntry cannot check this
2032 if ((field->flags & Modifier::STATIC) == 0)
2033 throw_incompatible_class_change_error
2034 (JvNewStringLatin1 ("field no longer static"));
2036 void *newinsn = NULL;
2037 if (type->isPrimitive ())
2039 switch (type->size_in_bytes)
2043 jint value = POPI();
2044 *field->u.byte_addr = value;
2045 newinsn = AMPAMP (putstatic_resolved_1);
2051 jint value = POPI();
2052 *field->u.char_addr = value;
2053 newinsn = AMPAMP (putstatic_resolved_2);
2059 jint value = POPI();
2060 *field->u.int_addr = value;
2061 newinsn = AMPAMP (putstatic_resolved_4);
2067 jlong value = POPL();
2068 *field->u.long_addr = value;
2069 newinsn = AMPAMP (putstatic_resolved_8);
2076 jobject value = POPA();
2077 *field->u.object_addr = value;
2078 newinsn = AMPAMP (putstatic_resolved_obj);
2081 #ifdef DIRECT_THREADED
2082 REWRITE_INSN (newinsn, datum, field->u.addr);
2083 #endif /* DIRECT_THREADED */
2087 #ifdef DIRECT_THREADED
2088 putstatic_resolved_1:
2089 *(jbyte *) AVAL () = POPI ();
2092 putstatic_resolved_2:
2093 *(jchar *) AVAL () = POPI ();
2096 putstatic_resolved_4:
2097 *(jint *) AVAL () = POPI ();
2100 putstatic_resolved_8:
2101 *(jlong *) AVAL () = POPL ();
2104 putstatic_resolved_obj:
2105 *(jobject *) AVAL () = POPA ();
2107 #endif /* DIRECT_THREADED */
2112 jint fieldref_index = GET2U ();
2113 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2114 _Jv_Field *field = pool_data[fieldref_index].field;
2116 jclass type = field->type;
2118 if ((field->flags & Modifier::STATIC) != 0)
2119 throw_incompatible_class_change_error
2120 (JvNewStringLatin1 ("field is static"));
2122 jint field_offset = field->u.boffset;
2124 void *newinsn = NULL;
2125 if (type->isPrimitive ())
2127 switch (type->size_in_bytes)
2131 jint value = POPI();
2132 jobject obj = POPA();
2134 *(jbyte*) ((char*)obj + field_offset) = value;
2135 newinsn = AMPAMP (putfield_resolved_1);
2141 jint value = POPI();
2142 jobject obj = POPA();
2144 *(jchar*) ((char*)obj + field_offset) = value;
2145 newinsn = AMPAMP (putfield_resolved_2);
2151 jint value = POPI();
2152 jobject obj = POPA();
2154 *(jint*) ((char*)obj + field_offset) = value;
2155 newinsn = AMPAMP (putfield_resolved_4);
2161 jlong value = POPL();
2162 jobject obj = POPA();
2164 *(jlong*) ((char*)obj + field_offset) = value;
2165 newinsn = AMPAMP (putfield_resolved_8);
2172 jobject value = POPA();
2173 jobject obj = POPA();
2175 *(jobject*) ((char*)obj + field_offset) = value;
2176 newinsn = AMPAMP (putfield_resolved_obj);
2179 #ifdef DIRECT_THREADED
2180 REWRITE_INSN (newinsn, int_val, field_offset);
2181 #endif /* DIRECT_THREADED */
2185 #ifdef DIRECT_THREADED
2186 putfield_resolved_1:
2189 char *obj = (char *) POPA ();
2191 *(jbyte *) (obj + INTVAL ()) = val;
2195 putfield_resolved_2:
2198 char *obj = (char *) POPA ();
2200 *(jchar *) (obj + INTVAL ()) = val;
2204 putfield_resolved_4:
2207 char *obj = (char *) POPA ();
2209 *(jint *) (obj + INTVAL ()) = val;
2213 putfield_resolved_8:
2215 jlong val = POPL ();
2216 char *obj = (char *) POPA ();
2218 *(jlong *) (obj + INTVAL ()) = val;
2222 putfield_resolved_obj:
2224 jobject val = POPA ();
2225 char *obj = (char *) POPA ();
2227 *(jobject *) (obj + INTVAL ()) = val;
2230 #endif /* DIRECT_THREADED */
2235 int index = GET2U ();
2237 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2240 sp -= rmeth->stack_item_count;
2242 // We don't use NULLCHECK here because we can't rely on that
2243 // working for <init>. So instead we do an explicit test.
2247 throw_null_pointer_exception ();
2250 fun = (void (*)()) rmeth->method->ncode;
2252 #ifdef DIRECT_THREADED
2253 // Rewrite instruction so that we use a faster pre-resolved
2255 REWRITE_INSN (&&invokespecial_resolved, datum, rmeth);
2256 #endif /* DIRECT_THREADED */
2258 goto perform_invoke;
2260 #ifdef DIRECT_THREADED
2261 invokespecial_resolved:
2264 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2265 sp -= rmeth->stack_item_count;
2266 // We don't use NULLCHECK here because we can't rely on that
2267 // working for <init>. So instead we do an explicit test.
2270 throw_null_pointer_exception ();
2272 fun = (void (*)()) rmeth->method->ncode;
2274 goto perform_invoke;
2275 #endif /* DIRECT_THREADED */
2280 int index = GET2U ();
2282 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2285 sp -= rmeth->stack_item_count;
2287 fun = (void (*)()) rmeth->method->ncode;
2289 #ifdef DIRECT_THREADED
2290 // Rewrite instruction so that we use a faster pre-resolved
2292 REWRITE_INSN (&&invokestatic_resolved, datum, rmeth);
2293 #endif /* DIRECT_THREADED */
2295 goto perform_invoke;
2297 #ifdef DIRECT_THREADED
2298 invokestatic_resolved:
2301 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2302 sp -= rmeth->stack_item_count;
2303 fun = (void (*)()) rmeth->method->ncode;
2305 goto perform_invoke;
2306 #endif /* DIRECT_THREADED */
2308 insn_invokeinterface:
2311 int index = GET2U ();
2313 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2316 sp -= rmeth->stack_item_count;
2318 jobject rcv = sp[0].o;
2323 _Jv_LookupInterfaceMethod (rcv->getClass (),
2324 rmeth->method->name,
2325 rmeth->method->signature);
2327 #ifdef DIRECT_THREADED
2328 // Rewrite instruction so that we use a faster pre-resolved
2330 REWRITE_INSN (&&invokeinterface_resolved, datum, rmeth);
2332 // Skip dummy bytes.
2334 #endif /* DIRECT_THREADED */
2336 goto perform_invoke;
2338 #ifdef DIRECT_THREADED
2339 invokeinterface_resolved:
2342 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2343 sp -= rmeth->stack_item_count;
2344 jobject rcv = sp[0].o;
2347 _Jv_LookupInterfaceMethod (rcv->getClass (),
2348 rmeth->method->name,
2349 rmeth->method->signature);
2351 goto perform_invoke;
2352 #endif /* DIRECT_THREADED */
2357 int index = GET2U ();
2358 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2360 /* VM spec, section 3.11.5 */
2361 if ((klass->getModifiers() & Modifier::ABSTRACT)
2362 || klass->isInterface())
2364 jthrowable t = new java::lang::InstantiationException;
2365 INTERP_REPORT_EXCEPTION (t);
2368 jobject res = _Jv_AllocObject (klass);
2371 #ifdef DIRECT_THREADED
2372 REWRITE_INSN (&&new_resolved, datum, klass);
2373 #endif /* DIRECT_THREADED */
2377 #ifdef DIRECT_THREADED
2380 jclass klass = (jclass) AVAL ();
2381 jobject res = _Jv_AllocObject (klass);
2385 #endif /* DIRECT_THREADED */
2389 int atype = GET1U ();
2391 jobject result = _Jv_NewArray (atype, size);
2399 int index = GET2U ();
2400 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2403 jobject result = _Jv_NewObjectArray (size, klass, 0);
2406 #ifdef DIRECT_THREADED
2407 REWRITE_INSN (&&anewarray_resolved, datum, klass);
2408 #endif /* DIRECT_THREADED */
2412 #ifdef DIRECT_THREADED
2415 jclass klass = (jclass) AVAL ();
2417 jobject result = _Jv_NewObjectArray (size, klass, 0);
2421 #endif /* DIRECT_THREADED */
2425 __JArray *arr = (__JArray*)POPA();
2426 NULLARRAYCHECK (arr);
2427 PUSHI (arr->length);
2433 jobject value = POPA();
2434 jthrowable t = static_cast<jthrowable> (value);
2435 INTERP_REPORT_EXCEPTION (t);
2443 jobject value = POPA();
2444 jint index = GET2U ();
2445 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2448 value = (jobject) _Jv_CheckCast (to, value);
2452 #ifdef DIRECT_THREADED
2453 REWRITE_INSN (&&checkcast_resolved, datum, to);
2454 #endif /* DIRECT_THREADED */
2458 #ifdef DIRECT_THREADED
2462 jobject value = POPA ();
2463 jclass to = (jclass) AVAL ();
2464 value = (jobject) _Jv_CheckCast (to, value);
2468 #endif /* DIRECT_THREADED */
2473 jobject value = POPA();
2474 jint index = GET2U ();
2475 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2477 PUSHI (to->isInstance (value));
2479 #ifdef DIRECT_THREADED
2480 REWRITE_INSN (&&instanceof_resolved, datum, to);
2481 #endif /* DIRECT_THREADED */
2485 #ifdef DIRECT_THREADED
2486 instanceof_resolved:
2488 jobject value = POPA ();
2489 jclass to = (jclass) AVAL ();
2490 PUSHI (to->isInstance (value));
2493 #endif /* DIRECT_THREADED */
2497 jobject value = POPA();
2499 _Jv_MonitorEnter (value);
2505 jobject value = POPA();
2507 _Jv_MonitorExit (value);
2513 jobject val = POPA();
2523 jobject val = POPA();
2531 insn_multianewarray:
2534 int kind_index = GET2U ();
2538 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2540 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
2542 for (int i = dim - 1; i >= 0; i--)
2547 jobject res = _Jv_NewMultiArray (type,dim, sizes);
2553 #ifndef DIRECT_THREADED
2556 jint the_mod_op = get1u (pc++);
2557 jint wide = get2u (pc); pc += 2;
2602 pc = (unsigned char*) PEEKA (wide);
2607 jint amount = get2s (pc); pc += 2;
2608 jint value = PEEKI (wide);
2609 POKEI (wide, value+amount);
2614 throw_internal_error ("illegal bytecode modified by wide");
2618 #endif /* DIRECT_THREADED */
2622 JvAssert (JVMTI_REQUESTED_EVENT (Breakpoint));
2624 // Send JVMTI notification
2625 using namespace ::java::lang;
2626 jmethodID method = meth->self;
2627 jlocation location = meth->insn_index (pc - 1);
2628 Thread *thread = Thread::currentThread ();
2629 JNIEnv *jni_env = _Jv_GetCurrentJNIEnv ();
2631 // Save the insn here since the breakpoint could be removed
2632 // before the JVMTI notification returns.
2633 using namespace gnu::gcj::jvmti;
2635 = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
2637 JvAssert (bp != NULL);
2638 pc_t opc = reinterpret_cast<pc_t> (bp->getInsn ());
2640 _Jv_JVMTI_PostEvent (JVMTI_EVENT_BREAKPOINT, thread, jni_env,
2643 // Continue execution
2644 #ifdef DIRECT_THREADED
2647 goto *(insn_target[*opc]);
2651 catch (java::lang::Throwable *ex)
2653 // Check if the exception is handled and, if so, set the pc to the start
2654 // of the appropriate catch block.
2655 if (meth->check_handler (&pc, meth, ex))
2658 sp++->o = ex; // Push exception.
2660 if (JVMTI_REQUESTED_EVENT (ExceptionCatch))
2662 using namespace gnu::gcj::jvmti;
2663 jlong catch_meth = reinterpret_cast<jlong> (meth->get_method ());
2664 jlong catch_loc = meth->insn_index (pc);
2665 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION_CATCH, thread,
2666 _Jv_GetCurrentJNIEnv (), catch_meth,
2673 // No handler, so re-throw.