forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.1 KiB
101 lines
2.1 KiB
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
PROJECT_ROOT=$(cd "$(dirname "$0")/.." ; pwd)
|
|
|
|
if [[ $# -lt 1 || $# -gt 3 ]]; then
|
|
echo "用法: $0 <input.sy> [output_dir] [--run]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
input=$1
|
|
out_dir="test/test_result/mir"
|
|
run_exec=false
|
|
input_dir=$(dirname "$input")
|
|
|
|
shift
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--run)
|
|
run_exec=true
|
|
;;
|
|
*)
|
|
out_dir="$1"
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [[ ! -f "$input" ]]; then
|
|
echo "输入文件不存在: $input" >&2
|
|
exit 1
|
|
fi
|
|
|
|
compiler="$PROJECT_ROOT/build/bin/compiler"
|
|
if [[ ! -x "$compiler" ]]; then
|
|
echo "未找到编译器: $compiler ,请先构建。" >&2
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p "$out_dir"
|
|
base=$(basename "$input")
|
|
stem=${base%.sy}
|
|
mir_file="$out_dir/$stem.mir"
|
|
asm_file="$out_dir/$stem.s"
|
|
exe="$out_dir/$stem"
|
|
stdin_file="$input_dir/$stem.in"
|
|
expected_file="$input_dir/$stem.out"
|
|
|
|
# 生成 MIR
|
|
"$compiler" --emit-mir "$input" > "$mir_file"
|
|
echo "MIR 已生成: $mir_file"
|
|
|
|
# 生成汇编
|
|
"$compiler" --emit-asm "$input" > "$asm_file"
|
|
echo "汇编已生成: $asm_file"
|
|
|
|
if [[ "$run_exec" == true ]]; then
|
|
if ! command -v riscv64-linux-gnu-gcc >/dev/null 2>&1; then
|
|
echo "未找到 riscv64-linux-gnu-gcc" >&2
|
|
exit 1
|
|
fi
|
|
if ! command -v qemu-riscv64 >/dev/null 2>&1; then
|
|
echo "未找到 qemu-riscv64" >&2
|
|
exit 1
|
|
fi
|
|
|
|
riscv64-linux-gnu-gcc -static "$asm_file" -o "$exe" -no-pie
|
|
|
|
stdout_file="$out_dir/$stem.stdout"
|
|
actual_file="$out_dir/$stem.actual.out"
|
|
echo "运行 $exe ..."
|
|
set +e
|
|
if [[ -f "$stdin_file" ]]; then
|
|
qemu-riscv64 "$exe" < "$stdin_file" > "$stdout_file"
|
|
else
|
|
qemu-riscv64 "$exe" > "$stdout_file"
|
|
fi
|
|
status=$?
|
|
set -e
|
|
cat "$stdout_file"
|
|
echo "退出码: $status"
|
|
{
|
|
cat "$stdout_file"
|
|
if [[ -s "$stdout_file" ]] && (( $(tail -c 1 "$stdout_file" | wc -l) == 0 )); then
|
|
printf '\n'
|
|
fi
|
|
printf '%s\n' "$status"
|
|
} > "$actual_file"
|
|
|
|
if [[ -f "$expected_file" ]]; then
|
|
if diff -u "$expected_file" "$actual_file"; then
|
|
echo "输出匹配: $expected_file"
|
|
else
|
|
echo "输出不匹配: $expected_file" >&2
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "未找到预期输出文件,跳过比对: $expected_file"
|
|
fi
|
|
fi |