Skip to content

Commit 4c60446

Browse files
committed
增加集成测试脚本
1 parent 899717e commit 4c60446

File tree

5 files changed

+404
-3
lines changed

5 files changed

+404
-3
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ out/
4141

4242
.vscode/
4343

44-
CMakeFiles
45-
46-
test/tests
44+
.llvm_cache/
45+
.test_cache/

scripts/build.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This script is used to build tolangc compiler with options.
2+
3+
# Usage: ./build.sh [llvm|pcode]
4+
5+
# Check if the number of arguments is correct
6+
if [ "$#" -ne 1 ]; then
7+
echo "Usage: ./build.sh [llvm|pcode]"
8+
exit 1
9+
fi
10+
11+
# Check if the argument is valid
12+
if [ "$1" != "llvm" ] && [ "$1" != "pcode" ]; then
13+
echo "Invalid argument! Use [llvm|pcode]."
14+
exit 1
15+
fi
16+
17+
backend=$1
18+
19+
# Check required tools (cmake, make, clang)
20+
if ! command -v cmake &>/dev/null; then
21+
echo "Command cmake not found! Please install CMake."
22+
exit 1
23+
fi
24+
if ! command -v make &>/dev/null; then
25+
echo "Command make not found! Please install Make."
26+
exit 1
27+
fi
28+
if ! command -v clang &>/dev/null; then
29+
echo "Command clang not found! Please install Clang."
30+
exit 1
31+
fi
32+
33+
# Create build directory if it doesn't exist
34+
if [ ! -d build ]; then
35+
mkdir build
36+
fi
37+
38+
# Change to build directory
39+
cd build
40+
41+
# Build the compiler
42+
opt=""
43+
if [ "$backend" = "pcode" ]; then
44+
opt="-DPCODE_BACKEND=ON"
45+
fi
46+
47+
cmake $opt ..
48+
49+
make

scripts/llvm_run.sh

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This script is used to run LLVM IR generated by the compiler
2+
3+
# Usage: ./llvm_run.sh <file.ll>
4+
5+
# Check if the number of arguments is correct
6+
if [ "$#" -ne 1 ]; then
7+
echo "Usage: ./llvm_run.sh <file.ll>"
8+
exit 1
9+
fi
10+
11+
ir_file=$1
12+
13+
# Check if the file exists
14+
if [ ! -f $ir_file ]; then
15+
echo "File $ir_file not found!"
16+
exit 1
17+
fi
18+
19+
# Check required tools
20+
if ! command -v lli &>/dev/null; then
21+
echo "Command lli not found! Please install LLVM."
22+
exit 1
23+
fi
24+
25+
CACHE_DIR=".llvm_cache"
26+
# Create cache directory if it doesn't exist
27+
if [ ! -d $CACHE_DIR ]; then
28+
mkdir $CACHE_DIR
29+
fi
30+
31+
# Check if lib.ll exists
32+
if [ ! -f $CACHE_DIR/lib.ll ]; then
33+
echo "Generating lib.ll..."
34+
35+
# Generate lib.c
36+
cat >$CACHE_DIR/lib.c <<EOF
37+
#include <stdio.h>
38+
39+
float get() {
40+
float ret;
41+
scanf("%f", &ret);
42+
return ret;
43+
}
44+
45+
void put(float val) { printf("%f\n", val); }
46+
EOF
47+
48+
# Compile lib.c to lib.ll
49+
clang -S -emit-llvm $CACHE_DIR/lib.c -o $CACHE_DIR/lib.ll -O0
50+
fi
51+
52+
# Link the IR file with lib.ll
53+
llvm-link $ir_file $CACHE_DIR/lib.ll -S -o $CACHE_DIR/out.ll
54+
55+
# Run the linked IR file
56+
lli $CACHE_DIR/out.ll
57+
58+
# Remove the linked IR file
59+
rm $CACHE_DIR/out.ll

scripts/mips_run.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This script is used to run MIPS assembly code generated by the compiler, using the MARS simulator.
2+
3+
# Usage: ./mips_run.sh <file.s>
4+
5+
# Check if the number of arguments is correct
6+
if [ "$#" -ne 1 ]; then
7+
echo "Usage: ./mips_run.sh <file.s>"
8+
exit 1
9+
fi
10+
11+
asm_file=$1
12+
13+
# Check if the file exists
14+
if [ ! -f $asm_file ]; then
15+
echo "File $asm_file not found!"
16+
exit 1
17+
fi
18+
19+
# Check required tools
20+
if ! command -v java &>/dev/null; then
21+
echo "Command java not found! Please install Java."
22+
exit 1
23+
fi
24+
25+
# Check if MARS_PATH is set
26+
if [ -z "$MARS_PATH" ]; then
27+
echo "MARS_PATH is not set! Please set it to the MARS simulator path."
28+
exit 1
29+
fi
30+
31+
# Check if MARS simulator exists
32+
if [ ! -f "$MARS_PATH" ]; then
33+
echo "MARS simulator not found in $MARS_PATH! Please set MARS_PATH to the MARS simulator path."
34+
exit 1
35+
fi
36+
37+
# Run the MARS simulator with the assembly file
38+
java -jar "$MARS_PATH" nc $asm_file

0 commit comments

Comments
 (0)