mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
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:
@ -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)
|
||||
|
||||
@ -40,8 +40,9 @@ def infer_int_type(expr, local_sym_tab, compilation_context):
|
||||
return _as_intty(local_sym_tab[expr.id].ir_type)
|
||||
if expr.id in compilation_context.bpf_globals:
|
||||
return _as_intty(compilation_context.bpf_globals[expr.id].ir_type)
|
||||
if VmlinuxHandlerRegistry.handle_name(expr.id) is not None:
|
||||
return IntTy(64, True) # enum constants are emitted as i64
|
||||
enum = VmlinuxHandlerRegistry.handle_name(expr.id)
|
||||
if enum is not None:
|
||||
return _as_intty(enum[1])
|
||||
return None
|
||||
|
||||
if isinstance(expr, ast.BinOp):
|
||||
|
||||
@ -41,6 +41,14 @@ class IntTy(ir.IntType):
|
||||
return f"{'i' if self.signed else 'u'}{self.width}"
|
||||
|
||||
|
||||
def int_literal_type(value: int) -> IntTy:
|
||||
"""C's type for an integer constant: `int` if the value fits, else
|
||||
`long long`. Literals and enum constants alike; an enum constant is an
|
||||
`int` whatever the enum's underlying type is (that type belongs to
|
||||
variables of the enum type, such as struct fields)."""
|
||||
return IntTy(32, True) if -(1 << 31) <= value < (1 << 31) else IntTy(64, True)
|
||||
|
||||
|
||||
def signedness(ty) -> bool:
|
||||
"""Sign of an integer type descriptor.
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import ctypes
|
||||
from llvmlite import ir
|
||||
|
||||
from pythonbpf.symbols import LocalSymbol
|
||||
from pythonbpf.type_deducer import is_signed_ctype
|
||||
from pythonbpf.type_deducer import int_literal_type, is_signed_ctype
|
||||
from pythonbpf.vmlinux_parser.assignment_info import AssignmentType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -74,7 +74,7 @@ class VmlinuxHandler:
|
||||
if self.is_vmlinux_enum(name):
|
||||
value = self.vmlinux_symtab[name].value
|
||||
logger.info(f"Resolving vmlinux enum {name} = {value}")
|
||||
return ir.Constant(ir.IntType(64), value), ir.IntType(64)
|
||||
return ir.Constant(ir.IntType(64), value), int_literal_type(value)
|
||||
return None
|
||||
|
||||
def get_vmlinux_enum_value(self, name):
|
||||
|
||||
Reference in New Issue
Block a user