From 2b9ed2e2a485c9f5b52c0c2cf3ba8ef517a309cf Mon Sep 17 00:00:00 2001 From: Yaossg Date: Tue, 24 Dec 2024 21:31:12 +0800 Subject: [PATCH] riscv back --- README.md | 6 +++--- boot.sh | 20 ++++++++++++++++---- run.sh | 17 ++++++++++++++--- test.sh | 28 +++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 35d031d..bcad759 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,13 @@ sudo apt install gcc-12-riscv64-linux-gnu qemu-user qemu-system-misc 编译运行程序,src 为本语言源代码。可以编译 demo 或 test 文件夹下的实例。 ```sh -$ sh run.sh +$ bash run.sh ``` 自举编译器,输出的文件位于 build 文件夹中。 ```sh -$ sh boot.sh +$ bash boot.sh ``` ### 自举过程 @@ -171,7 +171,7 @@ $ sh boot.sh 直接运行 ```sh -$ sh test.sh +$ bash test.sh ``` ### 覆盖率 diff --git a/boot.sh b/boot.sh index 1480b99..e5fe764 100644 --- a/boot.sh +++ b/boot.sh @@ -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 && gcc ../boot.c -o gcc.elf && ./gcc.elf < ../boot.c > boot1.s && -riscv64-linux-gnu-gcc-12 -static boot1.s -o boot1.elf && -qemu-riscv64 boot1.elf < ../boot.c > boot2.s && -riscv64-linux-gnu-gcc-12 -static boot2.s -o boot2.elf && -qemu-riscv64 boot2.elf < ../boot.c > boot3.s +compile_and_run boot1 boot2 && +compile_and_run boot2 boot3 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 boot1.s boot3.s && echo "boot1.s == boot3.s" || echo "boot1.s != boot3.s" diff --git a/run.sh b/run.sh index 268461c..e07d810 100644 --- a/run.sh +++ b/run.sh @@ -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 && ./boot.elf < $1 > $1.s && -riscv64-linux-gnu-gcc-12 -static $1.s -o $1.elf && -qemu-riscv64 $1.elf +compile_and_run $1 echo $? -rm $1.s $1.elf boot.elf 2> /dev/null +rm $1.s $1.s.elf boot.elf 2> /dev/null \ No newline at end of file diff --git a/test.sh b/test.sh index 3b257b5..85ce94e 100644 --- a/test.sh +++ b/test.sh @@ -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 gcc ../boot.c -o boot.elf all_cnt=0 @@ -12,12 +34,12 @@ for D in *; do i=$(basename $i .c) if [ -f $i.out ]; then ../boot.elf < $i.c > $i.s && - riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf + compile $i if [[ $? == 0 ]]; then if [ -f $i.in ]; then - qemu-riscv64 $i.elf < $i.in > $i.ans + run_with_input $i else - qemu-riscv64 $i.elf > $i.ans + run_without_input $i fi echo $? >> $i.ans cmp $i.out $i.ans