@@ -210,82 +210,66 @@ def _allocate_for_name(builder, var_name, rval, local_sym_tab):
210210 logger .error (f"Source variable '{ source_var } ' not found in symbol table" )
211211 return
212212
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
213+ # Get type and metadata from source variable
214+ source_symbol = local_sym_tab [source_var ]
216215
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
216+ # Allocate with same type and alignment
217+ var = _allocate_with_type (builder , var_name , source_symbol .ir_type )
218+ local_sym_tab [var_name ] = LocalSymbol (
219+ var , source_symbol .ir_type , source_symbol .metadata
220+ )
233221
234- local_sym_tab [var_name ] = LocalSymbol (var , source_type , source_metadata )
235222 logger .info (
236- f"Pre-allocated { var_name } from variable { source_var } with type { source_type } "
223+ f"Pre-allocated { var_name } from { source_var } with type { source_symbol . ir_type } "
237224 )
238225
239226
240227def _allocate_for_attribute (builder , var_name , rval , local_sym_tab , structs_sym_tab ):
241228 """Allocate memory for struct field-to-variable assignment (a = dat.fld)."""
242229 if not isinstance (rval .value , ast .Name ):
243- logger .warning (
244- f"Complex attribute access not supported for allocation of { var_name } "
245- )
230+ logger .warning (f"Complex attribute access not supported for { var_name } " )
246231 return
247232
248233 struct_var = rval .value .id
249234 field_name = rval .attr
250235
251- # Validate struct exists
236+ # Validate struct and field
252237 if struct_var not in local_sym_tab :
253- logger .error (f"Struct variable '{ struct_var } ' not found in symbol table " )
238+ logger .error (f"Struct variable '{ struct_var } ' not found" )
254239 return
255240
256241 struct_type = local_sym_tab [struct_var ].metadata
257242 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 " )
243+ logger .error (f"Struct type '{ struct_type } ' not found" )
259244 return
260245
261246 struct_info = structs_sym_tab [struct_type ]
262-
263- # Validate field exists
264247 if field_name not in struct_info .fields :
265248 logger .error (f"Field '{ field_name } ' not found in struct '{ struct_type } '" )
266249 return
267250
268- # Get field type
251+ # Allocate with field's type and alignment
269252 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-
253+ var = _allocate_with_type (builder , var_name , field_type )
288254 local_sym_tab [var_name ] = LocalSymbol (var , field_type )
255+
289256 logger .info (
290257 f"Pre-allocated { var_name } from { struct_var } .{ field_name } with type { field_type } "
291258 )
259+
260+
261+ def _allocate_with_type (builder , var_name , ir_type ):
262+ """Allocate variable with appropriate alignment for type."""
263+ var = builder .alloca (ir_type , name = var_name )
264+ var .align = _get_alignment (ir_type )
265+ return var
266+
267+
268+ def _get_alignment (ir_type ):
269+ """Get appropriate alignment for IR type."""
270+ if isinstance (ir_type , ir .IntType ):
271+ return ir_type .width // 8
272+ elif isinstance (ir_type , ir .ArrayType ) and isinstance (ir_type .element , ir .IntType ):
273+ return ir_type .element .width // 8
274+ else :
275+ return 8 # Default: pointer size
0 commit comments