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)[source]

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[source]

Return the opposite condition.

class Lib.Operands.DataLocation[source]

Bases: Operand

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

class Lib.Operands.Function(name: str)[source]

Bases: Operand

Operand for build-in function call.

class Lib.Operands.Immediate(val)[source]

Bases: DataLocation

Immediate operand (integer).

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

Bases: DataLocation

Offset = address in memory computed with base + offset.

get_offset() int[source]

Return the value of the offset.

class Lib.Operands.Operand[source]

Bases: object

class Lib.Operands.Register(number: int)[source]

Bases: DataLocation

A (physical) register.

class Lib.Operands.Renamer(pool: TemporaryPool)[source]

Bases: object

Manage a renaming of temporaries.

copy()[source]

Give a copy of the Renamer.

defined(t: Temporary) bool[source]

True if the Temporary is renamed.

fresh(t: Temporary) Temporary[source]

Give a fresh rename for a Temporary.

replace(t: Temporary) Temporary[source]

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

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

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[source]

Return the DataLocation allocated to this Temporary.

class Lib.Operands.TemporaryPool[source]

Bases: object

Manage a pool of temporaries.

add_tmp(t: Temporary)[source]

Add a temporary to the pool.

fresh_tmp() Temporary[source]

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

get_all_temps() List[Temporary][source]

Return all the temporaries of the pool.

get_alloced_loc(t: Temporary) DataLocation[source]

Get the actual DataLocation allocated for the temporary t.

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

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.