Skip to content

Commit 244ea14

Browse files
committed
Refactor bpf_map_lookup_elem_emitter, add utils
1 parent 168ab29 commit 244ea14

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from llvmlite import ir
33
from pythonbpf.expr_pass import eval_expr
44
from enum import Enum
5-
from .helper_utils import HelperHandlerRegistry
5+
from .helper_utils import HelperHandlerRegistry, get_key_ptr
66

77

88
class BPFHelperID(Enum):
@@ -38,31 +38,7 @@ def bpf_map_lookup_elem_emitter(call, map_ptr, module, builder, func,
3838
"""
3939
Emit LLVM IR for bpf_map_lookup_elem helper function call.
4040
"""
41-
if call.args and len(call.args) != 1:
42-
raise ValueError("Map lookup expects exactly one argument, got "
43-
f"{len(call.args)}")
44-
key_arg = call.args[0]
45-
if isinstance(key_arg, ast.Name):
46-
key_name = key_arg.id
47-
if local_sym_tab and key_name in local_sym_tab:
48-
key_ptr = local_sym_tab[key_name][0]
49-
else:
50-
raise ValueError(
51-
f"Key variable {key_name} not found in local symbol table.")
52-
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
53-
# handle constant integer keys
54-
key_val = key_arg.value
55-
key_type = ir.IntType(64)
56-
key_ptr = builder.alloca(key_type)
57-
key_ptr.align = key_type // 8
58-
builder.store(ir.Constant(key_type, key_val), key_ptr)
59-
else:
60-
raise NotImplementedError(
61-
"Only simple variable names are supported as keys in map lookup.")
62-
63-
if key_ptr is None:
64-
raise ValueError("Key pointer is None.")
65-
41+
key_ptr = get_key_ptr(call, builder, local_sym_tab)
6642
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
6743

6844
fn_type = ir.FunctionType(
@@ -72,7 +48,6 @@ def bpf_map_lookup_elem_emitter(call, map_ptr, module, builder, func,
7248
)
7349
fn_ptr_type = ir.PointerType(fn_type)
7450

75-
# Helper ID 1 is bpf_map_lookup_elem
7651
fn_addr = ir.Constant(ir.IntType(
7752
64), BPFHelperID.BPF_MAP_LOOKUP_ELEM.value)
7853
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)

pythonbpf/helper/helper_utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import ast
2+
from llvmlite import ir
3+
4+
15
class HelperHandlerRegistry:
26
"""Registry for BPF helpers"""
37
_handlers = {}
@@ -14,3 +18,38 @@ def decorator(func):
1418
def get_handler(cls, helper_name):
1519
"""Get the handler function for a helper"""
1620
return cls._handlers.get(helper_name)
21+
22+
23+
def get_var_ptr_from_name(var_name, local_sym_tab):
24+
"""Get a pointer to a variable from the symbol table."""
25+
if local_sym_tab and var_name in local_sym_tab:
26+
return local_sym_tab[var_name][0]
27+
raise ValueError(f"Variable '{var_name}' not found in local symbol table")
28+
29+
30+
def create_int_constant_ptr(value, builder, int_width=64):
31+
"""Create a pointer to an integer constant."""
32+
# Default to 64-bit integer
33+
int_type = ir.IntType(int_width)
34+
ptr = builder.alloca(int_type)
35+
ptr.align = int_type.width // 8
36+
builder.store(ir.Constant(int_type, value), ptr)
37+
return ptr
38+
39+
40+
def get_key_ptr(call, builder, local_sym_tab):
41+
"""Extract key pointer from the call arguments."""
42+
if not call.args or len(call.args) != 1:
43+
raise ValueError("Map lookup expects exactly one argument, got "
44+
f"{len(call.args)}")
45+
46+
key_arg = call.args[0]
47+
48+
if isinstance(key_arg, ast.Name):
49+
key_ptr = get_var_ptr_from_name(key_arg.id, local_sym_tab)
50+
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
51+
key_ptr = create_int_constant_ptr(key_arg.value, builder)
52+
else:
53+
raise NotImplementedError(
54+
"Only simple variable names are supported as keys in map lookup.")
55+
return key_ptr

0 commit comments

Comments
 (0)