Skip to content

Commit 2d850f4

Browse files
committed
Add _normalize_types to handle mismatched ints, move type_mismatch test to passing
1 parent 9e1142b commit 2d850f4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pythonbpf/expr_pass.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,29 @@ def _handle_ctypes_call(
129129
return val
130130

131131

132+
def _normalize_types(builder, lhs, rhs):
133+
"""Normalize types for comparison."""
134+
135+
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
136+
if lhs.type.width < rhs.type.width:
137+
lhs = builder.sext(lhs, rhs.type)
138+
else:
139+
rhs = builder.sext(rhs, lhs.type)
140+
return lhs, rhs
141+
142+
logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}")
143+
return None, None
144+
145+
132146
def _handle_comparator(builder, op, lhs, rhs):
133147
"""Handle comparison operations."""
134148

135149
# NOTE: For now assume same types
150+
if lhs.type != rhs.type:
151+
lhs, rhs = _normalize_types(builder, lhs, rhs)
152+
153+
if lhs is None or rhs is None:
154+
return None
136155

137156
comparison_ops = {
138157
ast.Eq: "==",

0 commit comments

Comments
 (0)