Skip to content

Commit fb63dbd

Browse files
committed
Move conditional logic to eval_expr, add _conver_to_bool, add passing bool test
1 parent 4f433d0 commit fb63dbd

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

pythonbpf/expr_pass.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,35 @@ def _handle_ctypes_call(
131131
return val
132132

133133

134+
def _handle_comparator(builder, op, lhs, rhs):
135+
"""Handle comparison operations."""
136+
137+
# NOTE: For now assume same types
138+
139+
comparison_ops = {
140+
ast.Eq: "==",
141+
ast.NotEq: "!=",
142+
ast.Lt: "<",
143+
ast.LtE: "<=",
144+
ast.Gt: ">",
145+
ast.GtE: ">=",
146+
}
147+
148+
if type(op) not in comparison_ops:
149+
logger.error(f"Unsupported comparison operator: {type(op)}")
150+
return None
151+
152+
predicate = comparison_ops[type(op)]
153+
result = builder.icmp_signed(predicate, lhs, rhs)
154+
logger.debug(f"Comparison result: {result}")
155+
return result, ir.IntType(1)
156+
157+
134158
def _handle_compare(
135159
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab=None
136160
):
161+
"""Handle ast.Compare expressions."""
162+
137163
if len(cond.ops) != 1 or len(cond.comparators) != 1:
138164
logger.error("Only single comparisons are supported")
139165
return None
@@ -162,7 +188,7 @@ def _handle_compare(
162188

163189
lhs, _ = lhs
164190
rhs, _ = rhs
165-
return None
191+
return _handle_comparator(builder, cond.ops[0], lhs, rhs)
166192

167193

168194
def eval_expr(

pythonbpf/functions/functions_pass.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,25 @@ def handle_assign(
240240
logger.info("Unsupported assignment value type")
241241

242242

243+
def _convert_to_bool(builder, val):
244+
if val.type == ir.IntType(1):
245+
return val
246+
if isinstance(val.type, ir.PointerType):
247+
zero = ir.Constant(val.type, None)
248+
else:
249+
zero = ir.Constant(val.type, 0)
250+
return builder.icmp_signed("!=", val, zero)
251+
252+
243253
def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
254+
if True:
255+
val = eval_expr(func, module, builder, cond, local_sym_tab, map_sym_tab)[0]
256+
return _convert_to_bool(builder, val)
244257
if isinstance(cond, ast.Constant):
245258
if isinstance(cond.value, bool) or isinstance(cond.value, int):
246-
return ir.Constant(ir.IntType(1), int(bool(cond.value)))
259+
return ir.Constant(ir.IntType(1), int(cond.value))
247260
else:
248-
logger.info("Unsupported constant type in condition")
261+
raise ValueError("Unsupported constant type in condition")
249262
return None
250263
elif isinstance(cond, ast.Name):
251264
if cond.id in local_sym_tab:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello_world(ctx: c_void_p) -> c_int64:
8+
if True:
9+
print("Hello, World!")
10+
else:
11+
print("Goodbye, World!")
12+
return
13+
14+
15+
@bpf
16+
@bpfglobal
17+
def LICENSE() -> str:
18+
return "GPL"
19+
20+
21+
compile()

0 commit comments

Comments
 (0)