# 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.Operand

  Bases: `object`

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.Function(name: str)

  Bases: `Operand`

  Operand for build-in function call.

class Lib.Operands.DataLocation

  Bases: `Operand`

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

class Lib.Operands.Register(number: int)

  Bases: `DataLocation`

  A (physical) register.

Lib.Operands.ZERO = zero

  Zero register

Lib.Operands.RA = ra

Lib.Operands.SP = sp

Lib.Operands.GP

  Register not used for this course

Lib.Operands.TP

  Register not used for this course

Lib.Operands.A = (a0, a1, a2, a3, a4, a5, a6, a7)

Lib.Operands.S = (fp, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11)

Lib.Operands.T = (t0, t1, t2, t3, t4, t5, t6)

Lib.Operands.A0 = a0

Lib.Operands.A1 = a1

Lib.Operands.FP = fp

  Frame Pointer = Saved register 0

Lib.Operands.GP_REGS = (s4, s5, s6, s7, s8, s9, s10, s11, t0, t1, t2, t3, t4, t5, t6)

  General purpose registers, usable for the allocator

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.Immediate(val)

  Bases: `DataLocation`

  Immediate operand (integer).

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.

  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.

  add_tmp(t: Temporary )

    Add a temporary to the pool.

  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.

  fresh_tmp() → Temporary

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

class Lib.Operands.Renamer(pool: TemporaryPool )

  Bases: `object`

  Manage a renaming of temporaries.

  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).

  defined(t: Temporary ) → bool

    True if the Temporary is renamed.

  copy()

    Give a copy of the Renamer.