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:
Pragyansh Chaturvedi
2026-09-04 10:14:38 +05:30
parent daf429d463
commit 4fa6fc59d4
6 changed files with 23 additions and 31 deletions

View File

@ -13,6 +13,7 @@ from pythonbpf.expr import (
convert_to_bool,
get_operand_value,
apply_binop,
convert,
VmlinuxHandlerRegistry,
)
from pythonbpf.assign_pass import (
@ -281,13 +282,10 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
)
# Same width discipline as binary-op evaluation: compute in i64, narrow
# back to the slot's width on the way out.
if current.type.width < 64:
current = builder.sext(current, ir.IntType(64))
if isinstance(rhs.type, ir.IntType) and rhs.type.width < 64:
rhs = builder.sext(rhs, ir.IntType(64))
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)
if result.type.width > slot_type.width:
result = builder.trunc(result, slot_type)
result = convert(builder, result, result.type, slot_type)
builder.store(result, slot)