Skip to content

Commit 28cc0c5

Browse files
committed
Refactor handle_helper_call
1 parent 99d6c19 commit 28cc0c5

1 file changed

Lines changed: 29 additions & 42 deletions

File tree

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -231,50 +231,37 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func,
231231
def handle_helper_call(call, module, builder, func,
232232
local_sym_tab=None, map_sym_tab=None,
233233
struct_sym_tab=None, local_var_metadata=None):
234-
print(local_var_metadata)
235-
if isinstance(call.func, ast.Name):
236-
func_name = call.func.id
237-
hdl_func = HelperHandlerRegistry.get_handler(func_name)
238-
if hdl_func:
239-
# it is not a map method call
240-
return hdl_func(call, None, module, builder, func, local_sym_tab, struct_sym_tab, local_var_metadata)
241-
else:
234+
"""Process a BPF helper function call and emit the appropriate LLVM IR."""
235+
# Helper function to get map pointer and invoke handler
236+
def invoke_helper(method_name, map_ptr=None):
237+
handler = HelperHandlerRegistry.get_handler(method_name)
238+
if not handler:
242239
raise NotImplementedError(
243-
f"Function {func_name} is not implemented as a helper function.")
240+
f"Helper function '{method_name}' is not implemented.")
241+
return handler(call, map_ptr, module, builder, func,
242+
local_sym_tab, struct_sym_tab, local_var_metadata)
243+
244+
# Handle direct function calls (e.g., print(), ktime())
245+
if isinstance(call.func, ast.Name):
246+
return invoke_helper(call.func.id)
247+
248+
# Handle method calls (e.g., map.lookup(), map.update())
244249
elif isinstance(call.func, ast.Attribute):
245-
# likely a map method call
246-
if isinstance(call.func.value, ast.Call) and isinstance(call.func.value.func, ast.Name):
247-
map_name = call.func.value.func.id
248-
method_name = call.func.attr
249-
if map_sym_tab and map_name in map_sym_tab:
250-
map_ptr = map_sym_tab[map_name]
251-
hdl_func = HelperHandlerRegistry.get_handler(method_name)
252-
if hdl_func:
253-
print(local_var_metadata)
254-
return hdl_func(
255-
call, map_ptr, module, builder, func, local_sym_tab, struct_sym_tab, local_var_metadata)
256-
else:
257-
raise NotImplementedError(
258-
f"Map method {method_name} is not implemented as a helper function.")
259-
else:
260-
raise ValueError(
261-
f"Map variable {map_name} not found in symbol tables.")
262-
elif isinstance(call.func.value, ast.Name):
263-
obj_name = call.func.value.id
264-
method_name = call.func.attr
265-
if map_sym_tab and obj_name in map_sym_tab:
266-
map_ptr = map_sym_tab[obj_name]
267-
hdl_func = HelperHandlerRegistry.get_handler(method_name)
268-
if hdl_func:
269-
return hdl_func(
270-
call, map_ptr, module, builder, func, local_sym_tab, struct_sym_tab, local_var_metadata)
271-
else:
272-
raise NotImplementedError(
273-
f"Map method {method_name} is not implemented as a helper function.")
274-
else:
275-
raise ValueError(
276-
f"Map variable {obj_name} not found in symbol tables.")
250+
method_name = call.func.attr
251+
value = call.func.value
252+
253+
# Get map pointer from different styles of map access
254+
if isinstance(value, ast.Call) and isinstance(value.func, ast.Name):
255+
# Variable style: my_map.lookup(key)
256+
map_name = value.func.id
277257
else:
278258
raise NotImplementedError(
279-
"Attribute not supported for map method calls.")
259+
f"Unsupported map access pattern: {ast.dump(value)}")
260+
261+
# Verify map exists and get pointer
262+
if not map_sym_tab or map_name not in map_sym_tab:
263+
raise ValueError(f"Map '{map_name}' not found in symbol table")
264+
265+
return invoke_helper(method_name, map_sym_tab[map_name])
266+
280267
return None

0 commit comments

Comments
 (0)