annotate test/conv1.c @ 720:6b7372e17970

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