comparison Paper/source/conv1.c~ @ 47:e07c7952f237

add sources
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Wed, 23 Nov 2011 02:27:44 +0900
parents
children
comparison
equal deleted inserted replaced
46:db63cc25d890 47:e07c7952f237
1 #include <stdio.h>
2 #include <stdlib.h>
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 typedef char *stack;
12 #include "conv1.h"
13
14 /* classical function call case (0) */
15
16 f0(int i) {
17 int k,j;
18 k = 3+i;
19 j = g0(i+3);
20 return k+4+j;
21 }
22
23 g0(int i) {
24 return h0(i+4)+i;
25 }
26
27 h0(int i) {
28 return i+4;
29 }
30
31 #if !CC_ONLY
32
33 /* straight conversion case (1) */
34
35
36 struct cont_interface { // General Return Continuation
37 __code (*ret)();
38 };
39
40 __code f(int i,stack sp) {
41 int k,j;
42 k = 3+i;
43 goto f_g0(i,k,sp);
44 }
45
46 struct f_g0_interface { // Specialized Return Continuation
47 __code (*ret)();
48 int i_,k_,j_;
49 };
50
51 __code f_g1(int j,stack sp);
52
53 __code f_g0(int i,int k,stack sp) { // Caller
54 struct f_g0_interface *c =
55 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
56
57 c->ret = f_g1;
58 c->k_ = k;
59 c->i_ = i;
60
61 goto g(i+3,sp);
62 }
63
64 __code f_g1(int j,stack sp) { // Continuation
65 struct f_g0_interface *c = (struct f_g0_interface *)sp;
66 int k = c->k_;
67 sp+=sizeof(struct f_g0_interface);
68 c = (struct f_g0_interface *)sp;
69 goto (c->ret)(k+4+j,sp);
70 }
71
72 __code g_h1(int j,stack sp);
73
74 __code g(int i,stack sp) { // Caller
75 struct f_g0_interface *c =
76 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
77
78 c->ret = g_h1;
79 c->i_ = i;
80
81 goto h(i+3,sp);
82 }
83
84 __code g_h1(int j,stack sp) { // Continuation
85 struct f_g0_interface *c = (struct f_g0_interface *)sp;
86 int i = c->i_;
87 sp+=sizeof(struct f_g0_interface);
88 c = (struct f_g0_interface *)sp;
89 goto (c->ret)(j+i,sp);
90 }
91
92 __code h(int i,stack sp) {
93 struct f_g0_interface *c = (struct f_g0_interface *)sp;
94 goto (c->ret)(i+4,sp);
95 }
96
97 struct main_continuation { // General Return Continuation
98 __code (*ret)();
99 __code (*main_ret)(int,void*);
100 void *env;
101 };
102
103 __code main_return(int i,stack sp) {
104 if (loop-->0)
105 goto f(233,sp);
106 printf("#0103:%d\n",i);
107 goto (( (struct main_continuation *)sp)->main_ret)(0,
108 ((struct main_continuation *)sp)->env);
109 }
110
111 /* little optimzation without stack continuation (2) */
112
113 __code f2(int i,char *sp) {
114 int k,j;
115 k = 3+i;
116 goto g2(i,k,i+3,sp);
117 }
118
119 __code g2(int i,int k,int j,char *sp) {
120 j = j+4;
121 goto h2(i,k+4+j,sp);
122 }
123
124 __code h2_1(int i,int k,int j,char *sp) {
125 goto main_return2(i+j,sp);
126 }
127
128 __code h2(int i,int k,char *sp) {
129 goto h2_1(i,k,i+4,sp);
130 }
131
132 __code main_return2(int i,stack sp) {
133 if (loop-->0)
134 goto f2(233,sp);
135 printf("#0132:%d\n",i);
136 goto (( (struct main_continuation *)sp)->main_ret)(0,
137 ((struct main_continuation *)sp)->env);
138 }
139
140 /* little optimizaed case (3) */
141
142 __code f2_1(int i,char *sp) {
143 int k,j;
144 k = 3+i;
145 goto g2_1(k,i+3,sp);
146 }
147
148 __code g2_1(int k,int i,char *sp) {
149 goto h2_11(k,i+4,sp);
150 }
151
152 __code f2_0_1(int k,int j,char *sp);
153 __code h2_1_1(int i,int k,int j,char *sp) {
154 goto f2_0_1(k,i+j,sp);
155 }
156
157 __code h2_11(int i,int k,char *sp) {
158 goto h2_1_1(i,k,i+4,sp);
159 }
160
161 __code f2_0_1(int k,int j,char *sp) {
162 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
163 }
164
165 __code main_return2_1(int i,stack sp) {
166 if (loop-->0)
167 goto f2_1(233,sp);
168 printf("#0165:%d\n",i);
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 */