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