RISC-V

Instruction

class Instruction : public LIEF::assembly::Instruction

This class represents a RISC-V (32 or 64 bit) instruction.

Public Types

using operands_it = iterator_range<Operand::Iterator>

Public Functions

OPCODE opcode() const

The instruction opcode as defined in LLVM.

operands_it operands() const

Iterator over the operands of the current instruction.

virtual ~Instruction() override = default

Public Static Functions

static bool classof(const assembly::Instruction *inst)

True if inst is an effective instance of riscv::Instruction.

Opcodes

See LIEF::assembly::riscv::OPCODE in include/asm/riscv/opcodes.hpp

Operands

class Operand

This class represents an operand for a RISC-V instruction.

Subclassed by LIEF::assembly::riscv::operands::Immediate, LIEF::assembly::riscv::operands::Memory, LIEF::assembly::riscv::operands::PCRelative, LIEF::assembly::riscv::operands::Register

Public Functions

std::string to_string() const

Pretty representation of the operand.

template<class T>
inline const T *as() const

This function can be used to down cast an Operand instance:

std::unique_ptr<assembly::riscv::Operand> op = ...;
if (const auto* imm = inst->as<assembly::riscv::operands::Immediate>()) {
  const int64_t value = imm->value();
}
virtual ~Operand()

Friends

inline friend std::ostream &operator<<(std::ostream &os, const Operand &op)
class Iterator : public LIEF::iterator_facade_base<Iterator, std::forward_iterator_tag, Operand, std::ptrdiff_t, const Operand*, const Operand&>

Forward iterator that lazily disassembles riscv Operand.

Public Types

using implementation = details::OperandIt

Public Functions

Iterator()
Iterator(std::unique_ptr<details::OperandIt> impl)
Iterator(const Iterator&)
Iterator &operator=(const Iterator&)
Iterator(Iterator&&) noexcept
Iterator &operator=(Iterator&&) noexcept
~Iterator()
Iterator &operator++()
const Operand &operator*() const
const Operand *operator->() const
std::unique_ptr<Operand> yield()

Transfer ownership of the operand at the current position to the caller. Returns nullptr if the iterator is past-the-end.

inline DerivedT operator++(int)

Friends

friend bool operator==(const Iterator &LHS, const Iterator &RHS)
inline friend bool operator!=(const Iterator &LHS, const Iterator &RHS)

Immediate

class Immediate : public LIEF::assembly::riscv::Operand

This class represents an immediate operand (i.e. a constant).

For instance:

addi a0, a1, 8
             |
             +---> Immediate(8)

Public Functions

int64_t value() const

The constant value wrapped by this operand.

~Immediate() override = default

Public Static Functions

static bool classof(const Operand *op)

Register

class Register : public LIEF::assembly::riscv::Operand

This class represents a register operand.

RISC-V exposes two kinds of registers: regular registers (GPR, FPR, vector, …) and control and status registers (CSR / system registers).

csrr    a0, mstatus
        |   |
 +------+   +-------+
 |                  |
 v                  v
 REG              SYSREG

Public Functions

reg_t value() const

The effective register as either: a REG or a SYSREG.

~Register() override = default

Public Static Functions

static bool classof(const Operand *op)
struct reg_t

Public Types

enum class TYPE

Enum type used to discriminate the anonymous union.

Values:

enumerator NONE = 0
enumerator SYSREG

The union holds a sysreg attribute.

enumerator REG

The union holds the reg attribute.

Public Members

REG reg = REG::NoRegister
SYSREG sysreg
union LIEF::assembly::riscv::operands::Register::reg_t::[anonymous] [anonymous]
TYPE type = TYPE::NONE

Memory

class Memory : public LIEF::assembly::riscv::Operand

This class represents a memory operand.

lw   a0, 8(sp)
         |  |
         |  +----> Base: sp
         |
         +-------> Displacement: 8

Public Functions

REG base() const

The base register.

For lw a0, 8(sp) it would return sp.

int64_t displacement() const

The displacement value.

For lw a0, 8(sp) it would return 8.

~Memory() override = default

Public Static Functions

static bool classof(const Operand *op)

PCRelative

class PCRelative : public LIEF::assembly::riscv::Operand

This class represents a PC-relative operand.

auipc a0, 0x1
          |
          v
       PC Relative operand

Public Functions

int64_t value() const

The effective value that is relative to the current pc register.

~PCRelative() override = default

Public Static Functions

static bool classof(const Operand *op)