Skip to content

Commit 357ad7c

Browse files
committed
Add if statement barebones
1 parent 617aac9 commit 357ad7c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

pythonbpf/functions_pass.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab):
8181
print("Unsupported assignment call function type")
8282

8383

84+
def handle_if_statement(module, builder, stmt, map_sym_tab, local_sym_tab):
85+
pass
86+
87+
8488
def handle_expr(module, builder, expr, local_sym_tab, map_sym_tab):
8589
"""Handle expression statements in the function body."""
8690
call = expr.value
@@ -94,6 +98,24 @@ def handle_expr(module, builder, expr, local_sym_tab, map_sym_tab):
9498
print("Unsupported expression statement")
9599

96100

101+
def handle_if(module, builder, stmt, map_sym_tab, local_sym_tab):
102+
"""Handle if statements in the function body."""
103+
func = builder.block.parent
104+
then_block = func.append_basic_block(name="if.then")
105+
merge_block = func.append_basic_block(name="if.end")
106+
107+
cond = stmt.test
108+
109+
builder.cbranch(cond, then_block, merge_block)
110+
builder.position_at_end(then_block)
111+
for s in stmt.body:
112+
pass
113+
if not builder.block.is_terminated:
114+
builder.branch(merge_block)
115+
116+
builder.position_at_end(merge_block)
117+
118+
97119
def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
98120
"""Process the body of a bpf function"""
99121
# TODO: A lot. We just have print -> bpf_trace_printk for now
@@ -106,6 +128,8 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
106128
handle_expr(module, builder, stmt, local_sym_tab, map_sym_tab)
107129
elif isinstance(stmt, ast.Assign):
108130
handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab)
131+
elif isinstance(stmt, ast.If):
132+
handle_if(module, builder, stmt, map_sym_tab, local_sym_tab)
109133
elif isinstance(stmt, ast.Return):
110134
if stmt.value is None:
111135
builder.ret(ir.Constant(ir.IntType(32), 0))

0 commit comments

Comments
 (0)