Skip to content

Commit d6cd5ac

Browse files
r41k0uclaude
andcommitted
Core: Allocate a local copied from a global with the global's type
x = some_global was rejected at allocation because the Name source path only looked in the local symbol table. Resolve local first, then the BPF globals, and give the copy the source's type as the local-source case already does. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
1 parent 5f9cead commit d6cd5ac

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

pythonbpf/allocation_pass.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
7878
)
7979
elif isinstance(rval, ast.Name):
8080
# Variable-to-variable assignment (b = a)
81-
_allocate_for_name(builder, var_name, rval, local_sym_tab)
81+
_allocate_for_name(
82+
builder, var_name, rval, local_sym_tab, compilation_context
83+
)
8284
elif isinstance(rval, ast.Attribute):
8385
# Struct field-to-variable assignment (a = dat.fld)
8486
_allocate_for_attribute(
@@ -323,21 +325,24 @@ def allocate_temp_pool(builder, max_temps, local_sym_tab):
323325
logger.debug(f"Allocated temp variable: {temp_name}")
324326

325327

326-
def _allocate_for_name(builder, var_name, rval, local_sym_tab):
328+
def _allocate_for_name(builder, var_name, rval, local_sym_tab, compilation_context):
327329
"""Allocate memory for variable-to-variable assignment (b = a)."""
328330
source_var = rval.id
329331

330-
if source_var not in local_sym_tab:
332+
# Local first, then a BPF global: the copy takes the source's type either
333+
# way (a c_uint32 global gives a c_uint32 local).
334+
if source_var in local_sym_tab:
335+
source_symbol = local_sym_tab[source_var]
336+
elif source_var in compilation_context.bpf_globals:
337+
source_symbol = compilation_context.bpf_globals[source_var]
338+
else:
331339
logger.error(f"Source variable '{source_var}' not found in symbol table")
332340
return
333341

334-
# Get type and metadata from source variable
335-
source_symbol = local_sym_tab[source_var]
336-
337342
# Allocate with same type and alignment
338343
var = _allocate_with_type(builder, var_name, source_symbol.ir_type)
339344
local_sym_tab[var_name] = LocalSymbol(
340-
var, source_symbol.ir_type, source_symbol.metadata
345+
var, source_symbol.ir_type, getattr(source_symbol, "metadata", None)
341346
)
342347

343348
logger.info(

0 commit comments

Comments
 (0)