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