Skip to content

Commit ff47d83

Browse files
[DwarfExpression] Refactor dwarf expression (NFC)
Refactor location description kind in order to be easier for extensions (needed for D60866). In addition, cut off some bits from the other class fields. Patch by Djordje Todorovic. Differential Revision: https://reviews.llvm.org/D62002 llvm-svn: 361480
1 parent e51b9e4 commit ff47d83

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void DwarfExpression::emitConstu(uint64_t Value) {
4040

4141
void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
4242
assert(DwarfReg >= 0 && "invalid negative dwarf register number");
43-
assert((LocationKind == Unknown || LocationKind == Register) &&
43+
assert((isUnknownLocation() || isRegisterLocation()) &&
4444
"location description already locked down");
4545
LocationKind = Register;
4646
if (DwarfReg < 32) {
@@ -53,7 +53,7 @@ void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
5353

5454
void DwarfExpression::addBReg(int DwarfReg, int Offset) {
5555
assert(DwarfReg >= 0 && "invalid negative dwarf register number");
56-
assert(LocationKind != Register && "location description already locked down");
56+
assert(!isRegisterLocation() && "location description already locked down");
5757
if (DwarfReg < 32) {
5858
emitOp(dwarf::DW_OP_breg0 + DwarfReg);
5959
} else {
@@ -184,20 +184,20 @@ void DwarfExpression::addStackValue() {
184184
}
185185

186186
void DwarfExpression::addSignedConstant(int64_t Value) {
187-
assert(LocationKind == Implicit || LocationKind == Unknown);
187+
assert(isImplicitLocation() || isUnknownLocation());
188188
LocationKind = Implicit;
189189
emitOp(dwarf::DW_OP_consts);
190190
emitSigned(Value);
191191
}
192192

193193
void DwarfExpression::addUnsignedConstant(uint64_t Value) {
194-
assert(LocationKind == Implicit || LocationKind == Unknown);
194+
assert(isImplicitLocation() || isUnknownLocation());
195195
LocationKind = Implicit;
196196
emitConstu(Value);
197197
}
198198

199199
void DwarfExpression::addUnsignedConstant(const APInt &Value) {
200-
assert(LocationKind == Implicit || LocationKind == Unknown);
200+
assert(isImplicitLocation() || isUnknownLocation());
201201
LocationKind = Implicit;
202202

203203
unsigned Size = Value.getBitWidth();
@@ -242,7 +242,7 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
242242
}
243243

244244
// Handle simple register locations.
245-
if (LocationKind != Memory && !HasComplexExpression) {
245+
if (!isMemoryLocation() && !HasComplexExpression) {
246246
for (auto &Reg : DwarfRegs) {
247247
if (Reg.DwarfRegNo >= 0)
248248
addReg(Reg.DwarfRegNo, Reg.Comment);
@@ -343,7 +343,7 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
343343
SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
344344

345345
// Emit a DW_OP_stack_value for implicit location descriptions.
346-
if (LocationKind == Implicit)
346+
if (isImplicitLocation())
347347
addStackValue();
348348

349349
// Emit the DW_OP_piece.
@@ -354,7 +354,7 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
354354
return;
355355
}
356356
case dwarf::DW_OP_plus_uconst:
357-
assert(LocationKind != Register);
357+
assert(!isRegisterLocation());
358358
emitOp(dwarf::DW_OP_plus_uconst);
359359
emitUnsigned(Op->getArg(0));
360360
break;
@@ -375,16 +375,16 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
375375
emitOp(Op->getOp());
376376
break;
377377
case dwarf::DW_OP_deref:
378-
assert(LocationKind != Register);
379-
if (LocationKind != Memory && ::isMemoryLocation(ExprCursor))
378+
assert(!isRegisterLocation());
379+
if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor))
380380
// Turning this into a memory location description makes the deref
381381
// implicit.
382382
LocationKind = Memory;
383383
else
384384
emitOp(dwarf::DW_OP_deref);
385385
break;
386386
case dwarf::DW_OP_constu:
387-
assert(LocationKind != Register);
387+
assert(!isRegisterLocation());
388388
emitConstu(Op->getArg(0));
389389
break;
390390
case dwarf::DW_OP_LLVM_convert: {
@@ -427,11 +427,11 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
427427
LocationKind = Implicit;
428428
break;
429429
case dwarf::DW_OP_swap:
430-
assert(LocationKind != Register);
430+
assert(!isRegisterLocation());
431431
emitOp(dwarf::DW_OP_swap);
432432
break;
433433
case dwarf::DW_OP_xderef:
434-
assert(LocationKind != Register);
434+
assert(!isRegisterLocation());
435435
emitOp(dwarf::DW_OP_xderef);
436436
break;
437437
case dwarf::DW_OP_deref_size:
@@ -443,7 +443,7 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
443443
}
444444
}
445445

446-
if (LocationKind == Implicit)
446+
if (isImplicitLocation())
447447
// Turn this into an implicit location description.
448448
addStackValue();
449449
}

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,40 @@ class DwarfExpression {
111111

112112
/// Current Fragment Offset in Bits.
113113
uint64_t OffsetInBits = 0;
114-
unsigned DwarfVersion;
115114

116115
/// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
117-
unsigned SubRegisterSizeInBits = 0;
118-
unsigned SubRegisterOffsetInBits = 0;
116+
unsigned SubRegisterSizeInBits : 16;
117+
unsigned SubRegisterOffsetInBits : 16;
119118

120119
/// The kind of location description being produced.
121-
enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown;
120+
enum { Unknown = 0, Register, Memory, Implicit };
122121

122+
unsigned LocationKind : 3;
123+
unsigned LocationFlags : 2;
124+
unsigned DwarfVersion : 4;
125+
126+
public:
127+
bool isUnknownLocation() const {
128+
return LocationKind == Unknown;
129+
}
130+
131+
bool isMemoryLocation() const {
132+
return LocationKind == Memory;
133+
}
134+
135+
bool isRegisterLocation() const {
136+
return LocationKind == Register;
137+
}
138+
139+
bool isImplicitLocation() const {
140+
return LocationKind == Implicit;
141+
}
142+
143+
protected:
123144
/// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
124145
/// to represent a subregister.
125146
void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
147+
assert(SizeInBits < 65536 && OffsetInBits < 65536);
126148
SubRegisterSizeInBits = SizeInBits;
127149
SubRegisterOffsetInBits = OffsetInBits;
128150
}
@@ -206,7 +228,9 @@ class DwarfExpression {
206228

207229
public:
208230
DwarfExpression(unsigned DwarfVersion, DwarfCompileUnit &CU)
209-
: CU(CU), DwarfVersion(DwarfVersion) {}
231+
: CU(CU), SubRegisterSizeInBits(0), SubRegisterOffsetInBits(0),
232+
LocationKind(Unknown), LocationFlags(Unknown),
233+
DwarfVersion(DwarfVersion) {}
210234

211235
/// This needs to be called last to commit any pending changes.
212236
void finalize();
@@ -220,12 +244,9 @@ class DwarfExpression {
220244
/// Emit an unsigned constant.
221245
void addUnsignedConstant(const APInt &Value);
222246

223-
bool isMemoryLocation() const { return LocationKind == Memory; }
224-
bool isUnknownLocation() const { return LocationKind == Unknown; }
225-
226247
/// Lock this down to become a memory location description.
227248
void setMemoryLocationKind() {
228-
assert(LocationKind == Unknown);
249+
assert(isUnknownLocation());
229250
LocationKind = Memory;
230251
}
231252

0 commit comments

Comments
 (0)