blob: 1e4ca3e450cede6a76645ded9ed4296366edc0c2 [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;
35 explicit OatQuickMethodHeader(uint32_t vmap_table_offset,
36 uint32_t method_info_offset,
37 uint32_t frame_size_in_bytes,
38 uint32_t core_spill_mask,
39 uint32_t fp_spill_mask,
40 uint32_t code_size);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010041
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010042 static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
43 uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
44 uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
Vladimir Marko562ff442015-10-27 18:51:20 +000045 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080046 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
47 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010048 return reinterpret_cast<OatQuickMethodHeader*>(header);
49 }
50
51 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
52 return FromCodePointer(EntryPointToCodePointer(entry_point));
53 }
54
Yi Kong2665bc82017-05-09 15:55:57 -070055 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
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 {
Mingyao Yang063fc772016-08-02 11:02:54 -070063 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010064 }
65
David Srbecky79aa6242018-07-12 13:28:42 +000066 const uint8_t* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010067 DCHECK(IsOptimized());
David Srbecky79aa6242018-07-12 13:28:42 +000068 return code_ - vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000069 }
70
Nicolas Geoffray132d8362016-11-16 09:19:42 +000071 uint8_t* GetOptimizedCodeInfoPtr() {
72 DCHECK(IsOptimized());
73 return code_ - vmap_table_offset_;
74 }
75
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070076 const void* GetOptimizedMethodInfoPtr() const {
77 DCHECK(IsOptimized());
78 return reinterpret_cast<const void*>(code_ - method_info_offset_);
79 }
80
81 uint8_t* GetOptimizedMethodInfoPtr() {
82 DCHECK(IsOptimized());
83 return code_ - method_info_offset_;
84 }
85
86 MethodInfo GetOptimizedMethodInfo() const {
87 return MethodInfo(reinterpret_cast<const uint8_t*>(GetOptimizedMethodInfoPtr()));
88 }
89
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010090 const uint8_t* GetCode() const {
91 return code_;
92 }
93
David Srbecky5d950762016-03-07 20:47:29 +000094 uint32_t GetCodeSize() const {
Mingyao Yang063fc772016-08-02 11:02:54 -070095 return code_size_ & kCodeSizeMask;
96 }
97
98 const uint32_t* GetCodeSizeAddr() const {
99 return &code_size_;
100 }
101
102 uint32_t GetVmapTableOffset() const {
103 return vmap_table_offset_;
104 }
105
106 void SetVmapTableOffset(uint32_t offset) {
107 vmap_table_offset_ = offset;
108 }
109
110 const uint32_t* GetVmapTableOffsetAddr() const {
111 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000112 }
113
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700114 uint32_t GetMethodInfoOffset() const {
115 return method_info_offset_;
116 }
117
118 void SetMethodInfoOffset(uint32_t offset) {
119 method_info_offset_ = offset;
120 }
121
122 const uint32_t* GetMethodInfoOffsetAddr() const {
123 return &method_info_offset_;
124 }
125
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100126 const uint8_t* GetVmapTable() const {
127 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
128 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
129 }
130
131 bool Contains(uintptr_t pc) const {
132 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000133 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
134 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100135 // On Thumb-2, the pc is offset by one.
136 code_start++;
137 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700138 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100139 }
140
141 const uint8_t* GetEntryPoint() const {
142 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
143 // (not `kThumb2`), *but* we always generate code for the Thumb-2
144 // instruction set anyway. Thumb-2 requires the entrypoint to be of
145 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000146 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
147 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100148 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
149 : code_;
150 }
151
152 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000153 uint32_t GetFrameSizeInBytes() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100154 uint32_t result = frame_info_.FrameSizeInBytes();
155 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000156 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100157 }
158 return result;
159 }
160
161 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000162 DCHECK(IsOptimized());
163 QuickMethodFrameInfo frame_info = CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
164 DCHECK_EQ(frame_info.FrameSizeInBytes(), frame_info_.FrameSizeInBytes());
165 DCHECK_EQ(frame_info.CoreSpillMask(), frame_info_.CoreSpillMask());
166 DCHECK_EQ(frame_info.FpSpillMask(), frame_info_.FpSpillMask());
167 return frame_info;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100168 }
169
170 uintptr_t ToNativeQuickPc(ArtMethod* method,
171 const uint32_t dex_pc,
172 bool is_for_catch_handler,
173 bool abort_on_failure = true) const;
174
175 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
176
Mingyao Yang063fc772016-08-02 11:02:54 -0700177 void SetHasShouldDeoptimizeFlag() {
178 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
179 code_size_ |= kShouldDeoptimizeMask;
180 }
181
182 bool HasShouldDeoptimizeFlag() const {
183 return (code_size_ & kShouldDeoptimizeMask) != 0;
184 }
185
186 private:
187 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
188 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
189
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100190 // The offset in bytes from the start of the vmap table to the end of the header.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700191 uint32_t vmap_table_offset_ = 0u;
192 // The offset in bytes from the start of the method info to the end of the header.
193 // The method info offset is not in the CodeInfo since CodeInfo has good dedupe properties that
194 // would be lost from doing so. The method info memory region contains method indices since they
195 // are hard to dedupe.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700196 uint32_t method_info_offset_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100197 // The stack frame information.
198 QuickMethodFrameInfo frame_info_;
Mingyao Yang063fc772016-08-02 11:02:54 -0700199 // The code size in bytes. The highest bit is used to signify if the compiled
200 // code with the method header has should_deoptimize flag.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700201 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100202 // The actual code.
203 uint8_t code_[0];
204};
205
206} // namespace art
207
208#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_