compiler.h revision 53c913bb71b218714823c8c87a1f92830c336f61
1f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray/*
2f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray *
4f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * you may not use this file except in compliance with the License.
6f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * You may obtain a copy of the License at
7f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray *
8f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray *
10f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * See the License for the specific language governing permissions and
14f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray * limitations under the License.
15f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray */
16f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
17b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#ifndef ART_COMPILER_COMPILER_H_
18b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#define ART_COMPILER_COMPILER_H_
19f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
20f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray#include "dex_file.h"
21f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray#include "os.h"
22f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
23f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffraynamespace art {
24f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
25f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass Backend;
26b48b9eb6d181a1f52e2e605cf26a21505f1d46edIan Rogersstruct CompilationUnit;
27f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass CompilerDriver;
28f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass CompiledMethod;
29f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass OatWriter;
30f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
31f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffraynamespace mirror {
32f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  class ArtMethod;
33f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray}
34f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
3553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe// Base class for compiler-specific thread-local storage for compiler worker threads
3653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass CompilerTls {
3753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  public:
3853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe    CompilerTls() {}
3953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe    ~CompilerTls() {}
4053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
4153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
42b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffrayclass Compiler {
43f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray public:
44f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  enum Kind {
45f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    kQuick,
46b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray    kOptimizing,
47f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    kPortable
48f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  };
49f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
5072d32629303f8f39362a4099481f48646aed042fIan Rogers  static Compiler* Create(CompilerDriver* driver, Kind kind);
51f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
5272d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual void Init() const = 0;
53f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
5472d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual void UnInit() const = 0;
55f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
5653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu)
5753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      const = 0;
5853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
5972d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual CompiledMethod* Compile(const DexFile::CodeItem* code_item,
60f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint32_t access_flags,
61f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  InvokeType invoke_type,
62f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint16_t class_def_idx,
63f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint32_t method_idx,
64f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  jobject class_loader,
65f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  const DexFile& dex_file) const = 0;
66f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
6772d32629303f8f39362a4099481f48646aed042fIan Rogers  static CompiledMethod* TryCompileWithSeaIR(const art::DexFile::CodeItem* code_item,
68b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             uint32_t access_flags,
69b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             art::InvokeType invoke_type,
70b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             uint16_t class_def_idx,
71b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             uint32_t method_idx,
72b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             jobject class_loader,
73b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                             const art::DexFile& dex_file);
74b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
7572d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual CompiledMethod* JniCompile(uint32_t access_flags,
76f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                     uint32_t method_idx,
77f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                     const DexFile& dex_file) const = 0;
78f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
79b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers  virtual uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const
80b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
81f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
82f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  virtual bool WriteElf(art::File* file,
833d504075f7c1204d581923460754bf6d3714b13fIan Rogers                        OatWriter* oat_writer,
84f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                        const std::vector<const art::DexFile*>& dex_files,
85f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                        const std::string& android_root,
8672d32629303f8f39362a4099481f48646aed042fIan Rogers                        bool is_host) const
87f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
88f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
8972d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const = 0;
90f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
91f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  uint64_t GetMaximumCompilationTimeBeforeWarning() const {
92f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    return maximum_compilation_time_before_warning_;
93f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  }
94f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
953d504075f7c1204d581923460754bf6d3714b13fIan Rogers  virtual bool IsPortable() const {
963d504075f7c1204d581923460754bf6d3714b13fIan Rogers    return false;
973d504075f7c1204d581923460754bf6d3714b13fIan Rogers  }
983d504075f7c1204d581923460754bf6d3714b13fIan Rogers
993d504075f7c1204d581923460754bf6d3714b13fIan Rogers  void SetBitcodeFileName(const CompilerDriver& driver, const std::string& filename) {
1003d504075f7c1204d581923460754bf6d3714b13fIan Rogers    UNUSED(driver);
101f3e2cc4a38389aa75eb8ee3973a535254bf1c8d2Nicolas Geoffray    UNUSED(filename);
102f3e2cc4a38389aa75eb8ee3973a535254bf1c8d2Nicolas Geoffray  }
103f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
104f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  virtual void InitCompilationUnit(CompilationUnit& cu) const = 0;
105f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
106b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  virtual ~Compiler() {}
107f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
108ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  /*
109ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @brief Generate and return Dwarf CFI initialization, if supported by the
110ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * backend.
111ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @param driver CompilerDriver for this compile.
112ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @returns nullptr if not supported by backend or a vector of bytes for CFI DWARF
113ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * information.
114ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @note This is used for backtrace information in generated code.
115ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   */
116ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  virtual std::vector<uint8_t>* GetCallFrameInformationInitialization(const CompilerDriver& driver)
117ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell      const {
118ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    return nullptr;
119ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  }
120ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
12153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  virtual CompilerTls* CreateNewCompilerTls() {
12253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe    return nullptr;
12353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  }
12453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
12572d32629303f8f39362a4099481f48646aed042fIan Rogers protected:
12672d32629303f8f39362a4099481f48646aed042fIan Rogers  explicit Compiler(CompilerDriver* driver, uint64_t warning) :
12772d32629303f8f39362a4099481f48646aed042fIan Rogers      driver_(driver), maximum_compilation_time_before_warning_(warning) {
12872d32629303f8f39362a4099481f48646aed042fIan Rogers  }
12972d32629303f8f39362a4099481f48646aed042fIan Rogers
13072d32629303f8f39362a4099481f48646aed042fIan Rogers  CompilerDriver* GetCompilerDriver() const {
13172d32629303f8f39362a4099481f48646aed042fIan Rogers    return driver_;
13272d32629303f8f39362a4099481f48646aed042fIan Rogers  }
13372d32629303f8f39362a4099481f48646aed042fIan Rogers
134f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray private:
13572d32629303f8f39362a4099481f48646aed042fIan Rogers  CompilerDriver* const driver_;
1363d504075f7c1204d581923460754bf6d3714b13fIan Rogers  const uint64_t maximum_compilation_time_before_warning_;
137f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
138b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(Compiler);
139f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray};
140f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
141f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray}  // namespace art
142f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
143b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif  // ART_COMPILER_COMPILER_H_
144