Merge pull request #19 from pythonbpf/fix-expr

Refactor expr_pass
This commit is contained in:
Pragyansh Chaturvedi
2025-10-03 17:36:31 +05:30
committed by GitHub
5 changed files with 150 additions and 126 deletions

View File

@ -8,68 +8,59 @@ logger: Logger = logging.getLogger(__name__)
def recursive_dereferencer(var, builder): def recursive_dereferencer(var, builder):
"""dereference until primitive type comes out""" """dereference until primitive type comes out"""
if var.type == ir.PointerType(ir.PointerType(ir.IntType(64))): # TODO: Not worrying about stack overflow for now
if isinstance(var.type, ir.PointerType):
a = builder.load(var) a = builder.load(var)
return recursive_dereferencer(a, builder) return recursive_dereferencer(a, builder)
elif var.type == ir.PointerType(ir.IntType(64)): elif isinstance(var.type, ir.IntType):
a = builder.load(var)
return recursive_dereferencer(a, builder)
elif var.type == ir.IntType(64):
return var return var
else: else:
raise TypeError(f"Unsupported type for dereferencing: {var.type}") raise TypeError(f"Unsupported type for dereferencing: {var.type}")
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func): def get_operand_value(operand, module, builder, local_sym_tab):
logger.info(f"module {module}") """Extract the value from an operand, handling variables and constants."""
left = rval.left if isinstance(operand, ast.Name):
right = rval.right if operand.id in local_sym_tab:
return recursive_dereferencer(local_sym_tab[operand.id].var, builder)
raise ValueError(f"Undefined variable: {operand.id}")
elif isinstance(operand, ast.Constant):
if isinstance(operand.value, int):
return ir.Constant(ir.IntType(64), operand.value)
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
elif isinstance(operand, ast.BinOp):
return handle_binary_op_impl(operand, module, builder, local_sym_tab)
raise TypeError(f"Unsupported operand type: {type(operand)}")
def handle_binary_op_impl(rval, module, builder, local_sym_tab):
op = rval.op op = rval.op
left = get_operand_value(rval.left, module, builder, local_sym_tab)
# Handle left operand right = get_operand_value(rval.right, module, builder, local_sym_tab)
if isinstance(left, ast.Name):
if left.id in local_sym_tab:
left = recursive_dereferencer(local_sym_tab[left.id].var, builder)
else:
raise SyntaxError(f"Undefined variable: {left.id}")
elif isinstance(left, ast.Constant):
left = ir.Constant(ir.IntType(64), left.value)
else:
raise SyntaxError("Unsupported left operand type")
if isinstance(right, ast.Name):
if right.id in local_sym_tab:
right = recursive_dereferencer(local_sym_tab[right.id].var, builder)
else:
raise SyntaxError(f"Undefined variable: {right.id}")
elif isinstance(right, ast.Constant):
right = ir.Constant(ir.IntType(64), right.value)
else:
raise SyntaxError("Unsupported right operand type")
logger.info(f"left is {left}, right is {right}, op is {op}") logger.info(f"left is {left}, right is {right}, op is {op}")
if isinstance(op, ast.Add): # Map AST operation nodes to LLVM IR builder methods
builder.store(builder.add(left, right), local_sym_tab[var_name].var) op_map = {
elif isinstance(op, ast.Sub): ast.Add: builder.add,
builder.store(builder.sub(left, right), local_sym_tab[var_name].var) ast.Sub: builder.sub,
elif isinstance(op, ast.Mult): ast.Mult: builder.mul,
builder.store(builder.mul(left, right), local_sym_tab[var_name].var) ast.Div: builder.sdiv,
elif isinstance(op, ast.Div): ast.Mod: builder.srem,
builder.store(builder.sdiv(left, right), local_sym_tab[var_name].var) ast.LShift: builder.shl,
elif isinstance(op, ast.Mod): ast.RShift: builder.lshr,
builder.store(builder.srem(left, right), local_sym_tab[var_name].var) ast.BitOr: builder.or_,
elif isinstance(op, ast.LShift): ast.BitXor: builder.xor,
builder.store(builder.shl(left, right), local_sym_tab[var_name].var) ast.BitAnd: builder.and_,
elif isinstance(op, ast.RShift): ast.FloorDiv: builder.udiv,
builder.store(builder.lshr(left, right), local_sym_tab[var_name].var) }
elif isinstance(op, ast.BitOr):
builder.store(builder.or_(left, right), local_sym_tab[var_name].var) if type(op) in op_map:
elif isinstance(op, ast.BitXor): result = op_map[type(op)](left, right)
builder.store(builder.xor(left, right), local_sym_tab[var_name].var) return result
elif isinstance(op, ast.BitAnd):
builder.store(builder.and_(left, right), local_sym_tab[var_name].var)
elif isinstance(op, ast.FloorDiv):
builder.store(builder.udiv(left, right), local_sym_tab[var_name].var)
else: else:
raise SyntaxError("Unsupported binary operation") raise SyntaxError("Unsupported binary operation")
def handle_binary_op(rval, module, builder, var_name, local_sym_tab):
result = handle_binary_op_impl(rval, module, builder, local_sym_tab)
builder.store(result, local_sym_tab[var_name].var)

View File

@ -2,10 +2,92 @@ import ast
from llvmlite import ir from llvmlite import ir
from logging import Logger from logging import Logger
import logging import logging
from typing import Dict
logger: Logger = logging.getLogger(__name__) logger: Logger = logging.getLogger(__name__)
def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder):
"""Handle ast.Name expressions."""
if expr.id in local_sym_tab:
var = local_sym_tab[expr.id].var
val = builder.load(var)
return val, local_sym_tab[expr.id].ir_type
else:
logger.info(f"Undefined variable {expr.id}")
return None
def _handle_constant_expr(expr: ast.Constant):
"""Handle ast.Constant expressions."""
if isinstance(expr.value, int):
return ir.Constant(ir.IntType(64), expr.value), ir.IntType(64)
elif isinstance(expr.value, bool):
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
else:
logger.info("Unsupported constant type")
return None
def _handle_attribute_expr(
expr: ast.Attribute,
local_sym_tab: Dict,
structs_sym_tab: Dict,
builder: ir.IRBuilder,
):
"""Handle ast.Attribute expressions for struct field access."""
if isinstance(expr.value, ast.Name):
var_name = expr.value.id
attr_name = expr.attr
if var_name in local_sym_tab:
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
metadata = structs_sym_tab[var_metadata]
if attr_name in metadata.fields:
gep = metadata.gep(builder, var_ptr, attr_name)
val = builder.load(gep)
field_type = metadata.field_type(attr_name)
return val, field_type
return None
def _handle_deref_call(expr: ast.Call, local_sym_tab: Dict, builder: ir.IRBuilder):
"""Handle deref function calls."""
logger.info(f"Handling deref {ast.dump(expr)}")
if len(expr.args) != 1:
logger.info("deref takes exactly one argument")
return None
arg = expr.args[0]
if (
isinstance(arg, ast.Call)
and isinstance(arg.func, ast.Name)
and arg.func.id == "deref"
):
logger.info("Multiple deref not supported")
return None
if isinstance(arg, ast.Name):
if arg.id in local_sym_tab:
arg_ptr = local_sym_tab[arg.id].var
else:
logger.info(f"Undefined variable {arg.id}")
return None
else:
logger.info("Unsupported argument type for deref")
return None
if arg_ptr is None:
logger.info("Failed to evaluate deref argument")
return None
# Load the value from pointer
val = builder.load(arg_ptr)
return val, local_sym_tab[arg.id].ir_type
def eval_expr( def eval_expr(
func, func,
module, module,
@ -17,64 +99,28 @@ def eval_expr(
): ):
logger.info(f"Evaluating expression: {ast.dump(expr)}") logger.info(f"Evaluating expression: {ast.dump(expr)}")
if isinstance(expr, ast.Name): if isinstance(expr, ast.Name):
if expr.id in local_sym_tab: return _handle_name_expr(expr, local_sym_tab, builder)
var = local_sym_tab[expr.id].var
val = builder.load(var)
return val, local_sym_tab[expr.id].ir_type # return value and type
else:
logger.info(f"Undefined variable {expr.id}")
return None
elif isinstance(expr, ast.Constant): elif isinstance(expr, ast.Constant):
if isinstance(expr.value, int): return _handle_constant_expr(expr)
return ir.Constant(ir.IntType(64), expr.value), ir.IntType(64)
elif isinstance(expr.value, bool):
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
else:
logger.info("Unsupported constant type")
return None
elif isinstance(expr, ast.Call): elif isinstance(expr, ast.Call):
if isinstance(expr.func, ast.Name) and expr.func.id == "deref":
return _handle_deref_call(expr, local_sym_tab, builder)
# delayed import to avoid circular dependency # delayed import to avoid circular dependency
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
if isinstance(expr.func, ast.Name): if isinstance(expr.func, ast.Name) and HelperHandlerRegistry.has_handler(
# check deref expr.func.id
if expr.func.id == "deref": ):
logger.info(f"Handling deref {ast.dump(expr)}") return handle_helper_call(
if len(expr.args) != 1: expr,
logger.info("deref takes exactly one argument") module,
return None builder,
arg = expr.args[0] func,
if ( local_sym_tab,
isinstance(arg, ast.Call) map_sym_tab,
and isinstance(arg.func, ast.Name) structs_sym_tab,
and arg.func.id == "deref" )
):
logger.info("Multiple deref not supported")
return None
if isinstance(arg, ast.Name):
if arg.id in local_sym_tab:
arg = local_sym_tab[arg.id].var
else:
logger.info(f"Undefined variable {arg.id}")
return None
if arg is None:
logger.info("Failed to evaluate deref argument")
return None
# Since we are handling only name case, directly take type from sym tab
val = builder.load(arg)
return val, local_sym_tab[expr.args[0].id].ir_type
# check for helpers
if HelperHandlerRegistry.has_handler(expr.func.id):
return handle_helper_call(
expr,
module,
builder,
func,
local_sym_tab,
map_sym_tab,
structs_sym_tab,
)
elif isinstance(expr.func, ast.Attribute): elif isinstance(expr.func, ast.Attribute):
logger.info(f"Handling method call: {ast.dump(expr.func)}") logger.info(f"Handling method call: {ast.dump(expr.func)}")
if isinstance(expr.func.value, ast.Call) and isinstance( if isinstance(expr.func.value, ast.Call) and isinstance(
@ -106,19 +152,7 @@ def eval_expr(
structs_sym_tab, structs_sym_tab,
) )
elif isinstance(expr, ast.Attribute): elif isinstance(expr, ast.Attribute):
if isinstance(expr.value, ast.Name): return _handle_attribute_expr(expr, local_sym_tab, structs_sym_tab, builder)
var_name = expr.value.id
attr_name = expr.attr
if var_name in local_sym_tab:
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
metadata = structs_sym_tab[var_metadata]
if attr_name in metadata.fields:
gep = metadata.gep(builder, var_ptr, attr_name)
val = builder.load(gep)
field_type = metadata.field_type(attr_name)
return val, field_type
logger.info("Unsupported expression evaluation") logger.info("Unsupported expression evaluation")
return None return None

View File

@ -233,9 +233,7 @@ def handle_assign(
else: else:
logger.info("Unsupported assignment call function type") logger.info("Unsupported assignment call function type")
elif isinstance(rval, ast.BinOp): elif isinstance(rval, ast.BinOp):
handle_binary_op( handle_binary_op(rval, module, builder, var_name, local_sym_tab)
rval, module, builder, var_name, local_sym_tab, map_sym_tab, func
)
else: else:
logger.info("Unsupported assignment value type") logger.info("Unsupported assignment value type")

View File

@ -3,9 +3,9 @@ from ctypes import c_void_p, c_int64
@bpf @bpf
@section("sometag1") @section("tracepoint/syscalls/sys_enter_sync")
def sometag(ctx: c_void_p) -> c_int64: def sometag(ctx: c_void_p) -> c_int64:
a = 1 + 2 + 1 a = 1 + 2 + 1 + 12 + 13
print(f"{a}") print(f"{a}")
return c_int64(0) return c_int64(0)

View File

@ -3,11 +3,12 @@ from ctypes import c_void_p, c_int64
@bpf @bpf
@section("sometag1") @section("tracepoint/syscalls/sys_enter_sync")
def sometag(ctx: c_void_p) -> c_int64: def sometag(ctx: c_void_p) -> c_int64:
b = 1 + 2 b = 1 + 2
a = 1 + b a = 1 + b
return c_int64(a) print(f"{a}")
return c_int64(0)
@bpf @bpf