Skip to content

Commit c9bbe1f

Browse files
committed
Call eval_expr properly within get_operand_value
1 parent 91a3fe1 commit c9bbe1f

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

pythonbpf/binary_ops.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
logger: Logger = logging.getLogger(__name__)
99

1010

11-
def get_operand_value(func, operand, builder, local_sym_tab):
11+
def get_operand_value(
12+
func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None
13+
):
1214
"""Extract the value from an operand, handling variables and constants."""
1315
logger.info(f"Getting operand value for: {ast.dump(operand)}")
1416
if isinstance(operand, ast.Name):
@@ -26,21 +28,31 @@ def get_operand_value(func, operand, builder, local_sym_tab):
2628
return cst
2729
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
2830
elif isinstance(operand, ast.BinOp):
29-
res = handle_binary_op_impl(func, operand, builder, local_sym_tab)
31+
res = handle_binary_op_impl(
32+
func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab
33+
)
3034
return res
31-
elif isinstance(operand, ast.Call):
32-
res = eval_expr(func, None, builder, operand, local_sym_tab, {}, {})
35+
else:
36+
res = eval_expr(
37+
func, module, builder, operand, local_sym_tab, map_sym_tab, structs_sym_tab
38+
)
3339
if res is None:
3440
raise ValueError(f"Failed to evaluate call expression: {operand}")
3541
val, _ = res
3642
return val
3743
raise TypeError(f"Unsupported operand type: {type(operand)}")
3844

3945

40-
def handle_binary_op_impl(func, rval, builder, local_sym_tab):
46+
def handle_binary_op_impl(
47+
func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None
48+
):
4149
op = rval.op
42-
left = get_operand_value(func, rval.left, builder, local_sym_tab)
43-
right = get_operand_value(func, rval.right, builder, local_sym_tab)
50+
left = get_operand_value(
51+
func, module, rval.left, builder, local_sym_tab, map_sym_tab, structs_sym_tab
52+
)
53+
right = get_operand_value(
54+
func, module, rval.right, builder, local_sym_tab, map_sym_tab, structs_sym_tab
55+
)
4456
logger.info(f"left is {left}, right is {right}, op is {op}")
4557

4658
# 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):
7385
raise SyntaxError("Unsupported binary operation")
7486

7587

76-
def handle_binary_op(func, rval, builder, var_name, local_sym_tab):
77-
result = handle_binary_op_impl(func, rval, builder, local_sym_tab)
88+
def handle_binary_op(
89+
func,
90+
module,
91+
rval,
92+
builder,
93+
var_name,
94+
local_sym_tab,
95+
map_sym_tab,
96+
structs_sym_tab=None,
97+
):
98+
result = handle_binary_op_impl(
99+
func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab
100+
)
78101
if var_name and var_name in local_sym_tab:
79102
logger.info(
80103
f"Storing result {result} into variable {local_sym_tab[var_name].var}"

pythonbpf/expr/expr_pass.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,16 @@ def eval_expr(
402402
elif isinstance(expr, ast.BinOp):
403403
from pythonbpf.binary_ops import handle_binary_op
404404

405-
return handle_binary_op(func, expr, builder, None, local_sym_tab)
405+
return handle_binary_op(
406+
func,
407+
module,
408+
expr,
409+
builder,
410+
None,
411+
local_sym_tab,
412+
map_sym_tab,
413+
structs_sym_tab,
414+
)
406415
elif isinstance(expr, ast.Compare):
407416
return _handle_compare(
408417
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab

0 commit comments

Comments
 (0)