momiji

m68k emulator infrastructure

About Source code

Table of contents

momiji::extractRegister

Defined in header <momiji/Parser.h>


std::int32_t extractRegister(const Operand& operand) (1)
Extracts the register number from a RegOperand.
  1. Parameters:
    • const momiji::Operand& operand : The operand of which the function should extract the value of the register.
    Returns: The register value in the operand if it is a RegOperand, zero otherwise.

This function takes any Operand: if it is a RegOperand then it will return the stored register number, else it will return zero.

This is mostly useful in generic code, otherwise one should have to switch on the Operand variant, and that would be annoying.

Example

Instead of this:

momiji::Operand op = /* ... */;

// ...
// A call stack later

std::int32_t regval = 0;

std::visit(op, asl::overloaded{
    [&regval] (const momiji::operands::DataRegister& op) {
        regval = op.reg;
    },

    [&regval] (const momiji::operands::AddressRegister& op) {
        regval = op.reg;
    },

    [&regval] (const momiji::operands::Address& op) {
        regval = op.reg;
    },

    // Repeat for all the other classes that expose 'reg'
    // ...

    [] (auto&&) {
        // Do nothing if none of the above
    }
});

You can write this instead:

momiji::Operand op = /* ... */;

const std::int32_t regval = momiji::extractReg(op);