mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-03-27 15:41:27 +00:00
Add docstrings to core modules and helper functions
Co-authored-by: varun-r-mallya <100590632+varun-r-mallya@users.noreply.github.com>
This commit is contained in:
@ -2,18 +2,64 @@ from llvmlite import ir
|
||||
|
||||
|
||||
class StructType:
|
||||
"""
|
||||
Wrapper class for LLVM IR struct types with field access helpers.
|
||||
|
||||
Attributes:
|
||||
ir_type: The LLVM IR struct type
|
||||
fields: Dictionary mapping field names to their types
|
||||
size: Total size of the struct in bytes
|
||||
"""
|
||||
|
||||
def __init__(self, ir_type, fields, size):
|
||||
"""
|
||||
Initialize a StructType.
|
||||
|
||||
Args:
|
||||
ir_type: The LLVM IR struct type
|
||||
fields: Dictionary mapping field names to their types
|
||||
size: Total size of the struct in bytes
|
||||
"""
|
||||
self.ir_type = ir_type
|
||||
self.fields = fields
|
||||
self.size = size
|
||||
|
||||
def field_idx(self, field_name):
|
||||
"""
|
||||
Get the index of a field in the struct.
|
||||
|
||||
Args:
|
||||
field_name: The name of the field
|
||||
|
||||
Returns:
|
||||
The zero-based index of the field
|
||||
"""
|
||||
return list(self.fields.keys()).index(field_name)
|
||||
|
||||
def field_type(self, field_name):
|
||||
"""
|
||||
Get the LLVM IR type of a field.
|
||||
|
||||
Args:
|
||||
field_name: The name of the field
|
||||
|
||||
Returns:
|
||||
The LLVM IR type of the field
|
||||
"""
|
||||
return self.fields[field_name]
|
||||
|
||||
def gep(self, builder, ptr, field_name):
|
||||
"""
|
||||
Generate a GEP (GetElementPtr) instruction to access a struct field.
|
||||
|
||||
Args:
|
||||
builder: LLVM IR builder
|
||||
ptr: Pointer to the struct
|
||||
field_name: Name of the field to access
|
||||
|
||||
Returns:
|
||||
A pointer to the field
|
||||
"""
|
||||
idx = self.field_idx(field_name)
|
||||
return builder.gep(
|
||||
ptr,
|
||||
@ -22,6 +68,18 @@ class StructType:
|
||||
)
|
||||
|
||||
def field_size(self, field_name):
|
||||
"""
|
||||
Calculate the size of a field in bytes.
|
||||
|
||||
Args:
|
||||
field_name: The name of the field
|
||||
|
||||
Returns:
|
||||
The size of the field in bytes
|
||||
|
||||
Raises:
|
||||
TypeError: If the field type is not supported
|
||||
"""
|
||||
fld = self.fields[field_name]
|
||||
if isinstance(fld, ir.ArrayType):
|
||||
return fld.count * (fld.element.width // 8)
|
||||
|
||||
@ -26,6 +26,15 @@ def structs_proc(tree, module, chunks):
|
||||
|
||||
|
||||
def is_bpf_struct(cls_node):
|
||||
"""
|
||||
Check if a class node is decorated with @struct.
|
||||
|
||||
Args:
|
||||
cls_node: The AST class node to check
|
||||
|
||||
Returns:
|
||||
True if the class is decorated with @struct, False otherwise
|
||||
"""
|
||||
return any(
|
||||
isinstance(decorator, ast.Name) and decorator.id == "struct"
|
||||
for decorator in cls_node.decorator_list
|
||||
@ -33,7 +42,16 @@ def is_bpf_struct(cls_node):
|
||||
|
||||
|
||||
def process_bpf_struct(cls_node, module):
|
||||
"""Process a single BPF struct definition"""
|
||||
"""
|
||||
Process a single BPF struct definition and create its LLVM IR representation.
|
||||
|
||||
Args:
|
||||
cls_node: The AST class node representing the struct
|
||||
module: The LLVM IR module (not used in current implementation)
|
||||
|
||||
Returns:
|
||||
A StructType object containing the struct's type information
|
||||
"""
|
||||
|
||||
fields = parse_struct_fields(cls_node)
|
||||
field_types = list(fields.values())
|
||||
@ -44,7 +62,18 @@ def process_bpf_struct(cls_node, module):
|
||||
|
||||
|
||||
def parse_struct_fields(cls_node):
|
||||
"""Parse fields of a struct class node"""
|
||||
"""
|
||||
Parse fields of a struct class node.
|
||||
|
||||
Args:
|
||||
cls_node: The AST class node representing the struct
|
||||
|
||||
Returns:
|
||||
A dictionary mapping field names to their LLVM IR types
|
||||
|
||||
Raises:
|
||||
TypeError: If a field has an unsupported type annotation
|
||||
"""
|
||||
fields = {}
|
||||
|
||||
for item in cls_node.body:
|
||||
@ -57,7 +86,18 @@ def parse_struct_fields(cls_node):
|
||||
|
||||
|
||||
def get_type_from_ann(annotation):
|
||||
"""Convert an AST annotation node to an LLVM IR type for struct fields"""
|
||||
"""
|
||||
Convert an AST annotation node to an LLVM IR type for struct fields.
|
||||
|
||||
Args:
|
||||
annotation: The AST annotation node (e.g., c_int64, str(32))
|
||||
|
||||
Returns:
|
||||
The corresponding LLVM IR type
|
||||
|
||||
Raises:
|
||||
TypeError: If the annotation type is not supported
|
||||
"""
|
||||
if isinstance(annotation, ast.Call) and isinstance(annotation.func, ast.Name):
|
||||
if annotation.func.id == "str":
|
||||
# Char array
|
||||
@ -72,7 +112,15 @@ def get_type_from_ann(annotation):
|
||||
|
||||
|
||||
def calc_struct_size(field_types):
|
||||
"""Calculate total size of the struct with alignment and padding"""
|
||||
"""
|
||||
Calculate total size of the struct with alignment and padding.
|
||||
|
||||
Args:
|
||||
field_types: List of LLVM IR types for each field
|
||||
|
||||
Returns:
|
||||
The total size of the struct in bytes
|
||||
"""
|
||||
curr_offset = 0
|
||||
for ftype in field_types:
|
||||
if isinstance(ftype, ir.IntType):
|
||||
|
||||
Reference in New Issue
Block a user