From daf429d46386325ce140a6bfdf0ea6bc6ce19473 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 4 Sep 2026 10:08:48 +0530 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU --- pythonbpf/expr/operators.py | 26 ++++++ pythonbpf/expr/type_normalization.py | 34 ++++++++ pythonbpf/globals_pass.py | 16 +--- pythonbpf/type_deducer.py | 119 ++++++++++++++++++++++----- 4 files changed, 159 insertions(+), 36 deletions(-) diff --git a/pythonbpf/expr/operators.py b/pythonbpf/expr/operators.py index fb4cadd..8925a12 100644 --- a/pythonbpf/expr/operators.py +++ b/pythonbpf/expr/operators.py @@ -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) diff --git a/pythonbpf/expr/type_normalization.py b/pythonbpf/expr/type_normalization.py index edea422..f939f84 100644 --- a/pythonbpf/expr/type_normalization.py +++ b/pythonbpf/expr/type_normalization.py @@ -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): diff --git a/pythonbpf/globals_pass.py b/pythonbpf/globals_pass.py index aabe8fd..c5dc0ba 100644 --- a/pythonbpf/globals_pass.py +++ b/pythonbpf/globals_pass.py @@ -3,7 +3,7 @@ import ast from logging import Logger import logging -from .type_deducer import ctypes_to_ir +from .type_deducer import ctypes_to_ir, is_signed_ctype from .symbols import BpfGlobalSymbol from .debuginfo import DebugInfoGenerator from .expr import VmlinuxHandlerRegistry @@ -11,18 +11,6 @@ from .debuginfo import dwarf_constants as dc logger: Logger = logging.getLogger(__name__) -_SIGNED_CTYPES = { - "c_int8", - "c_int16", - "c_int32", - "c_int64", - "c_int", - "c_short", - "c_long", - "c_longlong", - "c_byte", -} - _C_NAME_BY_WIDTH = {8: "char", 16: "short", 32: "int", 64: "long long"} @@ -106,7 +94,7 @@ def _emit_global_debug_info(compilation_context, gvar, name, ctype_name): """ generator = DebugInfoGenerator(compilation_context.module) width = gvar.value_type.width - signed = ctype_name in _SIGNED_CTYPES + signed = is_signed_ctype(ctype_name) base = _C_NAME_BY_WIDTH[width] if width == 8: encoding = dc.DW_ATE_signed_char if signed else dc.DW_ATE_unsigned_char diff --git a/pythonbpf/type_deducer.py b/pythonbpf/type_deducer.py index 2e4c77f..94189a4 100644 --- a/pythonbpf/type_deducer.py +++ b/pythonbpf/type_deducer.py @@ -1,30 +1,105 @@ from llvmlite import ir + +class IntTy(ir.IntType): + """An LLVM integer type that also remembers its signedness. + + LLVM integer types are sign-agnostic by design: `i32` is just 32 bits, and + the sign lives in the operations (sdiv/udiv, sext/zext, icmp s*/u*). The + frontend therefore has to carry it. IntTy is a plain ir.IntType for every + purpose LLVM cares about -- it renders as `i32`, compares and hashes equal + to ir.IntType(32), and passes every isinstance check -- with one extra + attribute the compiler reads when choosing between signed and unsigned + forms of an operation. + + Invariant: the sign is read only from a *descriptor* -- a Symbol.ir_type + or the type half of an eval_expr result -- never from `value.type`. Values + produced by the IRBuilder (loads, arithmetic, extensions) come back with a + plain ir.IntType, so a sign on a value's own type is lost at the first + operation. Descriptors are constructed by the compiler; that is where the + sign lives. + """ + + def __new__(cls, bits: int, signed: bool = True): + # ir.IntType.__new__ memoises one instance per width in a cache shared + # with subclasses. Going through it would (a) merge the signed and + # unsigned flavours of a width into one object and (b) plant an IntTy + # in the cache so that ir.IntType(32) itself started returning one. + # Construct directly instead; equality and hashing are inherited and + # depend only on the width, so an IntTy still compares equal to i32. + self = object.__new__(cls) + self.width = bits + return self + + def __init__(self, bits: int, signed: bool = True): + self.signed = signed + + def __getnewargs__(self): + return self.width, self.signed + + def describe(self) -> str: + return f"{'i' if self.signed else 'u'}{self.width}" + + +def signedness(ty) -> bool: + """Sign of an integer type descriptor. Plain ir.IntType (a site that has not + been taught to carry a sign yet) reads as signed, which is the compiler's + historical behaviour.""" + return getattr(ty, "signed", True) + + +_SIGNED_CTYPES = { + "c_int8", + "c_int16", + "c_int32", + "c_int64", + "c_int", + "c_short", + "c_long", + "c_longlong", + "c_byte", +} + +_INT_CTYPE_WIDTHS = { + "c_int8": 8, + "c_uint8": 8, + "c_byte": 8, + "c_ubyte": 8, + "c_int16": 16, + "c_uint16": 16, + "c_short": 16, + "c_ushort": 16, + "c_int32": 32, + "c_uint32": 32, + "c_int": 32, + "c_uint": 32, + "c_int64": 64, + "c_uint64": 64, + "c_long": 64, + "c_ulong": 64, + "c_longlong": 64, + # A pointer-sized integer; treated as unsigned like uintptr_t. + "c_void_p": 64, +} + + +def is_signed_ctype(ctype: str) -> bool: + return ctype in _SIGNED_CTYPES + + # TODO: THIS IS NOT SUPPOSED TO MATCH STRINGS :skull: mapping = { - "c_int8": ir.IntType(8), - "c_uint8": ir.IntType(8), - "c_int16": ir.IntType(16), - "c_uint16": ir.IntType(16), - "c_int32": ir.IntType(32), - "c_uint32": ir.IntType(32), - "c_int64": ir.IntType(64), - "c_uint64": ir.IntType(64), - "c_float": ir.FloatType(), - "c_double": ir.DoubleType(), - "c_void_p": ir.IntType(64), - "c_long": ir.IntType(64), - "c_ulong": ir.IntType(64), - "c_longlong": ir.IntType(64), - "c_uint": ir.IntType(32), - "c_int": ir.IntType(32), - "c_ushort": ir.IntType(16), - "c_short": ir.IntType(16), - "c_ubyte": ir.IntType(8), - "c_byte": ir.IntType(8), - # Not so sure about this one - "str": ir.PointerType(ir.IntType(8)), + name: IntTy(width, is_signed_ctype(name)) + for name, width in _INT_CTYPE_WIDTHS.items() } +mapping.update( + { + "c_float": ir.FloatType(), + "c_double": ir.DoubleType(), + # Not so sure about this one + "str": ir.PointerType(ir.IntType(8)), + } +) def ctypes_to_ir(ctype: str):