Skip to content

Commit 4857739

Browse files
committed
cleanup handle_cond in functions_pass
1 parent 3bb4b09 commit 4857739

File tree

1 file changed

+4
-67
lines changed

1 file changed

+4
-67
lines changed

pythonbpf/functions/functions_pass.py

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -243,73 +243,10 @@ def handle_assign(
243243
def handle_cond(
244244
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab=None
245245
):
246-
if True:
247-
val = eval_expr(
248-
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab
249-
)[0]
250-
return convert_to_bool(builder, val)
251-
if isinstance(cond, ast.Constant):
252-
if isinstance(cond.value, bool) or isinstance(cond.value, int):
253-
return ir.Constant(ir.IntType(1), int(cond.value))
254-
else:
255-
raise ValueError("Unsupported constant type in condition")
256-
return None
257-
elif isinstance(cond, ast.Name):
258-
if cond.id in local_sym_tab:
259-
var = local_sym_tab[cond.id].var
260-
val = builder.load(var)
261-
if val.type != ir.IntType(1):
262-
# Convert nonzero values to true, zero to false
263-
if isinstance(val.type, ir.PointerType):
264-
# For pointer types, compare with null pointer
265-
zero = ir.Constant(val.type, None)
266-
else:
267-
# For integer types, compare with zero
268-
zero = ir.Constant(val.type, 0)
269-
val = builder.icmp_signed("!=", val, zero)
270-
return val
271-
else:
272-
logger.info(f"Undefined variable {cond.id} in condition")
273-
return None
274-
elif isinstance(cond, ast.Compare):
275-
lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0]
276-
if len(cond.ops) != 1 or len(cond.comparators) != 1:
277-
logger.info("Unsupported complex comparison")
278-
return None
279-
rhs = eval_expr(
280-
func, module, builder, cond.comparators[0], local_sym_tab, map_sym_tab
281-
)[0]
282-
op = cond.ops[0]
283-
284-
if lhs.type != rhs.type:
285-
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
286-
# Extend the smaller type to the larger type
287-
if lhs.type.width < rhs.type.width:
288-
lhs = builder.sext(lhs, rhs.type)
289-
elif lhs.type.width > rhs.type.width:
290-
rhs = builder.sext(rhs, lhs.type)
291-
else:
292-
logger.info("Type mismatch in comparison")
293-
return None
294-
295-
if isinstance(op, ast.Eq):
296-
return builder.icmp_signed("==", lhs, rhs)
297-
elif isinstance(op, ast.NotEq):
298-
return builder.icmp_signed("!=", lhs, rhs)
299-
elif isinstance(op, ast.Lt):
300-
return builder.icmp_signed("<", lhs, rhs)
301-
elif isinstance(op, ast.LtE):
302-
return builder.icmp_signed("<=", lhs, rhs)
303-
elif isinstance(op, ast.Gt):
304-
return builder.icmp_signed(">", lhs, rhs)
305-
elif isinstance(op, ast.GtE):
306-
return builder.icmp_signed(">=", lhs, rhs)
307-
else:
308-
logger.info("Unsupported comparison operator")
309-
return None
310-
else:
311-
logger.info("Unsupported condition expression")
312-
return None
246+
val = eval_expr(
247+
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab
248+
)[0]
249+
return convert_to_bool(builder, val)
313250

314251

315252
def handle_if(

0 commit comments

Comments
 (0)