Skip to content

Commit 9fc939c

Browse files
committed
Add structs_pass, tweak functions_pass to respect structs
1 parent 780f53c commit 9fc939c

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

pythonbpf/codegen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .license_pass import license_processing
44
from .functions_pass import func_proc
55
from .maps_pass import maps_proc
6+
from .structs_pass import structs_proc
67
from .globals_pass import globals_processing
78
import os
89
import subprocess
@@ -30,6 +31,7 @@ def processor(source_code, filename, module):
3031
for func_node in bpf_chunks:
3132
print(f"Found BPF function/struct: {func_node.name}")
3233

34+
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
3335
map_sym_tab = maps_proc(tree, module, bpf_chunks)
3436
func_proc(tree, module, bpf_chunks, map_sym_tab)
3537

pythonbpf/functions_pass.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,7 @@ def func_proc(tree, module, chunks, map_sym_tab):
401401
for func_node in chunks:
402402
is_global = False
403403
for decorator in func_node.decorator_list:
404-
if isinstance(decorator, ast.Name) and decorator.id == "map":
405-
is_global = True
406-
break
407-
elif isinstance(decorator, ast.Name) and decorator.id == "bpfglobal":
404+
if isinstance(decorator, ast.Name) and decorator.id in ("map", "bpfglobal", "struct"):
408405
is_global = True
409406
break
410407
if is_global:

pythonbpf/structs_pass.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ast
2+
from llvmlite import ir
3+
from .type_deducer import ctypes_to_ir
4+
from . import dwarf_constants as dc
5+
6+
structs_sym_tab = {}
7+
8+
9+
def structs_proc(tree, module, chunks):
10+
for cls_node in chunks:
11+
# Check if this class is a struct
12+
is_struct = False
13+
for decorator in cls_node.decorator_list:
14+
if isinstance(decorator, ast.Name) and decorator.id == "struct":
15+
is_struct = True
16+
break
17+
if is_struct:
18+
print(f"Found BPF struct: {cls_node.name}")
19+
continue
20+
return structs_sym_tab

0 commit comments

Comments
 (0)