Skip to content

Commit 4e33fd4

Browse files
committed
Add negation UnaryOp
1 parent 2cf68f6 commit 4e33fd4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pythonbpf/expr/expr_pass.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _handle_unary_op(
176176
structs_sym_tab=None,
177177
):
178178
"""Handle ast.UnaryOp expressions."""
179-
if not isinstance(expr.op, ast.Not):
179+
if not isinstance(expr.op, ast.Not) and not isinstance(expr.op, ast.USub):
180180
logger.error("Only 'not' unary operator is supported")
181181
return None
182182

@@ -188,9 +188,16 @@ def _handle_unary_op(
188188
return None
189189

190190
operand_val, operand_type = operand
191-
true_const = ir.Constant(ir.IntType(1), 1)
192-
result = builder.xor(convert_to_bool(builder, operand_val), true_const)
193-
return result, ir.IntType(1)
191+
192+
if isinstance(expr.op, ast.Not):
193+
true_const = ir.Constant(ir.IntType(1), 1)
194+
result = builder.xor(convert_to_bool(builder, operand_val), true_const)
195+
return result, ir.IntType(1)
196+
elif isinstance(expr.op, ast.USub):
197+
# Multiply by -1
198+
neg_one = ir.Constant(ir.IntType(64), -1)
199+
result = builder.mul(operand_val, neg_one)
200+
return result, ir.IntType(64)
194201

195202

196203
def _handle_and_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):

0 commit comments

Comments
 (0)