Skip to content

Commit a574527

Browse files
committed
Add support for unary op 'not' in eval_expr, move not test to passing
1 parent 1766730 commit a574527

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

pythonbpf/expr_pass.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder
2222

2323
def _handle_constant_expr(expr: ast.Constant):
2424
"""Handle ast.Constant expressions."""
25-
logger.info("We the best")
2625
if isinstance(expr.value, int) or isinstance(expr.value, bool):
2726
return ir.Constant(ir.IntType(64), int(expr.value)), ir.IntType(64)
2827
else:
@@ -191,6 +190,43 @@ def _handle_compare(
191190
return _handle_comparator(builder, cond.ops[0], lhs, rhs)
192191

193192

193+
def convert_to_bool(builder, val):
194+
if val.type == ir.IntType(1):
195+
return val
196+
if isinstance(val.type, ir.PointerType):
197+
zero = ir.Constant(val.type, None)
198+
else:
199+
zero = ir.Constant(val.type, 0)
200+
return builder.icmp_signed("!=", val, zero)
201+
202+
203+
def _handle_unary_op(
204+
func,
205+
module,
206+
builder,
207+
expr: ast.UnaryOp,
208+
local_sym_tab,
209+
map_sym_tab,
210+
structs_sym_tab=None,
211+
):
212+
"""Handle ast.UnaryOp expressions."""
213+
if not isinstance(expr.op, ast.Not):
214+
logger.error("Only 'not' unary operator is supported")
215+
return None
216+
217+
operand = eval_expr(
218+
func, module, builder, expr.operand, local_sym_tab, map_sym_tab, structs_sym_tab
219+
)
220+
if operand is None:
221+
logger.error("Failed to evaluate operand for unary operation")
222+
return None
223+
224+
operand_val, operand_type = operand
225+
true_const = ir.Constant(ir.IntType(1), 1)
226+
result = builder.xor(convert_to_bool(builder, operand_val), true_const)
227+
return result, ir.IntType(1)
228+
229+
194230
def eval_expr(
195231
func,
196232
module,
@@ -275,6 +311,10 @@ def eval_expr(
275311
return _handle_compare(
276312
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
277313
)
314+
elif isinstance(expr, ast.UnaryOp):
315+
return _handle_unary_op(
316+
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
317+
)
278318
logger.info("Unsupported expression evaluation")
279319
return None
280320

pythonbpf/functions/functions_pass.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
88
from pythonbpf.type_deducer import ctypes_to_ir
99
from pythonbpf.binary_ops import handle_binary_op
10-
from pythonbpf.expr_pass import eval_expr, handle_expr
10+
from pythonbpf.expr_pass import eval_expr, handle_expr, convert_to_bool
1111

1212
from .return_utils import _handle_none_return, _handle_xdp_return, _is_xdp_name
1313

@@ -240,20 +240,10 @@ def handle_assign(
240240
logger.info("Unsupported assignment value type")
241241

242242

243-
def _convert_to_bool(builder, val):
244-
if val.type == ir.IntType(1):
245-
return val
246-
if isinstance(val.type, ir.PointerType):
247-
zero = ir.Constant(val.type, None)
248-
else:
249-
zero = ir.Constant(val.type, 0)
250-
return builder.icmp_signed("!=", val, zero)
251-
252-
253243
def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
254244
if True:
255245
val = eval_expr(func, module, builder, cond, local_sym_tab, map_sym_tab)[0]
256-
return _convert_to_bool(builder, val)
246+
return convert_to_bool(builder, val)
257247
if isinstance(cond, ast.Constant):
258248
if isinstance(cond.value, bool) or isinstance(cond.value, int):
259249
return ir.Constant(ir.IntType(1), int(cond.value))
File renamed without changes.

0 commit comments

Comments
 (0)