[cfe-commits] r65688 - in /cfe/trunk/lib/CodeGen: CGBlocks.cpp CGDecl.cpp CGExpr.cpp CGExprScalar.cpp CodeGenFunction.h

Mike Stump mrs at apple.com
Sat Feb 28 01:07:18 PST 2009


Author: mrs
Date: Sat Feb 28 03:07:16 2009
New Revision: 65688

URL: http://llvm.org/viewvc/llvm-project?rev=65688&view=rev
Log:
First cut CodeGen support for __block variables.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Sat Feb 28 03:07:16 2009
@@ -154,7 +154,7 @@
         Name = ND->getNameAsCString();
     BlockInfo Info(0, Name);
     uint64_t subBlockSize, subBlockAlign;
-    llvm::SmallVector<ValueDecl *, 8> subBlockDeclRefDecls;
+    llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
     llvm::Function *Fn
       = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
                                                    subBlockAlign, subBlockDeclRefDecls);
@@ -163,7 +163,6 @@
     // __descriptor
     Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
 
-    // FIXME: Also check to make sure there are no byref variables
     if (subBlockDeclRefDecls.size() == 0) {
       C = llvm::ConstantStruct::get(Elts);
 
@@ -181,8 +180,14 @@
     for (int i=0; i<5; ++i)
       Types[i] = Elts[i]->getType();
 
-    for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
-      Types[i+5] = ConvertType(subBlockDeclRefDecls[i]->getType());
+    for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
+      const Expr *E = subBlockDeclRefDecls[i];
+      const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
+      QualType Ty = E->getType();
+      if (BDRE && BDRE->isByRef())
+        Ty = getContext().getPointerType(Ty);
+      Types[i+5] = ConvertType(Ty);
+    }
 
     llvm::Type *Ty = llvm::StructType::get(Types, true);
 
@@ -195,14 +200,32 @@
     
     for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
       {
-        ValueDecl *VD = subBlockDeclRefDecls[i];
+        // FIXME: Push const down.
+        Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
+        DeclRefExpr *DR;
+        ValueDecl *VD;
+
+        DR = dyn_cast<DeclRefExpr>(E);
+        // Skip padding.
+        if (DR) continue;
+
+        BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
+        VD = BDRE->getDecl();
+
+        // FIXME: I want a better way to do this.
+        if (LocalDeclMap[VD]) {
+          E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
+                                              VD->getType(), SourceLocation(),
+                                              false, false);
+        }
+        if (BDRE->isByRef())
+          E = new (getContext())
+            UnaryOperator(E, UnaryOperator::AddrOf,
+                          getContext().getPointerType(E->getType()),
+                          SourceLocation());
 
-        if (VD->getIdentifier() == 0)
-            continue;
-        SourceLocation Loc = VD->getLocation();
-        DeclRefExpr D(VD, VD->getType(), Loc);
         llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
-        RValue r = EmitAnyExpr(&D, Addr, false);
+        RValue r = EmitAnyExpr(E, Addr, false);
         if (r.isScalar())
           Builder.CreateStore(r.getScalarVal(), Addr);
         else if (r.isComplex())
@@ -215,8 +238,6 @@
         // FIXME: Ensure that the offset created by the backend for
         // the struct matches the previously computed offset in BlockDecls.
       }
-
-    // FIXME: Add block_byref_decl_list.
   }
   
   QualType BPT = BE->getType();
@@ -412,7 +433,7 @@
 
   CodeGenFunction::BlockInfo Info(0, n);
   uint64_t subBlockSize, subBlockAlign;
-  llvm::SmallVector<ValueDecl *, 8> subBlockDeclRefDecls;
+  llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
   llvm::Function *Fn
     = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
                                                    subBlockAlign,
@@ -455,7 +476,7 @@
                                                        const BlockInfo& Info,
                                                        uint64_t &Size,
                                                        uint64_t &Align,
-                    llvm::SmallVector<ValueDecl *, 8> &subBlockDeclRefDecls) {
+                         llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
   const FunctionProtoType *FTy =
     cast<FunctionProtoType>(Expr->getFunctionType());
 
@@ -503,3 +524,42 @@
 
   return Fn;
 }
+
+uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
+  const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
+
+  uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
+  uint64_t Align = getContext().getDeclAlignInBytes(D);
+
+  if (BDRE->isByRef()) {
+    Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
+    Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
+  }
+
+  assert ((Align > 0) && "alignment must be 1 byte or more");
+
+  uint64_t OldOffset = BlockOffset;
+
+  // Ensure proper alignment, even if it means we have to have a gap
+  BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
+  BlockAlign = std::max(Align, BlockAlign);
+      
+  uint64_t Pad = BlockOffset - OldOffset;
+  if (Pad) {
+    llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
+    QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
+                                                       llvm::APInt(32, Pad),
+                                                       ArrayType::Normal, 0);
+    ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
+                                         0, QualType(PadTy), VarDecl::None,
+                                         SourceLocation());
+    Expr *E;
+    E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
+                                       SourceLocation(), false, false);
+    BlockDeclRefDecls.push_back(E);
+  }
+  BlockDeclRefDecls.push_back(BDRE);
+
+  BlockOffset += Size;
+  return BlockOffset-Size;
+}

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Sat Feb 28 03:07:16 2009
@@ -88,6 +88,10 @@
   else if (isa<ObjCMethodDecl>(CurFuncDecl))
     ContextName = std::string(CurFn->getNameStart(), 
                               CurFn->getNameStart() + CurFn->getNameLen());
+  else if (isa<BlockDecl>(CurFuncDecl))
+    // FIXME: We want to traverse up and pick a name based upon where we came
+    // from.
+    ContextName = "block";
   else
     assert(0 && "Unknown context for block var decl");
 

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sat Feb 28 03:07:16 2009
@@ -151,6 +151,9 @@
   case Expr::ObjCEncodeExprClass:
     return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
 
+  case Expr::BlockDeclRefExprClass: 
+    return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
+
   case Expr::CXXConditionDeclExprClass:
     return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
 
@@ -627,7 +630,7 @@
     }
     else {
       llvm::Value *V = LocalDeclMap[VD];
-      assert(V && "BlockVarDecl not entered in LocalDeclMap?");
+      assert(V && "DeclRefExpr not entered in LocalDeclMap?");
       // local variables do not get their gc attribute set.
       QualType::GCAttrTypes attr = QualType::GCNone;
       // local static?
@@ -660,6 +663,38 @@
   return LValue();
 }
 
+LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
+  return LValue::MakeAddr(GetAddrOfBlockDecl(E), 0);
+}
+
+llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
+  // FIXME: ensure we don't need copy/dispose.
+  uint64_t &offset = BlockDecls[E->getDecl()];
+
+  const llvm::Type *Ty;
+  Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
+
+  // See if we have already allocated an offset for this variable.
+  if (offset == 0) {
+    // if not, allocate one now.
+    offset = getBlockOffset(E);
+  }
+
+  llvm::Value *BlockLiteral = LoadBlockStruct();
+  llvm::Value *V = Builder.CreateGEP(BlockLiteral,
+                                     llvm::ConstantInt::get(llvm::Type::Int64Ty,
+                                                            offset),
+                                     "tmp");
+  Ty = llvm::PointerType::get(Ty, 0);
+  if (E->isByRef())
+    Ty = llvm::PointerType::get(Ty, 0);
+  V = Builder.CreateBitCast(V, Ty);
+  if (E->isByRef())
+    V = Builder.CreateLoad(V, false, "tmp");
+
+  return V;
+}
+
 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
   // __extension__ doesn't affect lvalue-ness.
   if (E->getOpcode() == UnaryOperator::Extension)

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sat Feb 28 03:07:16 2009
@@ -224,7 +224,7 @@
 
   Value *VisitStmtExpr(const StmtExpr *E);
 
-  Value *VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
+  Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
   
   // Unary Operators.
   Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
@@ -601,37 +601,8 @@
                               !E->getType()->isVoidType()).getScalarVal();
 }
 
-Value *ScalarExprEmitter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
-  if (E->isByRef()) {
-    // FIXME: Add codegen for __block variables.
-    return VisitExpr(E);
-  }
-
-  // FIXME: ensure we don't need copy/dispose.
-  uint64_t &offset = CGF.BlockDecls[E->getDecl()];
-
-  const llvm::Type *Ty;
-  Ty = CGF.CGM.getTypes().ConvertType(E->getDecl()->getType());
-
-  // See if we have already allocated an offset for this variable.
-  if (offset == 0) {
-    // if not, allocate one now.
-    offset = CGF.getBlockOffset(E->getDecl());
-  }
-
-  llvm::Value *BlockLiteral = CGF.LoadBlockStruct();
-  llvm::Value *V = Builder.CreateGEP(BlockLiteral,
-                                     llvm::ConstantInt::get(llvm::Type::Int64Ty,
-                                                            offset),
-                                     "tmp");
-  Ty = llvm::PointerType::get(Ty, 0);
-  if (E->isByRef())
-    Ty = llvm::PointerType::get(Ty, 0);
-  V = Builder.CreateBitCast(V, Ty);
-  V = Builder.CreateLoad(V, false, "tmp");
-  if (E->isByRef())
-    V = Builder.CreateLoad(V, false, "tmp");
-  return V;
+Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
+  return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=65688&r1=65687&r2=65688&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sat Feb 28 03:07:16 2009
@@ -282,7 +282,7 @@
   llvm::Function *GenerateBlockFunction(const BlockExpr *Expr,
                                         const BlockInfo& Info,
                                         uint64_t &Size, uint64_t &Align,
-                                        llvm::SmallVector<ValueDecl *, 8> &subBlockDeclRefDecls);
+                                        llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls);
 
   ImplicitParamDecl *BlockStructDecl;
 
@@ -295,40 +295,21 @@
 
   /// BlockDeclRefDecls - Decls from BlockDeclRefExprs in apperance order
   /// in a block literal.  Decls without names are used for padding.
-  llvm::SmallVector<ValueDecl *, 8> BlockDeclRefDecls;
+  llvm::SmallVector<const Expr *, 8> BlockDeclRefDecls;
 
   /// BlockOffset - The offset in bytes for the next allocation of an
   /// imported block variable.
   uint64_t BlockOffset;
   /// BlockAlign - Maximal alignment needed for the Block expressed in bytes.
   uint64_t BlockAlign;
-  /// getBlockOffset - Offset for next allocated variable used in a BlockExpr.
-  uint64_t getBlockOffset(ValueDecl *D) {
-    uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
-    uint64_t Align = getContext().getDeclAlignInBytes(D);
-
-    assert ((Align > 0) && "alignment must be 1 byte or more");
-
-    uint64_t OldOffset = BlockOffset;
-
-    // Ensure proper alignment, even if it means we have to have a gap
-    BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
-    BlockAlign = std::max(Align, BlockAlign);
-      
-    uint64_t Pad = BlockOffset - OldOffset;
-    llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
-    QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
-                                                       llvm::APInt(32, Pad),
-                                                       ArrayType::Normal, 0);
-    ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
-                                         0, QualType(PadTy), VarDecl::None, SourceLocation());
-    BlockDeclRefDecls.push_back(PadDecl);
-    BlockDeclRefDecls.push_back(D);
+  /// getBlockOffset - Allocate an offset for the ValueDecl from a
+  /// BlockDeclRefExpr in a block literal (BlockExpr).
+  uint64_t getBlockOffset(const BlockDeclRefExpr *E);
 
-    BlockOffset += Size;
-    return BlockOffset-Size;
-  }
-  std::map<Decl*, uint64_t> BlockDecls;
+  /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
+  std::map<const Decl*, uint64_t> BlockDecls;
+
+  llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E);
 
   void GenerateCode(const FunctionDecl *FD,
                     llvm::Function *Fn);
@@ -665,6 +646,8 @@
   LValue EmitLValueForBitfield(llvm::Value* Base, FieldDecl* Field,
                                 unsigned CVRQualifiers);
 
+  LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
+
   LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E);
 
   LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);





More information about the cfe-commits mailing list