From 4e33fd4a32b10d0b23a029ba31e05b5e8388c3eb Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 12 Oct 2025 09:11:56 +0530 Subject: [PATCH] Add negation UnaryOp --- pythonbpf/expr/expr_pass.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index 6f79406..bbb7277 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -176,7 +176,7 @@ def _handle_unary_op( structs_sym_tab=None, ): """Handle ast.UnaryOp expressions.""" - if not isinstance(expr.op, ast.Not): + if not isinstance(expr.op, ast.Not) and not isinstance(expr.op, ast.USub): logger.error("Only 'not' unary operator is supported") return None @@ -188,9 +188,16 @@ def _handle_unary_op( return None operand_val, operand_type = operand - true_const = ir.Constant(ir.IntType(1), 1) - result = builder.xor(convert_to_bool(builder, operand_val), true_const) - return result, ir.IntType(1) + + if isinstance(expr.op, ast.Not): + true_const = ir.Constant(ir.IntType(1), 1) + result = builder.xor(convert_to_bool(builder, operand_val), true_const) + return result, ir.IntType(1) + elif isinstance(expr.op, ast.USub): + # Multiply by -1 + neg_one = ir.Constant(ir.IntType(64), -1) + result = builder.mul(operand_val, neg_one) + return result, ir.IntType(64) def _handle_and_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):