Skip to content

Commit 99aacca

Browse files
committed
WIP: allow pointer assignments to var
1 parent 1d517d4 commit 99aacca

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

pythonbpf/assign_pass.py

Lines changed: 18 additions & 1 deletion
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
4+
from pythonbpf.expr import eval_expr, get_base_type_and_depth
55

66
logger = logging.getLogger(__name__)
77

@@ -38,6 +38,9 @@ def handle_variable_assignment(
3838

3939
val, val_type = val_result
4040
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}")
4144
if isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType):
4245
# Allow implicit int widening
4346
if val_type.width < var_type.width:
@@ -46,10 +49,24 @@ def handle_variable_assignment(
4649
elif val_type.width > var_type.width:
4750
val = builder.trunc(val, var_type)
4851
logger.info(f"Implicitly truncated int for variable {var_name}")
52+
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
4964
else:
5065
logger.error(
5166
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
5267
)
68+
logger.error(f"var_type: {isinstance(var_type, ir.PointerType)}")
69+
logger.error(f"val_type: {isinstance(val_type, ir.IntType)}")
5370
return False
5471

5572
builder.store(val, var_ptr)

pythonbpf/expr/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .expr_pass import eval_expr, handle_expr
2-
from .type_normalization import convert_to_bool
2+
from .type_normalization import convert_to_bool, get_base_type_and_depth
33

4-
__all__ = ["eval_expr", "handle_expr", "convert_to_bool"]
4+
__all__ = ["eval_expr", "handle_expr", "convert_to_bool", "get_base_type_and_depth"]

pythonbpf/expr/type_normalization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818

19-
def _get_base_type_and_depth(ir_type):
19+
def get_base_type_and_depth(ir_type):
2020
"""Get the base type for pointer types."""
2121
cur_type = ir_type
2222
depth = 0
@@ -88,8 +88,8 @@ def _normalize_types(func, builder, lhs, rhs):
8888
logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}")
8989
return None, None
9090
else:
91-
lhs_base, lhs_depth = _get_base_type_and_depth(lhs.type)
92-
rhs_base, rhs_depth = _get_base_type_and_depth(rhs.type)
91+
lhs_base, lhs_depth = get_base_type_and_depth(lhs.type)
92+
rhs_base, rhs_depth = get_base_type_and_depth(rhs.type)
9393
if lhs_base == rhs_base:
9494
if lhs_depth < rhs_depth:
9595
rhs = _deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)

pythonbpf/functions/functions_pass.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ def allocate_mem(
461461
var = builder.alloca(ir_type, name=var_name)
462462

463463
# declare an intermediate ptr type for map lookup
464-
ir_type = ir.IntType(64)
465-
var_tmp = builder.alloca(ir_type, name=f"{var_name}_tmp")
464+
tmp_ir_type = ir.IntType(64)
465+
var_tmp = builder.alloca(tmp_ir_type, name=f"{var_name}_tmp")
466466
double_alloc = True
467467
# var.align = ir_type.width // 8
468468
logger.info(
@@ -507,7 +507,7 @@ def allocate_mem(
507507
local_sym_tab[var_name] = LocalSymbol(var, ir_type)
508508

509509
if double_alloc:
510-
local_sym_tab[f"{var_name}_tmp"] = LocalSymbol(var_tmp, ir_type)
510+
local_sym_tab[f"{var_name}_tmp"] = LocalSymbol(var_tmp, tmp_ir_type)
511511
return local_sym_tab
512512

513513

0 commit comments

Comments
 (0)