Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "compiler_llvm.h" |
| 18 | |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 19 | #include "class_linker.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 20 | #include "compilation_unit.h" |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 21 | #include "compiled_method.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 22 | #include "compiler.h" |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 23 | #include "dex_cache.h" |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame^] | 24 | #include "elf_image.h" |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 25 | #include "elf_loader.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 26 | #include "ir_builder.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 27 | #include "jni_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 28 | #include "method_compiler.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 29 | #include "oat_compilation_unit.h" |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 30 | #include "stl_util.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 31 | #include "upcall_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 32 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 33 | #include <llvm/LinkAllPasses.h> |
| 34 | #include <llvm/LinkAllVMCore.h> |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 35 | #include <llvm/Support/CommandLine.h> |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 36 | #include <llvm/Support/ManagedStatic.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 37 | #include <llvm/Support/TargetSelect.h> |
| 38 | #include <llvm/Support/Threading.h> |
| 39 | |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 40 | namespace llvm { |
| 41 | extern bool TimePassesIsEnabled; |
| 42 | } |
| 43 | |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 44 | extern llvm::cl::opt<bool> EnableARMLongCalls; |
| 45 | // NOTE: Although EnableARMLongCalls is defined in llvm/lib/Target/ARM/ |
| 46 | // ARMISelLowering.cpp, however, it is not in the llvm namespace. |
| 47 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 48 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 49 | namespace { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 50 | |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 51 | pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT; |
| 52 | |
| 53 | void InitializeLLVM() { |
Logan Chien | 013b6f2 | 2012-03-02 17:20:33 +0800 | [diff] [blame] | 54 | // NOTE: Uncomment following line to show the time consumption of LLVM passes |
| 55 | //llvm::TimePassesIsEnabled = true; |
| 56 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 57 | // Initialize LLVM target, MC subsystem, asm printer, and asm parser |
| 58 | llvm::InitializeAllTargets(); |
| 59 | llvm::InitializeAllTargetMCs(); |
| 60 | llvm::InitializeAllAsmPrinters(); |
| 61 | llvm::InitializeAllAsmParsers(); |
| 62 | // TODO: Maybe we don't have to initialize "all" targets. |
| 63 | |
Logan Chien | 4c17dff | 2012-03-02 20:15:46 +0800 | [diff] [blame] | 64 | // Enable -arm-long-calls |
| 65 | EnableARMLongCalls = true; |
| 66 | |
Logan Chien | dd7cf5b | 2012-03-01 12:55:19 +0800 | [diff] [blame] | 67 | // Initialize LLVM optimization passes |
| 68 | llvm::PassRegistry ®istry = *llvm::PassRegistry::getPassRegistry(); |
| 69 | |
| 70 | llvm::initializeCore(registry); |
| 71 | llvm::initializeScalarOpts(registry); |
| 72 | llvm::initializeIPO(registry); |
| 73 | llvm::initializeAnalysis(registry); |
| 74 | llvm::initializeIPA(registry); |
| 75 | llvm::initializeTransformUtils(registry); |
| 76 | llvm::initializeInstCombine(registry); |
| 77 | llvm::initializeInstrumentation(registry); |
| 78 | llvm::initializeTarget(registry); |
| 79 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 80 | // Initialize LLVM internal data structure for multithreading |
| 81 | llvm::llvm_start_multithreaded(); |
| 82 | } |
| 83 | |
Logan Chien | f130655 | 2012-03-16 11:17:53 +0800 | [diff] [blame] | 84 | // The Guard to Shutdown LLVM |
Logan Chien | aeb5303 | 2012-03-18 02:29:38 +0800 | [diff] [blame] | 85 | // llvm::llvm_shutdown_obj llvm_guard; |
| 86 | // TODO: We are commenting out this line because this will cause SEGV from |
| 87 | // time to time. |
| 88 | // Two reasons: (1) the order of the destruction of static objects, or |
| 89 | // (2) dlopen/dlclose side-effect on static objects. |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 90 | |
| 91 | } // anonymous namespace |
| 92 | |
| 93 | |
| 94 | namespace art { |
| 95 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 96 | |
| 97 | |
Logan Chien | e75a8cc | 2012-02-24 12:26:43 +0800 | [diff] [blame] | 98 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 99 | |
| 100 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 101 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 102 | : compiler_(compiler), compiler_lock_("llvm_compiler_lock"), |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 103 | insn_set_(insn_set), curr_cunit_(NULL) { |
Shih-wei Liao | fc34adb | 2012-03-07 08:51:44 -0800 | [diff] [blame] | 104 | |
| 105 | |
| 106 | // Initialize LLVM libraries |
| 107 | pthread_once(&llvm_initialized, InitializeLLVM); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | CompilerLLVM::~CompilerLLVM() { |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 112 | STLDeleteElements(&cunits_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 116 | void CompilerLLVM::EnsureCompilationUnit() { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 117 | compiler_lock_.AssertHeld(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 118 | |
| 119 | if (curr_cunit_ != NULL) { |
| 120 | return; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 121 | } |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 122 | |
| 123 | // Allocate compilation unit |
| 124 | size_t cunit_idx = cunits_.size(); |
| 125 | |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 126 | curr_cunit_ = new CompilationUnit(insn_set_, cunit_idx); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 127 | |
| 128 | // Setup output filename |
| 129 | curr_cunit_->SetElfFileName( |
| 130 | StringPrintf("%s-%zu", elf_filename_.c_str(), cunit_idx)); |
| 131 | |
| 132 | if (IsBitcodeFileNameAvailable()) { |
| 133 | curr_cunit_->SetBitcodeFileName( |
| 134 | StringPrintf("%s-%zu", bitcode_filename_.c_str(), cunit_idx)); |
| 135 | } |
| 136 | |
| 137 | // Register compilation unit |
| 138 | cunits_.push_back(curr_cunit_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 142 | void CompilerLLVM::MaterializeRemainder() { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 143 | MutexLock GUARD(compiler_lock_); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 144 | if (curr_cunit_ != NULL) { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 145 | Materialize(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 146 | } |
| 147 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 148 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 149 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 150 | void CompilerLLVM::MaterializeIfThresholdReached() { |
| 151 | MutexLock GUARD(compiler_lock_); |
| 152 | if (curr_cunit_ != NULL && curr_cunit_->IsMaterializeThresholdReached()) { |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 153 | Materialize(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 158 | void CompilerLLVM::Materialize() { |
| 159 | compiler_lock_.AssertHeld(); |
| 160 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 161 | DCHECK(curr_cunit_ != NULL); |
| 162 | DCHECK(!curr_cunit_->IsMaterialized()); |
| 163 | |
| 164 | // Write bitcode to file when filename is set |
| 165 | if (IsBitcodeFileNameAvailable()) { |
| 166 | curr_cunit_->WriteBitcodeToFile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 169 | // Materialize the llvm::Module into ELF object file |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 170 | curr_cunit_->Materialize(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 171 | |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 172 | // Load ELF image when automatic ELF loading is enabled |
| 173 | if (IsAutoElfLoadingEnabled()) { |
| 174 | LoadElfFromCompilationUnit(curr_cunit_); |
| 175 | } |
| 176 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 177 | // Delete the compilation unit |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 178 | curr_cunit_ = NULL; |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 182 | void CompilerLLVM::EnableAutoElfLoading() { |
| 183 | MutexLock GUARD(compiler_lock_); |
| 184 | |
| 185 | if (IsAutoElfLoadingEnabled()) { |
| 186 | // If there is an existing ELF loader, then do nothing. |
| 187 | // Because the existing ELF loader may have returned some code address |
| 188 | // already. If we replace the existing ELF loader with |
| 189 | // elf_loader_.reset(...), then it is possible to have some dangling |
| 190 | // pointer. |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Create ELF loader and load the materialized CompilationUnit |
| 195 | elf_loader_.reset(new ElfLoader()); |
| 196 | |
| 197 | for (size_t i = 0; i < cunits_.size(); ++i) { |
| 198 | if (cunits_[i]->IsMaterialized()) { |
| 199 | LoadElfFromCompilationUnit(cunits_[i]); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void CompilerLLVM::LoadElfFromCompilationUnit(const CompilationUnit* cunit) { |
| 206 | compiler_lock_.AssertHeld(); |
| 207 | DCHECK(cunit->IsMaterialized()) << cunit->GetElfIndex(); |
| 208 | |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame^] | 209 | if (!elf_loader_->LoadElfAt(cunit->GetElfIndex(), cunit->GetElfImage())) { |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 210 | LOG(ERROR) << "Failed to load ELF from compilation unit " |
| 211 | << cunit->GetElfIndex(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | const void* CompilerLLVM::GetMethodCodeAddr(const CompiledMethod* cm, |
| 217 | const Method* method) const { |
| 218 | return elf_loader_->GetMethodCodeAddr(cm->GetElfIndex(), method); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | const Method::InvokeStub* CompilerLLVM:: |
| 223 | GetMethodInvokeStubAddr(const CompiledInvokeStub* cm, const Method* method) const { |
| 224 | return elf_loader_->GetMethodInvokeStubAddr(cm->GetElfIndex(), method); |
| 225 | } |
| 226 | |
| 227 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 228 | CompiledMethod* CompilerLLVM:: |
| 229 | CompileDexMethod(OatCompilationUnit* oat_compilation_unit) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 230 | MutexLock GUARD(compiler_lock_); |
| 231 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 232 | EnsureCompilationUnit(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 233 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 234 | UniquePtr<MethodCompiler> method_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 235 | new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 236 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 237 | return method_compiler->Compile(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 238 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 239 | |
| 240 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 241 | CompiledMethod* CompilerLLVM:: |
| 242 | CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) { |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 243 | MutexLock GUARD(compiler_lock_); |
| 244 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 245 | EnsureCompilationUnit(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 246 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 247 | UniquePtr<JniCompiler> jni_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 248 | new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 249 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 250 | return jni_compiler->Compile(); |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 254 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 255 | char const *shorty) { |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 256 | MutexLock GUARD(compiler_lock_); |
| 257 | |
Logan Chien | ce11906 | 2012-03-02 11:57:34 +0800 | [diff] [blame] | 258 | EnsureCompilationUnit(); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 259 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 260 | UniquePtr<UpcallCompiler> upcall_compiler( |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 261 | new UpcallCompiler(curr_cunit_, *compiler_)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 262 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 263 | return upcall_compiler->CreateStub(is_static, shorty); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 266 | } // namespace compiler_llvm |
| 267 | } // namespace art |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 268 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 269 | inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) { |
| 270 | void *compiler_context = compiler.GetCompilerContext(); |
| 271 | CHECK(compiler_context != NULL); |
| 272 | return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context); |
| 273 | } |
| 274 | |
| 275 | inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) { |
| 276 | void *compiler_context = compiler.GetCompilerContext(); |
| 277 | CHECK(compiler_context != NULL); |
| 278 | return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context); |
| 279 | } |
| 280 | |
| 281 | extern "C" void ArtInitCompilerContext(art::Compiler& compiler) { |
| 282 | CHECK(compiler.GetCompilerContext() == NULL); |
| 283 | |
| 284 | art::compiler_llvm::CompilerLLVM* compiler_llvm = |
| 285 | new art::compiler_llvm::CompilerLLVM(&compiler, |
| 286 | compiler.GetInstructionSet()); |
| 287 | |
| 288 | compiler.SetCompilerContext(compiler_llvm); |
| 289 | } |
| 290 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 291 | extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler, |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 292 | const art::DexFile::CodeItem* code_item, |
| 293 | uint32_t access_flags, uint32_t method_idx, |
| 294 | const art::ClassLoader* class_loader, |
| 295 | const art::DexFile& dex_file) |
| 296 | { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 297 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 298 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 299 | |
| 300 | art::OatCompilationUnit oat_compilation_unit( |
| 301 | class_loader, class_linker, dex_file, *dex_cache, code_item, |
| 302 | method_idx, access_flags); |
| 303 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 304 | return ContextOf(compiler)->CompileDexMethod(&oat_compilation_unit); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler, |
| 308 | uint32_t access_flags, uint32_t method_idx, |
| 309 | const art::ClassLoader* class_loader, |
| 310 | const art::DexFile& dex_file) { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 311 | art::ClassLinker *class_linker = art::Runtime::Current()->GetClassLinker(); |
| 312 | art::DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 313 | |
| 314 | art::OatCompilationUnit oat_compilation_unit( |
| 315 | class_loader, class_linker, dex_file, *dex_cache, NULL, |
| 316 | method_idx, access_flags); |
| 317 | |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 318 | art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler); |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 319 | art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit); |
| 320 | compiler_llvm->MaterializeIfThresholdReached(); |
Elliott Hughes | 13b835a | 2012-03-13 19:45:22 -0700 | [diff] [blame] | 321 | return result; |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static, |
| 325 | const char* shorty, uint32_t shorty_len) { |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 326 | return ContextOf(compiler)->CreateInvokeStub(is_static, shorty); |
| 327 | } |
| 328 | |
| 329 | extern "C" void compilerLLVMSetElfFileName(art::Compiler& compiler, |
| 330 | std::string const& filename) { |
| 331 | ContextOf(compiler)->SetElfFileName(filename); |
| 332 | } |
| 333 | |
| 334 | extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler, |
| 335 | std::string const& filename) { |
| 336 | ContextOf(compiler)->SetBitcodeFileName(filename); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) { |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 340 | ContextOf(compiler)->MaterializeRemainder(); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 343 | extern "C" void compilerLLVMEnableAutoElfLoading(art::Compiler& compiler) { |
| 344 | art::compiler_llvm::CompilerLLVM* compiler_llvm = |
| 345 | reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext()); |
| 346 | return compiler_llvm->EnableAutoElfLoading(); |
| 347 | } |
| 348 | |
| 349 | extern "C" const void* compilerLLVMGetMethodCodeAddr(const art::Compiler& compiler, |
| 350 | const art::CompiledMethod* cm, |
| 351 | const art::Method* method) { |
| 352 | const art::compiler_llvm::CompilerLLVM* compiler_llvm = |
| 353 | reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext()); |
| 354 | return compiler_llvm->GetMethodCodeAddr(cm, method); |
| 355 | } |
| 356 | |
| 357 | extern "C" const art::Method::InvokeStub* compilerLLVMGetMethodInvokeStubAddr(const art::Compiler& compiler, |
| 358 | const art::CompiledInvokeStub* cm, |
| 359 | const art::Method* method) { |
| 360 | const art::compiler_llvm::CompilerLLVM* compiler_llvm = |
| 361 | reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler.GetCompilerContext()); |
| 362 | return compiler_llvm->GetMethodInvokeStubAddr(cm, method); |
| 363 | } |
| 364 | |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 365 | // Note: Using this function carefully!!! This is temporary solution, we will remove it. |
| 366 | extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) { |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 367 | return new art::MutexLock(ContextOf(compiler)->compiler_lock_); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | extern "C" void compilerLLVMDispose(art::Compiler& compiler) { |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 371 | delete ContextOf(compiler); |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 372 | } |