[llvm-commits] [llvm-gcc-4.2] r50317 - /llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp

Devang Patel dpatel at apple.com
Sat Apr 26 23:42:26 PDT 2008


Author: dpatel
Date: Sun Apr 27 01:42:26 2008
New Revision: 50317

URL: http://llvm.org/viewvc/llvm-project?rev=50317&view=rev
Log:
Initialize padding bytes.

Modified:
    llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp

Modified: llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp?rev=50317&r1=50316&r2=50317&view=diff

==============================================================================
--- llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.2/branches/Apple/Tak/gcc/llvm-convert.cpp Sun Apr 27 01:42:26 2008
@@ -6251,7 +6251,7 @@
         return ConstantArray::get(ATy, ArrayElts);
       }
   }
-  
+
   // Otherwise, we can get away with this initialization.
   assert(TD.getABITypeAlignment(FieldTy) >= 
          TD.getABITypeAlignment(Val->getType()) &&
@@ -6262,7 +6262,7 @@
 
 Constant *TreeConstantToLLVM::ConvertRecordCONSTRUCTOR(tree exp) {
   const StructType *STy = cast<StructType>(ConvertType(TREE_TYPE(exp)));
-
+  bool OneEltIsUnion = false;
   std::vector<Constant*> ResultElts;
   ResultElts.resize(STy->getNumElements());
 
@@ -6282,9 +6282,12 @@
       }
     }
 
+   if (TREE_CODE(TREE_TYPE(elt_value)) == UNION_TYPE)
+     OneEltIsUnion = true;
+   
     // Decode the field's value.
     Constant *Val = Convert(elt_value);
-
+   
     // If the field is a bitfield, it could be spread across multiple fields and
     // may start at some bit offset.
     if (isBitfield(Field)) {
@@ -6293,7 +6296,7 @@
       // If not, things are much simpler.
       unsigned int FieldNo = GetFieldIndex(Field);
       assert(FieldNo < ResultElts.size() && "Invalid struct field number!");
-
+     
       // Example: struct X { int A; char C[]; } x = { 4, "foo" };
       assert((TYPE_SIZE(getDeclaredType(Field)) ||
              (FieldNo == ResultElts.size()-1 &&
@@ -6322,10 +6325,31 @@
   }
   
   // Fill in null elements with zeros.
-  for (unsigned i = 0, e = ResultElts.size(); i != e; ++i)
+  unsigned InitSize = 0;
+  for (unsigned i = 0, e = ResultElts.size(); i != e; ++i) {
     if (ResultElts[i] == 0)
       ResultElts[i] = Constant::getNullValue(STy->getElementType(i));
-  
+    InitSize += getTargetData().getABITypeSize(ResultElts[i]->getType());
+  }
+
+  // If the struct has a fixed size, and if one the union value we converted isn't 
+  // large enough to fill all the bits, add a zero initialized array at the end to pad
+  // it out.
+  tree StructType = TREE_TYPE(exp);
+  if (OneEltIsUnion && 
+      TYPE_SIZE(StructType) && TREE_CODE(TYPE_SIZE(StructType)) == INTEGER_CST) {
+    unsigned StructSize = ((unsigned)TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(exp)))+7)/8;
+    if (StructSize != InitSize) {
+      const Type *FillTy;
+      assert(StructSize > InitSize && "Init shouldn't be larger than struct!");
+      if (StructSize - InitSize == 1)
+        FillTy = Type::Int8Ty;
+      else
+        FillTy = ArrayType::get(Type::Int8Ty, StructSize - InitSize);
+      ResultElts.push_back(Constant::getNullValue(FillTy));
+    }
+  }
+
   return ConstantStruct::get(ResultElts, STy->isPacked());
 }
 





More information about the llvm-commits mailing list