Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 16 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_COMPILED_METHOD_H_ |
| 18 | #define ART_COMPILER_COMPILED_METHOD_H_ |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 21 | #include <iosfwd> |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 22 | #include <string> |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 25 | #include "arch/instruction_set.h" |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 26 | #include "base/array_ref.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "base/bit_utils.h" |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 28 | #include "base/length_prefixed_array.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 29 | #include "dex_file_types.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 30 | #include "method_reference.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 34 | class CompilerDriver; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 35 | class CompiledMethodStorage; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 36 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 37 | class CompiledCode { |
| 38 | public: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 39 | // For Quick to supply an code blob |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 40 | CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 41 | const ArrayRef<const uint8_t>& quick_code); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 42 | |
| 43 | virtual ~CompiledCode(); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 44 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 45 | InstructionSet GetInstructionSet() const { |
| 46 | return instruction_set_; |
| 47 | } |
| 48 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 49 | ArrayRef<const uint8_t> GetQuickCode() const { |
| 50 | return GetArray(quick_code_); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 53 | bool operator==(const CompiledCode& rhs) const; |
| 54 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 55 | // 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | size_t AlignCode(size_t offset) const; |
| 59 | static size_t AlignCode(size_t offset, InstructionSet instruction_set); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 60 | |
| 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 Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 64 | static size_t CodeDelta(InstructionSet instruction_set); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 65 | |
| 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 72 | 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 Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 85 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 86 | private: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 87 | CompilerDriver* const compiler_driver_; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 88 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 89 | const InstructionSet instruction_set_; |
Brian Carlstrom | 8227cc1 | 2013-03-06 14:26:48 -0800 | [diff] [blame] | 90 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | // Used to store the PIC code for Quick. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 92 | const LengthPrefixedArray<uint8_t>* const quick_code_; |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 93 | }; |
| 94 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 95 | class SrcMapElem { |
| 96 | public: |
| 97 | uint32_t from_; |
| 98 | int32_t to_; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 99 | }; |
| 100 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 101 | inline 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 | |
| 108 | inline bool operator==(const SrcMapElem& lhs, const SrcMapElem& rhs) { |
| 109 | return lhs.from_ == rhs.from_ && lhs.to_ == rhs.to_; |
| 110 | } |
| 111 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 112 | template <class Allocator> |
| 113 | class SrcMap FINAL : public std::vector<SrcMapElem, Allocator> { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 114 | public: |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 115 | 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 124 | explicit SrcMap(const Allocator& alloc) : std::vector<SrcMapElem, Allocator>(alloc) {} |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 125 | |
| 126 | template <class InputIt> |
| 127 | SrcMap(InputIt first, InputIt last, const Allocator& alloc) |
| 128 | : std::vector<SrcMapElem, Allocator>(first, last, alloc) {} |
| 129 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 130 | void push_back(const SrcMapElem& elem) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 131 | if (!empty()) { |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 132 | // 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 Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 139 | } |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 140 | std::vector<SrcMapElem, Allocator>::push_back(elem); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 141 | } |
| 142 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 143 | // 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 Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | }; |
| 160 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 161 | using DefaultSrcMap = SrcMap<std::allocator<SrcMapElem>>; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 162 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 163 | class LinkerPatch { |
| 164 | public: |
Vladimir Marko | ef88a11 | 2016-03-31 12:34:48 +0100 | [diff] [blame] | 165 | // 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 Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 173 | kRecordPosition, // Just record patch position for patchoat. |
| 174 | kMethod, |
| 175 | kCall, |
| 176 | kCallRelative, // NOTE: Actual patching is instruction_set-dependent. |
| 177 | kType, |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 178 | kTypeRelative, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 179 | kString, |
| 180 | kStringRelative, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 181 | kStringBssEntry, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 182 | kDexCacheArray, // NOTE: Actual patching is instruction_set-dependent. |
| 183 | }; |
| 184 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 185 | static LinkerPatch RecordPosition(size_t literal_offset) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 186 | return LinkerPatch(literal_offset, Type::kRecordPosition, /* target_dex_file */ nullptr); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 189 | static LinkerPatch MethodPatch(size_t literal_offset, |
| 190 | const DexFile* target_dex_file, |
| 191 | uint32_t target_method_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 192 | LinkerPatch patch(literal_offset, Type::kMethod, target_dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 193 | patch.method_idx_ = target_method_idx; |
| 194 | return patch; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static LinkerPatch CodePatch(size_t literal_offset, |
| 198 | const DexFile* target_dex_file, |
| 199 | uint32_t target_method_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 200 | LinkerPatch patch(literal_offset, Type::kCall, target_dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 201 | patch.method_idx_ = target_method_idx; |
| 202 | return patch; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static LinkerPatch RelativeCodePatch(size_t literal_offset, |
| 206 | const DexFile* target_dex_file, |
| 207 | uint32_t target_method_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 208 | LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 209 | patch.method_idx_ = target_method_idx; |
| 210 | return patch; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static LinkerPatch TypePatch(size_t literal_offset, |
| 214 | const DexFile* target_dex_file, |
| 215 | uint32_t target_type_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 216 | LinkerPatch patch(literal_offset, Type::kType, target_dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 217 | patch.type_idx_ = target_type_idx; |
| 218 | return patch; |
| 219 | } |
| 220 | |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 221 | static LinkerPatch RelativeTypePatch(size_t literal_offset, |
| 222 | const DexFile* target_dex_file, |
| 223 | uint32_t pc_insn_offset, |
| 224 | uint32_t target_type_idx) { |
| 225 | LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file); |
| 226 | patch.type_idx_ = target_type_idx; |
| 227 | patch.pc_insn_offset_ = pc_insn_offset; |
| 228 | return patch; |
| 229 | } |
| 230 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 231 | static LinkerPatch StringPatch(size_t literal_offset, |
| 232 | const DexFile* target_dex_file, |
| 233 | uint32_t target_string_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 234 | LinkerPatch patch(literal_offset, Type::kString, target_dex_file); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 235 | patch.string_idx_ = target_string_idx; |
| 236 | return patch; |
| 237 | } |
| 238 | |
| 239 | static LinkerPatch RelativeStringPatch(size_t literal_offset, |
| 240 | const DexFile* target_dex_file, |
| 241 | uint32_t pc_insn_offset, |
| 242 | uint32_t target_string_idx) { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 243 | LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 244 | patch.string_idx_ = target_string_idx; |
| 245 | patch.pc_insn_offset_ = pc_insn_offset; |
| 246 | return patch; |
| 247 | } |
| 248 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 249 | static LinkerPatch StringBssEntryPatch(size_t literal_offset, |
| 250 | const DexFile* target_dex_file, |
| 251 | uint32_t pc_insn_offset, |
| 252 | uint32_t target_string_idx) { |
| 253 | LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file); |
| 254 | patch.string_idx_ = target_string_idx; |
| 255 | patch.pc_insn_offset_ = pc_insn_offset; |
| 256 | return patch; |
| 257 | } |
| 258 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 259 | static LinkerPatch DexCacheArrayPatch(size_t literal_offset, |
| 260 | const DexFile* target_dex_file, |
| 261 | uint32_t pc_insn_offset, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 262 | uint32_t element_offset) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 263 | DCHECK(IsUint<32>(element_offset)); |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 264 | LinkerPatch patch(literal_offset, Type::kDexCacheArray, target_dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 265 | patch.pc_insn_offset_ = pc_insn_offset; |
| 266 | patch.element_offset_ = element_offset; |
| 267 | return patch; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | LinkerPatch(const LinkerPatch& other) = default; |
| 271 | LinkerPatch& operator=(const LinkerPatch& other) = default; |
| 272 | |
| 273 | size_t LiteralOffset() const { |
| 274 | return literal_offset_; |
| 275 | } |
| 276 | |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 277 | Type GetType() const { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 278 | return patch_type_; |
| 279 | } |
| 280 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 281 | bool IsPcRelative() const { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 282 | switch (GetType()) { |
| 283 | case Type::kCallRelative: |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 284 | case Type::kTypeRelative: |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 285 | case Type::kStringRelative: |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 286 | case Type::kStringBssEntry: |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 287 | case Type::kDexCacheArray: |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 288 | return true; |
| 289 | default: |
| 290 | return false; |
| 291 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 294 | MethodReference TargetMethod() const { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 295 | DCHECK(patch_type_ == Type::kMethod || |
| 296 | patch_type_ == Type::kCall || |
| 297 | patch_type_ == Type::kCallRelative); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 298 | return MethodReference(target_dex_file_, method_idx_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | const DexFile* TargetTypeDexFile() const { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 302 | DCHECK(patch_type_ == Type::kType || patch_type_ == Type::kTypeRelative); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 303 | return target_dex_file_; |
| 304 | } |
| 305 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 306 | dex::TypeIndex TargetTypeIndex() const { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 307 | DCHECK(patch_type_ == Type::kType || patch_type_ == Type::kTypeRelative); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 308 | return dex::TypeIndex(type_idx_); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 311 | const DexFile* TargetStringDexFile() const { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 312 | DCHECK(patch_type_ == Type::kString || |
| 313 | patch_type_ == Type::kStringRelative || |
| 314 | patch_type_ == Type::kStringBssEntry); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 315 | return target_dex_file_; |
| 316 | } |
| 317 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 318 | dex::StringIndex TargetStringIndex() const { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 319 | DCHECK(patch_type_ == Type::kString || |
| 320 | patch_type_ == Type::kStringRelative || |
| 321 | patch_type_ == Type::kStringBssEntry); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 322 | return dex::StringIndex(string_idx_); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 325 | const DexFile* TargetDexCacheDexFile() const { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 326 | DCHECK(patch_type_ == Type::kDexCacheArray); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 327 | return target_dex_file_; |
| 328 | } |
| 329 | |
| 330 | size_t TargetDexCacheElementOffset() const { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 331 | DCHECK(patch_type_ == Type::kDexCacheArray); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 332 | return element_offset_; |
| 333 | } |
| 334 | |
| 335 | uint32_t PcInsnOffset() const { |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 336 | DCHECK(patch_type_ == Type::kTypeRelative || |
| 337 | patch_type_ == Type::kStringRelative || |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 338 | patch_type_ == Type::kStringBssEntry || |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 339 | patch_type_ == Type::kDexCacheArray); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 340 | return pc_insn_offset_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | private: |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 344 | LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file) |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 345 | : target_dex_file_(target_dex_file), |
| 346 | literal_offset_(literal_offset), |
| 347 | patch_type_(patch_type) { |
| 348 | cmp1_ = 0u; |
| 349 | cmp2_ = 0u; |
| 350 | // The compiler rejects methods that are too big, so the compiled code |
| 351 | // of a single method really shouln't be anywhere close to 16MiB. |
| 352 | DCHECK(IsUint<24>(literal_offset)); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 355 | const DexFile* target_dex_file_; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 356 | uint32_t literal_offset_ : 24; // Method code size up to 16MiB. |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 357 | Type patch_type_ : 8; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 358 | union { |
| 359 | uint32_t cmp1_; // Used for relational operators. |
| 360 | uint32_t method_idx_; // Method index for Call/Method patches. |
| 361 | uint32_t type_idx_; // Type index for Type patches. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 362 | uint32_t string_idx_; // String index for String patches. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 363 | uint32_t element_offset_; // Element offset in the dex cache arrays. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 364 | static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators"); |
| 365 | static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 366 | static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators"); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 367 | static_assert(sizeof(element_offset_) == sizeof(cmp1_), "needed by relational operators"); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 368 | }; |
| 369 | union { |
Vladimir Marko | 34ed3af | 2016-02-04 20:01:32 +0000 | [diff] [blame] | 370 | // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`. |
| 371 | // This allows a hashing function to treat an array of linker patches as raw memory. |
| 372 | size_t cmp2_; // Used for relational operators. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 373 | // Literal offset of the insn loading PC (same as literal_offset if it's the same insn, |
| 374 | // may be different if the PC-relative addressing needs multiple insns). |
| 375 | uint32_t pc_insn_offset_; |
Vladimir Marko | 34ed3af | 2016-02-04 20:01:32 +0000 | [diff] [blame] | 376 | static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators"); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 377 | }; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 378 | |
| 379 | friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs); |
| 380 | friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs); |
| 381 | }; |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 382 | std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 383 | |
| 384 | inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 385 | return lhs.literal_offset_ == rhs.literal_offset_ && |
| 386 | lhs.patch_type_ == rhs.patch_type_ && |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 387 | lhs.target_dex_file_ == rhs.target_dex_file_ && |
| 388 | lhs.cmp1_ == rhs.cmp1_ && |
| 389 | lhs.cmp2_ == rhs.cmp2_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 393 | return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_ |
| 394 | : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_ |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 395 | : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_ |
| 396 | : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_ |
| 397 | : lhs.cmp2_ < rhs.cmp2_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 400 | class CompiledMethod FINAL : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 401 | public: |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 402 | // Constructs a CompiledMethod. |
| 403 | // Note: Consider using the static allocation methods below that will allocate the CompiledMethod |
| 404 | // in the swap space. |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 405 | CompiledMethod(CompilerDriver* driver, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 406 | InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 407 | const ArrayRef<const uint8_t>& quick_code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 408 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 409 | const uint32_t core_spill_mask, |
| 410 | const uint32_t fp_spill_mask, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 411 | const ArrayRef<const SrcMapElem>& src_mapping_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 412 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 413 | const ArrayRef<const uint8_t>& cfi_info, |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 414 | const ArrayRef<const LinkerPatch>& patches); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 415 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 416 | virtual ~CompiledMethod(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 417 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 418 | static CompiledMethod* SwapAllocCompiledMethod( |
| 419 | CompilerDriver* driver, |
| 420 | InstructionSet instruction_set, |
| 421 | const ArrayRef<const uint8_t>& quick_code, |
| 422 | const size_t frame_size_in_bytes, |
| 423 | const uint32_t core_spill_mask, |
| 424 | const uint32_t fp_spill_mask, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 425 | const ArrayRef<const SrcMapElem>& src_mapping_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 426 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 427 | const ArrayRef<const uint8_t>& cfi_info, |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 428 | const ArrayRef<const LinkerPatch>& patches); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 429 | |
| 430 | static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m); |
| 431 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 432 | size_t GetFrameSizeInBytes() const { |
| 433 | return frame_size_in_bytes_; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 434 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 435 | |
| 436 | uint32_t GetCoreSpillMask() const { |
| 437 | return core_spill_mask_; |
| 438 | } |
| 439 | |
| 440 | uint32_t GetFpSpillMask() const { |
| 441 | return fp_spill_mask_; |
| 442 | } |
| 443 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 444 | ArrayRef<const SrcMapElem> GetSrcMappingTable() const { |
| 445 | return GetArray(src_mapping_table_); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 446 | } |
| 447 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 448 | ArrayRef<const uint8_t> GetVmapTable() const { |
| 449 | return GetArray(vmap_table_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 452 | ArrayRef<const uint8_t> GetCFIInfo() const { |
| 453 | return GetArray(cfi_info_); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 456 | ArrayRef<const LinkerPatch> GetPatches() const { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 457 | return GetArray(patches_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 460 | private: |
Ian Rogers | a182704 | 2013-04-18 16:36:43 -0700 | [diff] [blame] | 461 | // For quick code, the size of the activation used by the code. |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 462 | const size_t frame_size_in_bytes_; |
Ian Rogers | a182704 | 2013-04-18 16:36:43 -0700 | [diff] [blame] | 463 | // For quick code, a bit mask describing spilled GPR callee-save registers. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 464 | const uint32_t core_spill_mask_; |
Ian Rogers | a182704 | 2013-04-18 16:36:43 -0700 | [diff] [blame] | 465 | // For quick code, a bit mask describing spilled FPR callee-save registers. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 466 | const uint32_t fp_spill_mask_; |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 467 | // For quick code, a set of pairs (PC, DEX) mapping from native PC offset to DEX offset. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 468 | const LengthPrefixedArray<SrcMapElem>* const src_mapping_table_; |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 469 | // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 470 | const LengthPrefixedArray<uint8_t>* const vmap_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 471 | // For quick code, a FDE entry for the debug_frame section. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 472 | const LengthPrefixedArray<uint8_t>* const cfi_info_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 473 | // For quick code, linker patches needed by the method. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 474 | const LengthPrefixedArray<LinkerPatch>* const patches_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 475 | }; |
| 476 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 477 | } // namespace art |
| 478 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 479 | #endif // ART_COMPILER_COMPILED_METHOD_H_ |