view final_main/chapter2.tex @ 4:6d00f6c9bb8a

update
author mir3636
date Mon, 13 Feb 2017 16:28:35 +0900
parents 30a433a94a9a
children 5b368e14bb64
line wrap: on
line source

\chapter{Continuation based C (CbC)}
\section{Continuation based C (CbC)}
CbC は 処理を Code Gear とした単位を用いて記述するプログラミング言語である。
Code Gear から次の Code Gear へと goto による継続で遷移をし処理を行う。
図\ref{fig:cs}は Code Gear 間の処理の流れを表している。

\begin{figure}[htpb]
 \begin{center}
  \scalebox{0.7}{\includegraphics{fig/codesegment.pdf}}
 \end{center}
 \caption{goto による code gear 間の継続}
 \label{fig:cs}
\end{figure}

\section{Code Gear}
Code Gear は CbC における最も基本的な処理単位である。
リスト \ref{code_simple} は最も基本的な CbC のコードの一例で、図 \ref{fig:code_simple}はそれを図示したものである。
CbC では Code Gear は \_\_code という型を持つ関数の構文で定義される。
Code Gear は戻り値を持たないので、関数とは異なり return 文は存在しない。
goto の後に Code Gear 名と引数を並べて、次の Code Gear の遷移を記述する。
この goto の行き先を継続と呼ぶ。
Scheme の継続と異なり CbC には呼び出し元の環境がないので、この継続は単なる行き先である。
したがってこれを軽量継続と呼ぶこともある。
軽量継続により、並列化、ループ制御、関数コールとスタックの操作を意識した最適化がソースコードレベルで行えるようにする。

\begin{lstlisting}[frame=lrbt,label=code_simple,caption={\footnotesize code segment の軽量継続}]
__code cs0(int a, int b){
  goto cs1(a+b);
}

__code cs1(int c){
  goto cs2(c);
}
\end{lstlisting}

\begin{figure}[htpb]
 \begin{center}
  \scalebox{0.55}{\includegraphics{fig/codesegment2.pdf}}
 \end{center}
 \caption{code segment の軽量継続}
 \label{fig:code_simple}
\end{figure}

もう少し複雑な CbC のプログラムの例が以下のリスト \ref{factorial} である。
これは与えられた数値の階乗を算出するプログラムである。
このコードの factorial0 という Code Gear に注目すると、条件判別を行い、その結果に応じて自分自身への再帰的な継続を行うか別の Code Gear への継続を行うかという処理を行っていることがわかる。
CbC ではこのようにしてループ処理を制御する。

\begin{lstlisting}[frame=lrbt,label=factorial,caption={\footnotesize 階乗を求める CbC プログラムの例}]
__code print_factorial(int prod)
{
  printf("factorial = %d\n",prod);
  exit(0);
}

__code factorial0(int prod, int x)
{
  if ( x >= 1) {
    goto factorial0(prod*x, x-1);
  }else{
    goto print_factorial(prod);
  }

}

__code factorial(int x)
{
  goto factorial0(1, x);
}

int main(int argc, char **argv)
{
  int i;
  i = atoi(argv[1]);

  goto factorial(i);
}
\end{lstlisting}

\section{環境付き継続}
環境付き継続は C との互換性のために必要な機能である。
CbC と C の記述を交える際、CbC の Code Gear から C の関数の呼び出しは問題なく行える。
しかし、C の関数から CbC の Code Gear へと継続する場合、呼び出し元の環境に戻るための特殊な継続が必要となる。
これを環境付き継続と呼ぶ。

環境付き継続を用いる場合、C の関数から Code Gear へ継続する際に \_\_ return、\_\_environment という変数を渡す。
\_\_return は \_\_code (*)(return\_type, void*) 型の変数で環境付き継続先が元の環境に戻る際に利用する Code Gear を表す。
\_\_environment は void** 型の変数で元の関数の環境を表す。
リスト\ref{gotoWithTheEnv}では関数 funcB から Code Gear cs に継続する際に環境付き継続を利用している。
cs は funcB から渡された Code Gear へ継続することで元の C の環境に復帰することが可能となる。
但し復帰先は \_\_return を渡した関数が終了する位置である。
このプログラムの例では、関数 funcA は戻り値として funcB の終わりにある -1 ではなく、環境付き継続によって渡される 1 を受け取る。
図\ref{fig:gotoWithTheEnv}にこの様子を表した。

\begin{lstlisting}[frame=lrbt,label=gotoWithTheEnv,caption={環境付き継続}]
__code cs(__code (*ret)(int, void*), void *env){
  /* C0 */
  goto ret(1, env);
}

int funcB(){
  /* B0 */
  goto cs(__return, __environment);
  /* B1 (never reached). */
  return -1;
}

int funcA(){
  /* A0 */
  int retval;
  retval = funcB();
  /* A1 */
  printf("retval = %d\n", retval);
  /* retval should not be -1 but be 1. */
  return 0;
}

\end{lstlisting}

\begin{figure}[htpb]
 \begin{center}
  \scalebox{0.55}{\includegraphics{fig/gotowithenv.pdf}}
 \end{center}
 \caption{環境付き継続}
 \label{fig:gotoWithTheEnv}
\end{figure}

このように、環境付き継続を用いることで C、CbC 間の処理の移動が可能になる。

%Data Gear はデータの単位であり、int や文字列などの Primitive Type を持っている。

%Code Gear は 任意の数の Input Data Gear を参照して処理を行い、Output Data Gear を出力し処理を終える。
%また、接続された Data Gear 以外には参照を行わない。

%処理やデータの構造が Code Gear、Data Gear に閉じているため、これにより実行時間、メモリ使用量などを予測可能なものにすることが可能になる。