sdcc を6800向けに改造する (2)

BASICMASTERCコンパイラ

sdcc-6800の移植の続き。これはASTからIR(中間表現)を作って、そこからコードを生成するコンパイラ。効率的なコード生成が期待できる。


ある程度動くようになった

ある程度のライブラリを揃えて、コンパイルがそれなりに通るようになった。volatileはかなり手を抜いているので怪しいけど。


knight tourを動かしてみる

ソースは下記。

sdcc-6800 の char はデフォルトでは unsigned なので、ソースを一部修正。


--- 9006-knight.c.orig	2026-11-23 02:11:55.582899769 +0000
+++ 9006-knight.c	2026-11-23 01:31:16.090392143 +0000
@@ -9,8 +9,8 @@
 #define NP (8 * 8 - 2)
 
 unsigned char board[16][16];
-char dx[8] = {2, 1, -1, -2, -2, -1, 1, 2};
-char dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
+signed char dx[8] = {2, 1, -1, -2, -2, -1, 1, 2};
+signed char dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
 int count = 0;
 int solution = 0;



I/O周りをでっちあげる。


#include <stdio.h>
#include <stdlib.h>

#define SIMIF (*(volatile unsigned char *)0xfefe)

int putchar(int c)
{
    SIMIF = 'p';
    SIMIF = (unsigned char)c;
    return (unsigned char)c;
}

void putstr(const char *s)
{
    while (*s)
        putchar((unsigned char)*s++);
}

void print(int n)
{
    char buf[6];
    unsigned char count = 0;
    unsigned int value;

    if (n < 0) {
        putchar('-');
        value = 0u - (unsigned int)n;
    } else {
        value = n;
    }

    do {
        buf[count++] = '0' + value % 10;
        value /= 10;
    } while (value);

    while (count)
        putchar(buf[--count]);
}

void cpu_counter(void)
{
}

void exit(int status)
{
    putstr("\n[exit status=");
    print(status);
    putstr("]\n");

    SIMIF = 's';	// stop uCsim
    for (;;)
        ;
}


コンパイルと実行。


SDCC="../bin/sdcc"
FLAGS="-mmc6800 --stack-auto --stack-loc 0xefff"
INCLUDE="-I ../device/include"

$SDCC $FLAGS $INCLUDE -c 9006-knight.c -o knight.rel
$SDCC $FLAGS $INCLUDE -c platform.c -o platform.rel

$SDCC $FLAGS --out-fmt-ihx --nostdlib \
    -L ../device/lib/build/mc6800-stack-auto \
    knight.rel platform.rel mc6800.lib -o knight.ihx

COUNTER_ADDR=$(awk '$3 == "_cpu_counter" { print "0x" $2; exit }' knight.map)

../sim/ucsim/src/sims/m6800.src/ucsim_m6800 \
    -b \
    -t 6800 \
    -C ../support/regression/ports/mc6800/ucsim-init.cmd \
    knight.ihx <<EOF
set hw simif rom 0xfefe
break $COUNTER_ADDR
commands 1 expression /u sim_ticks; run
run
state
quit
EOF

実行結果。


9006-knight.c:17: warning 283: function declarator with no prototype
9006-knight.c:122: warning 323: Function main should be void main(void) or int main(void)
9006-knight.c:124: warning 59: function 'main' must return value
9006-knight.c:124: warning 85: in function main unreferenced function parameter : 'argc'
9006-knight.c:124: warning 85: in function main unreferenced function parameter : 'argv'
uCsim 0.9.9, Copyright (C) 1997 Daniel Drotos.
uCsim comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
set hardware uart[0] 0xf000
set hardware uart[1] 0xf008
set hardware pia[0] 0xf010
set hardware pia[1] 0xf020
Loading from knight.ihx
1880 words read from knight.ihx
set hw simif rom 0xfefe
break 0x000005FE
commands 1 expression /u sim_ticks; run
run
state
quit
Breakpoint 1 at 0x0005fe (cond="")
0x05fe F  39          RTS                                            
Simulation started, PC=0x000100
44380492
Simulation started, PC=0x0005fe

Solution 1
 1 64 49 22 37 18  9 20 
48 53 38 33 10 21 36 17 
63  2 31 50 23 34 19  8 
54 47 52 39 32 11 16 35 
43 62  3 30 51 24  7 26 
46 55 44 59 40 27 12 15 
61 42 57  4 29 14 25  6 
56 45 60 41 58  5 28 13 
count=64

[exit status=0]

Stop at 0x000619: (110) Program stopped itself
F 0x000619
Simulated 221179 ticks (2.212e-01 sec)
Host usage: 0.015223 sec, rate=14.529240
CPU state= OK PC= 0x000619 frequency= 1000000 HZ
Operation since last reset= 27247133 vclks
Inst= 11972470 Fetch= 20708125 Read= 4935815 Write= 1603193
Total time since last reset= 44.601670999351214 sec (44601671 clks)
Time in isr = 0.000000000000000 sec (0 clks) 0.00%
Time in idle= 0.000000000000000 sec (0 clks) 0.00%
Most value of stack pointer= 0x000000
Simulation: stopped
Runtime: 2.962257 sec


考察

最初の結果が出るまでが 44,380,492cyc です。 chibicc-6800(-Os) だと 45,437,558。fccだと 247,803,033。

Compiler cycles
sdcc-6800 44,380,492
sdcc-6800(+shift) 39,598,938
sdcc-6800(+peep) 37,742,938
chibicc-6800 -Os 45,437,558
chibicc-6800 -O2 37,819,735
chibicc-6800 -O3 34,104,655
Fuzix-Compiler-Kit 247,803,033

素直に移植して -Os 程度の速度になるのはすごいけど、-O2に負けてるのはなんでだろ。

…

調べてみると、board[x][y] の計算で、*16 に相当する処理をループにしていた。aslb/rolaを4回繰り返すようにしたら、10%ほど速くなった。

staの直後に同じ値をldaしているのをpeepholeで潰すとさらに速くなった。

この状況で -O2 より速い。すごいなsdcc。

コメント

タイトルとURLをコピーしました