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" |
| 22 | #include "quick/quick_method_frame_info.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 23 | #include "method_info.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 24 | #include "stack_map.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 25 | #include "utils.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class ArtMethod; |
| 30 | |
| 31 | // OatQuickMethodHeader precedes the raw code chunk generated by the compiler. |
| 32 | class PACKED(4) OatQuickMethodHeader { |
| 33 | public: |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 34 | OatQuickMethodHeader() = default; |
| 35 | explicit OatQuickMethodHeader(uint32_t vmap_table_offset, |
| 36 | uint32_t method_info_offset, |
| 37 | uint32_t frame_size_in_bytes, |
| 38 | uint32_t core_spill_mask, |
| 39 | uint32_t fp_spill_mask, |
| 40 | uint32_t code_size); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 41 | |
| 42 | ~OatQuickMethodHeader(); |
| 43 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 44 | static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) { |
| 45 | uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr); |
| 46 | uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 47 | DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 48 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))) |
| 49 | << std::hex << code << " " << std::hex << header; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 50 | return reinterpret_cast<OatQuickMethodHeader*>(header); |
| 51 | } |
| 52 | |
| 53 | static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) { |
| 54 | return FromCodePointer(EntryPointToCodePointer(entry_point)); |
| 55 | } |
| 56 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 57 | OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default; |
| 58 | |
| 59 | uintptr_t NativeQuickPcOffset(const uintptr_t pc) const { |
| 60 | return pc - reinterpret_cast<uintptr_t>(GetEntryPoint()); |
| 61 | } |
| 62 | |
| 63 | bool IsOptimized() const { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 64 | return GetCodeSize() != 0 && vmap_table_offset_ != 0; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 65 | } |
| 66 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 67 | const void* GetOptimizedCodeInfoPtr() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 68 | DCHECK(IsOptimized()); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 69 | return reinterpret_cast<const void*>(code_ - vmap_table_offset_); |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 72 | uint8_t* GetOptimizedCodeInfoPtr() { |
| 73 | DCHECK(IsOptimized()); |
| 74 | return code_ - vmap_table_offset_; |
| 75 | } |
| 76 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 77 | CodeInfo GetOptimizedCodeInfo() const { |
| 78 | return CodeInfo(GetOptimizedCodeInfoPtr()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 81 | const void* GetOptimizedMethodInfoPtr() const { |
| 82 | DCHECK(IsOptimized()); |
| 83 | return reinterpret_cast<const void*>(code_ - method_info_offset_); |
| 84 | } |
| 85 | |
| 86 | uint8_t* GetOptimizedMethodInfoPtr() { |
| 87 | DCHECK(IsOptimized()); |
| 88 | return code_ - method_info_offset_; |
| 89 | } |
| 90 | |
| 91 | MethodInfo GetOptimizedMethodInfo() const { |
| 92 | return MethodInfo(reinterpret_cast<const uint8_t*>(GetOptimizedMethodInfoPtr())); |
| 93 | } |
| 94 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 95 | const uint8_t* GetCode() const { |
| 96 | return code_; |
| 97 | } |
| 98 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 99 | uint32_t GetCodeSize() const { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 100 | return code_size_ & kCodeSizeMask; |
| 101 | } |
| 102 | |
| 103 | const uint32_t* GetCodeSizeAddr() const { |
| 104 | return &code_size_; |
| 105 | } |
| 106 | |
| 107 | uint32_t GetVmapTableOffset() const { |
| 108 | return vmap_table_offset_; |
| 109 | } |
| 110 | |
| 111 | void SetVmapTableOffset(uint32_t offset) { |
| 112 | vmap_table_offset_ = offset; |
| 113 | } |
| 114 | |
| 115 | const uint32_t* GetVmapTableOffsetAddr() const { |
| 116 | return &vmap_table_offset_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 119 | uint32_t GetMethodInfoOffset() const { |
| 120 | return method_info_offset_; |
| 121 | } |
| 122 | |
| 123 | void SetMethodInfoOffset(uint32_t offset) { |
| 124 | method_info_offset_ = offset; |
| 125 | } |
| 126 | |
| 127 | const uint32_t* GetMethodInfoOffsetAddr() const { |
| 128 | return &method_info_offset_; |
| 129 | } |
| 130 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 131 | const uint8_t* GetVmapTable() const { |
| 132 | CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler"; |
| 133 | return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_; |
| 134 | } |
| 135 | |
| 136 | bool Contains(uintptr_t pc) const { |
| 137 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 138 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 139 | if (kRuntimeISA == kArm) { |
| 140 | // On Thumb-2, the pc is offset by one. |
| 141 | code_start++; |
| 142 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 143 | return code_start <= pc && pc <= (code_start + GetCodeSize()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | const uint8_t* GetEntryPoint() const { |
| 147 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 148 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 149 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 150 | // offset 1. |
| 151 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 152 | return (kRuntimeISA == kArm) |
| 153 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 154 | : code_; |
| 155 | } |
| 156 | |
| 157 | template <bool kCheckFrameSize = true> |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 158 | uint32_t GetFrameSizeInBytes() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 159 | uint32_t result = frame_info_.FrameSizeInBytes(); |
| 160 | if (kCheckFrameSize) { |
David Srbecky | 6832fbe | 2016-03-11 18:48:55 +0000 | [diff] [blame] | 161 | DCHECK_ALIGNED(result, kStackAlignment); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 162 | } |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | QuickMethodFrameInfo GetFrameInfo() const { |
| 167 | return frame_info_; |
| 168 | } |
| 169 | |
| 170 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 171 | const uint32_t dex_pc, |
| 172 | bool is_for_catch_handler, |
| 173 | bool abort_on_failure = true) const; |
| 174 | |
| 175 | uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const; |
| 176 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 177 | void SetHasShouldDeoptimizeFlag() { |
| 178 | DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u); |
| 179 | code_size_ |= kShouldDeoptimizeMask; |
| 180 | } |
| 181 | |
| 182 | bool HasShouldDeoptimizeFlag() const { |
| 183 | return (code_size_ & kShouldDeoptimizeMask) != 0; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000; |
| 188 | static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask; |
| 189 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 190 | // 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^] | 191 | uint32_t vmap_table_offset_ = 0u; |
| 192 | // The offset in bytes from the start of the method info to the end of the header. |
| 193 | // The method info offset is not in the CodeInfo since CodeInfo has good dedupe properties that |
| 194 | // would be lost from doing so. The method info memory region contains method indices since they |
| 195 | // are hard to dedupe. |
| 196 | |
| 197 | uint32_t method_info_offset_ = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 198 | // The stack frame information. |
| 199 | QuickMethodFrameInfo frame_info_; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 200 | // The code size in bytes. The highest bit is used to signify if the compiled |
| 201 | // code with the method header has should_deoptimize flag. |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame^] | 202 | uint32_t code_size_ = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 203 | // The actual code. |
| 204 | uint8_t code_[0]; |
| 205 | }; |
| 206 | |
| 207 | } // namespace art |
| 208 | |
| 209 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |