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_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
18 #define ART_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "base/logging.h"
25 #include "base/mutex.h"
26 #include "dex/compiler_internals.h"
27 #include "driver/compiler_driver.h"
28 #include "driver/dex_compilation_unit.h"
29 #include "globals.h"
30 #include "instruction_set.h"
31 #include "runtime_support_builder.h"
32 #include "runtime_support_llvm_func.h"
33 #include "safe_map.h"
34 
35 namespace art {
36   class CompiledMethod;
37 }
38 
39 namespace llvm {
40   class Function;
41   class LLVMContext;
42   class Module;
43   class raw_ostream;
44 }
45 
46 namespace art {
47 namespace llvm {
48 
49 class CompilerLLVM;
50 class IRBuilder;
51 
52 class LlvmCompilationUnit {
53  public:
54   ~LlvmCompilationUnit();
55 
GetCompilationUnitId()56   uint32_t GetCompilationUnitId() const {
57     return cunit_id_;
58   }
59 
60   InstructionSet GetInstructionSet() const;
61 
GetLLVMContext()62   ::llvm::LLVMContext* GetLLVMContext() const {
63     return context_.get();
64   }
65 
GetModule()66   ::llvm::Module* GetModule() const {
67     return module_;
68   }
69 
GetIRBuilder()70   IRBuilder* GetIRBuilder() const {
71     return irb_.get();
72   }
73 
SetBitcodeFileName(const std::string & bitcode_filename)74   void SetBitcodeFileName(const std::string& bitcode_filename) {
75     bitcode_filename_ = bitcode_filename;
76   }
77 
GetQuickContext()78   LLVMInfo* GetQuickContext() const {
79     return llvm_info_.get();
80   }
SetCompilerDriver(CompilerDriver * driver)81   void SetCompilerDriver(CompilerDriver* driver) {
82     driver_ = driver;
83   }
GetDexCompilationUnit()84   DexCompilationUnit* GetDexCompilationUnit() {
85     return dex_compilation_unit_;
86   }
SetDexCompilationUnit(DexCompilationUnit * dex_compilation_unit)87   void SetDexCompilationUnit(DexCompilationUnit* dex_compilation_unit) {
88     dex_compilation_unit_ = dex_compilation_unit;
89   }
90 
91   bool Materialize();
92 
IsMaterialized()93   bool IsMaterialized() const {
94     return !elf_object_.empty();
95   }
96 
GetElfObject()97   const std::string& GetElfObject() const {
98     DCHECK(IsMaterialized());
99     return elf_object_;
100   }
101 
102  private:
103   LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
104                       size_t cunit_id);
105 
106   const CompilerLLVM* compiler_llvm_;
107   const size_t cunit_id_;
108 
109   std::unique_ptr< ::llvm::LLVMContext> context_;
110   std::unique_ptr<IRBuilder> irb_;
111   std::unique_ptr<RuntimeSupportBuilder> runtime_support_;
112   ::llvm::Module* module_;  // Managed by context_
113   std::unique_ptr<IntrinsicHelper> intrinsic_helper_;
114   std::unique_ptr<LLVMInfo> llvm_info_;
115   CompilerDriver* driver_;
116   DexCompilationUnit* dex_compilation_unit_;
117 
118   std::string bitcode_filename_;
119 
120   std::string elf_object_;
121 
122   SafeMap<const ::llvm::Function*, CompiledMethod*> compiled_methods_map_;
123 
124   void CheckCodeAlign(uint32_t offset) const;
125 
126   void DumpBitcodeToFile();
127   void DumpBitcodeToString(std::string& str_buffer);
128 
129   bool MaterializeToString(std::string& str_buffer);
130   bool MaterializeToRawOStream(::llvm::raw_ostream& out_stream);
131 
132   friend class CompilerLLVM;  // For LlvmCompilationUnit constructor
133 };
134 
135 }  // namespace llvm
136 }  // namespace art
137 
138 #endif  // ART_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
139