# Lib.Allocator module

This file defines the base class `Allocator` and the naïve implementation `NaiveAllocator`.

class Lib.Allocator.Allocator(fdata: FunctionData )

  Bases: `object`

  General base class for Naive, AllInMem and Smart Allocators. Replace all temporaries in the code with actual data locations.

  Allocation is done in two steps:

  - First, `prepare()` is responsible for calling `Lib.Operands.TemporaryPool.set_temp_allocation()` with a mapping from temporaries to where they should actually be stored (in registers or in memory).

  - Then, `replace()` is called for each instruction in order to replace the temporary operands with the previously assigned locations (and possibly add some instructions before or after). Concretely, it returns a list of instructions that should replace the original instruction. The actual iteration over all the instructions is handled transparently by `Lib.LinearCode.LinearCode.iter_statements()`.

  prepare() → None
  replace(instr: Instruction ) → List[ Instruction ]

    Transform an instruction with temporaries into a list of instructions.

  rewriteCode(listcode) → None

    Modify the code to replace temporaries with registers or memory locations.

class Lib.Allocator.NaiveAllocator(fdata: FunctionData )

  Bases: `Allocator`

  Naive Allocator: try to assign a register to each temporary, fails if there are more temporaries than registers.

  replace(old_instr: Instruction ) → List[ Instruction ]

    Replace Temporary operands with the corresponding allocated Register.

  prepare() → None

    Allocate all temporaries to registers. Fail if there are too many temporaries.