1 //===- llvm/unittest/MC/DwarfLineTables.cpp ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/Support/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "gtest/gtest.h"
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
;
29 llvm::InitializeAllTargetInfos();
30 llvm::InitializeAllTargetMCs();
31 llvm::InitializeAllDisassemblers();
33 // If we didn't build x86, do not run the test.
35 const Target
*TheTarget
= TargetRegistry::lookupTarget(Triple
, Error
);
39 MRI
.reset(TheTarget
->createMCRegInfo(Triple
));
40 MAI
.reset(TheTarget
->createMCAsmInfo(*MRI
, Triple
));
41 Ctx
= llvm::make_unique
<MCContext
>(MAI
.get(), MRI
.get(), nullptr);
44 operator bool() { return Ctx
.get(); }
45 operator MCContext
&() { return *Ctx
; };
48 Context
&getContext() {
54 void verifyEncoding(MCDwarfLineTableParams Params
, int LineDelta
, int AddrDelta
,
55 ArrayRef
<uint8_t> ExpectedEncoding
) {
56 SmallString
<16> Buffer
;
57 raw_svector_ostream
EncodingOS(Buffer
);
58 MCDwarfLineAddr::Encode(getContext(), Params
, LineDelta
, AddrDelta
,
60 ArrayRef
<uint8_t> Encoding(reinterpret_cast<uint8_t *>(Buffer
.data()),
62 EXPECT_EQ(ExpectedEncoding
, Encoding
);
65 TEST(DwarfLineTables
, TestDefaultParams
) {
69 MCDwarfLineTableParams Params
;
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
);
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
);
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
);
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
);
87 // Biggest special opcode
88 const uint8_t Encoding4
[] = {255}; // Special opcode Addr += 17, Line += -1
89 verifyEncoding(Params
, -1, 17, Encoding4
);
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
);
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
);
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
);
108 // No special opcode possible.
109 const uint8_t Encoding8
[] = {dwarf::DW_LNS_advance_line
, 20,
110 dwarf::DW_LNS_advance_pc
, 100,
112 verifyEncoding(Params
, 20, 100, Encoding8
);
115 TEST(DwarfLineTables
, TestCustomParams
) {
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;
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
);
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
);
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
);
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
);
141 // Biggest special opcode
142 const uint8_t Encoding4
[] = {255}; // Special opcode Addr += 17, Line += -1
143 verifyEncoding(Params
, -1, 20, Encoding4
);
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
);
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
);
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
);
162 // No special opcode possible.
163 const uint8_t Encoding8
[] = {dwarf::DW_LNS_advance_line
, 20,
164 dwarf::DW_LNS_advance_pc
, 100,
166 verifyEncoding(Params
, 20, 100, Encoding8
);
169 TEST(DwarfLineTables
, TestCustomParams2
) {
173 // Corner case param values.
174 MCDwarfLineTableParams Params
;
175 Params
.DWARF2LineOpcodeBase
= 13;
176 Params
.DWARF2LineBase
= 1;
177 Params
.DWARF2LineRange
= 255;
179 const uint8_t Encoding0
[] = {dwarf::DW_LNS_advance_line
, 248, 1,
181 verifyEncoding(Params
, 248, 0, Encoding0
);