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