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 
      4 // X64: @.str = private unnamed_addr constant [6 x i8] c"v8@?0\00"
      5 // X64: @__block_literal_global = internal constant {{.*}} { i8** @_NSConcreteGlobalBlock, i32 1342177280,
      6 // X64: @.str.1 = private unnamed_addr constant [12 x i8] c"i16@?0c8f12\00"
      7 // X64:   store i32 1073741824, i32*
      8 
      9 // X32: [[STR1:@.*]] = private unnamed_addr constant [6 x i8] c"v4@?0\00"
     10 // X32: @__block_descriptor_tmp = internal constant [[FULL_DESCRIPTOR_T:.*]] { i32 0, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* [[STR1]], i32 0, i32 0), i8* null }
     11 // X32: @__block_literal_global = internal constant [[GLOBAL_LITERAL_T:.*]] { i8** @_NSConcreteGlobalBlock, i32 1342177280, i32 0, i8* bitcast (void (i8*)* @global_block_invoke{{.*}} to i8*), [[DESCRIPTOR_T:%.*]]* bitcast ([[FULL_DESCRIPTOR_T]]* @__block_descriptor_tmp to {{%.*}}*) }
     12 // X32: [[STR2:@.*]] = private unnamed_addr constant [11 x i8] c"i12@?0c4f8\00"
     13 // X32: @__block_descriptor_tmp{{.*}} = internal constant [[FULL_DESCRIPTOR_T]] { i32 0, i32 24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* [[STR2]], i32 0, i32 0), i8* null }
     14 // X32:   store i32 1073741824, i32*
     15 
     16 // rdar://7635294
     17 
     18 
     19 int globalInt;
     20 void (^global)(void) = ^{ ++globalInt; };
     21 
     22 
     23 void foo(int param) {
     24    extern int rand(void);
     25    extern void rand_r(int (^b)(char x, float y));   // name a function present at runtime
     26    while (param--)
     27       rand_r(^(char x, float y){ return x + (int)y + param + rand(); });  // generate a local block binding param
     28 }
     29 
     30 #if 0
     31 #include <stdio.h>
     32 enum {
     33     BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
     34     BLOCK_HAS_CXX_OBJ =       (1 << 26),
     35     BLOCK_IS_GLOBAL =         (1 << 28),
     36     BLOCK_HAS_DESCRIPTOR =    (1 << 29),
     37     BLOCK_HAS_OBJC_TYPE  =    (1 << 30)
     38 };
     39 
     40 struct block_descriptor_big {
     41     unsigned long int reserved;
     42     unsigned long int size;
     43     void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
     44     void (*dispose)(void *);            // conditional on BLOCK_HAS_COPY_DISPOSE
     45     const char *signature;                  // conditional on BLOCK_HAS_OBJC
     46     const char *layout;                 // conditional on BLOCK_HAS_OBJC
     47 };
     48 struct block_descriptor_small {
     49     unsigned long int reserved;
     50     unsigned long int size;
     51     const char *signature;              // conditional on BLOCK_HAS_OBJC
     52     const char *layout;                 // conditional on BLOCK_HAS_OBJC
     53 };
     54 
     55 struct block_layout_abi { // can't change
     56   void *isa;
     57   int flags;
     58   int reserved;
     59   void (*invoke)(void *, ...);
     60   struct block_descriptor_big *descriptor;
     61 };
     62 
     63 const char *getBlockSignature(void *block) {
     64    struct block_layout_abi *layout = (struct block_layout_abi *)block;
     65    if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL;
     66    if (layout->flags & BLOCK_HAS_COPY_DISPOSE)
     67       return layout->descriptor->signature;
     68    else
     69       return ((struct block_descriptor_small *)layout->descriptor)->signature;
     70 }
     71 
     72 
     73 
     74 int main(int argc, char *argv[]) {
     75    printf("desired global flags: %d\n", BLOCK_IS_GLOBAL  | BLOCK_HAS_OBJC_TYPE);
     76    printf("desired stack flags: %d\n",  BLOCK_HAS_OBJC_TYPE);
     77 
     78    printf("types for global: %s\n", getBlockSignature(global));
     79    printf("types for local: %s\n", getBlockSignature(^int(char x, float y) { return (int)(y + x); }));
     80    return 0;
     81 }
     82 
     83 /*
     84 x86_64
     85 desired global flags: 1342177280
     86 desired stack flags: 1073741824
     87 types for global: v8@?0
     88 types for local: i16@?0c8f12
     89 
     90 i386
     91 desired global flags: 1342177280
     92 desired stack flags: 1073741824
     93 types for global: v4@?0
     94 types for local: i12@?0c4f8
     95 */
     96 #endif
     97