blob: f940b54f22a348aeabeb0292af089f26e672d173 [file] [log] [blame]
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001/*
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
23namespace art {
24
Ian Rogers72d32622014-05-06 16:20:11 -070025extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver);
26extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver);
27extern "C" art::CompiledMethod* ArtQuickCompileMethod(art::CompilerDriver* driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000028 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 Rogers72d32622014-05-06 16:20:11 -070036extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000037 uint32_t access_flags, uint32_t method_idx,
38 const art::DexFile& dex_file);
39
40// Hack for CFI CIE initialization
41extern std::vector<uint8_t>* X86CFIInitialization();
42
Ian Rogers72d32622014-05-06 16:20:11 -070043void QuickCompiler::Init() const {
44 ArtInitQuickCompilerContext(GetCompilerDriver());
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000045}
46
Ian Rogers72d32622014-05-06 16:20:11 -070047void QuickCompiler::UnInit() const {
48 ArtUnInitQuickCompilerContext(GetCompilerDriver());
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000049}
50
Ian Rogers72d32622014-05-06 16:20:11 -070051CompiledMethod* 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 Geoffrayb34f69a2014-03-07 15:28:39 +000059 access_flags,
60 invoke_type,
61 class_def_idx,
62 method_idx,
63 class_loader,
64 dex_file);
Ian Rogers72d32622014-05-06 16:20:11 -070065 if (method != nullptr) {
66 return method;
67 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000068
Ian Rogers72d32622014-05-06 16:20:11 -070069 return ArtQuickCompileMethod(GetCompilerDriver(),
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000070 code_item,
71 access_flags,
72 invoke_type,
73 class_def_idx,
74 method_idx,
75 class_loader,
76 dex_file);
77}
78
Ian Rogers72d32622014-05-06 16:20:11 -070079CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000080 uint32_t method_idx,
81 const DexFile& dex_file) const {
Ian Rogers72d32622014-05-06 16:20:11 -070082 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000083}
84
85uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
86 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode());
87}
88
89bool QuickCompiler::WriteElf(art::File* file,
Ian Rogers72d32622014-05-06 16:20:11 -070090 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 Geoffrayb34f69a2014-03-07 15:28:39 +000096}
97
98Backend* 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 Xu9e06c8c2014-05-06 18:06:07 +0100104 case kArm64:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100105 mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100106 break;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000107 case kMips:
108 mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
109 break;
110 case kX86:
Elena Sayapinadd644502014-07-01 18:39:52 +0700111 // Fall-through.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700112 case kX86_64:
Elena Sayapinadd644502014-07-01 18:39:52 +0700113 mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700114 break;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000115 default:
116 LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
117 }
118
119 /* The number of compiler temporaries depends on backend so set it up now if possible */
120 if (mir_to_lir) {
121 size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
122 bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
123 CHECK(set_max);
124 }
125 return mir_to_lir;
126}
127
128std::vector<uint8_t>* QuickCompiler::GetCallFrameInformationInitialization(
Nicolas Geoffray896362b2014-03-13 09:50:25 +0000129 const CompilerDriver& driver) const {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000130 if (driver.GetInstructionSet() == kX86) {
131 return X86CFIInitialization();
132 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700133 if (driver.GetInstructionSet() == kX86_64) {
134 return X86CFIInitialization();
135 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000136 return nullptr;
137}
138
Ian Rogers72d32622014-05-06 16:20:11 -0700139CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000140 uint32_t access_flags,
141 InvokeType invoke_type,
142 uint16_t class_def_idx,
143 uint32_t method_idx,
144 jobject class_loader,
145 const DexFile& dex_file) const {
Ian Rogers72d32622014-05-06 16:20:11 -0700146 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
147 method_idx, class_loader, dex_file);
148 if (method != nullptr) {
149 return method;
150 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000151
Ian Rogers72d32622014-05-06 16:20:11 -0700152 return QuickCompiler::Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
153 class_loader, dex_file);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000154}
155
156} // namespace art