[llvm-commits] [dragonegg] r103009 - in /dragonegg/trunk: llvm-abi-default.cpp llvm-abi.h llvm-convert.cpp llvm-debug.cpp llvm-internal.h llvm-types.cpp

Duncan Sands baldrick at free.fr
Tue May 4 08:03:39 PDT 2010


Author: baldrick
Date: Tue May  4 10:03:39 2010
New Revision: 103009

URL: http://llvm.org/viewvc/llvm-project?rev=103009&view=rev
Log:
When dealing with bitfields, use the shrunk-to-fit types computed by
stor-layout.c, rather than the original declared types.  The goal is
to simplify the bitfield logic before generalizing it to handle Ada
bitfields (which can contain arbitrary types, such as structs and
arrays, as well as integers).  However it has to be said that this
also fixes all kinds of out-of-range memory accesses, all related
to bitfields, noticed by valgrind when using LLVM built with dragonegg.

Modified:
    dragonegg/trunk/llvm-abi-default.cpp
    dragonegg/trunk/llvm-abi.h
    dragonegg/trunk/llvm-convert.cpp
    dragonegg/trunk/llvm-debug.cpp
    dragonegg/trunk/llvm-internal.h
    dragonegg/trunk/llvm-types.cpp

Modified: dragonegg/trunk/llvm-abi-default.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-abi-default.cpp?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-abi-default.cpp (original)
+++ dragonegg/trunk/llvm-abi-default.cpp Tue May  4 10:03:39 2010
@@ -134,7 +134,7 @@
   } else if (TREE_CODE(type) == RECORD_TYPE) {
     for (tree Field = TYPE_FIELDS(type); Field; Field = TREE_CHAIN(Field))
       if (TREE_CODE(Field) == FIELD_DECL) {
-        const tree Ftype = getDeclaredType(Field);
+        const tree Ftype = TREE_TYPE(Field);
         const Type *FTy = ConvertType(Ftype);
         unsigned FNo = GetFieldIndex(Field, Ty);
         assert(FNo < INT_MAX && "Case not handled yet!");
@@ -147,7 +147,7 @@
         // HandleByValArgument.)
         if (!LLVM_SHOULD_PASS_AGGREGATE_USING_BYVAL_ATTR(Ftype, FTy)) {
           C.EnterField(FNo, Ty);
-          HandleArgument(getDeclaredType(Field), ScalarElts);
+          HandleArgument(TREE_TYPE(Field), ScalarElts);
           C.ExitField();
         }
       }

Modified: dragonegg/trunk/llvm-abi.h
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-abi.h?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-abi.h (original)
+++ dragonegg/trunk/llvm-abi.h Tue May  4 10:03:39 2010
@@ -190,10 +190,10 @@
         if (!FoundField) {
           if (rejectFatBitfield &&
               TREE_CODE(TYPE_SIZE(type)) == INTEGER_CST &&
-              TREE_INT_CST_LOW(TYPE_SIZE(getDeclaredType(Field))) > 
+              TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(Field))) > 
               TREE_INT_CST_LOW(TYPE_SIZE(type)))
             return 0;
-          FoundField = getDeclaredType(Field);
+          FoundField = TREE_TYPE(Field);
         } else {
           return 0;   // More than one field.
         }

Modified: dragonegg/trunk/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-convert.cpp?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-convert.cpp (original)
+++ dragonegg/trunk/llvm-convert.cpp Tue May  4 10:03:39 2010
@@ -5418,7 +5418,7 @@
 
   StructAddrLV.Ptr = Builder.CreateBitCast(StructAddrLV.Ptr,
                                            StructTy->getPointerTo());
-  const Type *FieldTy = ConvertType(getDeclaredType(FieldDecl));
+  const Type *FieldTy = ConvertType(TREE_TYPE(FieldDecl));
 
   // BitStart - This is the actual offset of the field from the start of the
   // struct, in bits.  For bitfields this may be on a non-byte boundary.
@@ -8761,7 +8761,7 @@
 
   StructAddrLV = TheFolder->CreateBitCast(StructAddrLV,
                                           StructTy->getPointerTo());
-  const Type *FieldTy = ConvertType(getDeclaredType(FieldDecl));
+  const Type *FieldTy = ConvertType(TREE_TYPE(FieldDecl));
 
   // BitStart - This is the actual offset of the field from the start of the
   // struct, in bits.  For bitfields this may be on a non-byte boundary.

Modified: dragonegg/trunk/llvm-debug.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-debug.cpp?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-debug.cpp (original)
+++ dragonegg/trunk/llvm-debug.cpp Tue May  4 10:03:39 2010
@@ -131,7 +131,8 @@
 ///
 static tree FieldType(tree Field) {
   if (TREE_CODE (Field) == ERROR_MARK) return integer_type_node;
-  return getDeclaredType(Field);
+  return DECL_BIT_FIELD_TYPE(Field) ?
+    DECL_BIT_FIELD_TYPE(Field) : TREE_TYPE (Field);
 }
 
 /// GetNodeName - Returns the name stored in a node regardless of whether the

Modified: dragonegg/trunk/llvm-internal.h
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-internal.h?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-internal.h (original)
+++ dragonegg/trunk/llvm-internal.h Tue May  4 10:03:39 2010
@@ -303,10 +303,6 @@
   return Result;
 }
 
-/// getDeclaredType - Get the declared type for the specified field, and
-/// not the shrunk-to-fit type that GCC gives us in TREE_TYPE.
-tree_node *getDeclaredType(tree_node *field_decl);
-
 /// ValidateRegisterVariable - Check that a static "asm" variable is
 /// well-formed.  If not, emit error messages and return true.  If so, return
 /// false.

Modified: dragonegg/trunk/llvm-types.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-types.cpp?rev=103009&r1=103008&r2=103009&view=diff
==============================================================================
--- dragonegg/trunk/llvm-types.cpp (original)
+++ dragonegg/trunk/llvm-types.cpp Tue May  4 10:03:39 2010
@@ -287,8 +287,7 @@
 
 /// isBitfield - Returns whether to treat the specified field as a bitfield.
 bool isBitfield(tree_node *field_decl) {
-  tree type = DECL_BIT_FIELD_TYPE(field_decl);
-  if (!type)
+  if (!DECL_BIT_FIELD(field_decl))
     return false;
 
   // A bitfield.  But do we need to treat it as one?
@@ -298,11 +297,11 @@
     // Does not start on a byte boundary - must treat as a bitfield.
     return true;
 
-  if (!isInt64(TYPE_SIZE (type), true))
+  if (!isInt64(TYPE_SIZE (TREE_TYPE(field_decl)), true))
     // No size or variable sized - play safe, treat as a bitfield.
     return true;
 
-  uint64_t TypeSizeInBits = getInt64(TYPE_SIZE (type), true);
+  uint64_t TypeSizeInBits = getInt64(TYPE_SIZE (TREE_TYPE(field_decl)), true);
   assert(!(TypeSizeInBits & 7) && "A type with a non-byte size!");
 
   assert(DECL_SIZE(field_decl) && "Bitfield with no bit size!");
@@ -314,13 +313,6 @@
   return false;
 }
 
-/// getDeclaredType - Get the declared type for the specified field_decl, and
-/// not the shrunk-to-fit type that GCC gives us in TREE_TYPE.
-tree getDeclaredType(tree_node *field_decl) {
-  return DECL_BIT_FIELD_TYPE(field_decl) ?
-    DECL_BIT_FIELD_TYPE(field_decl) : TREE_TYPE (field_decl);
-}
-
 /// refine_type_to - Cause all users of the opaque type old_type to switch
 /// to the more concrete type new_type.
 void refine_type_to(tree old_type, tree new_type)
@@ -646,7 +638,7 @@
         return true;
 
       uint64_t FieldBitOffset = getFieldOffsetInBits(Field);
-      if (GCCTypeOverlapsWithPadding(getDeclaredType(Field),
+      if (GCCTypeOverlapsWithPadding(TREE_TYPE(Field),
                                      PadStartBits-FieldBitOffset, PadSizeBits))
         return true;
     }
@@ -1631,9 +1623,9 @@
         return false;
       // If Field has user defined alignment and it does not match Ty alignment
       // then convert to a packed struct and try again.
-      if (TYPE_USER_ALIGN(DECL_BIT_FIELD_TYPE(Field))) {
-        const Type *Ty = ConvertType(getDeclaredType(Field));
-        if (TYPE_ALIGN(DECL_BIT_FIELD_TYPE(Field)) !=
+      if (TYPE_USER_ALIGN(TREE_TYPE(Field))) {
+        const Type *Ty = ConvertType(TREE_TYPE(Field));
+        if (TYPE_ALIGN(TREE_TYPE(Field)) !=
             8 * Info.getTypeAlignment(Ty))
           return false;
       }
@@ -1649,7 +1641,7 @@
   assert((StartOffsetInBits & 7) == 0 && "Non-bit-field has non-byte offset!");
   uint64_t StartOffsetInBytes = StartOffsetInBits/8;
 
-  const Type *Ty = ConvertType(getDeclaredType(Field));
+  const Type *Ty = ConvertType(TREE_TYPE(Field));
 
   // If this field is packed then the struct may need padding fields
   // before this field.





More information about the llvm-commits mailing list