xref: /aosp_15_r20/external/compiler-rt/test/BlocksRuntime/objectassign.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //
2*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
3*7c3d14c8STreehugger Robot //
4*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
5*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot /*
8*7c3d14c8STreehugger Robot  *  objectassign.c
9*7c3d14c8STreehugger Robot  *  testObjects
10*7c3d14c8STreehugger Robot  *
11*7c3d14c8STreehugger Robot  *  Created by Blaine Garst on 10/28/08.
12*7c3d14c8STreehugger Robot  *
13*7c3d14c8STreehugger Robot  * This just tests that the compiler is issuing the proper helper routines
14*7c3d14c8STreehugger Robot  * CONFIG C rdar://6175959
15*7c3d14c8STreehugger Robot  */
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #include <stdio.h>
19*7c3d14c8STreehugger Robot #include <Block_private.h>
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot int AssignCalled = 0;
23*7c3d14c8STreehugger Robot int DisposeCalled = 0;
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot // local copy instead of libSystem.B.dylib copy
_Block_object_assign(void * destAddr,const void * object,const int isWeak)26*7c3d14c8STreehugger Robot void _Block_object_assign(void *destAddr, const void *object, const int isWeak) {
27*7c3d14c8STreehugger Robot     //printf("_Block_object_assign(%p, %p, %d) called\n", destAddr, object, isWeak);
28*7c3d14c8STreehugger Robot     AssignCalled = 1;
29*7c3d14c8STreehugger Robot }
30*7c3d14c8STreehugger Robot 
_Block_object_dispose(const void * object,const int isWeak)31*7c3d14c8STreehugger Robot void _Block_object_dispose(const void *object, const int isWeak) {
32*7c3d14c8STreehugger Robot     //printf("_Block_object_dispose(%p, %d) called\n", object, isWeak);
33*7c3d14c8STreehugger Robot     DisposeCalled = 1;
34*7c3d14c8STreehugger Robot }
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot struct MyStruct {
37*7c3d14c8STreehugger Robot     long isa;
38*7c3d14c8STreehugger Robot     long field;
39*7c3d14c8STreehugger Robot };
40*7c3d14c8STreehugger Robot 
41*7c3d14c8STreehugger Robot typedef struct MyStruct *__attribute__((NSObject)) MyStruct_t;
42*7c3d14c8STreehugger Robot 
main(int argc,char * argv[])43*7c3d14c8STreehugger Robot int main(int argc, char *argv[]) {
44*7c3d14c8STreehugger Robot     if (__APPLE_CC__ < 5627) {
45*7c3d14c8STreehugger Robot         printf("need compiler version %d, have %d\n", 5627, __APPLE_CC__);
46*7c3d14c8STreehugger Robot         return 0;
47*7c3d14c8STreehugger Robot     }
48*7c3d14c8STreehugger Robot     // create a block
49*7c3d14c8STreehugger Robot     struct MyStruct X;
50*7c3d14c8STreehugger Robot     MyStruct_t xp = (MyStruct_t)&X;
51*7c3d14c8STreehugger Robot     xp->field = 10;
52*7c3d14c8STreehugger Robot     void (^myBlock)(void) = ^{ printf("field is %ld\n", xp->field); };
53*7c3d14c8STreehugger Robot     // should be a copy helper generated with a calls to above routines
54*7c3d14c8STreehugger Robot     // Lets find out!
55*7c3d14c8STreehugger Robot     struct Block_layout *bl = (struct Block_layout *)(void *)myBlock;
56*7c3d14c8STreehugger Robot     if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
57*7c3d14c8STreehugger Robot         printf("no copy dispose!!!!\n");
58*7c3d14c8STreehugger Robot         return 1;
59*7c3d14c8STreehugger Robot     }
60*7c3d14c8STreehugger Robot     // call helper routines directly.  These will, in turn, we hope, call the stubs above
61*7c3d14c8STreehugger Robot     long destBuffer[256];
62*7c3d14c8STreehugger Robot     //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
63*7c3d14c8STreehugger Robot     //printf("dump is %s\n", _Block_dump(myBlock));
64*7c3d14c8STreehugger Robot     bl->descriptor->copy(destBuffer, bl);
65*7c3d14c8STreehugger Robot     bl->descriptor->dispose(bl);
66*7c3d14c8STreehugger Robot     if (AssignCalled == 0) {
67*7c3d14c8STreehugger Robot         printf("did not call assign helper!\n");
68*7c3d14c8STreehugger Robot         return 1;
69*7c3d14c8STreehugger Robot     }
70*7c3d14c8STreehugger Robot     if (DisposeCalled == 0) {
71*7c3d14c8STreehugger Robot         printf("did not call dispose helper\n");
72*7c3d14c8STreehugger Robot         return 1;
73*7c3d14c8STreehugger Robot     }
74*7c3d14c8STreehugger Robot     printf("%s: Success!\n", argv[0]);
75*7c3d14c8STreehugger Robot     return 0;
76*7c3d14c8STreehugger Robot }
77