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