riscv back

This commit is contained in:
Yaossg 2024-12-24 21:31:12 +08:00
parent 92f4b4f561
commit 2b9ed2e2a4
4 changed files with 58 additions and 13 deletions

View file

@ -22,13 +22,13 @@ sudo apt install gcc-12-riscv64-linux-gnu qemu-user qemu-system-misc
编译运行程序src 为本语言源代码。可以编译 demo 或 test 文件夹下的实例。 编译运行程序src 为本语言源代码。可以编译 demo 或 test 文件夹下的实例。
```sh ```sh
$ sh run.sh <src> $ bash run.sh <src>
``` ```
自举编译器,输出的文件位于 build 文件夹中。 自举编译器,输出的文件位于 build 文件夹中。
```sh ```sh
$ sh boot.sh $ bash boot.sh
``` ```
### 自举过程 ### 自举过程
@ -171,7 +171,7 @@ $ sh boot.sh
直接运行 直接运行
```sh ```sh
$ sh test.sh $ bash test.sh
``` ```
### 覆盖率 ### 覆盖率

20
boot.sh
View file

@ -1,10 +1,22 @@
if [ $(uname -m) != "riscv64" ]; then
function compile_and_run() {
riscv64-linux-gnu-gcc-12 -static $1.s -o $1.elf &&
qemu-riscv64 $1.elf < ../boot.c > $2.s
return $?
}
else
function compile_and_run() {
gcc $1.s -o $1.elf &&
./$1.elf < ../boot.c > $2.s
return $?
}
fi
mkdir -p build && cd build && mkdir -p build && cd build &&
gcc ../boot.c -o gcc.elf && gcc ../boot.c -o gcc.elf &&
./gcc.elf < ../boot.c > boot1.s && ./gcc.elf < ../boot.c > boot1.s &&
riscv64-linux-gnu-gcc-12 -static boot1.s -o boot1.elf && compile_and_run boot1 boot2 &&
qemu-riscv64 boot1.elf < ../boot.c > boot2.s && compile_and_run boot2 boot3
riscv64-linux-gnu-gcc-12 -static boot2.s -o boot2.elf &&
qemu-riscv64 boot2.elf < ../boot.c > boot3.s
cmp --silent boot1.s boot2.s && echo "boot1.s == boot2.s" || echo "boot1.s != boot2.s" cmp --silent boot1.s boot2.s && echo "boot1.s == boot2.s" || echo "boot1.s != boot2.s"
cmp --silent boot2.s boot3.s && echo "boot2.s == boot3.s" || echo "boot2.s != boot3.s" cmp --silent boot2.s boot3.s && echo "boot2.s == boot3.s" || echo "boot2.s != boot3.s"
cmp --silent boot1.s boot3.s && echo "boot1.s == boot3.s" || echo "boot1.s != boot3.s" cmp --silent boot1.s boot3.s && echo "boot1.s == boot3.s" || echo "boot1.s != boot3.s"

17
run.sh
View file

@ -1,6 +1,17 @@
if [ $(uname -m) != "riscv64" ]; then
function compile_and_run() {
riscv64-linux-gnu-gcc-12 -static $1.s -o $1.elf
qemu-riscv64 $1.elf
}
else
function compile_and_run() {
gcc $1.s -o $1.elf
./$1.elf
}
fi
gcc boot.c -o boot.elf && gcc boot.c -o boot.elf &&
./boot.elf < $1 > $1.s && ./boot.elf < $1 > $1.s &&
riscv64-linux-gnu-gcc-12 -static $1.s -o $1.elf && compile_and_run $1
qemu-riscv64 $1.elf
echo $? echo $?
rm $1.s $1.elf boot.elf 2> /dev/null rm $1.s $1.s.elf boot.elf 2> /dev/null

28
test.sh
View file

@ -1,3 +1,25 @@
if [ $(uname -m) != "riscv64" ]; then
function compile() {
riscv64-linux-gnu-gcc-12 -static $1.s -o $1.elf
}
function run_with_input() {
qemu-riscv64 $1.elf < $1.in > $1.ans
}
function run_without_input() {
qemu-riscv64 $1.elf > $1.ans
}
else
function compile() {
gcc $1.s -o $1.elf
}
function run_with_input() {
./$1.elf < $1.in > $1.ans
}
function run_without_input() {
./$1.elf > $1.ans
}
fi
cd test cd test
gcc ../boot.c -o boot.elf gcc ../boot.c -o boot.elf
all_cnt=0 all_cnt=0
@ -12,12 +34,12 @@ for D in *; do
i=$(basename $i .c) i=$(basename $i .c)
if [ -f $i.out ]; then if [ -f $i.out ]; then
../boot.elf < $i.c > $i.s && ../boot.elf < $i.c > $i.s &&
riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf compile $i
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
if [ -f $i.in ]; then if [ -f $i.in ]; then
qemu-riscv64 $i.elf < $i.in > $i.ans run_with_input $i
else else
qemu-riscv64 $i.elf > $i.ans run_without_input $i
fi fi
echo $? >> $i.ans echo $? >> $i.ans
cmp $i.out $i.ans cmp $i.out $i.ans