blob: 5b025106ac0efaea1f5825ad1cae0c7d055e56c3 [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
25bool SameBits(MemoryRegion region, const BitVector& bit_vector) {
26 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 Levillain12baf472015-03-05 12:41:42 +000034size_t ComputeDexRegisterMapSize(size_t number_of_dex_registers) {
35 return DexRegisterMap::kFixedSize
36 + number_of_dex_registers * DexRegisterMap::SingleEntrySize();
37}
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);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010047 stream.AddDexRegisterEntry(DexRegisterMap::kInStack, 0);
48 stream.AddDexRegisterEntry(DexRegisterMap::kConstant, -2);
49
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 Levillain12baf472015-03-05 12:41:42 +000070 DexRegisterMap dex_registers =
71 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
72 ASSERT_EQ(16u, dex_registers.Size());
73 ASSERT_EQ(16u, ComputeDexRegisterMapSize(number_of_dex_registers));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010074 ASSERT_EQ(DexRegisterMap::kInStack, dex_registers.GetLocationKind(0));
75 ASSERT_EQ(DexRegisterMap::kConstant, dex_registers.GetLocationKind(1));
76 ASSERT_EQ(0, dex_registers.GetValue(0));
77 ASSERT_EQ(-2, dex_registers.GetValue(1));
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;
91 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010092 stream.AddDexRegisterEntry(DexRegisterMap::kInStack, 0);
93 stream.AddDexRegisterEntry(DexRegisterMap::kConstant, -2);
94 stream.AddInlineInfoEntry(42);
95 stream.AddInlineInfoEntry(82);
96
97 ArenaBitVector sp_mask2(&arena, 0, true);
98 sp_mask2.SetBit(3);
99 sp_mask1.SetBit(8);
Roland Levillain12baf472015-03-05 12:41:42 +0000100 stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
101 stream.AddDexRegisterEntry(DexRegisterMap::kInRegister, 18);
102 stream.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, 3);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100103
104 size_t size = stream.ComputeNeededSize();
105 void* memory = arena.Alloc(size, kArenaAllocMisc);
106 MemoryRegion region(memory, size);
107 stream.FillIn(region);
108
Nicolas Geoffray39468442014-09-02 15:17:15 +0100109 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100110 ASSERT_EQ(1u, code_info.GetStackMaskSize());
111 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
112
Roland Levillain12baf472015-03-05 12:41:42 +0000113 // First stack map.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100114 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100115 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100116 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100117 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100118 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100119 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
120
121 MemoryRegion stack_mask = stack_map.GetStackMask();
122 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
123
Roland Levillain442b46a2015-02-18 16:54:21 +0000124 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillain12baf472015-03-05 12:41:42 +0000125 DexRegisterMap dex_registers =
126 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
127 ASSERT_EQ(16u, dex_registers.Size());
128 ASSERT_EQ(16u, ComputeDexRegisterMapSize(number_of_dex_registers));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100129 ASSERT_EQ(DexRegisterMap::kInStack, dex_registers.GetLocationKind(0));
130 ASSERT_EQ(DexRegisterMap::kConstant, dex_registers.GetLocationKind(1));
131 ASSERT_EQ(0, dex_registers.GetValue(0));
132 ASSERT_EQ(-2, dex_registers.GetValue(1));
133
Roland Levillain12baf472015-03-05 12:41:42 +0000134 ASSERT_TRUE(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100135 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
136 ASSERT_EQ(2u, inline_info.GetDepth());
137 ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0));
138 ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1));
139
Roland Levillain12baf472015-03-05 12:41:42 +0000140 // Second stack map.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100141 stack_map = code_info.GetStackMapAt(1);
142 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144 ASSERT_EQ(1u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100145 ASSERT_EQ(128u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100146 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask());
147
148 stack_mask = stack_map.GetStackMask();
149 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
150
Roland Levillain12baf472015-03-05 12:41:42 +0000151 ASSERT_TRUE(stack_map.HasDexRegisterMap());
152 dex_registers =
153 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
154 ASSERT_EQ(16u, dex_registers.Size());
155 ASSERT_EQ(16u, ComputeDexRegisterMapSize(number_of_dex_registers));
156 ASSERT_EQ(DexRegisterMap::kInRegister, dex_registers.GetLocationKind(0));
157 ASSERT_EQ(DexRegisterMap::kInFpuRegister, dex_registers.GetLocationKind(1));
158 ASSERT_EQ(18, dex_registers.GetValue(0));
159 ASSERT_EQ(3, dex_registers.GetValue(1));
160
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100161 ASSERT_FALSE(stack_map.HasInlineInfo());
162}
163
164} // namespace art