blob: 59c37e4915d206f6eb396b3fea640cb0b88f886c [file] [log] [blame]
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001/*
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 Geoffray1dad3f62015-10-23 14:59:54 +010024#include "utils.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010025
26namespace art {
27
28class ArtMethod;
29
30// OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
31class 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 Geoffray1dad3f62015-10-23 14:59:54 +010043 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 Marko562ff442015-10-27 18:51:20 +000046 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080047 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
48 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010049 return reinterpret_cast<OatQuickMethodHeader*>(header);
50 }
51
52 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
53 return FromCodePointer(EntryPointToCodePointer(entry_point));
54 }
55
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010056 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
David Srbecky5d950762016-03-07 20:47:29 +000066 const void* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010067 DCHECK(IsOptimized());
68 const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_);
David Srbecky5d950762016-03-07 20:47:29 +000069 return data;
70 }
71
72 CodeInfo GetOptimizedCodeInfo() const {
73 return CodeInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010074 }
75
76 const uint8_t* GetCode() const {
77 return code_;
78 }
79
David Srbecky5d950762016-03-07 20:47:29 +000080 uint32_t GetCodeSize() const {
81 return code_size_;
82 }
83
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010084 const uint8_t* GetNativeGcMap() const {
85 return (gc_map_offset_ == 0) ? nullptr : code_ - gc_map_offset_;
86 }
87
88 const uint8_t* GetMappingTable() const {
89 return (mapping_table_offset_ == 0) ? nullptr : code_ - mapping_table_offset_;
90 }
91
92 const uint8_t* GetVmapTable() const {
93 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
94 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
95 }
96
97 bool Contains(uintptr_t pc) const {
98 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010099 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
100 if (kRuntimeISA == kArm) {
101 // On Thumb-2, the pc is offset by one.
102 code_start++;
103 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100104 return code_start <= pc && pc <= (code_start + code_size_);
105 }
106
107 const uint8_t* GetEntryPoint() const {
108 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
109 // (not `kThumb2`), *but* we always generate code for the Thumb-2
110 // instruction set anyway. Thumb-2 requires the entrypoint to be of
111 // offset 1.
112 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
113 return (kRuntimeISA == kArm)
114 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
115 : code_;
116 }
117
118 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000119 uint32_t GetFrameSizeInBytes() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100120 uint32_t result = frame_info_.FrameSizeInBytes();
121 if (kCheckFrameSize) {
122 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
123 }
124 return result;
125 }
126
127 QuickMethodFrameInfo GetFrameInfo() const {
128 return frame_info_;
129 }
130
131 uintptr_t ToNativeQuickPc(ArtMethod* method,
132 const uint32_t dex_pc,
133 bool is_for_catch_handler,
134 bool abort_on_failure = true) const;
135
136 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
137
138 // The offset in bytes from the start of the mapping table to the end of the header.
139 uint32_t mapping_table_offset_;
140 // The offset in bytes from the start of the vmap table to the end of the header.
141 uint32_t vmap_table_offset_;
142 // The offset in bytes from the start of the gc map to the end of the header.
143 uint32_t gc_map_offset_;
144 // The stack frame information.
145 QuickMethodFrameInfo frame_info_;
146 // The code size in bytes.
147 uint32_t code_size_;
148 // The actual code.
149 uint8_t code_[0];
150};
151
152} // namespace art
153
154#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_