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