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
This commit is contained in:
Pragyansh Chaturvedi
2026-09-03 11:14:14 +05:30
parent f4ec12df60
commit 2b77c406de
5 changed files with 39 additions and 75 deletions

View File

@ -50,24 +50,20 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
var_name = target.id
# Writes to @bpfglobal variables use the global symbol, not a stack
# slot. Requires Python's own `global` declaration; without it an
# assignment to a global's name would silently create a local that
# shadows it, which is exactly the bug class we refuse to compile.
if var_name in compilation_context.current_func_globals:
logger.debug(f"'{var_name}' is a declared global, no allocation needed")
# Already bound in this scope: a parameter, an earlier assignment, or a
# `global` declaration (whose slot is the GlobalVariable). No slot needed.
if var_name in local_sym_tab:
logger.debug(f"'{var_name}' already bound, no allocation needed")
continue
# Not declared `global`, yet named like one: in real Python this would
# create a shadowing local. Refuse rather than guess which was meant.
if var_name in compilation_context.bpf_globals:
raise SyntaxError(
f"assignment to '{var_name}' shadows the BPF global of the same "
f"name — add 'global {var_name}' to write to it"
)
# Skip if already allocated
if var_name in local_sym_tab:
logger.debug(f"Variable {var_name} already allocated, skipping")
continue
# Determine type and allocate based on rval
if isinstance(rval, ast.Call):
_allocate_for_call(