xref: /aosp_15_r20/external/compiler-rt/test/BlocksRuntime/recursive-block.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 #include <stdio.h>
8*7c3d14c8STreehugger Robot #include <Block.h>
9*7c3d14c8STreehugger Robot #include <Block_private.h>
10*7c3d14c8STreehugger Robot #include <stdlib.h>
11*7c3d14c8STreehugger Robot 
12*7c3d14c8STreehugger Robot // CONFIG
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot int cumulation = 0;
16*7c3d14c8STreehugger Robot 
doSomething(int i)17*7c3d14c8STreehugger Robot int doSomething(int i) {
18*7c3d14c8STreehugger Robot     cumulation += i;
19*7c3d14c8STreehugger Robot     return cumulation;
20*7c3d14c8STreehugger Robot }
21*7c3d14c8STreehugger Robot 
dirtyStack()22*7c3d14c8STreehugger Robot void dirtyStack() {
23*7c3d14c8STreehugger Robot     int i = random();
24*7c3d14c8STreehugger Robot     int j = doSomething(i);
25*7c3d14c8STreehugger Robot     int k = doSomething(j);
26*7c3d14c8STreehugger Robot     doSomething(i + j + k);
27*7c3d14c8STreehugger Robot }
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot typedef void (^voidVoid)(void);
30*7c3d14c8STreehugger Robot 
testFunction()31*7c3d14c8STreehugger Robot voidVoid testFunction() {
32*7c3d14c8STreehugger Robot     int i = random();
33*7c3d14c8STreehugger Robot     __block voidVoid inner = ^{ doSomething(i); };
34*7c3d14c8STreehugger Robot     //printf("inner, on stack, is %p\n", (void*)inner);
35*7c3d14c8STreehugger Robot     /*__block*/ voidVoid outer = ^{
36*7c3d14c8STreehugger Robot         //printf("will call inner block %p\n", (void *)inner);
37*7c3d14c8STreehugger Robot         inner();
38*7c3d14c8STreehugger Robot     };
39*7c3d14c8STreehugger Robot     //printf("outer looks like: %s\n", _Block_dump(outer));
40*7c3d14c8STreehugger Robot     voidVoid result = Block_copy(outer);
41*7c3d14c8STreehugger Robot     //Block_release(inner);
42*7c3d14c8STreehugger Robot     return result;
43*7c3d14c8STreehugger Robot }
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)46*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
47*7c3d14c8STreehugger Robot     voidVoid block = testFunction();
48*7c3d14c8STreehugger Robot     dirtyStack();
49*7c3d14c8STreehugger Robot     block();
50*7c3d14c8STreehugger Robot     Block_release(block);
51*7c3d14c8STreehugger Robot 
52*7c3d14c8STreehugger Robot     printf("%s: success\n", argv[0]);
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot     return 0;
55*7c3d14c8STreehugger Robot }
56