diff --git a/compiler/generator/instructions.hh b/compiler/generator/instructions.hh index 52b33d8d5c..5920eab456 100644 --- a/compiler/generator/instructions.hh +++ b/compiler/generator/instructions.hh @@ -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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + 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 + static ValueInst* convertToValueInst(const T& val) { - return operator<(lhs, IB::genInt32NumInst(rhs)); + if constexpr (std::is_same_v) { + return IB::genInt32NumInst(val); + } else if constexpr (std::is_same_v) { + return val.fValue; + } else { + return val; // Assume ValueInst* type + } } - - private: - ValueInst* fValue; }; Typed::VarType convert2FIRType(int type);