Skip to content

Commit 083ee21

Browse files
committed
structs_pass cleanup
1 parent ea5a1ab commit 083ee21

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

pythonbpf/structs_pass.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
def structs_proc(tree, module, chunks):
9+
""" Process all class definitions to find BPF structs """
910
for cls_node in chunks:
1011
# Check if this class is a struct
1112
is_struct = False
@@ -21,22 +22,35 @@ def structs_proc(tree, module, chunks):
2122

2223

2324
def process_bpf_struct(cls_node, module):
24-
struct_name = cls_node.name
25+
""" Process a single BPF struct definition """
26+
2527
field_names = []
2628
field_types = []
2729

2830
for item in cls_node.body:
29-
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
30-
print(f"Field: {item.target.id}, Type: "
31-
f"{ast.dump(item.annotation)}")
32-
field_names.append(item.target.id)
33-
if isinstance(item.annotation, ast.Call) and isinstance(item.annotation.func, ast.Name) and item.annotation.func.id == "str":
34-
# This is a char array with fixed length
35-
# TODO: For now assuming str is always called with constant
36-
field_types.append(ir.ArrayType(
37-
ir.IntType(8), item.annotation.args[0].value))
38-
else:
39-
field_types.append(ctypes_to_ir(item.annotation.id))
31+
#
32+
# field syntax:
33+
# class struct_example:
34+
# num: c_uint64
35+
#
36+
if isinstance(item, ast.AnnAssign):
37+
if isinstance(item.target, ast.Name):
38+
print(f"Field: {item.target.id}, Type: "
39+
f"{ast.dump(item.annotation)}")
40+
field_names.append(item.target.id)
41+
if isinstance(item.annotation, ast.Call):
42+
if isinstance(item.annotation.func, ast.Name):
43+
if item.annotation.func.id == "str":
44+
# This is a char array with fixed length
45+
# TODO: For now assume str is always with constant
46+
field_types.append(ir.ArrayType(
47+
ir.IntType(8), item.annotation.args[0].value))
48+
else:
49+
field_types.append(
50+
ctypes_to_ir(item.annotation.id))
51+
else:
52+
print(f"Unsupported struct field: {ast.dump(item)}")
53+
return
4054

4155
curr_offset = 0
4256
for ftype in field_types:
@@ -50,7 +64,7 @@ def process_bpf_struct(cls_node, module):
5064
fsize = 8
5165
alignment = 8
5266
else:
53-
print(f"Unsupported field type in struct {struct_name}")
67+
print(f"Unsupported field type in struct {cls_node.name}")
5468
return
5569
padding = (alignment - (curr_offset % alignment)) % alignment
5670
curr_offset += padding
@@ -59,10 +73,10 @@ def process_bpf_struct(cls_node, module):
5973
total_size = curr_offset + final_padding
6074

6175
struct_type = ir.LiteralStructType(field_types)
62-
structs_sym_tab[struct_name] = {
76+
structs_sym_tab[cls_node.name] = {
6377
"type": struct_type,
6478
"fields": {name: idx for idx, name in enumerate(field_names)},
6579
"size": total_size,
6680
"field_types": field_types,
6781
}
68-
print(f"Created struct {struct_name} with fields {field_names}")
82+
print(f"Created struct {cls_node.name} with fields {field_names}")

0 commit comments

Comments
 (0)