Skip to content

Commit 6d5895e

Browse files
committed
More fixes to recursive dereferencer, add get_operand value
1 parent c9ee6e4 commit 6d5895e

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

pythonbpf/binary_ops.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,35 @@
88

99
def recursive_dereferencer(var, builder):
1010
"""dereference until primitive type comes out"""
11+
# TODO: Not worrying about stack overflow for now
1112
if isinstance(var.type, ir.PointerType):
1213
a = builder.load(var)
1314
return recursive_dereferencer(a, builder)
14-
elif var.type == ir.IntType(64):
15+
elif isinstance(var.type, ir.IntType):
1516
return var
1617
else:
1718
raise TypeError(f"Unsupported type for dereferencing: {var.type}")
1819

1920

21+
def get_operand_value(operand, builder, local_sym_tab):
22+
"""Extract the value from an operand, handling variables and constants."""
23+
if isinstance(operand, ast.Name):
24+
if operand.id in local_sym_tab:
25+
return recursive_dereferencer(local_sym_tab[operand.id].var, builder)
26+
raise ValueError(f"Undefined variable: {operand.id}")
27+
elif isinstance(operand, ast.Constant):
28+
if isinstance(operand.value, int):
29+
return ir.Constant(ir.IntType(64), operand.value)
30+
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
31+
raise TypeError(f"Unsupported operand type: {type(operand)}")
32+
33+
2034
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func):
2135
logger.info(f"module {module}")
22-
left = rval.left
23-
right = rval.right
2436
op = rval.op
2537

26-
# Handle left operand
27-
if isinstance(left, ast.Name):
28-
if left.id in local_sym_tab:
29-
left = recursive_dereferencer(local_sym_tab[left.id].var, builder)
30-
else:
31-
raise SyntaxError(f"Undefined variable: {left.id}")
32-
elif isinstance(left, ast.Constant):
33-
left = ir.Constant(ir.IntType(64), left.value)
34-
else:
35-
raise SyntaxError("Unsupported left operand type")
36-
37-
if isinstance(right, ast.Name):
38-
if right.id in local_sym_tab:
39-
right = recursive_dereferencer(local_sym_tab[right.id].var, builder)
40-
else:
41-
raise SyntaxError(f"Undefined variable: {right.id}")
42-
elif isinstance(right, ast.Constant):
43-
right = ir.Constant(ir.IntType(64), right.value)
44-
else:
45-
raise SyntaxError("Unsupported right operand type")
46-
38+
left = get_operand_value(rval.left, builder, local_sym_tab)
39+
right = get_operand_value(rval.right, builder, local_sym_tab)
4740
logger.info(f"left is {left}, right is {right}, op is {op}")
4841

4942
if isinstance(op, ast.Add):

0 commit comments

Comments
 (0)