|
|
|
|
@ -0,0 +1,75 @@
|
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
#./scripts/gen_ir.sh test/test_case/simple_add.sy --run
|
|
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
if [[ $# -lt 1 || $# -gt 3 ]]; then
|
|
|
|
|
echo "用法: $0 <input.sy> [output_dir] [--run]" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
input=$1
|
|
|
|
|
out_dir="out/ir"
|
|
|
|
|
run_exec=false
|
|
|
|
|
|
|
|
|
|
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="./build/bin/compiler"
|
|
|
|
|
if [[ ! -x "$compiler" ]]; then
|
|
|
|
|
echo "未找到编译器: $compiler ,请先构建(如: mkdir -p build && cd build && cmake .. && make -j)" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
mkdir -p "$out_dir"
|
|
|
|
|
base=$(basename "$input")
|
|
|
|
|
stem=${base%.sy}
|
|
|
|
|
out_file="$out_dir/$stem.ll"
|
|
|
|
|
"$compiler" "$input" > "$out_file"
|
|
|
|
|
echo "IR 已生成: $out_file"
|
|
|
|
|
|
|
|
|
|
# 为 LLVM 使用准备纯净 IR(去掉可能的 AST 调试输出),写入同目录。
|
|
|
|
|
pure_file="$out_dir/$stem.ir.ll"
|
|
|
|
|
awk '/^define /{p=1} p' "$out_file" > "$pure_file"
|
|
|
|
|
|
|
|
|
|
if [[ ! -s "$pure_file" ]]; then
|
|
|
|
|
echo "警告: 未找到 IR 定义(文件可能只包含 AST 输出),未生成 $pure_file" >&2
|
|
|
|
|
else
|
|
|
|
|
echo "纯净 IR 已生成: $pure_file"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$run_exec" == true ]]; then
|
|
|
|
|
if ! command -v llc >/dev/null 2>&1; then
|
|
|
|
|
echo "未找到 llc,无法运行 IR。请安装 LLVM。" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
if ! command -v clang >/dev/null 2>&1; then
|
|
|
|
|
echo "未找到 clang,无法链接可执行文件。请安装 LLVM/Clang。" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
obj="$out_dir/$stem.o"
|
|
|
|
|
exe="$out_dir/$stem.exe"
|
|
|
|
|
llc -filetype=obj "$pure_file" -o "$obj"
|
|
|
|
|
clang "$obj" -o "$exe"
|
|
|
|
|
echo "运行 $exe ..."
|
|
|
|
|
set +e
|
|
|
|
|
"$exe"
|
|
|
|
|
status=$?
|
|
|
|
|
set -e
|
|
|
|
|
echo "退出码: $status"
|
|
|
|
|
fi
|