[cfe-commits] r66231 - in /cfe/trunk: lib/CodeGen/CGBlocks.cpp lib/CodeGen/CGBlocks.h lib/CodeGen/CGDecl.cpp lib/CodeGen/CodeGenFunction.cpp test/CodeGen/blocks-1.c

Mike Stump mrs at apple.com
Thu Mar 5 17:33:24 PST 2009


Author: mrs
Date: Thu Mar  5 19:33:24 2009
New Revision: 66231

URL: http://llvm.org/viewvc/llvm-project?rev=66231&view=rev
Log:
Framework for codegen for copy/dispose helpers.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/lib/CodeGen/CGBlocks.h
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/test/CodeGen/blocks-1.c

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Thu Mar  5 19:33:24 2009
@@ -465,13 +465,13 @@
   // FIXME: add support for copy/dispose helpers.
   if (!Enable__block && E->isByRef())
     ErrorUnsupported(E, "__block variable in block literal");
-  else if (E->getType()->isBlockPointerType())
+  else if (!Enable__block && E->getType()->isBlockPointerType())
     ErrorUnsupported(E, "block pointer in block literal");
   else if (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
            getContext().isObjCNSObjectType(E->getType()))
     ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
                      "literal");
-  else if (getContext().isObjCObjectPointerType(E->getType()))
+  else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType()))
     ErrorUnsupported(E, "Objective-C variable in block literal");
 
   // See if we have already allocated an offset for this variable.
@@ -677,20 +677,88 @@
   return BlockOffset-Size;
 }
 
-llvm::Value *BlockFunction::BuildCopyHelper(int flag) {
-  // FIXME: implement
-  llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 43);
-  V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
-  V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
-  return V;
+llvm::Constant *BlockFunction::GenerateCopyHelperFunction() {
+  QualType R = getContext().VoidTy;
+
+  FunctionArgList Args;
+  // FIXME: This leaks
+  ImplicitParamDecl *Src =
+    ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
+                              getContext().getPointerType(getContext().VoidTy));
+
+  Args.push_back(std::make_pair(Src, Src->getType()));
+  
+  const CGFunctionInfo &FI =
+    CGM.getTypes().getFunctionInfo(R, Args);
+
+  std::string Name = std::string("__copy_helper_block_");
+  CodeGenTypes &Types = CGM.getTypes();
+  const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
+
+  llvm::Function *Fn =
+    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
+                           Name,
+                           &CGM.getModule());
+
+  IdentifierInfo *II
+    = &CGM.getContext().Idents.get("__copy_helper_block_");
+
+  FunctionDecl *FD = FunctionDecl::Create(getContext(),
+                                          getContext().getTranslationUnitDecl(),
+                                          SourceLocation(), II, R,
+                                          FunctionDecl::Static, false,
+                                          true);
+  CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
+  // EmitStmt(BExpr->getBody());
+  CGF.FinishFunction();
+
+  return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
 }
 
-llvm::Value *BlockFunction::BuildDestroyHelper(int flag) {
-  // FIXME: implement
-  llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 44);
-  V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
-  V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
-  return V;
+llvm::Constant *BlockFunction::GenerateDestroyHelperFunction() {
+  QualType R = getContext().VoidTy;
+
+  FunctionArgList Args;
+  // FIXME: This leaks
+  ImplicitParamDecl *Src =
+    ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
+                              getContext().getPointerType(getContext().VoidTy));
+
+  Args.push_back(std::make_pair(Src, Src->getType()));
+  
+  const CGFunctionInfo &FI =
+    CGM.getTypes().getFunctionInfo(R, Args);
+
+  std::string Name = std::string("__destroy_helper_block_");
+  CodeGenTypes &Types = CGM.getTypes();
+  const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
+
+  llvm::Function *Fn =
+    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
+                           Name,
+                           &CGM.getModule());
+
+  IdentifierInfo *II
+    = &CGM.getContext().Idents.get("__destroy_helper_block_");
+
+  FunctionDecl *FD = FunctionDecl::Create(getContext(),
+                                          getContext().getTranslationUnitDecl(),
+                                          SourceLocation(), II, R,
+                                          FunctionDecl::Static, false,
+                                          true);
+  CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
+  // EmitStmt(BExpr->getBody());
+  CGF.FinishFunction();
+
+  return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
+}
+
+llvm::Constant *BlockFunction::BuildCopyHelper(int flag) {
+  return CodeGenFunction(CGM).GenerateCopyHelperFunction();
+}
+
+llvm::Constant *BlockFunction::BuildDestroyHelper(int flag) {
+  return CodeGenFunction(CGM).GenerateDestroyHelperFunction();
 }
 
 llvm::Value *BlockFunction::getBlockObjectDispose() {

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.h (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.h Thu Mar  5 19:33:24 2009
@@ -110,6 +110,7 @@
 
 class BlockFunction : public BlockBase {
   CodeGenModule &CGM;
+  CodeGenFunction &CGF;
   ASTContext &getContext() const;
 
 public:
@@ -148,16 +149,19 @@
 
   CGBuilderTy &Builder;
 
-  BlockFunction(CodeGenModule &cgm, CGBuilderTy &B)
-    : CGM(cgm), Builder(B) {
+  BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, CGBuilderTy &B)
+    : CGM(cgm), CGF(cgf), Builder(B) {
     PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   }
 
   ImplicitParamDecl *BlockStructDecl;
   ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
 
-  llvm::Value *BuildCopyHelper(int flag);
-  llvm::Value *BuildDestroyHelper(int flag);
+  llvm::Constant *GenerateCopyHelperFunction();
+  llvm::Constant *GenerateDestroyHelperFunction();
+
+  llvm::Constant *BuildCopyHelper(int flag);
+  llvm::Constant *BuildDestroyHelper(int flag);
 
   llvm::Value *getBlockObjectDispose();
   void BuildBlockRelease(const VarDecl &D, llvm::Value *DeclPtr);

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu Mar  5 19:33:24 2009
@@ -354,10 +354,10 @@
     if (flag&BLOCK_FIELD_IS_WEAK)
       isa = 1;
     V = llvm::ConstantInt::get(llvm::Type::Int32Ty, isa);
-    V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
+    V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
     Builder.CreateStore(V, isa_field);
 
-    V = Builder.CreateBitCast(DeclPtr, PtrToInt8Ty, "tmp");
+    V = Builder.CreateBitCast(DeclPtr, PtrToInt8Ty, "forwarding");
     Builder.CreateStore(V, forwarding_field);
 
     V = llvm::ConstantInt::get(llvm::Type::Int32Ty, flags);

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Thu Mar  5 19:33:24 2009
@@ -24,7 +24,8 @@
 using namespace CodeGen;
 
 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 
-  : BlockFunction(cgm, Builder), CGM(cgm), Target(CGM.getContext().Target),
+  : BlockFunction(cgm, *this, Builder), CGM(cgm),
+    Target(CGM.getContext().Target),
     DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0) {
   LLVMIntTy = ConvertType(getContext().IntTy);
   LLVMPointerWidth = Target.getPointerWidth(0);

Modified: cfe/trunk/test/CodeGen/blocks-1.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/blocks-1.c?rev=66231&r1=66230&r2=66231&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/blocks-1.c (original)
+++ cfe/trunk/test/CodeGen/blocks-1.c Thu Mar  5 19:33:24 2009
@@ -1,5 +1,7 @@
 // RUN: clang %s -emit-llvm -o %t -fblocks -f__block &&
-// RUN: grep "_Block_object_dispose" %t | count 3
+// RUN: grep "_Block_object_dispose" %t | count 4
+// RUN: grep "__copy_helper_block_" %t | count 2
+// RUN: grep "__destroy_helper_block_" %t | count 2
 #include <stdio.h>
 
 void test1() {
@@ -28,8 +30,14 @@
   printf("a is %d\n", a);
 }
 
+void test3() {
+  __block int (^j)(int);
+  ^{j=0;}();
+}
+
 int main() {
   test1();
   test2();
+  test3();
   return 0;
 }





More information about the cfe-commits mailing list