hotspot/src/share/vm/prims/methodHandleWalk.cpp
changeset 9946 b3d5b50e2289
parent 9943 a9aea2b8010f
child 9971 d496ecd7b9de
child 9963 4a783069663c
equal deleted inserted replaced
9943:a9aea2b8010f 9946:b3d5b50e2289
   263         BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
   263         BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
   264         assert(dest == T_OBJECT, "");
   264         assert(dest == T_OBJECT, "");
   265         assert(dest == arg_state->_type, "");
   265         assert(dest == arg_state->_type, "");
   266         ArgToken arg = arg_state->_arg;
   266         ArgToken arg = arg_state->_arg;
   267         ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
   267         ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
   268         assert(arg.token_type() >= tt_symbolic || arg.index() == new_arg.index(), "should be the same index");
   268         assert(!arg.has_index() || arg.index() == new_arg.index(), "should be the same index");
   269         debug_only(dest_klass = (klassOop)badOop);
   269         debug_only(dest_klass = (klassOop)badOop);
   270         break;
   270         break;
   271       }
   271       }
   272 
   272 
   273       case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
   273       case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
   441               !Klass::cast(rklass)->is_interface()) {
   441               !Klass::cast(rklass)->is_interface()) {
   442             // preserve type safety
   442             // preserve type safety
   443             ret = make_conversion(T_OBJECT, rklass, Bytecodes::_checkcast, ret, CHECK_(empty));
   443             ret = make_conversion(T_OBJECT, rklass, Bytecodes::_checkcast, ret, CHECK_(empty));
   444           }
   444           }
   445         }
   445         }
   446         int ret_slot = arg_slot + (retain_original_args ? coll_slots : 0);
   446         if (rtype != T_VOID) {
   447         change_argument(T_VOID, ret_slot, rtype, ret);
   447           int ret_slot = arg_slot + (retain_original_args ? coll_slots : 0);
       
   448           change_argument(T_VOID, ret_slot, rtype, ret);
       
   449         }
   448         break;
   450         break;
   449       }
   451       }
   450 
   452 
   451       case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
   453       case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
   452         klassOop array_klass_oop = NULL;
   454         klassOop array_klass_oop = NULL;
   688 
   690 
   689 
   691 
   690 // -----------------------------------------------------------------------------
   692 // -----------------------------------------------------------------------------
   691 // MethodHandleCompiler
   693 // MethodHandleCompiler
   692 
   694 
   693 MethodHandleCompiler::MethodHandleCompiler(Handle root, methodHandle callee, int invoke_count, bool is_invokedynamic, TRAPS)
   695 MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS)
   694   : MethodHandleWalker(root, is_invokedynamic, THREAD),
   696   : MethodHandleWalker(root, is_invokedynamic, THREAD),
   695     _callee(callee),
       
   696     _invoke_count(invoke_count),
   697     _invoke_count(invoke_count),
   697     _thread(THREAD),
   698     _thread(THREAD),
   698     _bytecode(THREAD, 50),
   699     _bytecode(THREAD, 50),
   699     _constants(THREAD, 10),
   700     _constants(THREAD, 10),
   700     _cur_stack(0),
   701     _cur_stack(0),
   704 
   705 
   705   // Element zero is always the null constant.
   706   // Element zero is always the null constant.
   706   (void) _constants.append(NULL);
   707   (void) _constants.append(NULL);
   707 
   708 
   708   // Set name and signature index.
   709   // Set name and signature index.
   709   _name_index      = cpool_symbol_put(_callee->name());
   710   _name_index      = cpool_symbol_put(name);
   710   _signature_index = cpool_symbol_put(_callee->signature());
   711   _signature_index = cpool_symbol_put(signature);
   711 
   712 
   712   // Get return type klass.
   713   // Get return type klass.
   713   Handle first_mtype(THREAD, chain().method_type_oop());
   714   Handle first_mtype(THREAD, chain().method_type_oop());
   714   // _rklass is NULL for primitives.
   715   // _rklass is NULL for primitives.
   715   _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
   716   _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
   716   if (_rtype == T_ARRAY)  _rtype = T_OBJECT;
   717   if (_rtype == T_ARRAY)  _rtype = T_OBJECT;
   717 
   718 
   718   int params = _callee->size_of_parameters();  // Incoming arguments plus receiver.
   719   ArgumentSizeComputer args(signature);
       
   720   int params = args.size() + 1;  // Incoming arguments plus receiver.
   719   _num_params = for_invokedynamic() ? params - 1 : params;  // XXX Check if callee is static?
   721   _num_params = for_invokedynamic() ? params - 1 : params;  // XXX Check if callee is static?
   720 }
   722 }
   721 
   723 
   722 
   724 
   723 // -----------------------------------------------------------------------------
   725 // -----------------------------------------------------------------------------
   731   (void) walk(CHECK_(nullHandle));
   733   (void) walk(CHECK_(nullHandle));
   732   return get_method_oop(CHECK_(nullHandle));
   734   return get_method_oop(CHECK_(nullHandle));
   733 }
   735 }
   734 
   736 
   735 
   737 
   736 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index) {
   738 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index, int args_size) {
   737   Bytecodes::check(op);  // Are we legal?
   739   Bytecodes::check(op);  // Are we legal?
   738 
   740 
   739   switch (op) {
   741   switch (op) {
   740   // b
   742   // b
   741   case Bytecodes::_aconst_null:
   743   case Bytecodes::_aconst_null:
   807   case Bytecodes::_f2l:
   809   case Bytecodes::_f2l:
   808   case Bytecodes::_f2d:
   810   case Bytecodes::_f2d:
   809   case Bytecodes::_d2i:
   811   case Bytecodes::_d2i:
   810   case Bytecodes::_d2l:
   812   case Bytecodes::_d2l:
   811   case Bytecodes::_d2f:
   813   case Bytecodes::_d2f:
       
   814   case Bytecodes::_iaload:
       
   815   case Bytecodes::_laload:
       
   816   case Bytecodes::_faload:
       
   817   case Bytecodes::_daload:
       
   818   case Bytecodes::_aaload:
       
   819   case Bytecodes::_baload:
       
   820   case Bytecodes::_caload:
       
   821   case Bytecodes::_saload:
   812   case Bytecodes::_ireturn:
   822   case Bytecodes::_ireturn:
   813   case Bytecodes::_lreturn:
   823   case Bytecodes::_lreturn:
   814   case Bytecodes::_freturn:
   824   case Bytecodes::_freturn:
   815   case Bytecodes::_dreturn:
   825   case Bytecodes::_dreturn:
   816   case Bytecodes::_areturn:
   826   case Bytecodes::_areturn:
   820     break;
   830     break;
   821 
   831 
   822   // bi
   832   // bi
   823   case Bytecodes::_ldc:
   833   case Bytecodes::_ldc:
   824     assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
   834     assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
   825     assert((char) index == index, "index does not fit in 8-bit");
   835     if (index == (index & 0xff)) {
   826     _bytecode.push(op);
   836       _bytecode.push(op);
   827     _bytecode.push(index);
   837       _bytecode.push(index);
       
   838     } else {
       
   839       _bytecode.push(Bytecodes::_ldc_w);
       
   840       _bytecode.push(index >> 8);
       
   841       _bytecode.push(index);
       
   842     }
   828     break;
   843     break;
   829 
   844 
   830   case Bytecodes::_iload:
   845   case Bytecodes::_iload:
   831   case Bytecodes::_lload:
   846   case Bytecodes::_lload:
   832   case Bytecodes::_fload:
   847   case Bytecodes::_fload:
   836   case Bytecodes::_lstore:
   851   case Bytecodes::_lstore:
   837   case Bytecodes::_fstore:
   852   case Bytecodes::_fstore:
   838   case Bytecodes::_dstore:
   853   case Bytecodes::_dstore:
   839   case Bytecodes::_astore:
   854   case Bytecodes::_astore:
   840     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
   855     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
   841     assert((char) index == index, "index does not fit in 8-bit");
   856     if (index == (index & 0xff)) {
   842     _bytecode.push(op);
   857       _bytecode.push(op);
   843     _bytecode.push(index);
   858       _bytecode.push(index);
       
   859     } else {
       
   860       // doesn't fit in a u2
       
   861       _bytecode.push(Bytecodes::_wide);
       
   862       _bytecode.push(op);
       
   863       _bytecode.push(index >> 8);
       
   864       _bytecode.push(index);
       
   865     }
   844     break;
   866     break;
   845 
   867 
   846   // bkk
   868   // bkk
   847   case Bytecodes::_ldc_w:
   869   case Bytecodes::_ldc_w:
   848   case Bytecodes::_ldc2_w:
   870   case Bytecodes::_ldc2_w:
   849   case Bytecodes::_checkcast:
   871   case Bytecodes::_checkcast:
   850     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
   872     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
   851     assert((short) index == index, "index does not fit in 16-bit");
   873     assert((unsigned short) index == index, "index does not fit in 16-bit");
   852     _bytecode.push(op);
   874     _bytecode.push(op);
   853     _bytecode.push(index >> 8);
   875     _bytecode.push(index >> 8);
   854     _bytecode.push(index);
   876     _bytecode.push(index);
   855     break;
   877     break;
   856 
   878 
   857   // bJJ
   879   // bJJ
   858   case Bytecodes::_invokestatic:
   880   case Bytecodes::_invokestatic:
   859   case Bytecodes::_invokespecial:
   881   case Bytecodes::_invokespecial:
   860   case Bytecodes::_invokevirtual:
   882   case Bytecodes::_invokevirtual:
   861     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
   883     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
   862     assert((short) index == index, "index does not fit in 16-bit");
   884     assert((unsigned short) index == index, "index does not fit in 16-bit");
   863     _bytecode.push(op);
   885     _bytecode.push(op);
   864     _bytecode.push(index >> 8);
   886     _bytecode.push(index >> 8);
   865     _bytecode.push(index);
   887     _bytecode.push(index);
       
   888     break;
       
   889 
       
   890   case Bytecodes::_invokeinterface:
       
   891     assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
       
   892     assert((unsigned short) index == index, "index does not fit in 16-bit");
       
   893     assert(args_size > 0, "valid args_size");
       
   894     _bytecode.push(op);
       
   895     _bytecode.push(index >> 8);
       
   896     _bytecode.push(index);
       
   897     _bytecode.push(args_size);
       
   898     _bytecode.push(0);
   866     break;
   899     break;
   867 
   900 
   868   default:
   901   default:
   869     ShouldNotReachHere();
   902     ShouldNotReachHere();
   870   }
   903   }
   981 MethodHandleWalker::ArgToken
  1014 MethodHandleWalker::ArgToken
   982 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
  1015 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
   983                                       const ArgToken& src, TRAPS) {
  1016                                       const ArgToken& src, TRAPS) {
   984 
  1017 
   985   BasicType srctype = src.basic_type();
  1018   BasicType srctype = src.basic_type();
   986   int index = src.index();
  1019   TokenType tt = src.token_type();
       
  1020   int index = -1;
   987 
  1021 
   988   switch (op) {
  1022   switch (op) {
   989   case Bytecodes::_i2l:
  1023   case Bytecodes::_i2l:
   990   case Bytecodes::_i2f:
  1024   case Bytecodes::_i2f:
   991   case Bytecodes::_i2d:
  1025   case Bytecodes::_i2d:
  1002   case Bytecodes::_f2d:
  1036   case Bytecodes::_f2d:
  1003 
  1037 
  1004   case Bytecodes::_d2i:
  1038   case Bytecodes::_d2i:
  1005   case Bytecodes::_d2l:
  1039   case Bytecodes::_d2l:
  1006   case Bytecodes::_d2f:
  1040   case Bytecodes::_d2f:
  1007     emit_load(srctype, index);
  1041     if (tt == tt_constant) {
       
  1042       emit_load_constant(src);
       
  1043     } else {
       
  1044       emit_load(srctype, src.index());
       
  1045     }
  1008     stack_pop(srctype);  // pop the src type
  1046     stack_pop(srctype);  // pop the src type
  1009     emit_bc(op);
  1047     emit_bc(op);
  1010     stack_push(type);    // push the dest value
  1048     stack_push(type);    // push the dest value
  1011     if (srctype != type)
  1049     if (tt != tt_constant)
       
  1050       index = src.index();
       
  1051     if (srctype != type || index == -1)
  1012       index = new_local_index(type);
  1052       index = new_local_index(type);
  1013     emit_store(type, index);
  1053     emit_store(type, index);
  1014     break;
  1054     break;
  1015 
  1055 
  1016   case Bytecodes::_checkcast:
  1056   case Bytecodes::_checkcast:
  1017     emit_load(srctype, index);
  1057     if (tt == tt_constant) {
       
  1058       emit_load_constant(src);
       
  1059     } else {
       
  1060       emit_load(srctype, src.index());
       
  1061       index = src.index();
       
  1062     }
  1018     emit_bc(op, cpool_klass_put(tk));
  1063     emit_bc(op, cpool_klass_put(tk));
       
  1064     if (index == -1)
       
  1065       index = new_local_index(type);
  1019     emit_store(srctype, index);
  1066     emit_store(srctype, index);
  1020     break;
  1067     break;
  1021 
  1068 
  1022   default:
  1069   default:
  1023     if (op == Bytecodes::_illegal)
  1070     if (op == Bytecodes::_illegal)
  1055   }
  1102   }
  1056 
  1103 
  1057   klassOop klass     = m->method_holder();
  1104   klassOop klass     = m->method_holder();
  1058   Symbol*  name      = m->name();
  1105   Symbol*  name      = m->name();
  1059   Symbol*  signature = m->signature();
  1106   Symbol*  signature = m->signature();
       
  1107 
       
  1108   // Count the number of arguments, not the size
       
  1109   ArgumentCount asc(signature);
       
  1110   assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
       
  1111          "argc mismatch");
  1060 
  1112 
  1061   if (tailcall) {
  1113   if (tailcall) {
  1062     // Actually, in order to make these methods more recognizable,
  1114     // Actually, in order to make these methods more recognizable,
  1063     // let's put them in holder class MethodHandle.  That way stack
  1115     // let's put them in holder class MethodHandle.  That way stack
  1064     // walkers and compiler heuristics can recognize them.
  1116     // walkers and compiler heuristics can recognize them.
  1104   case Bytecodes::_invokestatic:
  1156   case Bytecodes::_invokestatic:
  1105   case Bytecodes::_invokespecial:
  1157   case Bytecodes::_invokespecial:
  1106   case Bytecodes::_invokevirtual:
  1158   case Bytecodes::_invokevirtual:
  1107     emit_bc(op, methodref_index);
  1159     emit_bc(op, methodref_index);
  1108     break;
  1160     break;
  1109   case Bytecodes::_invokeinterface:
  1161 
  1110     Unimplemented();
  1162   case Bytecodes::_invokeinterface: {
       
  1163     ArgumentSizeComputer asc(signature);
       
  1164     emit_bc(op, methodref_index, asc.size() + 1);
  1111     break;
  1165     break;
       
  1166   }
       
  1167 
  1112   default:
  1168   default:
  1113     ShouldNotReachHere();
  1169     ShouldNotReachHere();
  1114   }
  1170   }
  1115 
  1171 
  1116   // If tailcall, we have walked all the way to a direct method handle.
  1172   // If tailcall, we have walked all the way to a direct method handle.
  1117   // Otherwise, make a recursive call to some helper routine.
  1173   // Otherwise, make a recursive call to some helper routine.
  1118   BasicType rbt = m->result_type();
  1174   BasicType rbt = m->result_type();
  1119   if (rbt == T_ARRAY)  rbt = T_OBJECT;
  1175   if (rbt == T_ARRAY)  rbt = T_OBJECT;
       
  1176   stack_push(rbt);  // The return value is already pushed onto the stack.
  1120   ArgToken ret;
  1177   ArgToken ret;
  1121   if (tailcall) {
  1178   if (tailcall) {
  1122     if (rbt != _rtype) {
  1179     if (rbt != _rtype) {
  1123       if (rbt == T_VOID) {
  1180       if (rbt == T_VOID) {
  1124         // push a zero of the right sort
  1181         // push a zero of the right sort
  1169     default: ShouldNotReachHere();
  1226     default: ShouldNotReachHere();
  1170     }
  1227     }
  1171     ret = ArgToken();  // Dummy return value.
  1228     ret = ArgToken();  // Dummy return value.
  1172   }
  1229   }
  1173   else {
  1230   else {
  1174     stack_push(rbt);  // The return value is already pushed onto the stack.
       
  1175     int index = new_local_index(rbt);
  1231     int index = new_local_index(rbt);
  1176     switch (rbt) {
  1232     switch (rbt) {
  1177     case T_BOOLEAN: case T_BYTE: case T_CHAR:  case T_SHORT:
  1233     case T_BOOLEAN: case T_BYTE: case T_CHAR:  case T_SHORT:
  1178     case T_INT:     case T_LONG: case T_FLOAT: case T_DOUBLE:
  1234     case T_INT:     case T_LONG: case T_FLOAT: case T_DOUBLE:
  1179     case T_OBJECT:
  1235     case T_OBJECT:
  1194 MethodHandleWalker::ArgToken
  1250 MethodHandleWalker::ArgToken
  1195 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
  1251 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
  1196                                  const MethodHandleWalker::ArgToken& base,
  1252                                  const MethodHandleWalker::ArgToken& base,
  1197                                  const MethodHandleWalker::ArgToken& offset,
  1253                                  const MethodHandleWalker::ArgToken& offset,
  1198                                  TRAPS) {
  1254                                  TRAPS) {
  1199   Unimplemented();
  1255   switch (base.token_type()) {
  1200   return ArgToken();
  1256     case tt_parameter:
       
  1257     case tt_temporary:
       
  1258       emit_load(base.basic_type(), base.index());
       
  1259       break;
       
  1260     case tt_constant:
       
  1261       emit_load_constant(base);
       
  1262       break;
       
  1263     default:
       
  1264       ShouldNotReachHere();
       
  1265   }
       
  1266   switch (offset.token_type()) {
       
  1267     case tt_parameter:
       
  1268     case tt_temporary:
       
  1269       emit_load(offset.basic_type(), offset.index());
       
  1270       break;
       
  1271     case tt_constant:
       
  1272       emit_load_constant(offset);
       
  1273       break;
       
  1274     default:
       
  1275       ShouldNotReachHere();
       
  1276   }
       
  1277   emit_bc(op);
       
  1278   int index = new_local_index(type);
       
  1279   emit_store(type, index);
       
  1280   return ArgToken(tt_temporary, type, index);
  1201 }
  1281 }
  1202 
  1282 
  1203 
  1283 
  1204 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
  1284 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
  1205   jvalue con_copy;
  1285   jvalue con_copy;
  1370     const char* s = _strbuf.as_string();
  1450     const char* s = _strbuf.as_string();
  1371     _strbuf.reset();
  1451     _strbuf.reset();
  1372     return s;
  1452     return s;
  1373   }
  1453   }
  1374   ArgToken token(const char* str) {
  1454   ArgToken token(const char* str) {
  1375     jvalue string_con;
  1455     return ArgToken(str);
  1376     string_con.j = (intptr_t) str;
       
  1377     return ArgToken(tt_symbolic, T_LONG, string_con);
       
  1378   }
  1456   }
  1379   const char* string(ArgToken token) {
  1457   const char* string(ArgToken token) {
  1380     return (const char*) (intptr_t) token.get_jlong();
  1458     return token.str();
  1381   }
  1459   }
  1382   void start_params() {
  1460   void start_params() {
  1383     _param_state <<= 1;
  1461     _param_state <<= 1;
  1384     _out->print("(");
  1462     _out->print("(");
  1385   }
  1463   }