Home | History | Annotate | Download | only in CodeGen
      1 // RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X64
      2 // RUN: %clang_cc1 -fblocks -triple i686-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X32
      3 // RUN: %clang_cc1 -fblocks -triple arm64-apple-darwin %s -emit-llvm -o - | FileCheck %s -check-prefix=ARM64
      4 
      5 // X64:   internal constant {{.*}} { i8** @_NSConcreteGlobalBlock, i32 1879048192
      6 // X64:   store i32 1610612736, i32* %want
      7 
      8 // X32:   @_NSConcreteGlobalBlock, i32 1879048192, i32 0,
      9 // X32:   store i32 1610612736, i32* %want
     10 
     11 // rdar://7677537
     12 
     13 // ARM64: @_NSConcreteGlobalBlock, i32 1342177280, i32 0,
     14 // ARM64: store i32 1610612736, i32* %want
     15 
     16 // rdar://9757126
     17 
     18 int printf(const char *, ...);
     19 void *malloc(__SIZE_TYPE__ size);
     20 
     21 typedef struct bigbig {
     22    int array[512];
     23    char more[32];
     24 } BigStruct_t;
     25 
     26 BigStruct_t (^global)(void) = ^{ return *(BigStruct_t *)malloc(sizeof(struct bigbig)); };
     27 
     28 const char * getBlockSignature(void *);
     29 
     30 BigStruct_t foo(int param) {
     31    BigStruct_t x;
     32    BigStruct_t (^f)(int) = ^(int param) {
     33      BigStruct_t *result = malloc(sizeof(BigStruct_t));
     34      result->array[23] = param;
     35      return *result;
     36    };
     37    getBlockSignature(f);
     38    return x;
     39 }
     40 
     41 enum {
     42     BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
     43     BLOCK_HAS_CXX_OBJ =       (1 << 26),
     44     BLOCK_IS_GLOBAL =         (1 << 28),
     45     BLOCK_USE_STRET =    (1 << 29),
     46     BLOCK_HAS_OBJC_TYPE  =    (1 << 30)
     47 };
     48 
     49 struct block_descriptor_big {
     50     unsigned long int reserved;
     51     unsigned long int size;
     52     void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
     53     void (*dispose)(void *);            // conditional on BLOCK_HAS_COPY_DISPOSE
     54     const char *signature;                  // conditional on BLOCK_HAS_OBJC
     55     const char *layout;                 // conditional on BLOCK_HAS_OBJC
     56 };
     57 struct block_descriptor_small {
     58     unsigned long int reserved;
     59     unsigned long int size;
     60     const char *signature;              // conditional on BLOCK_HAS_OBJC
     61     const char *layout;                 // conditional on BLOCK_HAS_OBJC
     62 };
     63 
     64 struct block_layout_abi { // can't change
     65   void *isa;
     66   int flags;
     67   int reserved;
     68   void (*invoke)(void *, ...);
     69   struct block_descriptor_big *descriptor;
     70 };
     71 
     72 const char *getBlockSignature(void *block) {
     73    struct block_layout_abi *layout = (struct block_layout_abi *)block;
     74    if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return 0;
     75    if (layout->flags & BLOCK_HAS_COPY_DISPOSE)
     76       return layout->descriptor->signature;
     77    else
     78       return ((struct block_descriptor_small *)layout->descriptor)->signature;
     79 }
     80 
     81 int usesStruct(void *block) {
     82    struct block_layout_abi *layout = (struct block_layout_abi *)block;
     83    int want = BLOCK_HAS_OBJC_TYPE | BLOCK_USE_STRET;
     84    return (layout->flags & want) == want;
     85 }
     86 
     87 
     88 int main(int argc, char *argv[]) {
     89    printf("desired global flags: %d\n", BLOCK_USE_STRET | BLOCK_IS_GLOBAL  | BLOCK_HAS_OBJC_TYPE);
     90    printf("desired stack flags: %d\n",  BLOCK_USE_STRET | BLOCK_HAS_OBJC_TYPE);
     91 
     92    printf("should be non-zero: %d\n", usesStruct(global));
     93    BigStruct_t x;
     94    BigStruct_t (^local)(int) = ^(int param) {
     95      BigStruct_t *result = (BigStruct_t *)malloc(sizeof(BigStruct_t));
     96      result->array[23] = argc;
     97      return *result;
     98    };
     99    printf("should be non-zero: %d\n", usesStruct(global));
    100    printf("should be non-zero: %d\n", usesStruct(local));
    101    printf("should be zero: %d\n", usesStruct(^void(int x){ }));
    102    return 0;
    103 }
    104 
    105 /*
    106 desired global flags: 1879048192
    107 desired stack flags: 1610612736
    108 should be non-zero: 1
    109 should be non-zero: 1
    110 should be non-zero: 1
    111 should be zero: 0
    112 
    113 */
    114