comparison CbC-examples/conv1.c @ 21:959d4c8c8abc

add conv.c conv1.c
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Tue, 29 Sep 2009 20:15:16 +0900
parents
children 9d1ff3ae1824
comparison
equal deleted inserted replaced
20:9de9dad105d4 21:959d4c8c8abc
1 #include "stdio.h"
2
3 static int loop;
4
5 #if 1 // def __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)(int, void *);
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)(int, void *);
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 = (struct f_g0_interface *)sp;
64 int k = c->k_;
65 sp+=sizeof(struct f_g0_interface);
66 c = (struct f_g0_interface *)sp;
67 goto (c->ret)(k+4+j,sp);
68 }
69
70 __code g_h1(int j,stack sp);
71
72 __code g(int i,stack sp) { // Caller
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
82 __code g_h1(int j,stack sp) { // Continuation
83 struct f_g0_interface *c = (struct f_g0_interface *)sp;
84 int i = c->i_;
85 sp+=sizeof(struct f_g0_interface);
86 c = (struct f_g0_interface *)sp;
87 goto (c->ret)(j+i,sp);
88 }
89
90 __code h(int i,stack sp) {
91 struct f_g0_interface *c = (struct f_g0_interface *)sp;
92 goto (c->ret)(i+4,sp);
93 }
94
95 struct main_continuation { // General Return Continuation
96 __code (*ret)();
97 __code (*main_ret)();
98 void *env;
99 };
100
101 __code main_return(int i,stack sp) {
102 if (loop-->0)
103 goto f(233,sp);
104 printf("#0103:%d\n",i);
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
111 __code f2(int i,char *sp) {
112 int k,j;
113 k = 3+i;
114 goto g2(i,k,i+3,sp);
115 }
116
117 __code g2(int i,int k,int j,char *sp) {
118 j = j+4;
119 goto h2(i,k+4+j,sp);
120 }
121
122 __code h2_1(int i,int k,int j,char *sp) {
123 goto main_return2(i+j,sp);
124 }
125
126 __code h2(int i,int k,char *sp) {
127 goto h2_1(i,k,i+4,sp);
128 }
129
130 __code main_return2(int i,stack sp) {
131 if (loop-->0)
132 goto f2(233,sp);
133 printf("#0132:%d\n",i);
134 goto (( (struct main_continuation *)sp)->main_ret)(0,
135 ((struct main_continuation *)sp)->env);
136 }
137
138 /* little optimizaed case (3) */
139
140 __code f2_1(int i,char *sp) {
141 int k,j;
142 k = 3+i;
143 goto g2_1(k,i+3,sp);
144 }
145
146 __code g2_1(int k,int i,char *sp) {
147 goto h2_11(k,i+4,sp);
148 }
149
150 __code f2_0_1(int k,int j,char *sp);
151 __code h2_1_1(int i,int k,int j,char *sp) {
152 goto f2_0_1(k,i+j,sp);
153 }
154
155 __code h2_11(int i,int k,char *sp) {
156 goto h2_1_1(i,k,i+4,sp);
157 }
158
159 __code f2_0_1(int k,int j,char *sp) {
160 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
161 }
162
163 __code main_return2_1(int i,stack sp) {
164 if (loop-->0)
165 goto f2_1(233,sp);
166 printf("#0165:%d\n",i);
167 goto (( (struct main_continuation *)sp)->main_ret)(0,
168 ((struct main_continuation *)sp)->env);
169 }
170
171 #define STACK_SIZE 2048
172 char main_stack[STACK_SIZE];
173 #define stack_last (main_stack+STACK_SIZE)
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;
187 if (ac==2) sw = atoi(av[1]);
188 else sw=3;
189
190 if (sw==0) {
191 for(loop=0;loop<LOOP_COUNT;loop++) {
192 j = f0(233);
193 }
194 printf("#0193:%d\n",j);
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;
201 cont->main_ret = _CbC_return;
202 cont->env = _CbC_environment;
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;
209 cont->main_ret = _CbC_return;
210 cont->env = _CbC_environment;
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;
217 cont->main_ret = _CbC_return;
218 cont->env = _CbC_environment;
219 goto f2_1(233,sp);
220 #endif
221 }
222 return 0;
223 }
224
225 /* end */