Skip to content

Commit 943697a

Browse files
committed
Pass down type info in local_sym_tab
1 parent ba90af9 commit 943697a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pythonbpf/bpf_helper_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
8585
raise NotImplementedError(
8686
"Only string and integer constants are supported in f-string.")
8787
elif isinstance(value, ast.FormattedValue):
88+
print("Formatted value:", ast.dump(value))
8889
# Assume int for now
8990
fmt_parts.append("%lld")
9091
if isinstance(value.value, ast.Name):

pythonbpf/functions_pass.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
8181
local_sym_tab[var_name])
8282
# local_sym_tab[var_name] = var
8383
print(f"Assigned constant {rval.value} to {var_name}")
84+
elif isinstance(rval.value, str):
85+
str_val = rval.value.encode('utf-8') + b'\x00'
86+
str_const = ir.Constant(ir.ArrayType(
87+
ir.IntType(8), len(str_val)), bytearray(str_val))
88+
global_str = ir.GlobalVariable(
89+
module, str_const.type, name=f"{var_name}_str")
90+
global_str.linkage = 'internal'
91+
global_str.global_constant = True
92+
global_str.initializer = str_const
93+
str_ptr = builder.bitcast(
94+
global_str, ir.PointerType(ir.IntType(8)))
95+
builder.store(str_ptr, local_sym_tab[var_name])
96+
print(f"Assigned string constant '{rval.value}' to {var_name}")
8497
else:
8598
print("Unsupported constant type")
8699
elif isinstance(rval, ast.Call):
@@ -389,7 +402,7 @@ def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_t
389402
else:
390403
print("Unsupported assignment value type")
391404
continue
392-
local_sym_tab[var_name] = var
405+
local_sym_tab[var_name] = (var, ir_type)
393406
return local_sym_tab
394407

395408

0 commit comments

Comments
 (0)