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