Skip to content

Commit 7156910

Browse files
committed
[CodeView] Encode signed int values correctly when emitting S_CONSTANTs
Differential Revision: https://reviews.llvm.org/D90199
1 parent 973317c commit 7156910

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,25 @@ void CodeViewDebug::emitStaticConstMemberList() {
31563156
}
31573157
}
31583158

3159+
static bool isFloatDIType(const DIType *Ty) {
3160+
if (auto *CTy = dyn_cast<DICompositeType>(Ty))
3161+
return false;
3162+
3163+
if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
3164+
dwarf::Tag T = (dwarf::Tag)Ty->getTag();
3165+
if (T == dwarf::DW_TAG_pointer_type ||
3166+
T == dwarf::DW_TAG_ptr_to_member_type ||
3167+
T == dwarf::DW_TAG_reference_type ||
3168+
T == dwarf::DW_TAG_rvalue_reference_type)
3169+
return false;
3170+
assert(DTy->getBaseType() && "Expected valid base type");
3171+
return isFloatDIType(DTy->getBaseType());
3172+
}
3173+
3174+
auto *BTy = cast<DIBasicType>(Ty);
3175+
return (BTy->getEncoding() == dwarf::DW_ATE_float);
3176+
}
3177+
31593178
void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
31603179
const DIGlobalVariable *DIGV = CVGV.DIGV;
31613180

@@ -3191,7 +3210,12 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
31913210
const DIExpression *DIE = CVGV.GVInfo.get<const DIExpression *>();
31923211
assert(DIE->isConstant() &&
31933212
"Global constant variables must contain a constant expression.");
3194-
uint64_t Val = DIE->getElement(1);
3213+
3214+
// Use unsigned for floats.
3215+
bool isUnsigned = isFloatDIType(DIGV->getType())
3216+
? true
3217+
: DebugHandlerBase::isUnsignedDIType(DIGV->getType());
3218+
APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);
31953219

31963220
MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);
31973221
OS.AddComment("Type");
@@ -3202,7 +3226,7 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
32023226
uint8_t data[10];
32033227
BinaryStreamWriter Writer(data, llvm::support::endianness::little);
32043228
CodeViewRecordIO IO(Writer);
3205-
cantFail(IO.mapEncodedInteger(Val));
3229+
cantFail(IO.mapEncodedInteger(Value));
32063230
StringRef SRef((char *)data, Writer.getOffset());
32073231
OS.emitBinaryData(SRef);
32083232

llvm/test/DebugInfo/COFF/global-constants.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737

3838
; ASM: .short 4359 # Record kind: S_CONSTANT
3939
; ASM-NEXT: .long 4101 # Type
40-
; ASM-NEXT: .byte 0x0a, 0x80, 0x40, 0x61 # Value
41-
; ASM-NEXT: .byte 0x07, 0x80, 0xff, 0xff
42-
; ASM-NEXT: .byte 0xff, 0xff
40+
; ASM-NEXT: .byte 0x03, 0x80, 0x40, 0x61 # Value
41+
; ASM-NEXT: .byte 0x07, 0x80
4342
; ASM-NEXT: .asciz "ENUM_B" # Name
4443
; ASM-NEXT: .p2align 2
4544
; ASM-NOT: .asciz "S::SEnum" # Name
@@ -64,7 +63,7 @@
6463
; OBJ-NEXT: ConstantSym {
6564
; OBJ-NEXT: Kind: S_CONSTANT (0x1107)
6665
; OBJ-NEXT: Type: TestEnum (0x1005)
67-
; OBJ-NEXT: Value: 18446744071562551616
66+
; OBJ-NEXT: Value: -214700000
6867
; OBJ-NEXT: Name: ENUM_B
6968
; OBJ-NEXT: }
7069
; OBJ-NOT: Name: S::SEnum

llvm/test/DebugInfo/COFF/globals.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@
8686

8787
; ASM: .short 4359 # Record kind: S_CONSTANT
8888
; ASM-NEXT: .long 4100 # Type
89-
; ASM-NEXT: .byte 0x08, 0x00 # Value
89+
; ASM-NEXT: .byte 0x00, 0x80, 0x08 # Value
9090
; ASM-NEXT: .asciz "foo::constExpr" # Name
9191
; ASM-NEXT: .p2align 2
9292

9393
; ASM: .short 4359 # Record kind: S_CONSTANT
9494
; ASM-NEXT: .long 4100 # Type
95-
; ASM-NEXT: .byte 0x09, 0x00 # Value
95+
; ASM-NEXT: .byte 0x00, 0x80, 0x09 # Value
9696
; ASM-NEXT: .asciz "foo::constVal" # Name
9797
; ASM-NEXT: .p2align 2
9898

9999
; ASM: .short 4359 # Record kind: S_CONSTANT
100100
; ASM-NEXT: .long 4100 # Type
101-
; ASM-NEXT: .byte 0x0e, 0x00 # Value
101+
; ASM-NEXT: .byte 0x00, 0x80, 0x0e # Value
102102
; ASM-NEXT: .asciz "foo::Data::DataConstExpr" # Name
103103
; ASM-NEXT: .p2align 2
104104

0 commit comments

Comments
 (0)