192
|
1 C with Continuation (CwC) and Continuation based C (CbC)
|
607
|
2
|
192
|
3 Shinji Kono
|
|
4 University of the Ryukyus
|
|
5
|
|
6 0. What is this.
|
|
7
|
861
|
8 We have gcc and LLVM version. You should use these.
|
|
9
|
192
|
10 This is a extension of C ( and a subset of C ). It has a
|
|
11 programming unit which is called code segment. Code
|
|
12 segment can be communicate with so called light weight continuation.
|
|
13
|
|
14
|
|
15 #include <stdio.h>
|
|
16
|
|
17 code factorial(int n,int result,int orig,
|
|
18 code(*print)(),code(*exit1)(), void *exit1env)
|
|
19 {
|
|
20 if (n<0) {
|
|
21 printf("err %d!\n",n);
|
|
22 goto (*exit1)(0),exit1env;
|
|
23 }
|
|
24 if (n==0)
|
|
25 goto (*print)(n,result,orig,print,exit1,exit1env);
|
|
26 else {
|
|
27 result *= n;
|
|
28 n--;
|
|
29 goto factorial(n,result,orig,print,exit1,exit1env);
|
|
30 }
|
|
31 }
|
|
32
|
|
33 int main( int ac, char *av[])
|
|
34 {
|
|
35 int n;
|
|
36 n = 10;
|
|
37 goto factorial(n,1,n,print,return,environment);
|
|
38 }
|
|
39
|
|
40 code print(int n,int result,int orig,code(*print)(),(*exit1)(),void*exit1env)
|
|
41 {
|
|
42 printf("%d! = %d\n",orig, result);
|
|
43 goto (*exit1)(0),exit1env;
|
|
44 }
|
|
45
|
|
46 If you don't use function call, this language becomes subset of C.
|
|
47 It is called Continuation based C. Actually it can be a lower layer of
|
|
48 C language. Normal C program can be compiled into CbC.
|
|
49
|
|
50 CbC is a kind of architecture independent assembler language.
|
|
51
|
|
52 1. Syntax
|
|
53
|
|
54 code code_segment_name(interfaces) {
|
|
55 body;
|
|
56 }
|
|
57
|
|
58 code is a type for code segment. A code segment has no return
|
|
59 statements.
|
|
60
|
|
61 Interfaces are arguments. It can be struts. or unions. Some
|
|
62 part of interfaces are mapped into registers. No
|
|
63 references are allowed in register interface variables.
|
|
64
|
|
65 Goto statements transfer the control from a segment
|
|
66 to a segment.
|
|
67 goto segment_name(interfaces);
|
|
68 If two code segments has a same interfaces, transfer cost is very
|
|
69 small. It us usually a single jump instruction.
|
|
70 If there are differences, some parallel assignment is performed.
|
|
71
|
|
72 2. Interaction between function and code segment.
|
|
73
|
|
74 In CwC, you can call C function at any time in code segments.
|
|
75 If you want to call code segment from C and want to do some
|
|
76 return, explicit handling of function environment.
|
|
77
|
|
78 goto factorial(n,1,n,print,return,environment);
|
|
79
|
|
80 return and environment is a special variable which contains
|
|
81 return point. An environment variable is something like jumpbuf
|
|
82 in setjump, but it is a simple pointer. Unlike jumpbuf,
|
|
83 there is no way to allocate are for environment.
|
|
84 void *environment;
|
|
85
|
|
86 A return variable is a continuation with environment of
|
|
87 original function. It's type is varied for called function.
|
|
88
|
|
89 code (*return)(int return_value);
|
|
90
|
|
91 To go to the continuation, use goto with environment.
|
|
92
|
|
93 goto (*exit1)(0),exit1env;
|
|
94
|
|
95 3. How to use
|
|
96
|
|
97 mc-powerpc, mc-ia32 is a compiler. It generates assembler
|
|
98 source code .s from C source code.
|
|
99
|
|
100 mc-powerpc source.c
|
|
101 gcc source.s
|
|
102 generates a.out.
|
|
103 mc-powerpc source.c
|
|
104 gcc -c sources.s
|
|
105 generates sources.o
|
|
106
|
|
107 -s comments in assembler source.
|
|
108 -c check only.
|
538
|
109 -v report memory usage
|
192
|
110 -oname output file names
|
538
|
111 -Dname define name
|
|
112 -Dname=value define name as value
|
|
113 '-Dname(a)=a+1' define macro function with args
|
|
114 -Idir/ add library include directory. / is necessary.
|
192
|
115
|
|
116 Some examples can be fond in test directory.
|
|
117
|
|
118 3. Unimplemented lists
|
|
119
|
|
120 Mips version is not ready.
|
|
121
|
245
|
122 64bit long long is now supported
|
|
123 // long long, long double, unsigned long long can be used as type,
|
|
124 // but no operation (including assignment) are no allowed.
|
|
125 // Long long value (0LL) can be used but it gives long value.
|
|
126 // Long is equal to an int and a pointer (32bit).
|
192
|
127
|
|
128 Only Mac OS X and Red hat Linux is supported.
|
637
|
129 Intel Mac is supported.
|
192
|
130
|
|
131 Inline directive is ignored and gives normal function definition.
|
|
132
|
245
|
133 Register float is supported in Power PC
|
|
134 // Register float arguments does no accepts assignment operation such as
|
|
135 // *=, /=, +=.
|
192
|
136
|
326
|
137 //No built-in alloca.
|
192
|
138
|
326
|
139 // No varargs.
|
|
140
|
|
141 Use stdarg
|
192
|
142
|
326
|
143 // Switch statements is implemented as series of compare and branch,
|
|
144 // no tables.
|
192
|
145
|
326
|
146 // Some operations such as concatenation are not implemented in macro
|
|
147 // processor.
|
192
|
148
|
|
149 Macro processor is a coroutine in this compiler, slightly different
|
|
150 from cpp.
|
|
151
|
|
152 No -g support.
|
|
153
|
|
154 No runtime driver for CbC.
|
|
155
|
245
|
156 // #include does not search, sources current directory.
|
|
157 #include does search, sources current directory.
|
192
|
158
|
339
|
159 bit-field is supported, but no global initialization of bit-field.
|
607
|
160
|
|
161
|
|
162 /************************************************************************
|
|
163 ** Copyright (C) 2006 Shinji Kono
|
|
164 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
165 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
166 **
|
|
167 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
168 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
169 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
170 ** 営利利用も上記に反しない範囲で許可します。
|
|
171 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
172 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
173 **
|
|
174 ** Everyone is permitted to do anything on this program
|
|
175 ** including copying, modifying, improving,
|
|
176 ** as long as you don't try to pretend that you wrote it.
|
|
177 ** i.e., the above copyright notice has to appear in all copies.
|
|
178 ** Binary distribution requires original version messages.
|
|
179 ** You don't have to ask before copying, redistribution or publishing.
|
|
180 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
181 ***********************************************************************/
|
|
182
|