Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace art { |
| 24 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 25 | static bool SameBits(MemoryRegion region, const BitVector& bit_vector) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 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 | |
| 34 | TEST(StackMapTest, Test1) { |
| 35 | ArenaPool pool; |
| 36 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 37 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 38 | |
| 39 | ArenaBitVector sp_mask(&arena, 0, false); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 40 | ArenaBitVector live_registers_mask(&arena, 0, true); |
| 41 | live_registers_mask.SetBit(0); |
| 42 | live_registers_mask.SetBit(1); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 43 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 44 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0, &live_registers_mask); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 45 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0); |
| 46 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 47 | |
| 48 | size_t size = stream.ComputeNeededSize(); |
| 49 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 50 | MemoryRegion region(memory, size); |
| 51 | stream.FillIn(region); |
| 52 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 53 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 54 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 55 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 56 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 57 | StackMap stack_map = code_info.GetStackMapAt(0); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 58 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 59 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | ASSERT_EQ(0u, stack_map.GetDexPc()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 61 | ASSERT_EQ(64u, stack_map.GetNativePcOffset()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 62 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 63 | |
| 64 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 65 | ASSERT_TRUE(SameBits(stack_mask, sp_mask)); |
| 66 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 67 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 68 | DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 69 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 72 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 78 | |
| 79 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(StackMapTest, Test2) { |
| 83 | ArenaPool pool; |
| 84 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 85 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 86 | |
| 87 | ArenaBitVector sp_mask1(&arena, 0, true); |
| 88 | sp_mask1.SetBit(2); |
| 89 | sp_mask1.SetBit(4); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 90 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 91 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 95 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0); |
| 96 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 97 | 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 Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 103 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 107 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, 18); |
| 108 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, 3); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 109 | |
| 110 | size_t size = stream.ComputeNeededSize(); |
| 111 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 112 | MemoryRegion region(memory, size); |
| 113 | stream.FillIn(region); |
| 114 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 115 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 116 | ASSERT_EQ(1u, code_info.GetStackMaskSize()); |
| 117 | ASSERT_EQ(2u, code_info.GetNumberOfStackMaps()); |
| 118 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 119 | // First stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 120 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 127 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 128 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 129 | ASSERT_TRUE(SameBits(stack_mask, sp_mask1)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 130 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 131 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
| 132 | DexRegisterMap dex_registers = |
| 133 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 134 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 139 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 145 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 146 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 152 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 153 | // Second stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 154 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 161 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 162 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 163 | ASSERT_TRUE(SameBits(stack_mask, sp_mask2)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 164 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 165 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
| 166 | DexRegisterMap dex_registers = |
| 167 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 168 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 173 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 179 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 180 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
| 181 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 184 | TEST(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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 213 | } // namespace art |