blob: e41c7eef68a4270f8aa897e55fe41e992c7a4c83 [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;
Nicolas Geoffray57083762019-03-05 09:24:45 +000034 OatQuickMethodHeader(uint32_t vmap_table_offset,
35 uint32_t code_size)
36 : vmap_table_offset_(vmap_table_offset),
37 code_size_(code_size) {
David Srbecky88087562018-06-23 22:05:56 +010038 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010040 static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
41 uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
42 uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
Vladimir Marko562ff442015-10-27 18:51:20 +000043 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080044 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
45 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010046 return reinterpret_cast<OatQuickMethodHeader*>(header);
47 }
48
49 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
50 return FromCodePointer(EntryPointToCodePointer(entry_point));
51 }
52
Yi Kong2665bc82017-05-09 15:55:57 -070053 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010054 OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
55
56 uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
57 return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
58 }
59
60 bool IsOptimized() const {
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000061 return GetCodeSize() != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010062 }
63
David Srbecky79aa6242018-07-12 13:28:42 +000064 const uint8_t* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000065 DCHECK(IsOptimized());
66 return code_ - vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000067 }
68
Nicolas Geoffray132d8362016-11-16 09:19:42 +000069 uint8_t* GetOptimizedCodeInfoPtr() {
Nicolas Geoffray57083762019-03-05 09:24:45 +000070 DCHECK(IsOptimized());
71 return code_ - vmap_table_offset_;
Nicolas Geoffray132d8362016-11-16 09:19:42 +000072 }
73
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010074 const uint8_t* GetCode() const {
75 return code_;
76 }
77
David Srbecky5d950762016-03-07 20:47:29 +000078 uint32_t GetCodeSize() const {
David Srbecky1eb5d872019-04-03 13:56:22 +010079 // ART compiled method are prefixed with header, but we can also easily
80 // accidentally use a function pointer to one of the stubs/trampolines.
81 // We prefix those with 0xFF in the aseembly so that we can do DCHECKs.
82 CHECK_NE(code_size_, 0xFFFFFFFF) << code_;
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000083 return code_size_ & kCodeSizeMask;
Nicolas Geoffray57083762019-03-05 09:24:45 +000084 }
85
86 const uint32_t* GetCodeSizeAddr() const {
87 return &code_size_;
Mingyao Yang063fc772016-08-02 11:02:54 -070088 }
89
90 uint32_t GetVmapTableOffset() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000091 return vmap_table_offset_;
Mingyao Yang063fc772016-08-02 11:02:54 -070092 }
93
94 void SetVmapTableOffset(uint32_t offset) {
Nicolas Geoffray57083762019-03-05 09:24:45 +000095 vmap_table_offset_ = offset;
Mingyao Yang063fc772016-08-02 11:02:54 -070096 }
97
98 const uint32_t* GetVmapTableOffsetAddr() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +000099 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +0000100 }
101
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100102 const uint8_t* GetVmapTable() const {
103 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
Nicolas Geoffray57083762019-03-05 09:24:45 +0000104 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100105 }
106
107 bool Contains(uintptr_t pc) const {
108 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000109 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
110 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100111 // On Thumb-2, the pc is offset by one.
112 code_start++;
113 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700114 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100115 }
116
117 const uint8_t* GetEntryPoint() const {
118 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
119 // (not `kThumb2`), *but* we always generate code for the Thumb-2
120 // instruction set anyway. Thumb-2 requires the entrypoint to be of
121 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000122 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
123 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100124 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
125 : code_;
126 }
127
128 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000129 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100130 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100131 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000132 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100133 }
134 return result;
135 }
136
137 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000138 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100139 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100140 }
141
142 uintptr_t ToNativeQuickPc(ArtMethod* method,
143 const uint32_t dex_pc,
144 bool is_for_catch_handler,
145 bool abort_on_failure = true) const;
146
147 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
148
Mingyao Yang063fc772016-08-02 11:02:54 -0700149 void SetHasShouldDeoptimizeFlag() {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000150 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
151 code_size_ |= kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700152 }
153
154 bool HasShouldDeoptimizeFlag() const {
Nicolas Geoffray57083762019-03-05 09:24:45 +0000155 return (code_size_ & kShouldDeoptimizeMask) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700156 }
157
158 private:
159 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
Nicolas Geoffray57083762019-03-05 09:24:45 +0000160 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700161
Nicolas Geoffray57083762019-03-05 09:24:45 +0000162 // The offset in bytes from the start of the vmap table to the end of the header.
163 uint32_t vmap_table_offset_ = 0u;
164 // The code size in bytes. The highest bit is used to signify if the compiled
165 // code with the method header has should_deoptimize flag.
166 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100167 // The actual code.
168 uint8_t code_[0];
169};
170
171} // namespace art
172
173#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_