blob: edd1218b13e86463330c7c87a531a11d8be4d773 [file] [log] [blame]
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +00001// 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
Stephen Hines651f13c2014-04-23 16:59:28 -07003// RUN: %clang_cc1 -fblocks -triple arm64-apple-darwin %s -emit-llvm -o - | FileCheck %s -check-prefix=ARM64
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +00004
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005// X64: internal constant {{.*}} { i8** @_NSConcreteGlobalBlock, i32 1879048192
Stephen Hines651f13c2014-04-23 16:59:28 -07006// X64: store i32 1610612736, i32* %want
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +00007
8// X32: @_NSConcreteGlobalBlock, i32 1879048192, i32 0,
9// X32: store i32 1610612736, i32* %want
10
11// rdar://7677537
Stephen Hines651f13c2014-04-23 16:59:28 -070012
13// ARM64: @_NSConcreteGlobalBlock, i32 1342177280, i32 0,
14// ARM64: store i32 1610612736, i32* %want
15
16// rdar://9757126
17
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +000018int printf(const char *, ...);
19void *malloc(__SIZE_TYPE__ size);
20
21typedef struct bigbig {
22 int array[512];
23 char more[32];
24} BigStruct_t;
25
26BigStruct_t (^global)(void) = ^{ return *(BigStruct_t *)malloc(sizeof(struct bigbig)); };
27
28const char * getBlockSignature(void *);
29
30BigStruct_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
41enum {
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
49struct 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};
57struct 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
64struct 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
72const 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
81int 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
88int 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/*
106desired global flags: 1879048192
107desired stack flags: 1610612736
108should be non-zero: 1
109should be non-zero: 1
110should be non-zero: 1
111should be zero: 0
112
113*/