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
25d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraynamespace jit {
26d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  class JitCodeCache;
27d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
28d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
29e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierclass ArtMethod;
30f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass CompilerDriver;
31f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass CompiledMethod;
32f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffrayclass OatWriter;
33f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
34b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffrayclass Compiler {
35f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray public:
36f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  enum Kind {
37f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    kQuick,
38956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    kOptimizing
39f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  };
40f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
4172d32629303f8f39362a4099481f48646aed042fIan Rogers  static Compiler* Create(CompilerDriver* driver, Kind kind);
42f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
43ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  virtual void Init() = 0;
44f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
4572d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual void UnInit() const = 0;
46f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
47df739841b781dbd49c247e3795696389c4799020Vladimir Marko  virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
4853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
4972d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual CompiledMethod* Compile(const DexFile::CodeItem* code_item,
50f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint32_t access_flags,
51f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  InvokeType invoke_type,
52f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint16_t class_def_idx,
53f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  uint32_t method_idx,
54f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                  jobject class_loader,
55736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                  const DexFile& dex_file,
56736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                  Handle<mirror::DexCache> dex_cache) const = 0;
57f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
5872d32629303f8f39362a4099481f48646aed042fIan Rogers  virtual CompiledMethod* JniCompile(uint32_t access_flags,
59f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                     uint32_t method_idx,
60f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray                                     const DexFile& dex_file) const = 0;
61f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
62d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  virtual bool JitCompile(Thread* self ATTRIBUTE_UNUSED,
63d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                          jit::JitCodeCache* code_cache ATTRIBUTE_UNUSED,
64b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                          ArtMethod* method ATTRIBUTE_UNUSED,
65b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                          bool osr ATTRIBUTE_UNUSED)
66d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      SHARED_REQUIRES(Locks::mutator_lock_) {
67d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    return false;
68d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
69d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
70e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  virtual uintptr_t GetEntryPointOf(ArtMethod* method) const
7190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier     SHARED_REQUIRES(Locks::mutator_lock_) = 0;
72f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
73f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  uint64_t GetMaximumCompilationTimeBeforeWarning() const {
74f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray    return maximum_compilation_time_before_warning_;
75f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray  }
76f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
77b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  virtual ~Compiler() {}
78f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
79ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  /*
80ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @brief Generate and return Dwarf CFI initialization, if supported by the
81ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * backend.
82ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @param driver CompilerDriver for this compile.
83ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @returns nullptr if not supported by backend or a vector of bytes for CFI DWARF
84ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * information.
85ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   * @note This is used for backtrace information in generated code.
86ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell   */
874b8f1ecd3aa5a29ec1463ff88fee9db365f257dcRoland Levillain  virtual std::vector<uint8_t>* GetCallFrameInformationInitialization(
884b8f1ecd3aa5a29ec1463ff88fee9db365f257dcRoland Levillain      const CompilerDriver& driver ATTRIBUTE_UNUSED) const {
89ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    return nullptr;
90ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  }
91ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
92b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  // Returns whether the method to compile is such a pathological case that
93b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  // it's not worth compiling.
94b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  static bool IsPathologicalCase(const DexFile::CodeItem& code_item,
95b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray                                 uint32_t method_idx,
96b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray                                 const DexFile& dex_file);
97b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
9872d32629303f8f39362a4099481f48646aed042fIan Rogers protected:
993887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  Compiler(CompilerDriver* driver, uint64_t warning) :
10072d32629303f8f39362a4099481f48646aed042fIan Rogers      driver_(driver), maximum_compilation_time_before_warning_(warning) {
10172d32629303f8f39362a4099481f48646aed042fIan Rogers  }
10272d32629303f8f39362a4099481f48646aed042fIan Rogers
10372d32629303f8f39362a4099481f48646aed042fIan Rogers  CompilerDriver* GetCompilerDriver() const {
10472d32629303f8f39362a4099481f48646aed042fIan Rogers    return driver_;
10572d32629303f8f39362a4099481f48646aed042fIan Rogers  }
10672d32629303f8f39362a4099481f48646aed042fIan Rogers
107f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray private:
10872d32629303f8f39362a4099481f48646aed042fIan Rogers  CompilerDriver* const driver_;
1093d504075f7c1204d581923460754bf6d3714b13fIan Rogers  const uint64_t maximum_compilation_time_before_warning_;
110f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
111b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(Compiler);
112f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray};
113f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
114f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray}  // namespace art
115f5df8974173124faddb8e2b6a331959afdb94fdfNicolas Geoffray
116b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#endif  // ART_COMPILER_COMPILER_H_
117