blob: 3b9f466220e1574c8bd0e5d1ca0d26223b6de9e4 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/utils.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070023#include "method_info.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "quick/quick_method_frame_info.h"
David Srbecky79aa6242018-07-12 13:28:42 +000025#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026
27namespace art {
28
29class ArtMethod;
30
31// OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
32class PACKED(4) OatQuickMethodHeader {
33 public:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070034 OatQuickMethodHeader() = default;
David Srbecky88087562018-06-23 22:05:56 +010035 OatQuickMethodHeader(uint32_t vmap_table_offset,
36 uint32_t method_info_offset,
37 uint32_t code_size)
38 : vmap_table_offset_(vmap_table_offset),
39 method_info_offset_(method_info_offset),
40 code_size_(code_size) {
41 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042
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
Yi Kong2665bc82017-05-09 15:55:57 -070056 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010057 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 Yang063fc772016-08-02 11:02:54 -070064 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010065 }
66
David Srbecky79aa6242018-07-12 13:28:42 +000067 const uint8_t* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010068 DCHECK(IsOptimized());
David Srbecky79aa6242018-07-12 13:28:42 +000069 return code_ - vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000070 }
71
Nicolas Geoffray132d8362016-11-16 09:19:42 +000072 uint8_t* GetOptimizedCodeInfoPtr() {
73 DCHECK(IsOptimized());
74 return code_ - vmap_table_offset_;
75 }
76
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070077 const void* GetOptimizedMethodInfoPtr() const {
78 DCHECK(IsOptimized());
79 return reinterpret_cast<const void*>(code_ - method_info_offset_);
80 }
81
82 uint8_t* GetOptimizedMethodInfoPtr() {
83 DCHECK(IsOptimized());
84 return code_ - method_info_offset_;
85 }
86
87 MethodInfo GetOptimizedMethodInfo() const {
88 return MethodInfo(reinterpret_cast<const uint8_t*>(GetOptimizedMethodInfoPtr()));
89 }
90
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010091 const uint8_t* GetCode() const {
92 return code_;
93 }
94
David Srbecky5d950762016-03-07 20:47:29 +000095 uint32_t GetCodeSize() const {
Mingyao Yang063fc772016-08-02 11:02:54 -070096 return code_size_ & kCodeSizeMask;
97 }
98
99 const uint32_t* GetCodeSizeAddr() const {
100 return &code_size_;
101 }
102
103 uint32_t GetVmapTableOffset() const {
104 return vmap_table_offset_;
105 }
106
107 void SetVmapTableOffset(uint32_t offset) {
108 vmap_table_offset_ = offset;
109 }
110
111 const uint32_t* GetVmapTableOffsetAddr() const {
112 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000113 }
114
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700115 uint32_t GetMethodInfoOffset() const {
116 return method_info_offset_;
117 }
118
119 void SetMethodInfoOffset(uint32_t offset) {
120 method_info_offset_ = offset;
121 }
122
123 const uint32_t* GetMethodInfoOffsetAddr() const {
124 return &method_info_offset_;
125 }
126
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100127 const uint8_t* GetVmapTable() const {
128 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
129 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
130 }
131
132 bool Contains(uintptr_t pc) const {
133 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000134 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
135 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100136 // On Thumb-2, the pc is offset by one.
137 code_start++;
138 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700139 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100140 }
141
142 const uint8_t* GetEntryPoint() const {
143 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
144 // (not `kThumb2`), *but* we always generate code for the Thumb-2
145 // instruction set anyway. Thumb-2 requires the entrypoint to be of
146 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000147 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
148 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100149 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
150 : code_;
151 }
152
153 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000154 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100155 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100156 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000157 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100158 }
159 return result;
160 }
161
162 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000163 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100164 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100165 }
166
167 uintptr_t ToNativeQuickPc(ArtMethod* method,
168 const uint32_t dex_pc,
169 bool is_for_catch_handler,
170 bool abort_on_failure = true) const;
171
172 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
173
Mingyao Yang063fc772016-08-02 11:02:54 -0700174 void SetHasShouldDeoptimizeFlag() {
175 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
176 code_size_ |= kShouldDeoptimizeMask;
177 }
178
179 bool HasShouldDeoptimizeFlag() const {
180 return (code_size_ & kShouldDeoptimizeMask) != 0;
181 }
182
183 private:
184 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
185 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
186
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100187 // The offset in bytes from the start of the vmap table to the end of the header.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700188 uint32_t vmap_table_offset_ = 0u;
189 // The offset in bytes from the start of the method info to the end of the header.
190 // The method info offset is not in the CodeInfo since CodeInfo has good dedupe properties that
191 // would be lost from doing so. The method info memory region contains method indices since they
192 // are hard to dedupe.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700193 uint32_t method_info_offset_ = 0u;
Mingyao Yang063fc772016-08-02 11:02:54 -0700194 // The code size in bytes. The highest bit is used to signify if the compiled
195 // code with the method header has should_deoptimize flag.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700196 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100197 // The actual code.
198 uint8_t code_[0];
199};
200
201} // namespace art
202
203#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_