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:
Pragyansh Chaturvedi
2026-09-04 10:08:48 +05:30
parent f3cc6c9ce7
commit daf429d463
4 changed files with 159 additions and 36 deletions

View File

@ -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