mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
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:
@ -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()
|
||||
|
||||
@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user