2008年12月3日水曜日

GCC のサンプルバックエンド

Incremental Machine Descriptions for Spim を参考に、gcc-4.3.2 に対応させて作ってみた。
http://github.com/alohakun/gcc-sample-backend/tree/master
最小限の C コードだけに対応したバックエンド。
$ ../bin/gcc-spim.exe -v
Using built-in specs.
Target: spim
Configured with: ../gcc-4.3.2/configure --with-gmp=/usr/local
--with-mpfr=/usr/local --enable-languages=c
--prefix=/home/aloha/test/gcc --target=spim --disable-libgcc
--enable-threads --disable-nls --disable-win32-registry --disable-shared
--without-x --enable-hash-synchronization
--enable-version-specific-runtime-libs --without-included-gettext
--disable-bootstrap --disable-libssp --program-suffix=-spim
--with-as=/usr/local/bin/as --with-ld=/usr/local/bin/ld
Thread model: single
gcc version 4.3.2 (GCC)

$ cat test0.c
void f()
{
L:
goto L;
}

void g()
{
}

$ ../bin/gcc-spim.exe -S -O2 test0.c

$ cat test0.s
.text
.align 2
.globl f
f:
L2: sw $ra, 0($sp)
sw $sp, -4($sp)
sw $fp, -8($sp)
move $fp,$sp
addi $sp, $fp, -44
L3: j L3
.align 2
.globl g
g:
sw $ra, 0($sp)
sw $sp, -4($sp)
sw $fp, -8($sp)
move $fp,$sp
sw $s0, 0($fp)
sw $s1, -4($fp)
sw $s2, -8($fp)
sw $s3, -12($fp)
sw $s4, -16($fp)
sw $s5, -20($fp)
sw $s6, -24($fp)
sw $s7, -28($fp)
addi $sp, $fp, -44
lw $s0, 0($fp)
lw $s1, -4($fp)
lw $s2, -8($fp)
lw $s3, -12($fp)
lw $s4, -16($fp)
lw $s5, -20($fp)
lw $s6, -24($fp)
lw $s7, -28($fp)
move $sp,$fp
lw $fp, -8($sp)
lw $ra, 0($sp)
jr $ra

こんだけでも move や addi が生成されるので、けっこう大きい。

変更ファイル : (spim ターゲットを追加しただけ)

gcc-4.3.2/config.sub
gcc-4.3.2/gcc/config.gcc

追加ファイル :

gcc-4.3.2/gcc/config/spim/spim.md (138 行)
gcc-43.2/gcc/config/spim/spim.h (370 行)
gcc-4.3.2/gcc/config/spim/spim.c (737 行)
gcc-4.3.2/gcc/config/spim/spim-protos.h (52 行)

展開して、--target=spim を指定して、通常通りビルドすればいけるはず (prefix を適当に指定)。
libgcc と as と ld を disable にしてあるので、binutils のインストールもたぶん不要 (もちろん、-S 付けてのアセンブリコード生成までしかできない)

0 件のコメント: