2
2
import ast
3
3
4
4
from llvmlite import ir
5
- from pythonbpf .type_deducer import ctypes_to_ir
6
- from pythonbpf .binary_ops import handle_binary_op
7
5
8
6
logger : logging .Logger = logging .getLogger (__name__ )
9
7
@@ -28,89 +26,6 @@ def _handle_none_return(builder) -> bool:
28
26
return True
29
27
30
28
31
- def _handle_typed_constant_return (call_type , return_value , builder , ret_type ) -> bool :
32
- """Handle typed constant return like: return c_int64(42)"""
33
-
34
- expected_type = ctypes_to_ir (call_type )
35
-
36
- if expected_type != ret_type :
37
- raise ValueError (
38
- f"Return type mismatch: expected { ret_type } , got { expected_type } "
39
- )
40
-
41
- # return_value = stmt.value.args[0].value
42
- builder .ret (ir .Constant (ret_type , return_value ))
43
- logger .debug (f"Generated typed constant return: { call_type } ({ return_value } )" )
44
- return True
45
-
46
-
47
- def _handle_binop_return (arg , builder , ret_type , local_sym_tab ) -> bool :
48
- """Handle return with binary operation: return c_int64(x + 1)"""
49
-
50
- result = handle_binary_op (arg , builder , None , local_sym_tab )
51
-
52
- if result is None :
53
- raise ValueError ("Failed to evaluate binary operation in return statement" )
54
-
55
- val , val_type = result
56
-
57
- if val_type != ret_type :
58
- raise ValueError (f"Return type mismatch: expected { ret_type } , got { val_type } " )
59
-
60
- builder .ret (val )
61
- logger .debug (f"Generated binary operation return: { val } " )
62
- return True
63
-
64
-
65
- def _handle_variable_return (var_name , builder , ret_type , local_sym_tab ) -> bool :
66
- """Handle return of a variable: return c_int64(my_var)"""
67
-
68
- if var_name not in local_sym_tab :
69
- raise ValueError (f"Undefined variable in return: { var_name } " )
70
-
71
- var = local_sym_tab [var_name ].var
72
- val = builder .load (var )
73
-
74
- if val .type != ret_type :
75
- raise ValueError (f"Return type mismatch: expected { ret_type } , got { val .type } " )
76
-
77
- builder .ret (val )
78
- logger .debug (f"Generated variable return: { var_name } " )
79
- return True
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
-
114
29
def _is_xdp_name (name : str ) -> bool :
115
30
"""Check if a name is an XDP action"""
116
31
return name in XDP_ACTIONS
0 commit comments