From c9bbe1ffd87de30e28a3b1433659d0e85f7c433a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sat, 11 Oct 2025 03:21:09 +0530 Subject: [PATCH] Call eval_expr properly within get_operand_value --- pythonbpf/binary_ops.py | 41 +++++++++++++++++++++++++++++-------- pythonbpf/expr/expr_pass.py | 11 +++++++++- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/pythonbpf/binary_ops.py b/pythonbpf/binary_ops.py index e8dd32b..a5b8dbe 100644 --- a/pythonbpf/binary_ops.py +++ b/pythonbpf/binary_ops.py @@ -8,7 +8,9 @@ from pythonbpf.expr import get_base_type_and_depth, deref_to_depth, eval_expr logger: Logger = logging.getLogger(__name__) -def get_operand_value(func, operand, builder, local_sym_tab): +def get_operand_value( + func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None +): """Extract the value from an operand, handling variables and constants.""" logger.info(f"Getting operand value for: {ast.dump(operand)}") if isinstance(operand, ast.Name): @@ -26,10 +28,14 @@ def get_operand_value(func, operand, builder, local_sym_tab): return cst raise TypeError(f"Unsupported constant type: {type(operand.value)}") elif isinstance(operand, ast.BinOp): - res = handle_binary_op_impl(func, operand, builder, local_sym_tab) + res = handle_binary_op_impl( + func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab + ) return res - elif isinstance(operand, ast.Call): - res = eval_expr(func, None, builder, operand, local_sym_tab, {}, {}) + else: + res = eval_expr( + func, module, builder, operand, local_sym_tab, map_sym_tab, structs_sym_tab + ) if res is None: raise ValueError(f"Failed to evaluate call expression: {operand}") val, _ = res @@ -37,10 +43,16 @@ def get_operand_value(func, operand, builder, local_sym_tab): raise TypeError(f"Unsupported operand type: {type(operand)}") -def handle_binary_op_impl(func, rval, builder, local_sym_tab): +def handle_binary_op_impl( + func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None +): op = rval.op - left = get_operand_value(func, rval.left, builder, local_sym_tab) - right = get_operand_value(func, rval.right, builder, local_sym_tab) + left = get_operand_value( + func, module, rval.left, builder, local_sym_tab, map_sym_tab, structs_sym_tab + ) + right = get_operand_value( + func, module, rval.right, builder, local_sym_tab, map_sym_tab, structs_sym_tab + ) logger.info(f"left is {left}, right is {right}, op is {op}") # NOTE: Before doing the operation, if the operands are integers @@ -73,8 +85,19 @@ def handle_binary_op_impl(func, rval, builder, local_sym_tab): raise SyntaxError("Unsupported binary operation") -def handle_binary_op(func, rval, builder, var_name, local_sym_tab): - result = handle_binary_op_impl(func, rval, builder, local_sym_tab) +def handle_binary_op( + func, + module, + rval, + builder, + var_name, + local_sym_tab, + map_sym_tab, + structs_sym_tab=None, +): + result = handle_binary_op_impl( + func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab + ) if var_name and var_name in local_sym_tab: logger.info( f"Storing result {result} into variable {local_sym_tab[var_name].var}" diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index 8a5b608..6f79406 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -402,7 +402,16 @@ def eval_expr( elif isinstance(expr, ast.BinOp): from pythonbpf.binary_ops import handle_binary_op - return handle_binary_op(func, expr, builder, None, local_sym_tab) + return handle_binary_op( + func, + module, + expr, + builder, + None, + local_sym_tab, + map_sym_tab, + structs_sym_tab, + ) elif isinstance(expr, ast.Compare): return _handle_compare( func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab