blob: 6c123c4eb525f05fb9d6bfe938761417dade7eeb [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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "quick/quick_method_frame_info.h"
David Srbecky79aa6242018-07-12 13:28:42 +000024#include "stack_map.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;
David Srbecky88087562018-06-23 22:05:56 +010034 OatQuickMethodHeader(uint32_t vmap_table_offset,
David Srbecky88087562018-06-23 22:05:56 +010035 uint32_t code_size)
36 : vmap_table_offset_(vmap_table_offset),
David Srbecky88087562018-06-23 22:05:56 +010037 code_size_(code_size) {
David Srbeckye1412da2019-02-13 17:27:17 +000038 DCHECK_NE(vmap_table_offset, 0u);
39 DCHECK_NE(code_size, 0u);
David Srbecky88087562018-06-23 22:05:56 +010040 }
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 {
David Srbeckye1412da2019-02-13 17:27:17 +000063 return (code_size_ & kCodeSizeMask) != 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
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010076 const uint8_t* GetCode() const {
77 return code_;
78 }
79
David Srbecky5d950762016-03-07 20:47:29 +000080 uint32_t GetCodeSize() const {
David Srbeckye1412da2019-02-13 17:27:17 +000081 DCHECK(IsOptimized());
82 size_t code_size1 = code_size_ & kCodeSizeMask;
83 size_t code_size2 = CodeInfo::DecodeCodeSize(GetOptimizedCodeInfoPtr());
84 DCHECK_EQ(code_size1, code_size2);
85 return code_size2;
Mingyao Yang063fc772016-08-02 11:02:54 -070086 }
87
88 const uint32_t* GetCodeSizeAddr() const {
89 return &code_size_;
90 }
91
92 uint32_t GetVmapTableOffset() const {
93 return vmap_table_offset_;
94 }
95
96 void SetVmapTableOffset(uint32_t offset) {
97 vmap_table_offset_ = offset;
98 }
99
100 const uint32_t* GetVmapTableOffsetAddr() const {
101 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000102 }
103
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100104 const uint8_t* GetVmapTable() const {
105 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
106 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
107 }
108
109 bool Contains(uintptr_t pc) const {
110 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000111 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
112 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100113 // On Thumb-2, the pc is offset by one.
114 code_start++;
115 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700116 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100117 }
118
119 const uint8_t* GetEntryPoint() const {
120 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
121 // (not `kThumb2`), *but* we always generate code for the Thumb-2
122 // instruction set anyway. Thumb-2 requires the entrypoint to be of
123 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000124 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
125 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100126 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
127 : code_;
128 }
129
130 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000131 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100132 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100133 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000134 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100135 }
136 return result;
137 }
138
139 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000140 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100141 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100142 }
143
144 uintptr_t ToNativeQuickPc(ArtMethod* method,
145 const uint32_t dex_pc,
146 bool is_for_catch_handler,
147 bool abort_on_failure = true) const;
148
149 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
150
Mingyao Yang063fc772016-08-02 11:02:54 -0700151 void SetHasShouldDeoptimizeFlag() {
152 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
153 code_size_ |= kShouldDeoptimizeMask;
154 }
155
156 bool HasShouldDeoptimizeFlag() const {
157 return (code_size_ & kShouldDeoptimizeMask) != 0;
158 }
159
160 private:
161 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
162 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
163
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100164 // The offset in bytes from the start of the vmap table to the end of the header.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700165 uint32_t vmap_table_offset_ = 0u;
Mingyao Yang063fc772016-08-02 11:02:54 -0700166 // The code size in bytes. The highest bit is used to signify if the compiled
167 // code with the method header has should_deoptimize flag.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700168 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100169 // The actual code.
170 uint8_t code_[0];
171};
172
173} // namespace art
174
175#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_