Skip to content

Commit

Permalink
Refactor FIRIndex class.
Browse files Browse the repository at this point in the history
  • Loading branch information
sletz committed Oct 26, 2024
1 parent 64b067b commit 3296c90
Showing 1 changed file with 55 additions and 100 deletions.
155 changes: 55 additions & 100 deletions compiler/generator/instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3153,153 +3153,108 @@ struct IB {
};

/*
* Syntactic sugar for index computations.
*
* wrapper for ValueInst* with support for basic arithmetics
*
* Syntactic sugar for index computationsw, wapper for ValueInst* with support for basic arithmetics.
*/

class FIRIndex {
public:
/* explicit constructors in order to avoid the generation of implicit conversions */
// Explicit constructors to avoid implicit conversions
explicit FIRIndex(ValueInst* inst) : fValue(inst) {}

explicit FIRIndex(int num) : fValue(IB::genInt32NumInst(num)) {}

FIRIndex(FIRIndex const& rhs) : fValue(rhs.fValue) {}

/* implicitly convert to ValueInst* in order to simplify the usage */
operator ValueInst*(void) const { return fValue; }

// Add
friend FIRIndex operator+(FIRIndex const& lhs, ValueInst* rhs)
{
return FIRIndex(IB::genAdd(lhs.fValue, rhs));
}

friend FIRIndex operator+(FIRIndex const& lhs, FIRIndex const& rhs)
{
return operator+(lhs, rhs.fValue);
}

friend FIRIndex operator+(FIRIndex const& lhs, int rhs)
{
return operator+(lhs, IB::genInt32NumInst(rhs));
}

// Sub
friend FIRIndex operator-(FIRIndex const& lhs, ValueInst* rhs)
{
return FIRIndex(IB::genSub(lhs.fValue, rhs));
}

friend FIRIndex operator-(FIRIndex const& lhs, FIRIndex const& rhs)
{
return operator-(lhs, rhs.fValue);
}

friend FIRIndex operator-(FIRIndex const& lhs, int rhs)
{
return operator-(lhs, IB::genInt32NumInst(rhs));
}

// Mul
friend FIRIndex operator*(FIRIndex const& lhs, ValueInst* rhs)
{
return FIRIndex(IB::genMul(lhs.fValue, rhs));
}

friend FIRIndex operator*(FIRIndex const& lhs, FIRIndex const& rhs)
{
return operator*(lhs, rhs.fValue);
}

friend FIRIndex operator*(FIRIndex const& lhs, int rhs)
{
return operator*(lhs, IB::genInt32NumInst(rhs));
}

// Div
friend FIRIndex operator/(FIRIndex const& lhs, ValueInst* rhs)
{
return FIRIndex(IB::genDiv(lhs.fValue, rhs));
}
// Implicit conversion to ValueInst* for ease of use
operator ValueInst*() const { return fValue; }

friend FIRIndex operator/(FIRIndex const& lhs, FIRIndex const& rhs)
// Arithmetic operators
template <typename T>
friend FIRIndex operator+(const FIRIndex& lhs, const T& rhs)
{
return operator/(lhs, rhs.fValue);
return FIRIndex(IB::genAdd(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator/(FIRIndex const& lhs, int rhs)
template <typename T>
friend FIRIndex operator-(const FIRIndex& lhs, const T& rhs)
{
return operator/(lhs, IB::genInt32NumInst(rhs));
return FIRIndex(IB::genSub(lhs.fValue, convertToValueInst(rhs)));
}

// And
friend FIRIndex operator&(FIRIndex const& lhs, ValueInst* rhs)
template <typename T>
friend FIRIndex operator*(const FIRIndex& lhs, const T& rhs)
{
return FIRIndex(IB::genAnd(lhs.fValue, rhs));
return FIRIndex(IB::genMul(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator&(FIRIndex const& lhs, FIRIndex const& rhs)
template <typename T>
friend FIRIndex operator/(const FIRIndex& lhs, const T& rhs)
{
return operator&(lhs, rhs.fValue);
return FIRIndex(IB::genDiv(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator&(FIRIndex const& lhs, int rhs)
template <typename T>
friend FIRIndex operator&(const FIRIndex& lhs, const T& rhs)
{
return operator&(lhs, IB::genInt32NumInst(rhs));
return FIRIndex(IB::genAnd(lhs.fValue, convertToValueInst(rhs)));
}

// Modulo
friend FIRIndex operator%(FIRIndex const& lhs, ValueInst* rhs)
template <typename T>
friend FIRIndex operator%(const FIRIndex& lhs, const T& rhs)
{
return FIRIndex(IB::genRem(lhs.fValue, rhs));
return FIRIndex(IB::genRem(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator%(FIRIndex const& lhs, FIRIndex const& rhs)
// Comparison operators
template <typename T>
friend FIRIndex operator==(const FIRIndex& lhs, const T& rhs)
{
return operator%(lhs, rhs.fValue);
return FIRIndex(IB::genEqual(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator%(FIRIndex const& lhs, int rhs)
template <typename T>
friend FIRIndex operator!=(const FIRIndex& lhs, const T& rhs)
{
return operator%(lhs, IB::genInt32NumInst(rhs));
return FIRIndex(IB::genNotEqual(lhs.fValue, convertToValueInst(rhs)));
}

// Equal
friend FIRIndex operator==(FIRIndex const& lhs, ValueInst* rhs)
template <typename T>
friend FIRIndex operator<(const FIRIndex& lhs, const T& rhs)
{
return FIRIndex(IB::genEqual(lhs.fValue, rhs));
return FIRIndex(IB::genLessThan(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator==(FIRIndex const& lhs, FIRIndex const& rhs)
template <typename T>
friend FIRIndex operator<=(const FIRIndex& lhs, const T& rhs)
{
return operator==(lhs, rhs.fValue);
return FIRIndex(IB::genLessEqual(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator==(FIRIndex const& lhs, int rhs)
template <typename T>
friend FIRIndex operator>(const FIRIndex& lhs, const T& rhs)
{
return operator==(lhs, IB::genInt32NumInst(rhs));
return FIRIndex(IB::genGreaterThan(lhs.fValue, convertToValueInst(rhs)));
}

// Inf
friend FIRIndex operator<(FIRIndex const& lhs, ValueInst* rhs)
template <typename T>
friend FIRIndex operator>=(const FIRIndex& lhs, const T& rhs)
{
return FIRIndex(IB::genLessThan(lhs.fValue, rhs));
return FIRIndex(IB::genGreaterEqual(lhs.fValue, convertToValueInst(rhs)));
}

friend FIRIndex operator<(FIRIndex const& lhs, FIRIndex const& rhs)
{
return operator<(lhs, rhs.fValue);
}
private:
ValueInst* fValue;

friend FIRIndex operator<(FIRIndex const& lhs, int rhs)
// Templated helper to handle different operand types
template <typename T>
static ValueInst* convertToValueInst(const T& val)
{
return operator<(lhs, IB::genInt32NumInst(rhs));
if constexpr (std::is_same_v<T, int>) {
return IB::genInt32NumInst(val);
} else if constexpr (std::is_same_v<T, FIRIndex>) {
return val.fValue;
} else {
return val; // Assume ValueInst* type
}
}

private:
ValueInst* fValue;
};

Typed::VarType convert2FIRType(int type);
Expand Down

0 comments on commit 3296c90

Please sign in to comment.