annotate paper/chapter1.tex @ 17:3afb4bfe1100

fix
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 15 Feb 2016 07:30:44 +0900
parents ea7938131775
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 \chapter{Continuation based C (CbC)}
4
24e4c08b4e35 implementation
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
2 \label{chapter:CbC}
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
3 CbC では関数ではなく code segment を処理の単位とする. code segment は入力と出力を持ち, CbC では引数が入出力となっている. code segment は次の code segment への遷移で処理を終え, 引数として出力を与える. CbC は C との行き来に環境付き継続を使用する.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
5 \section{CbC における Code Segment}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
6 code segment は CbC における最も基本的な処理単位である. リスト \ref{code_simple} は最も基本的な CbC のコードの一例で, 図 \ref {fig:code_simple}はそれを図示したものである.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
7 code segment の宣言は C の関数の構文と同じように行い, 型に \_\_code を用いる. ただし, これは \_\_code 型の戻り値を返すという意味ではない. \_\_code はそれが関数ではなく code segment であることを示すフラグのようなものである.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
9 現在の code segment から次の code segment への処理の移動は goto の後に code segment 名と引数を並べて記述する リスト \ref{code_simple} の goto cs1(a+b); などがこれにたる. これを軽量継続と呼び, このときの a+b が次のコードセグメントへの出力と成る.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
10
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
11 通常 Secheme の call/cc 等の継続はトップレベルから現在の位置までの位置を環境として保持する. ここで環境は現在のスタックの状態のことを指す.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
12 CbC の軽量継続は呼び出し元の環境を持たない. つまりスタックの情報を破棄して処理を続けていくのである.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
13
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
14 リスト \ref{code_simple} の場合, cs0 は cs1 に継続すると cs0 の環境は破棄されてしまうため, cs0 に戻ることは出来ない.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
15 \begin{lstlisting}[frame=lrbt,label=code_simple,caption={\footnotesize code segment の軽量継続}]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
16 __code cs0(int a, int b){
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
17 goto cs1(a+b);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
18 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
19
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
20 __code cs1(int c){
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
21 goto cs2(c);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
22 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
23 \end{lstlisting}
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
24
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
25 \begin{figure}[htpb]
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
26 \begin{center}
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
27 \scalebox{0.55}{\includegraphics{fig/codesegment.pdf}}
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
28 \end{center}
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
29 \caption{code segment の軽量継続}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
30 \label{fig:code_simple}
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
31 \end{figure}
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
33
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
34 もう少し複雑な CbC のプログラムの例が以下のリスト \ref{factorial} である. これは与えられた数値の階乗を算出するプログラムである. このコードの factorial0 という code segment に注目すると, 条件判別を行い, その結果に応じて自分自身への再帰的な継続を行うか別の code segment への継続を行うかという処理を行っていることがわかる. CbC ではこのようにしてループ処理を制御する.
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
35
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
36 \begin{lstlisting}[frame=lrbt,label=factorial,caption={\footnotesize 階乗を求める CbC プログラムの例}]
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
37 __code print_factorial(int prod)
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
38 {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
39 printf("factorial = %d\n",prod);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
40 exit(0);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
41 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
42
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
43 __code factorial0(int prod, int x)
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
44 {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
45 if ( x >= 1) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
46 goto factorial0(prod*x, x-1);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
47 }else{
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
48 goto print_factorial(prod);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
49 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
50
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
51 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
52
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
53 __code factorial(int x)
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
54 {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
55 goto factorial0(1, x);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
56 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
57
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
58 int main(int argc, char **argv)
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
59 {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
60 int i;
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
61 i = atoi(argv[1]);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
62
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
63 goto factorial(i);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
64 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
65 \end{lstlisting}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
66
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
67 \begin{figure}[htpb]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
68 \begin{center}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
69 \scalebox{0.55}{\includegraphics{fig/factorial.pdf}}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
70 \end{center}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
71 \caption{階乗を求める CbC プログラムの軽量継続図}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
72 \label{fig:factorial}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
73 \end{figure}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
74
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 \section{環境付き継続}
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
76 \label{sec:withEnv}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
77 環境付き継続は C との互換性のために必要な機能である. CbC と C の記述を交える際, CbC の code segment から C の関数の呼び出しは問題なく行える. しかし, C の関数から CbC の code segment へと継続する場合, 呼び出し元の環境に戻るための特殊な継続が必要となる. これを環境付き継続と呼ぶ.
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
78
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
79 環境付き継続を用いる場合, C の関数から code segment へ継続する際に \_\_return, \_\_environment という変数を渡す. \_\_return は \_\_code (*)(return\_type, void*) 型の変数で環境付き継続先が元の環境に戻る際に利用する code segmentを表す. \_\_environment は void** 型の変数で元の関数の環境を表す. リスト\ref{gotoWithTheEnv}では関数 funcB から code segment cs に継続する際に環境付き継続を利用している. cs は funcB から渡された code segment へ継続することで元の C の環境に復帰することが可能となる. 但し復帰先は \_\_return を渡した関数が終了する位置である. このプログラムの例では, 関数 funcA は戻り値として funcB の終わりにある -1 ではなく, 環境付き継続によって渡される 1 を受け取る. 図\ref{fig:gotoWithTheEnv}にこの様子を表した.
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
80
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
81 \begin{lstlisting}[frame=lrbt,label=gotoWithTheEnv,caption={環境付き継続}]
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
82 __code cs(__code (*ret)(int, void*), void *env){
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
83 /* C0 */
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
84 goto ret(1, env);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
85 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
86
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
87 int funcB(){
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
88 /* B0 */
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
89 goto cs(__return, __environment);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
90 /* B1 (never reached). */
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
91 return -1;
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
92 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
93
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
94 int funcA(){
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
95 /* A0 */
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
96 int retval;
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
97 retval = funcB();
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
98 /* A1 */
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
99 printf("retval = %d\n", retval);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
100 /* retval should not be -1 but be 1. */
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
101 return 0;
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
102 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
103
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
104 \end{lstlisting}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
105
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
106 \begin{figure}[htpb]
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
107 \begin{center}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
108 \scalebox{0.55}{\includegraphics{fig/gotowithenv.pdf}}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
109 \end{center}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
110 \caption{環境付き継続}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
111 \label{fig:gotoWithTheEnv}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
112 \end{figure}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
113
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
114
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
115 このように, 環境付き継続を用いることで C, CbC 間の処理の移動が可能になる.
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
116
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 \section{Gears OS サポート}
5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
118 \label{sec:Gears}
17
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
119 Gears OS は当研究室で開発している並列フレームワークで, CbC で記述している. Gears では通常の CbC には存在しない meta code segment, data segment, context 等を用いる. meta code segment は meta computation を行う code segment で, メモリの確保やネットワーク管理等が meta computation にあたる. data segment はデータの単位であり, code segment の入出力になる. context は meta data segment に相当し, code segment や data segment を管理している.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
120
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
121 また meta computation を用いる場合, code segment から code segment への軽量継続の間に meta code segment によって meta computation が行われる. これを図示したのが図 \ref{fig:metaCS} である.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
122
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
123 \begin{figure}[htpb]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
124 \begin{center}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
125 \scalebox{0.55}{\includegraphics{fig/metaCS.pdf}}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
126 \end{center}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
127 \caption{meta computation}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
128 \label{fig:metaCS}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
129 \end{figure}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
130
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
131 これらを現在の CbC の機能のみを用いて記述するとリスト\ref{gears}のようになり, 多くの労力を要する. そのためにこの記述を助ける機能を実装する必要があり, 本研究では以下の機能を提案した.
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
132
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
133 \begin{itemize}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
134 \item code segment から meta code segment への自動接続
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
135 \item 継続時に context から必要な情報を取得する stub の自動生成
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
136 \item code segment 内での context の隠蔽
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
137 \end{itemize}
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
138
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
139 \begin{lstlisting}[frame=lrbt,label=gears,caption={Gears OS コード例}]
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
140 __code meta(struct Context* context, enum Code next) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
141 goto (context->code[next])(context);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
142 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
143
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
144 __code code1_stub(struct Context* context) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
145 goto code1(context, &context->data[Allocate]->allocate);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
146 }
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
1
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
148 __code code1(struct Context* context, struct Allocate* allocate) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
149 allocate->size = sizeof(long);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
150 allocator(context);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
151 goto meta(context, Code2);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
152 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
153
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
154
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
155 __code code2(struct Context* context, long* count) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
156 *count = 0;
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
157 goto meta(context, Code3);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
158 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
159
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
160 __code code2_stub(struct Context* context) {
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
161 goto code2(context, &context->data[Count]->count);
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
162 }
2fd0f505cc68 chapter1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
163 \end{lstlisting}