Skip to content

Commit

Permalink
Merge branch 'grame-cncm:master-dev' into rust_os
Browse files Browse the repository at this point in the history
  • Loading branch information
crop2000 authored Oct 29, 2024
2 parents 1c65d4b + 3296c90 commit 58ad9d6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 122 deletions.
155 changes: 55 additions & 100 deletions compiler/generator/instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
40 changes: 18 additions & 22 deletions compiler/normalize/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 58ad9d6

Please sign in to comment.