alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/ValidateLimitations.h" |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 8 | |
| 9 | #include "compiler/translator/Diagnostics.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 10 | #include "compiler/translator/InitializeParseContext.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 11 | #include "compiler/translator/ParseContext.h" |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 12 | #include "angle_gl.h" |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 13 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 14 | namespace sh |
| 15 | { |
| 16 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 17 | namespace |
| 18 | { |
zmo@google.com | 0b8d4eb | 2011-04-04 19:17:11 +0000 | [diff] [blame] | 19 | |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 20 | int GetLoopSymbolId(TIntermLoop *loop) |
| 21 | { |
| 22 | // Here we assume all the operations are valid, because the loop node is |
| 23 | // already validated before this call. |
| 24 | TIntermSequence *declSeq = loop->getInit()->getAsDeclarationNode()->getSequence(); |
| 25 | TIntermBinary *declInit = (*declSeq)[0]->getAsBinaryNode(); |
| 26 | TIntermSymbol *symbol = declInit->getLeft()->getAsSymbolNode(); |
| 27 | |
| 28 | return symbol->getId(); |
| 29 | } |
| 30 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 31 | // Traverses a node to check if it represents a constant index expression. |
| 32 | // Definition: |
| 33 | // constant-index-expressions are a superset of constant-expressions. |
| 34 | // Constant-index-expressions can include loop indices as defined in |
| 35 | // GLSL ES 1.0 spec, Appendix A, section 4. |
| 36 | // The following are constant-index-expressions: |
| 37 | // - Constant expressions |
| 38 | // - Loop indices as defined in section 4 |
| 39 | // - Expressions composed of both of the above |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 40 | class ValidateConstIndexExpr : public TIntermTraverser |
| 41 | { |
| 42 | public: |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 43 | ValidateConstIndexExpr(const std::vector<int> &loopSymbols) |
| 44 | : TIntermTraverser(true, false, false), mValid(true), mLoopSymbolIds(loopSymbols) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame] | 45 | { |
| 46 | } |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 47 | |
| 48 | // Returns true if the parsed node represents a constant index expression. |
| 49 | bool isValid() const { return mValid; } |
| 50 | |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 51 | void visitSymbol(TIntermSymbol *symbol) override |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 52 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 53 | // Only constants and loop indices are allowed in a |
| 54 | // constant index expression. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 55 | if (mValid) |
| 56 | { |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 57 | bool isLoopSymbol = std::find(mLoopSymbolIds.begin(), mLoopSymbolIds.end(), |
| 58 | symbol->getId()) != mLoopSymbolIds.end(); |
| 59 | mValid = (symbol->getQualifier() == EvqConst) || isLoopSymbol; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 62 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 63 | private: |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 64 | bool mValid; |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 65 | const std::vector<int> mLoopSymbolIds; |
zmo@google.com | 0b8d4eb | 2011-04-04 19:17:11 +0000 | [diff] [blame] | 66 | }; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 67 | |
| 68 | } // namespace anonymous |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 69 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 70 | ValidateLimitations::ValidateLimitations(sh::GLenum shaderType, TDiagnostics *diagnostics) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame] | 71 | : TIntermTraverser(true, false, false), |
| 72 | mShaderType(shaderType), |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 73 | mDiagnostics(diagnostics), |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 74 | mValidateIndexing(true), |
| 75 | mValidateInnerLoops(true) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 76 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 77 | ASSERT(diagnostics); |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 80 | bool ValidateLimitations::visitBinary(Visit, TIntermBinary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 81 | { |
| 82 | // Check if loop index is modified in the loop body. |
| 83 | validateOperation(node, node->getLeft()); |
| 84 | |
| 85 | // Check indexing. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 86 | switch (node->getOp()) |
| 87 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 88 | case EOpIndexDirect: |
| 89 | case EOpIndexIndirect: |
| 90 | if (mValidateIndexing) |
| 91 | validateIndexing(node); |
| 92 | break; |
| 93 | default: |
| 94 | break; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 99 | bool ValidateLimitations::visitUnary(Visit, TIntermUnary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 100 | { |
| 101 | // Check if loop index is modified in the loop body. |
| 102 | validateOperation(node, node->getOperand()); |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 107 | bool ValidateLimitations::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 108 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 109 | switch (node->getOp()) |
| 110 | { |
| 111 | case EOpFunctionCall: |
| 112 | validateFunctionCall(node); |
| 113 | break; |
| 114 | default: |
| 115 | break; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 120 | bool ValidateLimitations::visitLoop(Visit, TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 121 | { |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 122 | if (!mValidateInnerLoops) |
| 123 | return true; |
| 124 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 125 | if (!validateLoopType(node)) |
| 126 | return false; |
| 127 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 128 | if (!validateForLoopHeader(node)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 129 | return false; |
| 130 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 131 | TIntermNode *body = node->getBody(); |
| 132 | if (body != NULL) |
| 133 | { |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 134 | mLoopSymbolIds.push_back(GetLoopSymbolId(node)); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 135 | body->traverse(this); |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 136 | mLoopSymbolIds.pop_back(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // The loop is fully processed - no need to visit children. |
| 140 | return false; |
| 141 | } |
| 142 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 143 | void ValidateLimitations::error(TSourceLoc loc, const char *reason, const char *token) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 144 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 145 | mDiagnostics->error(loc, reason, token); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool ValidateLimitations::withinLoopBody() const |
| 149 | { |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 150 | return !mLoopSymbolIds.empty(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 153 | bool ValidateLimitations::isLoopIndex(TIntermSymbol *symbol) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 154 | { |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 155 | return std::find(mLoopSymbolIds.begin(), mLoopSymbolIds.end(), symbol->getId()) != |
| 156 | mLoopSymbolIds.end(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 159 | bool ValidateLimitations::validateLoopType(TIntermLoop *node) |
| 160 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 161 | TLoopType type = node->getType(); |
| 162 | if (type == ELoopFor) |
| 163 | return true; |
| 164 | |
| 165 | // Reject while and do-while loops. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 166 | error(node->getLine(), "This type of loop is not allowed", type == ELoopWhile ? "while" : "do"); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 170 | bool ValidateLimitations::validateForLoopHeader(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 171 | { |
| 172 | ASSERT(node->getType() == ELoopFor); |
| 173 | |
| 174 | // |
| 175 | // The for statement has the form: |
| 176 | // for ( init-declaration ; condition ; expression ) statement |
| 177 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 178 | int indexSymbolId = validateForLoopInit(node); |
| 179 | if (indexSymbolId < 0) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 180 | return false; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 181 | if (!validateForLoopCond(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 182 | return false; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 183 | if (!validateForLoopExpr(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 184 | return false; |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 189 | int ValidateLimitations::validateForLoopInit(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 190 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 191 | TIntermNode *init = node->getInit(); |
| 192 | if (init == NULL) |
| 193 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 194 | error(node->getLine(), "Missing init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 195 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // |
| 199 | // init-declaration has the form: |
| 200 | // type-specifier identifier = constant-expression |
| 201 | // |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 202 | TIntermDeclaration *decl = init->getAsDeclarationNode(); |
| 203 | if (decl == nullptr) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 204 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 205 | error(init->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 206 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 207 | } |
| 208 | // To keep things simple do not allow declaration list. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 209 | TIntermSequence *declSeq = decl->getSequence(); |
| 210 | if (declSeq->size() != 1) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 211 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 212 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 213 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 214 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 215 | TIntermBinary *declInit = (*declSeq)[0]->getAsBinaryNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 216 | if ((declInit == NULL) || (declInit->getOp() != EOpInitialize)) |
| 217 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 218 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 219 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 220 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 221 | TIntermSymbol *symbol = declInit->getLeft()->getAsSymbolNode(); |
| 222 | if (symbol == NULL) |
| 223 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 224 | error(declInit->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 225 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 226 | } |
| 227 | // The loop index has type int or float. |
| 228 | TBasicType type = symbol->getBasicType(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 229 | if ((type != EbtInt) && (type != EbtUInt) && (type != EbtFloat)) |
| 230 | { |
| 231 | error(symbol->getLine(), "Invalid type for loop index", getBasicString(type)); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 232 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 233 | } |
| 234 | // The loop index is initialized with constant expression. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 235 | if (!isConstExpr(declInit->getRight())) |
| 236 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 237 | error(declInit->getLine(), "Loop index cannot be initialized with non-constant expression", |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 238 | symbol->getSymbol().c_str()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 239 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 242 | return symbol->getId(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 245 | bool ValidateLimitations::validateForLoopCond(TIntermLoop *node, int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 246 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 247 | TIntermNode *cond = node->getCondition(); |
| 248 | if (cond == NULL) |
| 249 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 250 | error(node->getLine(), "Missing condition", "for"); |
| 251 | return false; |
| 252 | } |
| 253 | // |
| 254 | // condition has the form: |
| 255 | // loop_index relational_operator constant_expression |
| 256 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 257 | TIntermBinary *binOp = cond->getAsBinaryNode(); |
| 258 | if (binOp == NULL) |
| 259 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 260 | error(node->getLine(), "Invalid condition", "for"); |
| 261 | return false; |
| 262 | } |
| 263 | // Loop index should be to the left of relational operator. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 264 | TIntermSymbol *symbol = binOp->getLeft()->getAsSymbolNode(); |
| 265 | if (symbol == NULL) |
| 266 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 267 | error(binOp->getLine(), "Invalid condition", "for"); |
| 268 | return false; |
| 269 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 270 | if (symbol->getId() != indexSymbolId) |
| 271 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 272 | error(symbol->getLine(), "Expected loop index", symbol->getSymbol().c_str()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | // Relational operator is one of: > >= < <= == or !=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 276 | switch (binOp->getOp()) |
| 277 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 278 | case EOpEqual: |
| 279 | case EOpNotEqual: |
| 280 | case EOpLessThan: |
| 281 | case EOpGreaterThan: |
| 282 | case EOpLessThanEqual: |
| 283 | case EOpGreaterThanEqual: |
| 284 | break; |
| 285 | default: |
| 286 | error(binOp->getLine(), "Invalid relational operator", |
| 287 | GetOperatorString(binOp->getOp())); |
| 288 | break; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 289 | } |
| 290 | // Loop index must be compared with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 291 | if (!isConstExpr(binOp->getRight())) |
| 292 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 293 | error(binOp->getLine(), "Loop index cannot be compared with non-constant expression", |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 294 | symbol->getSymbol().c_str()); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | return true; |
| 299 | } |
| 300 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 301 | bool ValidateLimitations::validateForLoopExpr(TIntermLoop *node, int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 302 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 303 | TIntermNode *expr = node->getExpression(); |
| 304 | if (expr == NULL) |
| 305 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 306 | error(node->getLine(), "Missing expression", "for"); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | // for expression has one of the following forms: |
| 311 | // loop_index++ |
| 312 | // loop_index-- |
| 313 | // loop_index += constant_expression |
| 314 | // loop_index -= constant_expression |
| 315 | // ++loop_index |
| 316 | // --loop_index |
| 317 | // The last two forms are not specified in the spec, but I am assuming |
| 318 | // its an oversight. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 319 | TIntermUnary *unOp = expr->getAsUnaryNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 320 | TIntermBinary *binOp = unOp ? NULL : expr->getAsBinaryNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 321 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 322 | TOperator op = EOpNull; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 323 | TIntermSymbol *symbol = NULL; |
| 324 | if (unOp != NULL) |
| 325 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 326 | op = unOp->getOp(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 327 | symbol = unOp->getOperand()->getAsSymbolNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 328 | } |
| 329 | else if (binOp != NULL) |
| 330 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 331 | op = binOp->getOp(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 332 | symbol = binOp->getLeft()->getAsSymbolNode(); |
| 333 | } |
| 334 | |
| 335 | // The operand must be loop index. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 336 | if (symbol == NULL) |
| 337 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 338 | error(expr->getLine(), "Invalid expression", "for"); |
| 339 | return false; |
| 340 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 341 | if (symbol->getId() != indexSymbolId) |
| 342 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 343 | error(symbol->getLine(), "Expected loop index", symbol->getSymbol().c_str()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
| 347 | // The operator is one of: ++ -- += -=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 348 | switch (op) |
| 349 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 350 | case EOpPostIncrement: |
| 351 | case EOpPostDecrement: |
| 352 | case EOpPreIncrement: |
| 353 | case EOpPreDecrement: |
| 354 | ASSERT((unOp != NULL) && (binOp == NULL)); |
| 355 | break; |
| 356 | case EOpAddAssign: |
| 357 | case EOpSubAssign: |
| 358 | ASSERT((unOp == NULL) && (binOp != NULL)); |
| 359 | break; |
| 360 | default: |
| 361 | error(expr->getLine(), "Invalid operator", GetOperatorString(op)); |
| 362 | return false; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Loop index must be incremented/decremented with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 366 | if (binOp != NULL) |
| 367 | { |
| 368 | if (!isConstExpr(binOp->getRight())) |
| 369 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 370 | error(binOp->getLine(), "Loop index cannot be modified by non-constant expression", |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 371 | symbol->getSymbol().c_str()); |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 379 | bool ValidateLimitations::validateFunctionCall(TIntermAggregate *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 380 | { |
| 381 | ASSERT(node->getOp() == EOpFunctionCall); |
| 382 | |
| 383 | // If not within loop body, there is nothing to check. |
| 384 | if (!withinLoopBody()) |
| 385 | return true; |
| 386 | |
| 387 | // List of param indices for which loop indices are used as argument. |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 388 | typedef std::vector<size_t> ParamIndex; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 389 | ParamIndex pIndex; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 390 | TIntermSequence *params = node->getSequence(); |
| 391 | for (TIntermSequence::size_type i = 0; i < params->size(); ++i) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 392 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 393 | TIntermSymbol *symbol = (*params)[i]->getAsSymbolNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 394 | if (symbol && isLoopIndex(symbol)) |
| 395 | pIndex.push_back(i); |
| 396 | } |
| 397 | // If none of the loop indices are used as arguments, |
| 398 | // there is nothing to check. |
| 399 | if (pIndex.empty()) |
| 400 | return true; |
| 401 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 402 | bool valid = true; |
| 403 | TSymbolTable &symbolTable = GetGlobalParseContext()->symbolTable; |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 404 | TSymbol *symbol = symbolTable.find(node->getFunctionSymbolInfo()->getName(), |
| 405 | GetGlobalParseContext()->getShaderVersion()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 406 | ASSERT(symbol && symbol->isFunction()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 407 | TFunction *function = static_cast<TFunction *>(symbol); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 408 | for (ParamIndex::const_iterator i = pIndex.begin(); i != pIndex.end(); ++i) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 409 | { |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 410 | const TConstParameter ¶m = function->getParam(*i); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 411 | TQualifier qual = param.type->getQualifier(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 412 | if ((qual == EvqOut) || (qual == EvqInOut)) |
| 413 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 414 | error((*params)[*i]->getLine(), |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 415 | "Loop index cannot be used as argument to a function out or inout parameter", |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 416 | (*params)[*i]->getAsSymbolNode()->getSymbol().c_str()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 417 | valid = false; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return valid; |
| 422 | } |
| 423 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 424 | bool ValidateLimitations::validateOperation(TIntermOperator *node, TIntermNode *operand) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 425 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 426 | // Check if loop index is modified in the loop body. |
Jamie Madill | f4b79ba | 2013-11-26 10:38:18 -0500 | [diff] [blame] | 427 | if (!withinLoopBody() || !node->isAssignment()) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 428 | return true; |
| 429 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 430 | TIntermSymbol *symbol = operand->getAsSymbolNode(); |
| 431 | if (symbol && isLoopIndex(symbol)) |
| 432 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 433 | error(node->getLine(), |
| 434 | "Loop index cannot be statically assigned to within the body of the loop", |
| 435 | symbol->getSymbol().c_str()); |
| 436 | } |
| 437 | return true; |
| 438 | } |
| 439 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 440 | bool ValidateLimitations::isConstExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 441 | { |
Olli Etuaho | d561057 | 2015-12-10 19:42:09 +0200 | [diff] [blame] | 442 | ASSERT(node != nullptr); |
| 443 | return node->getAsConstantUnion() != nullptr && node->getAsTyped()->getQualifier() == EvqConst; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 446 | bool ValidateLimitations::isConstIndexExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 447 | { |
| 448 | ASSERT(node != NULL); |
| 449 | |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame] | 450 | ValidateConstIndexExpr validate(mLoopSymbolIds); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 451 | node->traverse(&validate); |
| 452 | return validate.isValid(); |
| 453 | } |
| 454 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 455 | bool ValidateLimitations::validateIndexing(TIntermBinary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 456 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 457 | ASSERT((node->getOp() == EOpIndexDirect) || (node->getOp() == EOpIndexIndirect)); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 458 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 459 | bool valid = true; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 460 | TIntermTyped *index = node->getRight(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 461 | // The index expession must be a constant-index-expression unless |
| 462 | // the operand is a uniform in a vertex shader. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 463 | TIntermTyped *operand = node->getLeft(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 464 | bool skip = (mShaderType == GL_VERTEX_SHADER) && (operand->getQualifier() == EvqUniform); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 465 | if (!skip && !isConstIndexExpr(index)) |
| 466 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 467 | error(index->getLine(), "Index expression must be constant", "[]"); |
| 468 | valid = false; |
| 469 | } |
| 470 | return valid; |
| 471 | } |
| 472 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 473 | } // namespace sh |