1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/* 2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License. 6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at 7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and 14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License. 15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */ 16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 17b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "compiler.h" 18b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "compilers.h" 19b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "driver/compiler_driver.h" 20b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "mirror/art_method-inl.h" 21b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 22b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifdef ART_USE_PORTABLE_COMPILER 23b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "dex/portable/mir_to_gbc.h" 24b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "elf_writer_mclinker.h" 25b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif 26b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 27b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 28b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 29b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifdef ART_SEA_IR_MODE 3072d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" art::CompiledMethod* SeaIrCompileMethod(const art::DexFile::CodeItem* code_item, 31b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 32b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray art::InvokeType invoke_type, 33b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 34b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 35b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 36b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const art::DexFile& dex_file); 37b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif 38b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 39b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 4072d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* Compiler::TryCompileWithSeaIR(const art::DexFile::CodeItem* code_item, 41b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 42b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray art::InvokeType invoke_type, 43b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 44b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 45b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 46b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const art::DexFile& dex_file) { 47b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifdef ART_SEA_IR_MODE 4872d32629303f8f39362a4099481f48646aed042fIan Rogers bool use_sea = (std::string::npos != PrettyMethod(method_idx, dex_file).find("fibonacci")); 49b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray if (use_sea) { 50b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray LOG(INFO) << "Using SEA IR to compile..." << std::endl; 5172d32629303f8f39362a4099481f48646aed042fIan Rogers return SeaIrCompileMethod(code_item, 52b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray access_flags, 53b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray invoke_type, 54b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_def_idx, 55b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray method_idx, 56b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_loader, 57b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray dex_file); 58b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 59b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif 60b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return nullptr; 61b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 62b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 63b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 64b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifdef ART_USE_PORTABLE_COMPILER 65b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 6672d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" void ArtInitCompilerContext(art::CompilerDriver* driver); 67b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 6872d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" void ArtUnInitCompilerContext(art::CompilerDriver* driver); 69b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 7072d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" art::CompiledMethod* ArtCompileMethod(art::CompilerDriver* driver, 71b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const art::DexFile::CodeItem* code_item, 72b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 73b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray art::InvokeType invoke_type, 74b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 75b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 76b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 77b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const art::DexFile& dex_file); 78b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 7972d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" art::CompiledMethod* ArtLLVMJniCompileMethod(art::CompilerDriver* driver, 80b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, uint32_t method_idx, 81b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const art::DexFile& dex_file); 82b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 8372d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver* driver, 84b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray std::string const& filename); 85b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 86b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 8772d32629303f8f39362a4099481f48646aed042fIan Rogersclass LLVMCompiler FINAL : public Compiler { 88b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray public: 8972d32629303f8f39362a4099481f48646aed042fIan Rogers explicit LLVMCompiler(CompilerDriver* driver) : Compiler(driver, 1000) {} 90b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 9172d32629303f8f39362a4099481f48646aed042fIan Rogers void Init() const OVERRIDE { 9272d32629303f8f39362a4099481f48646aed042fIan Rogers ArtInitCompilerContext(GetCompilerDriver()); 93b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 94b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 9572d32629303f8f39362a4099481f48646aed042fIan Rogers void UnInit() const OVERRIDE { 9672d32629303f8f39362a4099481f48646aed042fIan Rogers ArtUnInitCompilerContext(GetCompilerDriver()); 97b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 98b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 9972d32629303f8f39362a4099481f48646aed042fIan Rogers CompiledMethod* Compile(const DexFile::CodeItem* code_item, 100b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 101b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray InvokeType invoke_type, 102b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 103b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 104b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 10572d32629303f8f39362a4099481f48646aed042fIan Rogers const DexFile& dex_file) const OVERRIDE { 10672d32629303f8f39362a4099481f48646aed042fIan Rogers CompiledMethod* method = TryCompileWithSeaIR(code_item, 107b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray access_flags, 108b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray invoke_type, 109b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_def_idx, 110b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray method_idx, 111b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_loader, 112b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray dex_file); 11372d32629303f8f39362a4099481f48646aed042fIan Rogers if (method != nullptr) { 11472d32629303f8f39362a4099481f48646aed042fIan Rogers return method; 11572d32629303f8f39362a4099481f48646aed042fIan Rogers } 116b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 11772d32629303f8f39362a4099481f48646aed042fIan Rogers return ArtCompileMethod(GetCompilerDriver(), 118b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray code_item, 119b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray access_flags, 120b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray invoke_type, 121b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_def_idx, 122b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray method_idx, 123b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray class_loader, 124b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray dex_file); 125b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 126b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 12772d32629303f8f39362a4099481f48646aed042fIan Rogers CompiledMethod* JniCompile(uint32_t access_flags, 128b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 12972d32629303f8f39362a4099481f48646aed042fIan Rogers const DexFile& dex_file) const OVERRIDE { 13072d32629303f8f39362a4099481f48646aed042fIan Rogers return ArtLLVMJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 131b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 132b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 133b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const { 134b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromPortableCompiledCode()); 135b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 136b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 137b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray bool WriteElf(art::File* file, 138b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray OatWriter* oat_writer, 139b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const std::vector<const art::DexFile*>& dex_files, 140b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const std::string& android_root, 141b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray bool is_host, const CompilerDriver& driver) const 142b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray OVERRIDE 143b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 144b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return art::ElfWriterMclinker::Create( 145b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray file, oat_writer, dex_files, android_root, is_host, driver); 146b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 147b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 148b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const { 149b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return PortableCodeGenerator( 150b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray cu, cu->mir_graph.get(), &cu->arena, 151b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray reinterpret_cast<art::llvm::LlvmCompilationUnit*>(compilation_unit)); 152b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 153b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 154b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu) const { 155b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray // Fused long branches not currently useful in bitcode. 156b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray cu.disable_opt |= 157b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray (1 << kBranchFusing) | 158b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray (1 << kSuppressExceptionEdges); 159b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 160b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 161b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray bool IsPortable() const OVERRIDE { 162b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return true; 163b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 164b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 165b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray void SetBitcodeFileName(const CompilerDriver& driver, const std::string& filename) { 166b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray typedef void (*SetBitcodeFileNameFn)(const CompilerDriver&, const std::string&); 167b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 168b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray SetBitcodeFileNameFn set_bitcode_file_name = 169b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray reinterpret_cast<SetBitcodeFileNameFn>(compilerLLVMSetBitcodeFileName); 170b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 171b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray set_bitcode_file_name(driver, filename); 172b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 173b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 174b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray private: 175b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(LLVMCompiler); 176b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}; 177b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif 178b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 17972d32629303f8f39362a4099481f48646aed042fIan RogersCompiler* Compiler::Create(CompilerDriver* driver, Compiler::Kind kind) { 180b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray switch (kind) { 181b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray case kQuick: 18272d32629303f8f39362a4099481f48646aed042fIan Rogers return new QuickCompiler(driver); 183b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray break; 184b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray case kOptimizing: 18572d32629303f8f39362a4099481f48646aed042fIan Rogers return new OptimizingCompiler(driver); 186b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray break; 187b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray case kPortable: 188b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifdef ART_USE_PORTABLE_COMPILER 18972d32629303f8f39362a4099481f48646aed042fIan Rogers return new LLVMCompiler(driver); 190b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#else 191b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray LOG(FATAL) << "Portable compiler not compiled"; 192b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif 193b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray break; 194b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray default: 195b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray LOG(FATAL) << "UNREACHABLE"; 196b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray } 197b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray return nullptr; 198b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 199b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 200b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 201