blob: ced9f812c0e3bd45f1ffb8138e2b844d8194bd7d [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
18#define ART_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include "base/logging.h"
21#include "base/mutex.h"
22#include "dex/compiler_internals.h"
23#include "driver/compiler_driver.h"
24#include "driver/dex_compilation_unit.h"
25#include "globals.h"
26#include "instruction_set.h"
27#include "runtime_support_builder.h"
28#include "runtime_support_llvm_func.h"
29#include "safe_map.h"
30
31#include <UniquePtr.h>
32#include <string>
33#include <vector>
34
35namespace art {
36 class CompiledMethod;
37}
38
39namespace llvm {
40 class Function;
41 class LLVMContext;
42 class Module;
43 class raw_ostream;
44}
45
46namespace art {
47namespace llvm {
48
49class CompilerLLVM;
50class IRBuilder;
51
52class LlvmCompilationUnit {
53 public:
54 ~LlvmCompilationUnit();
55
56 uint32_t GetCompilationUnitId() const {
57 return cunit_id_;
58 }
59
60 InstructionSet GetInstructionSet() const;
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
74 void SetBitcodeFileName(const std::string& bitcode_filename) {
75 bitcode_filename_ = bitcode_filename;
76 }
77
78 LLVMInfo* GetQuickContext() const {
79 return llvm_info_.get();
80 }
81 void SetCompilerDriver(CompilerDriver* driver) {
82 driver_ = driver;
83 }
84 DexCompilationUnit* GetDexCompilationUnit() {
85 return dex_compilation_unit_;
86 }
87 void SetDexCompilationUnit(DexCompilationUnit* dex_compilation_unit) {
88 dex_compilation_unit_ = dex_compilation_unit;
89 }
90
91 bool Materialize();
92
93 bool IsMaterialized() const {
94 return !elf_object_.empty();
95 }
96
97 const std::string& GetElfObject() const {
98 DCHECK(IsMaterialized());
99 return elf_object_;
100 }
101
102 private:
103 LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
104 uint32_t cunit_id);
105
106 const CompilerLLVM* compiler_llvm_;
107 const uint32_t cunit_id_;
108
109 UniquePtr< ::llvm::LLVMContext> context_;
110 UniquePtr<IRBuilder> irb_;
111 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700112 ::llvm::Module* module_; // Managed by context_
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 UniquePtr<IntrinsicHelper> intrinsic_helper_;
114 UniquePtr<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
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700135} // namespace llvm
136} // namespace art
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700138#endif // ART_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_