Skip to content

Commit c6fef16

Browse files
committed
Add _handle_binop_return
1 parent 192e03a commit c6fef16

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pythonbpf/functions/return_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from llvmlite import ir
55
from pythonbpf.type_deducer import ctypes_to_ir
6+
from pythonbpf.binary_ops import handle_binary_op
67

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

@@ -39,6 +40,25 @@ def _handle_typed_constant_return(call_type, return_value, builder, ret_type) ->
3940
return True
4041

4142

43+
def _handle_binop_return(arg, builder, ret_type, local_sym_tab) -> bool:
44+
"""Handle return with binary operation: return c_int64(x + 1)"""
45+
46+
# result = handle_binary_op(stmt.value.args[0], builder, None, local_sym_tab)
47+
result = handle_binary_op(arg, builder, None, local_sym_tab)
48+
49+
if result is None:
50+
raise ValueError("Failed to evaluate binary operation in return statement")
51+
52+
val, val_type = result
53+
54+
if val_type != ret_type:
55+
raise ValueError(f"Return type mismatch: expected {ret_type}, got {val_type}")
56+
57+
builder.ret(val)
58+
logger.debug(f"Generated binary operation return: {val}")
59+
return True
60+
61+
4262
def _handle_xdp_return(stmt: ast.Return, builder, ret_type) -> bool:
4363
"""Handle XDP returns"""
4464
if not isinstance(stmt.value, ast.Name):

0 commit comments

Comments
 (0)