Skip to content

Commit 6ab37cc

Browse files
r41k0uclaude
andcommitted
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
1 parent 1e665a1 commit 6ab37cc

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

‎pythonbpf/expr/expr_pass.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def _handle_unary_op(
421421
if isinstance(expr.op, ast.Not):
422422
true_const = ir.Constant(ir.IntType(1), 1)
423423
result = builder.xor(convert_to_bool(builder, operand), true_const)
424-
return result, ir.IntType(1)
424+
return result, IntTy(1, False)
425425
elif isinstance(expr.op, ast.USub):
426426
if isinstance(operand, ir.Constant) and isinstance(operand.constant, int):
427427
# -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):
491491
phi.add_incoming(val, block)
492492

493493
logger.debug(f"Generated 'and' with {len(incoming_values)} incoming values")
494-
return phi, ir.IntType(1)
494+
return phi, IntTy(1, False)
495495

496496

497497
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):
544544
phi.add_incoming(val, block)
545545

546546
logger.debug(f"Generated 'or' with {len(incoming_values)} incoming values")
547-
return phi, ir.IntType(1)
547+
return phi, IntTy(1, False)
548548

549549

550550
def _handle_boolean_op(

‎pythonbpf/expr/type_normalization.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from llvmlite import ir
33
from .ir_ops import deref_to_depth
4-
from pythonbpf.type_deducer import signedness
4+
from pythonbpf.type_deducer import IntTy, signedness
55
from .operators import COMPARISON_OPS
66

77
logger = logging.getLogger(__name__)
@@ -128,4 +128,4 @@ def handle_comparator(func, builder, op, lhs, rhs, signed=True):
128128
icmp = builder.icmp_signed if signed else builder.icmp_unsigned
129129
result = icmp(predicate, lhs, rhs)
130130
logger.debug(f"Comparison result: {result}")
131-
return result, ir.IntType(1)
131+
return result, IntTy(1, False)

0 commit comments

Comments
 (0)