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)) || |
| 47 | IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA))); |
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 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 55 | OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default; |
| 56 | |
| 57 | uintptr_t NativeQuickPcOffset(const uintptr_t pc) const { |
| 58 | return pc - reinterpret_cast<uintptr_t>(GetEntryPoint()); |
| 59 | } |
| 60 | |
| 61 | bool IsOptimized() const { |
| 62 | return gc_map_offset_ == 0 && vmap_table_offset_ != 0; |
| 63 | } |
| 64 | |
| 65 | CodeInfo GetOptimizedCodeInfo() const { |
| 66 | DCHECK(IsOptimized()); |
| 67 | const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_); |
| 68 | return CodeInfo(data); |
| 69 | } |
| 70 | |
| 71 | const uint8_t* GetCode() const { |
| 72 | return code_; |
| 73 | } |
| 74 | |
| 75 | const uint8_t* GetNativeGcMap() const { |
| 76 | return (gc_map_offset_ == 0) ? nullptr : code_ - gc_map_offset_; |
| 77 | } |
| 78 | |
| 79 | const uint8_t* GetMappingTable() const { |
| 80 | return (mapping_table_offset_ == 0) ? nullptr : code_ - mapping_table_offset_; |
| 81 | } |
| 82 | |
| 83 | const uint8_t* GetVmapTable() const { |
| 84 | CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler"; |
| 85 | return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_; |
| 86 | } |
| 87 | |
| 88 | bool Contains(uintptr_t pc) const { |
| 89 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 90 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 91 | if (kRuntimeISA == kArm) { |
| 92 | // On Thumb-2, the pc is offset by one. |
| 93 | code_start++; |
| 94 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 95 | return code_start <= pc && pc <= (code_start + code_size_); |
| 96 | } |
| 97 | |
| 98 | const uint8_t* GetEntryPoint() const { |
| 99 | // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm` |
| 100 | // (not `kThumb2`), *but* we always generate code for the Thumb-2 |
| 101 | // instruction set anyway. Thumb-2 requires the entrypoint to be of |
| 102 | // offset 1. |
| 103 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 104 | return (kRuntimeISA == kArm) |
| 105 | ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1) |
| 106 | : code_; |
| 107 | } |
| 108 | |
| 109 | template <bool kCheckFrameSize = true> |
| 110 | uint32_t GetFrameSizeInBytes() { |
| 111 | uint32_t result = frame_info_.FrameSizeInBytes(); |
| 112 | if (kCheckFrameSize) { |
| 113 | DCHECK_LE(static_cast<size_t>(kStackAlignment), result); |
| 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | QuickMethodFrameInfo GetFrameInfo() const { |
| 119 | return frame_info_; |
| 120 | } |
| 121 | |
| 122 | uintptr_t ToNativeQuickPc(ArtMethod* method, |
| 123 | const uint32_t dex_pc, |
| 124 | bool is_for_catch_handler, |
| 125 | bool abort_on_failure = true) const; |
| 126 | |
| 127 | uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const; |
| 128 | |
| 129 | // The offset in bytes from the start of the mapping table to the end of the header. |
| 130 | uint32_t mapping_table_offset_; |
| 131 | // The offset in bytes from the start of the vmap table to the end of the header. |
| 132 | uint32_t vmap_table_offset_; |
| 133 | // The offset in bytes from the start of the gc map to the end of the header. |
| 134 | uint32_t gc_map_offset_; |
| 135 | // The stack frame information. |
| 136 | QuickMethodFrameInfo frame_info_; |
| 137 | // The code size in bytes. |
| 138 | uint32_t code_size_; |
| 139 | // The actual code. |
| 140 | uint8_t code_[0]; |
| 141 | }; |
| 142 | |
| 143 | } // namespace art |
| 144 | |
| 145 | #endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_ |