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