Dwarf: [PR11022] Fix emitting DW_AT_const_value(>i64), to be host-endian-neutral.
authorNAKAMURA Takumi <geek4civic@gmail.com>
Fri, 28 Oct 2011 14:12:22 +0000 (14:12 +0000)
committerNAKAMURA Takumi <geek4civic@gmail.com>
Fri, 28 Oct 2011 14:12:22 +0000 (14:12 +0000)
Don't assume APInt::getRawData() would hold target-aware endianness nor host-compliant endianness. rawdata[0] holds most lower i64, even on big endian host.

FIXME: Add a testcase for big endian target.

FIXME: Ditto on CompileUnit::addConstantFPValue() ?

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143194 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
test/CodeGen/X86/dbg-i128-const.ll

index f9f1642b2ff00db3dcaeaddefea72924288d2dc8..95f1f92fe3d2f74990db2cd6180311d93a4b5e38 100644 (file)
@@ -534,18 +534,20 @@ bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
 
   // Get the raw data form of the large APInt.
   const APInt Val = CI->getValue();
-  const char *Ptr = (const char*)Val.getRawData();
+  const uint64_t *Ptr64 = Val.getRawData();
 
   int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
   bool LittleEndian = Asm->getTargetData().isLittleEndian();
-  int Incr = (LittleEndian ? 1 : -1);
-  int Start = (LittleEndian ? 0 : NumBytes - 1);
-  int Stop = (LittleEndian ? NumBytes : -1);
 
   // Output the constant to DWARF one byte at a time.
-  for (; Start != Stop; Start += Incr)
-    addUInt(Block, 0, dwarf::DW_FORM_data1,
-            (unsigned char)0xFF & Ptr[Start]);
+  for (int i = 0; i < NumBytes; i++) {
+    uint8_t c;
+    if (LittleEndian)
+      c = Ptr64[i / 8] >> (8 * (i & 7));
+    else
+      c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
+    addUInt(Block, 0, dwarf::DW_FORM_data1, c);
+  }
 
   addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
   return true;
index fb83fca4b7e6c1160e9c2bc6383e570871e86f08..bd96d9195d021e85d5ec70e8695997e5bc91d218 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: llc < %s | FileCheck %s
+; RUN: llc -mtriple=x86_64-linux < %s | FileCheck %s
 
 ; CHECK: DW_AT_const_value
 ; CHECK-NEXT: 42