7
7
8
8
logger : logging .Logger = logging .getLogger (__name__ )
9
9
10
+ # TODO: Ideally there should be only 3 cases:
11
+ # - Return none
12
+ # - Return XDP
13
+ # - Return expr
14
+
10
15
XDP_ACTIONS = {
11
16
"XDP_ABORTED" : 0 ,
12
17
"XDP_DROP" : 1 ,
@@ -26,7 +31,6 @@ def _handle_none_return(builder) -> bool:
26
31
def _handle_typed_constant_return (call_type , return_value , builder , ret_type ) -> bool :
27
32
"""Handle typed constant return like: return c_int64(42)"""
28
33
29
- # call_type = stmt.value.func.id
30
34
expected_type = ctypes_to_ir (call_type )
31
35
32
36
if expected_type != ret_type :
@@ -43,7 +47,6 @@ def _handle_typed_constant_return(call_type, return_value, builder, ret_type) ->
43
47
def _handle_binop_return (arg , builder , ret_type , local_sym_tab ) -> bool :
44
48
"""Handle return with binary operation: return c_int64(x + 1)"""
45
49
46
- # result = handle_binary_op(stmt.value.args[0], builder, None, local_sym_tab)
47
50
result = handle_binary_op (arg , builder , None , local_sym_tab )
48
51
49
52
if result is None :
@@ -62,8 +65,6 @@ def _handle_binop_return(arg, builder, ret_type, local_sym_tab) -> bool:
62
65
def _handle_variable_return (var_name , builder , ret_type , local_sym_tab ) -> bool :
63
66
"""Handle return of a variable: return c_int64(my_var)"""
64
67
65
- # var_name = stmt.value.args[0].id
66
-
67
68
if var_name not in local_sym_tab :
68
69
raise ValueError (f"Undefined variable in return: { var_name } " )
69
70
@@ -78,6 +79,38 @@ def _handle_variable_return(var_name, builder, ret_type, local_sym_tab) -> bool:
78
79
return True
79
80
80
81
82
+ def _handle_wrapped_return (stmt : ast .Return , builder , ret_type , local_sym_tab ) -> bool :
83
+ """Handle wrapped returns: return c_int64(42), return c_int64(x + 1), return c_int64(my_var)"""
84
+
85
+ if not (
86
+ isinstance (stmt .value , ast .Call )
87
+ and isinstance (stmt .value .func , ast .Name )
88
+ and len (stmt .value .args ) == 1
89
+ ):
90
+ return False
91
+
92
+ arg = stmt .value .args [0 ]
93
+
94
+ # Case 1: Constant value - return c_int64(42)
95
+ if isinstance (arg , ast .Constant ) and isinstance (arg .value , int ):
96
+ return _handle_typed_constant_return (
97
+ stmt .value .func .id , arg .value , builder , ret_type
98
+ )
99
+
100
+ # Case 2: Binary operation - return c_int64(x + 1)
101
+ elif isinstance (arg , ast .BinOp ):
102
+ return _handle_binop_return (arg , builder , ret_type , local_sym_tab )
103
+
104
+ # Case 3: Variable - return c_int64(my_var)
105
+ elif isinstance (arg , ast .Name ):
106
+ if not arg .id :
107
+ raise ValueError ("Variable return must have a type, e.g., c_int64" )
108
+ return _handle_variable_return (arg .id , builder , ret_type , local_sym_tab )
109
+
110
+ else :
111
+ raise ValueError (f"Unsupported return argument type: { type (arg ).__name__ } " )
112
+
113
+
81
114
def _handle_xdp_return (stmt : ast .Return , builder , ret_type ) -> bool :
82
115
"""Handle XDP returns"""
83
116
if not isinstance (stmt .value , ast .Name ):
0 commit comments