diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index 2270fdb..08967fb 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -22,12 +22,20 @@ logger: Logger = logging.getLogger(__name__) # ============================================================================ -def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder): +def _handle_name_expr( + expr: ast.Name, compilation_context, local_sym_tab: Dict, builder: ir.IRBuilder +): """Handle ast.Name expressions.""" if expr.id in local_sym_tab: var = local_sym_tab[expr.id].var val = builder.load(var) return val, local_sym_tab[expr.id].ir_type + elif expr.id in compilation_context.bpf_globals: + # A @bpfglobal: read straight off the global symbol, exactly the + # `load i64, ptr @counter` form clang emits (tests/c-form/global_vars). + sym = compilation_context.bpf_globals[expr.id] + val = builder.load(sym.var) + return val, sym.ir_type else: # Check if it's a vmlinux enum/constant vmlinux_result = VmlinuxHandlerRegistry.handle_name(expr.id) @@ -175,6 +183,9 @@ def get_operand_value(func, compilation_context, operand, builder, local_sym_tab else: val = deref_to_depth(func, builder, var, depth) return val + elif operand.id in compilation_context.bpf_globals: + # A @bpfglobal: plain load off the global symbol. + return builder.load(compilation_context.bpf_globals[operand.id].var) else: # Check if it's a vmlinux enum/constant vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id) @@ -662,7 +673,7 @@ def eval_expr( logger.info(f"Evaluating expression: {ast.dump(expr)}") if isinstance(expr, ast.Name): - return _handle_name_expr(expr, local_sym_tab, builder) + return _handle_name_expr(expr, compilation_context, local_sym_tab, builder) elif isinstance(expr, ast.Constant): return _handle_constant_expr(compilation_context, builder, expr) elif isinstance(expr, ast.Call): diff --git a/pythonbpf/helper/printk_formatter.py b/pythonbpf/helper/printk_formatter.py index 3328e1a..f9d3537 100644 --- a/pythonbpf/helper/printk_formatter.py +++ b/pythonbpf/helper/printk_formatter.py @@ -41,7 +41,7 @@ def handle_fstring_print( fmt_parts, exprs, local_sym_tab, - compilation_context.structs_sym_tab, + compilation_context, ) else: raise NotImplementedError(f"Unsupported f-string value type: {type(value)}") @@ -80,19 +80,21 @@ def _process_constant_in_fstring(cst, fmt_parts, exprs): ) -def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab): +def _process_fval(fval, fmt_parts, exprs, local_sym_tab, compilation_context): """Process formatted values in f-string.""" logger.debug(f"Processing formatted value: {ast.dump(fval)}") if isinstance(fval.value, ast.Name): - _process_name_in_fval(fval.value, fmt_parts, exprs, local_sym_tab) + _process_name_in_fval( + fval.value, fmt_parts, exprs, local_sym_tab, compilation_context + ) elif isinstance(fval.value, ast.Attribute): _process_attr_in_fval( fval.value, fmt_parts, exprs, local_sym_tab, - struct_sym_tab, + compilation_context.structs_sym_tab, ) else: raise NotImplementedError( @@ -100,11 +102,16 @@ def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab): ) -def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab): +def _process_name_in_fval( + name_node, fmt_parts, exprs, local_sym_tab, compilation_context +): """Process name nodes in formatted values.""" if local_sym_tab and name_node.id in local_sym_tab: _, var_type, tmp = local_sym_tab[name_node.id] _populate_fval(var_type, name_node, fmt_parts, exprs) + elif name_node.id in compilation_context.bpf_globals: + var_type = compilation_context.bpf_globals[name_node.id].ir_type + _populate_fval(var_type, name_node, fmt_parts, exprs) else: # Try to resolve through vmlinux registry if not in local symbol table result = VmlinuxHandlerRegistry.handle_name(name_node.id)