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