diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index d6ca18e..569a6e7 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -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) diff --git a/pythonbpf/expr/type_inference.py b/pythonbpf/expr/type_inference.py index 6532a53..3c10555 100644 --- a/pythonbpf/expr/type_inference.py +++ b/pythonbpf/expr/type_inference.py @@ -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): diff --git a/pythonbpf/type_deducer.py b/pythonbpf/type_deducer.py index c78b1cf..0987eab 100644 --- a/pythonbpf/type_deducer.py +++ b/pythonbpf/type_deducer.py @@ -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. diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index f8c8310..7305d11 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -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):