blob: 604787fd9241d546516151d1b4dbfc6f8820887f [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#include "stack_map.h"
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070018
19#include "base/arena_bit_vector.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010020#include "stack_map_stream.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010021
22#include "gtest/gtest.h"
23
24namespace art {
25
Roland Levillaina2d8ec62015-03-12 15:25:29 +000026static bool SameBits(MemoryRegion region, const BitVector& bit_vector) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010027 for (size_t i = 0; i < region.size_in_bits(); ++i) {
28 if (region.LoadBit(i) != bit_vector.IsBitSet(i)) {
29 return false;
30 }
31 }
32 return true;
33}
34
Roland Levillaina552e1c2015-03-26 15:01:03 +000035using Kind = DexRegisterLocation::Kind;
36
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010037TEST(StackMapTest, Test1) {
38 ArenaPool pool;
39 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010040 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010041
42 ArenaBitVector sp_mask(&arena, 0, false);
Roland Levillain12baf472015-03-05 12:41:42 +000043 size_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +010044 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010045 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
46 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +010047 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010048
Calin Juravle4f46ac52015-04-23 18:47:21 +010049 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010050 void* memory = arena.Alloc(size, kArenaAllocMisc);
51 MemoryRegion region(memory, size);
52 stream.FillIn(region);
53
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 CodeInfo code_info(region);
David Brazdilf677ebf2015-05-29 16:29:43 +010055 StackMapEncoding encoding = code_info.ExtractEncoding();
56 ASSERT_EQ(0u, encoding.NumberOfBytesForStackMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010057 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
58
Roland Levillain1c1da432015-07-16 11:54:44 +010059 uint32_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
Roland Levillaina552e1c2015-03-26 15:01:03 +000060 ASSERT_EQ(2u, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +010061 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(encoding);
Roland Levillaina552e1c2015-03-26 15:01:03 +000062 // The Dex register location catalog contains:
63 // - one 1-byte short Dex register location, and
64 // - one 5-byte large Dex register location.
65 size_t expected_location_catalog_size = 1u + 5u;
66 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
67
David Brazdilf677ebf2015-05-29 16:29:43 +010068 StackMap stack_map = code_info.GetStackMapAt(0, encoding);
69 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0, encoding)));
70 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64, encoding)));
71 ASSERT_EQ(0u, stack_map.GetDexPc(encoding));
72 ASSERT_EQ(64u, stack_map.GetNativePcOffset(encoding));
73 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010074
David Brazdilf677ebf2015-05-29 16:29:43 +010075 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010076 ASSERT_TRUE(SameBits(stack_mask, sp_mask));
77
David Brazdilf677ebf2015-05-29 16:29:43 +010078 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +000079 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010080 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +000081 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
82 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
83 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
84 // The Dex register map contains:
85 // - one 1-byte live bit mask, and
86 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
87 size_t expected_dex_register_map_size = 1u + 1u;
88 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
89
David Brazdilf677ebf2015-05-29 16:29:43 +010090 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationKind(
91 0, number_of_dex_registers, code_info, encoding));
92 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(
93 1, number_of_dex_registers, code_info, encoding));
94 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationInternalKind(
95 0, number_of_dex_registers, code_info, encoding));
96 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(
97 1, number_of_dex_registers, code_info, encoding));
98 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(
99 0, number_of_dex_registers, code_info, encoding));
100 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info, encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000101
102 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
103 0, number_of_dex_registers, number_of_location_catalog_entries);
104 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
105 1, number_of_dex_registers, number_of_location_catalog_entries);
106 ASSERT_EQ(0u, index0);
107 ASSERT_EQ(1u, index1);
108 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
109 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
110 ASSERT_EQ(Kind::kInStack, location0.GetKind());
111 ASSERT_EQ(Kind::kConstant, location1.GetKind());
112 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
113 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000114 ASSERT_EQ(0, location0.GetValue());
115 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000116
David Brazdilf677ebf2015-05-29 16:29:43 +0100117 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100118}
119
120TEST(StackMapTest, Test2) {
121 ArenaPool pool;
122 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100123 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100124
125 ArenaBitVector sp_mask1(&arena, 0, true);
126 sp_mask1.SetBit(2);
127 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +0000128 size_t number_of_dex_registers = 2;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100129 size_t number_of_dex_registers_in_inline_info = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100130 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100131 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
132 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100133 stream.BeginInlineInfoEntry(82, 3, kDirect, number_of_dex_registers_in_inline_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100134 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100135 stream.BeginInlineInfoEntry(42, 2, kStatic, number_of_dex_registers_in_inline_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100136 stream.EndInlineInfoEntry();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100137 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100138
139 ArenaBitVector sp_mask2(&arena, 0, true);
140 sp_mask2.SetBit(3);
David Brazdilf10a25f2015-06-02 14:29:52 +0100141 sp_mask2.SetBit(8);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100142 stream.BeginStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100143 stream.AddDexRegisterEntry(Kind::kInRegister, 18); // Short location.
144 stream.AddDexRegisterEntry(Kind::kInFpuRegister, 3); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100145 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100146
David Brazdild9cb68e2015-08-25 13:52:43 +0100147 ArenaBitVector sp_mask3(&arena, 0, true);
148 sp_mask3.SetBit(1);
149 sp_mask3.SetBit(5);
150 stream.BeginStackMapEntry(2, 192, 0xAB, &sp_mask3, number_of_dex_registers, 0);
151 stream.AddDexRegisterEntry(Kind::kInRegister, 6); // Short location.
152 stream.AddDexRegisterEntry(Kind::kInRegisterHigh, 8); // Short location.
153 stream.EndStackMapEntry();
154
155 ArenaBitVector sp_mask4(&arena, 0, true);
156 sp_mask4.SetBit(6);
157 sp_mask4.SetBit(7);
158 stream.BeginStackMapEntry(3, 256, 0xCD, &sp_mask4, number_of_dex_registers, 0);
159 stream.AddDexRegisterEntry(Kind::kInFpuRegister, 3); // Short location, same in stack map 2.
160 stream.AddDexRegisterEntry(Kind::kInFpuRegisterHigh, 1); // Short location.
161 stream.EndStackMapEntry();
162
Calin Juravle4f46ac52015-04-23 18:47:21 +0100163 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100164 void* memory = arena.Alloc(size, kArenaAllocMisc);
165 MemoryRegion region(memory, size);
166 stream.FillIn(region);
167
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 CodeInfo code_info(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100169 StackMapEncoding encoding = code_info.ExtractEncoding();
170 ASSERT_EQ(2u, encoding.NumberOfBytesForStackMask());
David Brazdild9cb68e2015-08-25 13:52:43 +0100171 ASSERT_EQ(4u, code_info.GetNumberOfStackMaps());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100172
Roland Levillain1c1da432015-07-16 11:54:44 +0100173 uint32_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Brazdild9cb68e2015-08-25 13:52:43 +0100174 ASSERT_EQ(7u, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100175 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(encoding);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000176 // The Dex register location catalog contains:
David Brazdild9cb68e2015-08-25 13:52:43 +0100177 // - six 1-byte short Dex register locations, and
Roland Levillaina552e1c2015-03-26 15:01:03 +0000178 // - one 5-byte large Dex register location.
David Brazdild9cb68e2015-08-25 13:52:43 +0100179 size_t expected_location_catalog_size = 6u * 1u + 5u;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000180 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
181
Roland Levillain12baf472015-03-05 12:41:42 +0000182 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000183 {
David Brazdilf677ebf2015-05-29 16:29:43 +0100184 StackMap stack_map = code_info.GetStackMapAt(0, encoding);
185 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0, encoding)));
186 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64, encoding)));
187 ASSERT_EQ(0u, stack_map.GetDexPc(encoding));
188 ASSERT_EQ(64u, stack_map.GetNativePcOffset(encoding));
189 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100190
David Brazdilf677ebf2015-05-29 16:29:43 +0100191 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000192 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100193
David Brazdilf677ebf2015-05-29 16:29:43 +0100194 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000195 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100196 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000197 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
198 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
199 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
200 // The Dex register map contains:
201 // - one 1-byte live bit mask, and
202 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
203 size_t expected_dex_register_map_size = 1u + 1u;
204 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
205
David Brazdilf677ebf2015-05-29 16:29:43 +0100206 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationKind(
207 0, number_of_dex_registers, code_info, encoding));
208 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(
209 1, number_of_dex_registers, code_info, encoding));
210 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationInternalKind(
211 0, number_of_dex_registers, code_info, encoding));
212 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(
213 1, number_of_dex_registers, code_info, encoding));
214 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(
215 0, number_of_dex_registers, code_info, encoding));
216 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info, encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000217
218 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
219 0, number_of_dex_registers, number_of_location_catalog_entries);
220 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
221 1, number_of_dex_registers, number_of_location_catalog_entries);
222 ASSERT_EQ(0u, index0);
223 ASSERT_EQ(1u, index1);
224 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
225 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
226 ASSERT_EQ(Kind::kInStack, location0.GetKind());
227 ASSERT_EQ(Kind::kConstant, location1.GetKind());
228 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
229 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000230 ASSERT_EQ(0, location0.GetValue());
231 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100232
David Brazdilf677ebf2015-05-29 16:29:43 +0100233 ASSERT_TRUE(stack_map.HasInlineInfo(encoding));
234 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000235 ASSERT_EQ(2u, inline_info.GetDepth());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100236 ASSERT_EQ(82u, inline_info.GetMethodIndexAtDepth(0));
237 ASSERT_EQ(42u, inline_info.GetMethodIndexAtDepth(1));
238 ASSERT_EQ(3u, inline_info.GetDexPcAtDepth(0));
239 ASSERT_EQ(2u, inline_info.GetDexPcAtDepth(1));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100240 ASSERT_EQ(kDirect, inline_info.GetInvokeTypeAtDepth(0));
241 ASSERT_EQ(kStatic, inline_info.GetInvokeTypeAtDepth(1));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000242 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100243
Roland Levillain12baf472015-03-05 12:41:42 +0000244 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000245 {
David Brazdilf677ebf2015-05-29 16:29:43 +0100246 StackMap stack_map = code_info.GetStackMapAt(1, encoding);
247 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u, encoding)));
248 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u, encoding)));
249 ASSERT_EQ(1u, stack_map.GetDexPc(encoding));
250 ASSERT_EQ(128u, stack_map.GetNativePcOffset(encoding));
251 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100252
David Brazdilf677ebf2015-05-29 16:29:43 +0100253 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000254 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100255
David Brazdilf677ebf2015-05-29 16:29:43 +0100256 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000257 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100258 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000259 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
260 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
261 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
262 // The Dex register map contains:
263 // - one 1-byte live bit mask, and
264 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
265 size_t expected_dex_register_map_size = 1u + 1u;
266 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
267
David Brazdilf677ebf2015-05-29 16:29:43 +0100268 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationKind(
269 0, number_of_dex_registers, code_info, encoding));
270 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationKind(
271 1, number_of_dex_registers, code_info, encoding));
272 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationInternalKind(
273 0, number_of_dex_registers, code_info, encoding));
274 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationInternalKind(
275 1, number_of_dex_registers, code_info, encoding));
276 ASSERT_EQ(18, dex_register_map.GetMachineRegister(
277 0, number_of_dex_registers, code_info, encoding));
278 ASSERT_EQ(3, dex_register_map.GetMachineRegister(
279 1, number_of_dex_registers, code_info, encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000280
281 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
282 0, number_of_dex_registers, number_of_location_catalog_entries);
283 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
284 1, number_of_dex_registers, number_of_location_catalog_entries);
285 ASSERT_EQ(2u, index0);
286 ASSERT_EQ(3u, index1);
287 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
288 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
289 ASSERT_EQ(Kind::kInRegister, location0.GetKind());
290 ASSERT_EQ(Kind::kInFpuRegister, location1.GetKind());
291 ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind());
292 ASSERT_EQ(Kind::kInFpuRegister, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000293 ASSERT_EQ(18, location0.GetValue());
294 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000295
David Brazdilf677ebf2015-05-29 16:29:43 +0100296 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000297 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100298
299 // Third stack map.
300 {
301 StackMap stack_map = code_info.GetStackMapAt(2, encoding);
302 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(2u, encoding)));
303 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(192u, encoding)));
304 ASSERT_EQ(2u, stack_map.GetDexPc(encoding));
305 ASSERT_EQ(192u, stack_map.GetNativePcOffset(encoding));
306 ASSERT_EQ(0xABu, stack_map.GetRegisterMask(encoding));
307
308 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
309 ASSERT_TRUE(SameBits(stack_mask, sp_mask3));
310
311 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
312 DexRegisterMap dex_register_map =
313 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
314 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
315 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
316 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
317 // The Dex register map contains:
318 // - one 1-byte live bit mask, and
319 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
320 size_t expected_dex_register_map_size = 1u + 1u;
321 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
322
323 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationKind(
324 0, number_of_dex_registers, code_info, encoding));
325 ASSERT_EQ(Kind::kInRegisterHigh, dex_register_map.GetLocationKind(
326 1, number_of_dex_registers, code_info, encoding));
327 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationInternalKind(
328 0, number_of_dex_registers, code_info, encoding));
329 ASSERT_EQ(Kind::kInRegisterHigh, dex_register_map.GetLocationInternalKind(
330 1, number_of_dex_registers, code_info, encoding));
331 ASSERT_EQ(6, dex_register_map.GetMachineRegister(
332 0, number_of_dex_registers, code_info, encoding));
333 ASSERT_EQ(8, dex_register_map.GetMachineRegister(
334 1, number_of_dex_registers, code_info, encoding));
335
336 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
337 0, number_of_dex_registers, number_of_location_catalog_entries);
338 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
339 1, number_of_dex_registers, number_of_location_catalog_entries);
340 ASSERT_EQ(4u, index0);
341 ASSERT_EQ(5u, index1);
342 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
343 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
344 ASSERT_EQ(Kind::kInRegister, location0.GetKind());
345 ASSERT_EQ(Kind::kInRegisterHigh, location1.GetKind());
346 ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind());
347 ASSERT_EQ(Kind::kInRegisterHigh, location1.GetInternalKind());
348 ASSERT_EQ(6, location0.GetValue());
349 ASSERT_EQ(8, location1.GetValue());
350
351 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
352 }
353
354 // Fourth stack map.
355 {
356 StackMap stack_map = code_info.GetStackMapAt(3, encoding);
357 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(3u, encoding)));
358 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(256u, encoding)));
359 ASSERT_EQ(3u, stack_map.GetDexPc(encoding));
360 ASSERT_EQ(256u, stack_map.GetNativePcOffset(encoding));
361 ASSERT_EQ(0xCDu, stack_map.GetRegisterMask(encoding));
362
363 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
364 ASSERT_TRUE(SameBits(stack_mask, sp_mask4));
365
366 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
367 DexRegisterMap dex_register_map =
368 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
369 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
370 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
371 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
372 // The Dex register map contains:
373 // - one 1-byte live bit mask, and
374 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
375 size_t expected_dex_register_map_size = 1u + 1u;
376 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
377
378 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationKind(
379 0, number_of_dex_registers, code_info, encoding));
380 ASSERT_EQ(Kind::kInFpuRegisterHigh, dex_register_map.GetLocationKind(
381 1, number_of_dex_registers, code_info, encoding));
382 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationInternalKind(
383 0, number_of_dex_registers, code_info, encoding));
384 ASSERT_EQ(Kind::kInFpuRegisterHigh, dex_register_map.GetLocationInternalKind(
385 1, number_of_dex_registers, code_info, encoding));
386 ASSERT_EQ(3, dex_register_map.GetMachineRegister(
387 0, number_of_dex_registers, code_info, encoding));
388 ASSERT_EQ(1, dex_register_map.GetMachineRegister(
389 1, number_of_dex_registers, code_info, encoding));
390
391 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
392 0, number_of_dex_registers, number_of_location_catalog_entries);
393 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
394 1, number_of_dex_registers, number_of_location_catalog_entries);
395 ASSERT_EQ(3u, index0); // Shared with second stack map.
396 ASSERT_EQ(6u, index1);
397 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
398 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
399 ASSERT_EQ(Kind::kInFpuRegister, location0.GetKind());
400 ASSERT_EQ(Kind::kInFpuRegisterHigh, location1.GetKind());
401 ASSERT_EQ(Kind::kInFpuRegister, location0.GetInternalKind());
402 ASSERT_EQ(Kind::kInFpuRegisterHigh, location1.GetInternalKind());
403 ASSERT_EQ(3, location0.GetValue());
404 ASSERT_EQ(1, location1.GetValue());
405
406 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
407 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100408}
409
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000410TEST(StackMapTest, TestNonLiveDexRegisters) {
411 ArenaPool pool;
412 ArenaAllocator arena(&pool);
413 StackMapStream stream(&arena);
414
415 ArenaBitVector sp_mask(&arena, 0, false);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000416 uint32_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100417 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100418 stream.AddDexRegisterEntry(Kind::kNone, 0); // No location.
419 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100420 stream.EndStackMapEntry();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000421
Calin Juravle4f46ac52015-04-23 18:47:21 +0100422 size_t size = stream.PrepareForFillIn();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000423 void* memory = arena.Alloc(size, kArenaAllocMisc);
424 MemoryRegion region(memory, size);
425 stream.FillIn(region);
426
427 CodeInfo code_info(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100428 StackMapEncoding encoding = code_info.ExtractEncoding();
429 ASSERT_EQ(0u, encoding.NumberOfBytesForStackMask());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000430 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
431
Roland Levillain1c1da432015-07-16 11:54:44 +0100432 uint32_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000433 ASSERT_EQ(1u, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100434 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(encoding);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000435 // The Dex register location catalog contains:
436 // - one 5-byte large Dex register location.
437 size_t expected_location_catalog_size = 5u;
438 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
439
David Brazdilf677ebf2015-05-29 16:29:43 +0100440 StackMap stack_map = code_info.GetStackMapAt(0, encoding);
441 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0, encoding)));
442 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64, encoding)));
443 ASSERT_EQ(0u, stack_map.GetDexPc(encoding));
444 ASSERT_EQ(64u, stack_map.GetNativePcOffset(encoding));
445 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000446
David Brazdilf677ebf2015-05-29 16:29:43 +0100447 ASSERT_TRUE(stack_map.HasDexRegisterMap(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000448 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100449 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000450 ASSERT_FALSE(dex_register_map.IsDexRegisterLive(0));
451 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
452 ASSERT_EQ(1u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
453 // The Dex register map contains:
454 // - one 1-byte live bit mask.
455 // No space is allocated for the sole location catalog entry index, as it is useless.
456 size_t expected_dex_register_map_size = 1u + 0u;
457 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
458
David Brazdilf677ebf2015-05-29 16:29:43 +0100459 ASSERT_EQ(Kind::kNone, dex_register_map.GetLocationKind(
460 0, number_of_dex_registers, code_info, encoding));
461 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(
462 1, number_of_dex_registers, code_info, encoding));
463 ASSERT_EQ(Kind::kNone, dex_register_map.GetLocationInternalKind(
464 0, number_of_dex_registers, code_info, encoding));
465 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(
466 1, number_of_dex_registers, code_info, encoding));
467 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info, encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000468
469 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
470 0, number_of_dex_registers, number_of_location_catalog_entries);
471 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
472 1, number_of_dex_registers, number_of_location_catalog_entries);
473 ASSERT_EQ(DexRegisterLocationCatalog::kNoLocationEntryIndex, index0);
474 ASSERT_EQ(0u, index1);
475 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
476 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
477 ASSERT_EQ(Kind::kNone, location0.GetKind());
478 ASSERT_EQ(Kind::kConstant, location1.GetKind());
479 ASSERT_EQ(Kind::kNone, location0.GetInternalKind());
480 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
481 ASSERT_EQ(0, location0.GetValue());
482 ASSERT_EQ(-2, location1.GetValue());
483
David Brazdilf677ebf2015-05-29 16:29:43 +0100484 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000485}
486
487// Generate a stack map whose dex register offset is
488// StackMap::kNoDexRegisterMapSmallEncoding, and ensure we do
489// not treat it as kNoDexRegisterMap.
490TEST(StackMapTest, DexRegisterMapOffsetOverflow) {
491 ArenaPool pool;
492 ArenaAllocator arena(&pool);
493 StackMapStream stream(&arena);
494
495 ArenaBitVector sp_mask(&arena, 0, false);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000496 uint32_t number_of_dex_registers = 1024;
497 // Create the first stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100498 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000499 uint32_t number_of_dex_live_registers_in_dex_register_map_0 = number_of_dex_registers - 8;
500 for (uint32_t i = 0; i < number_of_dex_live_registers_in_dex_register_map_0; ++i) {
501 // Use two different Dex register locations to populate this map,
502 // as using a single value (in the whole CodeInfo object) would
503 // make this Dex register mapping data empty (see
504 // art::DexRegisterMap::SingleEntrySizeInBits).
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100505 stream.AddDexRegisterEntry(Kind::kConstant, i % 2); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000506 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100507 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000508 // Create the second stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100509 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000510 for (uint32_t i = 0; i < number_of_dex_registers; ++i) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100511 stream.AddDexRegisterEntry(Kind::kConstant, 0); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000512 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100513 stream.EndStackMapEntry();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000514
Calin Juravle4f46ac52015-04-23 18:47:21 +0100515 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000516 void* memory = arena.Alloc(size, kArenaAllocMisc);
517 MemoryRegion region(memory, size);
518 stream.FillIn(region);
519
520 CodeInfo code_info(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100521 StackMapEncoding encoding = code_info.ExtractEncoding();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000522 // The location catalog contains two entries (DexRegisterLocation(kConstant, 0)
523 // and DexRegisterLocation(kConstant, 1)), therefore the location catalog index
524 // has a size of 1 bit.
Roland Levillain1c1da432015-07-16 11:54:44 +0100525 uint32_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000526 ASSERT_EQ(2u, number_of_location_catalog_entries);
527 ASSERT_EQ(1u, DexRegisterMap::SingleEntrySizeInBits(number_of_location_catalog_entries));
528
529 // The first Dex register map contains:
530 // - a live register bit mask for 1024 registers (that is, 128 bytes of
531 // data); and
532 // - Dex register mapping information for 1016 1-bit Dex (live) register
533 // locations (that is, 127 bytes of data).
534 // Hence it has a size of 255 bytes, and therefore...
535 ASSERT_EQ(128u, DexRegisterMap::GetLiveBitMaskSize(number_of_dex_registers));
David Brazdilf677ebf2015-05-29 16:29:43 +0100536 StackMap stack_map0 = code_info.GetStackMapAt(0, encoding);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000537 DexRegisterMap dex_register_map0 =
David Brazdilf677ebf2015-05-29 16:29:43 +0100538 code_info.GetDexRegisterMapOf(stack_map0, encoding, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000539 ASSERT_EQ(127u, dex_register_map0.GetLocationMappingDataSize(number_of_dex_registers,
540 number_of_location_catalog_entries));
541 ASSERT_EQ(255u, dex_register_map0.Size());
542
David Brazdilf677ebf2015-05-29 16:29:43 +0100543 StackMap stack_map1 = code_info.GetStackMapAt(1, encoding);
544 ASSERT_TRUE(stack_map1.HasDexRegisterMap(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000545 // ...the offset of the second Dex register map (relative to the
546 // beginning of the Dex register maps region) is 255 (i.e.,
547 // kNoDexRegisterMapSmallEncoding).
David Brazdilf677ebf2015-05-29 16:29:43 +0100548 ASSERT_NE(stack_map1.GetDexRegisterMapOffset(encoding), StackMap::kNoDexRegisterMap);
549 ASSERT_EQ(stack_map1.GetDexRegisterMapOffset(encoding), 0xFFu);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000550}
551
Calin Juravle6ae70962015-03-18 16:31:28 +0000552TEST(StackMapTest, TestShareDexRegisterMap) {
553 ArenaPool pool;
554 ArenaAllocator arena(&pool);
555 StackMapStream stream(&arena);
556
557 ArenaBitVector sp_mask(&arena, 0, false);
558 uint32_t number_of_dex_registers = 2;
559 // First stack map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100560 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100561 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
562 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100563 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000564 // Second stack map, which should share the same dex register map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100565 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100566 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
567 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100568 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000569 // Third stack map (doesn't share the dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100570 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100571 stream.AddDexRegisterEntry(Kind::kInRegister, 2); // Short location.
572 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100573 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000574
Calin Juravle4f46ac52015-04-23 18:47:21 +0100575 size_t size = stream.PrepareForFillIn();
Calin Juravle6ae70962015-03-18 16:31:28 +0000576 void* memory = arena.Alloc(size, kArenaAllocMisc);
577 MemoryRegion region(memory, size);
578 stream.FillIn(region);
579
580 CodeInfo ci(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100581 StackMapEncoding encoding = ci.ExtractEncoding();
582
Calin Juravle6ae70962015-03-18 16:31:28 +0000583 // Verify first stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100584 StackMap sm0 = ci.GetStackMapAt(0, encoding);
585 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, encoding, number_of_dex_registers);
586 ASSERT_EQ(0, dex_registers0.GetMachineRegister(0, number_of_dex_registers, ci, encoding));
587 ASSERT_EQ(-2, dex_registers0.GetConstant(1, number_of_dex_registers, ci, encoding));
Calin Juravle6ae70962015-03-18 16:31:28 +0000588
589 // Verify second stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100590 StackMap sm1 = ci.GetStackMapAt(1, encoding);
591 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapOf(sm1, encoding, number_of_dex_registers);
592 ASSERT_EQ(0, dex_registers1.GetMachineRegister(0, number_of_dex_registers, ci, encoding));
593 ASSERT_EQ(-2, dex_registers1.GetConstant(1, number_of_dex_registers, ci, encoding));
Calin Juravle6ae70962015-03-18 16:31:28 +0000594
595 // Verify third stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100596 StackMap sm2 = ci.GetStackMapAt(2, encoding);
597 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapOf(sm2, encoding, number_of_dex_registers);
598 ASSERT_EQ(2, dex_registers2.GetMachineRegister(0, number_of_dex_registers, ci, encoding));
599 ASSERT_EQ(-2, dex_registers2.GetConstant(1, number_of_dex_registers, ci, encoding));
Calin Juravle6ae70962015-03-18 16:31:28 +0000600
601 // Verify dex register map offsets.
David Brazdilf677ebf2015-05-29 16:29:43 +0100602 ASSERT_EQ(sm0.GetDexRegisterMapOffset(encoding), sm1.GetDexRegisterMapOffset(encoding));
603 ASSERT_NE(sm0.GetDexRegisterMapOffset(encoding), sm2.GetDexRegisterMapOffset(encoding));
604 ASSERT_NE(sm1.GetDexRegisterMapOffset(encoding), sm2.GetDexRegisterMapOffset(encoding));
Calin Juravle6ae70962015-03-18 16:31:28 +0000605}
606
Roland Levillaina552e1c2015-03-26 15:01:03 +0000607TEST(StackMapTest, TestNoDexRegisterMap) {
608 ArenaPool pool;
609 ArenaAllocator arena(&pool);
610 StackMapStream stream(&arena);
611
612 ArenaBitVector sp_mask(&arena, 0, false);
613 uint32_t number_of_dex_registers = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100614 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
615 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000616
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000617 number_of_dex_registers = 1;
618 stream.BeginStackMapEntry(1, 67, 0x4, &sp_mask, number_of_dex_registers, 0);
619 stream.EndStackMapEntry();
620
Calin Juravle4f46ac52015-04-23 18:47:21 +0100621 size_t size = stream.PrepareForFillIn();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000622 void* memory = arena.Alloc(size, kArenaAllocMisc);
623 MemoryRegion region(memory, size);
624 stream.FillIn(region);
625
626 CodeInfo code_info(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100627 StackMapEncoding encoding = code_info.ExtractEncoding();
628 ASSERT_EQ(0u, encoding.NumberOfBytesForStackMask());
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000629 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000630
Roland Levillain1c1da432015-07-16 11:54:44 +0100631 uint32_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000632 ASSERT_EQ(0u, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100633 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(encoding);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000634 ASSERT_EQ(0u, location_catalog.Size());
635
David Brazdilf677ebf2015-05-29 16:29:43 +0100636 StackMap stack_map = code_info.GetStackMapAt(0, encoding);
637 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0, encoding)));
638 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64, encoding)));
639 ASSERT_EQ(0u, stack_map.GetDexPc(encoding));
640 ASSERT_EQ(64u, stack_map.GetNativePcOffset(encoding));
641 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000642
David Brazdilf677ebf2015-05-29 16:29:43 +0100643 ASSERT_FALSE(stack_map.HasDexRegisterMap(encoding));
644 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000645
646 stack_map = code_info.GetStackMapAt(1, encoding);
647 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1, encoding)));
648 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(67, encoding)));
649 ASSERT_EQ(1u, stack_map.GetDexPc(encoding));
650 ASSERT_EQ(67u, stack_map.GetNativePcOffset(encoding));
651 ASSERT_EQ(0x4u, stack_map.GetRegisterMask(encoding));
652
653 ASSERT_FALSE(stack_map.HasDexRegisterMap(encoding));
654 ASSERT_FALSE(stack_map.HasInlineInfo(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000655}
656
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100657TEST(StackMapTest, InlineTest) {
658 ArenaPool pool;
659 ArenaAllocator arena(&pool);
660 StackMapStream stream(&arena);
661
662 ArenaBitVector sp_mask1(&arena, 0, true);
663 sp_mask1.SetBit(2);
664 sp_mask1.SetBit(4);
665
666 // First stack map.
667 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, 2, 2);
668 stream.AddDexRegisterEntry(Kind::kInStack, 0);
669 stream.AddDexRegisterEntry(Kind::kConstant, 4);
670
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100671 stream.BeginInlineInfoEntry(42, 2, kStatic, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100672 stream.AddDexRegisterEntry(Kind::kInStack, 8);
673 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100674 stream.BeginInlineInfoEntry(82, 3, kStatic, 3);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100675 stream.AddDexRegisterEntry(Kind::kInStack, 16);
676 stream.AddDexRegisterEntry(Kind::kConstant, 20);
677 stream.AddDexRegisterEntry(Kind::kInRegister, 15);
678 stream.EndInlineInfoEntry();
679
680 stream.EndStackMapEntry();
681
682 // Second stack map.
683 stream.BeginStackMapEntry(2, 22, 0x3, &sp_mask1, 2, 3);
684 stream.AddDexRegisterEntry(Kind::kInStack, 56);
685 stream.AddDexRegisterEntry(Kind::kConstant, 0);
686
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100687 stream.BeginInlineInfoEntry(42, 2, kDirect, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100688 stream.AddDexRegisterEntry(Kind::kInStack, 12);
689 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100690 stream.BeginInlineInfoEntry(82, 3, kStatic, 3);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100691 stream.AddDexRegisterEntry(Kind::kInStack, 80);
692 stream.AddDexRegisterEntry(Kind::kConstant, 10);
693 stream.AddDexRegisterEntry(Kind::kInRegister, 5);
694 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100695 stream.BeginInlineInfoEntry(52, 5, kVirtual, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100696 stream.EndInlineInfoEntry();
697
698 stream.EndStackMapEntry();
699
700 // Third stack map.
701 stream.BeginStackMapEntry(4, 56, 0x3, &sp_mask1, 2, 0);
702 stream.AddDexRegisterEntry(Kind::kNone, 0);
703 stream.AddDexRegisterEntry(Kind::kConstant, 4);
704 stream.EndStackMapEntry();
705
706 // Fourth stack map.
707 stream.BeginStackMapEntry(6, 78, 0x3, &sp_mask1, 2, 3);
708 stream.AddDexRegisterEntry(Kind::kInStack, 56);
709 stream.AddDexRegisterEntry(Kind::kConstant, 0);
710
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100711 stream.BeginInlineInfoEntry(42, 2, kVirtual, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100712 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100713 stream.BeginInlineInfoEntry(52, 5, kInterface, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100714 stream.AddDexRegisterEntry(Kind::kInRegister, 2);
715 stream.EndInlineInfoEntry();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100716 stream.BeginInlineInfoEntry(52, 10, kStatic, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100717 stream.AddDexRegisterEntry(Kind::kNone, 0);
718 stream.AddDexRegisterEntry(Kind::kInRegister, 3);
719 stream.EndInlineInfoEntry();
720
721 stream.EndStackMapEntry();
722
723 size_t size = stream.PrepareForFillIn();
724 void* memory = arena.Alloc(size, kArenaAllocMisc);
725 MemoryRegion region(memory, size);
726 stream.FillIn(region);
727
728 CodeInfo ci(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100729 StackMapEncoding encoding = ci.ExtractEncoding();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100730
731 {
732 // Verify first stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100733 StackMap sm0 = ci.GetStackMapAt(0, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100734
David Brazdilf677ebf2015-05-29 16:29:43 +0100735 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, encoding, 2);
736 ASSERT_EQ(0, dex_registers0.GetStackOffsetInBytes(0, 2, ci, encoding));
737 ASSERT_EQ(4, dex_registers0.GetConstant(1, 2, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100738
David Brazdilf677ebf2015-05-29 16:29:43 +0100739 InlineInfo if0 = ci.GetInlineInfoOf(sm0, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100740 ASSERT_EQ(2u, if0.GetDepth());
741 ASSERT_EQ(2u, if0.GetDexPcAtDepth(0));
742 ASSERT_EQ(42u, if0.GetMethodIndexAtDepth(0));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100743 ASSERT_EQ(kStatic, if0.GetInvokeTypeAtDepth(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100744 ASSERT_EQ(3u, if0.GetDexPcAtDepth(1));
745 ASSERT_EQ(82u, if0.GetMethodIndexAtDepth(1));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100746 ASSERT_EQ(kStatic, if0.GetInvokeTypeAtDepth(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100747
David Brazdilf677ebf2015-05-29 16:29:43 +0100748 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if0, encoding, 1);
749 ASSERT_EQ(8, dex_registers1.GetStackOffsetInBytes(0, 1, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100750
David Brazdilf677ebf2015-05-29 16:29:43 +0100751 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if0, encoding, 3);
752 ASSERT_EQ(16, dex_registers2.GetStackOffsetInBytes(0, 3, ci, encoding));
753 ASSERT_EQ(20, dex_registers2.GetConstant(1, 3, ci, encoding));
754 ASSERT_EQ(15, dex_registers2.GetMachineRegister(2, 3, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100755 }
756
757 {
758 // Verify second stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100759 StackMap sm1 = ci.GetStackMapAt(1, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100760
David Brazdilf677ebf2015-05-29 16:29:43 +0100761 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm1, encoding, 2);
762 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0, 2, ci, encoding));
763 ASSERT_EQ(0, dex_registers0.GetConstant(1, 2, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100764
David Brazdilf677ebf2015-05-29 16:29:43 +0100765 InlineInfo if1 = ci.GetInlineInfoOf(sm1, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100766 ASSERT_EQ(3u, if1.GetDepth());
767 ASSERT_EQ(2u, if1.GetDexPcAtDepth(0));
768 ASSERT_EQ(42u, if1.GetMethodIndexAtDepth(0));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100769 ASSERT_EQ(kDirect, if1.GetInvokeTypeAtDepth(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100770 ASSERT_EQ(3u, if1.GetDexPcAtDepth(1));
771 ASSERT_EQ(82u, if1.GetMethodIndexAtDepth(1));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100772 ASSERT_EQ(kStatic, if1.GetInvokeTypeAtDepth(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100773 ASSERT_EQ(5u, if1.GetDexPcAtDepth(2));
774 ASSERT_EQ(52u, if1.GetMethodIndexAtDepth(2));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100775 ASSERT_EQ(kVirtual, if1.GetInvokeTypeAtDepth(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100776
David Brazdilf677ebf2015-05-29 16:29:43 +0100777 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if1, encoding, 1);
778 ASSERT_EQ(12, dex_registers1.GetStackOffsetInBytes(0, 1, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100779
David Brazdilf677ebf2015-05-29 16:29:43 +0100780 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if1, encoding, 3);
781 ASSERT_EQ(80, dex_registers2.GetStackOffsetInBytes(0, 3, ci, encoding));
782 ASSERT_EQ(10, dex_registers2.GetConstant(1, 3, ci, encoding));
783 ASSERT_EQ(5, dex_registers2.GetMachineRegister(2, 3, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100784
785 ASSERT_FALSE(if1.HasDexRegisterMapAtDepth(2));
786 }
787
788 {
789 // Verify third stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100790 StackMap sm2 = ci.GetStackMapAt(2, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100791
David Brazdilf677ebf2015-05-29 16:29:43 +0100792 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm2, encoding, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100793 ASSERT_FALSE(dex_registers0.IsDexRegisterLive(0));
David Brazdilf677ebf2015-05-29 16:29:43 +0100794 ASSERT_EQ(4, dex_registers0.GetConstant(1, 2, ci, encoding));
795 ASSERT_FALSE(sm2.HasInlineInfo(encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100796 }
797
798 {
799 // Verify fourth stack map.
David Brazdilf677ebf2015-05-29 16:29:43 +0100800 StackMap sm3 = ci.GetStackMapAt(3, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100801
David Brazdilf677ebf2015-05-29 16:29:43 +0100802 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm3, encoding, 2);
803 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0, 2, ci, encoding));
804 ASSERT_EQ(0, dex_registers0.GetConstant(1, 2, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100805
David Brazdilf677ebf2015-05-29 16:29:43 +0100806 InlineInfo if2 = ci.GetInlineInfoOf(sm3, encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100807 ASSERT_EQ(3u, if2.GetDepth());
808 ASSERT_EQ(2u, if2.GetDexPcAtDepth(0));
809 ASSERT_EQ(42u, if2.GetMethodIndexAtDepth(0));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100810 ASSERT_EQ(kVirtual, if2.GetInvokeTypeAtDepth(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100811 ASSERT_EQ(5u, if2.GetDexPcAtDepth(1));
812 ASSERT_EQ(52u, if2.GetMethodIndexAtDepth(1));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100813 ASSERT_EQ(kInterface, if2.GetInvokeTypeAtDepth(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100814 ASSERT_EQ(10u, if2.GetDexPcAtDepth(2));
815 ASSERT_EQ(52u, if2.GetMethodIndexAtDepth(2));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100816 ASSERT_EQ(kStatic, if2.GetInvokeTypeAtDepth(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100817
818 ASSERT_FALSE(if2.HasDexRegisterMapAtDepth(0));
819
David Brazdilf677ebf2015-05-29 16:29:43 +0100820 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(1, if2, encoding, 1);
821 ASSERT_EQ(2, dex_registers1.GetMachineRegister(0, 1, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100822
David Brazdilf677ebf2015-05-29 16:29:43 +0100823 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(2, if2, encoding, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100824 ASSERT_FALSE(dex_registers2.IsDexRegisterLive(0));
David Brazdilf677ebf2015-05-29 16:29:43 +0100825 ASSERT_EQ(3, dex_registers2.GetMachineRegister(1, 2, ci, encoding));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100826 }
827}
828
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100829} // namespace art