165 "prim_to_ref" ,
166 "swap_args" ,
167 "rot_args" ,
168 "dup_args" ,
169 "drop_args" ,
170 "collect_args" ,
171 "spread_args" ,
172 "fold_args"
173 };
174
175 static const char* adapter_op_to_string(int op) {
176 if (op >= 0 && op < (int)ARRAY_SIZE(adapter_ops))
177 return adapter_ops[op];
178 return "unknown_op";
179 }
180
181 void MethodHandleChain::print(oopDesc* m) {
182 HandleMark hm;
183 ResourceMark rm;
184 Handle mh(m);
185 print(mh);
186 }
187
188 void MethodHandleChain::print(Handle mh) {
189 EXCEPTION_MARK;
190 MethodHandleChain mhc(mh, THREAD);
191 if (HAS_PENDING_EXCEPTION) {
192 oop ex = THREAD->pending_exception();
193 CLEAR_PENDING_EXCEPTION;
194 ex->print();
195 return;
196 }
197 mhc.print();
198 }
199
200
201 void MethodHandleChain::print() {
202 EXCEPTION_MARK;
203 print_impl(THREAD);
204 if (HAS_PENDING_EXCEPTION) {
205 oop ex = THREAD->pending_exception();
206 CLEAR_PENDING_EXCEPTION;
207 ex->print();
208 }
209 }
210
211 void MethodHandleChain::print_impl(TRAPS) {
212 ResourceMark rm;
213
214 MethodHandleChain chain(_root, CHECK);
215 for (;;) {
216 tty->print(INTPTR_FORMAT ": ", chain.method_handle()());
217 if (chain.is_bound()) {
218 tty->print("bound: arg_type %s arg_slot %d",
219 type2name(chain.bound_arg_type()),
220 chain.bound_arg_slot());
221 oop o = chain.bound_arg_oop();
222 if (o != NULL) {
223 if (o->is_instance()) {
224 tty->print(" instance %s", o->klass()->klass_part()->internal_name());
225 } else {
226 o->print();
227 }
228 }
229 } else if (chain.is_adapter()) {
230 tty->print("adapter: arg_slot %d conversion op %s",
231 chain.adapter_arg_slot(),
232 adapter_op_to_string(chain.adapter_conversion_op()));
233 switch (chain.adapter_conversion_op()) {
234 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
235 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
236 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
237 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
238 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
239 break;
240
241 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
242 tty->print(" src_type = %s", type2name(chain.adapter_conversion_src_type()));
243 break;
244 }
245
246 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS:
247 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
248 int dest_arg_slot = chain.adapter_conversion_vminfo();
249 tty->print(" dest_arg_slot %d type %s", dest_arg_slot, type2name(chain.adapter_conversion_src_type()));
250 break;
251 }
252
253 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS:
254 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
890 lose("requested ref-to-prim conversion not expected", CHECK);
891 } else {
892 lose(err_msg("requested raw conversion not allowed: %s -> %s", type2name(src), type2name(dst)), CHECK);
893 }
894 }
895 }
896
897
898 // -----------------------------------------------------------------------------
899 // MethodHandleCompiler
900
901 MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS)
902 : MethodHandleWalker(root, is_invokedynamic, THREAD),
903 _invoke_count(invoke_count),
904 _thread(THREAD),
905 _bytecode(THREAD, 50),
906 _constants(THREAD, 10),
907 _non_bcp_klasses(THREAD, 5),
908 _cur_stack(0),
909 _max_stack(0),
910 _rtype(T_ILLEGAL)
911 {
912
913 // Element zero is always the null constant.
914 (void) _constants.append(NULL);
915
916 // Set name and signature index.
917 _name_index = cpool_symbol_put(name);
918 _signature_index = cpool_symbol_put(signature);
919
920 // To make the resulting methods more recognizable by
921 // stack walkers and compiler heuristics,
922 // we put them in holder class MethodHandle.
923 // See klass_is_method_handle_adapter_holder
924 // and methodOopDesc::is_method_handle_adapter.
925 _target_klass = SystemDictionaryHandles::MethodHandle_klass();
926
927 check_non_bcp_klasses(java_lang_invoke_MethodHandle::type(root()), CHECK);
928
929 // Get return type klass.
930 Handle first_mtype(THREAD, chain().method_type_oop());
1098 case Bytecodes::_invokespecial:
1099 case Bytecodes::_invokevirtual:
1100 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1101 assert((unsigned short) index == index, "index does not fit in 16-bit");
1102 _bytecode.push(op);
1103 _bytecode.push(index >> 8);
1104 _bytecode.push(index);
1105 break;
1106
1107 case Bytecodes::_invokeinterface:
1108 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1109 assert((unsigned short) index == index, "index does not fit in 16-bit");
1110 assert(args_size > 0, "valid args_size");
1111 _bytecode.push(op);
1112 _bytecode.push(index >> 8);
1113 _bytecode.push(index);
1114 _bytecode.push(args_size);
1115 _bytecode.push(0);
1116 break;
1117
1118 default:
1119 ShouldNotReachHere();
1120 }
1121 }
1122
1123
1124 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
1125 if (index <= 3) {
1126 switch (bt) {
1127 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1128 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
1129 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
1130 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
1131 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
1132 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
1133 default:
1134 ShouldNotReachHere();
1135 }
1136 }
1137 else {
1138 switch (bt) {
1139 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1140 case T_INT: emit_bc(Bytecodes::_iload, index); break;
1141 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
1142 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
1143 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
1301 default:
1302 if (op == Bytecodes::_illegal)
1303 lose(err_msg("no such primitive conversion: %s -> %s", type2name(src.basic_type()), type2name(type)), THREAD);
1304 else
1305 lose(err_msg("bad primitive conversion op: %s", Bytecodes::name(op)), THREAD);
1306 return make_prim_constant(type, &zero_jvalue, THREAD);
1307 }
1308
1309 return make_parameter(type, tk, index, THREAD);
1310 }
1311
1312
1313 // -----------------------------------------------------------------------------
1314 // MethodHandleCompiler
1315 //
1316
1317 // Values used by the compiler.
1318 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
1319 jvalue MethodHandleCompiler::one_jvalue = { 1 };
1320
1321 // Emit bytecodes for the given invoke instruction.
1322 MethodHandleWalker::ArgToken
1323 MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid,
1324 Bytecodes::Code op, bool tailcall,
1325 int argc, MethodHandleWalker::ArgToken* argv,
1326 TRAPS) {
1327 ArgToken zero;
1328 if (m.is_null()) {
1329 // Get the intrinsic methodOop.
1330 m = methodHandle(THREAD, vmIntrinsics::method_for(iid));
1331 if (m.is_null()) {
1332 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
1333 }
1334 }
1335
1336 klassOop klass = m->method_holder();
1337 Symbol* name = m->name();
1338 Symbol* signature = m->signature();
1339
1340 if (iid == vmIntrinsics::_invokeGeneric &&
1350 // customize m to the exact required rtype
1351 bool has_non_bcp_klass = check_non_bcp_klasses(rtype(), CHECK_(zero));
1352 TempNewSymbol sig2 = java_lang_invoke_MethodType::as_signature(rtype(), true, CHECK_(zero));
1353 methodHandle m2;
1354 if (!has_non_bcp_klass) {
1355 methodOop m2_oop = SystemDictionary::find_method_handle_invoke(m->name(), sig2,
1356 KlassHandle(), CHECK_(zero));
1357 m2 = methodHandle(THREAD, m2_oop);
1358 }
1359 if (m2.is_null()) {
1360 // just build it fresh
1361 m2 = methodOopDesc::make_invoke_method(klass, m->name(), sig2, rtype, CHECK_(zero));
1362 if (m2.is_null())
1363 lose(err_msg("no customized invoker %s", sig2->as_utf8()), CHECK_(zero));
1364 }
1365 m = m2;
1366 signature = m->signature();
1367 }
1368 }
1369
1370 check_non_bcp_klass(klass, CHECK_(zero));
1371 if (m->is_method_handle_invoke()) {
1372 check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero));
1373 }
1374
1375 // Count the number of arguments, not the size
1376 ArgumentCount asc(signature);
1377 assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
1378 "argc mismatch");
1379
1380 // Inline the method.
1381 InvocationCounter* ic = m->invocation_counter();
1382 ic->set_carry_flag();
1383
1384 for (int i = 0; i < argc; i++) {
1385 ArgToken arg = argv[i];
1386 TokenType tt = arg.token_type();
1387 BasicType bt = arg.basic_type();
1388
1389 switch (tt) {
1390 case tt_parameter:
1391 case tt_temporary:
1392 emit_load(bt, arg.index());
1393 break;
1394 case tt_constant:
1395 emit_load_constant(arg);
1396 break;
1397 case tt_illegal:
1398 // Sentinel.
1399 assert(i == (argc - 1), "sentinel must be last entry");
1400 break;
1401 case tt_void:
1402 default:
1403 ShouldNotReachHere();
1669 }
1670
1671 switch (cv->tag()) {
1672 case JVM_CONSTANT_Long:
1673 case JVM_CONSTANT_Double:
1674 i++; // Skip empty entry.
1675 assert(_constants.at(i) == NULL, "empty entry");
1676 break;
1677 }
1678 }
1679
1680 cpool->set_preresolution();
1681
1682 // Set the constant pool holder to the target method's class.
1683 cpool->set_pool_holder(_target_klass());
1684
1685 return cpool;
1686 }
1687
1688
1689 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
1690 methodHandle empty;
1691 // Create a method that holds the generated bytecode. invokedynamic
1692 // has no receiver, normal MH calls do.
1693 int flags_bits;
1694 if (for_invokedynamic())
1695 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
1696 else
1697 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
1698
1699 // Create a new method
1700 methodHandle m;
1701 {
1702 methodOop m_oop = oopFactory::new_method(bytecode_length(),
1703 accessFlags_from(flags_bits),
1704 0, 0, 0, oopDesc::IsSafeConc, CHECK_(empty));
1705 m = methodHandle(THREAD, m_oop);
1706 }
1707
1708 constantPoolHandle cpool = get_constant_pool(CHECK_(empty));
1709 m->set_constants(cpool());
1748 }
1749 break;
1750 case JVM_CONSTANT_InterfaceMethodref:
1751 index = klassItable::compute_itable_index(m());
1752 e->set_interface_call(m, index);
1753 break;
1754 }
1755 }
1756
1757 // Set the invocation counter's count to the invoke count of the
1758 // original call site.
1759 InvocationCounter* ic = m->invocation_counter();
1760 ic->set(InvocationCounter::wait_for_compile, _invoke_count);
1761
1762 // Create a new MDO
1763 {
1764 methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
1765 assert(m->method_data() == NULL, "there should not be an MDO yet");
1766 m->set_method_data(mdo);
1767
1768 // Iterate over all profile data and set the count of the counter
1769 // data entries to the original call site counter.
1770 for (ProfileData* profile_data = mdo->first_data();
1771 mdo->is_valid(profile_data);
1772 profile_data = mdo->next_data(profile_data)) {
1773 if (profile_data->is_CounterData()) {
1774 CounterData* counter_data = profile_data->as_CounterData();
1775 counter_data->set_count(_invoke_count);
1776 }
1777 }
1778 }
1779
1780 #ifndef PRODUCT
1781 if (TraceMethodHandles) {
1782 m->print();
1783 m->print_codes();
1784 }
1785 #endif //PRODUCT
1786
1787 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
1788 return m;
1789 }
1790
1791
1792 #ifndef PRODUCT
1793
1794 // MH printer for debugging.
1795
1796 class MethodHandlePrinter : public MethodHandleWalker {
1797 private:
1798 outputStream* _out;
|
165 "prim_to_ref" ,
166 "swap_args" ,
167 "rot_args" ,
168 "dup_args" ,
169 "drop_args" ,
170 "collect_args" ,
171 "spread_args" ,
172 "fold_args"
173 };
174
175 static const char* adapter_op_to_string(int op) {
176 if (op >= 0 && op < (int)ARRAY_SIZE(adapter_ops))
177 return adapter_ops[op];
178 return "unknown_op";
179 }
180
181 void MethodHandleChain::print(oopDesc* m) {
182 HandleMark hm;
183 ResourceMark rm;
184 Handle mh(m);
185 EXCEPTION_MARK;
186 MethodHandleChain mhc(mh, THREAD);
187 if (HAS_PENDING_EXCEPTION) {
188 oop ex = THREAD->pending_exception();
189 CLEAR_PENDING_EXCEPTION;
190 ex->print();
191 return;
192 }
193 mhc.print();
194 }
195
196
197 void MethodHandleChain::print() {
198 EXCEPTION_MARK;
199 print_impl(THREAD);
200 if (HAS_PENDING_EXCEPTION) {
201 oop ex = THREAD->pending_exception();
202 CLEAR_PENDING_EXCEPTION;
203 ex->print();
204 }
205 }
206
207 void MethodHandleChain::print_impl(TRAPS) {
208 ResourceMark rm;
209
210 MethodHandleChain chain(_root, CHECK);
211 for (;;) {
212 tty->print(INTPTR_FORMAT ": ", chain.method_handle()());
213 if (chain.is_bound()) {
214 tty->print("bound: arg_type %s arg_slot %d",
215 type2name(chain.bound_arg_type()),
216 chain.bound_arg_slot());
217 oop o = chain.bound_arg_oop();
218 if (o != NULL) {
219 if (o->is_instance()) {
220 tty->print(" instance %s", o->klass()->klass_part()->internal_name());
221 if (java_lang_invoke_CountingMethodHandle::is_instance(o)) {
222 tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(o));
223 }
224 } else {
225 o->print();
226 }
227 }
228 oop vmt = chain.vmtarget_oop();
229 if (vmt != NULL) {
230 if (vmt->is_method()) {
231 tty->print(" ");
232 methodOop(vmt)->print_short_name(tty);
233 } else if (java_lang_invoke_MethodHandle::is_instance(vmt)) {
234 tty->print(" method handle " INTPTR_FORMAT, vmt);
235 } else {
236 ShouldNotReachHere();
237 }
238 }
239 } else if (chain.is_adapter()) {
240 tty->print("adapter: arg_slot %d conversion op %s",
241 chain.adapter_arg_slot(),
242 adapter_op_to_string(chain.adapter_conversion_op()));
243 switch (chain.adapter_conversion_op()) {
244 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
245 if (java_lang_invoke_CountingMethodHandle::is_instance(chain.method_handle_oop())) {
246 tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(chain.method_handle_oop()));
247 }
248 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
249 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
250 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
251 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
252 break;
253
254 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
255 tty->print(" src_type = %s", type2name(chain.adapter_conversion_src_type()));
256 break;
257 }
258
259 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS:
260 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
261 int dest_arg_slot = chain.adapter_conversion_vminfo();
262 tty->print(" dest_arg_slot %d type %s", dest_arg_slot, type2name(chain.adapter_conversion_src_type()));
263 break;
264 }
265
266 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS:
267 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
903 lose("requested ref-to-prim conversion not expected", CHECK);
904 } else {
905 lose(err_msg("requested raw conversion not allowed: %s -> %s", type2name(src), type2name(dst)), CHECK);
906 }
907 }
908 }
909
910
911 // -----------------------------------------------------------------------------
912 // MethodHandleCompiler
913
914 MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS)
915 : MethodHandleWalker(root, is_invokedynamic, THREAD),
916 _invoke_count(invoke_count),
917 _thread(THREAD),
918 _bytecode(THREAD, 50),
919 _constants(THREAD, 10),
920 _non_bcp_klasses(THREAD, 5),
921 _cur_stack(0),
922 _max_stack(0),
923 _rtype(T_ILLEGAL),
924 _selectAlternative_bci(-1),
925 _taken_count(0),
926 _not_taken_count(0)
927 {
928
929 // Element zero is always the null constant.
930 (void) _constants.append(NULL);
931
932 // Set name and signature index.
933 _name_index = cpool_symbol_put(name);
934 _signature_index = cpool_symbol_put(signature);
935
936 // To make the resulting methods more recognizable by
937 // stack walkers and compiler heuristics,
938 // we put them in holder class MethodHandle.
939 // See klass_is_method_handle_adapter_holder
940 // and methodOopDesc::is_method_handle_adapter.
941 _target_klass = SystemDictionaryHandles::MethodHandle_klass();
942
943 check_non_bcp_klasses(java_lang_invoke_MethodHandle::type(root()), CHECK);
944
945 // Get return type klass.
946 Handle first_mtype(THREAD, chain().method_type_oop());
1114 case Bytecodes::_invokespecial:
1115 case Bytecodes::_invokevirtual:
1116 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1117 assert((unsigned short) index == index, "index does not fit in 16-bit");
1118 _bytecode.push(op);
1119 _bytecode.push(index >> 8);
1120 _bytecode.push(index);
1121 break;
1122
1123 case Bytecodes::_invokeinterface:
1124 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1125 assert((unsigned short) index == index, "index does not fit in 16-bit");
1126 assert(args_size > 0, "valid args_size");
1127 _bytecode.push(op);
1128 _bytecode.push(index >> 8);
1129 _bytecode.push(index);
1130 _bytecode.push(args_size);
1131 _bytecode.push(0);
1132 break;
1133
1134 case Bytecodes::_ifeq:
1135 assert((unsigned short) index == index, "index does not fit in 16-bit");
1136 _bytecode.push(op);
1137 _bytecode.push(index >> 8);
1138 _bytecode.push(index);
1139 break;
1140
1141 default:
1142 ShouldNotReachHere();
1143 }
1144 }
1145
1146 void MethodHandleCompiler::update_branch_dest(int src, int dst) {
1147 switch (_bytecode.at(src)) {
1148 case Bytecodes::_ifeq:
1149 dst -= src; // compute the offset
1150 assert((unsigned short) dst == dst, "index does not fit in 16-bit");
1151 _bytecode.at_put(src + 1, dst >> 8);
1152 _bytecode.at_put(src + 2, dst);
1153 break;
1154 default:
1155 ShouldNotReachHere();
1156 }
1157 }
1158
1159 void MethodHandleCompiler::emit_load(ArgToken arg) {
1160 TokenType tt = arg.token_type();
1161 BasicType bt = arg.basic_type();
1162
1163 switch (tt) {
1164 case tt_parameter:
1165 case tt_temporary:
1166 emit_load(bt, arg.index());
1167 break;
1168 case tt_constant:
1169 emit_load_constant(arg);
1170 break;
1171 case tt_illegal:
1172 case tt_void:
1173 default:
1174 ShouldNotReachHere();
1175 }
1176 }
1177
1178
1179 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
1180 if (index <= 3) {
1181 switch (bt) {
1182 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1183 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
1184 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
1185 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
1186 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
1187 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
1188 default:
1189 ShouldNotReachHere();
1190 }
1191 }
1192 else {
1193 switch (bt) {
1194 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1195 case T_INT: emit_bc(Bytecodes::_iload, index); break;
1196 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
1197 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
1198 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
1356 default:
1357 if (op == Bytecodes::_illegal)
1358 lose(err_msg("no such primitive conversion: %s -> %s", type2name(src.basic_type()), type2name(type)), THREAD);
1359 else
1360 lose(err_msg("bad primitive conversion op: %s", Bytecodes::name(op)), THREAD);
1361 return make_prim_constant(type, &zero_jvalue, THREAD);
1362 }
1363
1364 return make_parameter(type, tk, index, THREAD);
1365 }
1366
1367
1368 // -----------------------------------------------------------------------------
1369 // MethodHandleCompiler
1370 //
1371
1372 // Values used by the compiler.
1373 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
1374 jvalue MethodHandleCompiler::one_jvalue = { 1 };
1375
1376 // Fetch any values from CountingMethodHandles and capture them for profiles
1377 bool MethodHandleCompiler::fetch_counts(ArgToken arg1, ArgToken arg2) {
1378 int count1 = -1, count2 = -1;
1379 if (arg1.token_type() == tt_constant && arg1.basic_type() == T_OBJECT &&
1380 java_lang_invoke_CountingMethodHandle::is_instance(arg1.object()())) {
1381 count1 = java_lang_invoke_CountingMethodHandle::vmcount(arg1.object()());
1382 }
1383 if (arg2.token_type() == tt_constant && arg2.basic_type() == T_OBJECT &&
1384 java_lang_invoke_CountingMethodHandle::is_instance(arg2.object()())) {
1385 count2 = java_lang_invoke_CountingMethodHandle::vmcount(arg2.object()());
1386 }
1387 int total = count1 + count2;
1388 if (count1 != -1 && count2 != -1 && total != 0) {
1389 // Normalize the collect counts to the invoke_count
1390 tty->print("counts %d %d scaled by %d = ", count2, count1, _invoke_count);
1391 if (count1 != 0) _not_taken_count = (int)(_invoke_count * count1 / (double)total);
1392 if (count2 != 0) _taken_count = (int)(_invoke_count * count2 / (double)total);
1393 tty->print_cr("%d %d", _taken_count, _not_taken_count);
1394 return true;
1395 }
1396 return false;
1397 }
1398
1399 // Emit bytecodes for the given invoke instruction.
1400 MethodHandleWalker::ArgToken
1401 MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid,
1402 Bytecodes::Code op, bool tailcall,
1403 int argc, MethodHandleWalker::ArgToken* argv,
1404 TRAPS) {
1405 ArgToken zero;
1406 if (m.is_null()) {
1407 // Get the intrinsic methodOop.
1408 m = methodHandle(THREAD, vmIntrinsics::method_for(iid));
1409 if (m.is_null()) {
1410 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
1411 }
1412 }
1413
1414 klassOop klass = m->method_holder();
1415 Symbol* name = m->name();
1416 Symbol* signature = m->signature();
1417
1418 if (iid == vmIntrinsics::_invokeGeneric &&
1428 // customize m to the exact required rtype
1429 bool has_non_bcp_klass = check_non_bcp_klasses(rtype(), CHECK_(zero));
1430 TempNewSymbol sig2 = java_lang_invoke_MethodType::as_signature(rtype(), true, CHECK_(zero));
1431 methodHandle m2;
1432 if (!has_non_bcp_klass) {
1433 methodOop m2_oop = SystemDictionary::find_method_handle_invoke(m->name(), sig2,
1434 KlassHandle(), CHECK_(zero));
1435 m2 = methodHandle(THREAD, m2_oop);
1436 }
1437 if (m2.is_null()) {
1438 // just build it fresh
1439 m2 = methodOopDesc::make_invoke_method(klass, m->name(), sig2, rtype, CHECK_(zero));
1440 if (m2.is_null())
1441 lose(err_msg("no customized invoker %s", sig2->as_utf8()), CHECK_(zero));
1442 }
1443 m = m2;
1444 signature = m->signature();
1445 }
1446 }
1447
1448 if (m->intrinsic_id() == vmIntrinsics::_selectAlternative &&
1449 fetch_counts(argv[1], argv[2])) {
1450 assert(argc == 3, "three arguments");
1451 assert(tailcall, "only");
1452
1453 // do inline bytecodes so we can drop profile data into it,
1454 // 0: iload_0
1455 emit_load(argv[0]);
1456 // 1: ifeq 8
1457 _selectAlternative_bci = _bytecode.length();
1458 emit_bc(Bytecodes::_ifeq, 0); // emit placeholder offset
1459 // 4: aload_1
1460 emit_load(argv[1]);
1461 // 5: areturn;
1462 emit_bc(Bytecodes::_areturn);
1463 // 8: aload_2
1464 update_branch_dest(_selectAlternative_bci, cur_bci());
1465 emit_load(argv[2]);
1466 // 9: areturn
1467 emit_bc(Bytecodes::_areturn);
1468 return ArgToken(); // Dummy return value.
1469 }
1470
1471 check_non_bcp_klass(klass, CHECK_(zero));
1472 if (m->is_method_handle_invoke()) {
1473 check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero));
1474 }
1475
1476 // Count the number of arguments, not the size
1477 ArgumentCount asc(signature);
1478 assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
1479 "argc mismatch");
1480
1481 for (int i = 0; i < argc; i++) {
1482 ArgToken arg = argv[i];
1483 TokenType tt = arg.token_type();
1484 BasicType bt = arg.basic_type();
1485
1486 switch (tt) {
1487 case tt_parameter:
1488 case tt_temporary:
1489 emit_load(bt, arg.index());
1490 break;
1491 case tt_constant:
1492 emit_load_constant(arg);
1493 break;
1494 case tt_illegal:
1495 // Sentinel.
1496 assert(i == (argc - 1), "sentinel must be last entry");
1497 break;
1498 case tt_void:
1499 default:
1500 ShouldNotReachHere();
1766 }
1767
1768 switch (cv->tag()) {
1769 case JVM_CONSTANT_Long:
1770 case JVM_CONSTANT_Double:
1771 i++; // Skip empty entry.
1772 assert(_constants.at(i) == NULL, "empty entry");
1773 break;
1774 }
1775 }
1776
1777 cpool->set_preresolution();
1778
1779 // Set the constant pool holder to the target method's class.
1780 cpool->set_pool_holder(_target_klass());
1781
1782 return cpool;
1783 }
1784
1785
1786 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) {
1787 methodHandle empty;
1788 // Create a method that holds the generated bytecode. invokedynamic
1789 // has no receiver, normal MH calls do.
1790 int flags_bits;
1791 if (for_invokedynamic())
1792 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
1793 else
1794 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
1795
1796 // Create a new method
1797 methodHandle m;
1798 {
1799 methodOop m_oop = oopFactory::new_method(bytecode_length(),
1800 accessFlags_from(flags_bits),
1801 0, 0, 0, oopDesc::IsSafeConc, CHECK_(empty));
1802 m = methodHandle(THREAD, m_oop);
1803 }
1804
1805 constantPoolHandle cpool = get_constant_pool(CHECK_(empty));
1806 m->set_constants(cpool());
1845 }
1846 break;
1847 case JVM_CONSTANT_InterfaceMethodref:
1848 index = klassItable::compute_itable_index(m());
1849 e->set_interface_call(m, index);
1850 break;
1851 }
1852 }
1853
1854 // Set the invocation counter's count to the invoke count of the
1855 // original call site.
1856 InvocationCounter* ic = m->invocation_counter();
1857 ic->set(InvocationCounter::wait_for_compile, _invoke_count);
1858
1859 // Create a new MDO
1860 {
1861 methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
1862 assert(m->method_data() == NULL, "there should not be an MDO yet");
1863 m->set_method_data(mdo);
1864
1865 bool found_selectAlternative = false;
1866 // Iterate over all profile data and set the count of the counter
1867 // data entries to the original call site counter.
1868 for (ProfileData* profile_data = mdo->first_data();
1869 mdo->is_valid(profile_data);
1870 profile_data = mdo->next_data(profile_data)) {
1871 if (profile_data->is_CounterData()) {
1872 CounterData* counter_data = profile_data->as_CounterData();
1873 counter_data->set_count(_invoke_count);
1874 }
1875 if (profile_data->is_BranchData() &&
1876 profile_data->bci() == _selectAlternative_bci) {
1877 BranchData* bd = profile_data->as_BranchData();
1878 bd->set_taken(_taken_count);
1879 bd->set_not_taken(_not_taken_count);
1880 found_selectAlternative = true;
1881 }
1882 }
1883 assert(_selectAlternative_bci == -1 || found_selectAlternative, "must have found profile entry");
1884 }
1885
1886 #ifndef PRODUCT
1887 if (TraceMethodHandles) {
1888 m->print();
1889 m->print_codes();
1890 }
1891 #endif //PRODUCT
1892
1893 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
1894 return m;
1895 }
1896
1897
1898 #ifndef PRODUCT
1899
1900 // MH printer for debugging.
1901
1902 class MethodHandlePrinter : public MethodHandleWalker {
1903 private:
1904 outputStream* _out;
|