From cd91cc98ed604aff309e6ba57253fbc7c145468e Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 24 Jun 2025 15:13:02 +0800 Subject: [PATCH] Created some shell scripts for testing --- test_script/clean.sh | 3 ++ test_script/exe-riscv32.sh | 49 +++++++++++++++++++++++++++++++ test_script/exe.sh | 49 +++++++++++++++++++++++++++++++ test_script/gcc-riscv32.sh | 57 +++++++++++++++++++++++++++++++++++++ test_script/ll.sh | 57 +++++++++++++++++++++++++++++++++++++ test_script/sysy-riscv32.sh | 57 +++++++++++++++++++++++++++++++++++++ test_script/sysyll.sh | 57 +++++++++++++++++++++++++++++++++++++ test_script/wrapper.sh | 3 ++ 8 files changed, 332 insertions(+) create mode 100644 test_script/clean.sh create mode 100644 test_script/exe-riscv32.sh create mode 100644 test_script/exe.sh create mode 100644 test_script/gcc-riscv32.sh create mode 100644 test_script/ll.sh create mode 100644 test_script/sysy-riscv32.sh create mode 100644 test_script/sysyll.sh create mode 100644 test_script/wrapper.sh diff --git a/test_script/clean.sh b/test_script/clean.sh new file mode 100644 index 0000000..40cd3de --- /dev/null +++ b/test_script/clean.sh @@ -0,0 +1,3 @@ +rm -rf tmp/* +rm -rf *.s +rm -rf *_riscv32 \ No newline at end of file diff --git a/test_script/exe-riscv32.sh b/test_script/exe-riscv32.sh new file mode 100644 index 0000000..3f4ce73 --- /dev/null +++ b/test_script/exe-riscv32.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# 定义输入目录 +input_dir="./tmp" + +# 获取tmp目录下的所有符合条件的可执行文件,并按前缀数字升序排序 +executable_files=$(ls "$input_dir" | grep -E '^[0-9]+_.*' | grep -E '_gcc_riscv32$|_sysyc_riscv32$' | sort -t '_' -k1,1n) + +# 用于存储前缀数字和返回值 +declare -A gcc_results +declare -A sysyc_results + +# 遍历所有符合条件的可执行文件 +for file in $executable_files; do + # 提取文件名前缀和后缀 + prefix=$(echo "$file" | cut -d '_' -f 1) + suffix=$(echo "$file" | cut -d '_' -f 2) + + # 检查是否已经处理过该前缀的两个文件 + if [[ ${gcc_results["$prefix"]} && ${sysyc_results["$prefix"]} ]]; then + continue + fi + + # 执行可执行文件并捕获返回值 + echo "Executing: $file" + qemu-riscv32 "$input_dir/$file" + ret_code=$? + + # 明确记录返回值 + echo "Return code for $file: $ret_code" + + # 根据后缀存储返回值 + if [[ "$suffix" == "gcc" ]]; then + gcc_results["$prefix"]=$ret_code + elif [[ "$suffix" == "sysyc" ]]; then + sysyc_results["$prefix"]=$ret_code + fi + + # 如果同一个前缀的两个文件都已执行,比较它们的返回值 + if [[ ${gcc_results["$prefix"]} && ${sysyc_results["$prefix"]} ]]; then + gcc_ret=${gcc_results["$prefix"]} + sysyc_ret=${sysyc_results["$prefix"]} + if [[ "$gcc_ret" -ne "$sysyc_ret" ]]; then + echo -e "\e[31mWARNING: Return codes differ for prefix $prefix: _gcc=$gcc_ret, _sysyc=$sysyc_ret\e[0m" + else + echo "Return codes match for prefix $prefix: $gcc_ret" + fi + fi +done diff --git a/test_script/exe.sh b/test_script/exe.sh new file mode 100644 index 0000000..0a03309 --- /dev/null +++ b/test_script/exe.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# 定义输入目录 +input_dir="." + +# 获取当前目录下的所有符合条件的可执行文件,并按前缀数字升序排序 +executable_files=$(ls "$input_dir" | grep -E '^[0-9]+_.*' | grep -E '_clang$|_sysyc$' | sort -t '_' -k1,1n) + +# 用于存储前缀数字和返回值 +declare -A clang_results +declare -A sysyc_results + +# 遍历所有符合条件的可执行文件 +for file in $executable_files; do + # 提取文件名前缀和后缀 + prefix=$(echo "$file" | cut -d '_' -f 1) + suffix=$(echo "$file" | cut -d '_' -f 2) + + # 检查是否已经处理过该前缀的两个文件 + if [[ ${clang_results["$prefix"]} && ${sysyc_results["$prefix"]} ]]; then + continue + fi + + # 执行可执行文件并捕获返回值 + echo "Executing: $file" + "./$file" + ret_code=$? + + # 明确记录返回值 + echo "Return code for $file: $ret_code" + + # 根据后缀存储返回值 + if [[ "$suffix" == "clang" ]]; then + clang_results["$prefix"]=$ret_code + elif [[ "$suffix" == "sysyc" ]]; then + sysyc_results["$prefix"]=$ret_code + fi + + # 如果同一个前缀的两个文件都已执行,比较它们的返回值 + if [[ ${clang_results["$prefix"]} && ${sysyc_results["$prefix"]} ]]; then + clang_ret=${clang_results["$prefix"]} + sysyc_ret=${sysyc_results["$prefix"]} + if [[ "$clang_ret" -ne "$sysyc_ret" ]]; then + echo -e "\e[31mWARNING: Return codes differ for prefix $prefix: _clang=$clang_ret, _sysyc=$sysyc_ret\e[0m" + else + echo "Return codes match for prefix $prefix: $clang_ret" + fi + fi +done \ No newline at end of file diff --git a/test_script/gcc-riscv32.sh b/test_script/gcc-riscv32.sh new file mode 100644 index 0000000..2598498 --- /dev/null +++ b/test_script/gcc-riscv32.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# 定义输入和输出路径 +input_dir="../test/" +output_dir="./tmp" + +# 默认不生成可执行文件 +generate_executable=false + +# 解析命令行参数 +while [[ "$#" -gt 0 ]]; do + case $1 in + --executable|-e) + generate_executable=true + shift + ;; + *) + echo "Unknown parameter: $1" + exit 1 + ;; + esac +done + +# 确保输出目录存在 +mkdir -p "$output_dir" + +# 遍历输入路径中的所有 .sy 文件 +for sy_file in "$input_dir"*.sy; do + # 获取文件名(不带路径和扩展名) + base_name=$(basename "$sy_file" .sy) + + # 定义输出文件路径 + output_file="${output_dir}/${base_name}_gcc_riscv32.s" + + # 使用 gcc 编译 .sy 文件为 .ll 文件 + riscv32-unknown-elf-gcc -x c -S "$sy_file" -o "$output_file" + + # 检查是否成功 + if [ $? -eq 0 ]; then + echo "Compiled $sy_file -> $output_file" + else + echo "Failed to compile $sy_file" + continue + fi + + # 如果指定了 --executable 或 -e 参数,则进一步编译为可执行文件 + if $generate_executable; then + executable_file="${output_dir}/${base_name}_gcc_riscv32" + riscv32-unknown-elf-gcc "$output_file" -o "$executable_file" + + if [ $? -eq 0 ]; then + echo "Generated executable: $executable_file" + else + echo "Failed to generate executable from $output_file" + fi + fi +done diff --git a/test_script/ll.sh b/test_script/ll.sh new file mode 100644 index 0000000..a64b92d --- /dev/null +++ b/test_script/ll.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# 定义输入和输出路径 +input_dir="../test/" +output_dir="./" + +# 默认不生成可执行文件 +generate_executable=false + +# 解析命令行参数 +while [[ "$#" -gt 0 ]]; do + case $1 in + --executable|-e) + generate_executable=true + shift + ;; + *) + echo "Unknown parameter: $1" + exit 1 + ;; + esac +done + +# 确保输出目录存在 +mkdir -p "$output_dir" + +# 遍历输入路径中的所有 .sy 文件 +for sy_file in "$input_dir"*.sy; do + # 获取文件名(不带路径和扩展名) + base_name=$(basename "$sy_file" .sy) + + # 定义输出文件路径 + output_file="${base_name}_clang.ll" + + # 使用 clang 编译 .sy 文件为 .ll 文件 + clang -x c -S -emit-llvm "$sy_file" -o "$output_file" + + # 检查是否成功 + if [ $? -eq 0 ]; then + echo "Compiled $sy_file -> $output_file" + else + echo "Failed to compile $sy_file" + continue + fi + + # 如果指定了 --executable 或 -e 参数,则进一步编译为可执行文件 + if $generate_executable; then + executable_file="${base_name}_clang" + clang "$output_file" -o "$executable_file" + + if [ $? -eq 0 ]; then + echo "Generated executable: $executable_file" + else + echo "Failed to generate executable from $output_file" + fi + fi +done \ No newline at end of file diff --git a/test_script/sysy-riscv32.sh b/test_script/sysy-riscv32.sh new file mode 100644 index 0000000..e2f236e --- /dev/null +++ b/test_script/sysy-riscv32.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# 定义输入和输出路径 +input_dir="../test/" +output_dir="./tmp" + +# 默认不生成可执行文件 +generate_executable=false + +# 解析命令行参数 +while [[ "$#" -gt 0 ]]; do + case $1 in + --executable|-e) + generate_executable=true + shift + ;; + *) + echo "Unknown parameter: $1" + exit 1 + ;; + esac +done + +# 确保输出目录存在 +mkdir -p "$output_dir" + +# 遍历输入路径中的所有 .sy 文件 +for sy_file in "$input_dir"*.sy; do + # 获取文件名(不带路径和扩展名) + base_name=$(basename "$sy_file" .sy) + + # 定义输出文件路径 + output_file="${output_dir}/${base_name}_sysyc_riscv32.s" + + # 使用 sysyc 编译 .sy 文件为 .s 文件 + ../build/bin/sysyc -s asm "$sy_file" > "$output_file" + + # 检查是否成功 + if [ $? -eq 0 ]; then + echo "Compiled $sy_file -> $output_file" + else + echo "Failed to compile $sy_file" + continue + fi + + # 如果指定了 --executable 或 -e 参数,则进一步编译为可执行文件 + if $generate_executable; then + executable_file="${output_dir}/${base_name}_sysyc_riscv32" + riscv32-unknown-elf-gcc "$output_file" -o "$executable_file" + + if [ $? -eq 0 ]; then + echo "Generated executable: $executable_file" + else + echo "Failed to generate executable from $output_file" + fi + fi +done diff --git a/test_script/sysyll.sh b/test_script/sysyll.sh new file mode 100644 index 0000000..4b27256 --- /dev/null +++ b/test_script/sysyll.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# 定义输入和输出路径 +input_dir="../test/" +output_dir="./" + +# 默认不生成可执行文件 +generate_executable=false + +# 解析命令行参数 +while [[ "$#" -gt 0 ]]; do + case $1 in + --executable|-e) + generate_executable=true + shift + ;; + *) + echo "Unknown parameter: $1" + exit 1 + ;; + esac +done + +# 确保输出目录存在 +mkdir -p "$output_dir" + +# 遍历输入路径中的所有 .sy 文件 +for sy_file in "$input_dir"*.sy; do + # 获取文件名(不带路径和扩展名) + base_name=$(basename "$sy_file" .sy) + + # 定义输出文件路径 + output_file="${base_name}_sysyc.ll" + + # 使用 sysyc 编译 .sy 文件为 .ll 文件 + ../build/bin/sysyc -s llvmir "$sy_file" > "$output_file" + + # 检查是否成功 + if [ $? -eq 0 ]; then + echo "Compiled $sy_file -> $output_file" + else + echo "Failed to compile $sy_file" + continue + fi + + # 如果指定了 --executable 或 -e 参数,则进一步编译为可执行文件 + if $generate_executable; then + executable_file="${base_name}_sysyc" + clang "$output_file" -o "$executable_file" + + if [ $? -eq 0 ]; then + echo "Generated executable: $executable_file" + else + echo "Failed to generate executable from $output_file" + fi + fi +done \ No newline at end of file diff --git a/test_script/wrapper.sh b/test_script/wrapper.sh new file mode 100644 index 0000000..b00583a --- /dev/null +++ b/test_script/wrapper.sh @@ -0,0 +1,3 @@ +sh ./gcc-riscv32.sh -e +sh ./sysy-riscv32.sh -e +sh ./exe-riscv32.sh \ No newline at end of file