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