Use scratch space to store consts passed to helpers

This commit is contained in:
Pragyansh Chaturvedi
2025-10-12 04:17:37 +05:30
parent 5dcf670f49
commit 207f714027
3 changed files with 17 additions and 13 deletions

View File

@ -58,6 +58,8 @@ class ScratchPoolManager:
f"Current counter: {self._counter}"
)
return local_sym_tab[temp_name].var, temp_name
_temp_pool_manager = ScratchPoolManager() # Singleton instance
@ -67,11 +69,6 @@ def reset_scratch_pool():
_temp_pool_manager.reset()
def get_next_scratch_temp(local_sym_tab):
"""Get the next temporary variable name from the scratch pool"""
return _temp_pool_manager.get_next_temp(local_sym_tab)
def get_var_ptr_from_name(var_name, local_sym_tab):
"""Get a pointer to a variable from the symbol table."""
if local_sym_tab and var_name in local_sym_tab:
@ -79,13 +76,14 @@ def get_var_ptr_from_name(var_name, local_sym_tab):
raise ValueError(f"Variable '{var_name}' not found in local symbol table")
def create_int_constant_ptr(value, builder, int_width=64):
def create_int_constant_ptr(value, builder, local_sym_tab, int_width=64):
"""Create a pointer to an integer constant."""
# Default to 64-bit integer
int_type = ir.IntType(int_width)
ptr = builder.alloca(int_type)
ptr.align = int_type.width // 8
builder.store(ir.Constant(int_type, value), ptr)
ptr, temp_name = _temp_pool_manager.get_next_temp(local_sym_tab)
logger.debug(f"Using temp variable '{temp_name}' for int constant {value}")
const_val = ir.Constant(ir.IntType(int_width), value)
builder.store(const_val, ptr)
return ptr
@ -95,7 +93,7 @@ def get_or_create_ptr_from_arg(arg, builder, local_sym_tab):
if isinstance(arg, ast.Name):
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
ptr = create_int_constant_ptr(arg.value, builder)
ptr = create_int_constant_ptr(arg.value, builder, local_sym_tab)
else:
raise NotImplementedError(
"Only simple variable names are supported as args in map helpers."