Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "compilers.h" |
| 18 | #include "dex/mir_graph.h" |
| 19 | #include "dex/quick/mir_to_lir.h" |
| 20 | #include "elf_writer_quick.h" |
| 21 | #include "mirror/art_method-inl.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 25 | extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver); |
| 26 | extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver); |
| 27 | extern "C" art::CompiledMethod* ArtQuickCompileMethod(art::CompilerDriver* driver, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 28 | const art::DexFile::CodeItem* code_item, |
| 29 | uint32_t access_flags, |
| 30 | art::InvokeType invoke_type, |
| 31 | uint16_t class_def_idx, |
| 32 | uint32_t method_idx, |
| 33 | jobject class_loader, |
| 34 | const art::DexFile& dex_file); |
| 35 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 36 | extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* driver, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 37 | uint32_t access_flags, uint32_t method_idx, |
| 38 | const art::DexFile& dex_file); |
| 39 | |
| 40 | // Hack for CFI CIE initialization |
| 41 | extern std::vector<uint8_t>* X86CFIInitialization(); |
| 42 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 43 | void QuickCompiler::Init() const { |
| 44 | ArtInitQuickCompilerContext(GetCompilerDriver()); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 47 | void QuickCompiler::UnInit() const { |
| 48 | ArtUnInitQuickCompilerContext(GetCompilerDriver()); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 51 | CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item, |
| 52 | uint32_t access_flags, |
| 53 | InvokeType invoke_type, |
| 54 | uint16_t class_def_idx, |
| 55 | uint32_t method_idx, |
| 56 | jobject class_loader, |
| 57 | const DexFile& dex_file) const { |
| 58 | CompiledMethod* method = TryCompileWithSeaIR(code_item, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 59 | access_flags, |
| 60 | invoke_type, |
| 61 | class_def_idx, |
| 62 | method_idx, |
| 63 | class_loader, |
| 64 | dex_file); |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 65 | if (method != nullptr) { |
| 66 | return method; |
| 67 | } |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 68 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 69 | return ArtQuickCompileMethod(GetCompilerDriver(), |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 70 | code_item, |
| 71 | access_flags, |
| 72 | invoke_type, |
| 73 | class_def_idx, |
| 74 | method_idx, |
| 75 | class_loader, |
| 76 | dex_file); |
| 77 | } |
| 78 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 79 | CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 80 | uint32_t method_idx, |
| 81 | const DexFile& dex_file) const { |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 82 | return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { |
| 86 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()); |
| 87 | } |
| 88 | |
| 89 | bool QuickCompiler::WriteElf(art::File* file, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 90 | OatWriter* oat_writer, |
| 91 | const std::vector<const art::DexFile*>& dex_files, |
| 92 | const std::string& android_root, |
| 93 | bool is_host) const { |
| 94 | return art::ElfWriterQuick::Create(file, oat_writer, dex_files, android_root, is_host, |
| 95 | *GetCompilerDriver()); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Backend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const { |
| 99 | Mir2Lir* mir_to_lir = nullptr; |
| 100 | switch (cu->instruction_set) { |
| 101 | case kThumb2: |
| 102 | mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 103 | break; |
Zheng Xu | 9e06c8c | 2014-05-06 18:06:07 +0100 | [diff] [blame^] | 104 | case kArm64: |
| 105 | // TODO(Arm64): replace the generator below with a proper one. |
| 106 | mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 107 | break; |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 108 | case kMips: |
| 109 | mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 110 | break; |
| 111 | case kX86: |
| 112 | mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 113 | break; |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 114 | case kX86_64: |
| 115 | mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 116 | break; |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 117 | default: |
| 118 | LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set; |
| 119 | } |
| 120 | |
| 121 | /* The number of compiler temporaries depends on backend so set it up now if possible */ |
| 122 | if (mir_to_lir) { |
| 123 | size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps(); |
| 124 | bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps); |
| 125 | CHECK(set_max); |
| 126 | } |
| 127 | return mir_to_lir; |
| 128 | } |
| 129 | |
| 130 | std::vector<uint8_t>* QuickCompiler::GetCallFrameInformationInitialization( |
Nicolas Geoffray | 896362b | 2014-03-13 09:50:25 +0000 | [diff] [blame] | 131 | const CompilerDriver& driver) const { |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 132 | if (driver.GetInstructionSet() == kX86) { |
| 133 | return X86CFIInitialization(); |
| 134 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 135 | if (driver.GetInstructionSet() == kX86_64) { |
| 136 | return X86CFIInitialization(); |
| 137 | } |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 138 | return nullptr; |
| 139 | } |
| 140 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 141 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 142 | uint32_t access_flags, |
| 143 | InvokeType invoke_type, |
| 144 | uint16_t class_def_idx, |
| 145 | uint32_t method_idx, |
| 146 | jobject class_loader, |
| 147 | const DexFile& dex_file) const { |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 148 | CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx, |
| 149 | method_idx, class_loader, dex_file); |
| 150 | if (method != nullptr) { |
| 151 | return method; |
| 152 | } |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 153 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 154 | return QuickCompiler::Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx, |
| 155 | class_loader, dex_file); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace art |