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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <memory> |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 21 | #include <string> |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 25 | #include "base/bit_field.h" |
| 26 | #include "base/bit_utils.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 30 | template <typename T> class ArrayRef; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 31 | class CompiledMethodStorage; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 32 | template<typename T> class LengthPrefixedArray; |
| 33 | |
| 34 | namespace linker { |
| 35 | class LinkerPatch; |
| 36 | } // namespace linker |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 37 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 38 | class CompiledCode { |
| 39 | public: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 40 | // For Quick to supply an code blob |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 41 | CompiledCode(CompiledMethodStorage* storage, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 42 | InstructionSet instruction_set, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 43 | const ArrayRef<const uint8_t>& quick_code); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 44 | |
| 45 | virtual ~CompiledCode(); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 46 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 47 | InstructionSet GetInstructionSet() const { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 48 | return GetPackedField<InstructionSetField>(); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 51 | ArrayRef<const uint8_t> GetQuickCode() const; |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 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. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 69 | static const void* CodePointer(const void* code_pointer, InstructionSet instruction_set); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 70 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 71 | protected: |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 72 | static constexpr size_t kInstructionSetFieldSize = |
| 73 | MinimumBitsToStore(static_cast<size_t>(InstructionSet::kLast)); |
| 74 | static constexpr size_t kNumberOfCompiledCodePackedBits = kInstructionSetFieldSize; |
| 75 | static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte; |
| 76 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 77 | template <typename T> |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 78 | static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 79 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 80 | CompiledMethodStorage* GetStorage() { |
| 81 | return storage_; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 82 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 83 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 84 | template <typename BitFieldType> |
| 85 | typename BitFieldType::value_type GetPackedField() const { |
| 86 | return BitFieldType::Decode(packed_fields_); |
| 87 | } |
| 88 | |
| 89 | template <typename BitFieldType> |
| 90 | void SetPackedField(typename BitFieldType::value_type value) { |
| 91 | DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value))); |
| 92 | packed_fields_ = BitFieldType::Update(value, packed_fields_); |
| 93 | } |
| 94 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 95 | private: |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 96 | using InstructionSetField = BitField<InstructionSet, 0u, kInstructionSetFieldSize>; |
| 97 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 98 | CompiledMethodStorage* const storage_; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 99 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 100 | // Used to store the compiled code. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 101 | const LengthPrefixedArray<uint8_t>* const quick_code_; |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 102 | |
| 103 | uint32_t packed_fields_; |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 104 | }; |
| 105 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 106 | class CompiledMethod final : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 107 | public: |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 108 | // Constructs a CompiledMethod. |
| 109 | // Note: Consider using the static allocation methods below that will allocate the CompiledMethod |
| 110 | // in the swap space. |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 111 | CompiledMethod(CompiledMethodStorage* storage, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 112 | InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 113 | const ArrayRef<const uint8_t>& quick_code, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 114 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 115 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 116 | const ArrayRef<const linker::LinkerPatch>& patches); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 117 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 118 | virtual ~CompiledMethod(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 119 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 120 | static CompiledMethod* SwapAllocCompiledMethod( |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 121 | CompiledMethodStorage* storage, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 122 | InstructionSet instruction_set, |
| 123 | const ArrayRef<const uint8_t>& quick_code, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 124 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 125 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 126 | const ArrayRef<const linker::LinkerPatch>& patches); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 127 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 128 | static void ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage, CompiledMethod* m); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 129 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 130 | bool IsIntrinsic() const { |
| 131 | return GetPackedField<IsIntrinsicField>(); |
| 132 | } |
| 133 | |
| 134 | // Marks the compiled method as being generated using an intrinsic codegen. |
| 135 | // Such methods have no relationships to their code items. |
| 136 | // This affects debug information generated at link time. |
| 137 | void MarkAsIntrinsic() { |
| 138 | DCHECK(!IsIntrinsic()); |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 139 | SetPackedField<IsIntrinsicField>(/* value= */ true); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 142 | ArrayRef<const uint8_t> GetVmapTable() const; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 143 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 144 | ArrayRef<const uint8_t> GetCFIInfo() const; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 145 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 146 | ArrayRef<const linker::LinkerPatch> GetPatches() const; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 147 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 148 | private: |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 149 | static constexpr size_t kIsIntrinsicLsb = kNumberOfCompiledCodePackedBits; |
| 150 | static constexpr size_t kIsIntrinsicSize = 1u; |
| 151 | static constexpr size_t kNumberOfCompiledMethodPackedBits = kIsIntrinsicLsb + kIsIntrinsicSize; |
| 152 | static_assert(kNumberOfCompiledMethodPackedBits <= CompiledCode::kMaxNumberOfPackedBits, |
| 153 | "Too many packed fields."); |
| 154 | |
| 155 | using IsIntrinsicField = BitField<bool, kIsIntrinsicLsb, kIsIntrinsicSize>; |
| 156 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 157 | // For quick code, holds code infos which contain stack maps, inline information, and etc. |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 158 | const LengthPrefixedArray<uint8_t>* const vmap_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 159 | // For quick code, a FDE entry for the debug_frame section. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 160 | const LengthPrefixedArray<uint8_t>* const cfi_info_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 161 | // For quick code, linker patches needed by the method. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 162 | const LengthPrefixedArray<linker::LinkerPatch>* const patches_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 165 | } // namespace art |
| 166 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 167 | #endif // ART_COMPILER_COMPILED_METHOD_H_ |