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