Skip to content

Commit 4f726a7

Browse files
committed
Add comparison ops
1 parent 3dd3784 commit 4f726a7

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

examples/execve3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def hello_again(ctx: c_void_p) -> c_int64:
3030
# if delta < 1000000000:
3131
# print("execve called within last second")
3232
# last().delete(key)
33-
x = True
33+
x = 1
3434
y = False
35-
if x:
36-
if y:
35+
if x > 0:
36+
if x < 2:
3737
print("we prevailed")
3838
else:
3939
print("we did not prevail")

pythonbpf/functions_pass.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,26 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
101101

102102
def handle_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
103103
"""Handle expression statements in the function body."""
104+
print(f"Handling expression: {ast.dump(expr)}")
105+
106+
if isinstance(expr, ast.Name):
107+
if expr.id in local_sym_tab:
108+
var = local_sym_tab[expr.id]
109+
val = builder.load(var)
110+
return val
111+
else:
112+
print(f"Undefined variable {expr.id}")
113+
return None
114+
elif isinstance(expr, ast.Constant):
115+
if isinstance(expr.value, int):
116+
return ir.Constant(ir.IntType(64), expr.value)
117+
elif isinstance(expr.value, bool):
118+
return ir.Constant(ir.IntType(1), int(expr.value))
119+
else:
120+
print("Unsupported constant type")
121+
return None
122+
104123
call = expr.value
105-
print(f"Handling expression: {ast.dump(call)}")
106124
if isinstance(call, ast.Call):
107125
if isinstance(call.func, ast.Name):
108126
# check for helpers first
@@ -154,6 +172,42 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
154172
else:
155173
print(f"Undefined variable {cond.id} in condition")
156174
return None
175+
elif isinstance(cond, ast.Compare):
176+
lhs = handle_expr(func, module, builder, cond.left,
177+
local_sym_tab, map_sym_tab)
178+
if len(cond.ops) != 1 or len(cond.comparators) != 1:
179+
print("Unsupported complex comparison")
180+
return None
181+
rhs = handle_expr(func, module, builder,
182+
cond.comparators[0], local_sym_tab, map_sym_tab)
183+
op = cond.ops[0]
184+
185+
if lhs.type != rhs.type:
186+
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
187+
# Extend the smaller type to the larger type
188+
if lhs.type.width < rhs.type.width:
189+
lhs = builder.sext(lhs, rhs.type)
190+
elif lhs.type.width > rhs.type.width:
191+
rhs = builder.sext(rhs, lhs.type)
192+
else:
193+
print("Type mismatch in comparison")
194+
return None
195+
196+
if isinstance(op, ast.Eq):
197+
return builder.icmp_signed("==", lhs, rhs)
198+
elif isinstance(op, ast.NotEq):
199+
return builder.icmp_signed("!=", lhs, rhs)
200+
elif isinstance(op, ast.Lt):
201+
return builder.icmp_signed("<", lhs, rhs)
202+
elif isinstance(op, ast.LtE):
203+
return builder.icmp_signed("<=", lhs, rhs)
204+
elif isinstance(op, ast.Gt):
205+
return builder.icmp_signed(">", lhs, rhs)
206+
elif isinstance(op, ast.GtE):
207+
return builder.icmp_signed(">=", lhs, rhs)
208+
else:
209+
print("Unsupported comparison operator")
210+
return None
157211
else:
158212
print("Unsupported condition expression")
159213
return None

0 commit comments

Comments
 (0)