blob: 3a5f80686dca2140850d2f2b6e2f4fb825ea4901 [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
Roland Levillaina2d8ec62015-03-12 15:25:29 +000034static size_t ComputeDexRegisterMapSize(const DexRegisterMap& dex_registers,
35 size_t number_of_dex_registers) {
36 return dex_registers.FindLocationOffset(number_of_dex_registers);
Roland Levillain12baf472015-03-05 12:41:42 +000037}
38
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010039TEST(StackMapTest, Test1) {
40 ArenaPool pool;
41 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010042 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010043
44 ArenaBitVector sp_mask(&arena, 0, false);
Roland Levillain12baf472015-03-05 12:41:42 +000045 size_t number_of_dex_registers = 2;
46 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000047 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0);
48 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010049
50 size_t size = stream.ComputeNeededSize();
51 void* memory = arena.Alloc(size, kArenaAllocMisc);
52 MemoryRegion region(memory, size);
53 stream.FillIn(region);
54
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010056 ASSERT_EQ(0u, code_info.GetStackMaskSize());
57 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
58
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010061 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010062 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010064 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010065
66 MemoryRegion stack_mask = stack_map.GetStackMask();
67 ASSERT_TRUE(SameBits(stack_mask, sp_mask));
68
Roland Levillain442b46a2015-02-18 16:54:21 +000069 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina2d8ec62015-03-12 15:25:29 +000070 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
71 ASSERT_EQ(6u, dex_registers.Size());
72 ASSERT_EQ(6u, ComputeDexRegisterMapSize(dex_registers, number_of_dex_registers));
73 DexRegisterLocation location0 = dex_registers.GetLocationKindAndValue(0);
74 DexRegisterLocation location1 = dex_registers.GetLocationKindAndValue(1);
75 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
76 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
77 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
78 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
79 ASSERT_EQ(0, location0.GetValue());
80 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +000081
82 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010083}
84
85TEST(StackMapTest, Test2) {
86 ArenaPool pool;
87 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010089
90 ArenaBitVector sp_mask1(&arena, 0, true);
91 sp_mask1.SetBit(2);
92 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +000093 size_t number_of_dex_registers = 2;
94 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
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);
Roland Levillain12baf472015-03-05 12:41:42 +0000103 stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000104 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, 18);
105 stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, 3);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100106
107 size_t size = stream.ComputeNeededSize();
108 void* memory = arena.Alloc(size, kArenaAllocMisc);
109 MemoryRegion region(memory, size);
110 stream.FillIn(region);
111
Nicolas Geoffray39468442014-09-02 15:17:15 +0100112 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100113 ASSERT_EQ(1u, code_info.GetStackMaskSize());
114 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
115
Roland Levillain12baf472015-03-05 12:41:42 +0000116 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000117 {
118 StackMap stack_map = code_info.GetStackMapAt(0);
119 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
120 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
121 ASSERT_EQ(0u, stack_map.GetDexPc());
122 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
123 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100124
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000125 MemoryRegion stack_mask = stack_map.GetStackMask();
126 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100127
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000128 ASSERT_TRUE(stack_map.HasDexRegisterMap());
129 DexRegisterMap dex_registers =
130 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
131 ASSERT_EQ(6u, dex_registers.Size());
132 ASSERT_EQ(6u, ComputeDexRegisterMapSize(dex_registers, number_of_dex_registers));
133 DexRegisterLocation location0 = dex_registers.GetLocationKindAndValue(0);
134 DexRegisterLocation location1 = dex_registers.GetLocationKindAndValue(1);
135 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetKind());
136 ASSERT_EQ(DexRegisterLocation::Kind::kConstant, location1.GetKind());
137 ASSERT_EQ(DexRegisterLocation::Kind::kInStack, location0.GetInternalKind());
138 ASSERT_EQ(DexRegisterLocation::Kind::kConstantLargeValue, location1.GetInternalKind());
139 ASSERT_EQ(0, location0.GetValue());
140 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100141
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000142 ASSERT_TRUE(stack_map.HasInlineInfo());
143 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
144 ASSERT_EQ(2u, inline_info.GetDepth());
145 ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0));
146 ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1));
147 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100148
Roland Levillain12baf472015-03-05 12:41:42 +0000149 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000150 {
151 StackMap stack_map = code_info.GetStackMapAt(1);
152 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
153 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
154 ASSERT_EQ(1u, stack_map.GetDexPc());
155 ASSERT_EQ(128u, stack_map.GetNativePcOffset());
156 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100157
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000158 MemoryRegion stack_mask = stack_map.GetStackMask();
159 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100160
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000161 ASSERT_TRUE(stack_map.HasDexRegisterMap());
162 DexRegisterMap dex_registers =
163 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
164 ASSERT_EQ(2u, dex_registers.Size());
165 ASSERT_EQ(2u, ComputeDexRegisterMapSize(dex_registers, number_of_dex_registers));
166 DexRegisterLocation location0 = dex_registers.GetLocationKindAndValue(0);
167 DexRegisterLocation location1 = dex_registers.GetLocationKindAndValue(1);
168 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetKind());
169 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetKind());
170 ASSERT_EQ(DexRegisterLocation::Kind::kInRegister, location0.GetInternalKind());
171 ASSERT_EQ(DexRegisterLocation::Kind::kInFpuRegister, location1.GetInternalKind());
172 ASSERT_EQ(18, location0.GetValue());
173 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000174
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000175 ASSERT_FALSE(stack_map.HasInlineInfo());
176 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100177}
178
179} // namespace art