MIPT-ILab/mipt-mips

Implement ALU as a template class

graudtv opened this issue · 6 comments

All ALU methods are parameterized with typename I. It may be better to extract it to ALU definition, so it will look like this:

template <typename I>
struct ALU
{
    using Predicate = bool (*)( const I*);
    // ...

    static bool eq( const I* instr)  { return instr->v_src[0] == instr->v_src[1]; }
    // ...
};

instead of current implementation:

struct ALU
{
    template<typename I> using Predicate = bool (*)( const I*);
    // ...

    template<typename I> static bool eq( const I* instr)  { return instr->v_src[0] == instr->v_src[1]; }
    template<typename I> /* ... */
    // ...
};

ALU has to access I internals, so we define it as a friend class:

public:
friend struct ALU;
friend struct RISCVMultALU;
friend struct MIPSMultALU;

If I recall correctly, C++ cannot befriend with templated class, and it prevents doing that.
But I may be wrong.

Your suggestion: https://godbolt.org/z/7h9d1a (does not work)
Current code: https://godbolt.org/z/GcP6bc (works)

It may work if investigated more accurately. I don't find it is an important change, actually.

I suppose there are simpler ways. In this case usingexecute_foo<Datapath<int>>(&instr) instead of execute_foo<Instruction<int>>(&instr) solves the problem.
I'll think about it more precisely although

Your return of investment would be low, a lot of mechanical work without much value.
Consider investing your time in #304, for example