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