16.1 Using the Cross Compiler

クロス・コンパイラは、 Forth そっくりの言語を使用しますが、 Forth ではありません。 主な違いは、 Forth コードは定義後に実行できるのに対し、 cross によってコンパイルされたコードは通常は実行できないことです。 これは、 コンパイルしているコードは通常、 コンパイルしているコンピュータとは異なるコンピュータ用であるためです。

Makefile はすでにセットアップされており、 簡単な make コマンドで新しいアーキテクチャ用のカーネルを作成できるようになります。 GCC でコンパイルされた仮想マシンを使用する汎用カーネルは、 make を使用した通常のビルド・プロセスで作成されます。 たとえば、 8086 プロセッサ用の埋め込み Gforth 実行可能ファイル(embedded Gforth executable)(DOS マシン上で実行)を作成するには、 以下のようにタイプします

make kernl-8086.fi

これにより、 arch/8086 ディレクトリのマシンの説明(machine description)が使用されて、 新しいカーネルが作成されます。 マシン・ファイルは以下のようになります:

\ Parameter for target systems                         06oct92py

    4 Constant cell             \ cell size in bytes
    2 Constant cell<<           \ cell shift to bytes
    5 Constant cell>bit         \ cell shift to bits
    8 Constant bits/char        \ bits per character
    8 Constant bits/byte        \ bits per byte [default: 8]
    8 Constant float            \ bytes per float
    8 Constant /maxalign        \ maximum alignment in bytes
false Constant bigendian        \ byte order
( true=big, false=little )

include machpc.fs               \ feature list

この部分はクロス・コンパイラにとって必須であり、 機能リスト(feature list)は、 ターゲットがこれらの機能をサポートしているかどうかに応じて、 いくつかの機能を条件付きでコンパイルしたりしなかったりするためにカーネルによって使用される。

あなた独自のプリミティブを定義する場合、 またはアセンブラーを使用する場合、 またはブート・プロセスを機能させるために特別な非標準の準備が必要な場合は、 オプションの機能(feature)がいくつかあります。 asm-include にはアセンブラーが含まれ、 prims-include にはプリミティブが含まれ、 >boot はブートの準備をします。

: asm-include    ." Include assembler" cr
  s" arch/8086/asm.fs" included ;

: prims-include  ." Include primitives" cr
  s" arch/8086/prim.fs" included ;

: >boot          ." Prepare booting" cr
  s" ' boot >body into-forth 1+ !" evaluate ;

これらのワードは、 ファイル kernel/main.fs をクロス・コンパイルする時に一種のマクロとして使用されます。 これらのマクロを使用する代わりに、 新しいカーネル・プロジェクト・ファイルを作成することも可能ですが、 より複雑になります。

kernel/main.fs はスタック上にマシン記述ファイル名(machine description file name)を期待します。 クロス・コンパイラ自体(cross.fs)は、 mach-file がカウンタ付き文字列をスタックに残すか、 または machine-file がファイル名のアドレスとカウントのペアをスタックに残すかのいずれかを想定します。

機能リスト(feature list)は通常​​ SetValue を使用して制御されますが、 複数のプロジェクトで使用される汎用ファイルは代わりに DefaultValue を使用できます。 どちらのワードも、 値が定義されていない場合は Value のように動作しますが、 値が定義されている場合は SetValueto のように動作し、 値が定義されている場合は DefaultValue は何も設定しません。

\ generic mach file for pc gforth                       03sep97jaw

true DefaultValue NIL  \ relocating

>ENVIRON

true DefaultValue file          \ controls the presence of the
                                \ file access wordset
true DefaultValue OS            \ flag to indicate a operating system

true DefaultValue prims         \ true: primitives are c-code

true DefaultValue floating      \ floating point wordset is present

true DefaultValue glocals       \ gforth locals are present
                                \ will be loaded
true DefaultValue dcomps        \ double number comparisons

true DefaultValue hash          \ hashing primitives are loaded/present

true DefaultValue xconds        \ used together with glocals,
                                \ special conditionals supporting gforths'
                                \ local variables
true DefaultValue header        \ save a header information

true DefaultValue backtrace     \ enables backtrace code

false DefaultValue ec
false DefaultValue crlf

cell 2 = [IF] &32 [ELSE] &256 [THEN] KB DefaultValue kernel-size

&16 KB          DefaultValue stack-size
&15 KB &512 +   DefaultValue fstack-size
&15 KB          DefaultValue rstack-size
&14 KB &512 +   DefaultValue lstack-size