Mercurial > hg > CbC > CbC_gcc
annotate CbC-examples/conv1/conv1.c @ 106:85047c4f1ca4
add CbC-examples/rectypeTest
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 13 Jun 2012 17:18:58 +0900 |
parents | 79124facde7c |
children |
rev | line source |
---|---|
31 | 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 f0(int i) { | |
15 int k,j; | |
16 k = 3+i; | |
17 j = g0(i+3); | |
18 return k+4+j; | |
19 } | |
20 | |
21 g0(int i) { | |
22 return h0(i+4)+i; | |
23 } | |
24 | |
25 h0(int i) { | |
26 return i+4; | |
27 } | |
28 | |
29 #if !CC_ONLY | |
30 | |
31 /* straight conversion case (1) */ | |
32 | |
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 | |
106
85047c4f1ca4
add CbC-examples/rectypeTest
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
45 __code (*ret)(int,stack); |
31 | 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)(int,void*); | |
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 exit(0); | |
168 //goto (( (struct main_continuation *)sp)->main_ret)(0, | |
169 //((struct main_continuation *)sp)->env); | |
170 } | |
171 | |
172 #define STACK_SIZE 2048 | |
173 char main_stack[STACK_SIZE]; | |
174 #define stack_last (main_stack+STACK_SIZE) | |
175 | |
176 #endif | |
177 | |
178 #define LOOP_COUNT 500000000 | |
179 int | |
180 main(int ac,char *av[]) | |
181 { | |
182 #if !CC_ONLY | |
183 struct main_continuation *cont; | |
184 stack sp = stack_last; | |
185 #endif | |
186 int sw; | |
187 int j; | |
188 if (ac==2) sw = atoi(av[1]); | |
189 else sw=3; | |
190 | |
191 if (sw==0) { | |
192 for(loop=0;loop<LOOP_COUNT;loop++) { | |
106
85047c4f1ca4
add CbC-examples/rectypeTest
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
193 j = f0(loop); |
31 | 194 } |
195 printf("#0193:%d\n",j); | |
196 #if !CC_ONLY | |
197 } else if (sw==1) { | |
198 loop = LOOP_COUNT; | |
199 sp -= sizeof(*cont); | |
200 cont = (struct main_continuation *)sp; | |
201 cont->ret = main_return; | |
202 cont->main_ret = _CbC_return; | |
203 cont->env = _CbC_environment; | |
106
85047c4f1ca4
add CbC-examples/rectypeTest
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
204 goto f(loop,sp); |
31 | 205 } else if (sw==2) { |
206 loop = LOOP_COUNT; | |
207 sp -= sizeof(*cont); | |
208 cont = (struct main_continuation *)sp; | |
209 cont->ret = main_return2; | |
210 cont->main_ret = _CbC_return; | |
211 cont->env = _CbC_environment; | |
106
85047c4f1ca4
add CbC-examples/rectypeTest
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
212 goto f2(loop,sp); |
31 | 213 } else if (sw==3) { |
214 loop = LOOP_COUNT; | |
215 sp -= sizeof(*cont); | |
216 cont = (struct main_continuation *)sp; | |
217 cont->ret = main_return2_1; | |
218 cont->main_ret = _CbC_return; | |
219 cont->env = _CbC_environment; | |
106
85047c4f1ca4
add CbC-examples/rectypeTest
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
220 goto f2_1(loop,sp); |
31 | 221 #endif |
222 } | |
223 return 0; | |
224 } | |
225 | |
226 /* end */ |