blob: 2481128e4d4d338d24031f9cac808181a89cb59c [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070018
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000019#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
24namespace art {
25
Ian Rogers72d32622014-05-06 16:20:11 -070026extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver);
27extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver);
28extern "C" art::CompiledMethod* ArtQuickCompileMethod(art::CompilerDriver* driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000029 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 Rogers72d32622014-05-06 16:20:11 -070037extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000038 uint32_t access_flags, uint32_t method_idx,
39 const art::DexFile& dex_file);
40
41// Hack for CFI CIE initialization
Tong Shen35e1e6a2014-07-30 09:31:22 -070042extern std::vector<uint8_t>* X86CFIInitialization(bool is_x86_64);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000043
Ian Rogers72d32622014-05-06 16:20:11 -070044void QuickCompiler::Init() const {
45 ArtInitQuickCompilerContext(GetCompilerDriver());
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000046}
47
Ian Rogers72d32622014-05-06 16:20:11 -070048void QuickCompiler::UnInit() const {
49 ArtUnInitQuickCompilerContext(GetCompilerDriver());
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000050}
51
Ian Rogers72d32622014-05-06 16:20:11 -070052CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item,
53 uint32_t access_flags,
54 InvokeType invoke_type,
55 uint16_t class_def_idx,
56 uint32_t method_idx,
57 jobject class_loader,
58 const DexFile& dex_file) const {
59 CompiledMethod* method = TryCompileWithSeaIR(code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000060 access_flags,
61 invoke_type,
62 class_def_idx,
63 method_idx,
64 class_loader,
65 dex_file);
Ian Rogers72d32622014-05-06 16:20:11 -070066 if (method != nullptr) {
67 return method;
68 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000069
Ian Rogers72d32622014-05-06 16:20:11 -070070 return ArtQuickCompileMethod(GetCompilerDriver(),
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000071 code_item,
72 access_flags,
73 invoke_type,
74 class_def_idx,
75 method_idx,
76 class_loader,
77 dex_file);
78}
79
Ian Rogers72d32622014-05-06 16:20:11 -070080CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000081 uint32_t method_idx,
82 const DexFile& dex_file) const {
Ian Rogers72d32622014-05-06 16:20:11 -070083 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000084}
85
86uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Mathieu Chartiere832e642014-11-10 11:08:06 -080087 size_t pointer_size = InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet());
88 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
89 pointer_size));
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000090}
91
92bool QuickCompiler::WriteElf(art::File* file,
Ian Rogers72d32622014-05-06 16:20:11 -070093 OatWriter* oat_writer,
94 const std::vector<const art::DexFile*>& dex_files,
95 const std::string& android_root,
96 bool is_host) const {
97 return art::ElfWriterQuick::Create(file, oat_writer, dex_files, android_root, is_host,
98 *GetCompilerDriver());
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000099}
100
101Backend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
102 Mir2Lir* mir_to_lir = nullptr;
103 switch (cu->instruction_set) {
104 case kThumb2:
105 mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
106 break;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100107 case kArm64:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100108 mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100109 break;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000110 case kMips:
111 mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
112 break;
113 case kX86:
Elena Sayapinadd644502014-07-01 18:39:52 +0700114 // Fall-through.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700115 case kX86_64:
Elena Sayapinadd644502014-07-01 18:39:52 +0700116 mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700117 break;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000118 default:
119 LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
120 }
121
122 /* The number of compiler temporaries depends on backend so set it up now if possible */
123 if (mir_to_lir) {
124 size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
125 bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
126 CHECK(set_max);
127 }
128 return mir_to_lir;
129}
130
131std::vector<uint8_t>* QuickCompiler::GetCallFrameInformationInitialization(
Nicolas Geoffray896362b2014-03-13 09:50:25 +0000132 const CompilerDriver& driver) const {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000133 if (driver.GetInstructionSet() == kX86) {
Tong Shen35e1e6a2014-07-30 09:31:22 -0700134 return X86CFIInitialization(false);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000135 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700136 if (driver.GetInstructionSet() == kX86_64) {
Tong Shen35e1e6a2014-07-30 09:31:22 -0700137 return X86CFIInitialization(true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700138 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000139 return nullptr;
140}
141
Ian Rogers72d32622014-05-06 16:20:11 -0700142CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000143 uint32_t access_flags,
144 InvokeType invoke_type,
145 uint16_t class_def_idx,
146 uint32_t method_idx,
147 jobject class_loader,
148 const DexFile& dex_file) const {
Ian Rogers72d32622014-05-06 16:20:11 -0700149 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
150 method_idx, class_loader, dex_file);
151 if (method != nullptr) {
152 return method;
153 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000154
Ian Rogers72d32622014-05-06 16:20:11 -0700155 return QuickCompiler::Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
156 class_loader, dex_file);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000157}
158
159} // namespace art