Mercurial > hg > CbC > CbC_gcc
annotate CbC-examples/stack1.c @ 133:420680fc7707
do normal call in goto codesegment in normal function
author | anatofuz |
---|---|
date | Sat, 03 Nov 2018 19:49:09 +0900 |
parents | 5d30d517ebed |
children | 26042f4007d5 |
rev | line source |
---|---|
16 | 1 /* |
2 test for CbC converted __code from C | |
3 */ | |
4 | |
133
420680fc7707
do normal call in goto codesegment in normal function
anatofuz
parents:
126
diff
changeset
|
5 #include <stdio.h> |
16 | 6 #define NULL 0 |
126 | 7 extern int printf(const char*, ...); |
16 | 8 |
126 | 9 extern void *malloc(size_t); |
16 | 10 |
11 typedef void *stack; | |
12 | |
13 void *stack0; /* size of void* == 1 */ | |
14 | |
15 struct cont_save { /* General Return Continuation */ | |
16 __code (*ret)(); | |
17 }; | |
18 | |
19 __code g(int,void *); | |
20 __code f_g0(int ,int ,void *); | |
21 __code f_g1(int,void *); | |
22 __code print(int i,int j,__code (*exit1)(),void*exit1env); | |
23 | |
24 struct f_g0_save { /* Specialized Return Continuation */ | |
25 __code (*ret)(); | |
26 int ii,kk,jj; | |
27 }; | |
28 | |
29 __code g(int i,void *sp) { | |
30 goto (* ((struct cont_save *)sp)->ret)(i+4,sp); | |
31 } | |
32 | |
33 __code __attribute__ ((fastcall)) f_g1(int j,void *sp) { /* Continuation */ | |
34 int k; | |
35 struct f_g0_save *c; | |
36 | |
37 c = sp; | |
38 k = c->kk; | |
39 sp += sizeof(struct f_g0_save); | |
40 goto (* ((struct cont_save *)sp)->ret)(k+4+j,sp); | |
41 } | |
42 | |
43 __code f(int i,void *sp) { | |
44 int k,j; | |
45 struct f_g0_save *c; | |
46 printf("#0042:f 0 sp: %x\n",sp-stack0); | |
47 | |
48 k = 3+i; | |
49 | |
50 printf("#0046:f 1 sp: %x\n",sp-stack0); | |
51 sp -= sizeof(struct f_g0_save); | |
52 printf("#0048:f 2 sp: %x\n",sp-stack0); | |
53 c = sp; | |
54 c->kk = k; | |
55 c->ii = i; | |
56 c->jj = j; | |
57 c->ret = f_g1; | |
58 goto g(i,sp); | |
59 } | |
60 | |
61 | |
62 | |
63 struct f0_save { /* Specialized Return Continuation */ | |
64 __code (*ret)(); | |
65 __code (*exit1)(); | |
66 void *exit1env; | |
67 int jj; | |
68 }; | |
69 | |
70 __code f1(int i,void *sp) ; | |
71 __code f0(int i,int j,__code(*exit2)(), void *exit2env,void *sp) | |
72 { | |
73 struct f0_save *c; | |
74 printf("#0070:f0 1 sp: %x\n",sp-stack0); | |
75 sp -= sizeof(struct f0_save); | |
76 printf("#0072:f0 2 sp: %x\n",sp-stack0); | |
77 c = sp; | |
78 c->jj = j; | |
79 c->exit1 = exit2; | |
80 c->exit1env = exit2env; | |
81 c->ret = f1; | |
82 printf("#0078:f0 3 sp: %x\n",sp-stack0); | |
83 goto f(i,sp); | |
84 } | |
85 | |
86 __code f1(int i,void *sp) { | |
87 int j; | |
88 int *exit2env; | |
89 __code (*exit2)(); | |
90 struct f0_save *c; | |
91 | |
92 c = sp; | |
93 j = c->jj; | |
94 exit2 = c->exit1; | |
95 exit2env = c->exit1env; | |
96 | |
97 sp += sizeof(struct f0_save); | |
98 goto print(i,j,exit2,exit2env); | |
99 } | |
100 | |
101 int main(int ac, char*av[]){ | |
102 main0(ac,av); | |
103 } | |
104 | |
105 int main0( 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("#0108:sp: %x %x\n",sp-(int*)stack0,sizeof(*stack0)); | |
117 //goto f0(i,j,_CbC_return,_CbC_environment,sp); | |
118 goto f0(i,j,NULL,NULL,sp); | |
119 } | |
120 | |
121 __code print(int i,int j,__code (*exit1)(),void*exit1env) | |
122 { | |
123 printf("#0114:%d %d\n",i,j); | |
124 //goto (*exit1)(0),exit1env; | |
125 exit(0); | |
126 } | |
127 |