forked from NUDT-compiler/nudt-compiler-cpp
fix(mir):通过所有的功能测试样例 #11
Open
pfwvrotsf
wants to merge 6 commits from pfwvrotsf/nudt-compiler-cpp:develop into develop
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,272 @@
|
||||
@MAX = global i32 1000000000
|
||||
@TWO = global i32 2
|
||||
@THREE = global i32 3
|
||||
@FIVE = global i32 5
|
||||
|
||||
declare void @putch(i32)
|
||||
declare void @memset(i32*, i32, i32)
|
||||
declare i32 @getfarray(float*)
|
||||
declare float @getfloat()
|
||||
declare void @putfloat(float)
|
||||
declare void @putint(i32)
|
||||
declare void @putfarray(i32, float*)
|
||||
|
||||
define float @float_abs(float %x) {
|
||||
entry:
|
||||
%0 = alloca float
|
||||
store float %x, float* %0
|
||||
%2 = load float, float* %0
|
||||
%3 = sitofp i32 0 to float
|
||||
%4 = fcmp olt float %2, %3
|
||||
br i1 %4, label %L0.if.then, label %L1.if.end
|
||||
L0.if.then:
|
||||
%6 = load float, float* %0
|
||||
%7 = fsub float 0x0, %6
|
||||
ret float %7
|
||||
L1.if.end:
|
||||
%9 = load float, float* %0
|
||||
ret float %9
|
||||
}
|
||||
|
||||
define float @circle_area(i32 %radius) {
|
||||
entry:
|
||||
%0 = alloca i32
|
||||
store i32 %radius, i32* %0
|
||||
%2 = load i32, i32* %0
|
||||
%3 = sitofp i32 %2 to float
|
||||
%4 = fmul float 0x400921FB60000000, %3
|
||||
%5 = load i32, i32* %0
|
||||
%6 = sitofp i32 %5 to float
|
||||
%7 = fmul float %4, %6
|
||||
%8 = load i32, i32* %0
|
||||
%9 = load i32, i32* %0
|
||||
%10 = mul i32 %8, %9
|
||||
%11 = sitofp i32 %10 to float
|
||||
%12 = fmul float %11, 0x400921FB60000000
|
||||
%13 = fadd float %7, %12
|
||||
%14 = sitofp i32 2 to float
|
||||
%15 = fdiv float %13, %14
|
||||
ret float %15
|
||||
}
|
||||
|
||||
define i32 @float_eq(float %a, float %b) {
|
||||
entry:
|
||||
%0 = alloca float
|
||||
%1 = alloca float
|
||||
store float %a, float* %0
|
||||
store float %b, float* %1
|
||||
%4 = load float, float* %0
|
||||
%5 = load float, float* %1
|
||||
%6 = fsub float %4, %5
|
||||
%7 = call float @float_abs(float %6)
|
||||
%8 = fcmp olt float %7, 0x3EB0C6F7A0000000
|
||||
br i1 %8, label %L2.if.then, label %L3.if.else
|
||||
L2.if.then:
|
||||
%10 = sitofp i32 1 to float
|
||||
%11 = fmul float %10, 0x4000000000000000
|
||||
%12 = sitofp i32 2 to float
|
||||
%13 = fdiv float %11, %12
|
||||
%14 = fptosi float %13 to i32
|
||||
ret i32 %14
|
||||
L3.if.else:
|
||||
ret i32 0
|
||||
L4.if.end:
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
define void @error() {
|
||||
entry:
|
||||
call void @putch(i32 101)
|
||||
call void @putch(i32 114)
|
||||
call void @putch(i32 114)
|
||||
call void @putch(i32 111)
|
||||
call void @putch(i32 114)
|
||||
call void @putch(i32 10)
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @ok() {
|
||||
entry:
|
||||
call void @putch(i32 111)
|
||||
call void @putch(i32 107)
|
||||
call void @putch(i32 10)
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @assert(i32 %cond) {
|
||||
entry:
|
||||
%0 = alloca i32
|
||||
store i32 %cond, i32* %0
|
||||
%2 = load i32, i32* %0
|
||||
%3 = icmp eq i32 %2, 0
|
||||
%4 = zext i1 %3 to i32
|
||||
%5 = icmp ne i32 %4, 0
|
||||
br i1 %5, label %L5.if.then, label %L6.if.else
|
||||
L5.if.then:
|
||||
call void @error()
|
||||
br label %L7.if.end
|
||||
L6.if.else:
|
||||
call void @ok()
|
||||
br label %L7.if.end
|
||||
L7.if.end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @assert_not(i32 %cond) {
|
||||
entry:
|
||||
%0 = alloca i32
|
||||
store i32 %cond, i32* %0
|
||||
%2 = load i32, i32* %0
|
||||
%3 = icmp ne i32 %2, 0
|
||||
br i1 %3, label %L8.if.then, label %L9.if.else
|
||||
L8.if.then:
|
||||
call void @error()
|
||||
br label %L10.if.end
|
||||
L9.if.else:
|
||||
call void @ok()
|
||||
br label %L10.if.end
|
||||
L10.if.end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i32 @main() {
|
||||
entry:
|
||||
%0 = alloca i32
|
||||
%1 = alloca i32
|
||||
%2 = alloca i32
|
||||
%3 = alloca i32
|
||||
%4 = alloca float, i32 10
|
||||
%5 = alloca i32
|
||||
%6 = alloca float
|
||||
%7 = alloca float
|
||||
%8 = alloca float
|
||||
%9 = call i32 @float_eq(float 0x3FB4000000000000, float 0xC0E01D0000000000)
|
||||
call void @assert_not(i32 %9)
|
||||
%11 = call i32 @float_eq(float 0x4057C21FC0000000, float 0x4041475CE0000000)
|
||||
call void @assert_not(i32 %11)
|
||||
%13 = call i32 @float_eq(float 0x4041475CE0000000, float 0x4041475CE0000000)
|
||||
call void @assert(i32 %13)
|
||||
%15 = fptosi float 0x4016000000000000 to i32
|
||||
%16 = call float @circle_area(i32 %15)
|
||||
%17 = load i32, i32* @FIVE
|
||||
%18 = call float @circle_area(i32 %17)
|
||||
%19 = call i32 @float_eq(float %16, float %18)
|
||||
call void @assert(i32 %19)
|
||||
%21 = call i32 @float_eq(float 0x406D200000000000, float 0x40AFFE0000000000)
|
||||
call void @assert_not(i32 %21)
|
||||
%23 = fcmp one float 0x3FF8000000000000, 0x0
|
||||
br i1 %23, label %L11.if.then, label %L12.if.end
|
||||
L11.if.then:
|
||||
call void @ok()
|
||||
br label %L12.if.end
|
||||
L12.if.end:
|
||||
%27 = fcmp oeq float 0x400A666660000000, 0x0
|
||||
%28 = zext i1 %27 to i32
|
||||
%29 = icmp eq i32 %28, 0
|
||||
%30 = zext i1 %29 to i32
|
||||
%31 = icmp ne i32 %30, 0
|
||||
br i1 %31, label %L13.if.then, label %L14.if.end
|
||||
L13.if.then:
|
||||
call void @ok()
|
||||
br label %L14.if.end
|
||||
L14.if.end:
|
||||
%35 = fcmp one float 0x0, 0x0
|
||||
%36 = zext i1 %35 to i32
|
||||
store i32 %36, i32* %0
|
||||
br i1 %35, label %L15.and.rhs, label %L16.and.end
|
||||
L15.and.rhs:
|
||||
%39 = icmp ne i32 3, 0
|
||||
%40 = zext i1 %39 to i32
|
||||
store i32 %40, i32* %0
|
||||
br label %L16.and.end
|
||||
L16.and.end:
|
||||
%43 = load i32, i32* %0
|
||||
%44 = icmp ne i32 %43, 0
|
||||
br i1 %44, label %L17.if.then, label %L18.if.end
|
||||
L17.if.then:
|
||||
call void @error()
|
||||
br label %L18.if.end
|
||||
L18.if.end:
|
||||
%48 = icmp ne i32 0, 0
|
||||
%49 = zext i1 %48 to i32
|
||||
store i32 %49, i32* %1
|
||||
br i1 %48, label %L20.or.end, label %L19.or.rhs
|
||||
L19.or.rhs:
|
||||
%52 = fcmp one float 0x3FD3333340000000, 0x0
|
||||
%53 = zext i1 %52 to i32
|
||||
store i32 %53, i32* %1
|
||||
br label %L20.or.end
|
||||
L20.or.end:
|
||||
%56 = load i32, i32* %1
|
||||
%57 = icmp ne i32 %56, 0
|
||||
br i1 %57, label %L21.if.then, label %L22.if.end
|
||||
L21.if.then:
|
||||
call void @ok()
|
||||
br label %L22.if.end
|
||||
L22.if.end:
|
||||
store i32 1, i32* %2
|
||||
store i32 0, i32* %3
|
||||
call void @memset(float* %4, i32 0, i32 40)
|
||||
%64 = getelementptr float, float* %4, i32 0
|
||||
store float 0x3FF0000000000000, float* %64
|
||||
%66 = getelementptr float, float* %4, i32 1
|
||||
store i32 2, float* %66
|
||||
%68 = getelementptr float, float* %4, i32 0
|
||||
%69 = call i32 @getfarray(float* %68)
|
||||
store i32 %69, i32* %5
|
||||
br label %L23.while.cond
|
||||
L23.while.cond:
|
||||
%72 = load i32, i32* %2
|
||||
%73 = load i32, i32* @MAX
|
||||
%74 = icmp slt i32 %72, %73
|
||||
br i1 %74, label %L24.while.body, label %L25.while.end
|
||||
L24.while.body:
|
||||
%76 = call float @getfloat()
|
||||
store float %76, float* %6
|
||||
%78 = load float, float* %6
|
||||
%79 = fmul float 0x400921FB60000000, %78
|
||||
%80 = load float, float* %6
|
||||
%81 = fmul float %79, %80
|
||||
store float %81, float* %7
|
||||
%83 = load float, float* %6
|
||||
%84 = fptosi float %83 to i32
|
||||
%85 = call float @circle_area(i32 %84)
|
||||
store float %85, float* %8
|
||||
%87 = load i32, i32* %3
|
||||
%88 = getelementptr float, float* %4, i32 %87
|
||||
%89 = load float, float* %88
|
||||
%90 = load float, float* %6
|
||||
%91 = fadd float %89, %90
|
||||
%92 = load i32, i32* %3
|
||||
%93 = getelementptr float, float* %4, i32 %92
|
||||
store float %91, float* %93
|
||||
%95 = load float, float* %7
|
||||
call void @putfloat(float %95)
|
||||
call void @putch(i32 32)
|
||||
%98 = load float, float* %8
|
||||
%99 = fptosi float %98 to i32
|
||||
call void @putint(i32 %99)
|
||||
call void @putch(i32 10)
|
||||
%102 = load i32, i32* %2
|
||||
%103 = fsub float 0x0, 0x4024000000000000
|
||||
%104 = fsub float 0x0, %103
|
||||
%105 = sitofp i32 %102 to float
|
||||
%106 = fmul float %105, %104
|
||||
%107 = fptosi float %106 to i32
|
||||
store i32 %107, i32* %2
|
||||
%109 = load i32, i32* %3
|
||||
%110 = add i32 %109, 1
|
||||
store i32 %110, i32* %3
|
||||
br label %L23.while.cond
|
||||
L25.while.end:
|
||||
%113 = load i32, i32* %5
|
||||
%114 = getelementptr float, float* %4, i32 0
|
||||
call void @putfarray(i32 %113, float* %114)
|
||||
%116 = srem i32 0, 256
|
||||
%117 = add i32 %116, 256
|
||||
%118 = srem i32 %117, 256
|
||||
call void @putint(i32 %118)
|
||||
call void @putch(i32 10)
|
||||
ret i32 0
|
||||
}
|
||||
|
||||
@ -1,65 +0,0 @@
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
PROJECT_ROOT=$(cd "$(dirname "$0")/.." ; pwd)
|
||||
COMPILER="$PROJECT_ROOT/build/bin/compiler"
|
||||
TEST_DIR="$PROJECT_ROOT/test/test_case/basic"
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
NC='\033[0m'
|
||||
|
||||
if [ ! -f "$COMPILER" ]; then
|
||||
echo "错误: 编译器不存在: $COMPILER"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=========================================="
|
||||
echo "RISC-V 浮点转换测试"
|
||||
echo "=========================================="
|
||||
|
||||
TESTS="
|
||||
float_conv:3
|
||||
float_add:13
|
||||
float_mul:30
|
||||
"
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
for test in $TESTS; do
|
||||
name=$(echo $test | cut -d: -f1)
|
||||
expected=$(echo $test | cut -d: -f2)
|
||||
|
||||
echo -n "测试 $name (期望 $expected) ... "
|
||||
|
||||
"$COMPILER" "$TEST_DIR/$name.sy" --emit-asm > /tmp/test_$name.s 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}失败 (汇编错误)${NC}"
|
||||
cat /tmp/test_$name.s | head -3
|
||||
FAIL=$((FAIL + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
riscv64-linux-gnu-gcc -static /tmp/test_$name.s -o /tmp/test_$name -no-pie 2>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}失败 (链接错误)${NC}"
|
||||
FAIL=$((FAIL + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
qemu-riscv64 /tmp/test_$name > /dev/null 2>&1
|
||||
exit_code=$?
|
||||
|
||||
if [ $exit_code -eq $expected ]; then
|
||||
echo -e "${GREEN}通过${NC}"
|
||||
PASS=$((PASS + 1))
|
||||
else
|
||||
echo -e "${RED}失败 (实际 $exit_code)${NC}"
|
||||
FAIL=$((FAIL + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "=========================================="
|
||||
echo -e "测试结果: ${GREEN}通过 $PASS${NC} / ${RED}失败 $FAIL${NC}"
|
||||
echo "=========================================="
|
||||
@ -1,85 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 获取项目根目录
|
||||
PROJECT_ROOT=$(cd "$(dirname "$0")/.." ; pwd)
|
||||
COMPILER="$PROJECT_ROOT/build/bin/compiler"
|
||||
TEST_DIR="$PROJECT_ROOT/test/test_case/basic"
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
NC='\033[0m'
|
||||
|
||||
# 检查编译器
|
||||
if [ ! -f "$COMPILER" ]; then
|
||||
echo "错误: 编译器不存在: $COMPILER"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查工具链
|
||||
if ! command -v riscv64-linux-gnu-gcc >/dev/null 2>&1; then
|
||||
echo "错误: 未找到 riscv64-linux-gnu-gcc"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v qemu-riscv64 >/dev/null 2>&1; then
|
||||
echo "错误: 未找到 qemu-riscv64"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=========================================="
|
||||
echo "RISC-V 基础功能测试"
|
||||
echo "=========================================="
|
||||
|
||||
# 定义测试用例
|
||||
TESTS="arith:50 add:30 sub:7 mul:50 div:25 mod:2 var:43"
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
for test in $TESTS; do
|
||||
name=$(echo $test | cut -d: -f1)
|
||||
expected=$(echo $test | cut -d: -f2)
|
||||
|
||||
echo -n "测试 $name (期望 $expected) ... "
|
||||
|
||||
# 生成汇编
|
||||
"$COMPILER" "$TEST_DIR/$name.sy" --emit-asm > /tmp/test_$name.s 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}失败 (汇编错误)${NC}"
|
||||
FAIL=$((FAIL + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
# 链接
|
||||
riscv64-linux-gnu-gcc -static /tmp/test_$name.s -o /tmp/test_$name -no-pie 2>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}失败 (链接错误)${NC}"
|
||||
FAIL=$((FAIL + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
# 运行
|
||||
qemu-riscv64 /tmp/test_$name > /dev/null 2>&1
|
||||
exit_code=$?
|
||||
|
||||
if [ $exit_code -eq $expected ]; then
|
||||
echo -e "${GREEN}通过${NC}"
|
||||
PASS=$((PASS + 1))
|
||||
else
|
||||
echo -e "${RED}失败 (实际 $exit_code)${NC}"
|
||||
FAIL=$((FAIL + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "=========================================="
|
||||
echo -e "测试结果: ${GREEN}通过 $PASS${NC} / ${RED}失败 $FAIL${NC}"
|
||||
echo "=========================================="
|
||||
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ 所有基础测试通过!${NC}"
|
||||
exit 0
|
||||
else
|
||||
echo -e "${RED}✗ 有 $FAIL 个测试失败${NC}"
|
||||
exit 1
|
||||
fi
|
||||
Loading…
Reference in new issue