From 568a651096e4043f1a6c31b9950cd21ccb3240e5 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 4 Sep 2026 11:00:12 +0530 Subject: [PATCH] Core: Type comparison and boolean results as unsigned i1 A comparison's descriptor was a plain i1, which reads as signed, so storing or returning a truth value would sign-extend true to -1. C's relational operators yield int 0 or 1; give the results an unsigned descriptor so any widening zero-extends. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU --- pythonbpf/expr/expr_pass.py | 6 +++--- pythonbpf/expr/type_normalization.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index ea3c8d6..d6ca18e 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -421,7 +421,7 @@ def _handle_unary_op( if isinstance(expr.op, ast.Not): true_const = ir.Constant(ir.IntType(1), 1) result = builder.xor(convert_to_bool(builder, operand), true_const) - return result, ir.IntType(1) + return result, IntTy(1, False) elif isinstance(expr.op, ast.USub): if isinstance(operand, ir.Constant) and isinstance(operand.constant, int): # -2 parses as USub(Constant 2); fold it so it is a literal like @@ -491,7 +491,7 @@ def _handle_and_op(func, builder, expr, local_sym_tab, compilation_context): phi.add_incoming(val, block) logger.debug(f"Generated 'and' with {len(incoming_values)} incoming values") - return phi, ir.IntType(1) + return phi, IntTy(1, False) def _handle_or_op(func, builder, expr, local_sym_tab, compilation_context): @@ -544,7 +544,7 @@ def _handle_or_op(func, builder, expr, local_sym_tab, compilation_context): phi.add_incoming(val, block) logger.debug(f"Generated 'or' with {len(incoming_values)} incoming values") - return phi, ir.IntType(1) + return phi, IntTy(1, False) def _handle_boolean_op( diff --git a/pythonbpf/expr/type_normalization.py b/pythonbpf/expr/type_normalization.py index e58577a..96a02ec 100644 --- a/pythonbpf/expr/type_normalization.py +++ b/pythonbpf/expr/type_normalization.py @@ -1,7 +1,7 @@ import logging from llvmlite import ir from .ir_ops import deref_to_depth -from pythonbpf.type_deducer import signedness +from pythonbpf.type_deducer import IntTy, signedness from .operators import COMPARISON_OPS logger = logging.getLogger(__name__) @@ -128,4 +128,4 @@ def handle_comparator(func, builder, op, lhs, rhs, signed=True): icmp = builder.icmp_signed if signed else builder.icmp_unsigned result = icmp(predicate, lhs, rhs) logger.debug(f"Comparison result: {result}") - return result, ir.IntType(1) + return result, IntTy(1, False)