blob: 4a97aa9bd65e2cea9974e2d8d3611a846813c7c7 [file] [log] [blame]
Chris Lattnere54c0b52009-03-09 06:57:46 +00001Block Implementation Specification
2
Blaine Garsta98c27b2010-03-16 22:02:16 +00003Copyright 2008-2010 Apple, Inc.
Chris Lattnere54c0b52009-03-09 06:57:46 +00004Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21
220. History
23
242008/7/14 - created
252008/8/21 - revised, C++
262008/9/24 - add NULL isa field to __block storage
272008/10/1 - revise block layout to use a static descriptor structure
282008/10/6 - revise block layout to use an unsigned long int flags
292008/10/28 - specify use of _Block_object_assign/dispose for all "Object" types in helper functions
302008/10/30 - revise new layout to have invoke function in same place
312008/10/30 - add __weak support
32
Blaine Garst6757aaa2010-03-16 21:21:07 +0000332010/3/16 - rev for stret return, signature field
Blaine Garst69ecbdb2010-04-06 17:46:43 +0000342010/4/6 - improved wording
Blaine Garst6757aaa2010-03-16 21:21:07 +000035
Chris Lattnere54c0b52009-03-09 06:57:46 +000036This document describes the Apple ABI implementation specification of Blocks.
37
Blaine Garst69ecbdb2010-04-06 17:46:43 +000038The first shipping version of this ABI is found in Mac OS X 10.6, and shall be referred to as 10.6.ABI. As of 2010/3/16, the following describes the ABI contract with the runtime and the compiler, and, as necessary, will be referred to as ABI.2010.3.16.
Blaine Garst6757aaa2010-03-16 21:21:07 +000039
40Since the Apple ABI references symbols from other elements of the system, any attempt to use this ABI on systems prior to SnowLeopard is undefined.
41
Chris Lattnere54c0b52009-03-09 06:57:46 +0000421. High Level
43
Blaine Garst6757aaa2010-03-16 21:21:07 +000044The ABI of blocks consist of their layout and the runtime functions required by the compiler.
Chris Lattnere54c0b52009-03-09 06:57:46 +000045A Block consists of a structure of the following form:
46
47struct Block_literal_1 {
48 void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
49 int flags;
50 int reserved;
51 void (*invoke)(void *, ...);
52 struct Block_descriptor_1 {
53 unsigned long int reserved; // NULL
Blaine Garst6757aaa2010-03-16 21:21:07 +000054 unsigned long int size; // sizeof(struct Block_literal_1)
Chris Lattnere54c0b52009-03-09 06:57:46 +000055 // optional helper functions
Blaine Garst6757aaa2010-03-16 21:21:07 +000056 void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
57 void (*dispose_helper)(void *src); // IFF (1<<25)
Blaine Garst69ecbdb2010-04-06 17:46:43 +000058 // required ABI.2010.3.16
Blaine Garst6757aaa2010-03-16 21:21:07 +000059 const char *signature; // IFF (1<<30)
Chris Lattnere54c0b52009-03-09 06:57:46 +000060 } *descriptor;
61 // imported variables
62};
63
Blaine Garst69ecbdb2010-04-06 17:46:43 +000064The following flags bits are in use thusly for a possible ABI.2010.3.16:
Chris Lattnere54c0b52009-03-09 06:57:46 +000065
66enum {
67 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
68 BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
69 BLOCK_IS_GLOBAL = (1 << 28),
Blaine Garstb03a9282010-10-27 22:59:01 +000070 BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE
Blaine Garst6757aaa2010-03-16 21:21:07 +000071 BLOCK_HAS_SIGNATURE = (1 << 30),
Chris Lattnere54c0b52009-03-09 06:57:46 +000072};
73
Blaine Garstb03a9282010-10-27 22:59:01 +000074In 10.6.ABI the (1<<29) was usually set and was always ignored by the runtime - it had been a transitional marker that did not get deleted after the transition. This bit is now paired with (1<<30), and represented as the pair (3<<30), for the following combinations of valid bit settings, and their meanings.
Blaine Garst6757aaa2010-03-16 21:21:07 +000075
76switch (flags & (3<<29)) {
Blaine Garstb03a9282010-10-27 22:59:01 +000077 case (0<<29): 10.6.ABI, no signature field available
Blaine Garst6757aaa2010-03-16 21:21:07 +000078 case (1<<29): 10.6.ABI, no signature field available
Blaine Garst69ecbdb2010-04-06 17:46:43 +000079 case (2<<29): ABI.2010.3.16, regular calling convention, presence of signature field
80 case (3<<29): ABI.2010.3.16, stret calling convention, presence of signature field,
Blaine Garst6757aaa2010-03-16 21:21:07 +000081}
82
Blaine Garstb03a9282010-10-27 22:59:01 +000083The signature field is not always populated.
84
Blaine Garst6757aaa2010-03-16 21:21:07 +000085The following discussions are presented as 10.6.ABI otherwise.
86
Chris Lattnere54c0b52009-03-09 06:57:46 +000087Block literals may occur within functions where the structure is created in stack local memory. They may also appear as initialization expressions for Block variables of global or static local variables.
88
89When a Block literal expression is evaluated the stack based structure is initialized as follows:
90
911) static descriptor structure is declared and initialized as follows:
921a) the invoke function pointer is set to a function that takes the Block structure as its first argument and the rest of the arguments (if any) to the Block and executes the Block compound statement.
931b) the size field is set to the size of the following Block literal structure.
941c) the copy_helper and dispose_helper function pointers are set to respective helper functions if they are required by the Block literal
952) a stack (or global) Block literal data structure is created and initialized as follows:
962a) the isa field is set to the address of the external _NSConcreteStackBlock, which is a block of uninitialized memory supplied in libSystem, or _NSConcreteGlobalBlock if this is a static or file level block literal.
972) The flags field is set to zero unless there are variables imported into the block that need helper functions for program level Block_copy() and Block_release() operations, in which case the (1<<25) flags bit is set.
98
Blaine Garst6757aaa2010-03-16 21:21:07 +000099
Chris Lattnere54c0b52009-03-09 06:57:46 +0000100As an example, the Block literal expression
101 ^ { printf("hello world\n"); }
102would cause to be created on a 32-bit system:
103
104struct __block_literal_1 {
105 void *isa;
106 int flags;
107 int reserved;
108 void (*invoke)(struct __block_literal_1 *);
109 struct __block_descriptor_1 *descriptor;
110};
111
112void __block_invoke_1(struct __block_literal_1 *_block) {
113 printf("hello world\n");
114}
115
116static struct __block_descriptor_1 {
117 unsigned long int reserved;
118 unsigned long int Block_size;
119} __block_descriptor_1 = { 0, sizeof(struct __block_literal_1), __block_invoke_1 };
120
121and where the block literal appeared
122
123 struct __block_literal_1 _block_literal = {
124 &_NSConcreteStackBlock,
125 (1<<29), <uninitialized>,
126 __block_invoke_1,
127 &__block_descriptor_1
128 };
129
130Blocks import other Block references, const copies of other variables, and variables marked __block. In Objective-C variables may additionally be objects.
131
132When a Block literal expression used as the initial value of a global or static local variable it is initialized as follows:
133 struct __block_literal_1 __block_literal_1 = {
134 &_NSConcreteGlobalBlock,
135 (1<<28)|(1<<29), <uninitialized>,
136 __block_invoke_1,
137 &__block_descriptor_1
138 };
139that is, a different address is provided as the first value and a particular (1<<28) bit is set in the flags field, and otherwise it is the same as for stack based Block literals. This is an optimization that can be used for any Block literal that imports no const or __block storage variables.
140
141
1422. Imported Variables
143
144Variables of "auto" storage class are imported as const copies. Variables of "__block" storage class are imported as a pointer to an enclosing data structure. Global variables are simply referenced and not considered as imported.
145
1462.1 Imported const copy variables
147
148Automatic storage variables not marked with __block are imported as const copies.
149
150The simplest example is that of importing a variable of type int.
151
152 int x = 10;
153 void (^vv)(void) = ^{ printf("x is %d\n", x); }
154 x = 11;
155 vv();
156
157would be compiled
158
159struct __block_literal_2 {
160 void *isa;
161 int flags;
162 int reserved;
163 void (*invoke)(struct __block_literal_2 *);
164 struct __block_descriptor_2 *descriptor;
165 const int x;
166};
167
168void __block_invoke_2(struct __block_literal_2 *_block) {
169 printf("x is %d\n", _block->x);
170}
171
172static struct __block_descriptor_2 {
173 unsigned long int reserved;
174 unsigned long int Block_size;
175} __block_descriptor_2 = { 0, sizeof(struct __block_literal_2) };
176
177and
178
179 struct __block_literal_2 __block_literal_2 = {
180 &_NSConcreteStackBlock,
181 (1<<29), <uninitialized>,
182 __block_invoke_2,
183 &__block_descriptor_2,
184 x
185 };
186
187In summary, scalars, structures, unions, and function pointers are generally imported as const copies with no need for helper functions.
188
1892.2 Imported const copy of Block reference
190
191The first case where copy and dispose helper functions are required is for the case of when a block itself is imported. In this case both a copy_helper function and a dispose_helper function are needed. The copy_helper function is passed both the existing stack based pointer and the pointer to the new heap version and should call back into the runtime to actually do the copy operation on the imported fields within the block. The runtime functions are all described in Section 5.0 Runtime Helper Functions.
192
193An example:
194
195 void (^existingBlock)(void) = ...;
196 void (^vv)(void) = ^{ existingBlock(); }
197 vv();
198
199struct __block_literal_3 {
200 ...; // existing block
201};
202
203struct __block_literal_4 {
204 void *isa;
205 int flags;
206 int reserved;
207 void (*invoke)(struct __block_literal_4 *);
208 struct __block_literal_3 *const existingBlock;
209};
210
211void __block_invoke_4(struct __block_literal_2 *_block) {
212 __block->existingBlock->invoke(__block->existingBlock);
213}
214
215void __block_copy_4(struct __block_literal_4 *dst, struct __block_literal_4 *src) {
216 //_Block_copy_assign(&dst->existingBlock, src->existingBlock, 0);
217 _Block_object_assign(&dst->existingBlock, src->existingBlock, BLOCK_FIELD_IS_BLOCK);
218}
219
220void __block_dispose_4(struct __block_literal_4 *src) {
221 // was _Block_destroy
222 _Block_object_dispose(src->existingBlock, BLOCK_FIELD_IS_BLOCK);
223}
224
225static struct __block_descriptor_4 {
226 unsigned long int reserved;
227 unsigned long int Block_size;
228 void (*copy_helper)(struct __block_literal_4 *dst, struct __block_literal_4 *src);
229 void (*dispose_helper)(struct __block_literal_4 *);
230} __block_descriptor_4 = {
231 0,
232 sizeof(struct __block_literal_4),
233 __block_copy_4,
234 __block_dispose_4,
235};
236
237and where it is used
238
239 struct __block_literal_4 _block_literal = {
240 &_NSConcreteStackBlock,
241 (1<<25)|(1<<29), <uninitialized>
242 __block_invoke_4,
243 & __block_descriptor_4
244 existingBlock,
245 };
246
2472.2.1 Importing __attribute__((NSObject)) variables.
248
249GCC introduces __attribute__((NSObject)) on structure pointers to mean "this is an object". This is useful because many low level data structures are declared as opaque structure pointers, e.g. CFStringRef, CFArrayRef, etc. When used from C, however, these are still really objects and are the second case where that requires copy and dispose helper functions to be generated. The copy helper functions generated by the compiler should use the _Block_object_assign runtime helper function and in the dispose helper the _Block_object_dispose runtime helper function should be called.
250
251For example, block xyzzy in the following
252
253 struct Opaque *__attribute__((NSObject)) objectPointer = ...;
254 ...
255 void (^xyzzy)(void) = ^{ CFPrint(objectPointer); };
256
257would have helper functions
258
259void __block_copy_xyzzy(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
260 _Block_object_assign(&dst->objectPointer, src-> objectPointer, BLOCK_FIELD_IS_OBJECT);
261}
262
263void __block_dispose_xyzzy(struct __block_literal_5 *src) {
264 _Block_object_dispose(src->objectPointer, BLOCK_FIELD_IS_OBJECT);
265}
266
267generated.
268
269
2702.3 Imported __block marked variables.
271
2722.3.1 Layout of __block marked variables
273
274The compiler must embed variables that are marked __block in a specialized structure of the form:
275
276struct _block_byref_xxxx {
277 void *isa;
278 struct Block_byref *forwarding;
279 int flags; //refcount;
280 int size;
281 typeof(marked_variable) marked_variable;
282};
283
284Variables of certain types require helper functions for when Block_copy() and Block_release() are performed upon a referencing Block. At the "C" level only variables that are of type Block or ones that have __attribute__((NSObject)) marked require helper functions. In Objective-C objects require helper functions and in C++ stack based objects require helper functions. Variables that require helper functions use the form:
285
286struct _block_byref_xxxx {
287 void *isa;
288 struct _block_byref_xxxx *forwarding;
289 int flags; //refcount;
290 int size;
291 // helper functions called via Block_copy() and Block_release()
292 void (*byref_keep)(void *dst, void *src);
293 void (*byref_dispose)(void *);
294 typeof(marked_variable) marked_variable;
295};
296
297The structure is initialized such that
298 a) the forwarding pointer is set to the beginning of its enclosing structure,
299 b) the size field is initialized to the total size of the enclosing structure,
300 c) the flags field is set to either 0 if no helper functions are needed or (1<<25) if they are,
301 d) the helper functions are initialized (if present)
302 e) the variable itself is set to its initial value.
303 f) the isa field is set to NULL
304
3052.3.2 Access to __block variables from within its lexical scope.
306
307In order to "move" the variable to the heap upon a copy_helper operation the compiler must rewrite access to such a variable to be indirect through the structures forwarding pointer. For example:
308
309 int __block i = 10;
310 i = 11;
311
312would be rewritten to be:
313
314 struct _block_byref_i {
315 void *isa;
316 struct _block_byref_i *forwarding;
317 int flags; //refcount;
318 int size;
319 int captured_i;
Zhongxing Xu77f41652009-12-08 05:05:26 +0000320 } i = { NULL, &i, 0, sizeof(struct _block_byref_i), 11 };
Chris Lattnere54c0b52009-03-09 06:57:46 +0000321
322 i.forwarding->captured_i = 11;
323
324In the case of a Block reference variable being marked __block the helper code generated must use the _Block_object_assign and _Block_object_dispose routines supplied by the runtime to make the copies. For example:
325
326 __block void (voidBlock)(void) = blockA;
327 voidBlock = blockB;
328
329would translate into
330
331struct _block_byref_voidBlock {
332 void *isa;
333 struct _block_byref_voidBlock *forwarding;
334 int flags; //refcount;
335 int size;
336 void (*byref_keep)(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src);
337 void (*byref_dispose)(struct _block_byref_voidBlock *);
338 void (^captured_voidBlock)(void);
339};
340
341void _block_byref_keep_helper(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
342 //_Block_copy_assign(&dst->captured_voidBlock, src->captured_voidBlock, 0);
343 _Block_object_assign(&dst->captured_voidBlock, src->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
344}
345
346void _block_byref_dispose_helper(struct _block_byref_voidBlock *param) {
347 //_Block_destroy(param->captured_voidBlock, 0);
348 _Block_object_dispose(param->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER)}
349
350and
351 struct _block_byref_voidBlock voidBlock = {( .forwarding=&voidBlock, .flags=(1<<25), .size=sizeof(struct _block_byref_voidBlock *),
352 .byref_keep=_block_byref_keep_helper, .byref_dispose=_block_byref_dispose_helper,
Blaine Garst53881552010-10-27 21:22:46 +0000353 .captured_voidBlock=blockA )};
Chris Lattnere54c0b52009-03-09 06:57:46 +0000354
355 voidBlock.forwarding->captured_voidBlock = blockB;
356
357
3582.3.3 Importing __block variables into Blocks
359
360A Block that uses a __block variable in its compound statement body must import the variable and emit copy_helper and dispose_helper helper functions that, in turn, call back into the runtime to actually copy or release the byref data block using the functions _Block_object_assign and _Block_object_dispose.
361
362For example:
363
364 int __block i = 2;
365 functioncall(^{ i = 10; });
366
367would translate to
368
369struct _block_byref_i {
370 void *isa; // set to NULL
371 struct _block_byref_voidBlock *forwarding;
372 int flags; //refcount;
373 int size;
374 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
375 void (*byref_dispose)(struct _block_byref_i *);
376 int captured_i;
377};
378
379
380struct __block_literal_5 {
381 void *isa;
382 int flags;
383 int reserved;
384 void (*invoke)(struct __block_literal_5 *);
385 struct __block_descriptor_5 *descriptor;
386 struct _block_byref_i *i_holder;
387};
388
389void __block_invoke_5(struct __block_literal_5 *_block) {
390 _block->forwarding->captured_i = 10;
391}
392
393void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
394 //_Block_byref_assign_copy(&dst->captured_i, src->captured_i);
395 _Block_object_assign(&dst->captured_i, src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
396}
397
398void __block_dispose_5(struct __block_literal_5 *src) {
399 //_Block_byref_release(src->captured_i);
400 _Block_object_dispose(src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
401}
402
403static struct __block_descriptor_5 {
404 unsigned long int reserved;
405 unsigned long int Block_size;
406 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
407 void (*dispose_helper)(struct __block_literal_5 *);
408} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5) __block_copy_5, __block_dispose_5 };
409
410and
411
412 struct _block_byref_i i = {( .forwarding=&i, .flags=0, .size=sizeof(struct _block_byref_i) )};
413 struct __block_literal_5 _block_literal = {
414 &_NSConcreteStackBlock,
415 (1<<25)|(1<<29), <uninitialized>,
416 __block_invoke_5,
417 &__block_descriptor_5,
418 2,
419 };
420
4212.3.4 Importing __attribute__((NSObject)) __block variables
422
423A __block variable that is also marked __attribute__((NSObject)) should have byref_keep and byref_dispose helper functions that use _Block_object_assign and _Block_object_dispose.
424
4252.3.5 __block escapes
426
Blaine Garst53881552010-10-27 21:22:46 +0000427Because Blocks referencing __block variables may have Block_copy() performed upon them the underlying storage for the variables may move to the heap. In Objective-C Garbage Collection Only compilation environments the heap used is the garbage collected one and no further action is required. Otherwise the compiler must issue a call to potentially release any heap storage for __block variables at all escapes or terminations of their scope. The call should be:
428
429 _Block_object_dispose(&_block_byref_xxx, BLOCK_FIELD_IS_BYREF);
Chris Lattnere54c0b52009-03-09 06:57:46 +0000430
431
4322.3.6 Nesting
433
434Blocks may contain Block literal expressions. Any variables used within inner blocks are imported into all enclosing Block scopes even if the variables are not used. This includes const imports as well as __block variables.
435
4363. Objective C Extensions to Blocks
437
4383.1 Importing Objects
439
440Objects should be treated as __attribute__((NSObject)) variables; all copy_helper, dispose_helper, byref_keep, and byref_dispose helper functions should use _Block_object_assign and _Block_object_dispose. There should be no code generated that uses -retain or -release methods.
441
442
4433.2 Blocks as Objects
444
445The compiler will treat Blocks as objects when synthesizing property setters and getters, will characterize them as objects when generating garbage collection strong and weak layout information in the same manner as objects, and will issue strong and weak write-barrier assignments in the same manner as objects.
446
4473.3 __weak __block Support
448
449Objective-C (and Objective-C++) support the __weak attribute on __block variables. Under normal circumstances the compiler uses the Objective-C runtime helper support functions objc_assign_weak and objc_read_weak. Both should continue to be used for all reads and writes of __weak __block variables:
450 objc_read_weak(&block->byref_i->forwarding->i)
451
452The __weak variable is stored in a _block_byref_xxxx structure and the Block has copy and dispose helpers for this structure that call:
453 _Block_object_assign(&dest->_block_byref_i, src-> _block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
454and
455 _Block_object_dispose(src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
456
457
458In turn, the block_byref copy support helpers distinguish between whether the __block variable is a Block or not and should either call:
459 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_OBJECT | BLOCK_BYREF_CALLER);
460for something declared as an object or
461 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
462for something declared as a Block.
463
464A full example follows:
465
466
467 __block __weak id obj = <initialization expression>;
468 functioncall(^{ [obj somemessage]; });
469
470would translate to
471
472struct _block_byref_obj {
473 void *isa; // uninitialized
474 struct _block_byref_obj *forwarding;
475 int flags; //refcount;
476 int size;
477 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
478 void (*byref_dispose)(struct _block_byref_i *);
479 int captured_obj;
480};
481
482void _block_byref_obj_keep(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
483 //_Block_copy_assign(&dst->captured_obj, src->captured_obj, 0);
484 _Block_object_assign(&dst->captured_obj, src->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
485}
486
487void _block_byref_obj_dispose(struct _block_byref_voidBlock *param) {
488 //_Block_destroy(param->captured_obj, 0);
489 _Block_object_dispose(param->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
490};
491
492for the block byref part and
493
494struct __block_literal_5 {
495 void *isa;
496 int flags;
497 int reserved;
498 void (*invoke)(struct __block_literal_5 *);
499 struct __block_descriptor_5 *descriptor;
500 struct _block_byref_obj *byref_obj;
501};
502
503void __block_invoke_5(struct __block_literal_5 *_block) {
504 [objc_read_weak(&_block->byref_obj->forwarding->captured_obj) somemessage];
505}
506
507void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
508 //_Block_byref_assign_copy(&dst->byref_obj, src->byref_obj);
509 _Block_object_assign(&dst->byref_obj, src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
510}
511
512void __block_dispose_5(struct __block_literal_5 *src) {
513 //_Block_byref_release(src->byref_obj);
514 _Block_object_dispose(src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
515}
516
517static struct __block_descriptor_5 {
518 unsigned long int reserved;
519 unsigned long int Block_size;
520 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
521 void (*dispose_helper)(struct __block_literal_5 *);
522} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5), __block_copy_5, __block_dispose_5 };
523
524and within the compound statement:
525
526 struct _block_byref_obj obj = {( .forwarding=&obj, .flags=(1<<25), .size=sizeof(struct _block_byref_obj),
527 .byref_keep=_block_byref_obj_keep, .byref_dispose=_block_byref_obj_dispose,
528 .captured_obj = <initialization expression> )};
529
530 struct __block_literal_5 _block_literal = {
531 &_NSConcreteStackBlock,
532 (1<<25)|(1<<29), <uninitialized>,
533 __block_invoke_5,
534 &__block_descriptor_5,
535 &obj, // a reference to the on-stack structure containing "captured_obj"
536 };
537
538
539 functioncall(_block_literal->invoke(&_block_literal));
540
541
5424.0 C++ Support
543
Blaine Garst02be29a2010-10-28 23:30:10 +0000544Within a block stack based C++ objects are copied into const copies using the copy constructor. It is an error if a stack based C++ object is used within a block if it does not have a copy constructor. In addition both copy and destroy helper routines must be synthesized for the block to support the Block_copy() operation, and the flags work marked with the (1<<26) bit in addition to the (1<<25) bit. The copy helper should call the constructor using appropriate offsets of the variable within the supplied stack based block source and heap based destination for all const constructed copies, and similarly should call the destructor in the destroy routine.
Chris Lattnere54c0b52009-03-09 06:57:46 +0000545
Blaine Garst02be29a2010-10-28 23:30:10 +0000546As an example, suppose a C++ class FOO existed with a copy constructor. Within a code block a stack version of a FOO object is declared and used within a Block literal expression:
Chris Lattnere54c0b52009-03-09 06:57:46 +0000547
548{
549 FOO foo;
550 void (^block)(void) = ^{ printf("%d\n", foo.value()); };
551}
552
553The compiler would synthesize
554
555struct __block_literal_10 {
556 void *isa;
557 int flags;
558 int reserved;
559 void (*invoke)(struct __block_literal_10 *);
560 struct __block_descriptor_10 *descriptor;
561 const FOO foo;
562};
563
564void __block_invoke_10(struct __block_literal_10 *_block) {
565 printf("%d\n", _block->foo.value());
566}
567
568void __block_literal_10(struct __block_literal_10 *dst, struct __block_literal_10 *src) {
Blaine Garst02be29a2010-10-28 23:30:10 +0000569 FOO_ctor(&dst->foo, &src->foo);
Chris Lattnere54c0b52009-03-09 06:57:46 +0000570}
571
572void __block_dispose_10(struct __block_literal_10 *src) {
Blaine Garst02be29a2010-10-28 23:30:10 +0000573 FOO_dtor(&src->foo);
Chris Lattnere54c0b52009-03-09 06:57:46 +0000574}
575
576static struct __block_descriptor_10 {
577 unsigned long int reserved;
578 unsigned long int Block_size;
579 void (*copy_helper)(struct __block_literal_10 *dst, struct __block_literal_10 *src);
580 void (*dispose_helper)(struct __block_literal_10 *);
581} __block_descriptor_10 = { 0, sizeof(struct __block_literal_10), __block_copy_10, __block_dispose_10 };
582
583and the code would be:
584{
585 FOO foo;
586 comp_ctor(&foo); // default constructor
587 struct __block_literal_10 _block_literal = {
588 &_NSConcreteStackBlock,
589 (1<<25)|(1<<26)|(1<<29), <uninitialized>,
590 __block_invoke_10,
591 &__block_descriptor_10,
592 };
593 comp_ctor(&_block_literal->foo, &foo); // const copy into stack version
594 struct __block_literal_10 &block = &_block_literal; // assign literal to block variable
595 block->invoke(block); // invoke block
596 comp_dtor(&_block_literal->foo); // destroy stack version of const block copy
597 comp_dtor(&foo); // destroy original version
598}
599
600
Blaine Garst02be29a2010-10-28 23:30:10 +0000601C++ objects stored in __block storage start out on the stack in a block_byref data structure as do other variables. Such objects (if not const objects) must support a regular copy constructor. The block_byref data structure will have copy and destroy helper routines synthesized by the compiler. The copy helper will have code created to perform the copy constructor based on the initial stack block_byref data structure, and will also set the (1<<26) bit in addition to the (1<<25) bit. The destroy helper will have code to do the destructor on the object stored within the supplied block_byref heap data structure. For example,
Chris Lattnere54c0b52009-03-09 06:57:46 +0000602
Blaine Garst02be29a2010-10-28 23:30:10 +0000603 __block FOO blockStorageFoo;
604
605requires the normal constructor for the embedded blockStorageFoo object
606
607 FOO_ctor(& _block_byref_blockStorageFoo->blockStorageFoo);
608
609and at scope termination the destructor:
610
611 FOO_dtor(& _block_byref_blockStorageFoo->blockStorageFoo);
612
613Note that the forwarding indirection is NOT used.
614
615The compiler would need to generate (if used from a block literal) the following copy/dispose helpers:
616
617void _block_byref_obj_keep(struct _block_byref_blockStorageFoo *dst, struct _block_byref_blockStorageFoo *src) {
618 FOO_ctor(&dst->blockStorageFoo, &src->blockStorageFoo);
619}
620
621void _block_byref_obj_dispose(struct _block_byref_blockStorageFoo *src) {
622 FOO_dtor(&src->blockStorageFoo);
623}
624
625for the appropriately named constructor and destructor for the class/struct FOO.
626
627To support member variable and function access the compiler will synthesize a const pointer to a block version of the "this" pointer.
Chris Lattnere54c0b52009-03-09 06:57:46 +0000628
6295.0 Runtime Helper Functions
630
631The runtime helper functions are described in /usr/local/include/Block_private.h. To summarize their use, a block requires copy/dispose helpers if it imports any block variables, __block storage variables, __attribute__((NSObject)) variables, or C++ const copied objects with constructor/destructors. The (1<<26) bit is set and functions are generated.
632
633The block copy helper function should, for each of the variables of the type mentioned above, call
634 _Block_object_assign(&dst->target, src->target, BLOCK_FIELD_<appropo>);
635in the copy helper and
636 _Block_object_dispose(->target, BLOCK_FIELD_<appropo>);
637in the dispose helper where
638 <appropo> is
639
640enum {
641 BLOCK_FIELD_IS_OBJECT = 3, // id, NSObject, __attribute__((NSObject)), block, ...
642 BLOCK_FIELD_IS_BLOCK = 7, // a block variable
643 BLOCK_FIELD_IS_BYREF = 8, // the on stack structure holding the __block variable
644
645 BLOCK_FIELD_IS_WEAK = 16, // declared __weak
646
647 BLOCK_BYREF_CALLER = 128, // called from byref copy/dispose helpers
648};
649
650and of course the CTORs/DTORs for const copied C++ objects.
651
652The block_byref data structure similarly requires copy/dispose helpers for block variables, __attribute__((NSObject)) variables, or C++ const copied objects with constructor/destructors, and again the (1<<26) bit is set and functions are generated in the same manner.
653
654Under ObjC we allow __weak as an attribute on __block variables, and this causes the addition of BLOCK_FIELD_IS_WEAK orred onto the BLOCK_FIELD_IS_BYREF flag when copying the block_byref structure in the block copy helper, and onto the BLOCK_FIELD_<appropo> field within the block_byref copy/dispose helper calls.
655
656The prototypes, and summary, of the helper functions are
657
658/* Certain field types require runtime assistance when being copied to the heap. The following function is used
659 to copy fields of types: blocks, pointers to byref structures, and objects (including __attribute__((NSObject)) pointers.
660 BLOCK_FIELD_IS_WEAK is orthogonal to the other choices which are mutually exclusive.
661 Only in a Block copy helper will one see BLOCK_FIELD_IS_BYREF.
662 */
663void _Block_object_assign(void *destAddr, const void *object, const int flags);
664
665/* Similarly a compiler generated dispose helper needs to call back for each field of the byref data structure.
666 (Currently the implementation only packs one field into the byref structure but in principle there could be more).
667 The same flags used in the copy helper should be used for each call generated to this function:
668 */
669void _Block_object_dispose(const void *object, const int flags);
670