@@ -16,7 +16,7 @@ def bpf_ktime_get_ns_emitter(call, map_ptr, module, builder, func, local_sym_tab
1616 return result
1717
1818
19- def bpf_map_lookup_elem_emitter (call , map_ptr , module , builder , local_sym_tab = None , struct_sym_tab = None ):
19+ def bpf_map_lookup_elem_emitter (call , map_ptr , module , builder , func , local_sym_tab = None , struct_sym_tab = None ):
2020 """
2121 Emit LLVM IR for bpf_map_lookup_elem helper function call.
2222 """
@@ -172,7 +172,7 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None)
172172 ir .IntType (32 ), len (fmt_str ))], tail = True )
173173
174174
175- def bpf_map_update_elem_emitter (call , map_ptr , module , builder , local_sym_tab = None , struct_sym_tab = None ):
175+ def bpf_map_update_elem_emitter (call , map_ptr , module , builder , func , local_sym_tab = None , struct_sym_tab = None ):
176176 """
177177 Emit LLVM IR for bpf_map_update_elem helper function call.
178178 Expected call signature: map.update(key, value, flags=0)
@@ -268,7 +268,7 @@ def bpf_map_update_elem_emitter(call, map_ptr, module, builder, local_sym_tab=No
268268 return result
269269
270270
271- def bpf_map_delete_elem_emitter (call , map_ptr , module , builder , local_sym_tab = None , struct_sym_tab = None ):
271+ def bpf_map_delete_elem_emitter (call , map_ptr , module , builder , func , local_sym_tab = None , struct_sym_tab = None ):
272272 """
273273 Emit LLVM IR for bpf_map_delete_elem helper function call.
274274 Expected call signature: map.delete(key)
@@ -340,8 +340,31 @@ def bpf_get_current_pid_tgid_emitter(call, map_ptr, module, builder, func, local
340340 return pid
341341
342342
343- def bpf_perf_event_output_handler (call , map_ptr , module , builder , local_sym_tab = None , struct_sym_tab = None ):
344- pass
343+ def bpf_perf_event_output_handler (call , map_ptr , module , builder , func , local_sym_tab = None , struct_sym_tab = None , local_var_metadata = None ):
344+ if len (call .args ) != 1 :
345+ raise ValueError ("Perf event output expects exactly one argument (data), got "
346+ f"{ len (call .args )} " )
347+ data_arg = call .args [0 ]
348+ ctx_ptr = func .args [0 ] # First argument to the function is ctx
349+
350+ if isinstance (data_arg , ast .Name ):
351+ data_name = data_arg .id
352+ if local_sym_tab and data_name in local_sym_tab :
353+ data_ptr = local_sym_tab [data_name ]
354+ else :
355+ raise ValueError (
356+ f"Data variable { data_name } not found in local symbol table." )
357+ # Check is data_name is a struct
358+ if local_var_metadata and data_name in local_var_metadata :
359+ data_type = local_var_metadata [data_name ]
360+ if data_type in struct_sym_tab :
361+ struct_info = struct_sym_tab [data_type ]
362+ data_size = 0
363+ for field_type in struct_info ["type" ].elements :
364+ if isinstance (field_type , ir .IntType ):
365+ data_size += field_type .width // 8
366+ elif isinstance (field_type , ir .PointerType ):
367+ data_size += 8
345368
346369
347370helper_func_list = {
@@ -355,7 +378,7 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, local_sym_tab=
355378}
356379
357380
358- def handle_helper_call (call , module , builder , func , local_sym_tab = None , map_sym_tab = None , struct_sym_tab = None ):
381+ def handle_helper_call (call , module , builder , func , local_sym_tab = None , map_sym_tab = None , struct_sym_tab = None , local_var_metadata = None ):
359382 if isinstance (call .func , ast .Name ):
360383 func_name = call .func .id
361384 if func_name in helper_func_list :
@@ -372,14 +395,29 @@ def handle_helper_call(call, module, builder, func, local_sym_tab=None, map_sym_
372395 if map_sym_tab and map_name in map_sym_tab :
373396 map_ptr = map_sym_tab [map_name ]
374397 if method_name in helper_func_list :
398+ print (local_var_metadata )
375399 return helper_func_list [method_name ](
376- call , map_ptr , module , builder , local_sym_tab , struct_sym_tab )
400+ call , map_ptr , module , builder , func , local_sym_tab , struct_sym_tab , local_var_metadata )
377401 else :
378402 raise NotImplementedError (
379403 f"Map method { method_name } is not implemented as a helper function." )
380404 else :
381405 raise ValueError (
382406 f"Map variable { map_name } not found in symbol tables." )
407+ elif isinstance (call .func .value , ast .Name ):
408+ obj_name = call .func .value .id
409+ method_name = call .func .attr
410+ if map_sym_tab and obj_name in map_sym_tab :
411+ map_ptr = map_sym_tab [obj_name ]
412+ if method_name in helper_func_list :
413+ return helper_func_list [method_name ](
414+ call , map_ptr , module , builder , func , local_sym_tab , struct_sym_tab , local_var_metadata )
415+ else :
416+ raise NotImplementedError (
417+ f"Map method { method_name } is not implemented as a helper function." )
418+ else :
419+ raise ValueError (
420+ f"Map variable { obj_name } not found in symbol tables." )
383421 else :
384422 raise NotImplementedError (
385423 "Attribute not supported for map method calls." )
0 commit comments