Mercurial > hg > Papers > 2017 > mitsuki-thesis
comparison final_main/chapter2.tex @ 0:30a433a94a9a
first commit
author | mir3636 |
---|---|
date | Fri, 10 Feb 2017 16:24:11 +0900 |
parents | |
children | 6d00f6c9bb8a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:30a433a94a9a |
---|---|
1 \chapter{Continuation based C (CbC)} | |
2 \section{Continuation based C (CbC)} | |
3 CbC は 処理を Code Gear とした単位を用いて記述するプログラミング言語である。 | |
4 Code Gear から次の Code Gear へと goto による継続で遷移をし処理を行う。 | |
5 図\ref{fig:cs}は Code Gear 間の処理の流れを表している。 | |
6 | |
7 \begin{figure}[htpb] | |
8 \begin{center} | |
9 \scalebox{0.7}{\includegraphics{fig/codesegment.pdf}} | |
10 \end{center} | |
11 \caption{goto による code gear 間の継続} | |
12 \label{fig:cs} | |
13 \end{figure} | |
14 | |
15 \section{Code Gear} | |
16 Code Gear は CbC における最も基本的な処理単位である。 | |
17 リスト \ref{code_simple} は最も基本的な CbC のコードの一例で、図 \ref{fig:code_simple}はそれを図示したものである。 | |
18 CbC では Code Gear は \_\_code という型を持つ関数の構文で定義される。 | |
19 Code Gear は戻り値を持たないので、関数とは異なり return 文は存在しない。goto の後に Code Gear 名と引数を並べて、次の Code Gear の遷移を記述する。 | |
20 この goto の行き先を継続と呼ぶ。Scheme の継続と異なり CbC には呼び出し元の環境がないので、この継続は単なる行き先である。したがってこれを軽量継続と呼ぶこともある。 | |
21 軽量継続により、並列化、ループ制御、関数コールとスタックの操作を意識した最適化がソースコードレベルで行えるようにする。 | |
22 | |
23 \begin{lstlisting}[frame=lrbt,label=code_simple,caption={\footnotesize code segment の軽量継続}] | |
24 __code cs0(int a, int b){ | |
25 goto cs1(a+b); | |
26 } | |
27 | |
28 __code cs1(int c){ | |
29 goto cs2(c); | |
30 } | |
31 \end{lstlisting} | |
32 | |
33 \begin{figure}[htpb] | |
34 \begin{center} | |
35 \scalebox{0.55}{\includegraphics{fig/codesegment2.pdf}} | |
36 \end{center} | |
37 \caption{code segment の軽量継続} | |
38 \label{fig:code_simple} | |
39 \end{figure} | |
40 | |
41 \section{環境付き継続} | |
42 環境付き継続は C との互換性のために必要な機能である。 | |
43 CbC と C の記述を交える際、CbC の Code Gear から C の関数の呼び出しは問題なく行える。 | |
44 しかし、C の関数から CbC の Code Gear へと継続する場合、呼び出し元の環境に戻るための特殊な継続が必要となる。 | |
45 これを環境付き継続と呼ぶ。 | |
46 | |
47 環境付き継続を用いる場合、C の関数から Code Gear へ継続する際に \_\_ return、\_\_environment という変数を渡す。 | |
48 \_\_return は \_\_code (*)(return\_type, void*) 型の変数で環境付き継続先が元の環境に戻る際に利用する Code Gear を表す。 | |
49 \_\_environment は void** 型の変数で元の関数の環境を表す。 | |
50 リスト\ref{gotoWithTheEnv}では関数 funcB から Code Gear cs に継続する際に環境付き継続を利用している。 | |
51 cs は funcB から渡された Code Gear へ継続することで元の C の環境に復帰することが可能となる。 | |
52 但し復帰先は \_\_return を渡した関数が終了する位置である。 | |
53 このプログラムの例では、関数 funcA は戻り値として funcB の終わりにある -1 ではなく、環境付き継続によって渡される 1 を受け取る。 | |
54 図\ref{fig:gotoWithTheEnv}にこの様子を表した。 | |
55 | |
56 \begin{lstlisting}[frame=lrbt,label=gotoWithTheEnv,caption={環境付き継続}] | |
57 __code cs(__code (*ret)(int, void*), void *env){ | |
58 /* C0 */ | |
59 goto ret(1, env); | |
60 } | |
61 | |
62 int funcB(){ | |
63 /* B0 */ | |
64 goto cs(__return, __environment); | |
65 /* B1 (never reached). */ | |
66 return -1; | |
67 } | |
68 | |
69 int funcA(){ | |
70 /* A0 */ | |
71 int retval; | |
72 retval = funcB(); | |
73 /* A1 */ | |
74 printf("retval = %d\n", retval); | |
75 /* retval should not be -1 but be 1. */ | |
76 return 0; | |
77 } | |
78 | |
79 \end{lstlisting} | |
80 | |
81 \begin{figure}[htpb] | |
82 \begin{center} | |
83 \scalebox{0.55}{\includegraphics{fig/gotowithenv.pdf}} | |
84 \end{center} | |
85 \caption{環境付き継続} | |
86 \label{fig:gotoWithTheEnv} | |
87 \end{figure} | |
88 | |
89 このように、環境付き継続を用いることで C、CbC 間の処理の移動が可能になる。 | |
90 | |
91 %Data Gear はデータの単位であり、int や文字列などの Primitive Type を持っている。 | |
92 | |
93 %Code Gear は 任意の数の Input Data Gear を参照して処理を行い、Output Data Gear を出力し処理を終える。 | |
94 %また、接続された Data Gear 以外には参照を行わない。 | |
95 | |
96 %処理やデータの構造が Code Gear、Data Gear に閉じているため、これにより実行時間、メモリ使用量などを予測可能なものにすることが可能になる。 | |
97 |