diff --git a/pythonbpf/context.py b/pythonbpf/context.py index 5297889..8ebd21b 100644 --- a/pythonbpf/context.py +++ b/pythonbpf/context.py @@ -5,6 +5,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from pythonbpf.structs.struct_type import StructType from pythonbpf.maps.maps_utils import MapSymbol + from pythonbpf.globals_pass import BpfGlobalSymbol logger = logging.getLogger(__name__) @@ -66,6 +67,11 @@ class CompilationContext: self.global_sym_tab: list[ir.GlobalVariable] = [] self.structs_sym_tab: dict[str, "StructType"] = {} self.map_sym_tab: dict[str, "MapSymbol"] = {} + self.bpf_globals: dict[str, "BpfGlobalSymbol"] = {} + + # Names a `global` statement declared writable in the function whose + # body is currently being emitted; managed by process_func_body. + self.current_func_globals: set[str] = set() # Helper management self.scratch_pool = ScratchPoolManager() @@ -80,3 +86,4 @@ class CompilationContext: """Reset state between functions if necessary, though new context per compile is preferred.""" self.scratch_pool.reset() self.current_func = None + self.current_func_globals = set() diff --git a/pythonbpf/globals_pass.py b/pythonbpf/globals_pass.py index 27eecd2..78fa564 100644 --- a/pythonbpf/globals_pass.py +++ b/pythonbpf/globals_pass.py @@ -1,6 +1,7 @@ from llvmlite import ir import ast +from dataclasses import dataclass from logging import Logger import logging from .type_deducer import ctypes_to_ir @@ -8,6 +9,20 @@ from .type_deducer import ctypes_to_ir logger: Logger = logging.getLogger(__name__) +@dataclass +class BpfGlobalSymbol: + """A mutable BPF global variable declared with @bpfglobal. + + Lands in .bss (zero initializer) or .data (non-zero) and is read with a + plain load / written with a plain store; libbpf exposes the sections to + userspace as global-data maps. + """ + + var: ir.GlobalVariable + ir_type: ir.Type + ctype_name: str + + def populate_global_symbol_table(tree, compilation_context): """ compilation_context: CompilationContext @@ -68,7 +83,11 @@ def _emit_global(module: ir.Module, node, name): gvar = ir.GlobalVariable(module, ty, name=name) gvar.initializer = llvm_init - gvar.align = 8 + # Natural alignment, matching what clang emits for the same declaration + # (align 4 for i32, align 8 for i64). llc derives the BTF DATASEC layout + # from these symbols, so the alignment should mirror the C reference in + # tests/c-form/global_vars.bpf.c. + gvar.align = ty.width // 8 if isinstance(ty, ir.IntType) else 8 gvar.linkage = "dso_local" gvar.global_constant = False return gvar @@ -111,7 +130,19 @@ def globals_processing(tree, compilation_context): node.body[0].value, (ast.Constant, ast.Name, ast.Call) ) ): - _emit_global(compilation_context.module, node, name) + gvar = _emit_global(compilation_context.module, node, name) + if isinstance(gvar.value_type, ir.IntType): + compilation_context.bpf_globals[name] = BpfGlobalSymbol( + var=gvar, + ir_type=gvar.value_type, + ctype_name=node.returns.id, + ) + else: + raise NotImplementedError( + f"Global '{name}': only integer scalar globals are " + f"supported so far; '{node.returns.id}' globals are " + f"planned for a later milestone" + ) else: raise SyntaxError(f"ERROR: Invalid syntax for {name} global")