• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/MC/DwarfLineTables.cpp ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Support/Dwarf.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDwarf.h"
15 #include "llvm/MC/MCRegisterInfo.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__anon396d93a30111::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     MAI.reset(TheTarget->createMCAsmInfo(*MRI, Triple));
42     Ctx = llvm::make_unique<MCContext>(MAI.get(), MRI.get(), nullptr);
43   }
44 
operator bool__anon396d93a30111::Context45   operator bool() { return Ctx.get(); }
operator MCContext&__anon396d93a30111::Context46   operator MCContext &() { return *Ctx; };
47 };
48 
49 Context Ctxt;
50 }
51 
verifyEncoding(MCDwarfLineTableParams Params,int LineDelta,int AddrDelta,ArrayRef<uint8_t> ExpectedEncoding)52 void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta,
53                     ArrayRef<uint8_t> ExpectedEncoding) {
54   SmallString<16> Buffer;
55   raw_svector_ostream EncodingOS(Buffer);
56   MCDwarfLineAddr::Encode(Ctxt, Params, LineDelta, AddrDelta, EncodingOS);
57   ArrayRef<uint8_t> Encoding(reinterpret_cast<uint8_t *>(Buffer.data()),
58                              Buffer.size());
59   EXPECT_EQ(ExpectedEncoding, Encoding);
60 }
61 
TEST(DwarfLineTables,TestDefaultParams)62 TEST(DwarfLineTables, TestDefaultParams) {
63   if (!Ctxt)
64     return;
65 
66   MCDwarfLineTableParams Params;
67 
68   // Minimal line offset expressible through extended opcode, 0 addr delta
69   const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5
70   verifyEncoding(Params, -5, 0, Encoding0);
71 
72   // Maximal line offset expressible through extended opcode,
73   const uint8_t Encoding1[] = {26}; // Special opcode Addr += 0, Line += +8
74   verifyEncoding(Params, 8, 0, Encoding1);
75 
76   // Random value in the middle of the special ocode range
77   const uint8_t Encoding2[] = {146}; // Special opcode Addr += 9, Line += 2
78   verifyEncoding(Params, 2, 9, Encoding2);
79 
80   // Minimal line offset expressible through extended opcode, max addr delta
81   const uint8_t Encoding3[] = {251}; // Special opcode Addr += 17, Line += -5
82   verifyEncoding(Params, -5, 17, Encoding3);
83 
84   // Biggest special opcode
85   const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1
86   verifyEncoding(Params, -1, 17, Encoding4);
87 
88   // Line delta outside of the special opcode range, address delta in range
89   const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9,
90                                158}; // Special opcode Addr += 10, Line += 0
91   verifyEncoding(Params, 9, 10, Encoding5);
92 
93   // Address delta outside of the special opcode range, but small
94   // enough to do DW_LNS_const_add_pc + special opcode.
95   const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 17
96                                62}; // Special opcode Addr += 3, Line += 2
97   verifyEncoding(Params, 2, 20, Encoding6);
98 
99   // Address delta big enough to require the use of DW_LNS_advance_pc
100   // Line delta in special opcode range
101   const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100,
102                                20}; // Special opcode Addr += 0, Line += 2
103   verifyEncoding(Params, 2, 100, Encoding7);
104 
105   // No special opcode possible.
106   const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20,
107                                dwarf::DW_LNS_advance_pc, 100,
108                                dwarf::DW_LNS_copy};
109   verifyEncoding(Params, 20, 100, Encoding8);
110 }
111 
TEST(DwarfLineTables,TestCustomParams)112 TEST(DwarfLineTables, TestCustomParams) {
113   if (!Ctxt)
114     return;
115 
116   // Some tests against the example values given in the standard.
117   MCDwarfLineTableParams Params;
118   Params.DWARF2LineOpcodeBase = 13;
119   Params.DWARF2LineBase = -3;
120   Params.DWARF2LineRange = 12;
121 
122   // Minimal line offset expressible through extended opcode, 0 addr delta
123   const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5
124   verifyEncoding(Params, -3, 0, Encoding0);
125 
126   // Maximal line offset expressible through extended opcode,
127   const uint8_t Encoding1[] = {24}; // Special opcode Addr += 0, Line += +8
128   verifyEncoding(Params, 8, 0, Encoding1);
129 
130   // Random value in the middle of the special ocode range
131   const uint8_t Encoding2[] = {126}; // Special opcode Addr += 9, Line += 2
132   verifyEncoding(Params, 2, 9, Encoding2);
133 
134   // Minimal line offset expressible through extended opcode, max addr delta
135   const uint8_t Encoding3[] = {253}; // Special opcode Addr += 20, Line += -3
136   verifyEncoding(Params, -3, 20, Encoding3);
137 
138   // Biggest special opcode
139   const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1
140   verifyEncoding(Params, -1, 20, Encoding4);
141 
142   // Line delta outside of the special opcode range, address delta in range
143   const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9,
144                                136}; // Special opcode Addr += 10, Line += 0
145   verifyEncoding(Params, 9, 10, Encoding5);
146 
147   // Address delta outside of the special opcode range, but small
148   // enough to do DW_LNS_const_add_pc + special opcode.
149   const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 20
150                                138}; // Special opcode Addr += 10, Line += 2
151   verifyEncoding(Params, 2, 30, Encoding6);
152 
153   // Address delta big enough to require the use of DW_LNS_advance_pc
154   // Line delta in special opcode range
155   const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100,
156                                18}; // Special opcode Addr += 0, Line += 2
157   verifyEncoding(Params, 2, 100, Encoding7);
158 
159   // No special opcode possible.
160   const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20,
161                                dwarf::DW_LNS_advance_pc, 100,
162                                dwarf::DW_LNS_copy};
163   verifyEncoding(Params, 20, 100, Encoding8);
164 }
165 
TEST(DwarfLineTables,TestCustomParams2)166 TEST(DwarfLineTables, TestCustomParams2) {
167   if (!Ctxt)
168     return;
169 
170   // Corner case param values.
171   MCDwarfLineTableParams Params;
172   Params.DWARF2LineOpcodeBase = 13;
173   Params.DWARF2LineBase = 1;
174   Params.DWARF2LineRange = 255;
175 
176   const uint8_t Encoding0[] = {dwarf::DW_LNS_advance_line, 248, 1,
177                                dwarf::DW_LNS_copy};
178   verifyEncoding(Params, 248, 0, Encoding0);
179 }
180