blob: 576b65f8b34c5b6c67d56f9ad47b102480eb4c44 [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"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080023#include "logging.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
28#include <UniquePtr.h>
29#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080030#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080031
Logan Chien110bcba2012-04-16 19:11:28 +080032namespace art {
33 class CompiledMethod;
34}
35
Logan Chien8b977d32012-02-21 19:14:55 +080036namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080037 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080038 class LLVMContext;
39 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080040 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080041}
42
43namespace art {
44namespace compiler_llvm {
45
Logan Chien971bf3f2012-05-01 15:47:55 +080046class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080047class IRBuilder;
48
49class CompilationUnit {
50 public:
Logan Chien971bf3f2012-05-01 15:47:55 +080051 CompilationUnit(const CompilerLLVM* compiler_llvm,
52 size_t cunit_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080053
54 ~CompilationUnit();
55
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
Logan Chien971bf3f2012-05-01 15:47:55 +080078 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080079
Logan Chien7f767612012-03-01 18:54:49 +080080 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080081 return (context_.get() == NULL);
82 }
83
Logan Chien971bf3f2012-05-01 15:47:55 +080084 const std::vector<uint8_t>& GetCompiledCode() const {
85 DCHECK(IsMaterialized());
86 return compiled_code_;
Logan Chien8b977d32012-02-21 19:14:55 +080087 }
88
Logan Chien8b977d32012-02-21 19:14:55 +080089 private:
Logan Chien971bf3f2012-05-01 15:47:55 +080090 const CompilerLLVM* compiler_llvm_;
91 const size_t cunit_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +080092
93 UniquePtr<llvm::LLVMContext> context_;
94 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -070095 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +080096 llvm::Module* module_;
97
TDYa127f15b0ab2012-05-11 21:01:36 -070098 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +080099
Logan Chien971bf3f2012-05-01 15:47:55 +0800100 std::vector<uint8_t> compiled_code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800101
Logan Chien971bf3f2012-05-01 15:47:55 +0800102 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700103
Logan Chien971bf3f2012-05-01 15:47:55 +0800104 void CheckCodeAlign(uint32_t offset) const;
105
106 void DeleteResources() {
107 module_ = NULL; // Managed by context_
108 context_.reset(NULL);
109 irb_.reset(NULL);
110 runtime_support_.reset(NULL);
111 }
112
113 bool MaterializeToString(std::string& str_buffer);
114 bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
115
116 bool ExtractCodeAndPrelink(const std::string& elf_image);
Logan Chien8b977d32012-02-21 19:14:55 +0800117};
118
119} // namespace compiler_llvm
120} // namespace art
121
122#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_