blob: 6ed8750e9b1ef8f068de5a7327a9a2f9cf328ceb [file] [log] [blame]
Blaine Garst2a7eb282010-02-23 21:51: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
4// X64: @.str = private constant [6 x i8] c"v8@?0\00"
5// X64: @__block_literal_global = internal constant %1 { i8** @_NSConcreteGlobalBlock, i32 1342177280,
6// X64: @.str1 = private constant [12 x i8] c"i16@?0c8f12\00"
Blaine Garst573e59d2010-02-23 22:59:01 +00007// X64: store i32 1073741824, i32*
Blaine Garst2a7eb282010-02-23 21:51:17 +00008
9// X32: @.str = private constant [6 x i8] c"v4@?0\00"
10// X32: @__block_literal_global = internal constant %1 { i8** @_NSConcreteGlobalBlock, i32 1342177280,
11// X32: @.str1 = private constant [11 x i8] c"i12@?0c4f8\00"
Blaine Garst573e59d2010-02-23 22:59:01 +000012// X32: store i32 1073741824, i32*
Blaine Garst2a7eb282010-02-23 21:51:17 +000013
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