@@ -20,7 +20,62 @@ def get_probe_string(func_node):
2020 return "helper"
2121
2222
23- def process_func_body (module , builder , func_node , func , ret_type ):
23+ def handle_assign (module , builder , stmt , map_sym_tab ):
24+ """Handle assignment statements in the function body."""
25+ if len (stmt .targets ) != 1 :
26+ print ("Unsupported multiassignment" )
27+ return
28+
29+ num_types = ("c_int32" , "c_int64" , "c_uint32" , "c_uint64" )
30+
31+ target = stmt .targets [0 ]
32+ if not isinstance (target , ast .Name ):
33+ print ("Unsupported assignment target" )
34+ return
35+ var_name = target .id
36+ rval = stmt .value
37+ if isinstance (rval , ast .Constant ):
38+ if isinstance (rval .value , int ):
39+ # Assume c_int64 for now
40+ # TODO: make symtab for this
41+ var = builder .alloca (ir .IntType (64 ), name = var_name )
42+ var .align = 8
43+ builder .store (ir .Constant (ir .IntType (64 ), rval .value ), var )
44+ print (f"Assigned constant { rval .value } to { var_name } " )
45+ elif isinstance (rval , ast .Call ):
46+ if isinstance (rval .func , ast .Name ):
47+ call_type = rval .func .id
48+ print (f"Assignment call type: { call_type } " )
49+ if call_type in num_types and len (rval .args ) == 1 and isinstance (rval .args [0 ], ast .Constant ) and isinstance (rval .args [0 ].value , int ):
50+ ir_type = ctypes_to_ir (call_type )
51+ var = builder .alloca (ir_type , name = var_name )
52+ var .align = ir_type .width // 8
53+ builder .store (ir .Constant (ir_type , rval .args [0 ].value ), var )
54+ print (f"Assigned { call_type } constant "
55+ f"{ rval .args [0 ].value } to { var_name } " )
56+ else :
57+ print (f"Unsupported assignment call type: { call_type } " )
58+ elif isinstance (rval .func , ast .Attribute ):
59+ if isinstance (rval .func .attr , str ) and rval .func .attr == "lookup" :
60+ # Get map name and check symtab
61+ # maps are called as funcs
62+ if isinstance (rval .func .value , ast .Call ) and isinstance (rval .func .value .func , ast .Name ):
63+ map_name = rval .func .value .func .id
64+ if map_name in map_sym_tab :
65+ map_global = map_sym_tab [map_name ]
66+ print (f"Found map { map_name } in symtab for lookup" )
67+ if len (rval .args ) != 1 :
68+ print ("Unsupported lookup with != 1 arg" )
69+ return
70+ key_arg = rval .args [0 ]
71+ print (f"Lookup key arg type: { type (key_arg )} " )
72+ else :
73+ print (f"Map { map_name } not found in symbol table" )
74+ else :
75+ print ("Unsupported assignment from method call" )
76+
77+
78+ def process_func_body (module , builder , func_node , func , ret_type , map_sym_tab ):
2479 """Process the body of a bpf function"""
2580 # TODO: A lot. We just have print -> bpf_trace_printk for now
2681 did_return = False
@@ -32,6 +87,8 @@ def process_func_body(module, builder, func_node, func, ret_type):
3287 bpf_printk_emitter (call , module , builder , func )
3388 if isinstance (call .func , ast .Name ) and call .func .id == "bpf_ktime_get_ns" :
3489 bpf_ktime_get_ns_emitter (call , module , builder , func )
90+ elif isinstance (stmt , ast .Assign ):
91+ handle_assign (module , builder , stmt , map_sym_tab )
3592 elif isinstance (stmt , ast .Return ):
3693 if stmt .value is None :
3794 builder .ret (ir .Constant (ir .IntType (32 ), 0 ))
@@ -51,7 +108,7 @@ def process_func_body(module, builder, func_node, func, ret_type):
51108 builder .ret (ir .Constant (ir .IntType (32 ), 0 ))
52109
53110
54- def process_bpf_chunk (func_node , module , return_type ):
111+ def process_bpf_chunk (func_node , module , return_type , map_sym_tab ):
55112 """Process a single BPF chunk (function) and emit corresponding LLVM IR."""
56113
57114 func_name = func_node .name
@@ -82,7 +139,7 @@ def process_bpf_chunk(func_node, module, return_type):
82139 block = func .append_basic_block (name = "entry" )
83140 builder = ir .IRBuilder (block )
84141
85- process_func_body (module , builder , func_node , func , ret_type )
142+ process_func_body (module , builder , func_node , func , ret_type , map_sym_tab )
86143
87144 return func
88145
@@ -100,7 +157,7 @@ def func_proc(tree, module, chunks, map_sym_tab):
100157 print (f"Found probe_string of { func_node .name } : { func_type } " )
101158
102159 process_bpf_chunk (func_node , module , ctypes_to_ir (
103- infer_return_type (func_node )))
160+ infer_return_type (func_node )), map_sym_tab )
104161
105162
106163def infer_return_type (func_node : ast .FunctionDef ):
0 commit comments