@@ -243,6 +243,21 @@ def handle_assign(
243243def handle_cond (
244244 func , module , builder , cond , local_sym_tab , map_sym_tab , structs_sym_tab = None
245245):
246+ """
247+ Evaluate a condition expression and convert it to a boolean value.
248+
249+ Args:
250+ func: The LLVM IR function being built
251+ module: The LLVM IR module
252+ builder: LLVM IR builder
253+ cond: The AST condition node to evaluate
254+ local_sym_tab: Local symbol table
255+ map_sym_tab: Map symbol table
256+ structs_sym_tab: Struct symbol table
257+
258+ Returns:
259+ LLVM IR boolean value representing the condition result
260+ """
246261 val = eval_expr (
247262 func , module , builder , cond , local_sym_tab , map_sym_tab , structs_sym_tab
248263 )[0 ]
@@ -298,6 +313,18 @@ def handle_if(
298313
299314
300315def handle_return (builder , stmt , local_sym_tab , ret_type ):
316+ """
317+ Handle return statements in BPF functions.
318+
319+ Args:
320+ builder: LLVM IR builder
321+ stmt: The AST Return node
322+ local_sym_tab: Local symbol table
323+ ret_type: Expected return type
324+
325+ Returns:
326+ True if a return was emitted, False otherwise
327+ """
301328 logger .info (f"Handling return statement: { ast .dump (stmt )} " )
302329 if stmt .value is None :
303330 return _handle_none_return (builder )
@@ -329,6 +356,23 @@ def process_stmt(
329356 did_return ,
330357 ret_type = ir .IntType (64 ),
331358):
359+ """
360+ Process a single statement in a BPF function.
361+
362+ Args:
363+ func: The LLVM IR function being built
364+ module: The LLVM IR module
365+ builder: LLVM IR builder
366+ stmt: The AST statement node to process
367+ local_sym_tab: Local symbol table
368+ map_sym_tab: Map symbol table
369+ structs_sym_tab: Struct symbol table
370+ did_return: Whether a return has been emitted
371+ ret_type: Expected return type
372+
373+ Returns:
374+ True if a return was emitted, False otherwise
375+ """
332376 logger .info (f"Processing statement: { ast .dump (stmt )} " )
333377 if isinstance (stmt , ast .Expr ):
334378 handle_expr (
@@ -363,6 +407,25 @@ def process_stmt(
363407def allocate_mem (
364408 module , builder , body , func , ret_type , map_sym_tab , local_sym_tab , structs_sym_tab
365409):
410+ """
411+ Pre-allocate stack memory for local variables in a BPF function.
412+
413+ This function scans the function body and creates alloca instructions
414+ for all local variables before processing the function statements.
415+
416+ Args:
417+ module: The LLVM IR module
418+ builder: LLVM IR builder
419+ body: List of AST statements in the function body
420+ func: The LLVM IR function being built
421+ ret_type: Expected return type
422+ map_sym_tab: Map symbol table
423+ local_sym_tab: Local symbol table to populate
424+ structs_sym_tab: Struct symbol table
425+
426+ Returns:
427+ Updated local symbol table
428+ """
366429 for stmt in body :
367430 has_metadata = False
368431 if isinstance (stmt , ast .If ):
@@ -556,6 +619,16 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_t
556619
557620
558621def func_proc (tree , module , chunks , map_sym_tab , structs_sym_tab ):
622+ """
623+ Process all BPF function chunks and generate LLVM IR.
624+
625+ Args:
626+ tree: The Python AST (not used in current implementation)
627+ module: The LLVM IR module to add functions to
628+ chunks: List of AST function nodes decorated with @bpf
629+ map_sym_tab: Map symbol table
630+ structs_sym_tab: Struct symbol table
631+ """
559632 for func_node in chunks :
560633 is_global = False
561634 for decorator in func_node .decorator_list :
@@ -581,6 +654,18 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
581654
582655
583656def infer_return_type (func_node : ast .FunctionDef ):
657+ """
658+ Infer the return type of a BPF function from annotations or return statements.
659+
660+ Args:
661+ func_node: The AST function node
662+
663+ Returns:
664+ String representation of the return type (e.g., 'c_int64')
665+
666+ Raises:
667+ TypeError: If func_node is not a FunctionDef
668+ """
584669 if not isinstance (func_node , (ast .FunctionDef , ast .AsyncFunctionDef )):
585670 raise TypeError ("Expected ast.FunctionDef" )
586671 if func_node .returns is not None :
0 commit comments