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