blob: 87ac2e79e916d1e7de40862e24fdaef83e18b60b [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);
Roland Levillain12baf472015-03-05 12:41:42 +000040 size_t number_of_dex_registers = 2;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000041 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
42 stream.AddDexRegisterEntry(0, DexRegisterLocation::Kind::kInStack, 0);
43 stream.AddDexRegisterEntry(1, DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010044
45 size_t size = stream.ComputeNeededSize();
46 void* memory = arena.Alloc(size, kArenaAllocMisc);
47 MemoryRegion region(memory, size);
48 stream.FillIn(region);
49
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010051 ASSERT_EQ(0u, code_info.GetStackMaskSize());
52 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
53
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010055 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010056 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010057 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +010058 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010059 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060
61 MemoryRegion stack_mask = stack_map.GetStackMask();
62 ASSERT_TRUE(SameBits(stack_mask, sp_mask));
63
Roland Levillain442b46a2015-02-18 16:54:21 +000064 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina2d8ec62015-03-12 15:25:29 +000065 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000066 ASSERT_EQ(7u, dex_registers.Size());
67 DexRegisterLocation location0 = dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
68 DexRegisterLocation location1 = dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000069 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
70 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
71 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
72 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
73 ASSERT_EQ(0, location0.GetValue());
74 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +000075
76 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010077}
78
79TEST(StackMapTest, Test2) {
80 ArenaPool pool;
81 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010083
84 ArenaBitVector sp_mask1(&arena, 0, true);
85 sp_mask1.SetBit(2);
86 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +000087 size_t number_of_dex_registers = 2;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000088 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
89 stream.AddDexRegisterEntry(0, DexRegisterLocation::Kind::kInStack, 0);
90 stream.AddDexRegisterEntry(1, DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010091 stream.AddInlineInfoEntry(42);
92 stream.AddInlineInfoEntry(82);
93
94 ArenaBitVector sp_mask2(&arena, 0, true);
95 sp_mask2.SetBit(3);
96 sp_mask1.SetBit(8);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000097 stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
98 stream.AddDexRegisterEntry(0, DexRegisterLocation::Kind::kInRegister, 18);
99 stream.AddDexRegisterEntry(1, DexRegisterLocation::Kind::kInFpuRegister, 3);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100100
101 size_t size = stream.ComputeNeededSize();
102 void* memory = arena.Alloc(size, kArenaAllocMisc);
103 MemoryRegion region(memory, size);
104 stream.FillIn(region);
105
Nicolas Geoffray39468442014-09-02 15:17:15 +0100106 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100107 ASSERT_EQ(1u, code_info.GetStackMaskSize());
108 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
109
Roland Levillain12baf472015-03-05 12:41:42 +0000110 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000111 {
112 StackMap stack_map = code_info.GetStackMapAt(0);
113 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
114 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
115 ASSERT_EQ(0u, stack_map.GetDexPc());
116 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
117 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100118
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000119 MemoryRegion stack_mask = stack_map.GetStackMask();
120 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100121
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000122 ASSERT_TRUE(stack_map.HasDexRegisterMap());
123 DexRegisterMap dex_registers =
124 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000125 ASSERT_EQ(7u, dex_registers.Size());
126 DexRegisterLocation location0 =
127 dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
128 DexRegisterLocation location1 =
129 dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000130 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
131 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
132 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
133 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
134 ASSERT_EQ(0, location0.GetValue());
135 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000137 ASSERT_TRUE(stack_map.HasInlineInfo());
138 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
139 ASSERT_EQ(2u, inline_info.GetDepth());
140 ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0));
141 ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1));
142 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100143
Roland Levillain12baf472015-03-05 12:41:42 +0000144 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000145 {
146 StackMap stack_map = code_info.GetStackMapAt(1);
147 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
148 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
149 ASSERT_EQ(1u, stack_map.GetDexPc());
150 ASSERT_EQ(128u, stack_map.GetNativePcOffset());
151 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100152
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000153 MemoryRegion stack_mask = stack_map.GetStackMask();
154 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100155
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000156 ASSERT_TRUE(stack_map.HasDexRegisterMap());
157 DexRegisterMap dex_registers =
158 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000159 ASSERT_EQ(3u, dex_registers.Size());
160 DexRegisterLocation location0 =
161 dex_registers.GetLocationKindAndValue(0, number_of_dex_registers);
162 DexRegisterLocation location1 =
163 dex_registers.GetLocationKindAndValue(1, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000164 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetKind());
165 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetKind());
166 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetInternalKind());
167 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetInternalKind());
168 ASSERT_EQ(18, location0.GetValue());
169 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000170
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000171 ASSERT_FALSE(stack_map.HasInlineInfo());
172 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100173}
174
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000175TEST(StackMapTest, TestNonLiveDexRegisters) {
176 ArenaPool pool;
177 ArenaAllocator arena(&pool);
178 StackMapStream stream(&arena);
179
180 ArenaBitVector sp_mask(&arena, 0, false);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000181 uint32_t number_of_dex_registers = 2;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000182 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
183 stream.AddDexRegisterEntry(0, DexRegisterLocation::Kind::kNone, 0);
184 stream.AddDexRegisterEntry(1, DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000185
186 size_t size = stream.ComputeNeededSize();
187 void* memory = arena.Alloc(size, kArenaAllocMisc);
188 MemoryRegion region(memory, size);
189 stream.FillIn(region);
190
191 CodeInfo code_info(region);
192 StackMap stack_map = code_info.GetStackMapAt(0);
193 ASSERT_TRUE(stack_map.HasDexRegisterMap());
194 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, 2);
195 ASSERT_EQ(DexRegisterLocation::Kind::kNone,
196 dex_registers.GetLocationKind(0, number_of_dex_registers));
197 ASSERT_EQ(DexRegisterLocation::Kind::kConstant,
198 dex_registers.GetLocationKind(1, number_of_dex_registers));
199 ASSERT_EQ(-2, dex_registers.GetConstant(1, number_of_dex_registers));
200 ASSERT_FALSE(stack_map.HasInlineInfo());
201}
202
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100203} // namespace art