mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Core: Support writes to @bpfglobal variables via the global statement
Python's own scoping marks the write: 'global cg_id' declares that assignments
to cg_id in this function mean the BPF global, and the assignment then emits
the plain 'store i64 %v, ptr @cg_id' of the C reference, with the same implicit
widening/truncation rules as local assignments.
Without the declaration, an assignment to a global's name is refused:
SyntaxError: assignment to 'cg_id' shadows the BPF global of the same
name -- add 'global cg_id' to write to it
That is deliberate. In real Python such an assignment creates a shadowing
local; silently compiling it as either a local or a global store would be wrong
in one direction or the other, so it is a loud error instead. 'global' naming
something that is not a @bpfglobal is also a compile error.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
@ -284,6 +284,9 @@ def process_stmt(
|
||||
handle_assign(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.AugAssign):
|
||||
raise SyntaxError("Augmented assignment not supported")
|
||||
elif isinstance(stmt, ast.Global):
|
||||
# Declarations were collected by process_func_body; nothing to emit.
|
||||
pass
|
||||
elif isinstance(stmt, ast.If):
|
||||
handle_if(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.Return):
|
||||
@ -311,6 +314,22 @@ def process_func_body(
|
||||
|
||||
local_sym_tab = {}
|
||||
|
||||
# Collect `global x` declarations. Python scoping rules apply: a declared
|
||||
# name may be written anywhere in this function and always means the
|
||||
# @bpfglobal, never a local. Undeclared writes to a global name are
|
||||
# rejected in the allocation pass rather than silently shadowing.
|
||||
declared_globals: set[str] = set()
|
||||
for node in ast.walk(func_node):
|
||||
if isinstance(node, ast.Global):
|
||||
for gname in node.names:
|
||||
if gname not in compilation_context.bpf_globals:
|
||||
raise SyntaxError(
|
||||
f"'global {gname}' in '{func_node.name}': no @bpfglobal "
|
||||
f"named '{gname}' is declared"
|
||||
)
|
||||
declared_globals.add(gname)
|
||||
compilation_context.current_func_globals = declared_globals
|
||||
|
||||
# Add the context parameter (first function argument) to the local symbol table
|
||||
if func_node.args.args and len(func_node.args.args) > 0:
|
||||
context_arg = func_node.args.args[0]
|
||||
@ -375,6 +394,8 @@ def process_func_body(
|
||||
if not did_return:
|
||||
builder.ret(ir.Constant(ir.IntType(64), 0))
|
||||
|
||||
compilation_context.current_func_globals = set()
|
||||
|
||||
|
||||
def process_bpf_chunk(func_node, compilation_context, return_type):
|
||||
"""Process a single BPF chunk (function) and emit corresponding LLVM IR."""
|
||||
|
||||
Reference in New Issue
Block a user