From 943697ac9fe213267267a7e7b9ee5819fc9a33ae Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 25 Sep 2025 23:43:19 +0530 Subject: [PATCH] Pass down type info in local_sym_tab --- pythonbpf/bpf_helper_handler.py | 1 + pythonbpf/functions_pass.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pythonbpf/bpf_helper_handler.py b/pythonbpf/bpf_helper_handler.py index f9cb7d8..9cc5cde 100644 --- a/pythonbpf/bpf_helper_handler.py +++ b/pythonbpf/bpf_helper_handler.py @@ -85,6 +85,7 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, raise NotImplementedError( "Only string and integer constants are supported in f-string.") elif isinstance(value, ast.FormattedValue): + print("Formatted value:", ast.dump(value)) # Assume int for now fmt_parts.append("%lld") if isinstance(value.value, ast.Name): diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index bbe636a..131b5f5 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -81,6 +81,19 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc local_sym_tab[var_name]) # local_sym_tab[var_name] = var print(f"Assigned constant {rval.value} to {var_name}") + elif isinstance(rval.value, str): + str_val = rval.value.encode('utf-8') + b'\x00' + str_const = ir.Constant(ir.ArrayType( + ir.IntType(8), len(str_val)), bytearray(str_val)) + global_str = ir.GlobalVariable( + module, str_const.type, name=f"{var_name}_str") + global_str.linkage = 'internal' + global_str.global_constant = True + global_str.initializer = str_const + str_ptr = builder.bitcast( + global_str, ir.PointerType(ir.IntType(8))) + builder.store(str_ptr, local_sym_tab[var_name]) + print(f"Assigned string constant '{rval.value}' to {var_name}") else: print("Unsupported constant type") elif isinstance(rval, ast.Call): @@ -389,7 +402,7 @@ def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_t else: print("Unsupported assignment value type") continue - local_sym_tab[var_name] = var + local_sym_tab[var_name] = (var, ir_type) return local_sym_tab