blob: 4afca7d828f4b2de08b46218d8bc0bbbfe63b356 [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"
22#include "quick/quick_method_frame_info.h"
23#include "stack_map.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010024#include "utils.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:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070033 explicit OatQuickMethodHeader(uint32_t vmap_table_offset = 0U,
34 uint32_t frame_size_in_bytes = 0U,
35 uint32_t core_spill_mask = 0U,
36 uint32_t fp_spill_mask = 0U,
37 uint32_t code_size = 0U);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010038
39 ~OatQuickMethodHeader();
40
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
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 {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010061 return code_size_ != 0 && vmap_table_offset_ != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010062 }
63
David Srbecky5d950762016-03-07 20:47:29 +000064 const void* GetOptimizedCodeInfoPtr() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010065 DCHECK(IsOptimized());
66 const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_);
David Srbecky5d950762016-03-07 20:47:29 +000067 return data;
68 }
69
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +010070 uint8_t* GetOptimizedCodeInfoPtr() {
71 DCHECK(IsOptimized());
72 return code_ - vmap_table_offset_;
73 }
74
David Srbecky5d950762016-03-07 20:47:29 +000075 CodeInfo GetOptimizedCodeInfo() const {
76 return CodeInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010077 }
78
79 const uint8_t* GetCode() const {
80 return code_;
81 }
82
David Srbecky5d950762016-03-07 20:47:29 +000083 uint32_t GetCodeSize() const {
84 return code_size_;
85 }
86
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010087 const uint8_t* GetVmapTable() const {
88 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
89 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
90 }
91
92 bool Contains(uintptr_t pc) const {
93 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010094 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
95 if (kRuntimeISA == kArm) {
96 // On Thumb-2, the pc is offset by one.
97 code_start++;
98 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010099 return code_start <= pc && pc <= (code_start + code_size_);
100 }
101
102 const uint8_t* GetEntryPoint() const {
103 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
104 // (not `kThumb2`), *but* we always generate code for the Thumb-2
105 // instruction set anyway. Thumb-2 requires the entrypoint to be of
106 // offset 1.
107 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
108 return (kRuntimeISA == kArm)
109 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
110 : code_;
111 }
112
113 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000114 uint32_t GetFrameSizeInBytes() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100115 uint32_t result = frame_info_.FrameSizeInBytes();
116 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000117 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100118 }
119 return result;
120 }
121
122 QuickMethodFrameInfo GetFrameInfo() const {
123 return frame_info_;
124 }
125
126 uintptr_t ToNativeQuickPc(ArtMethod* method,
127 const uint32_t dex_pc,
128 bool is_for_catch_handler,
129 bool abort_on_failure = true) const;
130
131 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
132
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100133 // The offset in bytes from the start of the vmap table to the end of the header.
134 uint32_t vmap_table_offset_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100135 // The stack frame information.
136 QuickMethodFrameInfo frame_info_;
137 // The code size in bytes.
138 uint32_t code_size_;
139 // The actual code.
140 uint8_t code_[0];
141};
142
143} // namespace art
144
145#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_