mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Allow int** pointers to store binops of type int** op int
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, get_base_type_and_depth
|
from pythonbpf.expr import eval_expr
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -37,10 +37,8 @@ def handle_variable_assignment(
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
val, val_type = val_result
|
val, val_type = val_result
|
||||||
|
logger.info(f"Evaluated value for {var_name}: {val} of type {val_type}, {var_type}")
|
||||||
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:
|
||||||
@ -50,23 +48,17 @@ def handle_variable_assignment(
|
|||||||
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):
|
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType):
|
||||||
ptr_target, ptr_depth = get_base_type_and_depth(var_type)
|
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
|
||||||
if ptr_target.width > val_type.width:
|
logger.info(
|
||||||
val = builder.sext(val, ptr_target)
|
f"Creating temporary variable for pointer assignment to {var_name}"
|
||||||
elif ptr_target.width < val_type.width:
|
)
|
||||||
val = builder.trunc(val, ptr_target)
|
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
|
||||||
|
builder.store(val, var_ptr_tmp)
|
||||||
if ptr_depth > 1:
|
val = var_ptr_tmp
|
||||||
# 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)
|
||||||
|
|||||||
@ -3,34 +3,21 @@ from llvmlite import ir
|
|||||||
from logging import Logger
|
from logging import Logger
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pythonbpf.expr import get_base_type_and_depth, deref_to_depth
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def deref_to_val(var, builder):
|
def get_operand_value(func, operand, builder, local_sym_tab):
|
||||||
"""Dereference a variable to get its value and pointer chain."""
|
|
||||||
logger.info(f"Dereferencing {var}, type is {var.type}")
|
|
||||||
|
|
||||||
chain = [var]
|
|
||||||
cur = var
|
|
||||||
|
|
||||||
while isinstance(cur.type, ir.PointerType):
|
|
||||||
cur = builder.load(cur)
|
|
||||||
chain.append(cur)
|
|
||||||
|
|
||||||
if isinstance(cur.type, ir.IntType):
|
|
||||||
logger.info(f"dereference chain: {chain}")
|
|
||||||
return cur, chain
|
|
||||||
else:
|
|
||||||
raise TypeError(f"Unsupported type for dereferencing: {cur.type}")
|
|
||||||
|
|
||||||
|
|
||||||
def get_operand_value(operand, builder, local_sym_tab):
|
|
||||||
"""Extract the value from an operand, handling variables and constants."""
|
"""Extract the value from an operand, handling variables and constants."""
|
||||||
if isinstance(operand, ast.Name):
|
if isinstance(operand, ast.Name):
|
||||||
if operand.id in local_sym_tab:
|
if operand.id in local_sym_tab:
|
||||||
var = local_sym_tab[operand.id].var
|
var = local_sym_tab[operand.id].var
|
||||||
val, chain = deref_to_val(var, builder)
|
var_type = var.type
|
||||||
return val, chain, var
|
base_type, depth = get_base_type_and_depth(var_type)
|
||||||
|
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
|
||||||
|
val = deref_to_depth(func, builder, var, depth)
|
||||||
|
return val, [val], var
|
||||||
raise ValueError(f"Undefined variable: {operand.id}")
|
raise ValueError(f"Undefined variable: {operand.id}")
|
||||||
elif isinstance(operand, ast.Constant):
|
elif isinstance(operand, ast.Constant):
|
||||||
if isinstance(operand.value, int):
|
if isinstance(operand.value, int):
|
||||||
@ -38,7 +25,7 @@ def get_operand_value(operand, builder, local_sym_tab):
|
|||||||
return cst, [cst], None
|
return cst, [cst], None
|
||||||
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
|
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
|
||||||
elif isinstance(operand, ast.BinOp):
|
elif isinstance(operand, ast.BinOp):
|
||||||
res = handle_binary_op_impl(operand, builder, local_sym_tab)
|
res = handle_binary_op_impl(func, operand, builder, local_sym_tab)
|
||||||
return res, [res], None
|
return res, [res], None
|
||||||
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
||||||
|
|
||||||
@ -53,10 +40,10 @@ def store_through_chain(value, chain, builder):
|
|||||||
value = ptr
|
value = ptr
|
||||||
|
|
||||||
|
|
||||||
def handle_binary_op_impl(rval, builder, local_sym_tab):
|
def handle_binary_op_impl(func, rval, builder, local_sym_tab):
|
||||||
op = rval.op
|
op = rval.op
|
||||||
left, lchain, _ = get_operand_value(rval.left, builder, local_sym_tab)
|
left, lchain, _ = get_operand_value(func, rval.left, builder, local_sym_tab)
|
||||||
right, rchain, _ = get_operand_value(rval.right, builder, local_sym_tab)
|
right, rchain, _ = get_operand_value(func, rval.right, builder, local_sym_tab)
|
||||||
logger.info(f"left is {left}, right is {right}, op is {op}")
|
logger.info(f"left is {left}, right is {right}, op is {op}")
|
||||||
|
|
||||||
logger.info(f"left chain: {lchain}, right chain: {rchain}")
|
logger.info(f"left chain: {lchain}, right chain: {rchain}")
|
||||||
@ -83,8 +70,8 @@ def handle_binary_op_impl(rval, builder, local_sym_tab):
|
|||||||
raise SyntaxError("Unsupported binary operation")
|
raise SyntaxError("Unsupported binary operation")
|
||||||
|
|
||||||
|
|
||||||
def handle_binary_op(rval, builder, var_name, local_sym_tab):
|
def handle_binary_op(func, rval, builder, var_name, local_sym_tab):
|
||||||
result = handle_binary_op_impl(rval, builder, local_sym_tab)
|
result = handle_binary_op_impl(func, rval, builder, local_sym_tab)
|
||||||
if var_name and var_name in local_sym_tab:
|
if var_name and var_name in local_sym_tab:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Storing result {result} into variable {local_sym_tab[var_name].var}"
|
f"Storing result {result} into variable {local_sym_tab[var_name].var}"
|
||||||
|
|||||||
@ -402,7 +402,7 @@ def eval_expr(
|
|||||||
elif isinstance(expr, ast.BinOp):
|
elif isinstance(expr, ast.BinOp):
|
||||||
from pythonbpf.binary_ops import handle_binary_op
|
from pythonbpf.binary_ops import handle_binary_op
|
||||||
|
|
||||||
return handle_binary_op(expr, builder, None, local_sym_tab)
|
return handle_binary_op(func, expr, builder, None, local_sym_tab)
|
||||||
elif isinstance(expr, ast.Compare):
|
elif isinstance(expr, ast.Compare):
|
||||||
return _handle_compare(
|
return _handle_compare(
|
||||||
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
|||||||
Reference in New Issue
Block a user