Skip to content

Commit 7a79258

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 a335757 commit 7a79258

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
@@ -418,7 +418,7 @@ def _handle_unary_op(
418418
if isinstance(expr.op, ast.Not):
419419
true_const = ir.Constant(ir.IntType(1), 1)
420420
result = builder.xor(convert_to_bool(builder, operand), true_const)
421-
return result, ir.IntType(1)
421+
return result, IntTy(1, False)
422422
elif isinstance(expr.op, ast.USub):
423423
if isinstance(operand, ir.Constant) and isinstance(operand.constant, int):
424424
# -2 parses as USub(Constant 2); fold it so it is a literal like
@@ -488,7 +488,7 @@ def _handle_and_op(func, builder, expr, local_sym_tab, compilation_context):
488488
phi.add_incoming(val, block)
489489

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

493493

494494
def _handle_or_op(func, builder, expr, local_sym_tab, compilation_context):
@@ -541,7 +541,7 @@ def _handle_or_op(func, builder, expr, local_sym_tab, compilation_context):
541541
phi.add_incoming(val, block)
542542

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

546546

547547
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)