From c6fef1693ee024947b461df21247b082c646fc2b Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Mon, 6 Oct 2025 00:03:34 +0530 Subject: [PATCH] Add _handle_binop_return --- pythonbpf/functions/return_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pythonbpf/functions/return_utils.py b/pythonbpf/functions/return_utils.py index 3c4e1c6..84a700c 100644 --- a/pythonbpf/functions/return_utils.py +++ b/pythonbpf/functions/return_utils.py @@ -3,6 +3,7 @@ import ast from llvmlite import ir from pythonbpf.type_deducer import ctypes_to_ir +from pythonbpf.binary_ops import handle_binary_op logger: logging.Logger = logging.getLogger(__name__) @@ -39,6 +40,25 @@ def _handle_typed_constant_return(call_type, return_value, builder, ret_type) -> return True +def _handle_binop_return(arg, builder, ret_type, local_sym_tab) -> bool: + """Handle return with binary operation: return c_int64(x + 1)""" + + # result = handle_binary_op(stmt.value.args[0], builder, None, local_sym_tab) + result = handle_binary_op(arg, builder, None, local_sym_tab) + + if result is None: + raise ValueError("Failed to evaluate binary operation in return statement") + + val, val_type = result + + if val_type != ret_type: + raise ValueError(f"Return type mismatch: expected {ret_type}, got {val_type}") + + builder.ret(val) + logger.debug(f"Generated binary operation return: {val}") + return True + + def _handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool: """Handle XDP returns""" if not isinstance(stmt.value, ast.Name):