Files
python-bpf/pythonbpf/local_symbol.py
Pragyansh Chaturvedi 2b77c406de Core: Bind declared globals in local_sym_tab instead of a per-function set
A 'global x' statement is a fact about one function's scope, but it was
tracked as current_func_globals on the compilation-wide context -- per-function
state living at the wrong level, which had to be set before each body and
reset after, and which every read and write site then had to consult in a
specific order relative to local_sym_tab (the source of the parameter
collision fixed in f4ec12d).

The declaration now does what it means: it binds the name in local_sym_tab to
the @bpfglobal's storage, as a LocalSymbol whose var is the GlobalVariable and
whose declared_global flag records why. A GlobalVariable is a pointer to
storage exactly as an alloca is, so every existing path that loads from or
stores through a symbol's var works unchanged. That deletes the dedicated
global-store branch in handle_variable_assignment, the global branch in
handle_aug_assign, the allocation-pass skip, and the reset -- one table, one
lookup, no order to get wrong. Reads of an undeclared global (legal in Python)
still fall back to bpf_globals after local_sym_tab.

Python's rules fall out naturally: a parameter is already in the table when
the declarations are processed, so 'global ctx' on a parameter is detected by
the same membership check. LocalSymbol.__iter__ deliberately still yields
three fields, since several sites tuple-unpack a symbol.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
2026-09-03 11:14:14 +05:30

25 lines
708 B
Python

import llvmlite.ir as ir
from dataclasses import dataclass
from typing import Any
@dataclass
class LocalSymbol:
"""One name visible in a BPF function's scope.
`var` is the storage the name resolves to: an alloca for locals, the
GlobalVariable for a name brought in by a `global` statement, or None for
the context parameter (which arrives as func.args[0], not a slot).
"""
var: ir.AllocaInstr | ir.GlobalVariable | None
ir_type: ir.Type
metadata: Any = None
declared_global: bool = False
def __iter__(self):
# Three fields on purpose: several call sites tuple-unpack a symbol.
yield self.var
yield self.ir_type
yield self.metadata