Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [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 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |
| 18 | #define ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |
| 19 | |
| 20 | #include "arch/instruction_set.h" |
| 21 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "quick/quick_method_frame_info.h" |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 24 | #include "stack_map.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class ArtMethod; |
| 29 | |
| 30 | // OatQuickMethodHeader precedes the raw code chunk generated by the compiler. |
| 31 | class PACKED(4) OatQuickMethodHeader { |
| 32 | public: |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 33 | OatQuickMethodHeader() = default; |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 34 | OatQuickMethodHeader(uint32_t vmap_table_offset, |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 35 | uint32_t code_size) |
| 36 | : vmap_table_offset_(vmap_table_offset), |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 37 | code_size_(code_size) { |
David Srbecky | e1412da | 2019-02-13 17:27:17 +0000 | [diff] [blame] | 38 | DCHECK_NE(vmap_table_offset, 0u); |
| 39 | DCHECK_NE(code_size, 0u); |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 40 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 41 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 42 | static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) { |
| 43 | uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr); |
| 44 | uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 45 | DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 46 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))) |
| 47 | << std::hex << code << " " << std::hex << header; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 48 | return reinterpret_cast<OatQuickMethodHeader*>(header); |
| 49 | } |
| 50 | |
| 51 | static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) { |
| 52 | return FromCodePointer(EntryPointToCodePointer(entry_point)); |
| 53 | } |
| 54 | |
Yi Kong | 2665bc8 | 2017-05-09 15:55:57 -0700 | [diff] [blame] | 55 | OatQuickMethodHeader(const OatQuickMethodHeader&) = default; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 56 | OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default; |
| 57 | |
| 58 | uintptr_t NativeQuickPcOffset(const uintptr_t pc) const { |
| 59 | return pc - reinterpret_cast<uintptr_t>(GetEntryPoint()); |
| 60 | } |
| 61 | |
| 62 | bool IsOptimized() const { |
David Srbecky | e1412da | 2019-02-13 17:27:17 +0000 | [diff] [blame] | 63 | return (code_size_ & kCodeSizeMask) != 0 && vmap_table_offset_ != 0; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 64 | } |
| 65 | |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 66 | const uint8_t* GetOptimizedCodeInfoPtr() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 67 | DCHECK(IsOptimized()); |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 68 | return code_ - vmap_table_offset_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 71 | uint8_t* GetOptimizedCodeInfoPtr() { |
| 72 | DCHECK(IsOptimized()); |
| 73 | return code_ - vmap_table_offset_; |
| 74 | } |
| 75 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 76 | const uint8_t* GetCode() const { |
| 77 | return code_; |
| 78 | } |
| 79 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 80 | uint32_t GetCodeSize() const { |
David Srbecky | e1412da | 2019-02-13 17:27:17 +0000 | [diff] [blame] | 81 | DCHECK(IsOptimized()); |
| 82 | size_t code_size1 = code_size_ & kCodeSizeMask; |
| 83 | size_t code_size2 = CodeInfo::DecodeCodeSize(GetOptimizedCodeInfoPtr()); |
| 84 | DCHECK_EQ(code_size1, code_size2); |
| 85 | return code_size2; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | const uint32_t* GetCodeSizeAddr() const { |
| 89 | return &code_size_; |
| 90 | } |
| 91 | |
| 92 | uint32_t GetVmapTableOffset() const { |
| 93 | return vmap_table_offset_; |
| 94 | } |
| 95 | |
| 96 | void SetVmapTableOffset(uint32_t offset) { |
| 97 | vmap_table_offset_ = offset; |
| 98 | } |
| 99 | |
| 100 | const uint32_t* GetVmapTableOffsetAddr() const { |
| 101 | return &vmap_table_offset_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 104 | const uint8_t* GetVmapTable() const { |
| 105 | CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler"; |
| 106 | return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_; |
| 107 | } |
| 108 | |
| 109 | bool Contains(uintptr_t pc) const { |
| 110 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code_); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 111 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 112 | if (kRuntimeISA == InstructionSet::kArm) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 113 | // On Thumb-2, the pc is offset by one. |
| 114 | code_start++; |
| 115 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 116 | return code_start <= pc && pc <= (code_start + GetCodeSize()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | const uint8_t* GetEntryPoint() const { |
| 120 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 121 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 122 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 123 | // offset 1. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 124 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 125 | return (kRuntimeISA == InstructionSet::kArm) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 126 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 127 | : code_; |
| 128 | } |
| 129 | |
| 130 | template <bool kCheckFrameSize = true> |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 131 | uint32_t GetFrameSizeInBytes() const { |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 132 | uint32_t result = GetFrameInfo().FrameSizeInBytes(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 133 | if (kCheckFrameSize) { |
David Srbecky | 6832fbe | 2016-03-11 18:48:55 +0000 | [diff] [blame] | 134 | DCHECK_ALIGNED(result, kStackAlignment); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | QuickMethodFrameInfo GetFrameInfo() const { |
David Srbecky | 79aa624 | 2018-07-12 13:28:42 +0000 | [diff] [blame] | 140 | DCHECK(IsOptimized()); |
David Srbecky | 8808756 | 2018-06-23 22:05:56 +0100 | [diff] [blame] | 141 | return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 145 | const uint32_t dex_pc, |
| 146 | bool is_for_catch_handler, |
| 147 | bool abort_on_failure = true) const; |
| 148 | |
| 149 | uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const; |
| 150 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 151 | void SetHasShouldDeoptimizeFlag() { |
| 152 | DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u); |
| 153 | code_size_ |= kShouldDeoptimizeMask; |
| 154 | } |
| 155 | |
| 156 | bool HasShouldDeoptimizeFlag() const { |
| 157 | return (code_size_ & kShouldDeoptimizeMask) != 0; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000; |
| 162 | static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask; |
| 163 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 164 | // The offset in bytes from the start of the vmap table to the end of the header. |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 165 | uint32_t vmap_table_offset_ = 0u; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 166 | // The code size in bytes. The highest bit is used to signify if the compiled |
| 167 | // code with the method header has should_deoptimize flag. |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 168 | uint32_t code_size_ = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 169 | // The actual code. |
| 170 | uint8_t code_[0]; |
| 171 | }; |
| 172 | |
| 173 | } // namespace art |
| 174 | |
| 175 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |