#!/usr/bin/env bash set -euo pipefail if [[ $# -lt 1 || $# -gt 3 ]]; then echo "usage: $0 input.sy [output_dir] [--run]" >&2 exit 1 fi input=$1 out_dir="test/test_result/ir" 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 file not found: $input" >&2 exit 1 fi compiler="" for candidate in ./build_lab2/bin/compiler ./build/bin/compiler; do if [[ -x "$candidate" ]]; then compiler="$candidate" break fi done if [[ -z "$compiler" ]]; then echo "compiler not found; try: cmake -S . -B build_lab2 && cmake --build build_lab2 -j" >&2 exit 1 fi mkdir -p "$out_dir" base=$(basename "$input") stem=${base%.sy} out_file="$out_dir/$stem.ll" stdin_file="$input_dir/$stem.in" expected_file="$input_dir/$stem.out" "$compiler" --emit-ir "$input" > "$out_file" echo "IR generated: $out_file" if [[ "$run_exec" == true ]]; then if ! command -v llc >/dev/null 2>&1; then echo "llc not found" >&2 exit 1 fi if ! command -v clang >/dev/null 2>&1; then echo "clang not found" >&2 exit 1 fi obj="$out_dir/$stem.o" exe="$out_dir/$stem" stdout_file="$out_dir/$stem.stdout" actual_file="$out_dir/$stem.actual.out" llc -opaque-pointers -filetype=obj "$out_file" -o "$obj" clang "$obj" sylib/sylib.c -o "$exe" # Optional timeout to prevent hanging test cases. # Override with RUN_TIMEOUT_SEC/PERF_TIMEOUT_SEC env vars. timeout_sec="${RUN_TIMEOUT_SEC:-60}" if [[ "$input" == *"/performance/"* || "$input" == *"/h_performance/"* ]]; then timeout_sec="${PERF_TIMEOUT_SEC:-300}" fi set +e if command -v timeout >/dev/null 2>&1; then if [[ -f "$stdin_file" ]]; then timeout "$timeout_sec" "$exe" < "$stdin_file" > "$stdout_file" else timeout "$timeout_sec" "$exe" > "$stdout_file" fi else if [[ -f "$stdin_file" ]]; then "$exe" < "$stdin_file" > "$stdout_file" else "$exe" > "$stdout_file" fi fi status=$? set -e if [[ $status -eq 124 ]]; then echo "timeout after ${timeout_sec}s: $exe" >&2 fi cat "$stdout_file" if [[ -s "$stdout_file" ]] && (( $(tail -c 1 "$stdout_file" | wc -l) == 0 )); then printf '\n' fi echo "exit code: $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 <(awk '{ sub(/\r$/, ""); print }' "$expected_file") <(awk '{ sub(/\r$/, ""); print }' "$actual_file"); then echo "matched: $expected_file" else echo "mismatch: $expected_file" >&2 echo "actual saved to: $actual_file" >&2 exit 1 fi else echo "expected output not found, skipped diff: $expected_file" fi fi