Repository: aliyun/coolbpf (language: python)

Issue:
Add tests for relation expression and verify functionality



Discussion / hints:

Fix patch:
diff --git a/lwcb/blang/src/passes/bpfir.rs b/lwcb/blang/src/passes/bpfir.rs
index de79ff3..f99cb8d 100644
--- a/lwcb/blang/src/passes/bpfir.rs
+++ b/lwcb/blang/src/passes/bpfir.rs
@@ -283,7 +283,15 @@ fn gen_expr_val(ctx: &mut LocalContext, fb: &mut FunctionBuilder, expr: &Expr) -
                     let rval = gen_expr_val(ctx, fb, r)?;
                     return Ok(to_clif_binary(fb, op, lval, rval));
                 }
-                _ => panic!("Relation binary op should not be handled here"),
+                BinaryOp::LT
+                | BinaryOp::LTE
+                | BinaryOp::GT
+                | BinaryOp::GTE
+                | BinaryOp::NonEqual
+                | BinaryOp::Equal => {
+                    return gen_expr_cf(ctx, fb, expr);
+                }
+                _ => todo!(),
             }
         }
         ExprKind::Cast(from, to) => gen_expr_val(ctx, fb, from),
@@ -411,6 +419,7 @@ fn type_size(ty: &bpfir::Type) -> u32 {
 
 fn to_clif_type(ty: &bpfir::Type) -> types::Type {
     match ty.kind {
+        TypeKind::Bool => types::I8,
         TypeKind::I8 => types::I8,
         TypeKind::U8 => types::I8,
         TypeKind::I16 => types::I16,
diff --git a/lwcb/blang/src/passes/typecheck.rs b/lwcb/blang/src/passes/typecheck.rs
index ee170a5..99c0ae8 100644
--- a/lwcb/blang/src/passes/typecheck.rs
+++ b/lwcb/blang/src/passes/typecheck.rs
@@ -271,7 +271,13 @@ fn type_check_expr_bianry(
         BinaryOp::Sub => l.ty.clone(),
         BinaryOp::Div => l.ty.clone(),
         BinaryOp::Mult => l.ty.clone(),
-        BinaryOp::Equal => Type::bool(),
+        BinaryOp::Equal
+        | BinaryOp::GT
+        | BinaryOp::NonEqual
+        | BinaryOp::GTE
+        | BinaryOp::LT
+        | BinaryOp::LTE => Type::bool(),
+
         _ => {
             todo!("not implment {op}");
         }


Test patch:
diff --git a/lwcb/blang/tests/files/relation_eq.cb b/lwcb/blang/tests/files/relation_eq.cb
new file mode 100644
index 0000000..a3dd789
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_eq.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp eq v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp eq v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp eq v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp eq v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp eq v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a == b;
+    d = a == 5;
+    e = 5 == a;
+    f = a == sk.sk_priority;
+    g = sk.sk_priority == a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/files/relation_gt.cb b/lwcb/blang/tests/files/relation_gt.cb
new file mode 100644
index 0000000..53be48b
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_gt.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp sgt v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp sgt v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp sgt v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp sgt v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp sgt v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a > b;
+    d = a > 5;
+    e = 5 > a;
+    f = a > sk.sk_priority;
+    g = sk.sk_priority > a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/files/relation_gte.cb b/lwcb/blang/tests/files/relation_gte.cb
new file mode 100644
index 0000000..c4c75aa
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_gte.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp sge v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp sge v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp sge v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp sge v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp sge v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a >= b;
+    d = a >= 5;
+    e = 5 >= a;
+    f = a >= sk.sk_priority;
+    g = sk.sk_priority >= a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/files/relation_lt.cb b/lwcb/blang/tests/files/relation_lt.cb
new file mode 100644
index 0000000..a60a44e
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_lt.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp slt v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp slt v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp slt v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp slt v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp slt v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a < b;
+    d = a < 5;
+    e = 5 < a;
+    f = a < sk.sk_priority;
+    g = sk.sk_priority < a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/files/relation_lte.cb b/lwcb/blang/tests/files/relation_lte.cb
new file mode 100644
index 0000000..0dac9de
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_lte.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp sle v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp sle v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp sle v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp sle v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp sle v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a <= b;
+    d = a <= 5;
+    e = 5 <= a;
+    f = a <= sk.sk_priority;
+    g = sk.sk_priority <= a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/files/relation_neq.cb b/lwcb/blang/tests/files/relation_neq.cb
new file mode 100644
index 0000000..4ba88cf
--- /dev/null
+++ b/lwcb/blang/tests/files/relation_neq.cb
@@ -0,0 +1,39 @@
+
+$
+function u0:0(i64) -> i64 system_v {
+block0(v0: i64):
+    v1 = iadd_imm v0, 112
+    v2 = load.i64 notrap aligned v1
+    v3 = iadd_imm v0, 104
+    v4 = load.i64 notrap aligned v3
+    v5 = iadd_imm v0, 96
+    v6 = load.i64 notrap aligned v5
+    v7 = iadd_imm v0, 88
+    v8 = load.i64 notrap aligned v7
+    v9 = iconst.i32 5
+    v10 = iconst.i32 5
+    v11 = icmp ne v9, v10  ; v9 = 5, v10 = 5
+    v12 = iconst.i32 5
+    v13 = icmp ne v9, v12  ; v9 = 5, v12 = 5
+    v14 = iconst.i32 5
+    v15 = icmp ne v14, v9  ; v14 = 5, v9 = 5
+    v16 = iadd_imm v4, 444
+    v17 = load.i32 notrap aligned v16
+    v18 = icmp ne v9, v17  ; v9 = 5
+    v19 = iadd_imm v4, 444
+    v20 = load.i32 notrap aligned v19
+    v21 = icmp ne v20, v9  ; v9 = 5
+    v22 = iconst.i64 0
+    return v22  ; v22 = 0
+}
+$
+
+kprobe:tcp_sendmsg {
+    a = 5;
+    b = 5;
+    c = a != b;
+    d = a != 5;
+    e = 5 != a;
+    f = a != sk.sk_priority;
+    g = sk.sk_priority != a;
+}
\ No newline at end of file
diff --git a/lwcb/blang/tests/test_bpfir.rs b/lwcb/blang/tests/test_bpfir.rs
index d317732..b4ac5ff 100644
--- a/lwcb/blang/tests/test_bpfir.rs
+++ b/lwcb/blang/tests/test_bpfir.rs
@@ -106,3 +106,33 @@ fn test_member_simpile() {
 fn test_member_complex() {
     run_one_test("member_complex.cb");
 }
+
+#[test]
+fn test_relation_eq() {
+    run_one_test("relation_eq.cb");
+}
+
+#[test]
+fn test_relation_neq() {
+    run_one_test("relation_neq.cb");
+}
+
+#[test]
+fn test_relation_gt() {
+    run_one_test("relation_gt.cb");
+}
+
+#[test]
+fn test_relation_gte() {
+    run_one_test("relation_gte.cb");
+}
+
+#[test]
+fn test_relation_lt() {
+    run_one_test("relation_lt.cb");
+}
+
+#[test]
+fn test_relation_lte() {
+    run_one_test("relation_lte.cb");
+}