From d245c72f2a92a74ccda83f41758c1948ae5132d3 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 24 Aug 2022 10:56:53 -0700 Subject: Compiler time evaluation of all int and bool operators. (#2376) * Compiler time evaluation of all int and bool operators. * Fix linux compile error. * Fix. Co-authored-by: Yong He --- source/slang/slang-ast-val.h | 64 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'source/slang/slang-ast-val.h') diff --git a/source/slang/slang-ast-val.h b/source/slang/slang-ast-val.h index 6eaaa8eb1..64f04abf9 100644 --- a/source/slang/slang-ast-val.h +++ b/source/slang/slang-ast-val.h @@ -52,29 +52,70 @@ protected: {} }; +// An compile time int val as result of some general computation. +class SomeIntVal : public IntVal +{ + SLANG_AST_CLASS(SomeIntVal) + + bool _equalsValOverride(Val* val); + void _toTextOverride(StringBuilder& out); + HashCode _getHashCodeOverride(); + Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff); + + DeclRef funcDeclRef; + List args; + Type* funcType; + static Val* tryFoldImpl(ASTBuilder* astBuilder, DeclRef newFuncDecl, List &newArgs, DiagnosticSink* sink); +}; + // polynomial expression "2*a*b^3 + 1" will be represented as: // { constantTerm:1, terms: [ { constFactor:2, paramFactors:[{"a", 1}, {"b", 3}] } ] } class PolynomialIntValFactor : public NodeBase { SLANG_AST_CLASS(PolynomialIntValFactor) public: - GenericParamIntVal* param; + IntVal* param; IntegerLiteralValue power; // for sorting only. bool operator<(const PolynomialIntValFactor& other) const { - if (param->declRef.decl < other.param->declRef.decl) - return true; - else if (param->declRef.decl == other.param->declRef.decl) - return power < other.power; - return false; + if (auto thisGenParam = as(param)) + { + if (auto thatGenParam = as(other.param)) + { + if (thisGenParam->equalsVal(thatGenParam)) + return power < other.power; + else + return thisGenParam->declRef.decl < thatGenParam->declRef.decl; + } + else + { + return true; + } + } + else + { + if (auto thatGenParam = as(other.param)) + { + return false; + } + return param == other.param ? power < other.power : param < other.param; + } + } // for sorting only. bool operator==(const PolynomialIntValFactor& other) const { - if (param->declRef.decl == other.param->declRef.decl && power == other.power) - return true; - return false; + if (auto thisGenParam = as(param)) + { + if (auto thatGenParam = as(other.param)) + { + if (param->equalsVal(other.param) && power == other.power) + return true; + } + return false; + } + return power == other.power && param == other.param; } bool equals(const PolynomialIntValFactor& other) const { @@ -132,7 +173,10 @@ public: IntegerLiteralValue constantTerm = 0; bool isConstant() { return terms.getCount() == 0; } - void canonicalize(ASTBuilder* builder); + // Canonicalize the polynomial. If the polynomial can be simplified to a constant or a genericparam, + // the method returns the value simplified to. + // Otherwise, in-place modifications are performed and returns this. + IntVal* canonicalize(ASTBuilder* builder); // Overrides should be public so base classes can access bool _equalsValOverride(Val* val); -- cgit v1.2.3