Skip to content

Commit 10fb1f0

Browse files
committed
Add else
1 parent 8d50298 commit 10fb1f0

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

examples/execve3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ 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 = False
3433
x = True
34+
x = False
3535
if x:
3636
print("we prevailed")
37-
if x:
37+
else:
3838
print("we did not prevail")
3939
ts = ktime()
4040
last().update(key, ts)
@@ -52,4 +52,5 @@ def hello_again(ctx: c_void_p) -> c_int64:
5252
def LICENSE() -> str:
5353
return "GPL"
5454

55+
5556
compile()

pythonbpf/functions_pass.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,33 @@ def handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab):
161161
start = builder.block.parent
162162
then_block = func.append_basic_block(name="if.then")
163163
merge_block = func.append_basic_block(name="if.end")
164+
if stmt.orelse:
165+
else_block = func.append_basic_block(name="if.else")
166+
else:
167+
else_block = None
164168

165169
cond = handle_cond(func, module, builder, stmt.test,
166170
local_sym_tab, map_sym_tab)
171+
if else_block:
172+
builder.cbranch(cond, then_block, else_block)
173+
else:
174+
builder.cbranch(cond, then_block, merge_block)
167175

168-
builder.cbranch(cond, then_block, merge_block)
169176
builder.position_at_end(then_block)
170177
for s in stmt.body:
171178
process_stmt(func, module, builder, s,
172179
local_sym_tab, map_sym_tab, False)
173180
if not builder.block.is_terminated:
174181
builder.branch(merge_block)
175182

183+
if else_block:
184+
builder.position_at_end(else_block)
185+
for s in stmt.orelse:
186+
process_stmt(func, module, builder, s,
187+
local_sym_tab, map_sym_tab, False)
188+
if not builder.block.is_terminated:
189+
builder.branch(merge_block)
190+
176191
builder.position_at_end(merge_block)
177192

178193

0 commit comments

Comments
 (0)