compilers.cc revision 98d1cc8033251c93786e2fa8c59a2e555a9493be
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 "compilers.h"
1898d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang
19b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "dex/mir_graph.h"
20b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "dex/quick/mir_to_lir.h"
21b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "elf_writer_quick.h"
22b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "mirror/art_method-inl.h"
23b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
24b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
25b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
2672d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver);
2772d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver);
2872d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" art::CompiledMethod* ArtQuickCompileMethod(art::CompilerDriver* driver,
29b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      const art::DexFile::CodeItem* code_item,
30b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      uint32_t access_flags,
31b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      art::InvokeType invoke_type,
32b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      uint16_t class_def_idx,
33b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      uint32_t method_idx,
34b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      jobject class_loader,
35b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                      const art::DexFile& dex_file);
36b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
3772d32629303f8f39362a4099481f48646aed042fIan Rogersextern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* driver,
38b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                         uint32_t access_flags, uint32_t method_idx,
39b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                                         const art::DexFile& dex_file);
40b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
41b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray// Hack for CFI CIE initialization
42b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffrayextern std::vector<uint8_t>* X86CFIInitialization();
43b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
4472d32629303f8f39362a4099481f48646aed042fIan Rogersvoid QuickCompiler::Init() const {
4572d32629303f8f39362a4099481f48646aed042fIan Rogers  ArtInitQuickCompilerContext(GetCompilerDriver());
46b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
47b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
4872d32629303f8f39362a4099481f48646aed042fIan Rogersvoid QuickCompiler::UnInit() const {
4972d32629303f8f39362a4099481f48646aed042fIan Rogers  ArtUnInitQuickCompilerContext(GetCompilerDriver());
50b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
51b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
5272d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item,
5372d32629303f8f39362a4099481f48646aed042fIan Rogers                                       uint32_t access_flags,
5472d32629303f8f39362a4099481f48646aed042fIan Rogers                                       InvokeType invoke_type,
5572d32629303f8f39362a4099481f48646aed042fIan Rogers                                       uint16_t class_def_idx,
5672d32629303f8f39362a4099481f48646aed042fIan Rogers                                       uint32_t method_idx,
5772d32629303f8f39362a4099481f48646aed042fIan Rogers                                       jobject class_loader,
5872d32629303f8f39362a4099481f48646aed042fIan Rogers                                       const DexFile& dex_file) const {
5972d32629303f8f39362a4099481f48646aed042fIan Rogers  CompiledMethod* method = TryCompileWithSeaIR(code_item,
60b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               access_flags,
61b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               invoke_type,
62b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               class_def_idx,
63b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               method_idx,
64b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               class_loader,
65b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               dex_file);
6672d32629303f8f39362a4099481f48646aed042fIan Rogers  if (method != nullptr) {
6772d32629303f8f39362a4099481f48646aed042fIan Rogers    return method;
6872d32629303f8f39362a4099481f48646aed042fIan Rogers  }
69b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
7072d32629303f8f39362a4099481f48646aed042fIan Rogers  return ArtQuickCompileMethod(GetCompilerDriver(),
71b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               code_item,
72b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               access_flags,
73b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               invoke_type,
74b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               class_def_idx,
75b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               method_idx,
76b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               class_loader,
77b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                               dex_file);
78b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
79b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
8072d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
81b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                          uint32_t method_idx,
82b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                          const DexFile& dex_file) const {
8372d32629303f8f39362a4099481f48646aed042fIan Rogers  return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
84b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
85b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
86b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffrayuintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
87b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode());
88b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
89b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
90b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraybool QuickCompiler::WriteElf(art::File* file,
9172d32629303f8f39362a4099481f48646aed042fIan Rogers                             OatWriter* oat_writer,
9272d32629303f8f39362a4099481f48646aed042fIan Rogers                             const std::vector<const art::DexFile*>& dex_files,
9372d32629303f8f39362a4099481f48646aed042fIan Rogers                             const std::string& android_root,
9472d32629303f8f39362a4099481f48646aed042fIan Rogers                             bool is_host) const {
9572d32629303f8f39362a4099481f48646aed042fIan Rogers  return art::ElfWriterQuick::Create(file, oat_writer, dex_files, android_root, is_host,
9672d32629303f8f39362a4099481f48646aed042fIan Rogers                                     *GetCompilerDriver());
97b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
98b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
99b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas GeoffrayBackend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
100b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  Mir2Lir* mir_to_lir = nullptr;
101b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  switch (cu->instruction_set) {
102b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kThumb2:
103b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
104b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      break;
1059e06c8cd4a2e1471754470e09aaab63c0795b4afZheng Xu    case kArm64:
106e45fb9e7976c8462b94a58ad60b006b0eacec49fMatteo Franchin      mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
1079e06c8cd4a2e1471754470e09aaab63c0795b4afZheng Xu      break;
108b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kMips:
109b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
110b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      break;
111b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kX86:
112dd64450b37776f68b9bfc47f8d9a88bc72c95727Elena Sayapina      // Fall-through.
1136a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    case kX86_64:
114dd64450b37776f68b9bfc47f8d9a88bc72c95727Elena Sayapina      mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
1156a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      break;
116b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    default:
117b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
118b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
119b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
120b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  /* The number of compiler temporaries depends on backend so set it up now if possible */
121b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  if (mir_to_lir) {
122b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
123b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
124b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    CHECK(set_max);
125b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
126b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  return mir_to_lir;
127b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
128b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
129b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraystd::vector<uint8_t>* QuickCompiler::GetCallFrameInformationInitialization(
130896362bbfa11b66b45f3ce5a9e8570a878d49e07Nicolas Geoffray    const CompilerDriver& driver) const {
131b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  if (driver.GetInstructionSet() == kX86) {
132b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    return X86CFIInitialization();
133b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
1346a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko  if (driver.GetInstructionSet() == kX86_64) {
1356a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    return X86CFIInitialization();
1366a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko  }
137b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  return nullptr;
138b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
139b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
14072d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
141b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint32_t access_flags,
142b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            InvokeType invoke_type,
143b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint16_t class_def_idx,
144b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint32_t method_idx,
145b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            jobject class_loader,
146b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            const DexFile& dex_file) const {
14772d32629303f8f39362a4099481f48646aed042fIan Rogers  CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
14872d32629303f8f39362a4099481f48646aed042fIan Rogers                                      method_idx, class_loader, dex_file);
14972d32629303f8f39362a4099481f48646aed042fIan Rogers  if (method != nullptr) {
15072d32629303f8f39362a4099481f48646aed042fIan Rogers    return method;
15172d32629303f8f39362a4099481f48646aed042fIan Rogers  }
15272d32629303f8f39362a4099481f48646aed042fIan Rogers
15372d32629303f8f39362a4099481f48646aed042fIan Rogers  return QuickCompiler::Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
15472d32629303f8f39362a4099481f48646aed042fIan Rogers                                class_loader, dex_file);
155b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
156b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
157b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
158