mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Core: Choose signed or unsigned operators and predicates from the promoted type
The operator table now carries both variants for the sign-sensitive operations: / and // -> sdiv/udiv, % -> srem/urem, >> -> ashr/lshr. The ring operations are unchanged since two's complement makes their low bits sign-blind. apply_binop picks the variant from the promoted type computed by the usual arithmetic conversions, so uint32(10) / int32(-2) is a udiv in u32 (0, as C) rather than a signed division (-5). Comparisons between two integers now go through the same promotion and pick icmp_signed or icmp_unsigned from the promoted type, so uint64(10) > int64(-1) is icmp ugt (false, as C). Pointer and struct comparisons keep the depth-normalising path unchanged. Corpus: only an XDP test comparing c_uint context fields changes, to an unsigned predicate. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
@ -185,9 +185,13 @@ def _handle_deref_call(expr: ast.Call, local_sym_tab: Dict, builder: ir.IRBuilde
|
||||
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))
|
||||
descriptor (an IntTy, a vmlinux Field, or plain -> signed). None when the
|
||||
value is not an integer at all."""
|
||||
if isinstance(ty, ir.IntType):
|
||||
return IntTy(ty.width, signedness(ty))
|
||||
if val is not None and isinstance(val.type, ir.IntType):
|
||||
return IntTy(val.type.width, signedness(ty))
|
||||
return None
|
||||
|
||||
|
||||
def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab):
|
||||
@ -263,7 +267,7 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t
|
||||
)
|
||||
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)
|
||||
result = apply_binop(builder, op, left, right, signedness(result_ty))
|
||||
return canonicalise(builder, result, result_ty), result_ty
|
||||
|
||||
|
||||
@ -373,8 +377,19 @@ def _handle_compare(func, compilation_context, builder, cond, local_sym_tab):
|
||||
logger.error("Failed to evaluate comparison operands")
|
||||
return None
|
||||
|
||||
lhs, _ = lhs
|
||||
rhs, _ = rhs
|
||||
lhs, lhs_ty = lhs
|
||||
rhs, rhs_ty = rhs
|
||||
lhs_desc, rhs_desc = _descriptor(lhs, lhs_ty), _descriptor(rhs, rhs_ty)
|
||||
if lhs_desc is not None and rhs_desc is not None:
|
||||
# Both integers: compare in the promoted type, which also picks the
|
||||
# signed or unsigned predicate (u64 > s64 is an unsigned compare in C).
|
||||
cmp_ty = usual_arithmetic_conversions(lhs_desc, rhs_desc)
|
||||
lhs = to_promoted(builder, lhs, lhs_desc, cmp_ty)
|
||||
rhs = to_promoted(builder, rhs, rhs_desc, cmp_ty)
|
||||
return handle_comparator(
|
||||
func, builder, cond.ops[0], lhs, rhs, signed=signedness(cmp_ty)
|
||||
)
|
||||
# Pointers and struct values: the depth-normalising path
|
||||
return handle_comparator(func, builder, cond.ops[0], lhs, rhs)
|
||||
|
||||
|
||||
|
||||
@ -11,20 +11,24 @@ import ast
|
||||
|
||||
from pythonbpf.type_deducer import IntTy, signedness
|
||||
|
||||
# ast.BinOp.op class -> llvmlite IRBuilder method name.
|
||||
# Shared by binary-op evaluation and augmented assignment.
|
||||
# ast.BinOp.op class -> (signed IRBuilder method, unsigned IRBuilder method).
|
||||
# Shared by binary-op evaluation and augmented assignment. The ring operations
|
||||
# are sign-blind (two's complement gives identical low bits); division,
|
||||
# remainder and right shift are not, and the operation's type decides. `/` and
|
||||
# `//` are the same C truncating division -- Python's floor semantics for `//`
|
||||
# and `%` on negatives are a documented divergence.
|
||||
BINOP_METHODS = {
|
||||
ast.Add: "add",
|
||||
ast.Sub: "sub",
|
||||
ast.Mult: "mul",
|
||||
ast.Div: "sdiv",
|
||||
ast.Mod: "srem",
|
||||
ast.LShift: "shl",
|
||||
ast.RShift: "lshr",
|
||||
ast.BitOr: "or_",
|
||||
ast.BitXor: "xor",
|
||||
ast.BitAnd: "and_",
|
||||
ast.FloorDiv: "udiv",
|
||||
ast.Add: ("add", "add"),
|
||||
ast.Sub: ("sub", "sub"),
|
||||
ast.Mult: ("mul", "mul"),
|
||||
ast.Div: ("sdiv", "udiv"),
|
||||
ast.FloorDiv: ("sdiv", "udiv"),
|
||||
ast.Mod: ("srem", "urem"),
|
||||
ast.LShift: ("shl", "shl"),
|
||||
ast.RShift: ("ashr", "lshr"),
|
||||
ast.BitOr: ("or_", "or_"),
|
||||
ast.BitXor: ("xor", "xor"),
|
||||
ast.BitAnd: ("and_", "and_"),
|
||||
}
|
||||
|
||||
# ast.Compare op class -> icmp predicate string.
|
||||
@ -44,12 +48,13 @@ UNARY_OPS = (ast.Not, ast.USub)
|
||||
BOOL_OPS = (ast.And, ast.Or)
|
||||
|
||||
|
||||
def apply_binop(builder, op, left, right):
|
||||
"""Emit the LLVM instruction for a Python binary operator."""
|
||||
method = BINOP_METHODS.get(type(op))
|
||||
if method is None:
|
||||
def apply_binop(builder, op, left, right, signed=True):
|
||||
"""Emit the LLVM instruction for a Python binary operator, in the signed or
|
||||
unsigned form the operation's type calls for."""
|
||||
methods = BINOP_METHODS.get(type(op))
|
||||
if methods is None:
|
||||
raise SyntaxError(f"Unsupported binary operation: {type(op).__name__}")
|
||||
return getattr(builder, method)(left, right)
|
||||
return getattr(builder, methods[0] if signed else methods[1])(left, right)
|
||||
|
||||
|
||||
def comparison_predicate(op):
|
||||
|
||||
@ -111,8 +111,8 @@ def convert_to_bool(builder, val):
|
||||
return builder.icmp_signed("!=", val, zero)
|
||||
|
||||
|
||||
def handle_comparator(func, builder, op, lhs, rhs):
|
||||
"""Handle comparison operations."""
|
||||
def handle_comparator(func, builder, op, lhs, rhs, signed=True):
|
||||
"""Handle comparison operations, signed or unsigned per the compared type."""
|
||||
|
||||
if lhs.type != rhs.type:
|
||||
lhs, rhs = _normalize_types(func, builder, lhs, rhs)
|
||||
@ -125,6 +125,7 @@ def handle_comparator(func, builder, op, lhs, rhs):
|
||||
return None
|
||||
|
||||
predicate = COMPARISON_OPS[type(op)]
|
||||
result = builder.icmp_signed(predicate, lhs, rhs)
|
||||
icmp = builder.icmp_signed if signed else builder.icmp_unsigned
|
||||
result = icmp(predicate, lhs, rhs)
|
||||
logger.debug(f"Comparison result: {result}")
|
||||
return result, ir.IntType(1)
|
||||
|
||||
Reference in New Issue
Block a user