blob: d1dfa0cdb7e364ee63546b40c72059a015dbc151 [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 Liao21d28f52012-06-12 05:55:00 -070022#ifdef ART_USE_DEXLANG_FRONTEND
23# 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
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
52class CompilationUnit {
53 public:
Logan Chien971bf3f2012-05-01 15:47:55 +080054 CompilationUnit(const CompilerLLVM* compiler_llvm,
55 size_t cunit_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080056
57 ~CompilationUnit();
58
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
Shih-wei Liao21d28f52012-06-12 05:55:00 -070077#ifdef ART_USE_DEXLANG_FRONTEND
78 greenland::DexLang::Context* GetDexLangContext() const {
79 return dex_lang_ctx_;
80 }
81#endif
82
TDYa127f15b0ab2012-05-11 21:01:36 -070083 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070084 bitcode_filename_ = bitcode_filename;
85 }
Logan Chien8b977d32012-02-21 19:14:55 +080086
Logan Chien971bf3f2012-05-01 15:47:55 +080087 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080088
Logan Chien7f767612012-03-01 18:54:49 +080089 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080090 return (context_.get() == NULL);
91 }
92
Logan Chien971bf3f2012-05-01 15:47:55 +080093 const std::vector<uint8_t>& GetCompiledCode() const {
94 DCHECK(IsMaterialized());
95 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +080096 }
97
Logan Chien8b977d32012-02-21 19:14:55 +080098 private:
Logan Chien971bf3f2012-05-01 15:47:55 +080099 const CompilerLLVM* compiler_llvm_;
100 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800101
102 UniquePtr<llvm::LLVMContext> context_;
103 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700104 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800105 llvm::Module* module_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700106#ifdef ART_USE_DEXLANG_FRONTEND
107 greenland::DexLang::Context* dex_lang_ctx_;
108#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800109
TDYa127f15b0ab2012-05-11 21:01:36 -0700110 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800111
Logan Chien971bf3f2012-05-01 15:47:55 +0800112 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800113
Logan Chien971bf3f2012-05-01 15:47:55 +0800114 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700115
Logan Chien971bf3f2012-05-01 15:47:55 +0800116 void CheckCodeAlign(uint32_t offset) const;
117
118 void DeleteResources() {
119 module_ = NULL; // Managed by context_
120 context_.reset(NULL);
121 irb_.reset(NULL);
122 runtime_support_.reset(NULL);
123 }
124
125 bool MaterializeToString(std::string& str_buffer);
126 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
127
128 bool ExtractCodeAndPrelink(const std::string& elf_image);
Logan Chien8b977d32012-02-21 19:14:55 +0800129};
130
131} // namespace compiler_llvm
132} // namespace art
133
134#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_