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: |
| 33 | OatQuickMethodHeader(uint32_t mapping_table_offset = 0U, |
| 34 | uint32_t vmap_table_offset = 0U, |
| 35 | uint32_t gc_map_offset = 0U, |
| 36 | uint32_t frame_size_in_bytes = 0U, |
| 37 | uint32_t core_spill_mask = 0U, |
| 38 | uint32_t fp_spill_mask = 0U, |
| 39 | uint32_t code_size = 0U); |
| 40 | |
| 41 | ~OatQuickMethodHeader(); |
| 42 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 43 | static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) { |
| 44 | uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr); |
| 45 | uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 46 | DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 47 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))) |
| 48 | << std::hex << code << " " << std::hex << header; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 49 | return reinterpret_cast<OatQuickMethodHeader*>(header); |
| 50 | } |
| 51 | |
| 52 | static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) { |
| 53 | return FromCodePointer(EntryPointToCodePointer(entry_point)); |
| 54 | } |
| 55 | |
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 { |
| 63 | return gc_map_offset_ == 0 && vmap_table_offset_ != 0; |
| 64 | } |
| 65 | |
| 66 | CodeInfo GetOptimizedCodeInfo() const { |
| 67 | DCHECK(IsOptimized()); |
| 68 | const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_); |
| 69 | return CodeInfo(data); |
| 70 | } |
| 71 | |
| 72 | const uint8_t* GetCode() const { |
| 73 | return code_; |
| 74 | } |
| 75 | |
| 76 | const uint8_t* GetNativeGcMap() const { |
| 77 | return (gc_map_offset_ == 0) ? nullptr : code_ - gc_map_offset_; |
| 78 | } |
| 79 | |
| 80 | const uint8_t* GetMappingTable() const { |
| 81 | return (mapping_table_offset_ == 0) ? nullptr : code_ - mapping_table_offset_; |
| 82 | } |
| 83 | |
| 84 | const uint8_t* GetVmapTable() const { |
| 85 | CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler"; |
| 86 | return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_; |
| 87 | } |
| 88 | |
| 89 | bool Contains(uintptr_t pc) const { |
| 90 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 91 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 92 | if (kRuntimeISA == kArm) { |
| 93 | // On Thumb-2, the pc is offset by one. |
| 94 | code_start++; |
| 95 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 96 | return code_start <= pc && pc <= (code_start + code_size_); |
| 97 | } |
| 98 | |
| 99 | const uint8_t* GetEntryPoint() const { |
| 100 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 101 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 102 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 103 | // offset 1. |
| 104 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 105 | return (kRuntimeISA == kArm) |
| 106 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 107 | : code_; |
| 108 | } |
| 109 | |
| 110 | template <bool kCheckFrameSize = true> |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 111 | uint32_t GetFrameSizeInBytes() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 112 | uint32_t result = frame_info_.FrameSizeInBytes(); |
| 113 | if (kCheckFrameSize) { |
| 114 | DCHECK_LE(static_cast<size_t>(kStackAlignment), result); |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | QuickMethodFrameInfo GetFrameInfo() const { |
| 120 | return frame_info_; |
| 121 | } |
| 122 | |
| 123 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 124 | const uint32_t dex_pc, |
| 125 | bool is_for_catch_handler, |
| 126 | bool abort_on_failure = true) const; |
| 127 | |
| 128 | uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const; |
| 129 | |
| 130 | // The offset in bytes from the start of the mapping table to the end of the header. |
| 131 | uint32_t mapping_table_offset_; |
| 132 | // The offset in bytes from the start of the vmap table to the end of the header. |
| 133 | uint32_t vmap_table_offset_; |
| 134 | // The offset in bytes from the start of the gc map to the end of the header. |
| 135 | uint32_t gc_map_offset_; |
| 136 | // The stack frame information. |
| 137 | QuickMethodFrameInfo frame_info_; |
| 138 | // The code size in bytes. |
| 139 | uint32_t code_size_; |
| 140 | // The actual code. |
| 141 | uint8_t code_[0]; |
| 142 | }; |
| 143 | |
| 144 | } // namespace art |
| 145 | |
| 146 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |