Core: Register @bpfglobal variables in a symbol table

globals_processing emitted each global and forgot it, which is why nothing
could ever read one back. Each integer-scalar global now lands in
CompilationContext.bpf_globals as a BpfGlobalSymbol; non-scalar globals get a
clear NotImplementedError instead of emitting something unusable. LICENSE stays
special-cased and unaffected.

Alignment becomes natural (width/8) instead of a blanket 8, matching what clang
emits for the same declarations in tests/c-form/global_vars.bpf.c -- llc
derives the BTF DATASEC layout from these symbols.

Also adds current_func_globals to the context for the upcoming write support.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
Pragyansh Chaturvedi
2026-09-01 15:24:59 +05:30
parent cd1f2beb98
commit 3d8a211038
2 changed files with 40 additions and 2 deletions

View File

@ -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()