1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB and Kjell Winblad 2019. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 /*
22  * Description:
23  *
24  * Author: Kjell Winblad
25  *
26  */
27 
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #define YCF_YIELD()
33 
fun2(int x)34 int fun2(int x){
35     YCF_YIELD();
36     return x;
37 }
38 
fun(char x)39 int fun(char x){
40   int y = 10;
41   /*special_code_start:ON_SAVE_YIELD_STATE*/
42   if(0){
43     printf("y=%d\n", y);
44     y = 42;
45   }
46   /*special_code_end*/
47   /*special_code_start:ON_RETURN*/
48   if(0){
49     printf("I returned y=%d\n", y);
50   }
51   /*special_code_end*/
52   /*special_code_start:ON_DESTROY_STATE_OR_RETURN*/
53   if(0){
54     int hej = 123;
55     printf("I got destroyed or returned y=%d hej=%d\n", y, hej);
56   }
57   /*special_code_end*/
58   /*special_code_start:ON_DESTROY_STATE*/
59   if(0){
60     int hej = 321;
61     printf("I got destroyed y=%d call_in_special_code=%d hej=%d\n", y, fun2(42), hej);
62   }
63   /*special_code_end*/
64   /*special_code_start:ON_RESTORE_YIELD_STATE*/
65   if(0){
66     x = 9;
67   }
68   /*special_code_end*/
69   if(0){
70     /*special_code_start:ON_SAVE_YIELD_STATE*/
71     if(0){
72       int z = 10;
73       printf("y=%d z=%d\n", y, z);
74     }
75     /*special_code_end*/
76   }
77   if(y != 10 || x != 1){
78     /*special_code_start:ON_RESTORE_YIELD_STATE*/
79     if(0){
80       printf("y=%d x=%d\n", y, x);
81       x = x*2;
82       printf("y=%d x=%d\n", y, x);
83     }
84     /*special_code_end*/
85     /*special_code_start:ON_RESTORE_YIELD_STATE*/
86     if(0){
87       printf("y=%d x=%d\n", y, x);
88       x = x/2;
89       printf("y=%d x=%d\n", y, x);
90     }
91     /*special_code_end*/
92     printf("ERROR BEFORE YIELD\n");
93     exit(1);
94   }
95   YCF_YIELD();
96   if(y != 42 || x != 9){
97     printf("ERROR AFTER YIELD\n");
98     exit(1);
99   }
100   printf("SUCCESS\n");
101   return x;
102 }
103 
allocator(size_t size,void * context)104 void* allocator(size_t size, void* context){
105   (void)context;
106   return malloc(size);
107 }
108 
freer(void * data,void * context)109 void freer(void* data, void* context){
110   (void)context;
111   free(data);
112 }
113 
main(int argc,const char * argv[])114 int main( int argc, const char* argv[] )
115 {
116 #ifdef YCF_YIELD_CODE_GENERATED
117   void* wb = NULL;
118 #endif
119   int ret = 0;
120   long nr_of_reductions = 1;
121 #ifdef YCF_YIELD_CODE_GENERATED
122   ret = fun_ycf_gen_yielding(&nr_of_reductions,&wb,NULL,allocator,freer,NULL,0,NULL,1);
123   printf("CALLING DESTROY\n");
124   fun_ycf_gen_destroy(wb);
125   wb = NULL;
126   printf("DESTROY ENDED\n");
127   do{
128     ret = fun_ycf_gen_yielding(&nr_of_reductions,&wb,NULL,allocator,freer,NULL,0,NULL,1);
129     if(wb != NULL){
130       printf("TRAPPED\n");
131     }
132   }while(wb != NULL);
133   if(wb != NULL){
134     free(wb);
135   }
136 #else
137   ret = fun(1);
138 #endif
139   printf("RETURNED %d\n", ret);
140   if(ret != 9){
141     return 1;
142   }else{
143     return 0;
144   }
145 }
146 
147