[cfe-commits] r69503 - in /cfe/trunk: lib/CodeGen/CGObjCMac.cpp test/CodeGenObjC/bitfield-ivar-offsets.m

Daniel Dunbar daniel at zuster.org
Sat Apr 18 19:03:42 PDT 2009


Author: ddunbar
Date: Sat Apr 18 21:03:42 2009
New Revision: 69503

URL: http://llvm.org/viewvc/llvm-project?rev=69503&view=rev
Log:
Fix bug in computation of ivar offsets for (adjacent) bitfields.
 - The confusing IRgen bitfield interface is partly to blame here;
   fixing the functional error for now, cleanups to the interface to
   follow.

Added:
    cfe/trunk/test/CodeGenObjC/bitfield-ivar-offsets.m
Modified:
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=69503&r1=69502&r2=69503&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Sat Apr 18 21:03:42 2009
@@ -1879,17 +1879,20 @@
 uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
                                             const FieldDecl *Field) {
   if (!Field->isBitField())
-    return Layout->getElementOffset(
-            CGM.getTypes().getLLVMFieldNo(Field));
+    return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
+  
   // FIXME. Must be a better way of getting a bitfield base offset.
-  uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field);
-  const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
-  uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
-  offset = (offset*size)/8; 
-  return offset;
+  CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field);
+  // FIXME: The "field no" for bitfields is something completely
+  // different; it is the offset in multiples of the base type size!
+  uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field);
+  const llvm::Type *Ty = 
+    CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
+  Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
+  return (Offset + BFI.Begin) / 8;
 }
 
-/// GetFieldBaseOffset - return's field byt offset.
+/// GetFieldBaseOffset - return the field's byte offset.
 uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
                                              const llvm::StructLayout *Layout,
                                              const FieldDecl *Field) {
@@ -4580,8 +4583,8 @@
   for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); 
        i != e; ++i) {
     FieldDecl *Field = *i;
-    uint64_t offset = GetIvarBaseOffset(Layout, Field);
-    Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset);
+    Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], 
+                                GetIvarBaseOffset(Layout, Field));
     if (Field->getIdentifier())
       Ivar[1] = GetMethodVarName(Field->getIdentifier());
     else

Added: cfe/trunk/test/CodeGenObjC/bitfield-ivar-offsets.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/bitfield-ivar-offsets.m?rev=69503&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenObjC/bitfield-ivar-offsets.m (added)
+++ cfe/trunk/test/CodeGenObjC/bitfield-ivar-offsets.m Sat Apr 18 21:03:42 2009
@@ -0,0 +1,23 @@
+// RUN: clang-cc -triple x86_64-apple-darwin10 -emit-llvm -o %t %s &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._b0" = global i64 0, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._b1" = global i64 0, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._b2" = global i64 1, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._x" = global i64 2, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._b3" = global i64 4, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._y" = global i64 6, section "__DATA, __objc_const", align 8' %t &&
+// RUN: grep -F '@"OBJC_IVAR_$_I0._b4" = global i64 7, section "__DATA, __objc_const", align 8' %t &&
+// RUN: true
+
+ at interface I0 {
+  unsigned _b0:4;
+  unsigned _b1:5;
+  unsigned _b2:5;
+  char _x;
+  unsigned _b3:9;
+  char _y;
+  char _b4:3;
+}
+ at end
+
+ at implementation I0
+ at end





More information about the cfe-commits mailing list