Skip to content

Commit 1f96bab

Browse files
committed
Add _handle_and_op in expr_pass
1 parent f98491f commit 1f96bab

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

pythonbpf/expr_pass.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,56 @@ def _handle_unary_op(
316316

317317

318318
def _handle_and_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):
319-
pass
319+
"""Handle `and` boolean operations."""
320+
321+
logger.debug(f"Handling 'and' operator with {len(expr.values)} operands")
322+
323+
merge_block = func.append_basic_block(name="and.merge")
324+
false_block = func.append_basic_block(name="and.false")
325+
326+
incoming_values = []
327+
328+
for i, value in enumerate(expr.values):
329+
is_last = i == len(expr.values) - 1
330+
331+
# Evaluate current operand
332+
operand_result = eval_expr(
333+
func, None, builder, value, local_sym_tab, map_sym_tab, structs_sym_tab
334+
)
335+
if operand_result is None:
336+
logger.error(f"Failed to evaluate operand {i} in 'and' expression")
337+
return None
338+
339+
operand_val, operand_type = operand_result
340+
341+
# Convert to boolean if needed
342+
operand_bool = convert_to_bool(builder, operand_val)
343+
current_block = builder.block
344+
345+
if is_last:
346+
# Last operand: result is this value
347+
builder.branch(merge_block)
348+
incoming_values.append((operand_bool, current_block))
349+
else:
350+
# Not last: check if true, continue or short-circuit
351+
next_check = func.append_basic_block(name=f"and.check_{i + 1}")
352+
builder.cbranch(operand_bool, next_check, false_block)
353+
builder.position_at_end(next_check)
354+
355+
# False block: short-circuit with false
356+
builder.position_at_end(false_block)
357+
builder.branch(merge_block)
358+
false_value = ir.Constant(ir.IntType(1), 0)
359+
incoming_values.append((false_value, false_block))
360+
361+
# Merge block: phi node
362+
builder.position_at_end(merge_block)
363+
phi = builder.phi(ir.IntType(1), name="and.result")
364+
for val, block in incoming_values:
365+
phi.add_incoming(val, block)
366+
367+
logger.debug(f"Generated 'and' with {len(incoming_values)} incoming values")
368+
return phi, ir.IntType(1)
320369

321370

322371
def _handle_or_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):

0 commit comments

Comments
 (0)