blob: a0882bbd7928581ef92f4917a959d66a63198b8e [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
20#include "constants.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080021#include "globals.h"
Logan Chien8b977d32012-02-21 19:14:55 +080022#include "logging.h"
23
24#include <UniquePtr.h>
25#include <string>
26
27namespace llvm {
28 class LLVMContext;
29 class Module;
30}
31
32namespace art {
33namespace compiler_llvm {
34
35class IRBuilder;
36
37class CompilationUnit {
38 public:
39 CompilationUnit(InstructionSet insn_set);
40
41 ~CompilationUnit();
42
43 InstructionSet GetInstructionSet() const {
44 return insn_set_;
45 }
46
47 llvm::LLVMContext* GetLLVMContext() const {
48 return context_.get();
49 }
50
51 llvm::Module* GetModule() const {
52 return module_;
53 }
54
55 IRBuilder* GetIRBuilder() const {
56 return irb_.get();
57 }
58
59 std::string const& GetElfFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080060 return elf_filename_;
61 }
62
63 std::string const& GetBitcodeFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080064 return bitcode_filename_;
65 }
66
67 void SetElfFileName(std::string const& filename) {
68 elf_filename_ = filename;
69 }
70
71 void SetBitcodeFileName(std::string const& filename) {
72 bitcode_filename_ = filename;
73 }
74
Logan Chienb9eaeea2012-03-17 19:45:01 +080075 const byte* GetElfImage() const {
76 return reinterpret_cast<const byte*>(&*elf_image_.begin());
77 }
78
79 size_t GetElfSize() const {
80 return elf_image_.size();
81 }
82
Logan Chien8b977d32012-02-21 19:14:55 +080083 bool WriteBitcodeToFile();
84
85 bool Materialize();
86
Logan Chien7f767612012-03-01 18:54:49 +080087 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080088 return (context_.get() == NULL);
89 }
90
Logan Chien8b977d32012-02-21 19:14:55 +080091 bool IsMaterializeThresholdReached() const {
92 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
93 }
94
95 void AddMemUsageApproximation(size_t usage) {
96 mem_usage_ += usage;
97 }
98
99 private:
100 InstructionSet insn_set_;
101
102 UniquePtr<llvm::LLVMContext> context_;
103 UniquePtr<IRBuilder> irb_;
104 llvm::Module* module_;
105
Logan Chienb9eaeea2012-03-17 19:45:01 +0800106 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800107 std::string elf_filename_;
108 std::string bitcode_filename_;
109
110 size_t mem_usage_;
111};
112
113} // namespace compiler_llvm
114} // namespace art
115
116#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_