blob: 23422570f0b27c0e5a06894f71a5505b32f88d62 [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"
Andreas Gampea5b09a62016-11-17 15:21:22 -080029#include "dex_file_types.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010030#include "method_reference.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031
32namespace art {
33
Mathieu Chartier193bad92013-08-29 18:46:00 -070034class CompilerDriver;
Vladimir Marko35831e82015-09-11 11:59:18 +010035class CompiledMethodStorage;
Mathieu Chartier193bad92013-08-29 18:46:00 -070036
Logan Chien598c5132012-04-28 22:00:44 +080037class CompiledCode {
38 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080039 // For Quick to supply an code blob
Mathieu Chartier193bad92013-08-29 18:46:00 -070040 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010041 const ArrayRef<const uint8_t>& quick_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042
43 virtual ~CompiledCode();
Logan Chien598c5132012-04-28 22:00:44 +080044
Logan Chien598c5132012-04-28 22:00:44 +080045 InstructionSet GetInstructionSet() const {
46 return instruction_set_;
47 }
48
Vladimir Marko35831e82015-09-11 11:59:18 +010049 ArrayRef<const uint8_t> GetQuickCode() const {
50 return GetArray(quick_code_);
Logan Chien598c5132012-04-28 22:00:44 +080051 }
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 bool operator==(const CompiledCode& rhs) const;
54
Logan Chien598c5132012-04-28 22:00:44 +080055 // To align an offset from a page-aligned value to make it suitable
56 // for code storage. For example on ARM, to ensure that PC relative
57 // valu computations work out as expected.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 size_t AlignCode(size_t offset) const;
59 static size_t AlignCode(size_t offset, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080060
61 // returns the difference between the code address and a usable PC.
62 // mainly to cope with kThumb2 where the lower bit must be set.
63 size_t CodeDelta() const;
Dave Allison50abf0a2014-06-23 13:19:59 -070064 static size_t CodeDelta(InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080065
66 // Returns a pointer suitable for invoking the code at the argument
67 // code_pointer address. Mainly to cope with kThumb2 where the
68 // lower bit must be set to indicate Thumb mode.
69 static const void* CodePointer(const void* code_pointer,
70 InstructionSet instruction_set);
71
Vladimir Marko35831e82015-09-11 11:59:18 +010072 protected:
73 template <typename T>
74 static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array) {
75 if (array == nullptr) {
76 return ArrayRef<const T>();
77 }
78 DCHECK_NE(array->size(), 0u);
79 return ArrayRef<const T>(&array->At(0), array->size());
80 }
81
82 CompilerDriver* GetCompilerDriver() {
83 return compiler_driver_;
84 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080085
Logan Chien598c5132012-04-28 22:00:44 +080086 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080087 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070088
Logan Chien598c5132012-04-28 22:00:44 +080089 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080090
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 // Used to store the PIC code for Quick.
Vladimir Marko35831e82015-09-11 11:59:18 +010092 const LengthPrefixedArray<uint8_t>* const quick_code_;
Logan Chien598c5132012-04-28 22:00:44 +080093};
94
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070095class SrcMapElem {
96 public:
97 uint32_t from_;
98 int32_t to_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070099};
100
Vladimir Marko35831e82015-09-11 11:59:18 +0100101inline bool operator<(const SrcMapElem& lhs, const SrcMapElem& rhs) {
102 if (lhs.from_ != rhs.from_) {
103 return lhs.from_ < rhs.from_;
104 }
105 return lhs.to_ < rhs.to_;
106}
107
108inline bool operator==(const SrcMapElem& lhs, const SrcMapElem& rhs) {
109 return lhs.from_ == rhs.from_ && lhs.to_ == rhs.to_;
110}
111
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800112template <class Allocator>
113class SrcMap FINAL : public std::vector<SrcMapElem, Allocator> {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700114 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800115 using std::vector<SrcMapElem, Allocator>::begin;
116 using typename std::vector<SrcMapElem, Allocator>::const_iterator;
117 using std::vector<SrcMapElem, Allocator>::empty;
118 using std::vector<SrcMapElem, Allocator>::end;
119 using std::vector<SrcMapElem, Allocator>::resize;
120 using std::vector<SrcMapElem, Allocator>::shrink_to_fit;
121 using std::vector<SrcMapElem, Allocator>::size;
122
123 explicit SrcMap() {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 explicit SrcMap(const Allocator& alloc) : std::vector<SrcMapElem, Allocator>(alloc) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800125
126 template <class InputIt>
127 SrcMap(InputIt first, InputIt last, const Allocator& alloc)
128 : std::vector<SrcMapElem, Allocator>(first, last, alloc) {}
129
David Srbecky6f715892015-03-30 14:21:42 +0100130 void push_back(const SrcMapElem& elem) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700131 if (!empty()) {
David Srbecky6f715892015-03-30 14:21:42 +0100132 // Check that the addresses are inserted in sorted order.
133 DCHECK_GE(elem.from_, this->back().from_);
134 // If two consequitive entries map to the same value, ignore the later.
135 // E.g. for map {{0, 1}, {4, 1}, {8, 2}}, all values in [0,8) map to 1.
136 if (elem.to_ == this->back().to_) {
137 return;
138 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700139 }
David Srbecky6f715892015-03-30 14:21:42 +0100140 std::vector<SrcMapElem, Allocator>::push_back(elem);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700141 }
142
David Srbecky6f715892015-03-30 14:21:42 +0100143 // Returns true and the corresponding "to" value if the mapping is found.
144 // Oterwise returns false and 0.
145 std::pair<bool, int32_t> Find(uint32_t from) const {
146 // Finds first mapping such that lb.from_ >= from.
147 auto lb = std::lower_bound(begin(), end(), SrcMapElem {from, INT32_MIN});
148 if (lb != end() && lb->from_ == from) {
149 // Found exact match.
150 return std::make_pair(true, lb->to_);
151 } else if (lb != begin()) {
152 // The previous mapping is still in effect.
153 return std::make_pair(true, (--lb)->to_);
154 } else {
155 // Not found because 'from' is smaller than first entry in the map.
156 return std::make_pair(false, 0);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700157 }
158 }
159};
160
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800161using DefaultSrcMap = SrcMap<std::allocator<SrcMapElem>>;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800162
Vladimir Markof4da6752014-08-01 19:04:18 +0100163class LinkerPatch {
164 public:
Vladimir Markoef88a112016-03-31 12:34:48 +0100165 // Note: We explicitly specify the underlying type of the enum because GCC
166 // would otherwise select a bigger underlying type and then complain that
167 // 'art::LinkerPatch::patch_type_' is too small to hold all
168 // values of 'enum class art::LinkerPatch::Type'
169 // which is ridiculous given we have only a handful of values here. If we
170 // choose to squeeze the Type into fewer than 8 bits, we'll have to declare
171 // patch_type_ as an uintN_t and do explicit static_cast<>s.
172 enum class Type : uint8_t {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100173 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 Marko6bec91c2017-01-09 15:03:12 +0000178 kTypeBssEntry, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100179 kString,
180 kStringRelative, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000181 kStringBssEntry, // NOTE: Actual patching is instruction_set-dependent.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100182 kDexCacheArray, // NOTE: Actual patching is instruction_set-dependent.
183 };
184
Vladimir Markof4da6752014-08-01 19:04:18 +0100185 static LinkerPatch MethodPatch(size_t literal_offset,
186 const DexFile* target_dex_file,
187 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100188 LinkerPatch patch(literal_offset, Type::kMethod, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000189 patch.method_idx_ = target_method_idx;
190 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100191 }
192
193 static LinkerPatch CodePatch(size_t literal_offset,
194 const DexFile* target_dex_file,
195 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100196 LinkerPatch patch(literal_offset, Type::kCall, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000197 patch.method_idx_ = target_method_idx;
198 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100199 }
200
201 static LinkerPatch RelativeCodePatch(size_t literal_offset,
202 const DexFile* target_dex_file,
203 uint32_t target_method_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100204 LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000205 patch.method_idx_ = target_method_idx;
206 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100207 }
208
209 static LinkerPatch TypePatch(size_t literal_offset,
210 const DexFile* target_dex_file,
211 uint32_t target_type_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100212 LinkerPatch patch(literal_offset, Type::kType, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000213 patch.type_idx_ = target_type_idx;
214 return patch;
215 }
216
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100217 static LinkerPatch RelativeTypePatch(size_t literal_offset,
218 const DexFile* target_dex_file,
219 uint32_t pc_insn_offset,
220 uint32_t target_type_idx) {
221 LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file);
222 patch.type_idx_ = target_type_idx;
223 patch.pc_insn_offset_ = pc_insn_offset;
224 return patch;
225 }
226
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000227 static LinkerPatch TypeBssEntryPatch(size_t literal_offset,
228 const DexFile* target_dex_file,
229 uint32_t pc_insn_offset,
230 uint32_t target_type_idx) {
231 LinkerPatch patch(literal_offset, Type::kTypeBssEntry, target_dex_file);
232 patch.type_idx_ = target_type_idx;
233 patch.pc_insn_offset_ = pc_insn_offset;
234 return patch;
235 }
236
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000237 static LinkerPatch StringPatch(size_t literal_offset,
238 const DexFile* target_dex_file,
239 uint32_t target_string_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100240 LinkerPatch patch(literal_offset, Type::kString, target_dex_file);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000241 patch.string_idx_ = target_string_idx;
242 return patch;
243 }
244
245 static LinkerPatch RelativeStringPatch(size_t literal_offset,
246 const DexFile* target_dex_file,
247 uint32_t pc_insn_offset,
248 uint32_t target_string_idx) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100249 LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000250 patch.string_idx_ = target_string_idx;
251 patch.pc_insn_offset_ = pc_insn_offset;
252 return patch;
253 }
254
Vladimir Markoaad75c62016-10-03 08:46:48 +0000255 static LinkerPatch StringBssEntryPatch(size_t literal_offset,
256 const DexFile* target_dex_file,
257 uint32_t pc_insn_offset,
258 uint32_t target_string_idx) {
259 LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file);
260 patch.string_idx_ = target_string_idx;
261 patch.pc_insn_offset_ = pc_insn_offset;
262 return patch;
263 }
264
Vladimir Marko20f85592015-03-19 10:07:02 +0000265 static LinkerPatch DexCacheArrayPatch(size_t literal_offset,
266 const DexFile* target_dex_file,
267 uint32_t pc_insn_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000268 uint32_t element_offset) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000269 DCHECK(IsUint<32>(element_offset));
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100270 LinkerPatch patch(literal_offset, Type::kDexCacheArray, target_dex_file);
Vladimir Marko20f85592015-03-19 10:07:02 +0000271 patch.pc_insn_offset_ = pc_insn_offset;
272 patch.element_offset_ = element_offset;
273 return patch;
Vladimir Markof4da6752014-08-01 19:04:18 +0100274 }
275
276 LinkerPatch(const LinkerPatch& other) = default;
277 LinkerPatch& operator=(const LinkerPatch& other) = default;
278
279 size_t LiteralOffset() const {
280 return literal_offset_;
281 }
282
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100283 Type GetType() const {
Vladimir Markof4da6752014-08-01 19:04:18 +0100284 return patch_type_;
285 }
286
Vladimir Marko20f85592015-03-19 10:07:02 +0000287 bool IsPcRelative() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100288 switch (GetType()) {
289 case Type::kCallRelative:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100290 case Type::kTypeRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 case Type::kTypeBssEntry:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100292 case Type::kStringRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +0000293 case Type::kStringBssEntry:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100294 case Type::kDexCacheArray:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000295 return true;
296 default:
297 return false;
298 }
Vladimir Marko20f85592015-03-19 10:07:02 +0000299 }
300
Vladimir Markof4da6752014-08-01 19:04:18 +0100301 MethodReference TargetMethod() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100302 DCHECK(patch_type_ == Type::kMethod ||
303 patch_type_ == Type::kCall ||
304 patch_type_ == Type::kCallRelative);
Vladimir Marko20f85592015-03-19 10:07:02 +0000305 return MethodReference(target_dex_file_, method_idx_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100306 }
307
308 const DexFile* TargetTypeDexFile() const {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000309 DCHECK(patch_type_ == Type::kType ||
310 patch_type_ == Type::kTypeRelative ||
311 patch_type_ == Type::kTypeBssEntry);
Vladimir Markof4da6752014-08-01 19:04:18 +0100312 return target_dex_file_;
313 }
314
Andreas Gampea5b09a62016-11-17 15:21:22 -0800315 dex::TypeIndex TargetTypeIndex() const {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 DCHECK(patch_type_ == Type::kType ||
317 patch_type_ == Type::kTypeRelative ||
318 patch_type_ == Type::kTypeBssEntry);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800319 return dex::TypeIndex(type_idx_);
Vladimir Marko20f85592015-03-19 10:07:02 +0000320 }
321
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000322 const DexFile* TargetStringDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000323 DCHECK(patch_type_ == Type::kString ||
324 patch_type_ == Type::kStringRelative ||
325 patch_type_ == Type::kStringBssEntry);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000326 return target_dex_file_;
327 }
328
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800329 dex::StringIndex TargetStringIndex() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000330 DCHECK(patch_type_ == Type::kString ||
331 patch_type_ == Type::kStringRelative ||
332 patch_type_ == Type::kStringBssEntry);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800333 return dex::StringIndex(string_idx_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000334 }
335
Vladimir Marko20f85592015-03-19 10:07:02 +0000336 const DexFile* TargetDexCacheDexFile() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100337 DCHECK(patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000338 return target_dex_file_;
339 }
340
341 size_t TargetDexCacheElementOffset() const {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100342 DCHECK(patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000343 return element_offset_;
344 }
345
346 uint32_t PcInsnOffset() const {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100347 DCHECK(patch_type_ == Type::kTypeRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000348 patch_type_ == Type::kTypeBssEntry ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100349 patch_type_ == Type::kStringRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350 patch_type_ == Type::kStringBssEntry ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100351 patch_type_ == Type::kDexCacheArray);
Vladimir Marko20f85592015-03-19 10:07:02 +0000352 return pc_insn_offset_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100353 }
354
355 private:
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100356 LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file)
Vladimir Marko20f85592015-03-19 10:07:02 +0000357 : target_dex_file_(target_dex_file),
358 literal_offset_(literal_offset),
359 patch_type_(patch_type) {
360 cmp1_ = 0u;
361 cmp2_ = 0u;
362 // The compiler rejects methods that are too big, so the compiled code
363 // of a single method really shouln't be anywhere close to 16MiB.
364 DCHECK(IsUint<24>(literal_offset));
Vladimir Markof4da6752014-08-01 19:04:18 +0100365 }
366
Vladimir Markof4da6752014-08-01 19:04:18 +0100367 const DexFile* target_dex_file_;
Vladimir Marko20f85592015-03-19 10:07:02 +0000368 uint32_t literal_offset_ : 24; // Method code size up to 16MiB.
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100369 Type patch_type_ : 8;
Vladimir Marko20f85592015-03-19 10:07:02 +0000370 union {
371 uint32_t cmp1_; // Used for relational operators.
372 uint32_t method_idx_; // Method index for Call/Method patches.
373 uint32_t type_idx_; // Type index for Type patches.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000374 uint32_t string_idx_; // String index for String patches.
Vladimir Marko20f85592015-03-19 10:07:02 +0000375 uint32_t element_offset_; // Element offset in the dex cache arrays.
Vladimir Marko35831e82015-09-11 11:59:18 +0100376 static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators");
377 static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000378 static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Marko35831e82015-09-11 11:59:18 +0100379 static_assert(sizeof(element_offset_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Marko20f85592015-03-19 10:07:02 +0000380 };
381 union {
Vladimir Marko34ed3af2016-02-04 20:01:32 +0000382 // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`.
383 // This allows a hashing function to treat an array of linker patches as raw memory.
384 size_t cmp2_; // Used for relational operators.
Vladimir Marko20f85592015-03-19 10:07:02 +0000385 // Literal offset of the insn loading PC (same as literal_offset if it's the same insn,
386 // may be different if the PC-relative addressing needs multiple insns).
387 uint32_t pc_insn_offset_;
Vladimir Marko34ed3af2016-02-04 20:01:32 +0000388 static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators");
Vladimir Marko20f85592015-03-19 10:07:02 +0000389 };
Vladimir Markof4da6752014-08-01 19:04:18 +0100390
391 friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs);
392 friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs);
393};
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100394std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type);
Vladimir Markof4da6752014-08-01 19:04:18 +0100395
396inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) {
397 return lhs.literal_offset_ == rhs.literal_offset_ &&
398 lhs.patch_type_ == rhs.patch_type_ &&
Vladimir Marko20f85592015-03-19 10:07:02 +0000399 lhs.target_dex_file_ == rhs.target_dex_file_ &&
400 lhs.cmp1_ == rhs.cmp1_ &&
401 lhs.cmp2_ == rhs.cmp2_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100402}
403
404inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) {
405 return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_
406 : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_
Vladimir Marko20f85592015-03-19 10:07:02 +0000407 : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_
408 : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_
409 : lhs.cmp2_ < rhs.cmp2_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100410}
411
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700412class CompiledMethod FINAL : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700413 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800414 // Constructs a CompiledMethod.
415 // Note: Consider using the static allocation methods below that will allocate the CompiledMethod
416 // in the swap space.
Ian Rogers72d32622014-05-06 16:20:11 -0700417 CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700418 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800419 const ArrayRef<const uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700420 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700421 const uint32_t core_spill_mask,
422 const uint32_t fp_spill_mask,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700423 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800424 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800425 const ArrayRef<const uint8_t>& cfi_info,
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100426 const ArrayRef<const LinkerPatch>& patches);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700427
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800428 virtual ~CompiledMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700429
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800430 static CompiledMethod* SwapAllocCompiledMethod(
431 CompilerDriver* driver,
432 InstructionSet instruction_set,
433 const ArrayRef<const uint8_t>& quick_code,
434 const size_t frame_size_in_bytes,
435 const uint32_t core_spill_mask,
436 const uint32_t fp_spill_mask,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700437 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800438 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800439 const ArrayRef<const uint8_t>& cfi_info,
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100440 const ArrayRef<const LinkerPatch>& patches);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800441
442 static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m);
443
Ian Rogers0c7abda2012-09-19 13:33:42 -0700444 size_t GetFrameSizeInBytes() const {
445 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800446 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700447
448 uint32_t GetCoreSpillMask() const {
449 return core_spill_mask_;
450 }
451
452 uint32_t GetFpSpillMask() const {
453 return fp_spill_mask_;
454 }
455
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700456 ArrayRef<const uint8_t> GetMethodInfo() const {
457 return GetArray(method_info_);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700458 }
459
Vladimir Marko35831e82015-09-11 11:59:18 +0100460 ArrayRef<const uint8_t> GetVmapTable() const {
461 return GetArray(vmap_table_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700462 }
463
Vladimir Marko35831e82015-09-11 11:59:18 +0100464 ArrayRef<const uint8_t> GetCFIInfo() const {
465 return GetArray(cfi_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800466 }
467
Vladimir Markob207e142015-04-02 21:25:21 +0100468 ArrayRef<const LinkerPatch> GetPatches() const {
Vladimir Marko35831e82015-09-11 11:59:18 +0100469 return GetArray(patches_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100470 }
471
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700472 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700473 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700474 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700475 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800476 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700477 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800478 const uint32_t fp_spill_mask_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700479 // For quick code, method specific information that is not very dedupe friendly (method indices).
480 const LengthPrefixedArray<uint8_t>* const method_info_;
481 // For quick code, holds code infos which contain stack maps, inline information, and etc.
Vladimir Marko35831e82015-09-11 11:59:18 +0100482 const LengthPrefixedArray<uint8_t>* const vmap_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800483 // For quick code, a FDE entry for the debug_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +0100484 const LengthPrefixedArray<uint8_t>* const cfi_info_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100485 // For quick code, linker patches needed by the method.
Vladimir Marko35831e82015-09-11 11:59:18 +0100486 const LengthPrefixedArray<LinkerPatch>* const patches_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700487};
488
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700489} // namespace art
490
Mathieu Chartier193bad92013-08-29 18:46:00 -0700491#endif // ART_COMPILER_COMPILED_METHOD_H_