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_;
|
|
65 sp += sizeof(struct f_g0_interface);
|
|
66 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
|
|
67 }
|
|
68
|
|
69 code g(int i,stack sp) { // Caller
|
|
70 struct f_g0_interface *c =
|
|
71 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
|
|
72
|
|
73 c->ret = g_h1;
|
|
74 c->i_ = i;
|
|
75
|
|
76 goto h(i+3,sp);
|
|
77 }
|
|
78
|
|
79 code g_h1(int j,stack sp) { // Continuation
|
|
80 struct f_g0_interface *c = sp;
|
|
81 int i = c->i_;
|
|
82 sp += sizeof(struct f_g0_interface);
|
|
83 goto (( (struct cont_interface *)sp)->ret)(j+i,sp);
|
|
84 }
|
|
85
|
|
86 code h(int i,stack sp) {
|
|
87 goto (( (struct cont_interface *)sp)->ret)(i+4,sp);
|
|
88 }
|
|
89
|
|
90 struct main_continuation { // General Return Continuation
|
|
91 code (*ret)();
|
|
92 code (*main_ret)();
|
|
93 void *env;
|
|
94 };
|
|
95
|
|
96 code main_return(int i,stack sp) {
|
|
97 if (loop-->0)
|
|
98 goto f(233,sp);
|
|
99 printf("%d\n",i);
|
|
100 goto (( (struct main_continuation *)sp)->main_ret)(0),
|
|
101 ((struct main_continuation *)sp)->env;
|
|
102 }
|
|
103
|
|
104 /* little optimzation without stack continuation (2) */
|
|
105
|
|
106 code f2(int i,char *sp) {
|
|
107 int k,j;
|
|
108 k = 3+i;
|
|
109 goto g2(i,k,i+3,sp);
|
|
110 }
|
|
111
|
|
112 code g2(int i,int k,int j,char *sp) {
|
|
113 j = j+4;
|
|
114 goto h2(i,k+4+j,sp);
|
|
115 }
|
|
116
|
|
117 code h2_1(int i,int k,int j,char *sp) {
|
|
118 goto main_return2(i+j,sp);
|
|
119 }
|
|
120
|
|
121 code h2(int i,int k,char *sp) {
|
|
122 goto h2_1(i,k,i+4,sp);
|
|
123 }
|
|
124
|
|
125 code main_return2(int i,stack sp) {
|
|
126 if (loop-->0)
|
|
127 goto f2(233,sp);
|
|
128 printf("%d\n",i);
|
|
129 goto (( (struct main_continuation *)sp)->main_ret)(0),
|
|
130 ((struct main_continuation *)sp)->env;
|
|
131 }
|
|
132
|
|
133 /* little optimizaed case (3) */
|
|
134
|
|
135 code f2_1(int i,char *sp) {
|
|
136 int k,j;
|
|
137 k = 3+i;
|
|
138 goto g2_1(k,i+3,sp);
|
|
139 }
|
|
140
|
|
141 code g2_1(int k,int i,char *sp) {
|
|
142 goto h2_11(k,i+4,sp);
|
|
143 }
|
|
144
|
|
145 code f2_0_1(int k,int j,char *sp);
|
|
146 code h2_1_1(int i,int k,int j,char *sp) {
|
|
147 goto f2_0_1(k,i+j,sp);
|
|
148 }
|
|
149
|
|
150 code h2_11(int i,int k,char *sp) {
|
|
151 goto h2_1_1(i,k,i+4,sp);
|
|
152 }
|
|
153
|
|
154 code f2_0_1(int k,int j,char *sp) {
|
|
155 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
|
|
156 }
|
|
157
|
|
158 code main_return2_1(int i,stack sp) {
|
|
159 if (loop-->0)
|
|
160 goto f2_1(233,sp);
|
|
161 printf("%d\n",i);
|
|
162 goto (( (struct main_continuation *)sp)->main_ret)(0),
|
|
163 ((struct main_continuation *)sp)->env;
|
|
164 }
|
|
165
|
|
166 #define STACK_SIZE 2048
|
|
167 stack main_stack[STACK_SIZE];
|
|
168 #define stack_last (&main_stack[STACK_SIZE])
|
|
169
|
|
170 #endif
|
|
171
|
|
172 #define LOOP_COUNT 10000000
|
|
173
|
|
174 main(int ac,char *av[])
|
|
175 {
|
|
176 #if !CC_ONLY
|
|
177 struct main_continuation *cont;
|
|
178 stack sp = stack_last;
|
|
179 #endif
|
|
180 int sw;
|
|
181 int j;
|
|
182 if (ac!=2) exit(0);
|
|
183 sw = atoi(av[1]);
|
|
184
|
|
185 if (sw==0) {
|
|
186 for(loop=0;loop<LOOP_COUNT;loop++) {
|
|
187 j = f0(233);
|
|
188 }
|
|
189 printf("%d\n",j);
|
|
190 #if !CC_ONLY
|
|
191 } else if (sw==1) {
|
|
192 loop = LOOP_COUNT;
|
|
193 sp -= sizeof(*cont);
|
|
194 cont = (struct main_continuation *)sp;
|
|
195 cont->ret = main_return;
|
|
196 cont->main_ret = return;
|
|
197 cont->env = environment;
|
|
198 goto f(233,sp);
|
|
199 } else if (sw==2) {
|
|
200 loop = LOOP_COUNT;
|
|
201 sp -= sizeof(*cont);
|
|
202 cont = (struct main_continuation *)sp;
|
|
203 cont->ret = main_return2;
|
|
204 cont->main_ret = return;
|
|
205 cont->env = environment;
|
|
206 goto f2(233,sp);
|
|
207 } else if (sw==3) {
|
|
208 loop = LOOP_COUNT;
|
|
209 sp -= sizeof(*cont);
|
|
210 cont = (struct main_continuation *)sp;
|
|
211 cont->ret = main_return2_1;
|
|
212 cont->main_ret = return;
|
|
213 cont->env = environment;
|
|
214 goto f2_1(233,sp);
|
|
215 #endif
|
|
216 }
|
|
217 }
|
|
218
|
|
219 /* end */
|