comparison CbC-examples/conv1/conv1.c @ 31:79124facde7c

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