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