diff --git a/compiler/generator/instructions.hh b/compiler/generator/instructions.hh index 64a929c452..133b3ca519 100644 --- a/compiler/generator/instructions.hh +++ b/compiler/generator/instructions.hh @@ -3156,153 +3156,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); diff --git a/compiler/normalize/simplify.cpp b/compiler/normalize/simplify.cpp index 9659344b26..9ba4ff3dc4 100644 --- a/compiler/normalize/simplify.cpp +++ b/compiler/normalize/simplify.cpp @@ -339,20 +339,18 @@ static Tree sigMap(Tree key, tfun f, Tree t) } else { tvec br; - int n = t->arity(); - bool has_label = isUIInputItem(t) || isUIOutputItem(t); - for (int i = 0; i < n; i++) { + int n = t->arity(); + int arg = 0; + if (isUIInputItem(t) || isUIOutputItem(t)) { // Do not handle labels to avoid simplifying them when using reserved keyword - if (has_label && i == 0) { - br.push_back(t->branch(i)); - } else { - br.push_back(sigMap(key, f, t->branch(i))); - } + br.push_back(t->branch(arg)); + arg++; + } + for (int i = arg; i < n; i++) { + br.push_back(sigMap(key, f, t->branch(i))); } - Tree r1 = tree(t->node(), br); - - Tree r2 = f(r1); + Tree r2 = f(tree(t->node(), br)); if (r2 == t) { setProperty(t, key, gGlobal->nil); } else { @@ -390,20 +388,18 @@ static Tree sigMapRename(Tree key, Tree env, tfun f, Tree t) } else { tvec br; - int n = t->arity(); - bool has_label = isUIInputItem(t) || isUIOutputItem(t); - for (int i = 0; i < n; i++) { + int n = t->arity(); + int arg = 0; + if (isUIInputItem(t) || isUIOutputItem(t)) { // Do not handle labels to avoid simplifying them when using reserved keyword - if (has_label && i == 0) { - br.push_back(t->branch(i)); - } else { - br.push_back(sigMapRename(key, env, f, t->branch(i))); - } + br.push_back(t->branch(arg)); + arg++; + } + for (int i = arg; i < n; i++) { + br.push_back(sigMapRename(key, env, f, t->branch(i))); } - Tree r1 = tree(t->node(), br); - - Tree r2 = f(r1); + Tree r2 = f(tree(t->node(), br)); if (r2 == t) { setProperty(t, key, gGlobal->nil); } else {