view paper/chapter4.tex @ 5:ea7938131775

script
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 09 Feb 2016 14:31:42 +0900
parents 68928e796ed8
children 3afb4bfe1100
line wrap: on
line source

\chapter{Gears OS サポート}
\ref{sec:Gears} 節で述べた Gears OS の記述を助ける構文について記す.

Gears OS のための構文のサポートには python スクリプトを用いた. 記述したプログラムをこのスクリプトに読み込ませることで CbC コンパイラでコンパイル可能な構文へと変換される. このようにすることで Gears OS のコード, CbC のコード両方のコンパイルが可能になる.
\section{meta Code Segment の接続}
Gears OS では code segment 間の遷移に meta レベルの meta code segment を挟む. その様子を表したのが図 \ref{fig:meta} である. 通常レベルの code segment は一度 meta code segment へと継続し, その後次の code segment へと継続する. このとき通常レベルの code segment からは meta code segment への継続は隠蔽され, 通常レベルの code segment に直接継続しているように見えるべきである (図 \ref{fig:meta} の破線の継続). しかし既存の CbC では meta code segment への継続はサポートしておらず,これを実現するとリスト \ref{GearsCode} のようになり, meta code segment への継続を隠蔽することは出来ない. これらをスクリプトを用いることで リスト \ref{hideMeta} のように記述できるようになる. code segment から見ると直接次の code segment へ継続しているように見えるが, スクリプトを通すことで リスト \ref{GearsCode} のコードに変換されるので, きちんと meta code segment の処理も行われる.

この変換は, meta code segment が通常レベルの code segment に継続する際に enum から成る code segment ID を利用して継続することを利用したものである.

また, 同時に context の隠蔽も行っている. context は全ての code segment, data segment を管理しており, これに通常レベルの code segment から触れるのは好ましくないためこのようにした. 

\begin{figure}[htpb]
 \begin{center}
  \scalebox{0.45}{\includegraphics{fig/meta.pdf}}
 \end{center}
 \caption{Gears OS における code segment の継続}
 \label{fig:meta}
\end{figure}

\begin{lstlisting}[frame=lrbt,label=GearsCode,caption={CbC で実現する Gears OS コード例}]
__code meta(struct Context* context, enum Code next) {
  goto (context->code[next])(context);
}

__code code1_stub(struct Context* context) {
  goto code1(context, &context->data[Allocate]->allocate);
}

__code code1(struct Context* context, struct Allocate* allocate) {
  allocate->size = sizeof(long);
  allocator(context);
  goto meta(context, Code2);
}


__code code2(struct Context* context, long* count) {
  *count = 0;
  goto meta(context, Code3);
}

__code code2_stub(struct Context* context) {
  goto code2(context, &context->data[Count]->count);
}
\end{lstlisting}

\begin{lstlisting}[frame=lrbt,label=hideMeta,caption={meta code segment への継続の隠蔽}]
__code meta(struct Context* context, enum Code next) {
  goto (context->code[next])(context);
}

__code code1_stub(struct Context* context) {
  goto code1(context, &context->data[Allocate]->allocate);
}

__code code1(struct Allocate* allocate) {
  allocate->size = sizeof(long);
  allocator();
  goto code2();
}


__code code2(long* count) {
  *count = 0;
  goto code3();
}

__code code2_stub(struct Context* context) {
  goto code2(context, &context->data[Count]->count);
}
\end{lstlisting}

\section{stub の自動生成}
もう一つの機能として stub の自動生成がある. 前節で, Gears OS では code segment は meta code segment に継続し, その後 code segment に継続すると述べたが, 正確には meta code segment から code segment に継続する際に stub という継続を挟む. stub では, code segment が必要とする data segment を context から取り出すという処理を行うもので, リスト \ref{GearsCode}, \ref{hideMeta} の code1\_stub, code2\_stub がそれにあたる. stub は code segment 毎に生成されるためこの自動生成を行うことで code segment の記述量を約半分にすることができる. meta code segment への継続, context の隠蔽, stub の生成全てを自動化した最終的なコードがリスト \ref{stubOmit} である. 

\begin{lstlisting}[frame=lrbt,label=stubOmit,caption={機械的な記述を取り除いた Gears OS のコード}]
__code meta(struct Context* context, enum Code next) {
  goto (context->code[next])(context);
}

__code code1(struct Allocate* allocate) {
  allocate->size = sizeof(long);
  allocator();
  goto code2();
}

__code code2(long* count) {
  *count = 0;
  goto code3();
}
\end{lstlisting}

改めて元のコードであるリスト \ref{GearsCode} と, スクリプトのサポートを受けるコードリスト \ref{stubOmit} を メタへの継続, stub による data segment の取り出しなど機械的な処理を記述する必要がなくなり CbC の記述が楽になっていることがわかる. 機械的な処理の自動化はコードを書く量を減らすだけでなく, 本来記述したい処理部分のプログラミングに集中することができるという利点も齎す.