blob: 0b40388cee6e8210f245c497d4fb2e7326de5c1a [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
17#ifndef ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_
18#define ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_
19
Logan Chien8ba2fc52012-04-23 09:10:46 +080020#include "../mutex.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080021#include "globals.h"
TDYa12755e5e6c2012-09-11 15:14:42 -070022#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023# include "greenland/dex_lang.h"
24#endif
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080026#include "logging.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
buzbeec531cef2012-10-18 07:09:20 -070031#if defined(ART_USE_PORTABLE_COMPILER)
TDYa12755e5e6c2012-09-11 15:14:42 -070032# include "compiler/Dalvik.h"
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070033# include "compiler.h"
34# include "oat_compilation_unit.h"
35#endif
36
Logan Chien8b977d32012-02-21 19:14:55 +080037#include <UniquePtr.h>
38#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080039#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080040
Logan Chien110bcba2012-04-16 19:11:28 +080041namespace art {
42 class CompiledMethod;
43}
44
Logan Chien8b977d32012-02-21 19:14:55 +080045namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080046 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080047 class LLVMContext;
48 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080049 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080050}
51
52namespace art {
53namespace compiler_llvm {
54
Logan Chien971bf3f2012-05-01 15:47:55 +080055class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080056class IRBuilder;
57
58class CompilationUnit {
59 public:
Logan Chien971bf3f2012-05-01 15:47:55 +080060 CompilationUnit(const CompilerLLVM* compiler_llvm,
61 size_t cunit_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080062
63 ~CompilationUnit();
64
Logan Chien971bf3f2012-05-01 15:47:55 +080065 size_t GetIndex() const {
66 return cunit_idx_;
Logan Chien6546ec52012-03-17 20:08:29 +080067 }
68
Logan Chien971bf3f2012-05-01 15:47:55 +080069 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080070
71 llvm::LLVMContext* GetLLVMContext() const {
72 return context_.get();
73 }
74
75 llvm::Module* GetModule() const {
76 return module_;
77 }
78
79 IRBuilder* GetIRBuilder() const {
80 return irb_.get();
81 }
82
TDYa12755e5e6c2012-09-11 15:14:42 -070083#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -070084 greenland::DexLang::Context* GetDexLangContext() const {
85 return dex_lang_ctx_;
86 }
87#endif
88
TDYa127f15b0ab2012-05-11 21:01:36 -070089 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070090 bitcode_filename_ = bitcode_filename;
91 }
Logan Chien8b977d32012-02-21 19:14:55 +080092
buzbeec531cef2012-10-18 07:09:20 -070093#if defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070094 LLVMInfo* GetQuickContext() const {
95 return llvm_info_.get();
TDYa12755e5e6c2012-09-11 15:14:42 -070096 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070097 void SetCompiler(Compiler* compiler) {
98 compiler_ = compiler;
99 }
100 void SetOatCompilationUnit(OatCompilationUnit* oat_compilation_unit) {
101 oat_compilation_unit_ = oat_compilation_unit;
102 }
103#endif
104
Logan Chien971bf3f2012-05-01 15:47:55 +0800105 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800106
Logan Chien7f767612012-03-01 18:54:49 +0800107 bool IsMaterialized() const {
TDYa12755e5e6c2012-09-11 15:14:42 -0700108 return !compiled_code_.empty();
Logan Chien8b977d32012-02-21 19:14:55 +0800109 }
110
Logan Chien971bf3f2012-05-01 15:47:55 +0800111 const std::vector<uint8_t>& GetCompiledCode() const {
112 DCHECK(IsMaterialized());
113 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +0800114 }
115
Logan Chien8b977d32012-02-21 19:14:55 +0800116 private:
Logan Chien971bf3f2012-05-01 15:47:55 +0800117 const CompilerLLVM* compiler_llvm_;
118 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800119
120 UniquePtr<llvm::LLVMContext> context_;
121 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700122 UniquePtr<RuntimeSupportBuilder> runtime_support_;
TDYa12755e5e6c2012-09-11 15:14:42 -0700123 llvm::Module* module_; // Managed by context_
124#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700125 greenland::DexLang::Context* dex_lang_ctx_;
126#endif
buzbeec531cef2012-10-18 07:09:20 -0700127#if defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -0700128 UniquePtr<LLVMInfo> llvm_info_;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700129 Compiler* compiler_;
130 OatCompilationUnit* oat_compilation_unit_;
131#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800132
TDYa127f15b0ab2012-05-11 21:01:36 -0700133 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800134
Logan Chien971bf3f2012-05-01 15:47:55 +0800135 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800136
Logan Chien971bf3f2012-05-01 15:47:55 +0800137 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700138
Logan Chien971bf3f2012-05-01 15:47:55 +0800139 void CheckCodeAlign(uint32_t offset) const;
140
Logan Chien971bf3f2012-05-01 15:47:55 +0800141 bool MaterializeToString(std::string& str_buffer);
142 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
143
144 bool ExtractCodeAndPrelink(const std::string& elf_image);
Logan Chien8b977d32012-02-21 19:14:55 +0800145};
146
147} // namespace compiler_llvm
148} // namespace art
149
150#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_