blob: 801bbef8eac2347fb8e91b4ecbdaf2c602663786 [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"
buzbee395116c2013-02-27 14:30:25 -080022#include "compiler/dex/compiler_internals.h"
Ian Rogers1212a022013-03-04 10:48:41 -080023#include "compiler/driver/compiler_driver.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
31#include <UniquePtr.h>
32#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080033#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080034
Logan Chien110bcba2012-04-16 19:11:28 +080035namespace art {
36 class CompiledMethod;
37}
38
Logan Chien8b977d32012-02-21 19:14:55 +080039namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080040 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080041 class LLVMContext;
42 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080043 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080044}
45
46namespace art {
47namespace compiler_llvm {
48
Logan Chien971bf3f2012-05-01 15:47:55 +080049class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080050class IRBuilder;
51
Brian Carlstrom641ce032013-01-31 15:21:37 -080052class LlvmCompilationUnit {
Logan Chien8b977d32012-02-21 19:14:55 +080053 public:
Brian Carlstrom641ce032013-01-31 15:21:37 -080054 ~LlvmCompilationUnit();
Logan Chien8b977d32012-02-21 19:14:55 +080055
Logan Chien971bf3f2012-05-01 15:47:55 +080056 size_t GetIndex() const {
57 return cunit_idx_;
Logan Chien6546ec52012-03-17 20:08:29 +080058 }
59
Logan Chien971bf3f2012-05-01 15:47:55 +080060 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080061
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
TDYa127f15b0ab2012-05-11 21:01:36 -070074 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070075 bitcode_filename_ = bitcode_filename;
76 }
Logan Chien8b977d32012-02-21 19:14:55 +080077
buzbee4df2bbd2012-10-11 14:46:06 -070078 LLVMInfo* GetQuickContext() const {
79 return llvm_info_.get();
TDYa12755e5e6c2012-09-11 15:14:42 -070080 }
Ian Rogers1212a022013-03-04 10:48:41 -080081 void SetCompiler(CompilerDriver* driver) {
82 driver_ = driver;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070083 }
84 void SetOatCompilationUnit(OatCompilationUnit* oat_compilation_unit) {
85 oat_compilation_unit_ = oat_compilation_unit;
86 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070087
Logan Chien971bf3f2012-05-01 15:47:55 +080088 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080089
Logan Chien7f767612012-03-01 18:54:49 +080090 bool IsMaterialized() const {
TDYa12755e5e6c2012-09-11 15:14:42 -070091 return !compiled_code_.empty();
Logan Chien8b977d32012-02-21 19:14:55 +080092 }
93
Logan Chien971bf3f2012-05-01 15:47:55 +080094 const std::vector<uint8_t>& GetCompiledCode() const {
95 DCHECK(IsMaterialized());
96 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +080097 }
98
Logan Chien8b977d32012-02-21 19:14:55 +080099 private:
Brian Carlstrom641ce032013-01-31 15:21:37 -0800100 LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
101 size_t cunit_idx);
102
Logan Chien971bf3f2012-05-01 15:47:55 +0800103 const CompilerLLVM* compiler_llvm_;
104 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800105
106 UniquePtr<llvm::LLVMContext> context_;
107 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700108 UniquePtr<RuntimeSupportBuilder> runtime_support_;
TDYa12755e5e6c2012-09-11 15:14:42 -0700109 llvm::Module* module_; // Managed by context_
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800110 UniquePtr<IntrinsicHelper> intrinsic_helper_;
buzbee4df2bbd2012-10-11 14:46:06 -0700111 UniquePtr<LLVMInfo> llvm_info_;
Ian Rogers1212a022013-03-04 10:48:41 -0800112 CompilerDriver* driver_;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700113 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_