21
|
1 #include "stdio.h"
|
|
2
|
119
|
3 extern int atoi (const char *);
|
|
4
|
21
|
5 static int loop;
|
|
6
|
|
7 #if 1 // def __micro_c__
|
|
8 #define CC_ONLY 0
|
|
9 #else
|
|
10 #define CC_ONLY 1
|
|
11 #endif
|
|
12
|
|
13 /* classical function call case (0) */
|
119
|
14 int g0(int);
|
|
15 int h0(int);
|
21
|
16
|
119
|
17 int
|
21
|
18 f0(int i) {
|
|
19 int k,j;
|
|
20 k = 3+i;
|
|
21 j = g0(i+3);
|
|
22 return k+4+j;
|
|
23 }
|
|
24
|
119
|
25 int
|
21
|
26 g0(int i) {
|
|
27 return h0(i+4)+i;
|
|
28 }
|
|
29
|
119
|
30 int
|
21
|
31 h0(int i) {
|
|
32 return i+4;
|
|
33 }
|
|
34
|
|
35 #if !CC_ONLY
|
|
36
|
|
37 /* straight conversion case (1) */
|
|
38
|
119
|
39 typedef void *stack;
|
21
|
40
|
|
41 struct cont_interface { // General Return Continuation
|
|
42 __code (*ret)(int, void *);
|
|
43 };
|
|
44
|
119
|
45 #ifndef __llvm__
|
|
46 __code f_g0(int i,int k,stack sp);
|
|
47 __code f_g1(int j,stack sp);
|
|
48 __code g(int i,stack sp);
|
|
49 __code h(int i,stack sp);
|
|
50 __code f2(int i,char *sp);
|
|
51 __code g2(int i,int k,int j,char *sp);
|
|
52 __code h2(int i,int k,char *sp);
|
|
53 __code main_return2(int i,stack sp);
|
|
54 __code g2_1(int k,int i,stack *sp);
|
|
55 __code h2_11(int i,int k,stack *sp);
|
|
56 #endif
|
|
57
|
21
|
58 __code f(int i,stack sp) {
|
|
59 int k,j;
|
|
60 k = 3+i;
|
|
61 goto f_g0(i,k,sp);
|
|
62 }
|
|
63
|
|
64 struct f_g0_interface { // Specialized Return Continuation
|
|
65 __code (*ret)(int, void *);
|
|
66 int i_,k_,j_;
|
|
67 };
|
|
68
|
|
69 __code f_g1(int j,stack sp);
|
|
70
|
|
71 __code f_g0(int i,int k,stack sp) { // Caller
|
|
72 struct f_g0_interface *c =
|
|
73 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
|
|
74
|
|
75 c->ret = f_g1;
|
|
76 c->k_ = k;
|
|
77 c->i_ = i;
|
|
78
|
|
79 goto g(i+3,sp);
|
|
80 }
|
|
81
|
|
82 __code f_g1(int j,stack sp) { // Continuation
|
|
83 struct f_g0_interface *c = (struct f_g0_interface *)sp;
|
|
84 int k = c->k_;
|
|
85 sp+=sizeof(struct f_g0_interface);
|
|
86 c = (struct f_g0_interface *)sp;
|
|
87 goto (c->ret)(k+4+j,sp);
|
|
88 }
|
|
89
|
|
90 __code g_h1(int j,stack sp);
|
|
91
|
|
92 __code g(int i,stack sp) { // Caller
|
|
93 struct f_g0_interface *c =
|
|
94 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
|
|
95
|
|
96 c->ret = g_h1;
|
|
97 c->i_ = i;
|
|
98
|
|
99 goto h(i+3,sp);
|
|
100 }
|
|
101
|
|
102 __code g_h1(int j,stack sp) { // Continuation
|
|
103 struct f_g0_interface *c = (struct f_g0_interface *)sp;
|
|
104 int i = c->i_;
|
|
105 sp+=sizeof(struct f_g0_interface);
|
|
106 c = (struct f_g0_interface *)sp;
|
|
107 goto (c->ret)(j+i,sp);
|
|
108 }
|
|
109
|
|
110 __code h(int i,stack sp) {
|
|
111 struct f_g0_interface *c = (struct f_g0_interface *)sp;
|
|
112 goto (c->ret)(i+4,sp);
|
|
113 }
|
|
114
|
|
115 struct main_continuation { // General Return Continuation
|
120
|
116 __code (*ret)(int, void*);
|
|
117 __code (*main_ret)(int, void*);
|
21
|
118 void *env;
|
|
119 };
|
|
120
|
|
121 __code main_return(int i,stack sp) {
|
|
122 if (loop-->0)
|
|
123 goto f(233,sp);
|
|
124 printf("#0103:%d\n",i);
|
|
125 goto (( (struct main_continuation *)sp)->main_ret)(0,
|
|
126 ((struct main_continuation *)sp)->env);
|
|
127 }
|
|
128
|
|
129 /* little optimzation without stack continuation (2) */
|
|
130
|
|
131 __code f2(int i,char *sp) {
|
|
132 int k,j;
|
|
133 k = 3+i;
|
|
134 goto g2(i,k,i+3,sp);
|
|
135 }
|
|
136
|
|
137 __code g2(int i,int k,int j,char *sp) {
|
|
138 j = j+4;
|
|
139 goto h2(i,k+4+j,sp);
|
|
140 }
|
|
141
|
|
142 __code h2_1(int i,int k,int j,char *sp) {
|
|
143 goto main_return2(i+j,sp);
|
|
144 }
|
|
145
|
|
146 __code h2(int i,int k,char *sp) {
|
|
147 goto h2_1(i,k,i+4,sp);
|
|
148 }
|
|
149
|
|
150 __code main_return2(int i,stack sp) {
|
|
151 if (loop-->0)
|
|
152 goto f2(233,sp);
|
|
153 printf("#0132:%d\n",i);
|
|
154 goto (( (struct main_continuation *)sp)->main_ret)(0,
|
|
155 ((struct main_continuation *)sp)->env);
|
|
156 }
|
|
157
|
|
158 /* little optimizaed case (3) */
|
|
159
|
119
|
160 __code f2_1(int i,stack *sp) {
|
21
|
161 int k,j;
|
|
162 k = 3+i;
|
|
163 goto g2_1(k,i+3,sp);
|
|
164 }
|
|
165
|
119
|
166 __code g2_1(int k,int i,stack *sp) {
|
21
|
167 goto h2_11(k,i+4,sp);
|
|
168 }
|
|
169
|
119
|
170 __code f2_0_1(int k,int j,stack *sp);
|
|
171 __code h2_1_1(int i,int k,int j,stack *sp) {
|
21
|
172 goto f2_0_1(k,i+j,sp);
|
|
173 }
|
|
174
|
119
|
175 __code h2_11(int i,int k,stack *sp) {
|
21
|
176 goto h2_1_1(i,k,i+4,sp);
|
|
177 }
|
|
178
|
119
|
179 __code f2_0_1(int k,int j,stack *sp) {
|
21
|
180 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
|
|
181 }
|
|
182
|
|
183 __code main_return2_1(int i,stack sp) {
|
|
184 if (loop-->0)
|
|
185 goto f2_1(233,sp);
|
|
186 printf("#0165:%d\n",i);
|
|
187 goto (( (struct main_continuation *)sp)->main_ret)(0,
|
|
188 ((struct main_continuation *)sp)->env);
|
|
189 }
|
|
190
|
|
191 #define STACK_SIZE 2048
|
|
192 char main_stack[STACK_SIZE];
|
|
193 #define stack_last (main_stack+STACK_SIZE)
|
|
194
|
|
195 #endif
|
|
196
|
|
197 #define LOOP_COUNT 10000000
|
|
198
|
119
|
199 int
|
21
|
200 main(int ac,char *av[])
|
|
201 {
|
|
202 #if !CC_ONLY
|
|
203 struct main_continuation *cont;
|
|
204 stack sp = stack_last;
|
|
205 #endif
|
|
206 int sw;
|
|
207 int j;
|
|
208 if (ac==2) sw = atoi(av[1]);
|
|
209 else sw=3;
|
|
210
|
|
211 if (sw==0) {
|
|
212 for(loop=0;loop<LOOP_COUNT;loop++) {
|
|
213 j = f0(233);
|
|
214 }
|
|
215 printf("#0193:%d\n",j);
|
|
216 #if !CC_ONLY
|
|
217 } else if (sw==1) {
|
|
218 loop = LOOP_COUNT;
|
|
219 sp -= sizeof(*cont);
|
|
220 cont = (struct main_continuation *)sp;
|
|
221 cont->ret = main_return;
|
|
222 cont->main_ret = _CbC_return;
|
|
223 cont->env = _CbC_environment;
|
|
224 goto f(233,sp);
|
|
225 } else if (sw==2) {
|
|
226 loop = LOOP_COUNT;
|
|
227 sp -= sizeof(*cont);
|
|
228 cont = (struct main_continuation *)sp;
|
|
229 cont->ret = main_return2;
|
|
230 cont->main_ret = _CbC_return;
|
|
231 cont->env = _CbC_environment;
|
|
232 goto f2(233,sp);
|
|
233 } else if (sw==3) {
|
|
234 loop = LOOP_COUNT;
|
|
235 sp -= sizeof(*cont);
|
|
236 cont = (struct main_continuation *)sp;
|
|
237 cont->ret = main_return2_1;
|
|
238 cont->main_ret = _CbC_return;
|
|
239 cont->env = _CbC_environment;
|
|
240 goto f2_1(233,sp);
|
|
241 #endif
|
|
242 }
|
|
243 return 0;
|
|
244 }
|
|
245
|
|
246 /* end */
|