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