@@ -26,9 +26,7 @@ def create_targets_and_rvals(stmt):
2626 return stmt .targets , [stmt .value ]
2727
2828
29- def handle_assign_allocation (
30- builder , stmt , local_sym_tab , map_sym_tab , structs_sym_tab
31- ):
29+ def handle_assign_allocation (compilation_context , builder , stmt , local_sym_tab ):
3230 """Handle memory allocation for assignment statements."""
3331
3432 logger .info (f"Handling assignment for allocation: { ast .dump (stmt )} " )
@@ -59,7 +57,7 @@ def handle_assign_allocation(
5957 # Determine type and allocate based on rval
6058 if isinstance (rval , ast .Call ):
6159 _allocate_for_call (
62- builder , var_name , rval , local_sym_tab , map_sym_tab , structs_sym_tab
60+ builder , var_name , rval , local_sym_tab , compilation_context
6361 )
6462 elif isinstance (rval , ast .Constant ):
6563 _allocate_for_constant (builder , var_name , rval , local_sym_tab )
@@ -71,18 +69,17 @@ def handle_assign_allocation(
7169 elif isinstance (rval , ast .Attribute ):
7270 # Struct field-to-variable assignment (a = dat.fld)
7371 _allocate_for_attribute (
74- builder , var_name , rval , local_sym_tab , structs_sym_tab
72+ builder , var_name , rval , local_sym_tab , compilation_context
7573 )
7674 else :
7775 logger .warning (
7876 f"Unsupported assignment value type for { var_name } : { type (rval ).__name__ } "
7977 )
8078
8179
82- def _allocate_for_call (
83- builder , var_name , rval , local_sym_tab , map_sym_tab , structs_sym_tab
84- ):
80+ def _allocate_for_call (builder , var_name , rval , local_sym_tab , compilation_context ):
8581 """Allocate memory for variable assigned from a call."""
82+ structs_sym_tab = compilation_context .structs_sym_tab
8683
8784 if isinstance (rval .func , ast .Name ):
8885 call_type = rval .func .id
@@ -149,17 +146,19 @@ def _allocate_for_call(
149146 elif isinstance (rval .func , ast .Attribute ):
150147 # Map method calls - need double allocation for ptr handling
151148 _allocate_for_map_method (
152- builder , var_name , rval , local_sym_tab , map_sym_tab , structs_sym_tab
149+ builder , var_name , rval , local_sym_tab , compilation_context
153150 )
154151
155152 else :
156153 logger .warning (f"Unsupported call function type for { var_name } " )
157154
158155
159156def _allocate_for_map_method (
160- builder , var_name , rval , local_sym_tab , map_sym_tab , structs_sym_tab
157+ builder , var_name , rval , local_sym_tab , compilation_context
161158):
162159 """Allocate memory for variable assigned from map method (double alloc)."""
160+ map_sym_tab = compilation_context .map_sym_tab
161+ structs_sym_tab = compilation_context .structs_sym_tab
163162
164163 map_name = rval .func .value .id
165164 method_name = rval .func .attr
@@ -299,6 +298,15 @@ def allocate_temp_pool(builder, max_temps, local_sym_tab):
299298 logger .debug (f"Allocated temp variable: { temp_name } " )
300299
301300
301+ def _get_alignment (tmp_type ):
302+ """Return alignment for a given type."""
303+ if isinstance (tmp_type , ir .PointerType ):
304+ return 8
305+ elif isinstance (tmp_type , ir .IntType ):
306+ return tmp_type .width // 8
307+ return 8
308+
309+
302310def _allocate_for_name (builder , var_name , rval , local_sym_tab ):
303311 """Allocate memory for variable-to-variable assignment (b = a)."""
304312 source_var = rval .id
@@ -321,8 +329,22 @@ def _allocate_for_name(builder, var_name, rval, local_sym_tab):
321329 )
322330
323331
324- def _allocate_for_attribute (builder , var_name , rval , local_sym_tab , structs_sym_tab ):
332+ def _allocate_with_type (builder , var_name , ir_type ):
333+ """Allocate memory for a variable with a specific type."""
334+ var = builder .alloca (ir_type , name = var_name )
335+ if isinstance (ir_type , ir .IntType ):
336+ var .align = ir_type .width // 8
337+ elif isinstance (ir_type , ir .PointerType ):
338+ var .align = 8
339+ return var
340+
341+
342+ def _allocate_for_attribute (
343+ builder , var_name , rval , local_sym_tab , compilation_context
344+ ):
325345 """Allocate memory for struct field-to-variable assignment (a = dat.fld)."""
346+ structs_sym_tab = compilation_context .structs_sym_tab
347+
326348 if not isinstance (rval .value , ast .Name ):
327349 logger .warning (f"Complex attribute access not supported for { var_name } " )
328350 return
@@ -455,20 +477,3 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_
455477 logger .info (
456478 f"Pre-allocated { var_name } from { struct_var } .{ field_name } with type { alloc_type } "
457479 )
458-
459-
460- def _allocate_with_type (builder , var_name , ir_type ):
461- """Allocate variable with appropriate alignment for type."""
462- var = builder .alloca (ir_type , name = var_name )
463- var .align = _get_alignment (ir_type )
464- return var
465-
466-
467- def _get_alignment (ir_type ):
468- """Get appropriate alignment for IR type."""
469- if isinstance (ir_type , ir .IntType ):
470- return ir_type .width // 8
471- elif isinstance (ir_type , ir .ArrayType ) and isinstance (ir_type .element , ir .IntType ):
472- return ir_type .element .width // 8
473- else :
474- return 8 # Default: pointer size
0 commit comments