From 9fc939cb8edfa06b3eaa7e7c8faac5102ff3f4f6 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 21 Sep 2025 03:29:05 +0530 Subject: [PATCH] Add structs_pass, tweak functions_pass to respect structs --- pythonbpf/codegen.py | 2 ++ pythonbpf/functions_pass.py | 5 +---- pythonbpf/structs_pass.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 pythonbpf/structs_pass.py diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 9f989c0..3ed0e7f 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -3,6 +3,7 @@ from llvmlite import ir from .license_pass import license_processing from .functions_pass import func_proc from .maps_pass import maps_proc +from .structs_pass import structs_proc from .globals_pass import globals_processing import os import subprocess @@ -30,6 +31,7 @@ def processor(source_code, filename, module): for func_node in bpf_chunks: print(f"Found BPF function/struct: {func_node.name}") + structs_sym_tab = structs_proc(tree, module, bpf_chunks) map_sym_tab = maps_proc(tree, module, bpf_chunks) func_proc(tree, module, bpf_chunks, map_sym_tab) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 1c651ed..4494fe1 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -401,10 +401,7 @@ def func_proc(tree, module, chunks, map_sym_tab): for func_node in chunks: is_global = False for decorator in func_node.decorator_list: - if isinstance(decorator, ast.Name) and decorator.id == "map": - is_global = True - break - elif isinstance(decorator, ast.Name) and decorator.id == "bpfglobal": + if isinstance(decorator, ast.Name) and decorator.id in ("map", "bpfglobal", "struct"): is_global = True break if is_global: diff --git a/pythonbpf/structs_pass.py b/pythonbpf/structs_pass.py new file mode 100644 index 0000000..bf81e32 --- /dev/null +++ b/pythonbpf/structs_pass.py @@ -0,0 +1,20 @@ +import ast +from llvmlite import ir +from .type_deducer import ctypes_to_ir +from . import dwarf_constants as dc + +structs_sym_tab = {} + + +def structs_proc(tree, module, chunks): + for cls_node in chunks: + # Check if this class is a struct + is_struct = False + for decorator in cls_node.decorator_list: + if isinstance(decorator, ast.Name) and decorator.id == "struct": + is_struct = True + break + if is_struct: + print(f"Found BPF struct: {cls_node.name}") + continue + return structs_sym_tab