blob: 99b0ac10d1bbea86cbf33a365317435a34944f48 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom3320cf42011-10-04 14:58:28 -070016
Mathieu Chartier193bad92013-08-29 18:46:00 -070017#ifndef ART_COMPILER_COMPILED_METHOD_H_
18#define ART_COMPILER_COMPILED_METHOD_H_
Brian Carlstrom3320cf42011-10-04 14:58:28 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Vladimir Markocac5a7e2016-02-22 10:39:50 +000021#include <iosfwd>
Brian Carlstrom265091e2013-01-30 14:08:26 -080022#include <string>
Brian Carlstrom3320cf42011-10-04 14:58:28 -070023#include <vector>
24
Ian Rogersd582fa42014-11-05 23:46:43 -080025#include "arch/instruction_set.h"
David Brazdild9c90372016-09-14 16:53:55 +010026#include "base/array_ref.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/bit_utils.h"
Alex Lighte64300b2015-12-15 15:02:47 -080028#include "base/length_prefixed_array.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010029#include "method_reference.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070030
31namespace art {
32
Mathieu Chartier193bad92013-08-29 18:46:00 -070033class CompilerDriver;
Vladimir Marko35831e82015-09-11 11:59:18 +010034class CompiledMethodStorage;
Mathieu Chartier193bad92013-08-29 18:46:00 -070035
Logan Chien598c5132012-04-28 22:00:44 +080036class CompiledCode {
37 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080038 // For Quick to supply an code blob
Mathieu Chartier193bad92013-08-29 18:46:00 -070039 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010040 const ArrayRef<const uint8_t>& quick_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041
42 virtual ~CompiledCode();
Logan Chien598c5132012-04-28 22:00:44 +080043
Logan Chien598c5132012-04-28 22:00:44 +080044 InstructionSet GetInstructionSet() const {
45 return instruction_set_;
46 }
47
Vladimir Marko35831e82015-09-11 11:59:18 +010048 ArrayRef<const uint8_t> GetQuickCode() const {
49 return GetArray(quick_code_);
Logan Chien598c5132012-04-28 22:00:44 +080050 }
51
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 bool operator==(const CompiledCode& rhs) const;
53
Logan Chien598c5132012-04-28 22:00:44 +080054 // To align an offset from a page-aligned value to make it suitable
55 // for code storage. For example on ARM, to ensure that PC relative
56 // valu computations work out as expected.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 size_t AlignCode(size_t offset) const;
58 static size_t AlignCode(size_t offset, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080059
60 // returns the difference between the code address and a usable PC.
61 // mainly to cope with kThumb2 where the lower bit must be set.
62 size_t CodeDelta() const;
Dave Allison50abf0a2014-06-23 13:19:59 -070063 static size_t CodeDelta(InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080064
65 // Returns a pointer suitable for invoking the code at the argument
66 // code_pointer address. Mainly to cope with kThumb2 where the
67 // lower bit must be set to indicate Thumb mode.
68 static const void* CodePointer(const void* code_pointer,
69 InstructionSet instruction_set);
70
Vladimir Marko35831e82015-09-11 11:59:18 +010071 protected:
72 template <typename T>
73 static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array) {
74 if (array == nullptr) {
75 return ArrayRef<const T>();
76 }
77 DCHECK_NE(array->size(), 0u);
78 return ArrayRef<const T>(&array->At(0), array->size());
79 }
80
81 CompilerDriver* GetCompilerDriver() {
82 return compiler_driver_;
83 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080084
Logan Chien598c5132012-04-28 22:00:44 +080085 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070087
Logan Chien598c5132012-04-28 22:00:44 +080088 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080089
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 // Used to store the PIC code for Quick.
Vladimir Marko35831e82015-09-11 11:59:18 +010091 const LengthPrefixedArray<uint8_t>* const quick_code_;
Logan Chien598c5132012-04-28 22:00:44 +080092};
93
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070094class SrcMapElem {
95 public:
96 uint32_t from_;
97 int32_t to_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070098};
99
Vladimir Marko35831e82015-09-11 11:59:18 +0100100inline bool operator<(const SrcMapElem& lhs, const SrcMapElem& rhs) {
101 if (lhs.from_ != rhs.from_) {
102 return lhs.from_ < rhs.from_;
103 }
104 return lhs.to_ < rhs.to_;
105}
106
107inline bool operator==(const SrcMapElem& lhs, const SrcMapElem& rhs) {
108 return lhs.from_ == rhs.from_ && lhs.to_ == rhs.to_;
109}
110
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800111template <class Allocator>
112class SrcMap FINAL : public std::vector<SrcMapElem, Allocator> {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700113 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800114 using std::vector<SrcMapElem, Allocator>::begin;
115 using typename std::vector<SrcMapElem, Allocator>::const_iterator;
116 using std::vector<SrcMapElem, Allocator>::empty;
117 using std::vector<SrcMapElem, Allocator>::end;
118 using std::vector<SrcMapElem, Allocator>::resize;
119 using std::vector<SrcMapElem, Allocator>::shrink_to_fit;
120 using std::vector<SrcMapElem, Allocator>::size;
121
122 explicit SrcMap() {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 explicit SrcMap(const Allocator& alloc) : std::vector<SrcMapElem, Allocator>(alloc) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800124
125 template <class InputIt>
126 SrcMap(InputIt first, InputIt last, const Allocator& alloc)
127 : std::vector<SrcMapElem, Allocator>(first, last, alloc) {}
128
David Srbecky6f715892015-03-30 14:21:42 +0100129 void push_back(const SrcMapElem& elem) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700130 if (!empty()) {
David Srbecky6f715892015-03-30 14:21:42 +0100131 // Check that the addresses are inserted in sorted order.
132 DCHECK_GE(elem.from_, this->back().from_);
133 // If two consequitive entries map to the same value, ignore the later.
134 // E.g. for map {{0, 1}, {4, 1}, {8, 2}}, all values in [0,8) map to 1.
135 if (elem.to_ == this->back().to_) {
136 return;
137 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700138 }
David Srbecky6f715892015-03-30 14:21:42 +0100139 std::vector<SrcMapElem, Allocator>::push_back(elem);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700140 }
141
David Srbecky6f715892015-03-30 14:21:42 +0100142 // Returns true and the corresponding "to" value if the mapping is found.
143 // Oterwise returns false and 0.
144 std::pair<bool, int32_t> Find(uint32_t from) const {
145 // Finds first mapping such that lb.from_ >= from.
146 auto lb = std::lower_bound(begin(), end(), SrcMapElem {from, INT32_MIN});
147 if (lb != end() && lb->from_ == from) {
148 // Found exact match.
149 return std::make_pair(true, lb->to_);
150 } else if (lb != begin()) {
151 // The previous mapping is still in effect.
152 return std::make_pair(true, (--lb)->to_);
153 } else {
154 // Not found because 'from' is smaller than first entry in the map.
155 return std::make_pair(false, 0);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700156 }
157 }
158};
159
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800160using DefaultSrcMap = SrcMap<std::allocator<SrcMapElem>>;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800161
Vladimir Markof4da6752014-08-01 19:04:18 +0100162class LinkerPatch {
163 public:
Vladimir Markoef88a112016-03-31 12:34:48 +0100164 // Note: We explicitly specify the underlying type of the enum because GCC
165 // would otherwise select a bigger underlying type and then complain that
166 // 'art::LinkerPatch::patch_type_' is too small to hold all
167 // values of 'enum class art::LinkerPatch::Type'
168 // which is ridiculous given we have only a handful of values here. If we
169 // choose to squeeze the Type into fewer than 8 bits, we'll have to declare
170 // patch_type_ as an uintN_t and do explicit static_cast<>s.
171 enum class Type : uint8_t {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100172 kRecordPosition, // Just record patch position for patchoat.
173 kMethod,
174 kCall,
175 kCallRelative, // NOTE: Actual patching is instruction_set-dependent.
176 kType,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100177 kTypeRelative, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100178 kString,
179 kStringRelative, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000180 kStringBssEntry, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100181 kDexCacheArray, // NOTE: Actual patching is instruction_set-dependent.
182 };
183
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000184 static LinkerPatch RecordPosition(size_t literal_offset) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100185 return LinkerPatch(literal_offset, Type::kRecordPosition, /* target_dex_file */ nullptr);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000186 }
187
Vladimir Markof4da6752014-08-01 19:04:18 +0100188 static LinkerPatch MethodPatch(size_t literal_offset,
189 const DexFile* target_dex_file,
190 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100191 LinkerPatch patch(literal_offset, Type::kMethod, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000192 patch.method_idx_ = target_method_idx;
193 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100194 }
195
196 static LinkerPatch CodePatch(size_t literal_offset,
197 const DexFile* target_dex_file,
198 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100199 LinkerPatch patch(literal_offset, Type::kCall, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000200 patch.method_idx_ = target_method_idx;
201 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100202 }
203
204 static LinkerPatch RelativeCodePatch(size_t literal_offset,
205 const DexFile* target_dex_file,
206 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100207 LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000208 patch.method_idx_ = target_method_idx;
209 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100210 }
211
212 static LinkerPatch TypePatch(size_t literal_offset,
213 const DexFile* target_dex_file,
214 uint32_t target_type_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100215 LinkerPatch patch(literal_offset, Type::kType, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000216 patch.type_idx_ = target_type_idx;
217 return patch;
218 }
219
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100220 static LinkerPatch RelativeTypePatch(size_t literal_offset,
221 const DexFile* target_dex_file,
222 uint32_t pc_insn_offset,
223 uint32_t target_type_idx) {
224 LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file);
225 patch.type_idx_ = target_type_idx;
226 patch.pc_insn_offset_ = pc_insn_offset;
227 return patch;
228 }
229
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000230 static LinkerPatch StringPatch(size_t literal_offset,
231 const DexFile* target_dex_file,
232 uint32_t target_string_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100233 LinkerPatch patch(literal_offset, Type::kString, target_dex_file);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000234 patch.string_idx_ = target_string_idx;
235 return patch;
236 }
237
238 static LinkerPatch RelativeStringPatch(size_t literal_offset,
239 const DexFile* target_dex_file,
240 uint32_t pc_insn_offset,
241 uint32_t target_string_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100242 LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000243 patch.string_idx_ = target_string_idx;
244 patch.pc_insn_offset_ = pc_insn_offset;
245 return patch;
246 }
247
Vladimir Markoaad75c62016-10-03 08:46:48 +0000248 static LinkerPatch StringBssEntryPatch(size_t literal_offset,
249 const DexFile* target_dex_file,
250 uint32_t pc_insn_offset,
251 uint32_t target_string_idx) {
252 LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file);
253 patch.string_idx_ = target_string_idx;
254 patch.pc_insn_offset_ = pc_insn_offset;
255 return patch;
256 }
257
Vladimir Marko20f85592015-03-19 10:07:02 +0000258 static LinkerPatch DexCacheArrayPatch(size_t literal_offset,
259 const DexFile* target_dex_file,
260 uint32_t pc_insn_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000261 uint32_t element_offset) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000262 DCHECK(IsUint<32>(element_offset));
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100263 LinkerPatch patch(literal_offset, Type::kDexCacheArray, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000264 patch.pc_insn_offset_ = pc_insn_offset;
265 patch.element_offset_ = element_offset;
266 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100267 }
268
269 LinkerPatch(const LinkerPatch& other) = default;
270 LinkerPatch& operator=(const LinkerPatch& other) = default;
271
272 size_t LiteralOffset() const {
273 return literal_offset_;
274 }
275
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100276 Type GetType() const {
Vladimir Markof4da6752014-08-01 19:04:18 +0100277 return patch_type_;
278 }
279
Vladimir Marko20f85592015-03-19 10:07:02 +0000280 bool IsPcRelative() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100281 switch (GetType()) {
282 case Type::kCallRelative:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100283 case Type::kTypeRelative:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100284 case Type::kStringRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +0000285 case Type::kStringBssEntry:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100286 case Type::kDexCacheArray:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000287 return true;
288 default:
289 return false;
290 }
Vladimir Marko20f85592015-03-19 10:07:02 +0000291 }
292
Vladimir Markof4da6752014-08-01 19:04:18 +0100293 MethodReference TargetMethod() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100294 DCHECK(patch_type_ == Type::kMethod ||
295 patch_type_ == Type::kCall ||
296 patch_type_ == Type::kCallRelative);
Vladimir Marko20f85592015-03-19 10:07:02 +0000297 return MethodReference(target_dex_file_, method_idx_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100298 }
299
300 const DexFile* TargetTypeDexFile() const {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100301 DCHECK(patch_type_ == Type::kType || patch_type_ == Type::kTypeRelative);
Vladimir Markof4da6752014-08-01 19:04:18 +0100302 return target_dex_file_;
303 }
304
305 uint32_t TargetTypeIndex() const {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100306 DCHECK(patch_type_ == Type::kType || patch_type_ == Type::kTypeRelative);
Vladimir Marko20f85592015-03-19 10:07:02 +0000307 return type_idx_;
308 }
309
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000310 const DexFile* TargetStringDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000311 DCHECK(patch_type_ == Type::kString ||
312 patch_type_ == Type::kStringRelative ||
313 patch_type_ == Type::kStringBssEntry);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000314 return target_dex_file_;
315 }
316
317 uint32_t TargetStringIndex() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000318 DCHECK(patch_type_ == Type::kString ||
319 patch_type_ == Type::kStringRelative ||
320 patch_type_ == Type::kStringBssEntry);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000321 return string_idx_;
322 }
323
Vladimir Marko20f85592015-03-19 10:07:02 +0000324 const DexFile* TargetDexCacheDexFile() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100325 DCHECK(patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000326 return target_dex_file_;
327 }
328
329 size_t TargetDexCacheElementOffset() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100330 DCHECK(patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000331 return element_offset_;
332 }
333
334 uint32_t PcInsnOffset() const {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100335 DCHECK(patch_type_ == Type::kTypeRelative ||
336 patch_type_ == Type::kStringRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000337 patch_type_ == Type::kStringBssEntry ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100338 patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000339 return pc_insn_offset_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100340 }
341
342 private:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100343 LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file)
Vladimir Marko20f85592015-03-19 10:07:02 +0000344 : target_dex_file_(target_dex_file),
345 literal_offset_(literal_offset),
346 patch_type_(patch_type) {
347 cmp1_ = 0u;
348 cmp2_ = 0u;
349 // The compiler rejects methods that are too big, so the compiled code
350 // of a single method really shouln't be anywhere close to 16MiB.
351 DCHECK(IsUint<24>(literal_offset));
Vladimir Markof4da6752014-08-01 19:04:18 +0100352 }
353
Vladimir Markof4da6752014-08-01 19:04:18 +0100354 const DexFile* target_dex_file_;
Vladimir Marko20f85592015-03-19 10:07:02 +0000355 uint32_t literal_offset_ : 24; // Method code size up to 16MiB.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100356 Type patch_type_ : 8;
Vladimir Marko20f85592015-03-19 10:07:02 +0000357 union {
358 uint32_t cmp1_; // Used for relational operators.
359 uint32_t method_idx_; // Method index for Call/Method patches.
360 uint32_t type_idx_; // Type index for Type patches.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000361 uint32_t string_idx_; // String index for String patches.
Vladimir Marko20f85592015-03-19 10:07:02 +0000362 uint32_t element_offset_; // Element offset in the dex cache arrays.
Vladimir Marko35831e82015-09-11 11:59:18 +0100363 static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators");
364 static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000365 static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Marko35831e82015-09-11 11:59:18 +0100366 static_assert(sizeof(element_offset_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Marko20f85592015-03-19 10:07:02 +0000367 };
368 union {
Vladimir Marko34ed3af2016-02-04 20:01:32 +0000369 // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`.
370 // This allows a hashing function to treat an array of linker patches as raw memory.
371 size_t cmp2_; // Used for relational operators.
Vladimir Marko20f85592015-03-19 10:07:02 +0000372 // Literal offset of the insn loading PC (same as literal_offset if it's the same insn,
373 // may be different if the PC-relative addressing needs multiple insns).
374 uint32_t pc_insn_offset_;
Vladimir Marko34ed3af2016-02-04 20:01:32 +0000375 static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators");
Vladimir Marko20f85592015-03-19 10:07:02 +0000376 };
Vladimir Markof4da6752014-08-01 19:04:18 +0100377
378 friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs);
379 friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs);
380};
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100381std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type);
Vladimir Markof4da6752014-08-01 19:04:18 +0100382
383inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) {
384 return lhs.literal_offset_ == rhs.literal_offset_ &&
385 lhs.patch_type_ == rhs.patch_type_ &&
Vladimir Marko20f85592015-03-19 10:07:02 +0000386 lhs.target_dex_file_ == rhs.target_dex_file_ &&
387 lhs.cmp1_ == rhs.cmp1_ &&
388 lhs.cmp2_ == rhs.cmp2_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100389}
390
391inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) {
392 return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_
393 : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_
Vladimir Marko20f85592015-03-19 10:07:02 +0000394 : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_
395 : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_
396 : lhs.cmp2_ < rhs.cmp2_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100397}
398
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700399class CompiledMethod FINAL : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700400 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800401 // Constructs a CompiledMethod.
402 // Note: Consider using the static allocation methods below that will allocate the CompiledMethod
403 // in the swap space.
Ian Rogers72d32622014-05-06 16:20:11 -0700404 CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700405 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800406 const ArrayRef<const uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700407 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408 const uint32_t core_spill_mask,
409 const uint32_t fp_spill_mask,
Vladimir Marko35831e82015-09-11 11:59:18 +0100410 const ArrayRef<const SrcMapElem>& src_mapping_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800411 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800412 const ArrayRef<const uint8_t>& cfi_info,
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100413 const ArrayRef<const LinkerPatch>& patches);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700414
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800415 virtual ~CompiledMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700416
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800417 static CompiledMethod* SwapAllocCompiledMethod(
418 CompilerDriver* driver,
419 InstructionSet instruction_set,
420 const ArrayRef<const uint8_t>& quick_code,
421 const size_t frame_size_in_bytes,
422 const uint32_t core_spill_mask,
423 const uint32_t fp_spill_mask,
Vladimir Marko35831e82015-09-11 11:59:18 +0100424 const ArrayRef<const SrcMapElem>& src_mapping_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800425 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800426 const ArrayRef<const uint8_t>& cfi_info,
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100427 const ArrayRef<const LinkerPatch>& patches);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800428
429 static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m);
430
Ian Rogers0c7abda2012-09-19 13:33:42 -0700431 size_t GetFrameSizeInBytes() const {
432 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800433 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700434
435 uint32_t GetCoreSpillMask() const {
436 return core_spill_mask_;
437 }
438
439 uint32_t GetFpSpillMask() const {
440 return fp_spill_mask_;
441 }
442
Vladimir Marko35831e82015-09-11 11:59:18 +0100443 ArrayRef<const SrcMapElem> GetSrcMappingTable() const {
444 return GetArray(src_mapping_table_);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700445 }
446
Vladimir Marko35831e82015-09-11 11:59:18 +0100447 ArrayRef<const uint8_t> GetVmapTable() const {
448 return GetArray(vmap_table_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700449 }
450
Vladimir Marko35831e82015-09-11 11:59:18 +0100451 ArrayRef<const uint8_t> GetCFIInfo() const {
452 return GetArray(cfi_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800453 }
454
Vladimir Markob207e142015-04-02 21:25:21 +0100455 ArrayRef<const LinkerPatch> GetPatches() const {
Vladimir Marko35831e82015-09-11 11:59:18 +0100456 return GetArray(patches_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100457 }
458
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700459 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700460 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700461 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700462 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800463 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700464 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800465 const uint32_t fp_spill_mask_;
David Srbecky6f715892015-03-30 14:21:42 +0100466 // For quick code, a set of pairs (PC, DEX) mapping from native PC offset to DEX offset.
Vladimir Marko35831e82015-09-11 11:59:18 +0100467 const LengthPrefixedArray<SrcMapElem>* const src_mapping_table_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700468 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed.
Vladimir Marko35831e82015-09-11 11:59:18 +0100469 const LengthPrefixedArray<uint8_t>* const vmap_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800470 // For quick code, a FDE entry for the debug_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +0100471 const LengthPrefixedArray<uint8_t>* const cfi_info_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100472 // For quick code, linker patches needed by the method.
Vladimir Marko35831e82015-09-11 11:59:18 +0100473 const LengthPrefixedArray<LinkerPatch>* const patches_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474};
475
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476} // namespace art
477
Mathieu Chartier193bad92013-08-29 18:46:00 -0700478#endif // ART_COMPILER_COMPILED_METHOD_H_