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