Skip to content

Commit 7c15bff

Browse files
committed
Implement lookup key parsing
1 parent 03404bb commit 7c15bff

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

pythonbpf/functions_pass.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_probe_string(func_node):
2222
return "helper"
2323

2424

25-
def handle_assign(module, builder, stmt, map_sym_tab):
25+
def handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab):
2626
"""Handle assignment statements in the function body."""
2727
if len(stmt.targets) != 1:
2828
print("Unsupported multiassignment")
@@ -43,6 +43,7 @@ def handle_assign(module, builder, stmt, map_sym_tab):
4343
var = builder.alloca(ir.IntType(64), name=var_name)
4444
var.align = 8
4545
builder.store(ir.Constant(ir.IntType(64), rval.value), var)
46+
local_sym_tab[var_name] = var
4647
print(f"Assigned constant {rval.value} to {var_name}")
4748
elif isinstance(rval, ast.Call):
4849
if isinstance(rval.func, ast.Name):
@@ -55,6 +56,7 @@ def handle_assign(module, builder, stmt, map_sym_tab):
5556
builder.store(ir.Constant(ir_type, rval.args[0].value), var)
5657
print(f"Assigned {call_type} constant "
5758
f"{rval.args[0].value} to {var_name}")
59+
local_sym_tab[var_name] = var
5860
else:
5961
print(f"Unsupported assignment call type: {call_type}")
6062
elif isinstance(rval.func, ast.Attribute):
@@ -71,6 +73,35 @@ def handle_assign(module, builder, stmt, map_sym_tab):
7173
return
7274
key_arg = rval.args[0]
7375
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+
elif isinstance(key_arg, ast.Name):
83+
# Check in local symtab first
84+
if key_arg.id in local_sym_tab:
85+
key_var = local_sym_tab[key_arg.id]
86+
key_type = key_var.type.pointee
87+
key_val = builder.load(key_var)
88+
elif key_arg.id in map_sym_tab:
89+
key_var = map_sym_tab[key_arg.id]
90+
key_type = key_var.type.pointee
91+
key_val = builder.load(key_var)
92+
else:
93+
print("Key variable "
94+
f"{key_arg.id} not found in symtabs")
95+
return
96+
print(f"Found key variable {key_arg.id} in symtab")
97+
print(f"Key type: {key_type}")
98+
print(f"Key val: {key_val}")
99+
else:
100+
print("Unsupported lookup key arg")
101+
return
102+
103+
# TODO: generate call to bpf_map_lookup_elem
104+
74105
else:
75106
print(f"Map {map_name} not found in symbol table")
76107
else:
@@ -82,6 +113,8 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
82113
# TODO: A lot. We just have print -> bpf_trace_printk for now
83114
did_return = False
84115

116+
local_sym_tab = {}
117+
85118
for stmt in func_node.body:
86119
if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call):
87120
call = stmt.value
@@ -90,7 +123,7 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
90123
if isinstance(call.func, ast.Name) and call.func.id == "bpf_ktime_get_ns":
91124
bpf_ktime_get_ns_emitter(call, module, builder, func)
92125
elif isinstance(stmt, ast.Assign):
93-
handle_assign(module, builder, stmt, map_sym_tab)
126+
handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab)
94127
elif isinstance(stmt, ast.Return):
95128
if stmt.value is None:
96129
builder.ret(ir.Constant(ir.IntType(32), 0))

0 commit comments

Comments
 (0)