mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Type each binary operation per node with C's arithmetic conversions
Arithmetic is now typed bottom-up through the expression tree the way C does it. get_typed_operand evaluates an operand to (value, IntTy); a binary node computes its type with usual_arithmetic_conversions, converts each operand to that type per the operand's own sign (source-driven, folding literals), runs the operation in the i64 working register, and narrows the result back to the node's type. That last step is what makes a u32 * u32 wrap at 32 bits before it is widened into a u64, exactly as clang emits (mul i32, zext) -- verified against tests/c-form/signedness.bpf.c. There is no expression-wide signed or unsigned mode; each node decides from its operands, and the assignment target only converts the finished result. Augmented assignment is typed identically (x op= v is x = x op v), unary minus negates in the operand's promoted type (2^N - x for unsigned), and return statements now convert the value to the declared return type instead of emitting whatever width the expression happened to have. Corpus: 13 programs change; with SSA numbering normalised every changed line is a trunc/sext/zext inserted by canonicalisation where an operation's type is narrower than 64 bits (int-typed literals and c_int32/c_uint32 operands). No program changes compile status. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
from .expr_pass import eval_expr, handle_expr, get_operand_value
|
||||
from .expr_pass import eval_expr, handle_expr, get_operand_value, get_typed_operand
|
||||
from .type_normalization import (
|
||||
convert_to_bool,
|
||||
get_base_type_and_depth,
|
||||
convert,
|
||||
canonicalise,
|
||||
to_promoted,
|
||||
)
|
||||
from .operators import usual_arithmetic_conversions
|
||||
from .ir_ops import deref_to_depth, access_struct_field
|
||||
from .operators import apply_binop
|
||||
from .call_registry import CallHandlerRegistry
|
||||
@ -16,6 +18,9 @@ __all__ = [
|
||||
"convert_to_bool",
|
||||
"convert",
|
||||
"canonicalise",
|
||||
"to_promoted",
|
||||
"get_typed_operand",
|
||||
"usual_arithmetic_conversions",
|
||||
"get_base_type_and_depth",
|
||||
"deref_to_depth",
|
||||
"apply_binop",
|
||||
|
||||
@ -4,12 +4,14 @@ from logging import Logger
|
||||
import logging
|
||||
from typing import Dict
|
||||
|
||||
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes, IntTy
|
||||
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes, IntTy, signedness
|
||||
from .call_registry import CallHandlerRegistry
|
||||
from .ir_ops import deref_to_depth, access_struct_field
|
||||
from .operators import apply_binop, UNARY_OPS, BOOL_OPS
|
||||
from .operators import apply_binop, usual_arithmetic_conversions, UNARY_OPS, BOOL_OPS
|
||||
from .type_normalization import (
|
||||
convert,
|
||||
to_promoted,
|
||||
canonicalise,
|
||||
convert_to_bool,
|
||||
handle_comparator,
|
||||
get_base_type_and_depth,
|
||||
@ -180,71 +182,89 @@ def _handle_deref_call(expr: ast.Call, local_sym_tab: Dict, builder: ir.IRBuilde
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def get_operand_value(func, compilation_context, operand, builder, local_sym_tab):
|
||||
"""Extract the value from an operand, handling variables and constants."""
|
||||
logger.info(f"Getting operand value for: {ast.dump(operand)}")
|
||||
def _descriptor(val, ty):
|
||||
"""IntTy descriptor for an evaluated integer value: width from the physical
|
||||
value unless the descriptor is itself an integer type, sign from the
|
||||
descriptor (an IntTy, a vmlinux Field, or plain -> signed)."""
|
||||
width = ty.width if isinstance(ty, ir.IntType) else val.type.width
|
||||
return IntTy(width, signedness(ty))
|
||||
|
||||
|
||||
def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab):
|
||||
"""Evaluate an operand to (value, IntTy). Pointers (map-lookup results) are
|
||||
dereferenced to the scalar they point at."""
|
||||
logger.info(f"Getting typed operand for: {ast.dump(operand)}")
|
||||
if isinstance(operand, ast.Name):
|
||||
if operand.id in local_sym_tab:
|
||||
var = local_sym_tab[operand.id].var
|
||||
var_type = var.type
|
||||
base_type, depth = get_base_type_and_depth(var_type)
|
||||
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
|
||||
if depth == 1:
|
||||
val = builder.load(var)
|
||||
return val
|
||||
else:
|
||||
val = deref_to_depth(func, builder, var, depth)
|
||||
return val
|
||||
sym = local_sym_tab[operand.id]
|
||||
var = sym.var
|
||||
base_type, depth = get_base_type_and_depth(var.type)
|
||||
val = (
|
||||
builder.load(var)
|
||||
if depth == 1
|
||||
else deref_to_depth(func, builder, var, depth)
|
||||
)
|
||||
return val, _descriptor(val, sym.ir_type if depth == 1 else base_type)
|
||||
elif operand.id in compilation_context.bpf_globals:
|
||||
# A @bpfglobal: plain load off the global symbol.
|
||||
return builder.load(compilation_context.bpf_globals[operand.id].var)
|
||||
sym = compilation_context.bpf_globals[operand.id]
|
||||
return builder.load(sym.var), _descriptor(None, sym.ir_type)
|
||||
else:
|
||||
# Check if it's a vmlinux enum/constant
|
||||
vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id)
|
||||
if vmlinux_result is not None:
|
||||
val, _ = vmlinux_result
|
||||
return val
|
||||
return val, IntTy(64, True)
|
||||
elif isinstance(operand, ast.Constant):
|
||||
if isinstance(operand.value, int):
|
||||
cst = ir.Constant(ir.IntType(64), int(operand.value))
|
||||
return cst
|
||||
if isinstance(operand.value, (int, bool)):
|
||||
v = int(operand.value)
|
||||
lit_ty = IntTy(32, True) if -(1 << 31) <= v < (1 << 31) else IntTy(64, True)
|
||||
return ir.Constant(ir.IntType(64), v), lit_ty
|
||||
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
|
||||
elif isinstance(operand, ast.BinOp):
|
||||
res = _handle_binary_op_impl(
|
||||
return _handle_binary_op_impl(
|
||||
func, compilation_context, operand, builder, local_sym_tab
|
||||
)
|
||||
return res
|
||||
else:
|
||||
res = eval_expr(func, compilation_context, builder, operand, local_sym_tab)
|
||||
if res is None:
|
||||
raise ValueError(f"Failed to evaluate call expression: {operand}")
|
||||
val, _ = res
|
||||
val, ty = res
|
||||
logger.info(f"Evaluated expr to {val} of type {val.type}")
|
||||
base_type, depth = get_base_type_and_depth(val.type)
|
||||
if depth > 0:
|
||||
val = deref_to_depth(func, builder, val, depth)
|
||||
return val
|
||||
return val, _descriptor(val, ty)
|
||||
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
||||
|
||||
|
||||
def get_operand_value(func, compilation_context, operand, builder, local_sym_tab):
|
||||
"""Extract the value from an operand, handling variables and constants."""
|
||||
return get_typed_operand(
|
||||
func, compilation_context, operand, builder, local_sym_tab
|
||||
)[0]
|
||||
|
||||
|
||||
def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_tab):
|
||||
"""A binary operation, typed per node the way C types it: the operation is
|
||||
performed in the type given by the usual arithmetic conversions of its two
|
||||
operands, each operand converted to that type first, and the result
|
||||
narrowed to it -- so u32 * u32 wraps at 32 bits even though the arithmetic
|
||||
itself runs in an i64 register. Returns (value, IntTy)."""
|
||||
op = rval.op
|
||||
left = get_operand_value(
|
||||
left, left_ty = get_typed_operand(
|
||||
func, compilation_context, rval.left, builder, local_sym_tab
|
||||
)
|
||||
right = get_operand_value(
|
||||
right, right_ty = get_typed_operand(
|
||||
func, compilation_context, rval.right, builder, local_sym_tab
|
||||
)
|
||||
logger.info(f"left is {left}, right is {right}, op is {op}")
|
||||
|
||||
# NOTE: Before doing the operation, if the operands are integers
|
||||
# we always extend them to i64. The assignment to LHS will take
|
||||
# care of truncation if needed.
|
||||
left = convert(builder, left, left.type, ir.IntType(64))
|
||||
right = convert(builder, right, right.type, ir.IntType(64))
|
||||
|
||||
# Map AST operation nodes to LLVM IR builder methods
|
||||
return apply_binop(builder, op, left, right)
|
||||
result_ty = usual_arithmetic_conversions(left_ty, right_ty)
|
||||
logger.info(
|
||||
f"binop {type(op).__name__}: {left_ty.describe()} x {right_ty.describe()} "
|
||||
f"-> {result_ty.describe()}"
|
||||
)
|
||||
left = to_promoted(builder, left, left_ty, result_ty)
|
||||
right = to_promoted(builder, right, right_ty, result_ty)
|
||||
result = apply_binop(builder, op, left, right)
|
||||
return canonicalise(builder, result, result_ty), result_ty
|
||||
|
||||
|
||||
def _handle_binary_op(
|
||||
@ -255,15 +275,14 @@ def _handle_binary_op(
|
||||
var_name,
|
||||
local_sym_tab,
|
||||
):
|
||||
result = _handle_binary_op_impl(
|
||||
result, result_ty = _handle_binary_op_impl(
|
||||
func, compilation_context, rval, builder, local_sym_tab
|
||||
)
|
||||
if var_name and var_name in local_sym_tab:
|
||||
logger.info(
|
||||
f"Storing result {result} into variable {local_sym_tab[var_name].var}"
|
||||
)
|
||||
builder.store(result, local_sym_tab[var_name].var)
|
||||
return result, result.type
|
||||
slot = local_sym_tab[var_name]
|
||||
logger.info(f"Storing result {result} into variable {slot.var}")
|
||||
builder.store(convert(builder, result, result_ty, slot.ir_type), slot.var)
|
||||
return result, result_ty
|
||||
|
||||
|
||||
# ============================================================================
|
||||
@ -371,7 +390,7 @@ def _handle_unary_op(
|
||||
logger.error("Only 'not' and '-' unary operators are supported")
|
||||
return None
|
||||
|
||||
operand = get_operand_value(
|
||||
operand, operand_ty = get_typed_operand(
|
||||
func, compilation_context, expr.operand, builder, local_sym_tab
|
||||
)
|
||||
if operand is None:
|
||||
@ -383,10 +402,12 @@ def _handle_unary_op(
|
||||
result = builder.xor(convert_to_bool(builder, operand), true_const)
|
||||
return result, ir.IntType(1)
|
||||
elif isinstance(expr.op, ast.USub):
|
||||
# Multiply by -1
|
||||
neg_one = ir.Constant(ir.IntType(64), -1)
|
||||
result = builder.mul(operand, neg_one)
|
||||
return result, ir.IntType(64)
|
||||
# Negation happens in the operand's promoted type; for an unsigned
|
||||
# operand that is C's 2^N - x, which the narrowing produces.
|
||||
result_ty = usual_arithmetic_conversions(operand_ty, operand_ty)
|
||||
operand = to_promoted(builder, operand, operand_ty, result_ty)
|
||||
result = builder.mul(operand, ir.Constant(ir.IntType(64), -1))
|
||||
return canonicalise(builder, result, result_ty), result_ty
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@ -62,12 +62,36 @@ def convert(builder, val, from_ty, to_ty):
|
||||
return val
|
||||
|
||||
|
||||
def _fold_int_constant(val, ty, width):
|
||||
"""A literal re-expressed at the working width holding type ty's value:
|
||||
wrap to ty's width, take the representative ty's sign implies."""
|
||||
v = val.constant % (1 << ty.width)
|
||||
if signedness(ty) and v >= 1 << (ty.width - 1):
|
||||
v -= 1 << ty.width
|
||||
return ir.Constant(ir.IntType(width), v)
|
||||
|
||||
|
||||
def to_promoted(builder, val, from_ty, to_ty, width=64):
|
||||
"""Bring an operand to the promoted type of its operation, C-style.
|
||||
|
||||
First convert it to to_ty per its *own* sign (that is C's conversion of an
|
||||
operand to the common type), then widen to the working width per to_ty's
|
||||
sign so the i64 register holds exactly a to_ty value. Literals are folded.
|
||||
"""
|
||||
if isinstance(val, ir.Constant) and isinstance(val.constant, int):
|
||||
return _fold_int_constant(val, to_ty, width)
|
||||
val = convert(builder, val, from_ty, ir.IntType(to_ty.width))
|
||||
return canonicalise(builder, val, to_ty, width)
|
||||
|
||||
|
||||
def canonicalise(builder, val, ty, width=64):
|
||||
"""Bring `val` to the working width holding exactly the value of type `ty`:
|
||||
truncate to ty's width if the register is wider (so the operation wraps at
|
||||
ty's width, as C does), then extend per ty's sign."""
|
||||
if not isinstance(val.type, ir.IntType):
|
||||
return val
|
||||
if isinstance(val, ir.Constant) and isinstance(val.constant, int):
|
||||
return _fold_int_constant(val, ty, width)
|
||||
if val.type.width > ty.width:
|
||||
val = builder.trunc(val, ir.IntType(ty.width))
|
||||
if val.type.width < width:
|
||||
|
||||
@ -11,9 +11,12 @@ from pythonbpf.expr import (
|
||||
eval_expr,
|
||||
handle_expr,
|
||||
convert_to_bool,
|
||||
get_operand_value,
|
||||
get_typed_operand,
|
||||
apply_binop,
|
||||
convert,
|
||||
to_promoted,
|
||||
canonicalise,
|
||||
usual_arithmetic_conversions,
|
||||
VmlinuxHandlerRegistry,
|
||||
)
|
||||
from pythonbpf.assign_pass import (
|
||||
@ -273,20 +276,22 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
|
||||
# Python evaluates the target's current value before the right-hand side.
|
||||
current = builder.load(slot)
|
||||
rhs = get_operand_value(
|
||||
rhs, rhs_ty = get_typed_operand(
|
||||
func, compilation_context, stmt.value, builder, local_sym_tab
|
||||
)
|
||||
if rhs is None:
|
||||
raise SyntaxError(
|
||||
f"Failed to evaluate augmented-assignment value: {ast.dump(stmt.value)}"
|
||||
)
|
||||
# Same width discipline as binary-op evaluation: compute in i64, narrow
|
||||
# back to the slot's width on the way out.
|
||||
current = convert(builder, current, slot_type, ir.IntType(64))
|
||||
rhs = convert(builder, rhs, rhs.type, ir.IntType(64))
|
||||
result = apply_binop(builder, stmt.op, current, rhs)
|
||||
result = convert(builder, result, result.type, slot_type)
|
||||
builder.store(result, slot)
|
||||
# x op= v is typed exactly like x = x op v: operate in the promoted type,
|
||||
# then convert the result to the target's type on the way back in.
|
||||
result_ty = usual_arithmetic_conversions(slot_type, rhs_ty)
|
||||
current = to_promoted(builder, current, slot_type, result_ty)
|
||||
rhs = to_promoted(builder, rhs, rhs_ty, result_ty)
|
||||
result = canonicalise(
|
||||
builder, apply_binop(builder, stmt.op, current, rhs), result_ty
|
||||
)
|
||||
builder.store(convert(builder, result, result_ty, slot_type), slot)
|
||||
|
||||
|
||||
def handle_cond(func, compilation_context, builder, cond, local_sym_tab):
|
||||
@ -367,7 +372,9 @@ def handle_return(builder, stmt, local_sym_tab, ret_type, compilation_context=No
|
||||
local_sym_tab=local_sym_tab,
|
||||
)
|
||||
logger.info(f"Evaluated return expression to {val}")
|
||||
builder.ret(val[0])
|
||||
# The declared return type is the LHS of an implicit assignment:
|
||||
# widen per the value's sign, truncate if narrower.
|
||||
builder.ret(convert(builder, val[0], val[1], ret_type))
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user