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 | |
| 25 | bool 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 34 | size_t ComputeDexRegisterMapSize(size_t number_of_dex_registers) { |
| 35 | return DexRegisterMap::kFixedSize |
| 36 | + number_of_dex_registers * DexRegisterMap::SingleEntrySize(); |
| 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); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 47 | 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 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 | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 70 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 74 | 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 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; |
| 91 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 92 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 100 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 103 | |
| 104 | size_t size = stream.ComputeNeededSize(); |
| 105 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 106 | MemoryRegion region(memory, size); |
| 107 | stream.FillIn(region); |
| 108 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 109 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 110 | ASSERT_EQ(1u, code_info.GetStackMaskSize()); |
| 111 | ASSERT_EQ(2u, code_info.GetNumberOfStackMaps()); |
| 112 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 113 | // First stack map. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 114 | StackMap stack_map = code_info.GetStackMapAt(0); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 115 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 116 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 117 | ASSERT_EQ(0u, stack_map.GetDexPc()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 118 | ASSERT_EQ(64u, stack_map.GetNativePcOffset()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 119 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask()); |
| 120 | |
| 121 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 122 | ASSERT_TRUE(SameBits(stack_mask, sp_mask1)); |
| 123 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 124 | ASSERT_TRUE(stack_map.HasDexRegisterMap()); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 125 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 129 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 134 | ASSERT_TRUE(stack_map.HasInlineInfo()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 135 | 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 Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 140 | // Second stack map. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 141 | stack_map = code_info.GetStackMapAt(1); |
| 142 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 143 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u))); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 144 | ASSERT_EQ(1u, stack_map.GetDexPc()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 145 | ASSERT_EQ(128u, stack_map.GetNativePcOffset()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 146 | ASSERT_EQ(0xFFu, stack_map.GetRegisterMask()); |
| 147 | |
| 148 | stack_mask = stack_map.GetStackMask(); |
| 149 | ASSERT_TRUE(SameBits(stack_mask, sp_mask2)); |
| 150 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame^] | 151 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 161 | ASSERT_FALSE(stack_map.HasInlineInfo()); |
| 162 | } |
| 163 | |
| 164 | } // namespace art |