]> granicus.if.org Git - clang/commitdiff
add support for a 1<<29 bit in the block flags field to mark blocks using alternate...
authorBlaine Garst <blaine@apple.com>
Fri, 5 Mar 2010 01:29:59 +0000 (01:29 +0000)
committerBlaine Garst <blaine@apple.com>
Fri, 5 Mar 2010 01:29:59 +0000 (01:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97775 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBlocks.cpp
lib/CodeGen/CGBlocks.h
test/CodeGen/blockstret.c [new file with mode: 0644]

index 7076067e4381b2b050bf19cc841b3cb6236f9e91..c10a401d8abfa0b4e52ffab27168860a3aa498bc 100644 (file)
@@ -154,7 +154,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
 
   {
     // C = BuildBlockStructInitlist();
-    unsigned int flags = BLOCK_HAS_OBJC_TYPE;
+    unsigned int flags = BLOCK_HAS_SIGNATURE;
 
     // We run this first so that we set BlockHasCopyDispose from the entire
     // block literal.
@@ -184,6 +184,18 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
     Elts[0] = C;
 
     // __flags
+    {
+      QualType BPT = BE->getType();
+      const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
+      QualType ResultType = ftype->getResultType();
+    
+      CallArgList Args;
+      CodeGenTypes &Types = CGM.getTypes();
+      const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
+                                                       CC_Default, false);
+      if (CGM.ReturnTypeUsesSret(FnInfo))
+        flags |= BLOCK_USE_STRET;
+    }
     const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
       CGM.getTypes().ConvertType(CGM.getContext().IntTy));
     C = llvm::ConstantInt::get(IntTy, flags);
@@ -200,6 +212,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
 
       // Optimize to being a global block.
       Elts[0] = CGM.getNSConcreteGlobalBlock();
+      
       Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
 
       C = llvm::ConstantStruct::get(VMContext, Elts, false);
@@ -604,7 +617,7 @@ BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
 
   // Flags
   LiteralFields[1] =
-    llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
+    llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
 
   // Reserved
   LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
index 39f26f8b1363e66522db2072a98d99b1731ba50b..e91319f6ddab653fd57ab01e0656a383b5d5fb9f 100644 (file)
@@ -54,10 +54,12 @@ public:
         BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
         BLOCK_HAS_CXX_OBJ =       (1 << 26),
         BLOCK_IS_GLOBAL =         (1 << 28),
-        BLOCK_HAS_OBJC_TYPE  =    (1 << 30)
+        BLOCK_USE_STRET =         (1 << 29),
+        BLOCK_HAS_SIGNATURE  =    (1 << 30)
     };
 };
 
+
 class BlockModule : public BlockBase {
   ASTContext &Context;
   llvm::Module &TheModule;
diff --git a/test/CodeGen/blockstret.c b/test/CodeGen/blockstret.c
new file mode 100644 (file)
index 0000000..abe689e
--- /dev/null
@@ -0,0 +1,110 @@
+// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X64
+// RUN: %clang_cc1 -fblocks -triple i686-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X32
+
+// X64:   internal constant %2 { i8** @_NSConcreteGlobalBlock, i32 1879048192
+// X64:     store i32 1610612736, i32* %want
+
+// X32:   @_NSConcreteGlobalBlock, i32 1879048192, i32 0,
+// X32:   store i32 1610612736, i32* %want
+
+// rdar://7677537
+
+#include <stdlib.h>
+
+typedef struct bigbig {
+   int array[512];
+   char more[32];
+} BigStruct_t;
+
+BigStruct_t (^global)(void) = ^{ return *(BigStruct_t *)malloc(sizeof(struct bigbig)); };
+
+const char * getBlockSignature(void *);
+BigStruct_t foo(int param) {
+   BigStruct_t x;
+   BigStruct_t (^f)(int) = ^(int param) {
+     BigStruct_t *result = malloc(sizeof(BigStruct_t));
+     result->array[23] = param;
+     return *result;
+   };
+   getBlockSignature(f);
+   return x;
+}
+
+#if 1
+#include <stdio.h>
+#include <stdbool.h>
+enum {
+    BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
+    BLOCK_HAS_CXX_OBJ =       (1 << 26),
+    BLOCK_IS_GLOBAL =         (1 << 28),
+    BLOCK_USE_STRET =    (1 << 29),
+    BLOCK_HAS_OBJC_TYPE  =    (1 << 30)
+};
+
+struct block_descriptor_big {
+    unsigned long int reserved;
+    unsigned long int size;
+    void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
+    void (*dispose)(void *);            // conditional on BLOCK_HAS_COPY_DISPOSE
+    const char *signature;                  // conditional on BLOCK_HAS_OBJC
+    const char *layout;                 // conditional on BLOCK_HAS_OBJC
+};
+struct block_descriptor_small {
+    unsigned long int reserved;
+    unsigned long int size;
+    const char *signature;              // conditional on BLOCK_HAS_OBJC
+    const char *layout;                 // conditional on BLOCK_HAS_OBJC
+};
+
+struct block_layout_abi { // can't change
+  void *isa;
+  int flags;
+  int reserved; 
+  void (*invoke)(void *, ...);
+  struct block_descriptor_big *descriptor;
+};
+
+const char *getBlockSignature(void *block) {
+   struct block_layout_abi *layout = (struct block_layout_abi *)block;
+   if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL;
+   if (layout->flags & BLOCK_HAS_COPY_DISPOSE) 
+      return layout->descriptor->signature;
+   else
+      return ((struct block_descriptor_small *)layout->descriptor)->signature;
+}
+
+bool usesStruct(void *block) {
+   struct block_layout_abi *layout = (struct block_layout_abi *)block;
+   int want = BLOCK_HAS_OBJC_TYPE | BLOCK_USE_STRET;
+   return (layout->flags & want) == want;
+}
+    
+   
+int main(int argc, char *argv[]) {
+   printf("desired global flags: %d\n", BLOCK_USE_STRET | BLOCK_IS_GLOBAL  | BLOCK_HAS_OBJC_TYPE);
+   printf("desired stack flags: %d\n",  BLOCK_USE_STRET | BLOCK_HAS_OBJC_TYPE);
+   
+   printf("should be non-zero: %d\n", usesStruct(global));
+   BigStruct_t x;
+   BigStruct_t (^local)(int) = ^(int param) {
+     BigStruct_t *result = (BigStruct_t *)malloc(sizeof(BigStruct_t));
+     result->array[23] = argc;
+     return *result;
+   };
+   printf("should be non-zero: %d\n", usesStruct(global));
+   printf("should be non-zero: %d\n", usesStruct(local));
+   printf("should be zero: %d\n", usesStruct(^void(int x){ }));
+   return 0;
+}
+
+/*
+desired global flags: 1879048192
+desired stack flags: 1610612736
+should be non-zero: 0
+should be non-zero: 0
+should be non-zero: 1
+should be zero: 0
+
+*/
+#endif