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