hotspot/src/share/vm/jvmci/jvmciCodeInstaller.cpp
changeset 35582 c32a0cc19877
parent 34502 1cfcb971cb17
child 35823 59a847ec6ee3
equal deleted inserted replaced
35581:dd47cf4734f2 35582:c32a0cc19877
     1 /*
     1 /*
     2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   487   initialize_fields(target(), JNIHandles::resolve(compiled_code_obj), CHECK_OK);
   487   initialize_fields(target(), JNIHandles::resolve(compiled_code_obj), CHECK_OK);
   488   JVMCIEnv::CodeInstallResult result = initialize_buffer(buffer, CHECK_OK);
   488   JVMCIEnv::CodeInstallResult result = initialize_buffer(buffer, CHECK_OK);
   489   if (result != JVMCIEnv::ok) {
   489   if (result != JVMCIEnv::ok) {
   490     return result;
   490     return result;
   491   }
   491   }
   492   process_exception_handlers();
       
   493 
   492 
   494   _debug_recorder->pcs_size(); // ehm, create the sentinel record
   493   _debug_recorder->pcs_size(); // ehm, create the sentinel record
   495 
   494 
   496   assert(_debug_recorder->pcs_length() >= 2, "must be at least 2");
   495   assert(_debug_recorder->pcs_length() >= 2, "must be at least 2");
   497 
   496 
   521   initialize_fields(target(), JNIHandles::resolve(compiled_code_obj), CHECK_OK);
   520   initialize_fields(target(), JNIHandles::resolve(compiled_code_obj), CHECK_OK);
   522   JVMCIEnv::CodeInstallResult result = initialize_buffer(buffer, CHECK_OK);
   521   JVMCIEnv::CodeInstallResult result = initialize_buffer(buffer, CHECK_OK);
   523   if (result != JVMCIEnv::ok) {
   522   if (result != JVMCIEnv::ok) {
   524     return result;
   523     return result;
   525   }
   524   }
   526   process_exception_handlers();
       
   527 
   525 
   528   int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
   526   int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
   529 
   527 
   530   if (!compiled_code->is_a(HotSpotCompiledNmethod::klass())) {
   528   if (!compiled_code->is_a(HotSpotCompiledNmethod::klass())) {
   531     oop stubName = HotSpotCompiledCode::name(compiled_code_obj);
   529     oop stubName = HotSpotCompiledCode::name(compiled_code_obj);
   572     // Must be a HotSpotCompiledRuntimeStub.
   570     // Must be a HotSpotCompiledRuntimeStub.
   573     // Only used in OopMap constructor for non-product builds
   571     // Only used in OopMap constructor for non-product builds
   574     _parameter_count = 0;
   572     _parameter_count = 0;
   575   }
   573   }
   576   _sites_handle = JNIHandles::make_local(HotSpotCompiledCode::sites(compiled_code));
   574   _sites_handle = JNIHandles::make_local(HotSpotCompiledCode::sites(compiled_code));
   577   _exception_handlers_handle = JNIHandles::make_local(HotSpotCompiledCode::exceptionHandlers(compiled_code));
       
   578 
   575 
   579   _code_handle = JNIHandles::make_local(HotSpotCompiledCode::targetCode(compiled_code));
   576   _code_handle = JNIHandles::make_local(HotSpotCompiledCode::targetCode(compiled_code));
   580   _code_size = HotSpotCompiledCode::targetCodeSize(compiled_code);
   577   _code_size = HotSpotCompiledCode::targetCodeSize(compiled_code);
   581   _total_frame_size = HotSpotCompiledCode::totalFrameSize(compiled_code);
   578   _total_frame_size = HotSpotCompiledCode::totalFrameSize(compiled_code);
   582   _custom_stack_area_offset = HotSpotCompiledCode::customStackAreaOffset(compiled_code);
   579   _custom_stack_area_offset = HotSpotCompiledCode::customStackAreaOffset(compiled_code);
   606   // Estimate the number of static call stubs that might be emitted.
   603   // Estimate the number of static call stubs that might be emitted.
   607   int static_call_stubs = 0;
   604   int static_call_stubs = 0;
   608   objArrayOop sites = this->sites();
   605   objArrayOop sites = this->sites();
   609   for (int i = 0; i < sites->length(); i++) {
   606   for (int i = 0; i < sites->length(); i++) {
   610     oop site = sites->obj_at(i);
   607     oop site = sites->obj_at(i);
   611     if (site != NULL && site->is_a(CompilationResult_Mark::klass())) {
   608     if (site != NULL && site->is_a(site_Mark::klass())) {
   612       oop id_obj = CompilationResult_Mark::id(site);
   609       oop id_obj = site_Mark::id(site);
   613       if (id_obj != NULL) {
   610       if (id_obj != NULL) {
   614         if (!java_lang_boxing_object::is_instance(id_obj, T_INT)) {
   611         if (!java_lang_boxing_object::is_instance(id_obj, T_INT)) {
   615           JVMCI_ERROR_0("expected Integer id, got %s", id_obj->klass()->signature_name());
   612           JVMCI_ERROR_0("expected Integer id, got %s", id_obj->klass()->signature_name());
   616         }
   613         }
   617         jint id = id_obj->int_field(java_lang_boxing_object::value_offset_in_bytes(T_INT));
   614         jint id = id_obj->int_field(java_lang_boxing_object::value_offset_in_bytes(T_INT));
   667   for (int i = 0; i < data_section_patches()->length(); i++) {
   664   for (int i = 0; i < data_section_patches()->length(); i++) {
   668     Handle patch = data_section_patches()->obj_at(i);
   665     Handle patch = data_section_patches()->obj_at(i);
   669     if (patch.is_null()) {
   666     if (patch.is_null()) {
   670       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   667       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   671     }
   668     }
   672     Handle reference = CompilationResult_DataPatch::reference(patch);
   669     Handle reference = site_DataPatch::reference(patch);
   673     if (reference.is_null()) {
   670     if (reference.is_null()) {
   674       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   671       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   675     }
   672     }
   676     if (!reference->is_a(CompilationResult_ConstantReference::klass())) {
   673     if (!reference->is_a(site_ConstantReference::klass())) {
   677       JVMCI_ERROR_OK("invalid patch in data section: %s", reference->klass()->signature_name());
   674       JVMCI_ERROR_OK("invalid patch in data section: %s", reference->klass()->signature_name());
   678     }
   675     }
   679     Handle constant = CompilationResult_ConstantReference::constant(reference);
   676     Handle constant = site_ConstantReference::constant(reference);
   680     if (constant.is_null()) {
   677     if (constant.is_null()) {
   681       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   678       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   682     }
   679     }
   683     address dest = _constants->start() + CompilationResult_Site::pcOffset(patch);
   680     address dest = _constants->start() + site_Site::pcOffset(patch);
   684     if (constant->is_a(HotSpotMetaspaceConstantImpl::klass())) {
   681     if (constant->is_a(HotSpotMetaspaceConstantImpl::klass())) {
   685       if (HotSpotMetaspaceConstantImpl::compressed(constant)) {
   682       if (HotSpotMetaspaceConstantImpl::compressed(constant)) {
   686 #ifdef _LP64
   683 #ifdef _LP64
   687         *((narrowKlass*) dest) = record_narrow_metadata_reference(constant, CHECK_OK);
   684         *((narrowKlass*) dest) = record_narrow_metadata_reference(constant, CHECK_OK);
   688 #else
   685 #else
   714     Handle site = sites->obj_at(i);
   711     Handle site = sites->obj_at(i);
   715     if (site.is_null()) {
   712     if (site.is_null()) {
   716       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   713       THROW_(vmSymbols::java_lang_NullPointerException(), JVMCIEnv::ok);
   717     }
   714     }
   718 
   715 
   719     jint pc_offset = CompilationResult_Site::pcOffset(site);
   716     jint pc_offset = site_Site::pcOffset(site);
   720 
   717 
   721     if (site->is_a(CompilationResult_Call::klass())) {
   718     if (site->is_a(site_Call::klass())) {
   722       TRACE_jvmci_4("call at %i", pc_offset);
   719       TRACE_jvmci_4("call at %i", pc_offset);
   723       site_Call(buffer, pc_offset, site, CHECK_OK);
   720       site_Call(buffer, pc_offset, site, CHECK_OK);
   724     } else if (site->is_a(CompilationResult_Infopoint::klass())) {
   721     } else if (site->is_a(site_Infopoint::klass())) {
   725       // three reasons for infopoints denote actual safepoints
   722       // three reasons for infopoints denote actual safepoints
   726       oop reason = CompilationResult_Infopoint::reason(site);
   723       oop reason = site_Infopoint::reason(site);
   727       if (InfopointReason::SAFEPOINT() == reason || InfopointReason::CALL() == reason || InfopointReason::IMPLICIT_EXCEPTION() == reason) {
   724       if (site_InfopointReason::SAFEPOINT() == reason || site_InfopointReason::CALL() == reason || site_InfopointReason::IMPLICIT_EXCEPTION() == reason) {
   728         TRACE_jvmci_4("safepoint at %i", pc_offset);
   725         TRACE_jvmci_4("safepoint at %i", pc_offset);
   729         site_Safepoint(buffer, pc_offset, site, CHECK_OK);
   726         site_Safepoint(buffer, pc_offset, site, CHECK_OK);
   730       } else {
   727       } else {
   731         TRACE_jvmci_4("infopoint at %i", pc_offset);
   728         TRACE_jvmci_4("infopoint at %i", pc_offset);
   732         site_Infopoint(buffer, pc_offset, site, CHECK_OK);
   729         site_Infopoint(buffer, pc_offset, site, CHECK_OK);
   733       }
   730       }
   734     } else if (site->is_a(CompilationResult_DataPatch::klass())) {
   731     } else if (site->is_a(site_DataPatch::klass())) {
   735       TRACE_jvmci_4("datapatch at %i", pc_offset);
   732       TRACE_jvmci_4("datapatch at %i", pc_offset);
   736       site_DataPatch(buffer, pc_offset, site, CHECK_OK);
   733       site_DataPatch(buffer, pc_offset, site, CHECK_OK);
   737     } else if (site->is_a(CompilationResult_Mark::klass())) {
   734     } else if (site->is_a(site_Mark::klass())) {
   738       TRACE_jvmci_4("mark at %i", pc_offset);
   735       TRACE_jvmci_4("mark at %i", pc_offset);
   739       site_Mark(buffer, pc_offset, site, CHECK_OK);
   736       site_Mark(buffer, pc_offset, site, CHECK_OK);
       
   737     } else if (site->is_a(site_ExceptionHandler::klass())) {
       
   738       TRACE_jvmci_4("exceptionhandler at %i", pc_offset);
       
   739       site_ExceptionHandler(pc_offset, site);
   740     } else {
   740     } else {
   741       JVMCI_ERROR_OK("unexpected site subclass: %s", site->klass()->signature_name());
   741       JVMCI_ERROR_OK("unexpected site subclass: %s", site->klass()->signature_name());
   742     }
   742     }
   743     last_pc_offset = pc_offset;
   743     last_pc_offset = pc_offset;
   744 
   744 
   800   Handle methodHandle = Assumptions_CallSiteTargetValue::methodHandle(assumption());
   800   Handle methodHandle = Assumptions_CallSiteTargetValue::methodHandle(assumption());
   801 
   801 
   802   _dependencies->assert_call_site_target_value(callSite(), methodHandle());
   802   _dependencies->assert_call_site_target_value(callSite(), methodHandle());
   803 }
   803 }
   804 
   804 
   805 void CodeInstaller::process_exception_handlers() {
   805 void CodeInstaller::site_ExceptionHandler(jint pc_offset, Handle exc) {
   806   if (exception_handlers() != NULL) {
   806   jint handler_offset = site_ExceptionHandler::handlerPos(exc);
   807     objArrayOop handlers = exception_handlers();
   807 
   808     for (int i = 0; i < handlers->length(); i++) {
   808   // Subtable header
   809       oop exc = handlers->obj_at(i);
   809   _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
   810       jint pc_offset = CompilationResult_Site::pcOffset(exc);
   810 
   811       jint handler_offset = CompilationResult_ExceptionHandler::handlerPos(exc);
   811   // Subtable entry
   812 
   812   _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
   813       // Subtable header
       
   814       _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
       
   815 
       
   816       // Subtable entry
       
   817       _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
       
   818     }
       
   819   }
       
   820 }
   813 }
   821 
   814 
   822 // If deoptimization happens, the interpreter should reexecute these bytecodes.
   815 // If deoptimization happens, the interpreter should reexecute these bytecodes.
   823 // This function mainly helps the compilers to set up the reexecute bit.
   816 // This function mainly helps the compilers to set up the reexecute bit.
   824 static bool bytecode_should_reexecute(Bytecodes::Code code) {
   817 static bool bytecode_should_reexecute(Bytecodes::Code code) {
   986   _debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, false, false,
   979   _debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, false, false,
   987                                   locals_token, expressions_token, monitors_token);
   980                                   locals_token, expressions_token, monitors_token);
   988 }
   981 }
   989 
   982 
   990 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
   983 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
   991   Handle debug_info = CompilationResult_Infopoint::debugInfo(site);
   984   Handle debug_info = site_Infopoint::debugInfo(site);
   992   if (debug_info.is_null()) {
   985   if (debug_info.is_null()) {
   993     JVMCI_ERROR("debug info expected at safepoint at %i", pc_offset);
   986     JVMCI_ERROR("debug info expected at safepoint at %i", pc_offset);
   994   }
   987   }
   995 
   988 
   996   // address instruction = _instructions->start() + pc_offset;
   989   // address instruction = _instructions->start() + pc_offset;
  1000   record_scope(pc_offset, debug_info, CodeInstaller::FullFrame, CHECK);
   993   record_scope(pc_offset, debug_info, CodeInstaller::FullFrame, CHECK);
  1001   _debug_recorder->end_safepoint(pc_offset);
   994   _debug_recorder->end_safepoint(pc_offset);
  1002 }
   995 }
  1003 
   996 
  1004 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
   997 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1005   Handle debug_info = CompilationResult_Infopoint::debugInfo(site);
   998   Handle debug_info = site_Infopoint::debugInfo(site);
  1006   if (debug_info.is_null()) {
   999   if (debug_info.is_null()) {
  1007     JVMCI_ERROR("debug info expected at infopoint at %i", pc_offset);
  1000     JVMCI_ERROR("debug info expected at infopoint at %i", pc_offset);
  1008   }
  1001   }
  1009 
  1002 
  1010   // We'd like to check that pc_offset is greater than the
  1003   // We'd like to check that pc_offset is greater than the
  1015   record_scope(pc_offset, debug_info, CodeInstaller::BytecodePosition, CHECK);
  1008   record_scope(pc_offset, debug_info, CodeInstaller::BytecodePosition, CHECK);
  1016   _debug_recorder->end_non_safepoint(pc_offset);
  1009   _debug_recorder->end_non_safepoint(pc_offset);
  1017 }
  1010 }
  1018 
  1011 
  1019 void CodeInstaller::site_Call(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1012 void CodeInstaller::site_Call(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1020   Handle target = CompilationResult_Call::target(site);
  1013   Handle target = site_Call::target(site);
  1021   InstanceKlass* target_klass = InstanceKlass::cast(target->klass());
  1014   InstanceKlass* target_klass = InstanceKlass::cast(target->klass());
  1022 
  1015 
  1023   Handle hotspot_method; // JavaMethod
  1016   Handle hotspot_method; // JavaMethod
  1024   Handle foreign_call;
  1017   Handle foreign_call;
  1025 
  1018 
  1027     foreign_call = target;
  1020     foreign_call = target;
  1028   } else {
  1021   } else {
  1029     hotspot_method = target;
  1022     hotspot_method = target;
  1030   }
  1023   }
  1031 
  1024 
  1032   Handle debug_info = CompilationResult_Call::debugInfo(site);
  1025   Handle debug_info = site_Call::debugInfo(site);
  1033 
  1026 
  1034   assert(hotspot_method.not_null() ^ foreign_call.not_null(), "Call site needs exactly one type");
  1027   assert(hotspot_method.not_null() ^ foreign_call.not_null(), "Call site needs exactly one type");
  1035 
  1028 
  1036   NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
  1029   NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
  1037   jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, hotspot_method, CHECK);
  1030   jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, hotspot_method, CHECK);
  1064     _debug_recorder->end_safepoint(next_pc_offset);
  1057     _debug_recorder->end_safepoint(next_pc_offset);
  1065   }
  1058   }
  1066 }
  1059 }
  1067 
  1060 
  1068 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1061 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1069   Handle reference = CompilationResult_DataPatch::reference(site);
  1062   Handle reference = site_DataPatch::reference(site);
  1070   if (reference.is_null()) {
  1063   if (reference.is_null()) {
  1071     THROW(vmSymbols::java_lang_NullPointerException());
  1064     THROW(vmSymbols::java_lang_NullPointerException());
  1072   } else if (reference->is_a(CompilationResult_ConstantReference::klass())) {
  1065   } else if (reference->is_a(site_ConstantReference::klass())) {
  1073     Handle constant = CompilationResult_ConstantReference::constant(reference);
  1066     Handle constant = site_ConstantReference::constant(reference);
  1074     if (constant.is_null()) {
  1067     if (constant.is_null()) {
  1075       THROW(vmSymbols::java_lang_NullPointerException());
  1068       THROW(vmSymbols::java_lang_NullPointerException());
  1076     } else if (constant->is_a(HotSpotObjectConstantImpl::klass())) {
  1069     } else if (constant->is_a(HotSpotObjectConstantImpl::klass())) {
  1077       pd_patch_OopConstant(pc_offset, constant, CHECK);
  1070       pd_patch_OopConstant(pc_offset, constant, CHECK);
  1078     } else if (constant->is_a(HotSpotMetaspaceConstantImpl::klass())) {
  1071     } else if (constant->is_a(HotSpotMetaspaceConstantImpl::klass())) {
  1079       pd_patch_MetaspaceConstant(pc_offset, constant, CHECK);
  1072       pd_patch_MetaspaceConstant(pc_offset, constant, CHECK);
  1080     } else {
  1073     } else {
  1081       JVMCI_ERROR("unknown constant type in data patch: %s", constant->klass()->signature_name());
  1074       JVMCI_ERROR("unknown constant type in data patch: %s", constant->klass()->signature_name());
  1082     }
  1075     }
  1083   } else if (reference->is_a(CompilationResult_DataSectionReference::klass())) {
  1076   } else if (reference->is_a(site_DataSectionReference::klass())) {
  1084     int data_offset = CompilationResult_DataSectionReference::offset(reference);
  1077     int data_offset = site_DataSectionReference::offset(reference);
  1085     if (0 <= data_offset && data_offset < _constants_size) {
  1078     if (0 <= data_offset && data_offset < _constants_size) {
  1086       pd_patch_DataSectionReference(pc_offset, data_offset);
  1079       pd_patch_DataSectionReference(pc_offset, data_offset);
  1087     } else {
  1080     } else {
  1088       JVMCI_ERROR("data offset 0x%X points outside data section (size 0x%X)", data_offset, _constants_size);
  1081       JVMCI_ERROR("data offset 0x%X points outside data section (size 0x%X)", data_offset, _constants_size);
  1089     }
  1082     }
  1091     JVMCI_ERROR("unknown data patch type: %s", reference->klass()->signature_name());
  1084     JVMCI_ERROR("unknown data patch type: %s", reference->klass()->signature_name());
  1092   }
  1085   }
  1093 }
  1086 }
  1094 
  1087 
  1095 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1088 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS) {
  1096   Handle id_obj = CompilationResult_Mark::id(site);
  1089   Handle id_obj = site_Mark::id(site);
  1097 
  1090 
  1098   if (id_obj.not_null()) {
  1091   if (id_obj.not_null()) {
  1099     if (!java_lang_boxing_object::is_instance(id_obj(), T_INT)) {
  1092     if (!java_lang_boxing_object::is_instance(id_obj(), T_INT)) {
  1100       JVMCI_ERROR("expected Integer id, got %s", id_obj->klass()->signature_name());
  1093       JVMCI_ERROR("expected Integer id, got %s", id_obj->klass()->signature_name());
  1101     }
  1094     }