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