mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +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:
@ -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