-
Notifications
You must be signed in to change notification settings - Fork 3
Struct refactor #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
083ee21
structs_pass cleanup
r41k0u fe91a17
fix structs_proc
r41k0u 5bcc02a
add parser_struct_fields
r41k0u 0d21f84
Remove redundant functions from struct_pass
r41k0u 8450030
Move structs_pass under structs, create StructType
r41k0u 4557b09
Use StructType in struct_pass, fix indexing
r41k0u 32c22c3
fix struct imports
r41k0u fed4c17
fix struct usage in functions_pass
r41k0u e464a3f
fix struct usage in handle_helper_functions
r41k0u 715442d
fix struct usage in expr_pass
r41k0u 3ded17b
Fix size calc for ArrayType in structs
r41k0u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from llvmlite import ir | ||
|
||
|
||
class StructType: | ||
def __init__(self, ir_type, fields, size): | ||
self.ir_type = ir_type | ||
self.fields = fields | ||
self.size = size | ||
|
||
def field_idx(self, field_name): | ||
return list(self.fields.keys()).index(field_name) | ||
|
||
def field_type(self, field_name): | ||
return self.fields[field_name] | ||
|
||
def gep(self, builder, ptr, field_name): | ||
idx = self.field_idx(field_name) | ||
return builder.gep(ptr, [ir.Constant(ir.IntType(32), 0), | ||
ir.Constant(ir.IntType(32), idx)], | ||
inbounds=True) | ||
|
||
def field_size(self, field_name): | ||
fld = self.fields[field_name] | ||
if isinstance(fld, ir.ArrayType): | ||
return fld.count * (fld.element.width // 8) | ||
elif isinstance(fld, ir.IntType): | ||
return fld.width // 8 | ||
elif isinstance(fld, ir.PointerType): | ||
return 8 | ||
|
||
raise TypeError(f"Unsupported field type: {fld}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import ast | ||
import logging | ||
from llvmlite import ir | ||
from pythonbpf.type_deducer import ctypes_to_ir | ||
from .struct_type import StructType | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# TODO: Shall we allow the following syntax: | ||
# struct MyStruct: | ||
# field1: int | ||
# field2: str(32) | ||
# Where int is mapped to c_uint64? | ||
# Shall we just int64, int32 and uint32 similarly? | ||
|
||
|
||
def structs_proc(tree, module, chunks): | ||
""" Process all class definitions to find BPF structs """ | ||
structs_sym_tab = {} | ||
for cls_node in chunks: | ||
if is_bpf_struct(cls_node): | ||
print(f"Found BPF struct: {cls_node.name}") | ||
struct_info = process_bpf_struct(cls_node, module) | ||
structs_sym_tab[cls_node.name] = struct_info | ||
return structs_sym_tab | ||
|
||
|
||
def is_bpf_struct(cls_node): | ||
return any( | ||
isinstance(decorator, ast.Name) and decorator.id == "struct" | ||
for decorator in cls_node.decorator_list | ||
) | ||
|
||
|
||
def process_bpf_struct(cls_node, module): | ||
""" Process a single BPF struct definition """ | ||
|
||
fields = parse_struct_fields(cls_node) | ||
field_types = list(fields.values()) | ||
total_size = calc_struct_size(field_types) | ||
struct_type = ir.LiteralStructType(field_types) | ||
logger.info(f"Created struct {cls_node.name} with fields {fields.keys()}") | ||
return StructType(struct_type, fields, total_size) | ||
|
||
|
||
def parse_struct_fields(cls_node): | ||
""" Parse fields of a struct class node """ | ||
fields = {} | ||
|
||
for item in cls_node.body: | ||
if isinstance(item, ast.AnnAssign) and \ | ||
isinstance(item.target, ast.Name): | ||
fields[item.target.id] = get_type_from_ann(item.annotation) | ||
else: | ||
logger.error(f"Unsupported struct field: {ast.dump(item)}") | ||
raise TypeError(f"Unsupported field in {ast.dump(cls_node)}") | ||
return fields | ||
|
||
|
||
def get_type_from_ann(annotation): | ||
""" Convert an AST annotation node to an LLVM IR type for struct fields""" | ||
if isinstance(annotation, ast.Call) and \ | ||
isinstance(annotation.func, ast.Name): | ||
if annotation.func.id == "str": | ||
# Char array | ||
# Assumes constant integer argument | ||
length = annotation.args[0].value | ||
return ir.ArrayType(ir.IntType(8), length) | ||
elif isinstance(annotation, ast.Name): | ||
# Int type, written as c_int64, c_uint32, etc. | ||
return ctypes_to_ir(annotation.id) | ||
|
||
raise TypeError(f"Unsupported annotation type: {ast.dump(annotation)}") | ||
|
||
|
||
def calc_struct_size(field_types): | ||
""" Calculate total size of the struct with alignment and padding """ | ||
curr_offset = 0 | ||
for ftype in field_types: | ||
if isinstance(ftype, ir.IntType): | ||
fsize = ftype.width // 8 | ||
alignment = fsize | ||
elif isinstance(ftype, ir.ArrayType): | ||
fsize = ftype.count * (ftype.element.width // 8) | ||
alignment = ftype.element.width // 8 | ||
elif isinstance(ftype, ir.PointerType): | ||
# We won't encounter this rn, but for the future | ||
fsize = 8 | ||
alignment = 8 | ||
else: | ||
raise TypeError(f"Unsupported field type: {ftype}") | ||
|
||
padding = (alignment - (curr_offset % alignment)) % alignment | ||
curr_offset += padding + fsize | ||
|
||
final_padding = (8 - (curr_offset % 8)) % 8 | ||
return curr_offset + final_padding |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.