Mercurial > hg > CbC > CbC_examples
comparison test1.c @ 0:bacef8675607
init repository
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Oct 2014 17:34:59 +0900 |
parents | |
children | 35d6eabeadb0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bacef8675607 |
---|---|
1 #define __environment _CbC_environment | |
2 #define __return _CbC_return | |
3 | |
4 /* | |
5 test for CbC converted __code from C | |
6 */ | |
7 | |
8 #include <stdio.h> | |
9 | |
10 extern void *malloc(int); | |
11 | |
12 typedef void *stack; | |
13 | |
14 void *stack0; /* size of void* == 1 */ | |
15 | |
16 struct cont_save { /* General Return Continuation */ | |
17 __code (*ret)(); | |
18 }; | |
19 | |
20 /* | |
21 __code g(int,void *); | |
22 __code f_g0(int ,int ,void *); | |
23 __code f_g1(int,void *); | |
24 */ | |
25 | |
26 struct f_g0_save { /* Specialized Return Continuation */ | |
27 __code (*ret)(); | |
28 int ii,kk,jj; | |
29 }; | |
30 | |
31 __code g(int i,void *sp) { | |
32 goto (* ((struct cont_save *)sp)->ret)(i+4,sp); | |
33 } | |
34 | |
35 __code f_g1(int j,void *sp) { /* Continuation */ | |
36 int k; | |
37 struct f_g0_save *c; | |
38 | |
39 c = sp; | |
40 k = c->kk; | |
41 sp += sizeof(struct f_g0_save); | |
42 goto (* ((struct cont_save *)sp)->ret)(k+4+j,sp); | |
43 } | |
44 | |
45 __code f(int i,void *sp) { | |
46 int k,j; | |
47 struct f_g0_save *c; | |
48 printf("#0047:f 0 sp: %x\n",sp-stack0); | |
49 | |
50 k = 3+i; | |
51 | |
52 printf("#0051:f 1 sp: %x\n",sp-stack0); | |
53 sp -= sizeof(struct f_g0_save); | |
54 printf("#0053:f 2 sp: %x\n",sp-stack0); | |
55 c = sp; | |
56 c->kk = k; | |
57 c->ii = i; | |
58 c->jj = j; | |
59 c->ret = f_g1; | |
60 goto g(i,sp); | |
61 } | |
62 | |
63 | |
64 | |
65 struct f0_save { /* Specialized Return Continuation */ | |
66 __code (*ret)(); | |
67 __code (*exit1)(); | |
68 void *exit1env; | |
69 int jj; | |
70 }; | |
71 | |
72 __code f1(int i,void *sp) ; | |
73 __code f0(int i,int j,__code(*exit2)(), void *exit2env,void *sp) | |
74 { | |
75 struct f0_save *c; | |
76 printf("#0075:f0 1 sp: %x\n",sp-stack0); | |
77 sp -= sizeof(struct f0_save); | |
78 printf("#0077:f0 2 sp: %x\n",sp-stack0); | |
79 c = sp; | |
80 c->jj = j; | |
81 c->exit1 = exit2; | |
82 c->exit1env = exit2env; | |
83 c->ret = f1; | |
84 printf("#0083:f0 3 sp: %x\n",sp-stack0); | |
85 goto f(i,sp); | |
86 } | |
87 | |
88 __code print(int i,int j,__code (*exit1)(),void*exit1env); | |
89 | |
90 __code f1(int i,void *sp) { | |
91 int j; | |
92 int *exit2env; | |
93 __code (*exit2)(); | |
94 struct f0_save *c; | |
95 | |
96 c = sp; | |
97 j = c->jj; | |
98 exit2 = c->exit1; | |
99 exit2env = c->exit1env; | |
100 | |
101 sp += sizeof(struct f0_save); | |
102 goto print(i,j,exit2,exit2env); | |
103 } | |
104 | |
105 int main( int ac, char *av[]) | |
106 { | |
107 int i,j; | |
108 int *sp; | |
109 | |
110 // i = atoi(av[1]); | |
111 i = 1; | |
112 stack0 = ((char *)malloc(1024)+1024); | |
113 sp = stack0; | |
114 j = i; | |
115 | |
116 printf("#0115:sp: %x %x\n",sp-(int*)stack0,sizeof(*stack0)); | |
117 goto f0(i,j,__return,__environment,sp); | |
118 } | |
119 | |
120 __code print(int i,int j,__code (*exit1)(),void*exit1env) | |
121 { | |
122 printf("#0121:%d %d\n",i,j); | |
123 goto (*exit1)(0,exit1env); | |
124 } | |
125 |