mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
structs_pass cleanup
This commit is contained in:
@ -6,6 +6,7 @@ structs_sym_tab = {}
|
|||||||
|
|
||||||
|
|
||||||
def structs_proc(tree, module, chunks):
|
def structs_proc(tree, module, chunks):
|
||||||
|
""" Process all class definitions to find BPF structs """
|
||||||
for cls_node in chunks:
|
for cls_node in chunks:
|
||||||
# Check if this class is a struct
|
# Check if this class is a struct
|
||||||
is_struct = False
|
is_struct = False
|
||||||
@ -21,22 +22,35 @@ def structs_proc(tree, module, chunks):
|
|||||||
|
|
||||||
|
|
||||||
def process_bpf_struct(cls_node, module):
|
def process_bpf_struct(cls_node, module):
|
||||||
struct_name = cls_node.name
|
""" Process a single BPF struct definition """
|
||||||
|
|
||||||
field_names = []
|
field_names = []
|
||||||
field_types = []
|
field_types = []
|
||||||
|
|
||||||
for item in cls_node.body:
|
for item in cls_node.body:
|
||||||
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
|
#
|
||||||
print(f"Field: {item.target.id}, Type: "
|
# field syntax:
|
||||||
f"{ast.dump(item.annotation)}")
|
# class struct_example:
|
||||||
field_names.append(item.target.id)
|
# num: c_uint64
|
||||||
if isinstance(item.annotation, ast.Call) and isinstance(item.annotation.func, ast.Name) and item.annotation.func.id == "str":
|
#
|
||||||
# This is a char array with fixed length
|
if isinstance(item, ast.AnnAssign):
|
||||||
# TODO: For now assuming str is always called with constant
|
if isinstance(item.target, ast.Name):
|
||||||
field_types.append(ir.ArrayType(
|
print(f"Field: {item.target.id}, Type: "
|
||||||
ir.IntType(8), item.annotation.args[0].value))
|
f"{ast.dump(item.annotation)}")
|
||||||
else:
|
field_names.append(item.target.id)
|
||||||
field_types.append(ctypes_to_ir(item.annotation.id))
|
if isinstance(item.annotation, ast.Call):
|
||||||
|
if isinstance(item.annotation.func, ast.Name):
|
||||||
|
if item.annotation.func.id == "str":
|
||||||
|
# This is a char array with fixed length
|
||||||
|
# TODO: For now assume str is always with constant
|
||||||
|
field_types.append(ir.ArrayType(
|
||||||
|
ir.IntType(8), item.annotation.args[0].value))
|
||||||
|
else:
|
||||||
|
field_types.append(
|
||||||
|
ctypes_to_ir(item.annotation.id))
|
||||||
|
else:
|
||||||
|
print(f"Unsupported struct field: {ast.dump(item)}")
|
||||||
|
return
|
||||||
|
|
||||||
curr_offset = 0
|
curr_offset = 0
|
||||||
for ftype in field_types:
|
for ftype in field_types:
|
||||||
@ -50,7 +64,7 @@ def process_bpf_struct(cls_node, module):
|
|||||||
fsize = 8
|
fsize = 8
|
||||||
alignment = 8
|
alignment = 8
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported field type in struct {struct_name}")
|
print(f"Unsupported field type in struct {cls_node.name}")
|
||||||
return
|
return
|
||||||
padding = (alignment - (curr_offset % alignment)) % alignment
|
padding = (alignment - (curr_offset % alignment)) % alignment
|
||||||
curr_offset += padding
|
curr_offset += padding
|
||||||
@ -59,10 +73,10 @@ def process_bpf_struct(cls_node, module):
|
|||||||
total_size = curr_offset + final_padding
|
total_size = curr_offset + final_padding
|
||||||
|
|
||||||
struct_type = ir.LiteralStructType(field_types)
|
struct_type = ir.LiteralStructType(field_types)
|
||||||
structs_sym_tab[struct_name] = {
|
structs_sym_tab[cls_node.name] = {
|
||||||
"type": struct_type,
|
"type": struct_type,
|
||||||
"fields": {name: idx for idx, name in enumerate(field_names)},
|
"fields": {name: idx for idx, name in enumerate(field_names)},
|
||||||
"size": total_size,
|
"size": total_size,
|
||||||
"field_types": field_types,
|
"field_types": field_types,
|
||||||
}
|
}
|
||||||
print(f"Created struct {struct_name} with fields {field_names}")
|
print(f"Created struct {cls_node.name} with fields {field_names}")
|
||||||
|
|||||||
Reference in New Issue
Block a user