Skip to content

Commit 81f72a7

Browse files
committed
Support var-to-var and var-to-struct-fld allocations
1 parent fb48063 commit 81f72a7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

pythonbpf/allocation_pass.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def handle_assign_allocation(builder, stmt, local_sym_tab, structs_sym_tab):
7272
_allocate_for_constant(builder, var_name, rval, local_sym_tab)
7373
elif isinstance(rval, ast.BinOp):
7474
_allocate_for_binop(builder, var_name, local_sym_tab)
75+
elif isinstance(rval, ast.Name):
76+
# Variable-to-variable assignment (b = a)
77+
_allocate_for_name(builder, var_name, rval, local_sym_tab)
78+
elif isinstance(rval, ast.Attribute):
79+
# Struct field-to-variable assignment (a = dat.fld)
80+
_allocate_for_attribute(
81+
builder, var_name, rval, local_sym_tab, structs_sym_tab
82+
)
7583
else:
7684
logger.warning(
7785
f"Unsupported assignment value type for {var_name}: {type(rval).__name__}"
@@ -192,3 +200,92 @@ def allocate_temp_pool(builder, max_temps, local_sym_tab):
192200
temp_var = builder.alloca(ir.IntType(64), name=temp_name)
193201
temp_var.align = 8
194202
local_sym_tab[temp_name] = LocalSymbol(temp_var, ir.IntType(64))
203+
204+
205+
def _allocate_for_name(builder, var_name, rval, local_sym_tab):
206+
"""Allocate memory for variable-to-variable assignment (b = a)."""
207+
source_var = rval.id
208+
209+
if source_var not in local_sym_tab:
210+
logger.error(f"Source variable '{source_var}' not found in symbol table")
211+
return
212+
213+
# Get type from source variable
214+
source_type = local_sym_tab[source_var].ir_type
215+
source_metadata = local_sym_tab[source_var].metadata
216+
217+
# Allocate with same type
218+
var = builder.alloca(source_type, name=var_name)
219+
220+
# Set alignment based on type
221+
if isinstance(source_type, ir.IntType):
222+
var.align = source_type.width // 8
223+
elif isinstance(source_type, ir.PointerType):
224+
var.align = 8
225+
elif isinstance(source_type, ir.ArrayType):
226+
var.align = (
227+
source_type.element.width // 8
228+
if isinstance(source_type.element, ir.IntType)
229+
else 1
230+
)
231+
else:
232+
var.align = 8 # Default alignment
233+
234+
local_sym_tab[var_name] = LocalSymbol(var, source_type, source_metadata)
235+
logger.info(
236+
f"Pre-allocated {var_name} from variable {source_var} with type {source_type}"
237+
)
238+
239+
240+
def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_tab):
241+
"""Allocate memory for struct field-to-variable assignment (a = dat.fld)."""
242+
if not isinstance(rval.value, ast.Name):
243+
logger.warning(
244+
f"Complex attribute access not supported for allocation of {var_name}"
245+
)
246+
return
247+
248+
struct_var = rval.value.id
249+
field_name = rval.attr
250+
251+
# Validate struct exists
252+
if struct_var not in local_sym_tab:
253+
logger.error(f"Struct variable '{struct_var}' not found in symbol table")
254+
return
255+
256+
struct_type = local_sym_tab[struct_var].metadata
257+
if not struct_type or struct_type not in structs_sym_tab:
258+
logger.error(f"Struct type '{struct_type}' not found in struct symbol table")
259+
return
260+
261+
struct_info = structs_sym_tab[struct_type]
262+
263+
# Validate field exists
264+
if field_name not in struct_info.fields:
265+
logger.error(f"Field '{field_name}' not found in struct '{struct_type}'")
266+
return
267+
268+
# Get field type
269+
field_type = struct_info.field_type(field_name)
270+
271+
# Allocate with field's type
272+
var = builder.alloca(field_type, name=var_name)
273+
274+
# Set alignment based on type
275+
if isinstance(field_type, ir.IntType):
276+
var.align = field_type.width // 8
277+
elif isinstance(field_type, ir.PointerType):
278+
var.align = 8
279+
elif isinstance(field_type, ir.ArrayType):
280+
var.align = (
281+
field_type.element.width // 8
282+
if isinstance(field_type.element, ir.IntType)
283+
else 1
284+
)
285+
else:
286+
var.align = 8 # Default alignment
287+
288+
local_sym_tab[var_name] = LocalSymbol(var, field_type)
289+
logger.info(
290+
f"Pre-allocated {var_name} from {struct_var}.{field_name} with type {field_type}"
291+
)

0 commit comments

Comments
 (0)