From a8b3f4f86c9ce55cb94f57c13f9e08a69207171c Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 3 Oct 2025 17:08:41 +0530 Subject: [PATCH] Fix recursive binops, move failing binops to passing --- pythonbpf/binary_ops.py | 17 ++++++++++++----- pythonbpf/codegen.py | 2 +- pythonbpf/functions_pass.py | 4 +--- .../{failing_tests => passing_tests}/binops.py | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) rename tests/{failing_tests => passing_tests}/binops.py (84%) diff --git a/pythonbpf/binary_ops.py b/pythonbpf/binary_ops.py index 26d59cd..5ab393f 100644 --- a/pythonbpf/binary_ops.py +++ b/pythonbpf/binary_ops.py @@ -18,7 +18,7 @@ def recursive_dereferencer(var, builder): raise TypeError(f"Unsupported type for dereferencing: {var.type}") -def get_operand_value(operand, builder, local_sym_tab): +def get_operand_value(operand, module, builder, local_sym_tab): """Extract the value from an operand, handling variables and constants.""" if isinstance(operand, ast.Name): if operand.id in local_sym_tab: @@ -28,13 +28,15 @@ def get_operand_value(operand, builder, local_sym_tab): if isinstance(operand.value, int): return ir.Constant(ir.IntType(64), operand.value) raise TypeError(f"Unsupported constant type: {type(operand.value)}") + elif isinstance(operand, ast.BinOp): + return handle_binary_op_impl(operand, module, builder, local_sym_tab) raise TypeError(f"Unsupported operand type: {type(operand)}") -def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func): +def handle_binary_op_impl(rval, module, builder, local_sym_tab): op = rval.op - left = get_operand_value(rval.left, builder, local_sym_tab) - right = get_operand_value(rval.right, builder, local_sym_tab) + left = get_operand_value(rval.left, module, builder, local_sym_tab) + right = get_operand_value(rval.right, module, builder, local_sym_tab) logger.info(f"left is {left}, right is {right}, op is {op}") # Map AST operation nodes to LLVM IR builder methods @@ -54,6 +56,11 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab if type(op) in op_map: result = op_map[type(op)](left, right) - builder.store(result, local_sym_tab[var_name].var) + return result else: raise SyntaxError("Unsupported binary operation") + + +def handle_binary_op(rval, module, builder, var_name, local_sym_tab): + result = handle_binary_op_impl(rval, module, builder, local_sym_tab) + builder.store(result, local_sym_tab[var_name].var) diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 5de23a5..c50d316 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -121,7 +121,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING): return output -def compile(loglevel=logging.WARNING) -> bool: +def compile(loglevel=logging.INFO) -> bool: # Look one level up the stack to the caller of this function caller_frame = inspect.stack()[1] caller_file = Path(caller_frame.filename).resolve() diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 6653677..430b55f 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -233,9 +233,7 @@ def handle_assign( else: logger.info("Unsupported assignment call function type") elif isinstance(rval, ast.BinOp): - handle_binary_op( - rval, module, builder, var_name, local_sym_tab, map_sym_tab, func - ) + handle_binary_op(rval, module, builder, var_name, local_sym_tab) else: logger.info("Unsupported assignment value type") diff --git a/tests/failing_tests/binops.py b/tests/passing_tests/binops.py similarity index 84% rename from tests/failing_tests/binops.py rename to tests/passing_tests/binops.py index 3a86765..14c5eef 100644 --- a/tests/failing_tests/binops.py +++ b/tests/passing_tests/binops.py @@ -3,7 +3,7 @@ from ctypes import c_void_p, c_int64 @bpf -@section("sometag1") +@section("tracepoint/syscalls/sys_enter_sync") def sometag(ctx: c_void_p) -> c_int64: a = 1 + 2 + 1 print(f"{a}")