Skip to content

Commit 7529820

Browse files
committed
Allow int** pointers to store binops of type int** op int
1 parent 9febadf commit 7529820

File tree

3 files changed

+24
-45
lines changed

3 files changed

+24
-45
lines changed

pythonbpf/assign_pass.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ast
22
import logging
33
from llvmlite import ir
4-
from pythonbpf.expr import eval_expr, get_base_type_and_depth
4+
from pythonbpf.expr import eval_expr
55

66
logger = logging.getLogger(__name__)
77

@@ -37,10 +37,8 @@ def handle_variable_assignment(
3737
return False
3838

3939
val, val_type = val_result
40+
logger.info(f"Evaluated value for {var_name}: {val} of type {val_type}, {var_type}")
4041
if val_type != var_type:
41-
logger.info(f"val = {val}")
42-
logger.info(f"var = {var_ptr}")
43-
logger.info(f"truthy {var_type}")
4442
if isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType):
4543
# Allow implicit int widening
4644
if val_type.width < var_type.width:
@@ -50,23 +48,17 @@ def handle_variable_assignment(
5048
val = builder.trunc(val, var_type)
5149
logger.info(f"Implicitly truncated int for variable {var_name}")
5250
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType):
53-
ptr_target, ptr_depth = get_base_type_and_depth(var_type)
54-
if ptr_target.width > val_type.width:
55-
val = builder.sext(val, ptr_target)
56-
elif ptr_target.width < val_type.width:
57-
val = builder.trunc(val, ptr_target)
58-
59-
if ptr_depth > 1:
60-
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
61-
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
62-
builder.store(val, var_ptr_tmp)
63-
val = var_ptr_tmp
51+
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
52+
logger.info(
53+
f"Creating temporary variable for pointer assignment to {var_name}"
54+
)
55+
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
56+
builder.store(val, var_ptr_tmp)
57+
val = var_ptr_tmp
6458
else:
6559
logger.error(
6660
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
6761
)
68-
logger.error(f"var_type: {isinstance(var_type, ir.PointerType)}")
69-
logger.error(f"val_type: {isinstance(val_type, ir.IntType)}")
7062
return False
7163

7264
builder.store(val, var_ptr)

pythonbpf/binary_ops.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,29 @@
33
from logging import Logger
44
import logging
55

6-
logger: Logger = logging.getLogger(__name__)
7-
8-
9-
def deref_to_val(var, builder):
10-
"""Dereference a variable to get its value and pointer chain."""
11-
logger.info(f"Dereferencing {var}, type is {var.type}")
12-
13-
chain = [var]
14-
cur = var
6+
from pythonbpf.expr import get_base_type_and_depth, deref_to_depth
157

16-
while isinstance(cur.type, ir.PointerType):
17-
cur = builder.load(cur)
18-
chain.append(cur)
19-
20-
if isinstance(cur.type, ir.IntType):
21-
logger.info(f"dereference chain: {chain}")
22-
return cur, chain
23-
else:
24-
raise TypeError(f"Unsupported type for dereferencing: {cur.type}")
8+
logger: Logger = logging.getLogger(__name__)
259

2610

27-
def get_operand_value(operand, builder, local_sym_tab):
11+
def get_operand_value(func, operand, builder, local_sym_tab):
2812
"""Extract the value from an operand, handling variables and constants."""
2913
if isinstance(operand, ast.Name):
3014
if operand.id in local_sym_tab:
3115
var = local_sym_tab[operand.id].var
32-
val, chain = deref_to_val(var, builder)
33-
return val, chain, var
16+
var_type = var.type
17+
base_type, depth = get_base_type_and_depth(var_type)
18+
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
19+
val = deref_to_depth(func, builder, var, depth)
20+
return val, [val], var
3421
raise ValueError(f"Undefined variable: {operand.id}")
3522
elif isinstance(operand, ast.Constant):
3623
if isinstance(operand.value, int):
3724
cst = ir.Constant(ir.IntType(64), operand.value)
3825
return cst, [cst], None
3926
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
4027
elif isinstance(operand, ast.BinOp):
41-
res = handle_binary_op_impl(operand, builder, local_sym_tab)
28+
res = handle_binary_op_impl(func, operand, builder, local_sym_tab)
4229
return res, [res], None
4330
raise TypeError(f"Unsupported operand type: {type(operand)}")
4431

@@ -53,10 +40,10 @@ def store_through_chain(value, chain, builder):
5340
value = ptr
5441

5542

56-
def handle_binary_op_impl(rval, builder, local_sym_tab):
43+
def handle_binary_op_impl(func, rval, builder, local_sym_tab):
5744
op = rval.op
58-
left, lchain, _ = get_operand_value(rval.left, builder, local_sym_tab)
59-
right, rchain, _ = get_operand_value(rval.right, builder, local_sym_tab)
45+
left, lchain, _ = get_operand_value(func, rval.left, builder, local_sym_tab)
46+
right, rchain, _ = get_operand_value(func, rval.right, builder, local_sym_tab)
6047
logger.info(f"left is {left}, right is {right}, op is {op}")
6148

6249
logger.info(f"left chain: {lchain}, right chain: {rchain}")
@@ -83,8 +70,8 @@ def handle_binary_op_impl(rval, builder, local_sym_tab):
8370
raise SyntaxError("Unsupported binary operation")
8471

8572

86-
def handle_binary_op(rval, builder, var_name, local_sym_tab):
87-
result = handle_binary_op_impl(rval, builder, local_sym_tab)
73+
def handle_binary_op(func, rval, builder, var_name, local_sym_tab):
74+
result = handle_binary_op_impl(func, rval, builder, local_sym_tab)
8875
if var_name and var_name in local_sym_tab:
8976
logger.info(
9077
f"Storing result {result} into variable {local_sym_tab[var_name].var}"

pythonbpf/expr/expr_pass.py

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

405-
return handle_binary_op(expr, builder, None, local_sym_tab)
405+
return handle_binary_op(func, expr, builder, None, local_sym_tab)
406406
elif isinstance(expr, ast.Compare):
407407
return _handle_compare(
408408
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab

0 commit comments

Comments
 (0)