From 2a1eabc10d1356dd8d93bbf3cabaad084824f617 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Mon, 13 Oct 2025 00:00:43 +0530 Subject: [PATCH] Fix regression in struct_perf_output --- pythonbpf/expr/expr_pass.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index ecf1119..85645b0 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -21,10 +21,24 @@ def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder return None -def _handle_constant_expr(expr: ast.Constant): +def _handle_constant_expr(module, builder, expr: ast.Constant): """Handle ast.Constant expressions.""" if isinstance(expr.value, int) or isinstance(expr.value, bool): return ir.Constant(ir.IntType(64), int(expr.value)), ir.IntType(64) + elif isinstance(expr.value, str): + str_name = f".str.{id(expr)}" + str_bytes = expr.value.encode("utf-8") + b"\x00" + str_type = ir.ArrayType(ir.IntType(8), len(str_bytes)) + str_constant = ir.Constant(str_type, bytearray(str_bytes)) + + # Create global variable + global_str = ir.GlobalVariable(module, str_type, name=str_name) + global_str.linkage = "internal" + global_str.global_constant = True + global_str.initializer = str_constant + + str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8))) + return str_ptr, ir.PointerType(ir.IntType(8)) else: logger.error(f"Unsupported constant type {ast.dump(expr)}") return None @@ -343,7 +357,7 @@ def eval_expr( if isinstance(expr, ast.Name): return _handle_name_expr(expr, local_sym_tab, builder) elif isinstance(expr, ast.Constant): - return _handle_constant_expr(expr) + return _handle_constant_expr(module, builder, expr) elif isinstance(expr, ast.Call): if isinstance(expr.func, ast.Name) and expr.func.id == "deref": return _handle_deref_call(expr, local_sym_tab, builder)