blob: c9a2cfbc0e8c5b04e0af93e744cdae9f70fd06fb [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:
33 OatQuickMethodHeader(uint32_t mapping_table_offset = 0U,
34 uint32_t vmap_table_offset = 0U,
35 uint32_t gc_map_offset = 0U,
36 uint32_t frame_size_in_bytes = 0U,
37 uint32_t core_spill_mask = 0U,
38 uint32_t fp_spill_mask = 0U,
39 uint32_t code_size = 0U);
40
41 ~OatQuickMethodHeader();
42
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010043 static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
44 uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
45 uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
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
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010053 OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
54
55 uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
56 return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
57 }
58
59 bool IsOptimized() const {
60 return gc_map_offset_ == 0 && vmap_table_offset_ != 0;
61 }
62
63 CodeInfo GetOptimizedCodeInfo() const {
64 DCHECK(IsOptimized());
65 const void* data = reinterpret_cast<const void*>(code_ - vmap_table_offset_);
66 return CodeInfo(data);
67 }
68
69 const uint8_t* GetCode() const {
70 return code_;
71 }
72
73 const uint8_t* GetNativeGcMap() const {
74 return (gc_map_offset_ == 0) ? nullptr : code_ - gc_map_offset_;
75 }
76
77 const uint8_t* GetMappingTable() const {
78 return (mapping_table_offset_ == 0) ? nullptr : code_ - mapping_table_offset_;
79 }
80
81 const uint8_t* GetVmapTable() const {
82 CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
83 return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
84 }
85
86 bool Contains(uintptr_t pc) const {
87 uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010088 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
89 if (kRuntimeISA == kArm) {
90 // On Thumb-2, the pc is offset by one.
91 code_start++;
92 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010093 return code_start <= pc && pc <= (code_start + code_size_);
94 }
95
96 const uint8_t* GetEntryPoint() const {
97 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
98 // (not `kThumb2`), *but* we always generate code for the Thumb-2
99 // instruction set anyway. Thumb-2 requires the entrypoint to be of
100 // offset 1.
101 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
102 return (kRuntimeISA == kArm)
103 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
104 : code_;
105 }
106
107 template <bool kCheckFrameSize = true>
108 uint32_t GetFrameSizeInBytes() {
109 uint32_t result = frame_info_.FrameSizeInBytes();
110 if (kCheckFrameSize) {
111 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
112 }
113 return result;
114 }
115
116 QuickMethodFrameInfo GetFrameInfo() const {
117 return frame_info_;
118 }
119
120 uintptr_t ToNativeQuickPc(ArtMethod* method,
121 const uint32_t dex_pc,
122 bool is_for_catch_handler,
123 bool abort_on_failure = true) const;
124
125 uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
126
127 // The offset in bytes from the start of the mapping table to the end of the header.
128 uint32_t mapping_table_offset_;
129 // The offset in bytes from the start of the vmap table to the end of the header.
130 uint32_t vmap_table_offset_;
131 // The offset in bytes from the start of the gc map to the end of the header.
132 uint32_t gc_map_offset_;
133 // The stack frame information.
134 QuickMethodFrameInfo frame_info_;
135 // The code size in bytes.
136 uint32_t code_size_;
137 // The actual code.
138 uint8_t code_[0];
139};
140
141} // namespace art
142
143#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_