Add _handle_typed_constant_return

This commit is contained in:
Pragyansh Chaturvedi
2025-10-05 23:59:04 +05:30
parent 6f02b61527
commit 192e03aa98

View File

@ -2,6 +2,7 @@ import logging
import ast
from llvmlite import ir
from pythonbpf.type_deducer import ctypes_to_ir
logger: logging.Logger = logging.getLogger(__name__)
@ -21,6 +22,23 @@ def _handle_none_return(builder) -> bool:
return True
def _handle_typed_constant_return(call_type, return_value, builder, ret_type) -> bool:
"""Handle typed constant return like: return c_int64(42)"""
# call_type = stmt.value.func.id
expected_type = ctypes_to_ir(call_type)
if expected_type != ret_type:
raise ValueError(
f"Return type mismatch: expected {ret_type}, got {expected_type}"
)
# return_value = stmt.value.args[0].value
builder.ret(ir.Constant(ret_type, return_value))
logger.debug(f"Generated typed constant return: {call_type}({return_value})")
return True
def _handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool:
"""Handle XDP returns"""
if not isinstance(stmt.value, ast.Name):