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 // RUN: %clang_cc1 -fblocks -triple arm64-apple-darwin %s -emit-llvm -o - | FileCheck %s -check-prefix=ARM64
5 // X64: internal constant {{.*}} { ptr @_NSConcreteGlobalBlock, i32 1879048192
6 // X64: store i32 1610612736, ptr %want
8 // X32: @_NSConcreteGlobalBlock, i32 1879048192, i32 0,
9 // X32: store i32 1610612736, ptr %want
11 // ARM64: @_NSConcreteGlobalBlock, i32 1342177280, i32 0,
12 // ARM64: store i32 1610612736, ptr %want
14 int printf(const char *, ...);
15 void *malloc(__SIZE_TYPE__ size
);
17 typedef struct bigbig
{
22 BigStruct_t (^global
)(void) = ^{ return *(BigStruct_t
*)malloc(sizeof(struct bigbig
)); };
24 const char * getBlockSignature(void *);
26 BigStruct_t
foo(int param
) {
28 BigStruct_t (^f
)(int) = ^(int param
) {
29 BigStruct_t
*result
= malloc(sizeof(BigStruct_t
));
30 result
->array
[23] = param
;
38 BLOCK_HAS_COPY_DISPOSE
= (1 << 25),
39 BLOCK_HAS_CXX_OBJ
= (1 << 26),
40 BLOCK_IS_GLOBAL
= (1 << 28),
41 BLOCK_USE_STRET
= (1 << 29),
42 BLOCK_HAS_OBJC_TYPE
= (1 << 30)
45 struct block_descriptor_big
{
46 unsigned long int reserved
;
47 unsigned long int size
;
48 void (*copy
)(void *dst
, void *src
); // conditional on BLOCK_HAS_COPY_DISPOSE
49 void (*dispose
)(void *); // conditional on BLOCK_HAS_COPY_DISPOSE
50 const char *signature
; // conditional on BLOCK_HAS_OBJC
51 const char *layout
; // conditional on BLOCK_HAS_OBJC
53 struct block_descriptor_small
{
54 unsigned long int reserved
;
55 unsigned long int size
;
56 const char *signature
; // conditional on BLOCK_HAS_OBJC
57 const char *layout
; // conditional on BLOCK_HAS_OBJC
60 struct block_layout_abi
{ // can't change
64 void (*invoke
)(void *, ...);
65 struct block_descriptor_big
*descriptor
;
68 const char *getBlockSignature(void *block
) {
69 struct block_layout_abi
*layout
= (struct block_layout_abi
*)block
;
70 if ((layout
->flags
& BLOCK_HAS_OBJC_TYPE
) != BLOCK_HAS_OBJC_TYPE
) return 0;
71 if (layout
->flags
& BLOCK_HAS_COPY_DISPOSE
)
72 return layout
->descriptor
->signature
;
74 return ((struct block_descriptor_small
*)layout
->descriptor
)->signature
;
77 int usesStruct(void *block
) {
78 struct block_layout_abi
*layout
= (struct block_layout_abi
*)block
;
79 int want
= BLOCK_HAS_OBJC_TYPE
| BLOCK_USE_STRET
;
80 return (layout
->flags
& want
) == want
;
84 int main(int argc
, char *argv
[]) {
85 printf("desired global flags: %d\n", BLOCK_USE_STRET
| BLOCK_IS_GLOBAL
| BLOCK_HAS_OBJC_TYPE
);
86 printf("desired stack flags: %d\n", BLOCK_USE_STRET
| BLOCK_HAS_OBJC_TYPE
);
88 printf("should be non-zero: %d\n", usesStruct(global
));
90 BigStruct_t (^local
)(int) = ^(int param
) {
91 BigStruct_t
*result
= (BigStruct_t
*)malloc(sizeof(BigStruct_t
));
92 result
->array
[23] = argc
;
95 printf("should be non-zero: %d\n", usesStruct(global
));
96 printf("should be non-zero: %d\n", usesStruct(local
));
97 printf("should be zero: %d\n", usesStruct(^void(int x
){ }));
102 desired global flags: 1879048192
103 desired stack flags: 1610612736
104 should be non-zero: 1
105 should be non-zero: 1
106 should be non-zero: 1