blob: 8798c6968cc787b842e6157060a8743825e348cf [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) {
38 }
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 {
Mingyao Yang063fc772016-08-02 11:02:54 -070061 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 Geoffray524e7ea2015-10-16 17:13:34 +010065 DCHECK(IsOptimized());
David Srbecky79aa6242018-07-12 13:28:42 +000066 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() {
70 DCHECK(IsOptimized());
71 return code_ - vmap_table_offset_;
72 }
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 {
Mingyao Yang063fc772016-08-02 11:02:54 -070079 return code_size_ & kCodeSizeMask;
80 }
81
82 const uint32_t* GetCodeSizeAddr() const {
83 return &code_size_;
84 }
85
86 uint32_t GetVmapTableOffset() const {
87 return vmap_table_offset_;
88 }
89
90 void SetVmapTableOffset(uint32_t offset) {
91 vmap_table_offset_ = offset;
92 }
93
94 const uint32_t* GetVmapTableOffsetAddr() const {
95 return &vmap_table_offset_;
David Srbecky5d950762016-03-07 20:47:29 +000096 }
97
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010098 const uint8_t* GetVmapTable() const {
99 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
100 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
101 }
102
103 bool Contains(uintptr_t pc) const {
104 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Vladimir Marko33bff252017-11-01 14:35:42 +0000105 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
106 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100107 // On Thumb-2, the pc is offset by one.
108 code_start++;
109 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700110 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100111 }
112
113 const uint8_t* GetEntryPoint() const {
114 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
115 // (not `kThumb2`), *but* we always generate code for the Thumb-2
116 // instruction set anyway. Thumb-2 requires the entrypoint to be of
117 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000118 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
119 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100120 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
121 : code_;
122 }
123
124 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000125 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100126 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100127 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000128 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100129 }
130 return result;
131 }
132
133 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000134 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100135 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100136 }
137
138 uintptr_t ToNativeQuickPc(ArtMethod* method,
139 const uint32_t dex_pc,
140 bool is_for_catch_handler,
141 bool abort_on_failure = true) const;
142
143 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
144
Mingyao Yang063fc772016-08-02 11:02:54 -0700145 void SetHasShouldDeoptimizeFlag() {
146 DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
147 code_size_ |= kShouldDeoptimizeMask;
148 }
149
150 bool HasShouldDeoptimizeFlag() const {
151 return (code_size_ & kShouldDeoptimizeMask) != 0;
152 }
153
154 private:
155 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
156 static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
157
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100158 // The offset in bytes from the start of the vmap table to the end of the header.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700159 uint32_t vmap_table_offset_ = 0u;
Mingyao Yang063fc772016-08-02 11:02:54 -0700160 // The code size in bytes. The highest bit is used to signify if the compiled
161 // code with the method header has should_deoptimize flag.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700162 uint32_t code_size_ = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100163 // The actual code.
164 uint8_t code_[0];
165};
166
167} // namespace art
168
169#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_