blob: ec3769993521f32d4367b1642f4516890e107d62 [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
149 private:
150 static constexpr int kFixedSize = 0;
151
152 MemoryRegion region_;
153
Nicolas Geoffray39468442014-09-02 15:17:15 +0100154 friend class CodeInfo;
155 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100156};
157
158/**
159 * A Stack Map holds compilation information for a specific PC necessary for:
160 * - Mapping it to a dex PC,
161 * - Knowing which stack entries are objects,
162 * - Knowing which registers hold objects,
163 * - Knowing the inlining information,
164 * - Knowing the values of dex registers.
165 *
166 * The information is of the form:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100167 * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask, stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100168 *
169 * Note that register_mask is fixed size, but stack_mask is variable size, depending on the
170 * stack size of a method.
171 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100172class StackMap {
173 public:
174 explicit StackMap(MemoryRegion region) : region_(region) {}
175
176 uint32_t GetDexPc() const {
177 return region_.Load<uint32_t>(kDexPcOffset);
178 }
179
180 void SetDexPc(uint32_t dex_pc) {
181 region_.Store<uint32_t>(kDexPcOffset, dex_pc);
182 }
183
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 uint32_t GetNativePcOffset() const {
185 return region_.Load<uint32_t>(kNativePcOffsetOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100186 }
187
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188 void SetNativePcOffset(uint32_t native_pc_offset) {
189 return region_.Store<uint32_t>(kNativePcOffsetOffset, native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100190 }
191
192 uint32_t GetDexRegisterMapOffset() const {
193 return region_.Load<uint32_t>(kDexRegisterMapOffsetOffset);
194 }
195
196 void SetDexRegisterMapOffset(uint32_t offset) {
197 return region_.Store<uint32_t>(kDexRegisterMapOffsetOffset, offset);
198 }
199
200 uint32_t GetInlineDescriptorOffset() const {
201 return region_.Load<uint32_t>(kInlineDescriptorOffsetOffset);
202 }
203
204 void SetInlineDescriptorOffset(uint32_t offset) {
205 return region_.Store<uint32_t>(kInlineDescriptorOffsetOffset, offset);
206 }
207
208 uint32_t GetRegisterMask() const {
209 return region_.Load<uint32_t>(kRegisterMaskOffset);
210 }
211
212 void SetRegisterMask(uint32_t mask) {
213 region_.Store<uint32_t>(kRegisterMaskOffset, mask);
214 }
215
216 MemoryRegion GetStackMask() const {
217 return region_.Subregion(kStackMaskOffset, StackMaskSize());
218 }
219
220 void SetStackMask(const BitVector& sp_map) {
221 MemoryRegion region = GetStackMask();
222 for (size_t i = 0; i < region.size_in_bits(); i++) {
223 region.StoreBit(i, sp_map.IsBitSet(i));
224 }
225 }
226
Roland Levillain442b46a2015-02-18 16:54:21 +0000227 bool HasDexRegisterMap() const {
228 return GetDexRegisterMapOffset() != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100229 }
230
Roland Levillain442b46a2015-02-18 16:54:21 +0000231 bool HasInlineInfo() const {
232 return GetInlineDescriptorOffset() != kNoInlineInfo;
233 }
234
235 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100236 return region_.pointer() == other.region_.pointer()
237 && region_.size() == other.region_.size();
238 }
239
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000240 static size_t ComputeAlignedStackMapSize(size_t stack_mask_size) {
241 // On ARM, the stack maps must be 4-byte aligned.
242 return RoundUp(StackMap::kFixedSize + stack_mask_size, 4);
243 }
244
Roland Levillain442b46a2015-02-18 16:54:21 +0000245 // Special (invalid) offset for the DexRegisterMapOffset field meaning
246 // that there is no Dex register map for this stack map.
247 static constexpr uint32_t kNoDexRegisterMap = -1;
248
249 // Special (invalid) offset for the InlineDescriptorOffset field meaning
250 // that there is no inline info for this stack map.
251 static constexpr uint32_t kNoInlineInfo = -1;
252
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100253 private:
254 static constexpr int kDexPcOffset = 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100255 static constexpr int kNativePcOffsetOffset = kDexPcOffset + sizeof(uint32_t);
256 static constexpr int kDexRegisterMapOffsetOffset = kNativePcOffsetOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100257 static constexpr int kInlineDescriptorOffsetOffset =
258 kDexRegisterMapOffsetOffset + sizeof(uint32_t);
259 static constexpr int kRegisterMaskOffset = kInlineDescriptorOffsetOffset + sizeof(uint32_t);
260 static constexpr int kFixedSize = kRegisterMaskOffset + sizeof(uint32_t);
261 static constexpr int kStackMaskOffset = kFixedSize;
262
263 size_t StackMaskSize() const { return region_.size() - kFixedSize; }
264
265 MemoryRegion region_;
266
Nicolas Geoffray39468442014-09-02 15:17:15 +0100267 friend class CodeInfo;
268 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100269};
270
271
272/**
273 * Wrapper around all compiler information collected for a method.
274 * The information is of the form:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100275 * [overall_size, number_of_stack_maps, stack_mask_size, StackMap+, DexRegisterInfo+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100276 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100277class CodeInfo {
278 public:
279 explicit CodeInfo(MemoryRegion region) : region_(region) {}
280
Nicolas Geoffray39468442014-09-02 15:17:15 +0100281 explicit CodeInfo(const void* data) {
282 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
283 region_ = MemoryRegion(const_cast<void*>(data), size);
284 }
285
286 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100287 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100288 return StackMap(GetStackMaps().Subregion(i * size, size));
289 }
290
291 uint32_t GetOverallSize() const {
292 return region_.Load<uint32_t>(kOverallSizeOffset);
293 }
294
295 void SetOverallSize(uint32_t size) {
296 region_.Store<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100297 }
298
299 uint32_t GetStackMaskSize() const {
300 return region_.Load<uint32_t>(kStackMaskSizeOffset);
301 }
302
303 void SetStackMaskSize(uint32_t size) {
304 region_.Store<uint32_t>(kStackMaskSizeOffset, size);
305 }
306
307 size_t GetNumberOfStackMaps() const {
308 return region_.Load<uint32_t>(kNumberOfStackMapsOffset);
309 }
310
311 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
312 region_.Store<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
313 }
314
315 size_t StackMapSize() const {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000316 return StackMap::ComputeAlignedStackMapSize(GetStackMaskSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100317 }
318
Roland Levillain442b46a2015-02-18 16:54:21 +0000319 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
320 DCHECK(stack_map.HasDexRegisterMap());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100321 uint32_t offset = stack_map.GetDexRegisterMapOffset();
322 return DexRegisterMap(region_.Subregion(offset,
323 DexRegisterMap::kFixedSize + number_of_dex_registers * DexRegisterMap::SingleEntrySize()));
324 }
325
Roland Levillain442b46a2015-02-18 16:54:21 +0000326 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
327 DCHECK(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100328 uint32_t offset = stack_map.GetInlineDescriptorOffset();
329 uint8_t depth = region_.Load<uint8_t>(offset);
330 return InlineInfo(region_.Subregion(offset,
331 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
332 }
333
Roland Levillain442b46a2015-02-18 16:54:21 +0000334 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100335 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100336 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100337 if (stack_map.GetDexPc() == dex_pc) {
338 return stack_map;
339 }
340 }
341 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700342 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100343 }
344
Roland Levillain442b46a2015-02-18 16:54:21 +0000345 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100346 // TODO: stack maps are sorted by native pc, we can do a binary search.
347 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100348 StackMap stack_map = GetStackMapAt(i);
349 if (stack_map.GetNativePcOffset() == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100350 return stack_map;
351 }
352 }
353 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700354 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100355 }
356
357 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100358 static constexpr int kOverallSizeOffset = 0;
359 static constexpr int kNumberOfStackMapsOffset = kOverallSizeOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100360 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
361 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
362
363 MemoryRegion GetStackMaps() const {
364 return region_.size() == 0
365 ? MemoryRegion()
366 : region_.Subregion(kFixedSize, StackMapSize() * GetNumberOfStackMaps());
367 }
368
369 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100370 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100371};
372
373} // namespace art
374
375#endif // ART_RUNTIME_STACK_MAP_H_