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