blob: 6244eea9ccceb43cc004cd2ab1d172a72e1bc112 [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"
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070022#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
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
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070031#if defined(ART_USE_QUICK_COMPILER)
32# include "compiler.h"
33# include "oat_compilation_unit.h"
34#endif
35
Logan Chien8b977d32012-02-21 19:14:55 +080036#include <UniquePtr.h>
37#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080038#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080039
Logan Chien110bcba2012-04-16 19:11:28 +080040namespace art {
41 class CompiledMethod;
42}
43
Logan Chien8b977d32012-02-21 19:14:55 +080044namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080045 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080046 class LLVMContext;
47 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080048 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080049}
50
51namespace art {
52namespace compiler_llvm {
53
Logan Chien971bf3f2012-05-01 15:47:55 +080054class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080055class IRBuilder;
56
57class CompilationUnit {
58 public:
Logan Chien971bf3f2012-05-01 15:47:55 +080059 CompilationUnit(const CompilerLLVM* compiler_llvm,
60 size_t cunit_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080061
62 ~CompilationUnit();
63
Logan Chien971bf3f2012-05-01 15:47:55 +080064 size_t GetIndex() const {
65 return cunit_idx_;
Logan Chien6546ec52012-03-17 20:08:29 +080066 }
67
Logan Chien971bf3f2012-05-01 15:47:55 +080068 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080069
70 llvm::LLVMContext* GetLLVMContext() const {
71 return context_.get();
72 }
73
74 llvm::Module* GetModule() const {
75 return module_;
76 }
77
78 IRBuilder* GetIRBuilder() const {
79 return irb_.get();
80 }
81
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070082#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
Shih-wei Liao21d28f52012-06-12 05:55:00 -070083 greenland::DexLang::Context* GetDexLangContext() const {
84 return dex_lang_ctx_;
85 }
86#endif
87
TDYa127f15b0ab2012-05-11 21:01:36 -070088 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070089 bitcode_filename_ = bitcode_filename;
90 }
Logan Chien8b977d32012-02-21 19:14:55 +080091
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070092#if defined(ART_USE_QUICK_COMPILER)
93 void SetCompiler(Compiler* compiler) {
94 compiler_ = compiler;
95 }
96 void SetOatCompilationUnit(OatCompilationUnit* oat_compilation_unit) {
97 oat_compilation_unit_ = oat_compilation_unit;
98 }
99#endif
100
Logan Chien971bf3f2012-05-01 15:47:55 +0800101 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800102
Logan Chien7f767612012-03-01 18:54:49 +0800103 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +0800104 return (context_.get() == NULL);
105 }
106
Logan Chien971bf3f2012-05-01 15:47:55 +0800107 const std::vector<uint8_t>& GetCompiledCode() const {
108 DCHECK(IsMaterialized());
109 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +0800110 }
111
Logan Chien8b977d32012-02-21 19:14:55 +0800112 private:
Logan Chien971bf3f2012-05-01 15:47:55 +0800113 const CompilerLLVM* compiler_llvm_;
114 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800115
116 UniquePtr<llvm::LLVMContext> context_;
117 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700118 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800119 llvm::Module* module_;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700120#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700121 greenland::DexLang::Context* dex_lang_ctx_;
122#endif
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700123#if defined(ART_USE_QUICK_COMPILER)
124 Compiler* compiler_;
125 OatCompilationUnit* oat_compilation_unit_;
126#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800127
TDYa127f15b0ab2012-05-11 21:01:36 -0700128 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800129
Logan Chien971bf3f2012-05-01 15:47:55 +0800130 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800131
Logan Chien971bf3f2012-05-01 15:47:55 +0800132 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700133
Logan Chien971bf3f2012-05-01 15:47:55 +0800134 void CheckCodeAlign(uint32_t offset) const;
135
136 void DeleteResources() {
137 module_ = NULL; // Managed by context_
138 context_.reset(NULL);
139 irb_.reset(NULL);
140 runtime_support_.reset(NULL);
141 }
142
143 bool MaterializeToString(std::string& str_buffer);
144 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
145
146 bool ExtractCodeAndPrelink(const std::string& elf_image);
Logan Chien8b977d32012-02-21 19:14:55 +0800147};
148
149} // namespace compiler_llvm
150} // namespace art
151
152#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_