blob: af3ba7a7e02eab467c91bd611e0daa5603734f37 [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"
Logan Chienb9eaeea2012-03-17 19:45:01 +080022#include "globals.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070023#include "instruction_set.h"
TDYa127d668a062012-04-13 12:36:57 -070024#include "runtime_support_builder.h"
25#include "runtime_support_func.h"
Logan Chien110bcba2012-04-16 19:11:28 +080026#include "safe_map.h"
Logan Chien8b977d32012-02-21 19:14:55 +080027
buzbeec531cef2012-10-18 07:09:20 -070028#if defined(ART_USE_PORTABLE_COMPILER)
buzbee26f10ee2012-12-21 11:16:29 -080029# include "compiler/compiler_internals.h"
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070030# include "compiler.h"
31# include "oat_compilation_unit.h"
32#endif
33
Logan Chien8b977d32012-02-21 19:14:55 +080034#include <UniquePtr.h>
35#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080036#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080037
Logan Chien110bcba2012-04-16 19:11:28 +080038namespace art {
39 class CompiledMethod;
40}
41
Logan Chien8b977d32012-02-21 19:14:55 +080042namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080043 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080044 class LLVMContext;
45 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080046 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080047}
48
49namespace art {
50namespace compiler_llvm {
51
Logan Chien971bf3f2012-05-01 15:47:55 +080052class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080053class IRBuilder;
54
Brian Carlstrom641ce032013-01-31 15:21:37 -080055class LlvmCompilationUnit {
Logan Chien8b977d32012-02-21 19:14:55 +080056 public:
Brian Carlstrom641ce032013-01-31 15:21:37 -080057 ~LlvmCompilationUnit();
Logan Chien8b977d32012-02-21 19:14:55 +080058
Logan Chien971bf3f2012-05-01 15:47:55 +080059 size_t GetIndex() const {
60 return cunit_idx_;
Logan Chien6546ec52012-03-17 20:08:29 +080061 }
62
Logan Chien971bf3f2012-05-01 15:47:55 +080063 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080064
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
TDYa127f15b0ab2012-05-11 21:01:36 -070077 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070078 bitcode_filename_ = bitcode_filename;
79 }
Logan Chien8b977d32012-02-21 19:14:55 +080080
buzbeec531cef2012-10-18 07:09:20 -070081#if defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070082 LLVMInfo* GetQuickContext() const {
83 return llvm_info_.get();
TDYa12755e5e6c2012-09-11 15:14:42 -070084 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070085 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 Chien971bf3f2012-05-01 15:47:55 +080093 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080094
Logan Chien7f767612012-03-01 18:54:49 +080095 bool IsMaterialized() const {
TDYa12755e5e6c2012-09-11 15:14:42 -070096 return !compiled_code_.empty();
Logan Chien8b977d32012-02-21 19:14:55 +080097 }
98
Logan Chien971bf3f2012-05-01 15:47:55 +080099 const std::vector<uint8_t>& GetCompiledCode() const {
100 DCHECK(IsMaterialized());
101 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +0800102 }
103
Logan Chien8b977d32012-02-21 19:14:55 +0800104 private:
Brian Carlstrom641ce032013-01-31 15:21:37 -0800105 LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
106 size_t cunit_idx);
107
Logan Chien971bf3f2012-05-01 15:47:55 +0800108 const CompilerLLVM* compiler_llvm_;
109 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800110
111 UniquePtr<llvm::LLVMContext> context_;
112 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700113 UniquePtr<RuntimeSupportBuilder> runtime_support_;
TDYa12755e5e6c2012-09-11 15:14:42 -0700114 llvm::Module* module_; // Managed by context_
buzbeec531cef2012-10-18 07:09:20 -0700115#if defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -0700116 UniquePtr<LLVMInfo> llvm_info_;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700117 Compiler* compiler_;
118 OatCompilationUnit* oat_compilation_unit_;
119#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800120
TDYa127f15b0ab2012-05-11 21:01:36 -0700121 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800122
Logan Chien971bf3f2012-05-01 15:47:55 +0800123 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800124
Logan Chien971bf3f2012-05-01 15:47:55 +0800125 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700126
Logan Chien971bf3f2012-05-01 15:47:55 +0800127 void CheckCodeAlign(uint32_t offset) const;
128
Logan Chien971bf3f2012-05-01 15:47:55 +0800129 bool MaterializeToString(std::string& str_buffer);
130 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
131
132 bool ExtractCodeAndPrelink(const std::string& elf_image);
Brian Carlstrom641ce032013-01-31 15:21:37 -0800133
134 friend class CompilerLLVM; // For LlvmCompilationUnit constructor
Logan Chien8b977d32012-02-21 19:14:55 +0800135};
136
137} // namespace compiler_llvm
138} // namespace art
139
Brian Carlstrom641ce032013-01-31 15:21:37 -0800140#endif // ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_