mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
WIP: allow pointer assignments to var
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import ast
|
import ast
|
||||||
import logging
|
import logging
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
from pythonbpf.expr import eval_expr
|
from pythonbpf.expr import eval_expr, get_base_type_and_depth
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,6 +38,9 @@ def handle_variable_assignment(
|
|||||||
|
|
||||||
val, val_type = val_result
|
val, val_type = val_result
|
||||||
if val_type != var_type:
|
if val_type != var_type:
|
||||||
|
logger.info(f"val = {val}")
|
||||||
|
logger.info(f"var = {var_ptr}")
|
||||||
|
logger.info(f"truthy {var_type}")
|
||||||
if isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType):
|
if isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType):
|
||||||
# Allow implicit int widening
|
# Allow implicit int widening
|
||||||
if val_type.width < var_type.width:
|
if val_type.width < var_type.width:
|
||||||
@ -46,10 +49,24 @@ def handle_variable_assignment(
|
|||||||
elif val_type.width > var_type.width:
|
elif val_type.width > var_type.width:
|
||||||
val = builder.trunc(val, var_type)
|
val = builder.trunc(val, var_type)
|
||||||
logger.info(f"Implicitly truncated int for variable {var_name}")
|
logger.info(f"Implicitly truncated int for variable {var_name}")
|
||||||
|
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType):
|
||||||
|
ptr_target, ptr_depth = get_base_type_and_depth(var_type)
|
||||||
|
if ptr_target.width > val_type.width:
|
||||||
|
val = builder.sext(val, ptr_target)
|
||||||
|
elif ptr_target.width < val_type.width:
|
||||||
|
val = builder.trunc(val, ptr_target)
|
||||||
|
|
||||||
|
if ptr_depth > 1:
|
||||||
|
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
|
||||||
|
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
|
||||||
|
builder.store(val, var_ptr_tmp)
|
||||||
|
val = var_ptr_tmp
|
||||||
else:
|
else:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
|
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
|
||||||
)
|
)
|
||||||
|
logger.error(f"var_type: {isinstance(var_type, ir.PointerType)}")
|
||||||
|
logger.error(f"val_type: {isinstance(val_type, ir.IntType)}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
builder.store(val, var_ptr)
|
builder.store(val, var_ptr)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from .expr_pass import eval_expr, handle_expr
|
from .expr_pass import eval_expr, handle_expr
|
||||||
from .type_normalization import convert_to_bool
|
from .type_normalization import convert_to_bool, get_base_type_and_depth
|
||||||
|
|
||||||
__all__ = ["eval_expr", "handle_expr", "convert_to_bool"]
|
__all__ = ["eval_expr", "handle_expr", "convert_to_bool", "get_base_type_and_depth"]
|
||||||
|
|||||||
@ -16,7 +16,7 @@ COMPARISON_OPS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _get_base_type_and_depth(ir_type):
|
def get_base_type_and_depth(ir_type):
|
||||||
"""Get the base type for pointer types."""
|
"""Get the base type for pointer types."""
|
||||||
cur_type = ir_type
|
cur_type = ir_type
|
||||||
depth = 0
|
depth = 0
|
||||||
@ -88,8 +88,8 @@ def _normalize_types(func, builder, lhs, rhs):
|
|||||||
logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}")
|
logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}")
|
||||||
return None, None
|
return None, None
|
||||||
else:
|
else:
|
||||||
lhs_base, lhs_depth = _get_base_type_and_depth(lhs.type)
|
lhs_base, lhs_depth = get_base_type_and_depth(lhs.type)
|
||||||
rhs_base, rhs_depth = _get_base_type_and_depth(rhs.type)
|
rhs_base, rhs_depth = get_base_type_and_depth(rhs.type)
|
||||||
if lhs_base == rhs_base:
|
if lhs_base == rhs_base:
|
||||||
if lhs_depth < rhs_depth:
|
if lhs_depth < rhs_depth:
|
||||||
rhs = _deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)
|
rhs = _deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)
|
||||||
|
|||||||
@ -461,8 +461,8 @@ def allocate_mem(
|
|||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
|
|
||||||
# declare an intermediate ptr type for map lookup
|
# declare an intermediate ptr type for map lookup
|
||||||
ir_type = ir.IntType(64)
|
tmp_ir_type = ir.IntType(64)
|
||||||
var_tmp = builder.alloca(ir_type, name=f"{var_name}_tmp")
|
var_tmp = builder.alloca(tmp_ir_type, name=f"{var_name}_tmp")
|
||||||
double_alloc = True
|
double_alloc = True
|
||||||
# var.align = ir_type.width // 8
|
# var.align = ir_type.width // 8
|
||||||
logger.info(
|
logger.info(
|
||||||
@ -507,7 +507,7 @@ def allocate_mem(
|
|||||||
local_sym_tab[var_name] = LocalSymbol(var, ir_type)
|
local_sym_tab[var_name] = LocalSymbol(var, ir_type)
|
||||||
|
|
||||||
if double_alloc:
|
if double_alloc:
|
||||||
local_sym_tab[f"{var_name}_tmp"] = LocalSymbol(var_tmp, ir_type)
|
local_sym_tab[f"{var_name}_tmp"] = LocalSymbol(var_tmp, tmp_ir_type)
|
||||||
return local_sym_tab
|
return local_sym_tab
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user