blob: d28df1dcce542db69f68c6eff71eba950ba12c3e [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_COMPILER_IR_H_
18#define ART_COMPILER_DEX_COMPILER_IR_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020#include "jni.h"
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -070021#include <string>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include <vector>
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000023
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "arch/instruction_set.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_allocator.h"
26#include "base/scoped_arena_allocator.h"
buzbeea61f4952013-08-23 14:27:06 -070027#include "base/timing_logger.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080028#include "invoke_type.h"
29#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Andreas Gampe53c913b2014-08-12 23:19:23 -070033class ClassLinker;
Andreas Gampe0b9203e2015-01-22 20:39:27 -080034class CompilerDriver;
Vladimir Marko80afd022015-05-19 18:08:00 +010035class DexFile;
Andreas Gampe9c462082015-01-27 14:31:40 -080036class Mir2Lir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070037class MIRGraph;
Andreas Gampe53c913b2014-08-12 23:19:23 -070038
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +010039constexpr size_t kOptionStringMaxLength = 2048;
40
41/**
42 * Structure abstracting pass option values, which can be of type string or integer.
43 */
44struct OptionContent {
45 OptionContent(const OptionContent& option) :
46 type(option.type), container(option.container, option.type) {}
47
48 explicit OptionContent(const char* value) :
49 type(kString), container(value) {}
50
51 explicit OptionContent(int value) :
52 type(kInteger), container(value) {}
53
54 explicit OptionContent(int64_t value) :
55 type(kInteger), container(value) {}
56
57 ~OptionContent() {
58 if (type == kString) {
59 container.StringDelete();
60 }
61 }
62
63 /**
64 * Allows for a transparent display of the option content.
65 */
66 friend std::ostream& operator<<(std::ostream& out, const OptionContent& option) {
67 if (option.type == kString) {
68 out << option.container.s;
69 } else {
70 out << option.container.i;
71 }
72
73 return out;
74 }
75
76 inline const char* GetString() const {
77 return container.s;
78 }
79
80 inline int64_t GetInteger() const {
81 return container.i;
82 }
83
84 /**
85 * @brief Used to compare a string option value to a given @p value.
86 * @details Will return whether the internal string option is equal to
87 * the parameter @p value. It will return false if the type of the
88 * object is not a string.
89 * @param value The string to compare to.
90 * @return Returns whether the internal string option is equal to the
91 * parameter @p value.
92 */
93 inline bool Equals(const char* value) const {
94 DCHECK(value != nullptr);
95 if (type != kString) {
96 return false;
97 }
98 return !strncmp(container.s, value, kOptionStringMaxLength);
99 }
100
101 /**
102 * @brief Used to compare an integer option value to a given @p value.
103 * @details Will return whether the internal integer option is equal to
104 * the parameter @p value. It will return false if the type of the
105 * object is not an integer.
106 * @param value The integer to compare to.
107 * @return Returns whether the internal integer option is equal to the
108 * parameter @p value.
109 */
110 inline bool Equals(int64_t value) const {
111 if (type != kInteger) {
112 return false;
113 }
114 return container.i == value;
115 }
116
117 /**
118 * Describes the type of parameters allowed as option values.
119 */
120 enum OptionType {
121 kString = 0,
122 kInteger
123 };
124
125 OptionType type;
126
127 private:
128 /**
129 * Union containing the option value of either type.
130 */
131 union OptionContainer {
132 explicit OptionContainer(const OptionContainer& c, OptionType t) {
133 if (t == kString) {
134 DCHECK(c.s != nullptr);
135 s = strndup(c.s, kOptionStringMaxLength);
136 } else {
137 i = c.i;
138 }
139 }
140
141 explicit OptionContainer(const char* value) {
142 DCHECK(value != nullptr);
143 s = strndup(value, kOptionStringMaxLength);
144 }
145
146 explicit OptionContainer(int64_t value) : i(value) {}
147 ~OptionContainer() {}
148
149 void StringDelete() {
150 if (s != nullptr) {
151 free(s);
152 }
153 }
154
155 char* s;
156 int64_t i;
157 };
158
159 OptionContainer container;
160};
161
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162struct CompilationUnit {
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800163 CompilationUnit(ArenaPool* pool, InstructionSet isa, CompilerDriver* driver, ClassLinker* linker);
Vladimir Marko25724ef2013-11-12 15:09:20 +0000164 ~CompilationUnit();
buzbeea61f4952013-08-23 14:27:06 -0700165
166 void StartTimingSplit(const char* label);
167 void NewTimingSplit(const char* label);
168 void EndTiming();
169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 /*
171 * Fields needed/generated by common frontend and generally used throughout
172 * the compiler.
173 */
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800174 CompilerDriver* const compiler_driver;
175 ClassLinker* const class_linker; // Linker to resolve fields and methods.
176 const DexFile* dex_file; // DexFile containing the method being compiled.
177 jobject class_loader; // compiling method's class loader.
178 uint16_t class_def_idx; // compiling method's defining class definition index.
179 uint32_t method_idx; // compiling method's index into method_ids of DexFile.
180 uint32_t access_flags; // compiling method's access flags.
181 InvokeType invoke_type; // compiling method's invocation type.
182 const char* shorty; // compiling method's shorty.
183 uint32_t disable_opt; // opt_control_vector flags.
184 uint32_t enable_debug; // debugControlVector flags.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 bool verbose;
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800186 const InstructionSet instruction_set;
187 const bool target64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
189 // TODO: move memory management to mir_graph, or just switch to using standard containers.
190 ArenaAllocator arena;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000191 ArenaStack arena_stack; // Arenas for ScopedArenaAllocator.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
Ian Rogers700a4022014-05-19 16:49:03 -0700193 std::unique_ptr<MIRGraph> mir_graph; // MIR container.
Andreas Gampe9c462082015-01-27 14:31:40 -0800194 std::unique_ptr<Mir2Lir> cg; // Target-specific codegen.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800195 TimingLogger timings;
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700196 bool print_pass; // Do we want to print a pass or not?
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700197
198 /**
199 * @brief Holds pass options for current pass being applied to compilation unit.
200 * @details This is updated for every pass to contain the overridden pass options
201 * that were specified by user. The pass itself will check this to see if the
202 * default settings have been changed. The key is simply the option string without
203 * the pass name.
204 */
Jean-Philippe Halimi98a26e12015-01-20 10:52:43 +0100205 SafeMap<const std::string, const OptionContent> overridden_pass_options;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206};
207
208} // namespace art
209
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700210#endif // ART_COMPILER_DEX_COMPILER_IR_H_