From 0a6571726a459567eff87ad00ba7af282904dadb Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Wed, 8 Oct 2025 07:14:42 +0530 Subject: [PATCH] Move convert_to_bool to type_normalization --- pythonbpf/expr/expr_pass.py | 12 +----------- pythonbpf/expr/type_normalization.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index f0c58a5..a72a4bb 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -5,7 +5,7 @@ import logging from typing import Dict from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes -from .type_normalization import normalize_types +from .type_normalization import normalize_types, convert_to_bool logger: Logger = logging.getLogger(__name__) @@ -197,16 +197,6 @@ def _handle_compare( return _handle_comparator(func, builder, cond.ops[0], lhs, rhs) -def convert_to_bool(builder, val): - if val.type == ir.IntType(1): - return val - if isinstance(val.type, ir.PointerType): - zero = ir.Constant(val.type, None) - else: - zero = ir.Constant(val.type, 0) - return builder.icmp_signed("!=", val, zero) - - def _handle_unary_op( func, module, diff --git a/pythonbpf/expr/type_normalization.py b/pythonbpf/expr/type_normalization.py index a1b3fad..2715e1f 100644 --- a/pythonbpf/expr/type_normalization.py +++ b/pythonbpf/expr/type_normalization.py @@ -84,3 +84,14 @@ def normalize_types(func, builder, lhs, rhs): elif rhs_depth < lhs_depth: lhs = _deref_to_depth(func, builder, lhs, lhs_depth - rhs_depth) return normalize_types(func, builder, lhs, rhs) + + +def convert_to_bool(builder, val): + """Convert a value to boolean.""" + if val.type == ir.IntType(1): + return val + if isinstance(val.type, ir.PointerType): + zero = ir.Constant(val.type, None) + else: + zero = ir.Constant(val.type, 0) + return builder.icmp_signed("!=", val, zero)