Skip to content

Commit 192e03a

Browse files
committed
Add _handle_typed_constant_return
1 parent 6f02b61 commit 192e03a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pythonbpf/functions/return_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import ast
33

44
from llvmlite import ir
5+
from pythonbpf.type_deducer import ctypes_to_ir
56

67
logger: logging.Logger = logging.getLogger(__name__)
78

@@ -21,6 +22,23 @@ def _handle_none_return(builder) -> bool:
2122
return True
2223

2324

25+
def _handle_typed_constant_return(call_type, return_value, builder, ret_type) -> bool:
26+
"""Handle typed constant return like: return c_int64(42)"""
27+
28+
# call_type = stmt.value.func.id
29+
expected_type = ctypes_to_ir(call_type)
30+
31+
if expected_type != ret_type:
32+
raise ValueError(
33+
f"Return type mismatch: expected {ret_type}, got {expected_type}"
34+
)
35+
36+
# return_value = stmt.value.args[0].value
37+
builder.ret(ir.Constant(ret_type, return_value))
38+
logger.debug(f"Generated typed constant return: {call_type}({return_value})")
39+
return True
40+
41+
2442
def _handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool:
2543
"""Handle XDP returns"""
2644
if not isinstance(stmt.value, ast.Name):

0 commit comments

Comments
 (0)