Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/MC/DwarfLineTables.cpp ------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 10 | #include "llvm/BinaryFormat/Dwarf.h" |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
| 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCDwarf.h" |
| 14 | #include "llvm/MC/MCRegisterInfo.h" |
| 15 | #include "llvm/Support/TargetRegistry.h" |
| 16 | #include "llvm/Support/TargetSelect.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | struct Context { |
| 23 | const char *Triple = "x86_64-pc-linux"; |
| 24 | std::unique_ptr<MCRegisterInfo> MRI; |
| 25 | std::unique_ptr<MCAsmInfo> MAI; |
| 26 | std::unique_ptr<MCContext> Ctx; |
| 27 | |
| 28 | Context() { |
| 29 | llvm::InitializeAllTargetInfos(); |
| 30 | llvm::InitializeAllTargetMCs(); |
| 31 | llvm::InitializeAllDisassemblers(); |
| 32 | |
| 33 | // If we didn't build x86, do not run the test. |
| 34 | std::string Error; |
| 35 | const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); |
| 36 | if (!TheTarget) |
| 37 | return; |
| 38 | |
| 39 | MRI.reset(TheTarget->createMCRegInfo(Triple)); |
| 40 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, Triple)); |
| 41 | Ctx = llvm::make_unique<MCContext>(MAI.get(), MRI.get(), nullptr); |
| 42 | } |
| 43 | |
| 44 | operator bool() { return Ctx.get(); } |
| 45 | operator MCContext &() { return *Ctx; }; |
| 46 | }; |
| 47 | |
Mehdi Amini | 3ccc39e | 2016-11-11 22:18:42 +0000 | [diff] [blame] | 48 | Context &getContext() { |
| 49 | static Context Ctxt; |
| 50 | return Ctxt; |
| 51 | } |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta, |
| 55 | ArrayRef<uint8_t> ExpectedEncoding) { |
| 56 | SmallString<16> Buffer; |
| 57 | raw_svector_ostream EncodingOS(Buffer); |
Mehdi Amini | 3ccc39e | 2016-11-11 22:18:42 +0000 | [diff] [blame] | 58 | MCDwarfLineAddr::Encode(getContext(), Params, LineDelta, AddrDelta, |
| 59 | EncodingOS); |
Fangrui Song | 6a0746a | 2019-04-07 03:58:42 +0000 | [diff] [blame] | 60 | EXPECT_EQ(ExpectedEncoding, arrayRefFromStringRef(Buffer)); |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(DwarfLineTables, TestDefaultParams) { |
Mehdi Amini | 3ccc39e | 2016-11-11 22:18:42 +0000 | [diff] [blame] | 64 | if (!getContext()) |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 65 | return; |
| 66 | |
| 67 | MCDwarfLineTableParams Params; |
| 68 | |
| 69 | // Minimal line offset expressible through extended opcode, 0 addr delta |
| 70 | const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5 |
| 71 | verifyEncoding(Params, -5, 0, Encoding0); |
| 72 | |
| 73 | // Maximal line offset expressible through extended opcode, |
| 74 | const uint8_t Encoding1[] = {26}; // Special opcode Addr += 0, Line += +8 |
| 75 | verifyEncoding(Params, 8, 0, Encoding1); |
| 76 | |
| 77 | // Random value in the middle of the special ocode range |
| 78 | const uint8_t Encoding2[] = {146}; // Special opcode Addr += 9, Line += 2 |
| 79 | verifyEncoding(Params, 2, 9, Encoding2); |
| 80 | |
| 81 | // Minimal line offset expressible through extended opcode, max addr delta |
| 82 | const uint8_t Encoding3[] = {251}; // Special opcode Addr += 17, Line += -5 |
| 83 | verifyEncoding(Params, -5, 17, Encoding3); |
| 84 | |
| 85 | // Biggest special opcode |
| 86 | const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1 |
| 87 | verifyEncoding(Params, -1, 17, Encoding4); |
| 88 | |
| 89 | // Line delta outside of the special opcode range, address delta in range |
| 90 | const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9, |
| 91 | 158}; // Special opcode Addr += 10, Line += 0 |
| 92 | verifyEncoding(Params, 9, 10, Encoding5); |
| 93 | |
| 94 | // Address delta outside of the special opcode range, but small |
| 95 | // enough to do DW_LNS_const_add_pc + special opcode. |
| 96 | const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 17 |
| 97 | 62}; // Special opcode Addr += 3, Line += 2 |
| 98 | verifyEncoding(Params, 2, 20, Encoding6); |
| 99 | |
| 100 | // Address delta big enough to require the use of DW_LNS_advance_pc |
| 101 | // Line delta in special opcode range |
| 102 | const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100, |
| 103 | 20}; // Special opcode Addr += 0, Line += 2 |
| 104 | verifyEncoding(Params, 2, 100, Encoding7); |
| 105 | |
| 106 | // No special opcode possible. |
| 107 | const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20, |
| 108 | dwarf::DW_LNS_advance_pc, 100, |
| 109 | dwarf::DW_LNS_copy}; |
| 110 | verifyEncoding(Params, 20, 100, Encoding8); |
| 111 | } |
| 112 | |
| 113 | TEST(DwarfLineTables, TestCustomParams) { |
Mehdi Amini | 3ccc39e | 2016-11-11 22:18:42 +0000 | [diff] [blame] | 114 | if (!getContext()) |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 115 | return; |
| 116 | |
| 117 | // Some tests against the example values given in the standard. |
| 118 | MCDwarfLineTableParams Params; |
| 119 | Params.DWARF2LineOpcodeBase = 13; |
| 120 | Params.DWARF2LineBase = -3; |
| 121 | Params.DWARF2LineRange = 12; |
| 122 | |
| 123 | // Minimal line offset expressible through extended opcode, 0 addr delta |
| 124 | const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5 |
| 125 | verifyEncoding(Params, -3, 0, Encoding0); |
| 126 | |
| 127 | // Maximal line offset expressible through extended opcode, |
| 128 | const uint8_t Encoding1[] = {24}; // Special opcode Addr += 0, Line += +8 |
| 129 | verifyEncoding(Params, 8, 0, Encoding1); |
| 130 | |
| 131 | // Random value in the middle of the special ocode range |
| 132 | const uint8_t Encoding2[] = {126}; // Special opcode Addr += 9, Line += 2 |
| 133 | verifyEncoding(Params, 2, 9, Encoding2); |
| 134 | |
| 135 | // Minimal line offset expressible through extended opcode, max addr delta |
| 136 | const uint8_t Encoding3[] = {253}; // Special opcode Addr += 20, Line += -3 |
| 137 | verifyEncoding(Params, -3, 20, Encoding3); |
| 138 | |
| 139 | // Biggest special opcode |
| 140 | const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1 |
| 141 | verifyEncoding(Params, -1, 20, Encoding4); |
| 142 | |
| 143 | // Line delta outside of the special opcode range, address delta in range |
| 144 | const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9, |
| 145 | 136}; // Special opcode Addr += 10, Line += 0 |
| 146 | verifyEncoding(Params, 9, 10, Encoding5); |
| 147 | |
| 148 | // Address delta outside of the special opcode range, but small |
| 149 | // enough to do DW_LNS_const_add_pc + special opcode. |
| 150 | const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 20 |
| 151 | 138}; // Special opcode Addr += 10, Line += 2 |
| 152 | verifyEncoding(Params, 2, 30, Encoding6); |
| 153 | |
| 154 | // Address delta big enough to require the use of DW_LNS_advance_pc |
| 155 | // Line delta in special opcode range |
| 156 | const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100, |
| 157 | 18}; // Special opcode Addr += 0, Line += 2 |
| 158 | verifyEncoding(Params, 2, 100, Encoding7); |
| 159 | |
| 160 | // No special opcode possible. |
| 161 | const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20, |
| 162 | dwarf::DW_LNS_advance_pc, 100, |
| 163 | dwarf::DW_LNS_copy}; |
| 164 | verifyEncoding(Params, 20, 100, Encoding8); |
| 165 | } |
| 166 | |
| 167 | TEST(DwarfLineTables, TestCustomParams2) { |
Mehdi Amini | 3ccc39e | 2016-11-11 22:18:42 +0000 | [diff] [blame] | 168 | if (!getContext()) |
Frederic Riss | e5b78d8 | 2016-01-31 22:06:35 +0000 | [diff] [blame] | 169 | return; |
| 170 | |
| 171 | // Corner case param values. |
| 172 | MCDwarfLineTableParams Params; |
| 173 | Params.DWARF2LineOpcodeBase = 13; |
| 174 | Params.DWARF2LineBase = 1; |
| 175 | Params.DWARF2LineRange = 255; |
| 176 | |
| 177 | const uint8_t Encoding0[] = {dwarf::DW_LNS_advance_line, 248, 1, |
| 178 | dwarf::DW_LNS_copy}; |
| 179 | verifyEncoding(Params, 248, 0, Encoding0); |
| 180 | } |