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