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 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 34 | static size_t ComputeDexRegisterMapSize(const DexRegisterMap& dex_registers, |
| 35 | size_t number_of_dex_registers) { |
| 36 | return dex_registers.FindLocationOffset(number_of_dex_registers); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 39 | TEST(StackMapTest, Test1) { |
| 40 | ArenaPool pool; |
| 41 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 42 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 43 | |
| 44 | ArenaBitVector sp_mask(&arena, 0, false); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 45 | size_t number_of_dex_registers = 2; |
| 46 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 47 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, 0); |
| 48 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, -2); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 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 Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 55 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 56 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 57 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 58 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 59 | StackMap stack_map = code_info.GetStackMapAt(0); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 61 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 62 | ASSERT_EQ(0u, stack_map.GetDexPc()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 63 | ASSERT_EQ(64u, stack_map.GetNativePcOffset()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 64 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 65 | |
| 66 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 67 | ASSERT_TRUE(SameBits(stack_mask, sp_mask)); |
| 68 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 69 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 70 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 81 | |
| 82 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(StackMapTest, Test2) { |
| 86 | ArenaPool pool; |
| 87 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 88 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 89 | |
| 90 | ArenaBitVector sp_mask1(&arena, 0, true); |
| 91 | sp_mask1.SetBit(2); |
| 92 | sp_mask1.SetBit(4); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 93 | size_t number_of_dex_registers = 2; |
| 94 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2); |
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); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 103 | stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 104 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, 18); |
| 105 | stream.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, 3); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 106 | |
| 107 | size_t size = stream.ComputeNeededSize(); |
| 108 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 109 | MemoryRegion region(memory, size); |
| 110 | stream.FillIn(region); |
| 111 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 112 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 113 | ASSERT_EQ(1u, code_info.GetStackMaskSize()); |
| 114 | ASSERT_EQ(2u, code_info.GetNumberOfStackMaps()); |
| 115 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 116 | // First stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 117 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 124 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 125 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 126 | ASSERT_TRUE(SameBits(stack_mask, sp_mask1)); |
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 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 141 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 142 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 148 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 149 | // Second stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 150 | { |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 157 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 158 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 159 | ASSERT_TRUE(SameBits(stack_mask, sp_mask2)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 160 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 161 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 174 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame^] | 175 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
| 176 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } // namespace art |