blob: 4606bd63c98f2bb6e4bada9be4dac1ef3a95f43f [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"
18#include "stack_map_stream.h"
19#include "utils/arena_bit_vector.h"
20
21#include "gtest/gtest.h"
22
23namespace art {
24
Roland Levillaina2d8ec62015-03-12 15:25:29 +000025static bool SameBits(MemoryRegion region, const BitVector& bit_vector) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010026 for (size_t i = 0; i < region.size_in_bits(); ++i) {
27 if (region.LoadBit(i) != bit_vector.IsBitSet(i)) {
28 return false;
29 }
30 }
31 return true;
32}
33
34TEST(StackMapTest, Test1) {
35 ArenaPool pool;
36 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010037 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010038
39 ArenaBitVector sp_mask(&arena, 0, false);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000040 ArenaBitVector live_registers_mask(&arena, 0, true);
41 live_registers_mask.SetBit(0);
42 live_registers_mask.SetBit(1);
Roland Levillain12baf472015-03-05 12:41:42 +000043 size_t number_of_dex_registers = 2;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000044 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0, &live_registers_mask);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000045 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0);
46 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010047
48 size_t size = stream.ComputeNeededSize();
49 void* memory = arena.Alloc(size, kArenaAllocMisc);
50 MemoryRegion region(memory, size);
51 stream.FillIn(region);
52
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010054 ASSERT_EQ(0u, code_info.GetStackMaskSize());
55 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
56
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010058 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +010061 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010062 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010063
64 MemoryRegion stack_mask = stack_map.GetStackMask();
65 ASSERT_TRUE(SameBits(stack_mask, sp_mask));
66
Roland Levillain442b46a2015-02-18 16:54:21 +000067 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina2d8ec62015-03-12 15:25:29 +000068 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000069 ASSERT_EQ(7u, dex_registers.Size());
70 DexRegisterLocation location0 = dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
71 DexRegisterLocation location1 = dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000072 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
73 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
74 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
75 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
76 ASSERT_EQ(0, location0.GetValue());
77 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +000078
79 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010080}
81
82TEST(StackMapTest, Test2) {
83 ArenaPool pool;
84 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010086
87 ArenaBitVector sp_mask1(&arena, 0, true);
88 sp_mask1.SetBit(2);
89 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +000090 size_t number_of_dex_registers = 2;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000091 ArenaBitVector live_registers_mask1(&arena, 0, true);
92 live_registers_mask1.SetBit(0);
93 live_registers_mask1.SetBit(1);
94 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2, &live_registers_mask1);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000095 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0);
96 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010097 stream.AddInlineInfoEntry(42);
98 stream.AddInlineInfoEntry(82);
99
100 ArenaBitVector sp_mask2(&arena, 0, true);
101 sp_mask2.SetBit(3);
102 sp_mask1.SetBit(8);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000103 ArenaBitVector live_registers_mask2(&arena, 0, true);
104 live_registers_mask2.SetBit(0);
105 live_registers_mask2.SetBit(1);
106 stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0, &live_registers_mask2);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000107 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, 18);
108 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, 3);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100109
110 size_t size = stream.ComputeNeededSize();
111 void* memory = arena.Alloc(size, kArenaAllocMisc);
112 MemoryRegion region(memory, size);
113 stream.FillIn(region);
114
Nicolas Geoffray39468442014-09-02 15:17:15 +0100115 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100116 ASSERT_EQ(1u, code_info.GetStackMaskSize());
117 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
118
Roland Levillain12baf472015-03-05 12:41:42 +0000119 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000120 {
121 StackMap stack_map = code_info.GetStackMapAt(0);
122 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
123 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
124 ASSERT_EQ(0u, stack_map.GetDexPc());
125 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
126 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100127
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000128 MemoryRegion stack_mask = stack_map.GetStackMask();
129 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100130
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000131 ASSERT_TRUE(stack_map.HasDexRegisterMap());
132 DexRegisterMap dex_registers =
133 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000134 ASSERT_EQ(7u, dex_registers.Size());
135 DexRegisterLocation location0 =
136 dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
137 DexRegisterLocation location1 =
138 dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000139 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
140 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
141 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
142 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
143 ASSERT_EQ(0, location0.GetValue());
144 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100145
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000146 ASSERT_TRUE(stack_map.HasInlineInfo());
147 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
148 ASSERT_EQ(2u, inline_info.GetDepth());
149 ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0));
150 ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1));
151 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100152
Roland Levillain12baf472015-03-05 12:41:42 +0000153 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000154 {
155 StackMap stack_map = code_info.GetStackMapAt(1);
156 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
157 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
158 ASSERT_EQ(1u, stack_map.GetDexPc());
159 ASSERT_EQ(128u, stack_map.GetNativePcOffset());
160 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100161
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000162 MemoryRegion stack_mask = stack_map.GetStackMask();
163 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100164
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000165 ASSERT_TRUE(stack_map.HasDexRegisterMap());
166 DexRegisterMap dex_registers =
167 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000168 ASSERT_EQ(3u, dex_registers.Size());
169 DexRegisterLocation location0 =
170 dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
171 DexRegisterLocation location1 =
172 dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000173 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetKind());
174 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetKind());
175 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetInternalKind());
176 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetInternalKind());
177 ASSERT_EQ(18, location0.GetValue());
178 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000179
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000180 ASSERT_FALSE(stack_map.HasInlineInfo());
181 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100182}
183
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000184TEST(StackMapTest, TestNonLiveDexRegisters) {
185 ArenaPool pool;
186 ArenaAllocator arena(&pool);
187 StackMapStream stream(&arena);
188
189 ArenaBitVector sp_mask(&arena, 0, false);
190 ArenaBitVector live_registers_mask(&arena, 0, true);
191 live_registers_mask.SetBit(1);
192 uint32_t number_of_dex_registers = 2;
193 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0, &live_registers_mask);
194 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2);
195
196 size_t size = stream.ComputeNeededSize();
197 void* memory = arena.Alloc(size, kArenaAllocMisc);
198 MemoryRegion region(memory, size);
199 stream.FillIn(region);
200
201 CodeInfo code_info(region);
202 StackMap stack_map = code_info.GetStackMapAt(0);
203 ASSERT_TRUE(stack_map.HasDexRegisterMap());
204 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, 2);
205 ASSERT_EQ(DexRegisterLocation::Kind::kNone,
206 dex_registers.GetLocationKind(0, number_of_dex_registers));
207 ASSERT_EQ(DexRegisterLocation::Kind::kConstant,
208 dex_registers.GetLocationKind(1, number_of_dex_registers));
209 ASSERT_EQ(-2, dex_registers.GetConstant(1, number_of_dex_registers));
210 ASSERT_FALSE(stack_map.HasInlineInfo());
211}
212
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100213} // namespace art