Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jit_compiler.h" |
| 18 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
| 21 | #include "arch/instruction_set_features.h" |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 22 | #include "base/stringpiece.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 24 | #include "base/timing_logger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 25 | #include "compiler_callbacks.h" |
| 26 | #include "dex/pass_manager.h" |
| 27 | #include "dex/quick_compiler_callbacks.h" |
| 28 | #include "driver/compiler_driver.h" |
| 29 | #include "driver/compiler_options.h" |
| 30 | #include "jit/jit.h" |
| 31 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | #include "oat_file-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame^] | 33 | #include "oat_quick_method_header.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "object_lock.h" |
| 35 | #include "thread_list.h" |
| 36 | #include "verifier/method_verifier-inl.h" |
| 37 | |
| 38 | namespace art { |
| 39 | namespace jit { |
| 40 | |
| 41 | JitCompiler* JitCompiler::Create() { |
| 42 | return new JitCompiler(); |
| 43 | } |
| 44 | |
| 45 | extern "C" void* jit_load(CompilerCallbacks** callbacks) { |
| 46 | VLOG(jit) << "loading jit compiler"; |
| 47 | auto* const jit_compiler = JitCompiler::Create(); |
| 48 | CHECK(jit_compiler != nullptr); |
| 49 | *callbacks = jit_compiler->GetCompilerCallbacks(); |
| 50 | VLOG(jit) << "Done loading jit compiler"; |
| 51 | return jit_compiler; |
| 52 | } |
| 53 | |
| 54 | extern "C" void jit_unload(void* handle) { |
| 55 | DCHECK(handle != nullptr); |
| 56 | delete reinterpret_cast<JitCompiler*>(handle); |
| 57 | } |
| 58 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 59 | extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 60 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 61 | auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle); |
| 62 | DCHECK(jit_compiler != nullptr); |
| 63 | return jit_compiler->CompileMethod(self, method); |
| 64 | } |
| 65 | |
| 66 | JitCompiler::JitCompiler() : total_time_(0) { |
| 67 | auto* pass_manager_options = new PassManagerOptions; |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 68 | pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup"); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 69 | compiler_options_.reset(new CompilerOptions( |
| 70 | CompilerOptions::kDefaultCompilerFilter, |
| 71 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 72 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 73 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 74 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 75 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 76 | CompilerOptions::kDefaultInlineDepthLimit, |
| 77 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 78 | /* include_patch_information */ false, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 79 | CompilerOptions::kDefaultTopKProfileThreshold, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 80 | Runtime::Current()->IsDebuggable(), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 81 | CompilerOptions::kDefaultGenerateDebugInfo, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 82 | /* implicit_null_checks */ true, |
| 83 | /* implicit_so_checks */ true, |
| 84 | /* implicit_suspend_checks */ false, |
| 85 | /* pic */ true, // TODO: Support non-PIC in optimizing. |
| 86 | /* verbose_methods */ nullptr, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 87 | pass_manager_options, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 88 | /* init_failure_output */ nullptr, |
| 89 | /* abort_on_hard_verifier_failure */ false)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 90 | const InstructionSet instruction_set = kRuntimeISA; |
Mathieu Chartier | 085fc87 | 2015-10-15 18:19:01 -0700 | [diff] [blame] | 91 | for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 92 | VLOG(compiler) << "JIT compiler option " << option; |
| 93 | std::string error_msg; |
| 94 | if (option.starts_with("--instruction-set-variant=")) { |
| 95 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 96 | VLOG(compiler) << "JIT instruction set variant " << str; |
| 97 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 98 | instruction_set, str.as_string(), &error_msg)); |
| 99 | if (instruction_set_features_ == nullptr) { |
| 100 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 101 | } |
| 102 | } else if (option.starts_with("--instruction-set-features=")) { |
| 103 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 104 | VLOG(compiler) << "JIT instruction set features " << str; |
| 105 | if (instruction_set_features_.get() == nullptr) { |
| 106 | instruction_set_features_.reset(InstructionSetFeatures::FromVariant( |
| 107 | instruction_set, "default", &error_msg)); |
| 108 | if (instruction_set_features_ == nullptr) { |
| 109 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 110 | } |
| 111 | } |
| 112 | instruction_set_features_.reset( |
| 113 | instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg)); |
| 114 | if (instruction_set_features_ == nullptr) { |
| 115 | LOG(WARNING) << "Error parsing " << option << " message=" << error_msg; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | if (instruction_set_features_ == nullptr) { |
| 120 | instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); |
| 121 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 122 | cumulative_logger_.reset(new CumulativeLogger("jit times")); |
| 123 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 124 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 125 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 126 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 127 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | compiler_driver_.reset(new CompilerDriver( |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 129 | compiler_options_.get(), |
| 130 | verification_results_.get(), |
| 131 | method_inliner_map_.get(), |
| 132 | Compiler::kOptimizing, |
| 133 | instruction_set, |
| 134 | instruction_set_features_.get(), |
| 135 | /* image */ false, |
| 136 | /* image_classes */ nullptr, |
| 137 | /* compiled_classes */ nullptr, |
| 138 | /* compiled_methods */ nullptr, |
| 139 | /* thread_count */ 1, |
| 140 | /* dump_stats */ false, |
| 141 | /* dump_passes */ false, |
| 142 | /* dump_cfg_file_name */ "", |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 143 | /* dump_cfg_append */ false, |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 144 | cumulative_logger_.get(), |
| 145 | /* swap_fd */ -1, |
| 146 | /* profile_file */ "")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 147 | // Disable dedupe so we can remove compiled methods. |
| 148 | compiler_driver_->SetDedupeEnabled(false); |
| 149 | compiler_driver_->SetSupportBootImageFixup(false); |
| 150 | } |
| 151 | |
| 152 | JitCompiler::~JitCompiler() { |
| 153 | } |
| 154 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 155 | bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 156 | TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit)); |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 157 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 158 | StackHandleScope<2> hs(self); |
| 159 | self->AssertNoPendingException(); |
| 160 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 161 | if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) { |
| 162 | VLOG(jit) << "Already compiled " << PrettyMethod(method); |
| 163 | return true; // Already compiled |
| 164 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 165 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 166 | { |
| 167 | TimingLogger::ScopedTiming t2("Initializing", &logger); |
| 168 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 169 | VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 170 | return false; |
| 171 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 172 | } |
| 173 | const DexFile* dex_file = h_class->GetDexCache()->GetDexFile(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | MethodReference method_ref(dex_file, method->GetDexMethodIndex()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 175 | // Only verify if we don't already have verification results. |
| 176 | if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 177 | TimingLogger::ScopedTiming t2("Verifying", &logger); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 178 | std::string error; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 179 | if (verifier::MethodVerifier::VerifyMethod(method, true, &error) == |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 180 | verifier::MethodVerifier::kHardFailure) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 181 | VLOG(jit) << "Not compile method " << PrettyMethod(method) |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 182 | << " due to verification failure " << error; |
| 183 | return false; |
| 184 | } |
| 185 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 186 | CompiledMethod* compiled_method = nullptr; |
| 187 | { |
| 188 | TimingLogger::ScopedTiming t2("Compiling", &logger); |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 189 | compiled_method = compiler_driver_->CompileArtMethod(self, method); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 190 | } |
| 191 | { |
| 192 | TimingLogger::ScopedTiming t2("TrimMaps", &logger); |
| 193 | // Trim maps to reduce memory usage, TODO: measure how much this increases compile time. |
| 194 | runtime->GetArenaPool()->TrimMaps(); |
| 195 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 196 | if (compiled_method == nullptr) { |
| 197 | return false; |
| 198 | } |
| 199 | total_time_ += NanoTime() - start_time; |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 200 | // Don't add the method if we are supposed to be deoptimized. |
| 201 | bool result = false; |
| 202 | if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 203 | const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 204 | if (code != nullptr) { |
| 205 | // Already have some compiled code, just use this instead of linking. |
| 206 | // TODO: Fix recompilation. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 207 | method->SetEntryPointFromQuickCompiledCode(code); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 208 | result = true; |
| 209 | } else { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 210 | TimingLogger::ScopedTiming t2("MakeExecutable", &logger); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 211 | result = MakeExecutable(compiled_method, method); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 214 | // Remove the compiled method to save memory. |
| 215 | compiler_driver_->RemoveCompiledMethod(method_ref); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 216 | runtime->GetJit()->AddTimingLogger(logger); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 217 | return result; |
| 218 | } |
| 219 | |
| 220 | CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const { |
| 221 | return callbacks_.get(); |
| 222 | } |
| 223 | |
| 224 | uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method, |
| 225 | uint8_t* reserve_begin, uint8_t* reserve_end, |
| 226 | const uint8_t* mapping_table, |
| 227 | const uint8_t* vmap_table, |
| 228 | const uint8_t* gc_map) { |
| 229 | reserve_begin += sizeof(OatQuickMethodHeader); |
| 230 | reserve_begin = reinterpret_cast<uint8_t*>( |
| 231 | compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin))); |
| 232 | const auto* quick_code = compiled_method->GetQuickCode(); |
| 233 | CHECK_LE(reserve_begin, reserve_end); |
| 234 | CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin)); |
| 235 | auto* code_ptr = reserve_begin; |
| 236 | OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1; |
| 237 | // Construct the header last. |
| 238 | const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 239 | const auto core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 240 | const auto fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 241 | const auto code_size = quick_code->size(); |
| 242 | CHECK_NE(code_size, 0U); |
| 243 | std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr); |
| 244 | // After we are done writing we need to update the method header. |
| 245 | // Write out the method header last. |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 246 | method_header = new(method_header) OatQuickMethodHeader( |
| 247 | (mapping_table == nullptr) ? 0 : code_ptr - mapping_table, |
| 248 | (vmap_table == nullptr) ? 0 : code_ptr - vmap_table, |
| 249 | (gc_map == nullptr) ? 0 : code_ptr - gc_map, |
| 250 | frame_size_in_bytes, |
| 251 | core_spill_mask, |
| 252 | fp_spill_mask, |
| 253 | code_size); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 254 | // Return the code ptr. |
| 255 | return code_ptr; |
| 256 | } |
| 257 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 258 | bool JitCompiler::AddToCodeCache(ArtMethod* method, const CompiledMethod* compiled_method, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 259 | OatFile::OatMethod* out_method) { |
| 260 | Runtime* runtime = Runtime::Current(); |
| 261 | JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache(); |
| 262 | const auto* quick_code = compiled_method->GetQuickCode(); |
| 263 | if (quick_code == nullptr) { |
| 264 | return false; |
| 265 | } |
| 266 | const auto code_size = quick_code->size(); |
| 267 | Thread* const self = Thread::Current(); |
| 268 | const uint8_t* base = code_cache->CodeCachePtr(); |
| 269 | auto* const mapping_table = compiled_method->GetMappingTable(); |
| 270 | auto* const vmap_table = compiled_method->GetVmapTable(); |
| 271 | auto* const gc_map = compiled_method->GetGcMap(); |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 272 | uint8_t* mapping_table_ptr = nullptr; |
| 273 | uint8_t* vmap_table_ptr = nullptr; |
| 274 | uint8_t* gc_map_ptr = nullptr; |
| 275 | |
| 276 | if (mapping_table != nullptr) { |
| 277 | // Write out pre-header stuff. |
| 278 | mapping_table_ptr = code_cache->AddDataArray( |
| 279 | self, mapping_table->data(), mapping_table->data() + mapping_table->size()); |
| 280 | if (mapping_table_ptr == nullptr) { |
| 281 | return false; // Out of data cache. |
| 282 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 283 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 284 | |
| 285 | if (vmap_table != nullptr) { |
| 286 | vmap_table_ptr = code_cache->AddDataArray( |
| 287 | self, vmap_table->data(), vmap_table->data() + vmap_table->size()); |
| 288 | if (vmap_table_ptr == nullptr) { |
| 289 | return false; // Out of data cache. |
| 290 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 291 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 292 | |
| 293 | if (gc_map != nullptr) { |
| 294 | gc_map_ptr = code_cache->AddDataArray( |
| 295 | self, gc_map->data(), gc_map->data() + gc_map->size()); |
| 296 | if (gc_map_ptr == nullptr) { |
| 297 | return false; // Out of data cache. |
| 298 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 299 | } |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 300 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 301 | // Don't touch this until you protect / unprotect the code. |
| 302 | const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32; |
| 303 | uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size); |
| 304 | if (code_reserve == nullptr) { |
| 305 | return false; |
| 306 | } |
| 307 | auto* code_ptr = WriteMethodHeaderAndCode( |
| 308 | compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr, |
| 309 | vmap_table_ptr, gc_map_ptr); |
| 310 | |
Mathieu Chartier | 5783a74 | 2015-06-01 19:12:36 -0700 | [diff] [blame] | 311 | __builtin___clear_cache(reinterpret_cast<char*>(code_ptr), |
| 312 | reinterpret_cast<char*>(code_ptr + quick_code->size())); |
| 313 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 314 | const size_t thumb_offset = compiled_method->CodeDelta(); |
| 315 | const uint32_t code_offset = code_ptr - base + thumb_offset; |
| 316 | *out_method = OatFile::OatMethod(base, code_offset); |
| 317 | DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr); |
| 318 | DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr); |
| 319 | DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr); |
| 320 | DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes()); |
| 321 | DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask()); |
| 322 | DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask()); |
| 323 | VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size=" |
| 324 | << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr) |
| 325 | << "," << reinterpret_cast<void*>(code_ptr + code_size); |
| 326 | return true; |
| 327 | } |
| 328 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 329 | bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, ArtMethod* method) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 330 | CHECK(method != nullptr); |
| 331 | CHECK(compiled_method != nullptr); |
| 332 | OatFile::OatMethod oat_method(nullptr, 0); |
| 333 | if (!AddToCodeCache(method, compiled_method, &oat_method)) { |
| 334 | return false; |
| 335 | } |
| 336 | // TODO: Flush instruction cache. |
| 337 | oat_method.LinkMethod(method); |
| 338 | CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) |
| 339 | << PrettyMethod(method); |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | } // namespace jit |
| 344 | } // namespace art |