Skip to content

Commit dafd6d1

Browse files
committed
janitorial: create unified helper handler
1 parent 3628276 commit dafd6d1

2 files changed

Lines changed: 71 additions & 60 deletions

File tree

pythonbpf/bpf_helper_handler.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def bpf_ktime_get_ns_emitter(call, module, builder, func):
66
"""
77
Emit LLVM IR for bpf_ktime_get_ns helper function call.
88
"""
9+
# func is an arg to just have a uniform signature with other emitters
910
helper_id = ir.Constant(ir.IntType(64), 5)
1011
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
1112
fn_ptr_type = ir.PointerType(fn_type)
@@ -14,15 +15,38 @@ def bpf_ktime_get_ns_emitter(call, module, builder, func):
1415
return result
1516

1617

17-
def bpf_map_lookup_elem_emitter(map_ptr, key_ptr, module, builder):
18+
def bpf_map_lookup_elem_emitter(call, map_ptr, module, builder, local_sym_tab=None):
1819
"""
1920
Emit LLVM IR for bpf_map_lookup_elem helper function call.
2021
"""
21-
# Cast pointers to void*
22+
23+
if call.args and len(call.args) != 1:
24+
raise ValueError("Map lookup expects exactly one argument, got "
25+
f"{len(call.args)}")
26+
key_arg = call.args[0]
27+
if isinstance(key_arg, ast.Name):
28+
key_name = key_arg.id
29+
if local_sym_tab and key_name in local_sym_tab:
30+
key_ptr = local_sym_tab[key_name]
31+
else:
32+
raise ValueError(
33+
f"Key variable {key_name} not found in local symbol table.")
34+
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
35+
# handle constant integer keys
36+
key_val = key_arg.value
37+
key_type = ir.IntType(64)
38+
key_ptr = builder.alloca(key_type)
39+
key_ptr.align = key_type // 8
40+
builder.store(ir.Constant(key_type, key_val), key_ptr)
41+
else:
42+
raise NotImplementedError(
43+
"Only simple variable names are supported as keys in map lookup.")
44+
45+
if key_ptr is None:
46+
raise ValueError("Key pointer is None.")
47+
2248
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
2349

24-
# Define function type for bpf_map_lookup_elem
25-
# The function takes two void* arguments and returns void*
2650
fn_type = ir.FunctionType(
2751
ir.PointerType(), # Return type: void*
2852
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
@@ -34,7 +58,6 @@ def bpf_map_lookup_elem_emitter(map_ptr, key_ptr, module, builder):
3458
fn_addr = ir.Constant(ir.IntType(64), 1)
3559
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
3660

37-
# Call the helper function
3861
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
3962

4063
return result
@@ -79,5 +102,31 @@ def bpf_printk_emitter(call, module, builder, func):
79102
}
80103

81104

82-
def handle_helper_call(call, module, builder, func):
83-
return None
105+
def handle_helper_call(call, module, builder, func, local_sym_tab=None, map_sym_tab=None):
106+
if isinstance(call.func, ast.Name):
107+
func_name = call.func.id
108+
if func_name in helper_func_list:
109+
# it is not a map method call
110+
helper_func_list[func_name](call, module, builder, func)
111+
else:
112+
raise NotImplementedError(
113+
f"Function {func_name} is not implemented as a helper function.")
114+
elif isinstance(call.func, ast.Attribute):
115+
# likely a map method call
116+
if isinstance(call.func.value, ast.Call) and isinstance(call.func.value.func, ast.Name):
117+
map_name = call.func.value.func.id
118+
method_name = call.func.attr
119+
if map_sym_tab and map_name in map_sym_tab:
120+
map_ptr = map_sym_tab[map_name]
121+
if method_name in helper_func_list:
122+
helper_func_list[method_name](
123+
call, map_ptr, module, builder, local_sym_tab)
124+
else:
125+
raise NotImplementedError(
126+
f"Map method {method_name} is not implemented as a helper function.")
127+
else:
128+
raise ValueError(
129+
f"Map variable {map_name} not found in symbol tables.")
130+
else:
131+
raise NotImplementedError(
132+
"Attribute not supported for map method calls.")

pythonbpf/functions_pass.py

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from llvmlite import ir
22
import 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
55
from .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

11475
def 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

Comments
 (0)