Skip to content

Commit a622c53

Browse files
committed
Add deref
1 parent a4f1363 commit a622c53

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

pythonbpf/expr_pass.py

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def _handle_attribute_expr(
4141
attr_name = expr.attr
4242
if var_name in local_sym_tab:
4343
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
44-
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
44+
logger.info(f"Loading attribute {
45+
attr_name} from variable {var_name}")
4546
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
4647

4748
metadata = structs_sym_tab[var_metadata]
@@ -53,6 +54,41 @@ def _handle_attribute_expr(
5354
return None
5455

5556

57+
def _handle_deref_call(expr: ast.Call, local_sym_tab: Dict, builder: ir.IRBuilder):
58+
"""Handle deref function calls."""
59+
logger.info(f"Handling deref {ast.dump(expr)}")
60+
if len(expr.args) != 1:
61+
logger.info("deref takes exactly one argument")
62+
return None
63+
64+
arg = expr.args[0]
65+
if (
66+
isinstance(arg, ast.Call)
67+
and isinstance(arg.func, ast.Name)
68+
and arg.func.id == "deref"
69+
):
70+
logger.info("Multiple deref not supported")
71+
return None
72+
73+
if isinstance(arg, ast.Name):
74+
if arg.id in local_sym_tab:
75+
arg_ptr = local_sym_tab[arg.id].var
76+
else:
77+
logger.info(f"Undefined variable {arg.id}")
78+
return None
79+
else:
80+
logger.info("Unsupported argument type for deref")
81+
return None
82+
83+
if arg_ptr is None:
84+
logger.info("Failed to evaluate deref argument")
85+
return None
86+
87+
# Load the value from pointer
88+
val = builder.load(arg_ptr)
89+
return val, local_sym_tab[arg.id].ir_type
90+
91+
5692
def eval_expr(
5793
func,
5894
module,
@@ -68,48 +104,24 @@ def eval_expr(
68104
elif isinstance(expr, ast.Constant):
69105
return _handle_constant_expr(expr)
70106
elif isinstance(expr, ast.Call):
107+
if isinstance(expr.func, ast.Name) and expr.func.id == "deref":
108+
return _handle_deref_call(expr, local_sym_tab, builder)
109+
71110
# delayed import to avoid circular dependency
72111
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
73112

74-
if isinstance(expr.func, ast.Name):
75-
# check deref
76-
if expr.func.id == "deref":
77-
logger.info(f"Handling deref {ast.dump(expr)}")
78-
if len(expr.args) != 1:
79-
logger.info("deref takes exactly one argument")
80-
return None
81-
arg = expr.args[0]
82-
if (
83-
isinstance(arg, ast.Call)
84-
and isinstance(arg.func, ast.Name)
85-
and arg.func.id == "deref"
86-
):
87-
logger.info("Multiple deref not supported")
88-
return None
89-
if isinstance(arg, ast.Name):
90-
if arg.id in local_sym_tab:
91-
arg = local_sym_tab[arg.id].var
92-
else:
93-
logger.info(f"Undefined variable {arg.id}")
94-
return None
95-
if arg is None:
96-
logger.info("Failed to evaluate deref argument")
97-
return None
98-
# Since we are handling only name case, directly take type from sym tab
99-
val = builder.load(arg)
100-
return val, local_sym_tab[expr.args[0].id].ir_type
101-
102-
# check for helpers
103-
if HelperHandlerRegistry.has_handler(expr.func.id):
104-
return handle_helper_call(
105-
expr,
106-
module,
107-
builder,
108-
func,
109-
local_sym_tab,
110-
map_sym_tab,
111-
structs_sym_tab,
112-
)
113+
if isinstance(expr.func, ast.Name) and HelperHandlerRegistry.has_handler(
114+
expr.func.id
115+
):
116+
return handle_helper_call(
117+
expr,
118+
module,
119+
builder,
120+
func,
121+
local_sym_tab,
122+
map_sym_tab,
123+
structs_sym_tab,
124+
)
113125
elif isinstance(expr.func, ast.Attribute):
114126
logger.info(f"Handling method call: {ast.dump(expr.func)}")
115127
if isinstance(expr.func.value, ast.Call) and isinstance(

0 commit comments

Comments
 (0)