Skip to content

Commit 63c44fa

Browse files
committed
Pass down structs_sym_tab
1 parent c79dc63 commit 63c44fa

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

pythonbpf/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def processor(source_code, filename, module):
3333

3434
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
3535
map_sym_tab = maps_proc(tree, module, bpf_chunks)
36-
func_proc(tree, module, bpf_chunks, map_sym_tab)
36+
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
3737

3838
license_processing(tree, module)
3939
globals_processing(tree, module)

pythonbpf/functions_pass.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab):
219219
builder.position_at_end(merge_block)
220220

221221

222-
def process_stmt(func, module, builder, stmt, local_sym_tab, map_sym_tab, did_return, ret_type=ir.IntType(64)):
222+
def process_stmt(func, module, builder, stmt, local_sym_tab, map_sym_tab, structs_sym_tab, did_return, ret_type=ir.IntType(64)):
223223
print(f"Processing statement: {ast.dump(stmt)}")
224224
if isinstance(stmt, ast.Expr):
225225
handle_expr(func, module, builder, stmt, local_sym_tab, map_sym_tab)
@@ -256,15 +256,15 @@ def process_stmt(func, module, builder, stmt, local_sym_tab, map_sym_tab, did_re
256256
return did_return
257257

258258

259-
def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_tab):
259+
def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab):
260260
for stmt in body:
261261
if isinstance(stmt, ast.If):
262262
if stmt.body:
263263
local_sym_tab = allocate_mem(
264-
module, builder, stmt.body, func, ret_type, map_sym_tab, local_sym_tab)
264+
module, builder, stmt.body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab)
265265
if stmt.orelse:
266266
local_sym_tab = allocate_mem(
267-
module, builder, stmt.orelse, func, ret_type, map_sym_tab, local_sym_tab)
267+
module, builder, stmt.orelse, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab)
268268
elif isinstance(stmt, ast.Assign):
269269
if len(stmt.targets) != 1:
270270
print("Unsupported multiassignment")
@@ -338,7 +338,7 @@ def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_t
338338
return local_sym_tab
339339

340340

341-
def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
341+
def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab, structs_sym_tab):
342342
"""Process the body of a bpf function"""
343343
# TODO: A lot. We just have print -> bpf_trace_printk for now
344344
did_return = False
@@ -347,19 +347,19 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
347347

348348
# pre-allocate dynamic variables
349349
local_sym_tab = allocate_mem(
350-
module, builder, func_node.body, func, ret_type, map_sym_tab, local_sym_tab)
350+
module, builder, func_node.body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab)
351351

352352
print(f"Local symbol table: {local_sym_tab.keys()}")
353353

354354
for stmt in func_node.body:
355355
did_return = process_stmt(func, module, builder, stmt, local_sym_tab,
356-
map_sym_tab, did_return, ret_type)
356+
map_sym_tab, structs_sym_tab, did_return, ret_type)
357357

358358
if not did_return:
359359
builder.ret(ir.Constant(ir.IntType(32), 0))
360360

361361

362-
def process_bpf_chunk(func_node, module, return_type, map_sym_tab):
362+
def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_tab):
363363
"""Process a single BPF chunk (function) and emit corresponding LLVM IR."""
364364

365365
func_name = func_node.name
@@ -392,12 +392,13 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab):
392392
block = func.append_basic_block(name="entry")
393393
builder = ir.IRBuilder(block)
394394

395-
process_func_body(module, builder, func_node, func, ret_type, map_sym_tab)
395+
process_func_body(module, builder, func_node, func,
396+
ret_type, map_sym_tab, structs_sym_tab)
396397

397398
return func
398399

399400

400-
def func_proc(tree, module, chunks, map_sym_tab):
401+
def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
401402
for func_node in chunks:
402403
is_global = False
403404
for decorator in func_node.decorator_list:
@@ -410,7 +411,7 @@ def func_proc(tree, module, chunks, map_sym_tab):
410411
print(f"Found probe_string of {func_node.name}: {func_type}")
411412

412413
process_bpf_chunk(func_node, module, ctypes_to_ir(
413-
infer_return_type(func_node)), map_sym_tab)
414+
infer_return_type(func_node)), map_sym_tab, structs_sym_tab)
414415

415416

416417
def infer_return_type(func_node: ast.FunctionDef):

0 commit comments

Comments
 (0)