blob: 6d996722b41bdb25d2d708d696ca82755ce510f7 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 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_STACK_MAP_H_
18#define ART_RUNTIME_STACK_MAP_H_
19
20#include "base/bit_vector.h"
21#include "memory_region.h"
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +000022#include "utils.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023
24namespace art {
25
26/**
27 * Classes in the following file are wrapper on stack map information backed
28 * by a MemoryRegion. As such they read and write to the region, they don't have
29 * their own fields.
30 */
31
32/**
33 * Inline information for a specific PC. The information is of the form:
34 * [inlining_depth, [method_dex reference]+]
35 */
36class InlineInfo {
37 public:
38 explicit InlineInfo(MemoryRegion region) : region_(region) {}
39
40 uint8_t GetDepth() const {
41 return region_.Load<uint8_t>(kDepthOffset);
42 }
43
44 void SetDepth(uint8_t depth) {
45 region_.Store<uint8_t>(kDepthOffset, depth);
46 }
47
48 uint32_t GetMethodReferenceIndexAtDepth(uint8_t depth) const {
49 return region_.Load<uint32_t>(kFixedSize + depth * SingleEntrySize());
50 }
51
52 void SetMethodReferenceIndexAtDepth(uint8_t depth, uint32_t index) {
53 region_.Store<uint32_t>(kFixedSize + depth * SingleEntrySize(), index);
54 }
55
56 static size_t SingleEntrySize() {
57 return sizeof(uint32_t);
58 }
59
60 private:
61 static constexpr int kDepthOffset = 0;
62 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
63
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010064 MemoryRegion region_;
65
Nicolas Geoffray39468442014-09-02 15:17:15 +010066 friend class CodeInfo;
67 friend class StackMap;
68 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010069};
70
71/**
72 * Information on dex register values for a specific PC. The information is
73 * of the form:
74 * [location_kind, register_value]+.
75 *
76 * The location_kind for a Dex register can either be:
Roland Levillain442b46a2015-02-18 16:54:21 +000077 * - kConstant: register_value holds the constant,
78 * - kStack: register_value holds the stack offset,
79 * - kRegister: register_value holds the physical register number.
80 * - kFpuRegister: register_value holds the physical register number.
81 * - kNone: the register has no location yet, meaning it has not been set.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010082 */
83class DexRegisterMap {
84 public:
85 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
86
87 enum LocationKind {
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 kNone,
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010089 kInStack,
90 kInRegister,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010091 kInFpuRegister,
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010092 kConstant
93 };
94
Roland Levillain442b46a2015-02-18 16:54:21 +000095 static const char* PrettyDescriptor(LocationKind kind) {
96 switch (kind) {
97 case kNone:
98 return "none";
99 case kInStack:
100 return "in stack";
101 case kInRegister:
102 return "in register";
103 case kInFpuRegister:
104 return "in fpu register";
105 case kConstant:
106 return "as constant";
Roland Levillain442b46a2015-02-18 16:54:21 +0000107 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100108 UNREACHABLE();
109 return nullptr;
Roland Levillain442b46a2015-02-18 16:54:21 +0000110 }
111
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100112 LocationKind GetLocationKind(uint16_t register_index) const {
113 return region_.Load<LocationKind>(
114 kFixedSize + register_index * SingleEntrySize());
115 }
116
117 void SetRegisterInfo(uint16_t register_index, LocationKind kind, int32_t value) {
118 size_t entry = kFixedSize + register_index * SingleEntrySize();
119 region_.Store<LocationKind>(entry, kind);
120 region_.Store<int32_t>(entry + sizeof(LocationKind), value);
121 }
122
123 int32_t GetValue(uint16_t register_index) const {
124 return region_.Load<int32_t>(
125 kFixedSize + sizeof(LocationKind) + register_index * SingleEntrySize());
126 }
127
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100128 int32_t GetStackOffsetInBytes(uint16_t register_index) const {
129 DCHECK(GetLocationKind(register_index) == kInStack);
130 // We currently encode the offset in bytes.
131 return GetValue(register_index);
132 }
133
134 int32_t GetConstant(uint16_t register_index) const {
135 DCHECK(GetLocationKind(register_index) == kConstant);
136 return GetValue(register_index);
137 }
138
139 int32_t GetMachineRegister(uint16_t register_index) const {
140 DCHECK(GetLocationKind(register_index) == kInRegister
141 || GetLocationKind(register_index) == kInFpuRegister);
142 return GetValue(register_index);
143 }
144
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100145 static size_t SingleEntrySize() {
146 return sizeof(LocationKind) + sizeof(int32_t);
147 }
148
Roland Levillain12baf472015-03-05 12:41:42 +0000149 size_t Size() const {
150 return region_.size();
151 }
152
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100153 static constexpr int kFixedSize = 0;
154
Roland Levillain12baf472015-03-05 12:41:42 +0000155 private:
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100156 MemoryRegion region_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100157};
158
159/**
160 * A Stack Map holds compilation information for a specific PC necessary for:
161 * - Mapping it to a dex PC,
162 * - Knowing which stack entries are objects,
163 * - Knowing which registers hold objects,
164 * - Knowing the inlining information,
165 * - Knowing the values of dex registers.
166 *
167 * The information is of the form:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask, stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100169 *
170 * Note that register_mask is fixed size, but stack_mask is variable size, depending on the
171 * stack size of a method.
172 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100173class StackMap {
174 public:
175 explicit StackMap(MemoryRegion region) : region_(region) {}
176
177 uint32_t GetDexPc() const {
178 return region_.Load<uint32_t>(kDexPcOffset);
179 }
180
181 void SetDexPc(uint32_t dex_pc) {
182 region_.Store<uint32_t>(kDexPcOffset, dex_pc);
183 }
184
Nicolas Geoffray39468442014-09-02 15:17:15 +0100185 uint32_t GetNativePcOffset() const {
186 return region_.Load<uint32_t>(kNativePcOffsetOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100187 }
188
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 void SetNativePcOffset(uint32_t native_pc_offset) {
190 return region_.Store<uint32_t>(kNativePcOffsetOffset, native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100191 }
192
193 uint32_t GetDexRegisterMapOffset() const {
194 return region_.Load<uint32_t>(kDexRegisterMapOffsetOffset);
195 }
196
197 void SetDexRegisterMapOffset(uint32_t offset) {
198 return region_.Store<uint32_t>(kDexRegisterMapOffsetOffset, offset);
199 }
200
201 uint32_t GetInlineDescriptorOffset() const {
202 return region_.Load<uint32_t>(kInlineDescriptorOffsetOffset);
203 }
204
205 void SetInlineDescriptorOffset(uint32_t offset) {
206 return region_.Store<uint32_t>(kInlineDescriptorOffsetOffset, offset);
207 }
208
209 uint32_t GetRegisterMask() const {
210 return region_.Load<uint32_t>(kRegisterMaskOffset);
211 }
212
213 void SetRegisterMask(uint32_t mask) {
214 region_.Store<uint32_t>(kRegisterMaskOffset, mask);
215 }
216
217 MemoryRegion GetStackMask() const {
218 return region_.Subregion(kStackMaskOffset, StackMaskSize());
219 }
220
221 void SetStackMask(const BitVector& sp_map) {
222 MemoryRegion region = GetStackMask();
223 for (size_t i = 0; i < region.size_in_bits(); i++) {
224 region.StoreBit(i, sp_map.IsBitSet(i));
225 }
226 }
227
Roland Levillain442b46a2015-02-18 16:54:21 +0000228 bool HasDexRegisterMap() const {
229 return GetDexRegisterMapOffset() != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100230 }
231
Roland Levillain442b46a2015-02-18 16:54:21 +0000232 bool HasInlineInfo() const {
233 return GetInlineDescriptorOffset() != kNoInlineInfo;
234 }
235
236 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100237 return region_.pointer() == other.region_.pointer()
238 && region_.size() == other.region_.size();
239 }
240
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000241 static size_t ComputeAlignedStackMapSize(size_t stack_mask_size) {
242 // On ARM, the stack maps must be 4-byte aligned.
243 return RoundUp(StackMap::kFixedSize + stack_mask_size, 4);
244 }
245
Roland Levillain442b46a2015-02-18 16:54:21 +0000246 // Special (invalid) offset for the DexRegisterMapOffset field meaning
247 // that there is no Dex register map for this stack map.
248 static constexpr uint32_t kNoDexRegisterMap = -1;
249
250 // Special (invalid) offset for the InlineDescriptorOffset field meaning
251 // that there is no inline info for this stack map.
252 static constexpr uint32_t kNoInlineInfo = -1;
253
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100254 private:
255 static constexpr int kDexPcOffset = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100256 static constexpr int kNativePcOffsetOffset = kDexPcOffset + sizeof(uint32_t);
257 static constexpr int kDexRegisterMapOffsetOffset = kNativePcOffsetOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100258 static constexpr int kInlineDescriptorOffsetOffset =
259 kDexRegisterMapOffsetOffset + sizeof(uint32_t);
260 static constexpr int kRegisterMaskOffset = kInlineDescriptorOffsetOffset + sizeof(uint32_t);
261 static constexpr int kFixedSize = kRegisterMaskOffset + sizeof(uint32_t);
262 static constexpr int kStackMaskOffset = kFixedSize;
263
264 size_t StackMaskSize() const { return region_.size() - kFixedSize; }
265
266 MemoryRegion region_;
267
Nicolas Geoffray39468442014-09-02 15:17:15 +0100268 friend class CodeInfo;
269 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100270};
271
272
273/**
274 * Wrapper around all compiler information collected for a method.
275 * The information is of the form:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100276 * [overall_size, number_of_stack_maps, stack_mask_size, StackMap+, DexRegisterInfo+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100277 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100278class CodeInfo {
279 public:
280 explicit CodeInfo(MemoryRegion region) : region_(region) {}
281
Nicolas Geoffray39468442014-09-02 15:17:15 +0100282 explicit CodeInfo(const void* data) {
283 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
284 region_ = MemoryRegion(const_cast<void*>(data), size);
285 }
286
287 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100288 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100289 return StackMap(GetStackMaps().Subregion(i * size, size));
290 }
291
292 uint32_t GetOverallSize() const {
293 return region_.Load<uint32_t>(kOverallSizeOffset);
294 }
295
296 void SetOverallSize(uint32_t size) {
297 region_.Store<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100298 }
299
300 uint32_t GetStackMaskSize() const {
301 return region_.Load<uint32_t>(kStackMaskSizeOffset);
302 }
303
304 void SetStackMaskSize(uint32_t size) {
305 region_.Store<uint32_t>(kStackMaskSizeOffset, size);
306 }
307
308 size_t GetNumberOfStackMaps() const {
309 return region_.Load<uint32_t>(kNumberOfStackMapsOffset);
310 }
311
312 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
313 region_.Store<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
314 }
315
316 size_t StackMapSize() const {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000317 return StackMap::ComputeAlignedStackMapSize(GetStackMaskSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100318 }
319
Roland Levillain442b46a2015-02-18 16:54:21 +0000320 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
321 DCHECK(stack_map.HasDexRegisterMap());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100322 uint32_t offset = stack_map.GetDexRegisterMapOffset();
323 return DexRegisterMap(region_.Subregion(offset,
324 DexRegisterMap::kFixedSize + number_of_dex_registers * DexRegisterMap::SingleEntrySize()));
325 }
326
Roland Levillain442b46a2015-02-18 16:54:21 +0000327 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
328 DCHECK(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100329 uint32_t offset = stack_map.GetInlineDescriptorOffset();
330 uint8_t depth = region_.Load<uint8_t>(offset);
331 return InlineInfo(region_.Subregion(offset,
332 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
333 }
334
Roland Levillain442b46a2015-02-18 16:54:21 +0000335 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100336 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100337 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100338 if (stack_map.GetDexPc() == dex_pc) {
339 return stack_map;
340 }
341 }
342 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700343 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100344 }
345
Roland Levillain442b46a2015-02-18 16:54:21 +0000346 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100347 // TODO: stack maps are sorted by native pc, we can do a binary search.
348 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100349 StackMap stack_map = GetStackMapAt(i);
350 if (stack_map.GetNativePcOffset() == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100351 return stack_map;
352 }
353 }
354 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700355 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100356 }
357
358 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100359 static constexpr int kOverallSizeOffset = 0;
360 static constexpr int kNumberOfStackMapsOffset = kOverallSizeOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100361 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
362 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
363
364 MemoryRegion GetStackMaps() const {
365 return region_.size() == 0
366 ? MemoryRegion()
367 : region_.Subregion(kFixedSize, StackMapSize() * GetNumberOfStackMaps());
368 }
369
370 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100371 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100372};
373
374} // namespace art
375
376#endif // ART_RUNTIME_STACK_MAP_H_