blob: 26f2048bbcc7d71549c2a3cd8b06a08299921ced [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
3
Rafael Espindola1257bc62011-01-10 22:34:03 +00004// X64: @.str = private unnamed_addr constant [6 x i8] c"v8@?0\00"
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +00005// X64: @__block_literal_global = internal constant %1 { i8** @_NSConcreteGlobalBlock, i32 1342177280,
Rafael Espindola1257bc62011-01-10 22:34:03 +00006// X64: @.str1 = private unnamed_addr constant [12 x i8] c"i16@?0c8f12\00"
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +00007// X64: store i32 1073741824, i32*
8
Rafael Espindola1257bc62011-01-10 22:34:03 +00009// X32: @.str = private unnamed_addr constant [6 x i8] c"v4@?0\00"
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +000010// X32: @__block_literal_global = internal constant %1 { i8** @_NSConcreteGlobalBlock, i32 1342177280,
Rafael Espindola1257bc62011-01-10 22:34:03 +000011// X32: @.str1 = private unnamed_addr constant [11 x i8] c"i12@?0c4f8\00"
Fariborz Jahanian5c8e13f2010-11-15 17:06:17 +000012// X32: store i32 1073741824, i32*
13
14// rdar://7635294
15
16
17int globalInt;
18void (^global)(void) = ^{ ++globalInt; };
19
20
21void foo(int param) {
22 extern int rand(void);
23 extern void rand_r(int (^b)(char x, float y)); // name a function present at runtime
24 while (param--)
25 rand_r(^(char x, float y){ return x + (int)y + param + rand(); }); // generate a local block binding param
26}
27
28#if 0
29#include <stdio.h>
30enum {
31 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
32 BLOCK_HAS_CXX_OBJ = (1 << 26),
33 BLOCK_IS_GLOBAL = (1 << 28),
34 BLOCK_HAS_DESCRIPTOR = (1 << 29),
35 BLOCK_HAS_OBJC_TYPE = (1 << 30)
36};
37
38struct block_descriptor_big {
39 unsigned long int reserved;
40 unsigned long int size;
41 void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
42 void (*dispose)(void *); // conditional on BLOCK_HAS_COPY_DISPOSE
43 const char *signature; // conditional on BLOCK_HAS_OBJC
44 const char *layout; // conditional on BLOCK_HAS_OBJC
45};
46struct block_descriptor_small {
47 unsigned long int reserved;
48 unsigned long int size;
49 const char *signature; // conditional on BLOCK_HAS_OBJC
50 const char *layout; // conditional on BLOCK_HAS_OBJC
51};
52
53struct block_layout_abi { // can't change
54 void *isa;
55 int flags;
56 int reserved;
57 void (*invoke)(void *, ...);
58 struct block_descriptor_big *descriptor;
59};
60
61const char *getBlockSignature(void *block) {
62 struct block_layout_abi *layout = (struct block_layout_abi *)block;
63 if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL;
64 if (layout->flags & BLOCK_HAS_COPY_DISPOSE)
65 return layout->descriptor->signature;
66 else
67 return ((struct block_descriptor_small *)layout->descriptor)->signature;
68}
69
70
71
72int main(int argc, char *argv[]) {
73 printf("desired global flags: %d\n", BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
74 printf("desired stack flags: %d\n", BLOCK_HAS_OBJC_TYPE);
75
76 printf("types for global: %s\n", getBlockSignature(global));
77 printf("types for local: %s\n", getBlockSignature(^int(char x, float y) { return (int)(y + x); }));
78 return 0;
79}
80
81/*
82x86_64
83desired global flags: 1342177280
84desired stack flags: 1073741824
85types for global: v8@?0
86types for local: i16@?0c8f12
87
88i386
89desired global flags: 1342177280
90desired stack flags: 1073741824
91types for global: v4@?0
92types for local: i12@?0c4f8
93*/
94#endif