# Lib.Operands module

This file defines the base class `Operand` and its subclasses for different operands: `Condition`, `DataLocation` and `Function`.

The class `DataLocation` itself has subclasses: `Register`, `Offset` for address in memory, `Immediate` for constants and `Temporary` for location not yet allocated.

This file also define shortcuts for registers in RISCV.

class Lib.Operands.Condition(optype)

  Bases: `Operand`

  Condition, i.e. comparison operand for a CondJump.

  Example usage :

  - Condition(‘beq’) = branch if equal.

  - Condition(MiniCParser.LT) = branch if lower than.

  - …

  The constructor’s argument shall be a string in the list all_ops, or a comparison operator in MiniCParser.LT, MiniCParser.GT, … (one of the keys in opdict).

  A ‘negate’ method allows getting the negation of this condition.

  negate() → Condition

    Return the opposite condition.

class Lib.Operands.DataLocation

  Bases: `Operand`

  A Data Location is either a register, a temporary or a place in memory (offset).

class Lib.Operands.Function(name: str)

  Bases: `Operand`

  Operand for build-in function call.

class Lib.Operands.Immediate(val)

  Bases: `DataLocation`

  Immediate operand (integer).

class Lib.Operands.Offset(basereg: Register, offset: int)

  Bases: `DataLocation`

  Offset = address in memory computed with base + offset.

  get_offset() → int

    Return the value of the offset.

class Lib.Operands.Operand

  Bases: `object`

class Lib.Operands.Register(number: int)

  Bases: `DataLocation`

  A (physical) register.

class Lib.Operands.Renamer(pool: TemporaryPool)

  Bases: `object`

  Manage a renaming of temporaries.

  copy()

    Give a copy of the Renamer.

  defined(t: Temporary) → bool

    True if the Temporary is renamed.

  fresh(t: Temporary) → Temporary

    Give a fresh rename for a Temporary.

  replace(t: Temporary) → Temporary

    Give the rename for a Temporary (which is itself if it is not renamed).

class Lib.Operands.Temporary(number: int, pool: TemporaryPool)

  Bases: `DataLocation`

  Temporary, a location that has not been allocated yet. It will later be mapped to a physical register (Register) or to a memory location (Offset).

  get_alloced_loc() → DataLocation

    Return the DataLocation allocated to this Temporary.

class Lib.Operands.TemporaryPool

  Bases: `object`

  Manage a pool of temporaries.

  add_tmp(t: Temporary)

    Add a temporary to the pool.

  fresh_tmp() → Temporary

    Give a new fresh Temporary and add it to the pool.

  get_all_temps() → List[Temporary]

    Return all the temporaries of the pool.

  get_alloced_loc(t: Temporary) → DataLocation

    Get the actual DataLocation allocated for the temporary t.

  set_temp_allocation(allocation: Dict[Temporary, DataLocation]) → None

    Give a mapping from temporaries to actual registers. The argument allocation must be a dict from Temporary to DataLocation other than Temporary (typically Register or Offset). Typing enforces that keys are Temporary and values are Datalocation. We check the values are indeed not Temporary.