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

@ -113,33 +113,6 @@ def handle_variable_assignment(
):
"""Handle single named variable assignment."""
# A name declared with `global` writes the @bpfglobal symbol directly:
# the plain `store i64 %v, ptr @counter` form of the C reference.
if (
var_name not in local_sym_tab
and var_name in compilation_context.current_func_globals
):
sym = compilation_context.bpf_globals[var_name]
val_result = eval_expr(func, compilation_context, builder, rval, local_sym_tab)
if val_result is None:
logger.error(f"Failed to evaluate value for global {var_name}")
return False
val, val_type = val_result
if isinstance(val_type, ir.IntType) and isinstance(sym.ir_type, ir.IntType):
# Same implicit widening/truncation rules as local assignments
if val_type.width < sym.ir_type.width:
val = builder.sext(val, sym.ir_type)
elif val_type.width > sym.ir_type.width:
val = builder.trunc(val, sym.ir_type)
elif val_type != sym.ir_type:
logger.error(
f"Type mismatch for global {var_name}: {val_type} vs {sym.ir_type}"
)
return False
builder.store(val, sym.var)
logger.info(f"Stored to BPF global {var_name}")
return True
if var_name not in local_sym_tab:
logger.error(f"Variable {var_name} not declared.")
return False