Move convert_to_bool to type_normalization

This commit is contained in:
Pragyansh Chaturvedi
2025-10-08 07:14:42 +05:30
committed by varun-r-mallya
parent eff0f66d95
commit fa720f8e6b
2 changed files with 12 additions and 11 deletions

View File

@ -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,

View File

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