Skip to content

Commit 2228982

Browse files
format chore
1 parent d86dd68 commit 2228982

18 files changed

+134
-128
lines changed

pythonbpf/binary_ops.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def get_operand_value(operand, builder, local_sym_tab):
4444
def handle_binary_op_impl(rval, builder, local_sym_tab):
4545
"""
4646
Handle binary operations and emit corresponding LLVM IR instructions.
47-
47+
4848
Args:
4949
rval: The AST BinOp node representing the binary operation
5050
builder: LLVM IR builder for emitting instructions
5151
local_sym_tab: Symbol table mapping variable names to their IR representations
52-
52+
5353
Returns:
5454
The LLVM IR value representing the result of the binary operation
5555
"""
@@ -83,13 +83,13 @@ def handle_binary_op_impl(rval, builder, local_sym_tab):
8383
def handle_binary_op(rval, builder, var_name, local_sym_tab):
8484
"""
8585
Handle binary operations and optionally store the result to a variable.
86-
86+
8787
Args:
8888
rval: The AST BinOp node representing the binary operation
8989
builder: LLVM IR builder for emitting instructions
9090
var_name: Optional variable name to store the result
9191
local_sym_tab: Symbol table mapping variable names to their IR representations
92-
92+
9393
Returns:
9494
A tuple of (result_value, result_type)
9595
"""

pythonbpf/codegen.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def find_bpf_chunks(tree):
4747
def processor(source_code, filename, module):
4848
"""
4949
Process Python source code and convert BPF-decorated functions to LLVM IR.
50-
50+
5151
Args:
5252
source_code: The Python source code to process
5353
filename: The name of the source file
@@ -74,12 +74,12 @@ def processor(source_code, filename, module):
7474
def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
7575
"""
7676
Compile a Python BPF program to LLVM IR.
77-
77+
7878
Args:
7979
filename: Path to the Python source file containing BPF programs
8080
output: Path where the LLVM IR (.ll) file will be written
8181
loglevel: Logging level for compilation messages
82-
82+
8383
Returns:
8484
Path to the generated LLVM IR file
8585
"""
@@ -158,13 +158,13 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
158158
def compile(loglevel=logging.INFO) -> bool:
159159
"""
160160
Compile the calling Python BPF program to an object file.
161-
161+
162162
This function should be called from a Python file containing BPF programs.
163163
It will compile the calling file to LLVM IR and then to a BPF object file.
164-
164+
165165
Args:
166166
loglevel: Logging level for compilation messages
167-
167+
168168
Returns:
169169
True if compilation succeeded, False otherwise
170170
"""
@@ -203,13 +203,13 @@ def compile(loglevel=logging.INFO) -> bool:
203203
def BPF(loglevel=logging.INFO) -> BpfProgram:
204204
"""
205205
Compile the calling Python BPF program and return a BpfProgram object.
206-
206+
207207
This function compiles the calling file's BPF programs to an object file
208208
and loads it into a BpfProgram object for immediate use.
209-
209+
210210
Args:
211211
loglevel: Logging level for compilation messages
212-
212+
213213
Returns:
214214
A BpfProgram object that can be used to load and attach BPF programs
215215
"""

pythonbpf/debuginfo/debug_info_generator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
class DebugInfoGenerator:
1111
"""
1212
Generator for DWARF/BTF debug information in LLVM IR modules.
13-
13+
1414
This class provides methods to create debug metadata for BPF programs,
1515
including types, structs, globals, and compilation units.
1616
"""
17-
17+
1818
def __init__(self, module):
1919
"""
2020
Initialize the debug info generator.
21-
21+
2222
Args:
2323
module: LLVM IR module to attach debug info to
2424
"""
@@ -28,7 +28,7 @@ def __init__(self, module):
2828
def generate_file_metadata(self, filename, dirname):
2929
"""
3030
Generate file metadata for debug info.
31-
31+
3232
Args:
3333
filename: Name of the source file
3434
dirname: Directory containing the source file
@@ -46,7 +46,7 @@ def generate_debug_cu(
4646
):
4747
"""
4848
Generate debug compile unit metadata.
49-
49+
5050
Args:
5151
language: DWARF language code (e.g., DW_LANG_C11)
5252
producer: Compiler/producer string
@@ -114,11 +114,11 @@ def create_array_type(self, base_type: Any, count: int) -> Any:
114114
def _compute_array_size(base_type: Any, count: int) -> int:
115115
"""
116116
Compute the size of an array in bits.
117-
117+
118118
Args:
119119
base_type: The base type of the array
120120
count: Number of elements in the array
121-
121+
122122
Returns:
123123
Total size in bits
124124
"""

pythonbpf/debuginfo/dtypes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class DwarfBehaviorEnum:
77
"""DWARF module flag behavior constants for LLVM."""
8+
89
ERROR_IF_MISMATCH = ir.Constant(ir.IntType(32), 1)
910
WARNING_IF_MISMATCH = ir.Constant(ir.IntType(32), 2)
1011
OVERRIDE_USE_LARGEST = ir.Constant(ir.IntType(32), 7)

pythonbpf/decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ def struct(cls):
3333
def section(name: str):
3434
"""
3535
Decorator to specify the ELF section name for a BPF program.
36-
36+
3737
Args:
3838
name: The section name (e.g., 'xdp', 'tracepoint/syscalls/sys_enter_execve')
39-
39+
4040
Returns:
4141
A decorator function that marks the function with the section name
4242
"""
43+
4344
def wrapper(fn):
4445
"""Decorator that sets the section name on the function."""
4546
fn._section = name

pythonbpf/expr/expr_pass.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def eval_expr(
342342
):
343343
"""
344344
Evaluate an expression and return its LLVM IR value and type.
345-
345+
346346
Args:
347347
func: The LLVM IR function being built
348348
module: The LLVM IR module
@@ -351,7 +351,7 @@ def eval_expr(
351351
local_sym_tab: Local symbol table
352352
map_sym_tab: Map symbol table
353353
structs_sym_tab: Struct symbol table
354-
354+
355355
Returns:
356356
A tuple of (value, type) or None if evaluation fails
357357
"""

pythonbpf/expr/type_normalization.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
def _get_base_type_and_depth(ir_type):
2727
"""
2828
Get the base type and pointer depth for an LLVM IR type.
29-
29+
3030
Args:
3131
ir_type: The LLVM IR type to analyze
32-
32+
3333
Returns:
3434
A tuple of (base_type, depth) where depth is the number of pointer levels
3535
"""
@@ -44,13 +44,13 @@ def _get_base_type_and_depth(ir_type):
4444
def _deref_to_depth(func, builder, val, target_depth):
4545
"""
4646
Dereference a pointer to a certain depth with null checks.
47-
47+
4848
Args:
4949
func: The LLVM IR function being built
5050
builder: LLVM IR builder
5151
val: The pointer value to dereference
5252
target_depth: Number of levels to dereference
53-
53+
5454
Returns:
5555
The dereferenced value, or None if dereferencing fails
5656
"""
@@ -101,13 +101,13 @@ def _deref_to_depth(func, builder, val, target_depth):
101101
def _normalize_types(func, builder, lhs, rhs):
102102
"""
103103
Normalize types for comparison by casting or dereferencing as needed.
104-
104+
105105
Args:
106106
func: The LLVM IR function being built
107107
builder: LLVM IR builder
108108
lhs: Left-hand side value
109109
rhs: Right-hand side value
110-
110+
111111
Returns:
112112
A tuple of (normalized_lhs, normalized_rhs) or (None, None) on error
113113
"""
@@ -138,11 +138,11 @@ def _normalize_types(func, builder, lhs, rhs):
138138
def convert_to_bool(builder, val):
139139
"""
140140
Convert an LLVM IR value to a boolean (i1) type.
141-
141+
142142
Args:
143143
builder: LLVM IR builder
144144
val: The value to convert
145-
145+
146146
Returns:
147147
An i1 boolean value
148148
"""
@@ -158,14 +158,14 @@ def convert_to_bool(builder, val):
158158
def handle_comparator(func, builder, op, lhs, rhs):
159159
"""
160160
Handle comparison operations between two values.
161-
161+
162162
Args:
163163
func: The LLVM IR function being built
164164
builder: LLVM IR builder
165165
op: The AST comparison operator node
166166
lhs: Left-hand side value
167167
rhs: Right-hand side value
168-
168+
169169
Returns:
170170
A tuple of (result, ir.IntType(1)) or None on error
171171
"""

pythonbpf/functions/functions_pass.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
class LocalSymbol:
2828
"""
2929
Represents a local variable in a BPF function.
30-
30+
3131
Attributes:
3232
var: LLVM IR alloca instruction for the variable
3333
ir_type: LLVM IR type of the variable
3434
metadata: Optional metadata (e.g., struct type name)
3535
"""
36+
3637
var: ir.AllocaInstr
3738
ir_type: ir.Type
3839
metadata: Any = None
@@ -262,7 +263,7 @@ def handle_cond(
262263
):
263264
"""
264265
Evaluate a condition expression and convert it to a boolean value.
265-
266+
266267
Args:
267268
func: The LLVM IR function being built
268269
module: The LLVM IR module
@@ -271,7 +272,7 @@ def handle_cond(
271272
local_sym_tab: Local symbol table
272273
map_sym_tab: Map symbol table
273274
structs_sym_tab: Struct symbol table
274-
275+
275276
Returns:
276277
LLVM IR boolean value representing the condition result
277278
"""
@@ -332,13 +333,13 @@ def handle_if(
332333
def handle_return(builder, stmt, local_sym_tab, ret_type):
333334
"""
334335
Handle return statements in BPF functions.
335-
336+
336337
Args:
337338
builder: LLVM IR builder
338339
stmt: The AST Return node
339340
local_sym_tab: Local symbol table
340341
ret_type: Expected return type
341-
342+
342343
Returns:
343344
True if a return was emitted, False otherwise
344345
"""
@@ -375,7 +376,7 @@ def process_stmt(
375376
):
376377
"""
377378
Process a single statement in a BPF function.
378-
379+
379380
Args:
380381
func: The LLVM IR function being built
381382
module: The LLVM IR module
@@ -386,7 +387,7 @@ def process_stmt(
386387
structs_sym_tab: Struct symbol table
387388
did_return: Whether a return has been emitted
388389
ret_type: Expected return type
389-
390+
390391
Returns:
391392
True if a return was emitted, False otherwise
392393
"""
@@ -426,10 +427,10 @@ def allocate_mem(
426427
):
427428
"""
428429
Pre-allocate stack memory for local variables in a BPF function.
429-
430+
430431
This function scans the function body and creates alloca instructions
431432
for all local variables before processing the function statements.
432-
433+
433434
Args:
434435
module: The LLVM IR module
435436
builder: LLVM IR builder
@@ -439,7 +440,7 @@ def allocate_mem(
439440
map_sym_tab: Map symbol table
440441
local_sym_tab: Local symbol table to populate
441442
structs_sym_tab: Struct symbol table
442-
443+
443444
Returns:
444445
Updated local symbol table
445446
"""
@@ -638,7 +639,7 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_t
638639
def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
639640
"""
640641
Process all BPF function chunks and generate LLVM IR.
641-
642+
642643
Args:
643644
tree: The Python AST (not used in current implementation)
644645
module: The LLVM IR module to add functions to
@@ -673,13 +674,13 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
673674
def infer_return_type(func_node: ast.FunctionDef):
674675
"""
675676
Infer the return type of a BPF function from annotations or return statements.
676-
677+
677678
Args:
678679
func_node: The AST function node
679-
680+
680681
Returns:
681682
String representation of the return type (e.g., 'c_int64')
682-
683+
683684
Raises:
684685
TypeError: If func_node is not a FunctionDef
685686
"""

0 commit comments

Comments
 (0)