Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #ifndef ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |
| 18 | #define ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |
| 19 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 21 | #include "base/mutex.h" |
Logan Chien | b9eaeea | 2012-03-17 19:45:01 +0800 | [diff] [blame] | 22 | #include "globals.h" |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 23 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 24 | # include "greenland/dex_lang.h" |
| 25 | #endif |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 26 | #include "instruction_set.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 27 | #include "runtime_support_builder.h" |
| 28 | #include "runtime_support_func.h" |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 29 | #include "safe_map.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 30 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 31 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 32 | # include "compiler/Dalvik.h" |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 33 | # include "compiler.h" |
| 34 | # include "oat_compilation_unit.h" |
| 35 | #endif |
| 36 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 37 | #include <UniquePtr.h> |
| 38 | #include <string> |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 39 | #include <vector> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 40 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 41 | namespace art { |
| 42 | class CompiledMethod; |
| 43 | } |
| 44 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 45 | namespace llvm { |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 46 | class Function; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 47 | class LLVMContext; |
| 48 | class Module; |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 49 | class raw_ostream; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | namespace art { |
| 53 | namespace compiler_llvm { |
| 54 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 55 | class CompilerLLVM; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 56 | class IRBuilder; |
| 57 | |
| 58 | class CompilationUnit { |
| 59 | public: |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 60 | CompilationUnit(const CompilerLLVM* compiler_llvm, |
| 61 | size_t cunit_idx); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 62 | |
| 63 | ~CompilationUnit(); |
| 64 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 65 | size_t GetIndex() const { |
| 66 | return cunit_idx_; |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 69 | InstructionSet GetInstructionSet() const; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 70 | |
| 71 | llvm::LLVMContext* GetLLVMContext() const { |
| 72 | return context_.get(); |
| 73 | } |
| 74 | |
| 75 | llvm::Module* GetModule() const { |
| 76 | return module_; |
| 77 | } |
| 78 | |
| 79 | IRBuilder* GetIRBuilder() const { |
| 80 | return irb_.get(); |
| 81 | } |
| 82 | |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 83 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 84 | greenland::DexLang::Context* GetDexLangContext() const { |
| 85 | return dex_lang_ctx_; |
| 86 | } |
| 87 | #endif |
| 88 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 89 | void SetBitcodeFileName(const std::string& bitcode_filename) { |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 90 | bitcode_filename_ = bitcode_filename; |
| 91 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 92 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 93 | #if defined(ART_USE_PORTABLE_COMPILER) |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 94 | LLVMInfo* GetQuickContext() const { |
| 95 | return llvm_info_.get(); |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 96 | } |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 97 | void SetCompiler(Compiler* compiler) { |
| 98 | compiler_ = compiler; |
| 99 | } |
| 100 | void SetOatCompilationUnit(OatCompilationUnit* oat_compilation_unit) { |
| 101 | oat_compilation_unit_ = oat_compilation_unit; |
| 102 | } |
| 103 | #endif |
| 104 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 105 | bool Materialize(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 106 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 107 | bool IsMaterialized() const { |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 108 | return !compiled_code_.empty(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 111 | const std::vector<uint8_t>& GetCompiledCode() const { |
| 112 | DCHECK(IsMaterialized()); |
| 113 | return compiled_code_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 114 | } |
| 115 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 116 | private: |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 117 | const CompilerLLVM* compiler_llvm_; |
| 118 | const size_t cunit_idx_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 119 | |
| 120 | UniquePtr<llvm::LLVMContext> context_; |
| 121 | UniquePtr<IRBuilder> irb_; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 122 | UniquePtr<RuntimeSupportBuilder> runtime_support_; |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 123 | llvm::Module* module_; // Managed by context_ |
| 124 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 125 | greenland::DexLang::Context* dex_lang_ctx_; |
| 126 | #endif |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 127 | #if defined(ART_USE_PORTABLE_COMPILER) |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 128 | UniquePtr<LLVMInfo> llvm_info_; |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 129 | Compiler* compiler_; |
| 130 | OatCompilationUnit* oat_compilation_unit_; |
| 131 | #endif |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 132 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 133 | std::string bitcode_filename_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 134 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 135 | std::vector<uint8_t> compiled_code_; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 136 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 137 | SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 138 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 139 | void CheckCodeAlign(uint32_t offset) const; |
| 140 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 141 | bool MaterializeToString(std::string& str_buffer); |
| 142 | bool MaterializeToRawOStream(llvm::raw_ostream& out_stream); |
| 143 | |
| 144 | bool ExtractCodeAndPrelink(const std::string& elf_image); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace compiler_llvm |
| 148 | } // namespace art |
| 149 | |
| 150 | #endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |