[PATCH] D47953: [builtin] Add bitfield support for __builtin_dump_struct

Paul Semel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 29 09:46:43 PDT 2018


paulsemel updated this revision to Diff 153511.
paulsemel added a comment.

Fixed version problem. Now building.


Repository:
  rC Clang

https://reviews.llvm.org/D47953

Files:
  lib/CodeGen/CGBuiltin.cpp


Index: lib/CodeGen/CGBuiltin.cpp
===================================================================
--- lib/CodeGen/CGBuiltin.cpp
+++ lib/CodeGen/CGBuiltin.cpp
@@ -1170,6 +1170,7 @@
   RecordDecl *RD = RT->getDecl()->getDefinition();
   ASTContext &Ctx = RD->getASTContext();
   const ASTRecordLayout &RL = Ctx.getASTRecordLayout(RD);
+  const CGRecordLayout &CGRL = CGF.getTypes().getCGRecordLayout(RD);
   std::string Pad = std::string(Lvl * 4, ' ');
 
   Value *GString =
@@ -1208,7 +1209,7 @@
           FieldPtr, CGF.ConvertType(Context.getPointerType(FD->getType())));
     else
       FieldPtr = CGF.Builder.CreateStructGEP(CGF.ConvertType(RType), FieldPtr,
-                                             FD->getFieldIndex());
+                                             CGRL.getLLVMFieldNo(FD));
 
     GString = CGF.Builder.CreateGlobalStringPtr(
         llvm::Twine(Pad)
@@ -1236,10 +1237,37 @@
                              ? Types[Context.VoidPtrTy]
                              : Types[CanonicalType];
 
+    if (FD->isBitField())
+      FieldPtr = CGF.Builder.CreatePointerCast(
+          FieldPtr, CGF.ConvertType(Context.getPointerType(CanonicalType)));
+
     Address FieldAddress = Address(FieldPtr, Align);
     FieldPtr = CGF.Builder.CreateLoad(FieldAddress);
 
-    // FIXME Need to handle bitfield here
+    if (FD->isBitField()) {
+      const CGBitFieldInfo &Info = CGRL.getBitFieldInfo(FD);
+      if (Info.IsSigned) {
+        unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size;
+        if (HighBits)
+          FieldPtr = CGF.Builder.CreateShl(FieldPtr, HighBits);
+        if (Info.Offset + HighBits)
+          FieldPtr = CGF.Builder.CreateAShr(FieldPtr, Info.Offset + HighBits);
+      } else {
+        if (Info.Offset)
+          FieldPtr = CGF.Builder.CreateLShr(FieldPtr, Info.Offset);
+        if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize)
+          FieldPtr = CGF.Builder.CreateAnd(
+              FieldPtr,
+              ConstantInt::get(CGF.ConvertType(CanonicalType),
+                               llvm::APInt::getLowBitsSet(
+                                   CGF.CGM.getDataLayout().getTypeSizeInBits(
+                                       CGF.ConvertType(CanonicalType)),
+                                   Info.Size)));
+      }
+      FieldPtr = CGF.Builder.CreateIntCast(
+          FieldPtr, CGF.ConvertType(CanonicalType), Info.IsSigned);
+    }
+
     GString = CGF.Builder.CreateGlobalStringPtr(
         Format.concat(llvm::Twine('\n')).str());
     TmpRes = CGF.Builder.CreateCall(Func, {GString, FieldPtr});


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47953.153511.patch
Type: text/x-patch
Size: 2611 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180629/8256f8bd/attachment.bin>


More information about the cfe-commits mailing list