Skip to content

Commit f830fbe

Browse files
add bool assignment support
1 parent ebb872f commit f830fbe

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

examples/execve3.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ 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-
if True:
33+
x = False
34+
x = True
35+
if x:
3436
print("we prevailed")
37+
if x:
38+
print("we did not prevail")
3539
ts = ktime()
3640
# last().update(key, ts)
3741
return c_int64(0)

pythonbpf/functions_pass.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
3737
var_name = target.id
3838
rval = stmt.value
3939
if isinstance(rval, ast.Constant):
40-
if isinstance(rval.value, int):
40+
if isinstance(rval.value, bool):
41+
if rval.value:
42+
builder.store(ir.Constant(ir.IntType(1), 1),
43+
local_sym_tab[var_name])
44+
else:
45+
builder.store(ir.Constant(ir.IntType(1), 0),
46+
local_sym_tab[var_name])
47+
print(f"Assigned constant {rval.value} to {var_name}")
48+
elif isinstance(rval.value, int):
4149
# Assume c_int64 for now
4250
# var = builder.alloca(ir.IntType(64), name=var_name)
4351
# var.align = 8
@@ -232,7 +240,13 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
232240
print("Unsupported assignment call function type")
233241
continue
234242
elif isinstance(rval, ast.Constant):
235-
if isinstance(rval.value, int):
243+
if isinstance(rval.value, bool):
244+
ir_type = ir.IntType(1)
245+
var = builder.alloca(ir_type, name=var_name)
246+
var.align = 1
247+
print(
248+
f"Pre-allocated variable {var_name} of type c_bool")
249+
elif isinstance(rval.value, int):
236250
# Assume c_int64 for now
237251
ir_type = ir.IntType(64)
238252
var = builder.alloca(ir_type, name=var_name)

0 commit comments

Comments
 (0)