mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Add the signedness machinery (no behaviour change)
LLVM integer types are sign-agnostic; the sign lives in the operations. The frontend therefore has to carry it, and until now it did not: c_uint32 became a bare i32 at declaration and every later choice (sext, sdiv, icmp_signed) assumed signed. This commit adds the pieces without changing any emitted IR: - type_deducer.IntTy, an ir.IntType subclass carrying 'signed'. It renders, compares and hashes as the plain type, so the 40 isinstance checks and every == ir.IntType(64) comparison keep working; signedness() reads it and treats a plain IntType as signed (today's behaviour). Constructed outside IntType's per-width instance cache, which is shared with subclasses and would have merged the two signs of a width into one object. - ctypes_to_ir now returns IntTy, which types five declaration kinds at once: locals from ctypes constructors, struct fields, globals, context annotations, and map key/value types. is_signed_ctype replaces the private copy in globals_pass. - operators.usual_arithmetic_conversions: C's rule for the type a binary operation is performed in. - type_normalization.convert (source-driven zext/sext when widening, trunc when narrowing) and canonicalise (bring a value to the working width holding exactly a given type's value). Nothing consults the sign yet. The corpus of 67 compilable test programs produces byte-identical .ll before and after. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
@ -9,6 +9,8 @@ in this file, the compiler does not support it. Add new operators here first.
|
||||
|
||||
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.
|
||||
BINOP_METHODS = {
|
||||
@ -53,3 +55,27 @@ def apply_binop(builder, op, left, right):
|
||||
def comparison_predicate(op):
|
||||
"""icmp predicate for a Python comparison operator, or None if unsupported."""
|
||||
return COMPARISON_OPS.get(type(op))
|
||||
|
||||
|
||||
def usual_arithmetic_conversions(left, right) -> IntTy:
|
||||
"""The type a C binary operation on `left` and `right` is performed in.
|
||||
|
||||
Integer promotion first: anything narrower than int becomes a signed 32-bit
|
||||
int (int can represent every value of the narrower type, signed or not).
|
||||
Then, if the signs agree, the wider type wins; if they differ, the unsigned
|
||||
operand wins at equal or greater width, otherwise the signed one -- because
|
||||
it can then represent every value of the unsigned one.
|
||||
"""
|
||||
|
||||
def promote(ty):
|
||||
if ty.width < 32:
|
||||
return IntTy(32, True)
|
||||
return IntTy(ty.width, signedness(ty))
|
||||
|
||||
left, right = promote(left), promote(right)
|
||||
if left.signed == right.signed:
|
||||
return IntTy(max(left.width, right.width), left.signed)
|
||||
unsigned, signed = (left, right) if not left.signed else (right, left)
|
||||
if unsigned.width >= signed.width:
|
||||
return IntTy(unsigned.width, False)
|
||||
return IntTy(signed.width, True)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import logging
|
||||
from llvmlite import ir
|
||||
from .ir_ops import deref_to_depth
|
||||
from pythonbpf.type_deducer import signedness
|
||||
from .operators import COMPARISON_OPS
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -42,6 +43,39 @@ def _normalize_types(func, builder, lhs, rhs):
|
||||
return _normalize_types(func, builder, lhs, rhs)
|
||||
|
||||
|
||||
def convert(builder, val, from_ty, to_ty):
|
||||
"""Convert an integer value between types the way C does.
|
||||
|
||||
Widening is driven by the *source* sign (zext for unsigned, sext for
|
||||
signed) so the mathematical value is preserved; narrowing truncates; equal
|
||||
width is a reinterpretation and emits nothing. `from_ty` and `to_ty` are
|
||||
descriptors (see type_deducer.IntTy); the physical width comes from the
|
||||
value itself, which may already be wider than its descriptor says.
|
||||
"""
|
||||
if not (isinstance(to_ty, ir.IntType) and isinstance(val.type, ir.IntType)):
|
||||
return val
|
||||
if val.type.width > to_ty.width:
|
||||
return builder.trunc(val, to_ty)
|
||||
if val.type.width < to_ty.width:
|
||||
ext = builder.zext if not signedness(from_ty) else builder.sext
|
||||
return ext(val, to_ty)
|
||||
return val
|
||||
|
||||
|
||||
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 val.type.width > ty.width:
|
||||
val = builder.trunc(val, ir.IntType(ty.width))
|
||||
if val.type.width < width:
|
||||
ext = builder.zext if not signedness(ty) else builder.sext
|
||||
val = ext(val, ir.IntType(width))
|
||||
return val
|
||||
|
||||
|
||||
def convert_to_bool(builder, val):
|
||||
"""Convert a value to boolean."""
|
||||
if val.type == ir.IntType(1):
|
||||
|
||||
Reference in New Issue
Block a user