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
4235e1e6ad4b50f1adbe9f93fe467766f042491896Tong Shenextern std::vector<uint8_t>* X86CFIInitialization(bool is_x86_64);
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 {
87e832e64a7e82d7f72aedbd7d798fb929d458ee8fMathieu Chartier  size_t pointer_size = InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet());
88e832e64a7e82d7f72aedbd7d798fb929d458ee8fMathieu Chartier  return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
89e832e64a7e82d7f72aedbd7d798fb929d458ee8fMathieu Chartier      pointer_size));
90b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
91b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
92b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraybool QuickCompiler::WriteElf(art::File* file,
9372d32629303f8f39362a4099481f48646aed042fIan Rogers                             OatWriter* oat_writer,
9472d32629303f8f39362a4099481f48646aed042fIan Rogers                             const std::vector<const art::DexFile*>& dex_files,
9572d32629303f8f39362a4099481f48646aed042fIan Rogers                             const std::string& android_root,
9672d32629303f8f39362a4099481f48646aed042fIan Rogers                             bool is_host) const {
9772d32629303f8f39362a4099481f48646aed042fIan Rogers  return art::ElfWriterQuick::Create(file, oat_writer, dex_files, android_root, is_host,
9872d32629303f8f39362a4099481f48646aed042fIan Rogers                                     *GetCompilerDriver());
99b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
100b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
101b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas GeoffrayBackend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
102b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  Mir2Lir* mir_to_lir = nullptr;
103b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  switch (cu->instruction_set) {
104b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kThumb2:
105b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
106b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      break;
1079e06c8cd4a2e1471754470e09aaab63c0795b4afZheng Xu    case kArm64:
108e45fb9e7976c8462b94a58ad60b006b0eacec49fMatteo Franchin      mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
1099e06c8cd4a2e1471754470e09aaab63c0795b4afZheng Xu      break;
110b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kMips:
111b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
112b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      break;
113b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    case kX86:
114dd64450b37776f68b9bfc47f8d9a88bc72c95727Elena Sayapina      // Fall-through.
1156a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    case kX86_64:
116dd64450b37776f68b9bfc47f8d9a88bc72c95727Elena Sayapina      mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
1176a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko      break;
118b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    default:
119b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray      LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
120b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
121b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
122b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  /* The number of compiler temporaries depends on backend so set it up now if possible */
123b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  if (mir_to_lir) {
124b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
125b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
126b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    CHECK(set_max);
127b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
128b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  return mir_to_lir;
129b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
130b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
131b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraystd::vector<uint8_t>* QuickCompiler::GetCallFrameInformationInitialization(
132896362bbfa11b66b45f3ce5a9e8570a878d49e07Nicolas Geoffray    const CompilerDriver& driver) const {
133b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  if (driver.GetInstructionSet() == kX86) {
13435e1e6ad4b50f1adbe9f93fe467766f042491896Tong Shen    return X86CFIInitialization(false);
135b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  }
1366a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko  if (driver.GetInstructionSet() == kX86_64) {
13735e1e6ad4b50f1adbe9f93fe467766f042491896Tong Shen    return X86CFIInitialization(true);
1386a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko  }
139b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  return nullptr;
140b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
141b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
14272d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
143b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint32_t access_flags,
144b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            InvokeType invoke_type,
145b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint16_t class_def_idx,
146b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            uint32_t method_idx,
147b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            jobject class_loader,
148b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                            const DexFile& dex_file) const {
14972d32629303f8f39362a4099481f48646aed042fIan Rogers  CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
15072d32629303f8f39362a4099481f48646aed042fIan Rogers                                      method_idx, class_loader, dex_file);
15172d32629303f8f39362a4099481f48646aed042fIan Rogers  if (method != nullptr) {
15272d32629303f8f39362a4099481f48646aed042fIan Rogers    return method;
15372d32629303f8f39362a4099481f48646aed042fIan Rogers  }
15472d32629303f8f39362a4099481f48646aed042fIan Rogers
15572d32629303f8f39362a4099481f48646aed042fIan Rogers  return QuickCompiler::Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
15672d32629303f8f39362a4099481f48646aed042fIan Rogers                                class_loader, dex_file);
157b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
158b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
159b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
160