11from llvmlite import ir
22import ast
33
4- from .bpf_helper_handler import bpf_printk_emitter , bpf_ktime_get_ns_emitter , bpf_map_lookup_elem_emitter
4+ from .bpf_helper_handler import helper_func_list , handle_helper_call
55from .type_deducer import ctypes_to_ir
66
77
@@ -60,55 +60,16 @@ def handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab):
6060 else :
6161 print (f"Unsupported assignment call type: { call_type } " )
6262 elif isinstance (rval .func , ast .Attribute ):
63- if isinstance (rval .func .attr , str ) and rval .func .attr == "lookup" :
64- # Get map name and check symtab
65- # maps are called as funcs
66- if isinstance (rval .func .value , ast .Call ) and isinstance (rval .func .value .func , ast .Name ):
67- map_name = rval .func .value .func .id
68- if map_name in map_sym_tab :
69- map_global = map_sym_tab [map_name ]
70- print (f"Found map { map_name } in symtab for lookup" )
71- if len (rval .args ) != 1 :
72- print ("Unsupported lookup with != 1 arg" )
73- return
74- key_arg = rval .args [0 ]
75- print (f"Lookup key arg type: { type (key_arg )} " )
76- # TODO: implement a parse_arg ffs as this can be a fucking expr
77- if isinstance (key_arg , ast .Constant ) and isinstance (key_arg .value , int ):
78- key_val = key_arg .value
79- key_type = ir .IntType (64 )
80- print (f"Key type: { key_type } " )
81- print (f"Key val: { key_val } " )
82- key_var = builder .alloca (key_type )
83- key_var .align = key_type // 8
84- builder .store (ir .Constant (
85- key_type , key_val ), key_var )
86- elif isinstance (key_arg , ast .Name ):
87- # Check in local symtab first
88- if key_arg .id in local_sym_tab :
89- key_var = local_sym_tab [key_arg .id ]
90- key_type = key_var .type .pointee
91- elif key_arg .id in map_sym_tab :
92- key_var = map_sym_tab [key_arg .id ]
93- key_type = key_var .type .pointee
94- else :
95- print ("Key variable "
96- f"{ key_arg .id } not found in symtabs" )
97- return
98- print (f"Found key variable { key_arg .id } in symtab" )
99- print (f"Key type: { key_type } " )
100- else :
101- print ("Unsupported lookup key arg" )
102- return
103-
104- # TODO: generate call to bpf_map_lookup_elem
105- result_ptr = bpf_map_lookup_elem_emitter (
106- map_global , key_var , module , builder )
107-
108- else :
109- print (f"Map { map_name } not found in symbol table" )
63+ if isinstance (rval .func .value , ast .Call ) and isinstance (rval .func .value .func , ast .Name ):
64+ map_name = rval .func .value .func .id
65+ method_name = rval .func .attr
66+ if map_name in map_sym_tab :
67+ map_ptr = map_sym_tab [map_name ]
68+ if method_name in helper_func_list :
69+ handle_helper_call (
70+ rval , module , builder , None , local_sym_tab , map_sym_tab )
11071 else :
111- print ("Unsupported assignment from method call" )
72+ print ("Unsupported assignment call structure " )
11273
11374
11475def process_func_body (module , builder , func_node , func , ret_type , map_sym_tab ):
@@ -121,10 +82,11 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
12182 for stmt in func_node .body :
12283 if isinstance (stmt , ast .Expr ) and isinstance (stmt .value , ast .Call ):
12384 call = stmt .value
124- if isinstance (call .func , ast .Name ) and call .func .id == "print" :
125- bpf_printk_emitter (call , module , builder , func )
126- if isinstance (call .func , ast .Name ) and call .func .id == "ktime" :
127- bpf_ktime_get_ns_emitter (call , module , builder , func )
85+ if isinstance (call .func , ast .Name ):
86+ # check for helpers first
87+ if call .func .id in helper_func_list :
88+ handle_helper_call (
89+ call , module , builder , func , local_sym_tab , map_sym_tab )
12890 elif isinstance (stmt , ast .Assign ):
12991 handle_assign (module , builder , stmt , map_sym_tab , local_sym_tab )
13092 elif isinstance (stmt , ast .Return ):
0 commit comments