1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "classfile/stringTable.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "jvmci/compilerRuntime.hpp" 28 #include "runtime/interfaceSupport.hpp" 29 #include "runtime/compilationPolicy.hpp" 30 #include "runtime/deoptimization.hpp" 31 32 // Resolve and allocate String 33 JRT_BLOCK_ENTRY(void, CompilerRuntime::resolve_string_by_symbol(JavaThread *thread, void* string_result, const char* name)) 34 JRT_BLOCK 35 oop str = *(oop*)string_result; // Is it resolved already? 36 if (str == NULL) { // Do resolution 37 // First 2 bytes of name contains length (number of bytes). 38 int len = build_u2_from((address)name); 39 name += 2; 40 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK); 41 str = StringTable::intern(sym, CHECK); 42 assert(java_lang_String::is_instance(str), "must be string"); 43 *(oop*)string_result = str; // Store result 44 } 45 assert(str != NULL, "Should be allocated!"); 46 thread->set_vm_result(str); 47 JRT_BLOCK_END 48 JRT_END 49 50 51 52 Klass* CompilerRuntime::resolve_klass_helper(JavaThread *thread, const char* name, int len, TRAPS) { 53 ResourceMark rm(THREAD); 54 // last java frame on stack (which includes native call frames) 55 RegisterMap cbl_map(thread, false); 56 // Skip stub 57 frame caller_frame = thread->last_frame().sender(&cbl_map); 58 CodeBlob* caller_cb = caller_frame.cb(); 59 guarantee(caller_cb != NULL && caller_cb->is_compiled(), "must be called from compiled method"); 60 CompiledMethod* caller_nm = caller_cb->as_compiled_method_or_null(); 61 methodHandle caller(THREAD, caller_nm->method()); 62 63 // Use class loader of aot method. 64 Handle loader(THREAD, caller->method_holder()->class_loader()); 65 Handle protection_domain(THREAD, caller->method_holder()->protection_domain()); 66 67 // Ignore wrapping L and ; 68 if (name[0] == 'L') { 69 assert(len > 2, "small name %s", name); 70 name++; 71 len -= 2; 72 } 73 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK_NULL); 74 if (sym == NULL) { 75 return NULL; 76 } 77 Klass* k = SystemDictionary::resolve_or_fail(sym, loader, protection_domain, true, CHECK_NULL); 78 79 return k; 80 } 81 82 // Resolve Klass 83 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::resolve_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 84 Klass* k = NULL; 85 JRT_BLOCK 86 k = *klass_result; // Is it resolved already? 87 if (k == NULL) { // Do resolution 88 // First 2 bytes of name contains length (number of bytes). 89 int len = build_u2_from((address)name); 90 name += 2; 91 k = CompilerRuntime::resolve_klass_helper(thread, name, len, CHECK_NULL); 92 *klass_result = k; // Store result 93 } 94 JRT_BLOCK_END 95 assert(k != NULL, " Should be loaded!"); 96 return k; 97 JRT_END 98 99 100 Method* CompilerRuntime::resolve_method_helper(Klass* klass, const char* method_name, int method_name_len, 101 const char* signature_name, int signature_name_len) { 102 Method* m = NULL; 103 TempNewSymbol name_symbol = SymbolTable::probe(method_name, method_name_len); 104 TempNewSymbol signature_symbol = SymbolTable::probe(signature_name, signature_name_len); 105 if (name_symbol != NULL && signature_symbol != NULL) { 106 if (name_symbol == vmSymbols::object_initializer_name() || 107 name_symbol == vmSymbols::class_initializer_name()) { 108 // Never search superclasses for constructors 109 if (klass->is_instance_klass()) { 110 m = InstanceKlass::cast(klass)->find_method(name_symbol, signature_symbol); 111 } 112 } else { 113 m = klass->lookup_method(name_symbol, signature_symbol); 114 if (m == NULL && klass->is_instance_klass()) { 115 m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name_symbol, signature_symbol); 116 } 117 } 118 } 119 return m; 120 } 121 122 JRT_BLOCK_ENTRY(MethodCounters*, CompilerRuntime::resolve_method_by_symbol_and_load_counters(JavaThread *thread, MethodCounters** counters_result, Klass* klass, const char* data)) 123 MethodCounters* c = *counters_result; // Is it resolved already? 124 JRT_BLOCK 125 if (c == NULL) { // Do resolution 126 // Get method name and its length 127 int method_name_len = build_u2_from((address)data); 128 data += sizeof(u2); 129 const char* method_name = data; 130 data += method_name_len; 131 132 // Get signature and its length 133 int signature_name_len = build_u2_from((address)data); 134 data += sizeof(u2); 135 const char* signature_name = data; 136 137 assert(klass != NULL, "Klass parameter must not be null"); 138 Method* m = resolve_method_helper(klass, method_name, method_name_len, signature_name, signature_name_len); 139 assert(m != NULL, "Method must resolve successfully"); 140 141 // Create method counters immediately to avoid check at runtime. 142 c = m->get_method_counters(thread); 143 if (c == NULL) { 144 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Cannot allocate method counters"); 145 } 146 147 *counters_result = c; 148 } 149 JRT_BLOCK_END 150 return c; 151 JRT_END 152 153 // Resolve and initialize Klass 154 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::initialize_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 155 Klass* k = NULL; 156 JRT_BLOCK 157 k = klass_result[0]; // Is it initialized already? 158 if (k == NULL) { // Do initialized 159 k = klass_result[1]; // Is it resolved already? 160 if (k == NULL) { // Do resolution 161 // First 2 bytes of name contains length (number of bytes). 162 int len = build_u2_from((address)name); 163 const char *cname = name + 2; 164 k = CompilerRuntime::resolve_klass_helper(thread, cname, len, CHECK_NULL); 165 klass_result[1] = k; // Store resolved result 166 } 167 Klass* k0 = klass_result[0]; // Is it initialized already? 168 if (k0 == NULL && k != NULL && k->is_instance_klass()) { 169 // Force initialization of instance class 170 InstanceKlass::cast(k)->initialize(CHECK_NULL); 171 // Double-check that it was really initialized, 172 // because we could be doing a recursive call 173 // from inside <clinit>. 174 if (InstanceKlass::cast(k)->is_initialized()) { 175 klass_result[0] = k; // Store initialized result 176 } 177 } 178 } 179 JRT_BLOCK_END 180 assert(k != NULL, " Should be loaded!"); 181 return k; 182 JRT_END 183 184 185 JRT_BLOCK_ENTRY(void, CompilerRuntime::invocation_event(JavaThread *thread, MethodCounters* counters)) 186 if (!TieredCompilation) { 187 // Ignore the event if tiered is off 188 return; 189 } 190 JRT_BLOCK 191 methodHandle mh(THREAD, counters->method()); 192 RegisterMap map(thread, false); 193 194 // Compute the enclosing method 195 frame fr = thread->last_frame().sender(&map); 196 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 197 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 198 methodHandle emh(THREAD, cm->method()); 199 200 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 201 CompilationPolicy::policy()->event(emh, mh, InvocationEntryBci, InvocationEntryBci, CompLevel_aot, cm, thread); 202 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 203 JRT_BLOCK_END 204 JRT_END 205 206 JRT_BLOCK_ENTRY(void, CompilerRuntime::backedge_event(JavaThread *thread, MethodCounters* counters, int branch_bci, int target_bci)) 207 if (!TieredCompilation) { 208 // Ignore the event if tiered is off 209 return; 210 } 211 assert(branch_bci != InvocationEntryBci && target_bci != InvocationEntryBci, "Wrong bci"); 212 assert(target_bci <= branch_bci, "Expected a back edge"); 213 JRT_BLOCK 214 methodHandle mh(THREAD, counters->method()); 215 RegisterMap map(thread, false); 216 217 // Compute the enclosing method 218 frame fr = thread->last_frame().sender(&map); 219 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 220 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 221 methodHandle emh(THREAD, cm->method()); 222 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 223 nmethod* osr_nm = CompilationPolicy::policy()->event(emh, mh, branch_bci, target_bci, CompLevel_aot, cm, thread); 224 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 225 if (osr_nm != NULL) { 226 Deoptimization::deoptimize_frame(thread, fr.id()); 227 } 228 JRT_BLOCK_END 229 JRT_END