comparison CbC-examples/conv.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 0eb6cac880f0
comparison
equal deleted inserted replaced
20:9de9dad105d4 21:959d4c8c8abc
1 #include "stdio.h"
2
3 f0(int i) {
4 int k,j;
5 k = 3+i;
6 j = g0(i+3);
7 return k+4+j;
8 }
9
10 g0(int i) {
11 return i+4;
12 }
13
14
15 typedef void *stack;
16
17 __code f_g0(int i,int k,stack sp) ;
18
19 struct cont_interface { // General Return Continuation
20 __code (*ret)(int, void*);
21 };
22
23 __code f(int i,stack sp) {
24 int k,j;
25 k = 3+i;
26 goto f_g0(i,k,sp);
27 }
28
29 struct f_g0_interface { // Specialized Return Continuation
30 __code (*ret)();
31 int i_,k_,j_;
32 };
33
34 __code f_g1(int j,stack sp);
35 __code g(int i,stack sp) ;
36
37 __code f_g0(int i,int k,stack sp) { // Caller
38 struct f_g0_interface *c =
39 (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
40
41 c->ret = f_g1;
42 c->k_ = k;
43 c->i_ = i;
44
45 goto g(i+3,sp);
46 }
47
48 __code f_g1(int j,stack sp) { // Continuation
49 struct f_g0_interface *c = (struct f_g0_interface *)sp;
50 int k = c->k_;
51 sp += sizeof(struct f_g0_interface);
52 goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
53 }
54
55 __code g(int i,stack sp) {
56 goto (( (struct cont_interface *)sp)->ret)(i+4,sp);
57 }
58
59 struct main_continuation { // General Return Continuation
60 __code (*ret)(int, void*);
61 __code (*main_ret)(int, void*);
62 void *env;
63 };
64
65 __code main_return(int i,stack sp) {
66 printf("#0061:%d\n",i);
67 goto (( (struct main_continuation *)sp)->main_ret)(0,
68 ((struct main_continuation *)sp)->env);
69 }
70
71 #define STACK_SIZE 2048
72 char main_stack[STACK_SIZE];
73 #define stack_last (&main_stack[STACK_SIZE])
74
75 typedef __code (*return_type)(int, void*);
76 int
77 main(int argc, char **argv)
78 {
79 struct main_continuation *cont;
80 stack sp = stack_last;
81
82 printf("#0075:%d\n",f0(233));
83
84 sp -= sizeof(*cont);
85 cont = (struct main_continuation *)sp;
86 cont->ret = main_return;
87 cont->main_ret = (return_type) _CbC_return;
88 cont->env = _CbC_environment;
89 goto f(233,sp);
90 }
91
92 /* end */