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