annotate README.jp @ 339:375d21a2b845

emit_data
author kono
date Fri, 25 Jun 2004 21:28:01 +0900
parents e5d40f8c4cce
children e055df7c1082
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
1 C with Continuation (CwC) and Continuation based C (CbC)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
2 $Id$
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
3 河野 真治
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
4 琉球大学情報工学科
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
5
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
6 0. What is this.
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
7
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
8 これはC言語の拡張であり、下位言語でもあります。サブルーチン
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
9 の代わりに code segment というプログラミング単位を持つ言語
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
10 です。code segment は、軽量継続(light weight continuation)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
11 によって接続されます。C の機能をすべて含む時は、C with
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
12 Continuation と呼びます。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
13
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
14 #include <stdio.h>
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
15
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
16 code factorial(int n,int result,int orig,
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
17 code(*print)(),code(*exit1)(), void *exit1env)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
18 {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
19 if (n<0) {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
20 printf("err %d!\n",n);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
21 goto (*exit1)(0),exit1env;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
22 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
23 if (n==0)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
24 goto (*print)(n,result,orig,print,exit1,exit1env);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
25 else {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
26 result *= n;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
27 n--;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
28 goto factorial(n,result,orig,print,exit1,exit1env);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
29 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
30 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
31
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
32 int main( int ac, char *av[])
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
33 {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
34 int n;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
35 n = 10;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
36 goto factorial(n,1,n,print,return,environment);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
37 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
38
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
39 code print(int n,int result,int orig,code(*print)(),(*exit1)(),void*exit1env)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
40 {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
41 printf("%d! = %d\n",orig, result);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
42 goto (*exit1)(0),exit1env;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
43 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
44
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
45 code segment だけを使い、手続き呼び出しをしないと、Cの下位言語
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
46 となります。これを、Continuation based B (Cbc) と呼びます。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
47 実際、C をCbCにコンパイルすることが可能です。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
48
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
49 CbC は、アーキテクチャに依存しないアセンブラ言語だと思えば良いでしょう。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
50
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
51 1. 構文
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
52
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
53 code code_segment_name(interfaces) {
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
54 body;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
55 }
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
56
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
57 code は code segment を表す型です。code segment ではreturn
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
58 文を使用することはできません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
59
326
e5d40f8c4cce bit-field continue.
kono
parents: 245
diff changeset
60 Interfaces は引数です。構造体も使うことができます。Interface
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
61 の一部はレジスタにマップされるので参照を取ることはできません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
62 構造体は常に可能です。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
63
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
64 code segment から移動するには goto 文を使います。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
65
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
66 goto segment_name(interfaces);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
67
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
68 間接gotoも可能です。同じinterfaceを持つcode segment間では、
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
69 効率の良い移動ができます。普通はjmp命令一つです。(stack
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
70 pointer の移動を含む場合もあるので、そうとは限らないんですが..)
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
71 異なるInterfaceの場合は、並列代入が起きます。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
72
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
73
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
74 2. C との関係。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
75
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
76 CwC からは自由にCの関数を呼び出すことができます。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
77
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
78 C の関数からcode segment にgotoするのは自由ですが、
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
79 戻る場合には、Cの関数の環境を明示する必要があります。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
80
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
81 goto factorial(n,1,n,print,return,environment);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
82
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
83 return と environment は特殊な変数で、それぞれ、呼び出した
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
84 関数の継続(呼び出した関数に戻ることはできません)と、
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
85 その関数の環境(つまりスタック)です。setjump のjump_buf
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
86 に似てますが、allocate することはできません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
87
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
88 void *environment;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
89
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
90 という型を持ちます。return 変数は、元の関数の継続、
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
91 つまりreturn文そのものです。その型は元の関数に
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
92 依存します。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
93
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
94 code (*return)(int return_value);
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
95
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
96 ここにgotoするには環境を明示したgotoを使います。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
97 普通にgotoしてはいけません。コンパイラはチェックしてません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
98 ごめんなさい。多重にfunction-code-function-codeして、
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
99 一番外のheavy continuationに抜けることはできます。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
100
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
101 goto (*exit1)(0),exit1env;
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
102
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
103 thread として使うことは今のところはできません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
104
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
105 3. 使い方
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
106
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
107 mc-powerpc, mc-ia32 がコンパイラです。アセンブラソースを出力します。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
108 アセンブルとリンクにはgccを使って下さい。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
109
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
110 mc-powerpc source.c
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
111 gcc source.s
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
112 a.out を生成。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
113 mc-powerpc source.c
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
114 gcc -c sources.s
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
115 .o を生成。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
116
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
117 -s comments in assembler source.
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
118 -c check only.
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
119 -oname output file names
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
120 -Idir add library include directory
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
121
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
122 test の下にいくつか例題があります。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
123
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
124 3. 実装されてない部分
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
125
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
126 Mips コンパイラはまだ動きません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
127
245
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
128 64 bit long long が動きます。
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
129 // long long, long double は型として使うことはできますが、
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
130 // 演算はできません。変数の定義もできないと思う。
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
131 // Long long value (0LL) は使えますが、単なるlongを返します。
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
132 // long は32bit. int もpointerも同じ。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
133
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
134 Mac OS X と Red hat Linux だけでテストしてあります。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
135
245
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
136 Inline は無視され、普通のstatic関数にコンパイルされます。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
137
245
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
138 //浮動小数点レジスタへの代入計算はできません。
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
139 // *=, /=, +=.
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
140
339
375d21a2b845 emit_data
kono
parents: 326
diff changeset
141 // built-in allocaはありません。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
142
339
375d21a2b845 emit_data
kono
parents: 326
diff changeset
143 // varargs もないです。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
144
339
375d21a2b845 emit_data
kono
parents: 326
diff changeset
145 // Switch 文は、分岐にコンパイルされます。テーブルは生成されません。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
146
339
375d21a2b845 emit_data
kono
parents: 326
diff changeset
147 // マクロの機能のうち連結とかは動きません。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
148
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
149 マクロはコルーチンで、プリプロセッサではありません。振舞いがcppと
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
150 少し異なります。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
151
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
152 デバッグ用の -g はありません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
153
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
154 CbC のcode segment から実行を始めることはできません。
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
155
245
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
156 // #include は、呼び出したソースコードのカレントディレクトリをサーチしません。
8a72b0afccfc *** empty log message ***
kono
parents: 192
diff changeset
157 #include は、呼び出したソースコードのカレントディレクトリをサーチします。
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
158
339
375d21a2b845 emit_data
kono
parents: 326
diff changeset
159 bit-field (こんなの要らないけどさ...)
375d21a2b845 emit_data
kono
parents: 326
diff changeset
160
192
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
161 その他、たくさん。ANSI コンパチを目指しているわけではないので...
b0d6a6940cb7 *** empty log message ***
kono
parents:
diff changeset
162