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); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 40 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame^] | 41 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 44 | |
| 45 | size_t size = stream.ComputeNeededSize(); |
| 46 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 47 | MemoryRegion region(memory, size); |
| 48 | stream.FillIn(region); |
| 49 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 50 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 51 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 52 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 53 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 54 | StackMap stack_map = code_info.GetStackMapAt(0); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 55 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 56 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 57 | ASSERT_EQ(0u, stack_map.GetDexPc()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 58 | ASSERT_EQ(64u, stack_map.GetNativePcOffset()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 59 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | |
| 61 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 62 | ASSERT_TRUE(SameBits(stack_mask, sp_mask)); |
| 63 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 64 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 65 | DexRegisterMap dex_registers = code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 66 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 69 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 75 | |
| 76 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(StackMapTest, Test2) { |
| 80 | ArenaPool pool; |
| 81 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 82 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 83 | |
| 84 | ArenaBitVector sp_mask1(&arena, 0, true); |
| 85 | sp_mask1.SetBit(2); |
| 86 | sp_mask1.SetBit(4); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 87 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame^] | 88 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 91 | 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 Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame^] | 97 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 100 | |
| 101 | size_t size = stream.ComputeNeededSize(); |
| 102 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 103 | MemoryRegion region(memory, size); |
| 104 | stream.FillIn(region); |
| 105 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 106 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 107 | ASSERT_EQ(1u, code_info.GetStackMaskSize()); |
| 108 | ASSERT_EQ(2u, code_info.GetNumberOfStackMaps()); |
| 109 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 110 | // First stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 111 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 118 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 119 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 120 | ASSERT_TRUE(SameBits(stack_mask, sp_mask1)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 121 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 122 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
| 123 | DexRegisterMap dex_registers = |
| 124 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 125 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 130 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 136 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 137 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 143 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 144 | // Second stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 145 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 152 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 153 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 154 | ASSERT_TRUE(SameBits(stack_mask, sp_mask2)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 155 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 156 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
| 157 | DexRegisterMap dex_registers = |
| 158 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 159 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 164 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 170 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 171 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
| 172 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 175 | TEST(StackMapTest, TestNonLiveDexRegisters) { |
| 176 | ArenaPool pool; |
| 177 | ArenaAllocator arena(&pool); |
| 178 | StackMapStream stream(&arena); |
| 179 | |
| 180 | ArenaBitVector sp_mask(&arena, 0, false); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 181 | uint32_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame^] | 182 | 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 Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 185 | |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 203 | } // namespace art |