192
|
1 C with Continuation (CwC) and Continuation based C (CbC)
|
|
2 $Id$
|
|
3 Shinji Kono
|
|
4 University of the Ryukyus
|
|
5
|
|
6 0. What is this.
|
|
7
|
|
8 This is a extension of C ( and a subset of C ). It has a
|
|
9 programming unit which is called code segment. Code
|
|
10 segment can be communicate with so called light weight continuation.
|
|
11
|
|
12
|
|
13 #include <stdio.h>
|
|
14
|
|
15 code factorial(int n,int result,int orig,
|
|
16 code(*print)(),code(*exit1)(), void *exit1env)
|
|
17 {
|
|
18 if (n<0) {
|
|
19 printf("err %d!\n",n);
|
|
20 goto (*exit1)(0),exit1env;
|
|
21 }
|
|
22 if (n==0)
|
|
23 goto (*print)(n,result,orig,print,exit1,exit1env);
|
|
24 else {
|
|
25 result *= n;
|
|
26 n--;
|
|
27 goto factorial(n,result,orig,print,exit1,exit1env);
|
|
28 }
|
|
29 }
|
|
30
|
|
31 int main( int ac, char *av[])
|
|
32 {
|
|
33 int n;
|
|
34 n = 10;
|
|
35 goto factorial(n,1,n,print,return,environment);
|
|
36 }
|
|
37
|
|
38 code print(int n,int result,int orig,code(*print)(),(*exit1)(),void*exit1env)
|
|
39 {
|
|
40 printf("%d! = %d\n",orig, result);
|
|
41 goto (*exit1)(0),exit1env;
|
|
42 }
|
|
43
|
|
44 If you don't use function call, this language becomes subset of C.
|
|
45 It is called Continuation based C. Actually it can be a lower layer of
|
|
46 C language. Normal C program can be compiled into CbC.
|
|
47
|
|
48 CbC is a kind of architecture independent assembler language.
|
|
49
|
|
50 1. Syntax
|
|
51
|
|
52 code code_segment_name(interfaces) {
|
|
53 body;
|
|
54 }
|
|
55
|
|
56 code is a type for code segment. A code segment has no return
|
|
57 statements.
|
|
58
|
|
59 Interfaces are arguments. It can be struts. or unions. Some
|
|
60 part of interfaces are mapped into registers. No
|
|
61 references are allowed in register interface variables.
|
|
62
|
|
63 Goto statements transfer the control from a segment
|
|
64 to a segment.
|
|
65 goto segment_name(interfaces);
|
|
66 If two code segments has a same interfaces, transfer cost is very
|
|
67 small. It us usually a single jump instruction.
|
|
68 If there are differences, some parallel assignment is performed.
|
|
69
|
|
70 2. Interaction between function and code segment.
|
|
71
|
|
72 In CwC, you can call C function at any time in code segments.
|
|
73 If you want to call code segment from C and want to do some
|
|
74 return, explicit handling of function environment.
|
|
75
|
|
76 goto factorial(n,1,n,print,return,environment);
|
|
77
|
|
78 return and environment is a special variable which contains
|
|
79 return point. An environment variable is something like jumpbuf
|
|
80 in setjump, but it is a simple pointer. Unlike jumpbuf,
|
|
81 there is no way to allocate are for environment.
|
|
82 void *environment;
|
|
83
|
|
84 A return variable is a continuation with environment of
|
|
85 original function. It's type is varied for called function.
|
|
86
|
|
87 code (*return)(int return_value);
|
|
88
|
|
89 To go to the continuation, use goto with environment.
|
|
90
|
|
91 goto (*exit1)(0),exit1env;
|
|
92
|
|
93 3. How to use
|
|
94
|
|
95 mc-powerpc, mc-ia32 is a compiler. It generates assembler
|
|
96 source code .s from C source code.
|
|
97
|
|
98 mc-powerpc source.c
|
|
99 gcc source.s
|
|
100 generates a.out.
|
|
101 mc-powerpc source.c
|
|
102 gcc -c sources.s
|
|
103 generates sources.o
|
|
104
|
|
105 -s comments in assembler source.
|
|
106 -c check only.
|
538
|
107 -v report memory usage
|
192
|
108 -oname output file names
|
538
|
109 -Dname define name
|
|
110 -Dname=value define name as value
|
|
111 '-Dname(a)=a+1' define macro function with args
|
|
112 -Idir/ add library include directory. / is necessary.
|
192
|
113
|
|
114 Some examples can be fond in test directory.
|
|
115
|
|
116 3. Unimplemented lists
|
|
117
|
|
118 Mips version is not ready.
|
|
119
|
245
|
120 64bit long long is now supported
|
|
121 // long long, long double, unsigned long long can be used as type,
|
|
122 // but no operation (including assignment) are no allowed.
|
|
123 // Long long value (0LL) can be used but it gives long value.
|
|
124 // Long is equal to an int and a pointer (32bit).
|
192
|
125
|
|
126 Only Mac OS X and Red hat Linux is supported.
|
|
127
|
|
128 Inline directive is ignored and gives normal function definition.
|
|
129
|
245
|
130 Register float is supported in Power PC
|
|
131 // Register float arguments does no accepts assignment operation such as
|
|
132 // *=, /=, +=.
|
192
|
133
|
326
|
134 //No built-in alloca.
|
192
|
135
|
326
|
136 // No varargs.
|
|
137
|
|
138 Use stdarg
|
192
|
139
|
326
|
140 // Switch statements is implemented as series of compare and branch,
|
|
141 // no tables.
|
192
|
142
|
326
|
143 // Some operations such as concatenation are not implemented in macro
|
|
144 // processor.
|
192
|
145
|
|
146 Macro processor is a coroutine in this compiler, slightly different
|
|
147 from cpp.
|
|
148
|
|
149 No -g support.
|
|
150
|
|
151 No runtime driver for CbC.
|
|
152
|
245
|
153 // #include does not search, sources current directory.
|
|
154 #include does search, sources current directory.
|
192
|
155
|
339
|
156 bit-field is supported, but no global initialization of bit-field.
|