Core: Rank an enum constant as C's int, not as i64

An enum constant has type int in C whatever the enum's underlying type
is; clang emits `sub nsw i32` + sext for `XDP_PASS - k` and reserves the
enum's u32 for variables of the enum type. The vmlinux handler was
typing constants as i64, which made `XDP_PASS - k` with an unsigned k a
signed 64-bit operation instead of C's u32 one. The constant's C rank
is now decided once, in type_deducer.int_literal_type, shared by
literals and enum constants, and the handler returns it as the
descriptor so both operand typing and local inference read it from
the source.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-18 17:52:28 +05:30
parent 8e802e8532
commit ca92f2ab77
4 changed files with 22 additions and 9 deletions

View File

@ -4,7 +4,13 @@ from logging import Logger
import logging
from typing import Dict
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes, IntTy, signedness
from pythonbpf.type_deducer import (
ctypes_to_ir,
is_ctypes,
IntTy,
int_literal_type,
signedness,
)
from .call_registry import CallHandlerRegistry
from .ir_ops import deref_to_depth, access_struct_field
from .operators import apply_binop, usual_arithmetic_conversions, UNARY_OPS, BOOL_OPS
@ -54,8 +60,7 @@ def _handle_name_expr(
def _int_literal(v: int):
"""An integer literal: a 64-bit constant with C's literal rank as its
descriptor, `int` if the value fits and `long long` otherwise."""
lit_ty = IntTy(32, True) if -(1 << 31) <= v < (1 << 31) else IntTy(64, True)
return ir.Constant(ir.IntType(64), v), lit_ty
return ir.Constant(ir.IntType(64), v), int_literal_type(v)
def _handle_constant_expr(compilation_context, builder, expr: ast.Constant):
@ -221,8 +226,7 @@ def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab
else:
vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id)
if vmlinux_result is not None:
val, _ = vmlinux_result
return val, IntTy(64, True)
return vmlinux_result # (i64 constant, its C rank)
elif isinstance(operand, ast.Constant):
if isinstance(operand.value, (int, bool)):
v = int(operand.value)