blob: 8cc3db7d1a3247f7adaa8411858426242ffc1627 [file] [log] [blame]
Logan Chien8b977d32012-02-21 19:14:55 +08001/*
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 Carlstrom641ce032013-01-31 15:21:37 -080017#ifndef ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
18#define ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
Logan Chien8b977d32012-02-21 19:14:55 +080019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
Ian Rogersc928de92013-02-27 14:30:44 -080022#include "compiler/compiler_internals.h"
23#include "compiler.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080024#include "globals.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "instruction_set.h"
Ian Rogersc928de92013-02-27 14:30:44 -080026#include "oat_compilation_unit.h"
TDYa127d668a062012-04-13 12:36:57 -070027#include "runtime_support_builder.h"
28#include "runtime_support_func.h"
Logan Chien110bcba2012-04-16 19:11:28 +080029#include "safe_map.h"
Logan Chien8b977d32012-02-21 19:14:55 +080030
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070031
Logan Chien8b977d32012-02-21 19:14:55 +080032#include <UniquePtr.h>
33#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080034#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080035
Logan Chien110bcba2012-04-16 19:11:28 +080036namespace art {
37 class CompiledMethod;
38}
39
Logan Chien8b977d32012-02-21 19:14:55 +080040namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080041 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080042 class LLVMContext;
43 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080044 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080045}
46
47namespace art {
48namespace compiler_llvm {
49
Logan Chien971bf3f2012-05-01 15:47:55 +080050class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080051class IRBuilder;
52
Brian Carlstrom641ce032013-01-31 15:21:37 -080053class LlvmCompilationUnit {
Logan Chien8b977d32012-02-21 19:14:55 +080054 public:
Brian Carlstrom641ce032013-01-31 15:21:37 -080055 ~LlvmCompilationUnit();
Logan Chien8b977d32012-02-21 19:14:55 +080056
Logan Chien971bf3f2012-05-01 15:47:55 +080057 size_t GetIndex() const {
58 return cunit_idx_;
Logan Chien6546ec52012-03-17 20:08:29 +080059 }
60
Logan Chien971bf3f2012-05-01 15:47:55 +080061 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080062
63 llvm::LLVMContext* GetLLVMContext() const {
64 return context_.get();
65 }
66
67 llvm::Module* GetModule() const {
68 return module_;
69 }
70
71 IRBuilder* GetIRBuilder() const {
72 return irb_.get();
73 }
74
TDYa127f15b0ab2012-05-11 21:01:36 -070075 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070076 bitcode_filename_ = bitcode_filename;
77 }
Logan Chien8b977d32012-02-21 19:14:55 +080078
buzbee4df2bbd2012-10-11 14:46:06 -070079 LLVMInfo* GetQuickContext() const {
80 return llvm_info_.get();
TDYa12755e5e6c2012-09-11 15:14:42 -070081 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070082 void SetCompiler(Compiler* compiler) {
83 compiler_ = compiler;
84 }
85 void SetOatCompilationUnit(OatCompilationUnit* oat_compilation_unit) {
86 oat_compilation_unit_ = oat_compilation_unit;
87 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070088
Logan Chien971bf3f2012-05-01 15:47:55 +080089 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080090
Logan Chien7f767612012-03-01 18:54:49 +080091 bool IsMaterialized() const {
TDYa12755e5e6c2012-09-11 15:14:42 -070092 return !compiled_code_.empty();
Logan Chien8b977d32012-02-21 19:14:55 +080093 }
94
Logan Chien971bf3f2012-05-01 15:47:55 +080095 const std::vector<uint8_t>& GetCompiledCode() const {
96 DCHECK(IsMaterialized());
97 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +080098 }
99
Logan Chien8b977d32012-02-21 19:14:55 +0800100 private:
Brian Carlstrom641ce032013-01-31 15:21:37 -0800101 LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
102 size_t cunit_idx);
103
Logan Chien971bf3f2012-05-01 15:47:55 +0800104 const CompilerLLVM* compiler_llvm_;
105 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800106
107 UniquePtr<llvm::LLVMContext> context_;
108 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700109 UniquePtr<RuntimeSupportBuilder> runtime_support_;
TDYa12755e5e6c2012-09-11 15:14:42 -0700110 llvm::Module* module_; // Managed by context_
buzbee4df2bbd2012-10-11 14:46:06 -0700111 UniquePtr<LLVMInfo> llvm_info_;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700112 Compiler* compiler_;
113 OatCompilationUnit* oat_compilation_unit_;
Logan Chien8b977d32012-02-21 19:14:55 +0800114
TDYa127f15b0ab2012-05-11 21:01:36 -0700115 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800116
Logan Chien971bf3f2012-05-01 15:47:55 +0800117 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800118
Logan Chien971bf3f2012-05-01 15:47:55 +0800119 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700120
Logan Chien971bf3f2012-05-01 15:47:55 +0800121 void CheckCodeAlign(uint32_t offset) const;
122
Logan Chien971bf3f2012-05-01 15:47:55 +0800123 bool MaterializeToString(std::string& str_buffer);
124 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
125
126 bool ExtractCodeAndPrelink(const std::string& elf_image);
Brian Carlstrom641ce032013-01-31 15:21:37 -0800127
128 friend class CompilerLLVM; // For LlvmCompilationUnit constructor
Logan Chien8b977d32012-02-21 19:14:55 +0800129};
130
131} // namespace compiler_llvm
132} // namespace art
133
Brian Carlstrom641ce032013-01-31 15:21:37 -0800134#endif // ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_