blob: 3d5be3650bcd4b3cc77fdfcd990847a84e9c1c1f [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"
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:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070033 OatQuickMethodHeader() = default;
34 explicit OatQuickMethodHeader(uint32_t vmap_table_offset,
35 uint32_t method_info_offset,
36 uint32_t frame_size_in_bytes,
37 uint32_t core_spill_mask,
38 uint32_t fp_spill_mask,
39 uint32_t code_size);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010040
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010041 static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
42 uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
43 uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
Vladimir Marko562ff442015-10-27 18:51:20 +000044 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080045 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
46 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010047 return reinterpret_cast<OatQuickMethodHeader*>(header);
48 }
49
50 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
51 return FromCodePointer(EntryPointToCodePointer(entry_point));
52 }
53
Yi Kong2665bc82017-05-09 15:55:57 -070054 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010055 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 {
Mingyao Yang063fc772016-08-02 11:02:54 -070062 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010063 }
64
Yohei Yukawacba94fa2018-07-05 01:35:44 +000065 const void* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010066 DCHECK(IsOptimized());
Yohei Yukawacba94fa2018-07-05 01:35:44 +000067 return reinterpret_cast<const void*>(code_ - vmap_table_offset_);
David Srbecky5d950762016-03-07 20:47:29 +000068 }
69
Nicolas Geoffray132d8362016-11-16 09:19:42 +000070 uint8_t* GetOptimizedCodeInfoPtr() {
71 DCHECK(IsOptimized());
72 return code_ - vmap_table_offset_;
73 }
74
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070075 const void* GetOptimizedMethodInfoPtr() const {
76 DCHECK(IsOptimized());
77 return reinterpret_cast<const void*>(code_ - method_info_offset_);
78 }
79
80 uint8_t* GetOptimizedMethodInfoPtr() {
81 DCHECK(IsOptimized());
82 return code_ - method_info_offset_;
83 }
84
85 MethodInfo GetOptimizedMethodInfo() const {
86 return MethodInfo(reinterpret_cast<const uint8_t*>(GetOptimizedMethodInfoPtr()));
87 }
88
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010089 const uint8_t* GetCode() const {
90 return code_;
91 }
92
David Srbecky5d950762016-03-07 20:47:29 +000093 uint32_t GetCodeSize() const {
Mingyao Yang063fc772016-08-02 11:02:54 -070094 return code_size_ & kCodeSizeMask;
95 }
96
97 const uint32_t* GetCodeSizeAddr() const {
98 return &code_size_;
99 }
100
101 uint32_t GetVmapTableOffset() const {
102 return vmap_table_offset_;
103 }
104
105 void SetVmapTableOffset(uint32_t offset) {
106 vmap_table_offset_ = offset;
107 }
108
109 const uint32_t* GetVmapTableOffsetAddr() const {
110 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000111 }
112
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700113 uint32_t GetMethodInfoOffset() const {
114 return method_info_offset_;
115 }
116
117 void SetMethodInfoOffset(uint32_t offset) {
118 method_info_offset_ = offset;
119 }
120
121 const uint32_t* GetMethodInfoOffsetAddr() const {
122 return &method_info_offset_;
123 }
124
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100125 const uint8_t* GetVmapTable() const {
126 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
127 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
128 }
129
130 bool Contains(uintptr_t pc) const {
131 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000132 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
133 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100134 // On Thumb-2, the pc is offset by one.
135 code_start++;
136 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700137 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100138 }
139
140 const uint8_t* GetEntryPoint() const {
141 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
142 // (not `kThumb2`), *but* we always generate code for the Thumb-2
143 // instruction set anyway. Thumb-2 requires the entrypoint to be of
144 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000145 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
146 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100147 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
148 : code_;
149 }
150
151 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000152 uint32_t GetFrameSizeInBytes() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100153 uint32_t result = frame_info_.FrameSizeInBytes();
154 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000155 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100156 }
157 return result;
158 }
159
160 QuickMethodFrameInfo GetFrameInfo() const {
Yohei Yukawacba94fa2018-07-05 01:35:44 +0000161 return frame_info_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100162 }
163
164 uintptr_t ToNativeQuickPc(ArtMethod* method,
165 const uint32_t dex_pc,
166 bool is_for_catch_handler,
167 bool abort_on_failure = true) const;
168
169 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
170
Mingyao Yang063fc772016-08-02 11:02:54 -0700171 void SetHasShouldDeoptimizeFlag() {
172 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
173 code_size_ |= kShouldDeoptimizeMask;
174 }
175
176 bool HasShouldDeoptimizeFlag() const {
177 return (code_size_ & kShouldDeoptimizeMask) != 0;
178 }
179
180 private:
181 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
182 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
183
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100184 // The offset in bytes from the start of the vmap table to the end of the header.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700185 uint32_t vmap_table_offset_ = 0u;
186 // The offset in bytes from the start of the method info to the end of the header.
187 // The method info offset is not in the CodeInfo since CodeInfo has good dedupe properties that
188 // would be lost from doing so. The method info memory region contains method indices since they
189 // are hard to dedupe.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700190 uint32_t method_info_offset_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100191 // The stack frame information.
192 QuickMethodFrameInfo frame_info_;
Mingyao Yang063fc772016-08-02 11:02:54 -0700193 // The code size in bytes. The highest bit is used to signify if the compiled
194 // code with the method header has should_deoptimize flag.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700195 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100196 // The actual code.
197 uint8_t code_[0];
198};
199
200} // namespace art
201
202#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_