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 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 108 | case EOpIndexDirect: |
| 109 | case EOpIndexIndirect: |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 110 | if (mValidateIndexing) |
| 111 | validateIndexing(node); |
| 112 | break; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 113 | default: |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 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 | { |
| 129 | switch (node->getOp()) { |
| 130 | case EOpFunctionCall: |
| 131 | validateFunctionCall(node); |
| 132 | break; |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 139 | bool ValidateLimitations::visitLoop(Visit, TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 140 | { |
Olli Etuaho | 8a76dcc | 2015-12-10 20:25:12 +0200 | [diff] [blame] | 141 | if (!mValidateInnerLoops) |
| 142 | return true; |
| 143 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 144 | if (!validateLoopType(node)) |
| 145 | return false; |
| 146 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 147 | if (!validateForLoopHeader(node)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 148 | return false; |
| 149 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 150 | TIntermNode *body = node->getBody(); |
| 151 | if (body != NULL) |
| 152 | { |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame^] | 153 | mLoopSymbolIds.push_back(GetLoopSymbolId(node)); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 154 | body->traverse(this); |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame^] | 155 | mLoopSymbolIds.pop_back(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // The loop is fully processed - no need to visit children. |
| 159 | return false; |
| 160 | } |
| 161 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 162 | void ValidateLimitations::error(TSourceLoc loc, |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 163 | 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. |
| 192 | error(node->getLine(), |
| 193 | "This type of loop is not allowed", |
| 194 | type == ELoopWhile ? "while" : "do"); |
| 195 | return false; |
| 196 | } |
| 197 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 198 | bool ValidateLimitations::validateForLoopHeader(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 199 | { |
| 200 | ASSERT(node->getType() == ELoopFor); |
| 201 | |
| 202 | // |
| 203 | // The for statement has the form: |
| 204 | // for ( init-declaration ; condition ; expression ) statement |
| 205 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 206 | int indexSymbolId = validateForLoopInit(node); |
| 207 | if (indexSymbolId < 0) |
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 (!validateForLoopCond(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 210 | return false; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 211 | if (!validateForLoopExpr(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 212 | return false; |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 217 | int ValidateLimitations::validateForLoopInit(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 218 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 219 | TIntermNode *init = node->getInit(); |
| 220 | if (init == NULL) |
| 221 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 222 | error(node->getLine(), "Missing 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 | |
| 226 | // |
| 227 | // init-declaration has the form: |
| 228 | // type-specifier identifier = constant-expression |
| 229 | // |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 230 | TIntermDeclaration *decl = init->getAsDeclarationNode(); |
| 231 | if (decl == nullptr) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 232 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 233 | error(init->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 234 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 235 | } |
| 236 | // To keep things simple do not allow declaration list. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 237 | TIntermSequence *declSeq = decl->getSequence(); |
| 238 | if (declSeq->size() != 1) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 239 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 240 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 241 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 242 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 243 | TIntermBinary *declInit = (*declSeq)[0]->getAsBinaryNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 244 | if ((declInit == NULL) || (declInit->getOp() != EOpInitialize)) |
| 245 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 246 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 247 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 248 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 249 | TIntermSymbol *symbol = declInit->getLeft()->getAsSymbolNode(); |
| 250 | if (symbol == NULL) |
| 251 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 252 | error(declInit->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 253 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 254 | } |
| 255 | // The loop index has type int or float. |
| 256 | TBasicType type = symbol->getBasicType(); |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 257 | if ((type != EbtInt) && (type != EbtUInt) && (type != EbtFloat)) { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 258 | error(symbol->getLine(), |
| 259 | "Invalid type for loop index", getBasicString(type)); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 260 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 261 | } |
| 262 | // The loop index is initialized with constant expression. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 263 | if (!isConstExpr(declInit->getRight())) |
| 264 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 265 | error(declInit->getLine(), |
| 266 | "Loop index cannot be initialized with non-constant expression", |
| 267 | symbol->getSymbol().c_str()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 268 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 271 | return symbol->getId(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 274 | bool ValidateLimitations::validateForLoopCond(TIntermLoop *node, |
| 275 | int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 276 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 277 | TIntermNode *cond = node->getCondition(); |
| 278 | if (cond == NULL) |
| 279 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 280 | error(node->getLine(), "Missing condition", "for"); |
| 281 | return false; |
| 282 | } |
| 283 | // |
| 284 | // condition has the form: |
| 285 | // loop_index relational_operator constant_expression |
| 286 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 287 | TIntermBinary *binOp = cond->getAsBinaryNode(); |
| 288 | if (binOp == NULL) |
| 289 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 290 | error(node->getLine(), "Invalid condition", "for"); |
| 291 | return false; |
| 292 | } |
| 293 | // Loop index should be to the left of relational operator. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 294 | TIntermSymbol *symbol = binOp->getLeft()->getAsSymbolNode(); |
| 295 | if (symbol == NULL) |
| 296 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 297 | error(binOp->getLine(), "Invalid condition", "for"); |
| 298 | return false; |
| 299 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 300 | if (symbol->getId() != indexSymbolId) |
| 301 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 302 | error(symbol->getLine(), |
| 303 | "Expected loop index", symbol->getSymbol().c_str()); |
| 304 | return false; |
| 305 | } |
| 306 | // Relational operator is one of: > >= < <= == or !=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 307 | switch (binOp->getOp()) |
| 308 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 309 | case EOpEqual: |
| 310 | case EOpNotEqual: |
| 311 | case EOpLessThan: |
| 312 | case EOpGreaterThan: |
| 313 | case EOpLessThanEqual: |
| 314 | case EOpGreaterThanEqual: |
| 315 | break; |
| 316 | default: |
| 317 | error(binOp->getLine(), |
| 318 | "Invalid relational operator", |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 319 | GetOperatorString(binOp->getOp())); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | // Loop index must be compared with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 323 | if (!isConstExpr(binOp->getRight())) |
| 324 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 325 | error(binOp->getLine(), |
| 326 | "Loop index cannot be compared with non-constant expression", |
| 327 | symbol->getSymbol().c_str()); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 334 | bool ValidateLimitations::validateForLoopExpr(TIntermLoop *node, |
| 335 | int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 336 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 337 | TIntermNode *expr = node->getExpression(); |
| 338 | if (expr == NULL) |
| 339 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 340 | error(node->getLine(), "Missing expression", "for"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // for expression has one of the following forms: |
| 345 | // loop_index++ |
| 346 | // loop_index-- |
| 347 | // loop_index += constant_expression |
| 348 | // loop_index -= constant_expression |
| 349 | // ++loop_index |
| 350 | // --loop_index |
| 351 | // The last two forms are not specified in the spec, but I am assuming |
| 352 | // its an oversight. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 353 | TIntermUnary *unOp = expr->getAsUnaryNode(); |
| 354 | TIntermBinary *binOp = unOp ? NULL : expr->getAsBinaryNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 355 | |
| 356 | TOperator op = EOpNull; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 357 | TIntermSymbol *symbol = NULL; |
| 358 | if (unOp != NULL) |
| 359 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 360 | op = unOp->getOp(); |
| 361 | symbol = unOp->getOperand()->getAsSymbolNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 362 | } |
| 363 | else if (binOp != NULL) |
| 364 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 365 | op = binOp->getOp(); |
| 366 | symbol = binOp->getLeft()->getAsSymbolNode(); |
| 367 | } |
| 368 | |
| 369 | // The operand must be loop index. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 370 | if (symbol == NULL) |
| 371 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 372 | error(expr->getLine(), "Invalid expression", "for"); |
| 373 | return false; |
| 374 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 375 | if (symbol->getId() != indexSymbolId) |
| 376 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 377 | error(symbol->getLine(), |
| 378 | "Expected loop index", symbol->getSymbol().c_str()); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // The operator is one of: ++ -- += -=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 383 | switch (op) |
| 384 | { |
| 385 | case EOpPostIncrement: |
| 386 | case EOpPostDecrement: |
| 387 | case EOpPreIncrement: |
| 388 | case EOpPreDecrement: |
| 389 | ASSERT((unOp != NULL) && (binOp == NULL)); |
| 390 | break; |
| 391 | case EOpAddAssign: |
| 392 | case EOpSubAssign: |
| 393 | ASSERT((unOp == NULL) && (binOp != NULL)); |
| 394 | break; |
| 395 | default: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 396 | error(expr->getLine(), "Invalid operator", GetOperatorString(op)); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 397 | return false; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Loop index must be incremented/decremented with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 401 | if (binOp != NULL) |
| 402 | { |
| 403 | if (!isConstExpr(binOp->getRight())) |
| 404 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 405 | error(binOp->getLine(), |
| 406 | "Loop index cannot be modified by non-constant expression", |
| 407 | symbol->getSymbol().c_str()); |
| 408 | return false; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 415 | bool ValidateLimitations::validateFunctionCall(TIntermAggregate *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 416 | { |
| 417 | ASSERT(node->getOp() == EOpFunctionCall); |
| 418 | |
| 419 | // If not within loop body, there is nothing to check. |
| 420 | if (!withinLoopBody()) |
| 421 | return true; |
| 422 | |
| 423 | // 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] | 424 | typedef std::vector<size_t> ParamIndex; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 425 | ParamIndex pIndex; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 426 | TIntermSequence *params = node->getSequence(); |
| 427 | for (TIntermSequence::size_type i = 0; i < params->size(); ++i) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 428 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 429 | TIntermSymbol *symbol = (*params)[i]->getAsSymbolNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 430 | if (symbol && isLoopIndex(symbol)) |
| 431 | pIndex.push_back(i); |
| 432 | } |
| 433 | // If none of the loop indices are used as arguments, |
| 434 | // there is nothing to check. |
| 435 | if (pIndex.empty()) |
| 436 | return true; |
| 437 | |
| 438 | bool valid = true; |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 439 | TSymbolTable& symbolTable = GetGlobalParseContext()->symbolTable; |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 440 | TSymbol *symbol = symbolTable.find(node->getFunctionSymbolInfo()->getName(), |
| 441 | GetGlobalParseContext()->getShaderVersion()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 442 | ASSERT(symbol && symbol->isFunction()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 443 | TFunction *function = static_cast<TFunction *>(symbol); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 444 | for (ParamIndex::const_iterator i = pIndex.begin(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 445 | i != pIndex.end(); ++i) |
| 446 | { |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 447 | const TConstParameter ¶m = function->getParam(*i); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 448 | TQualifier qual = param.type->getQualifier(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 449 | if ((qual == EvqOut) || (qual == EvqInOut)) |
| 450 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 451 | error((*params)[*i]->getLine(), |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 452 | "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] | 453 | (*params)[*i]->getAsSymbolNode()->getSymbol().c_str()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 454 | valid = false; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return valid; |
| 459 | } |
| 460 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 461 | bool ValidateLimitations::validateOperation(TIntermOperator *node, |
| 462 | TIntermNode* operand) |
| 463 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 464 | // Check if loop index is modified in the loop body. |
Jamie Madill | f4b79ba | 2013-11-26 10:38:18 -0500 | [diff] [blame] | 465 | if (!withinLoopBody() || !node->isAssignment()) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 466 | return true; |
| 467 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 468 | TIntermSymbol *symbol = operand->getAsSymbolNode(); |
| 469 | if (symbol && isLoopIndex(symbol)) |
| 470 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 471 | error(node->getLine(), |
| 472 | "Loop index cannot be statically assigned to within the body of the loop", |
| 473 | symbol->getSymbol().c_str()); |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 478 | bool ValidateLimitations::isConstExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 479 | { |
Olli Etuaho | d561057 | 2015-12-10 19:42:09 +0200 | [diff] [blame] | 480 | ASSERT(node != nullptr); |
| 481 | return node->getAsConstantUnion() != nullptr && node->getAsTyped()->getQualifier() == EvqConst; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 484 | bool ValidateLimitations::isConstIndexExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 485 | { |
| 486 | ASSERT(node != NULL); |
| 487 | |
Corentin Wallez | 1b896c6 | 2016-11-16 13:10:44 -0500 | [diff] [blame^] | 488 | ValidateConstIndexExpr validate(mLoopSymbolIds); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 489 | node->traverse(&validate); |
| 490 | return validate.isValid(); |
| 491 | } |
| 492 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 493 | bool ValidateLimitations::validateIndexing(TIntermBinary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 494 | { |
| 495 | ASSERT((node->getOp() == EOpIndexDirect) || |
| 496 | (node->getOp() == EOpIndexIndirect)); |
| 497 | |
| 498 | bool valid = true; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 499 | TIntermTyped *index = node->getRight(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 500 | // The index expession must be a constant-index-expression unless |
| 501 | // the operand is a uniform in a vertex shader. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 502 | TIntermTyped *operand = node->getLeft(); |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 503 | bool skip = (mShaderType == GL_VERTEX_SHADER) && |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 504 | (operand->getQualifier() == EvqUniform); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 505 | if (!skip && !isConstIndexExpr(index)) |
| 506 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 507 | error(index->getLine(), "Index expression must be constant", "[]"); |
| 508 | valid = false; |
| 509 | } |
| 510 | return valid; |
| 511 | } |
| 512 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 513 | } // namespace sh |