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