blob: 744fb45fffcfd17390720842da1bc15cb8dccc0c [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
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);
40 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, 2, 0);
41 stream.AddDexRegisterEntry(DexRegisterMap::kInStack, 0);
42 stream.AddDexRegisterEntry(DexRegisterMap::kConstant, -2);
43
44 size_t size = stream.ComputeNeededSize();
45 void* memory = arena.Alloc(size, kArenaAllocMisc);
46 MemoryRegion region(memory, size);
47 stream.FillIn(region);
48
Nicolas Geoffray39468442014-09-02 15:17:15 +010049 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010050 ASSERT_EQ(0u, code_info.GetStackMaskSize());
51 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
52
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010054 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010056 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010058 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
59 ASSERT_FALSE(stack_map.HasInlineInfo());
60
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());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010065 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, 2);
66 ASSERT_EQ(DexRegisterMap::kInStack, dex_registers.GetLocationKind(0));
67 ASSERT_EQ(DexRegisterMap::kConstant, dex_registers.GetLocationKind(1));
68 ASSERT_EQ(0, dex_registers.GetValue(0));
69 ASSERT_EQ(-2, dex_registers.GetValue(1));
70}
71
72TEST(StackMapTest, Test2) {
73 ArenaPool pool;
74 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010076
77 ArenaBitVector sp_mask1(&arena, 0, true);
78 sp_mask1.SetBit(2);
79 sp_mask1.SetBit(4);
80 stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, 2, 2);
81 stream.AddDexRegisterEntry(DexRegisterMap::kInStack, 0);
82 stream.AddDexRegisterEntry(DexRegisterMap::kConstant, -2);
83 stream.AddInlineInfoEntry(42);
84 stream.AddInlineInfoEntry(82);
85
86 ArenaBitVector sp_mask2(&arena, 0, true);
87 sp_mask2.SetBit(3);
88 sp_mask1.SetBit(8);
89 stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, 1, 0);
90 stream.AddDexRegisterEntry(DexRegisterMap::kInRegister, 0);
91
92 size_t size = stream.ComputeNeededSize();
93 void* memory = arena.Alloc(size, kArenaAllocMisc);
94 MemoryRegion region(memory, size);
95 stream.FillIn(region);
96
Nicolas Geoffray39468442014-09-02 15:17:15 +010097 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010098 ASSERT_EQ(1u, code_info.GetStackMaskSize());
99 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
100
Nicolas Geoffray39468442014-09-02 15:17:15 +0100101 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100102 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100103 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100104 ASSERT_EQ(0u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100105 ASSERT_EQ(64u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100106 ASSERT_EQ(0x3u, stack_map.GetRegisterMask());
107
108 MemoryRegion stack_mask = stack_map.GetStackMask();
109 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
110
Roland Levillain442b46a2015-02-18 16:54:21 +0000111 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100112 DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, 2);
113 ASSERT_EQ(DexRegisterMap::kInStack, dex_registers.GetLocationKind(0));
114 ASSERT_EQ(DexRegisterMap::kConstant, dex_registers.GetLocationKind(1));
115 ASSERT_EQ(0, dex_registers.GetValue(0));
116 ASSERT_EQ(-2, dex_registers.GetValue(1));
117
118 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
119 ASSERT_EQ(2u, inline_info.GetDepth());
120 ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0));
121 ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1));
122
123 stack_map = code_info.GetStackMapAt(1);
124 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100126 ASSERT_EQ(1u, stack_map.GetDexPc());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100127 ASSERT_EQ(128u, stack_map.GetNativePcOffset());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100128 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask());
129
130 stack_mask = stack_map.GetStackMask();
131 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
132
133 ASSERT_FALSE(stack_map.HasInlineInfo());
134}
135
136} // namespace art