mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Route every integer widening and narrowing through convert()
The same widen-to-i64 / truncate-to-slot logic was written out by hand in six places: local assignment, struct-field assignment, augmented assignment, helper-argument temporaries, binary-operation operands, and the comparison normaliser. Each now calls type_normalization.convert, which is the single point where the sign decides between zext and sext. Across the 67-program corpus this changes exactly one emitted instruction, and it is a correction rather than a regression: augmented assignment on a c_uint32 struct field now widens the field's current value with zext instead of sext, because that slot's descriptor already carries its sign from ctypes_to_ir. Every other site still sees plain (signed-by-default) descriptors and is byte-identical. The remaining hand-written widenings -- the printk formatter, the vmlinux field loader, return handling -- have their own rules and are migrated together with the sign sources. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
@ -1,5 +1,10 @@
|
||||
from .expr_pass import eval_expr, handle_expr, get_operand_value
|
||||
from .type_normalization import convert_to_bool, get_base_type_and_depth
|
||||
from .type_normalization import (
|
||||
convert_to_bool,
|
||||
get_base_type_and_depth,
|
||||
convert,
|
||||
canonicalise,
|
||||
)
|
||||
from .ir_ops import deref_to_depth, access_struct_field
|
||||
from .operators import apply_binop
|
||||
from .call_registry import CallHandlerRegistry
|
||||
@ -9,6 +14,8 @@ __all__ = [
|
||||
"eval_expr",
|
||||
"handle_expr",
|
||||
"convert_to_bool",
|
||||
"convert",
|
||||
"canonicalise",
|
||||
"get_base_type_and_depth",
|
||||
"deref_to_depth",
|
||||
"apply_binop",
|
||||
|
||||
@ -9,6 +9,7 @@ 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 .type_normalization import (
|
||||
convert,
|
||||
convert_to_bool,
|
||||
handle_comparator,
|
||||
get_base_type_and_depth,
|
||||
@ -235,10 +236,8 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t
|
||||
# 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.
|
||||
if isinstance(left.type, ir.IntType) and left.type.width < 64:
|
||||
left = builder.sext(left, ir.IntType(64))
|
||||
if isinstance(right.type, ir.IntType) and right.type.width < 64:
|
||||
right = builder.sext(right, ir.IntType(64))
|
||||
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)
|
||||
|
||||
@ -23,9 +23,9 @@ def _normalize_types(func, builder, lhs, rhs):
|
||||
logger.info(f"Normalizing types: {lhs.type} vs {rhs.type}")
|
||||
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
|
||||
if lhs.type.width < rhs.type.width:
|
||||
lhs = builder.sext(lhs, rhs.type)
|
||||
lhs = convert(builder, lhs, lhs.type, rhs.type)
|
||||
else:
|
||||
rhs = builder.sext(rhs, lhs.type)
|
||||
rhs = convert(builder, rhs, rhs.type, lhs.type)
|
||||
return lhs, rhs
|
||||
elif not isinstance(lhs.type, ir.PointerType) and not isinstance(
|
||||
rhs.type, ir.PointerType
|
||||
|
||||
Reference in New Issue
Block a user