alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/ValidateLimitations.h" |
| 8 | #include "compiler/translator/InfoSink.h" |
| 9 | #include "compiler/translator/InitializeParseContext.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 10 | #include "compiler/translator/ParseContext.h" |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 11 | #include "angle_gl.h" |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 12 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 13 | namespace |
| 14 | { |
zmo@google.com | 0b8d4eb | 2011-04-04 19:17:11 +0000 | [diff] [blame] | 15 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 16 | // Traverses a node to check if it represents a constant index expression. |
| 17 | // Definition: |
| 18 | // constant-index-expressions are a superset of constant-expressions. |
| 19 | // Constant-index-expressions can include loop indices as defined in |
| 20 | // GLSL ES 1.0 spec, Appendix A, section 4. |
| 21 | // The following are constant-index-expressions: |
| 22 | // - Constant expressions |
| 23 | // - Loop indices as defined in section 4 |
| 24 | // - Expressions composed of both of the above |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 25 | class ValidateConstIndexExpr : public TIntermTraverser |
| 26 | { |
| 27 | public: |
| 28 | ValidateConstIndexExpr(TLoopStack& stack) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 29 | : mValid(true), mLoopStack(stack) {} |
| 30 | |
| 31 | // Returns true if the parsed node represents a constant index expression. |
| 32 | bool isValid() const { return mValid; } |
| 33 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 34 | virtual void visitSymbol(TIntermSymbol *symbol) |
| 35 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 36 | // Only constants and loop indices are allowed in a |
| 37 | // constant index expression. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 38 | if (mValid) |
| 39 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 40 | mValid = (symbol->getQualifier() == EvqConst) || |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 41 | (mLoopStack.findLoop(symbol)); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 44 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 45 | private: |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 46 | bool mValid; |
zmo@google.com | 0b8d4eb | 2011-04-04 19:17:11 +0000 | [diff] [blame] | 47 | TLoopStack& mLoopStack; |
| 48 | }; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 49 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 50 | const char *GetOperatorString(TOperator op) |
| 51 | { |
| 52 | switch (op) |
| 53 | { |
| 54 | case EOpInitialize: return "="; |
| 55 | case EOpAssign: return "="; |
| 56 | case EOpAddAssign: return "+="; |
| 57 | case EOpSubAssign: return "-="; |
| 58 | case EOpDivAssign: return "/="; |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame^] | 59 | case EOpModAssign: return "%="; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 60 | |
| 61 | // Fall-through. |
| 62 | case EOpMulAssign: |
| 63 | case EOpVectorTimesMatrixAssign: |
| 64 | case EOpVectorTimesScalarAssign: |
| 65 | case EOpMatrixTimesScalarAssign: |
| 66 | case EOpMatrixTimesMatrixAssign: return "*="; |
| 67 | |
| 68 | // Fall-through. |
| 69 | case EOpIndexDirect: |
| 70 | case EOpIndexIndirect: return "[]"; |
| 71 | |
| 72 | case EOpIndexDirectStruct: |
| 73 | case EOpIndexDirectInterfaceBlock: return "."; |
| 74 | case EOpVectorSwizzle: return "."; |
| 75 | case EOpAdd: return "+"; |
| 76 | case EOpSub: return "-"; |
| 77 | case EOpMul: return "*"; |
| 78 | case EOpDiv: return "/"; |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame^] | 79 | case EOpMod: return "%"; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 80 | case EOpEqual: return "=="; |
| 81 | case EOpNotEqual: return "!="; |
| 82 | case EOpLessThan: return "<"; |
| 83 | case EOpGreaterThan: return ">"; |
| 84 | case EOpLessThanEqual: return "<="; |
| 85 | case EOpGreaterThanEqual: return ">="; |
| 86 | |
| 87 | // Fall-through. |
| 88 | case EOpVectorTimesScalar: |
| 89 | case EOpVectorTimesMatrix: |
| 90 | case EOpMatrixTimesVector: |
| 91 | case EOpMatrixTimesScalar: |
| 92 | case EOpMatrixTimesMatrix: return "*"; |
| 93 | |
| 94 | case EOpLogicalOr: return "||"; |
| 95 | case EOpLogicalXor: return "^^"; |
| 96 | case EOpLogicalAnd: return "&&"; |
| 97 | case EOpNegative: return "-"; |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 98 | case EOpPositive: return "+"; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 99 | case EOpVectorLogicalNot: return "not"; |
| 100 | case EOpLogicalNot: return "!"; |
| 101 | case EOpPostIncrement: return "++"; |
| 102 | case EOpPostDecrement: return "--"; |
| 103 | case EOpPreIncrement: return "++"; |
| 104 | case EOpPreDecrement: return "--"; |
| 105 | |
| 106 | case EOpRadians: return "radians"; |
| 107 | case EOpDegrees: return "degrees"; |
| 108 | case EOpSin: return "sin"; |
| 109 | case EOpCos: return "cos"; |
| 110 | case EOpTan: return "tan"; |
| 111 | case EOpAsin: return "asin"; |
| 112 | case EOpAcos: return "acos"; |
| 113 | case EOpAtan: return "atan"; |
| 114 | case EOpExp: return "exp"; |
| 115 | case EOpLog: return "log"; |
| 116 | case EOpExp2: return "exp2"; |
| 117 | case EOpLog2: return "log2"; |
| 118 | case EOpSqrt: return "sqrt"; |
| 119 | case EOpInverseSqrt: return "inversesqrt"; |
| 120 | case EOpAbs: return "abs"; |
| 121 | case EOpSign: return "sign"; |
| 122 | case EOpFloor: return "floor"; |
| 123 | case EOpCeil: return "ceil"; |
| 124 | case EOpFract: return "fract"; |
| 125 | case EOpLength: return "length"; |
| 126 | case EOpNormalize: return "normalize"; |
| 127 | case EOpDFdx: return "dFdx"; |
| 128 | case EOpDFdy: return "dFdy"; |
| 129 | case EOpFwidth: return "fwidth"; |
| 130 | case EOpAny: return "any"; |
| 131 | case EOpAll: return "all"; |
| 132 | |
| 133 | default: break; |
| 134 | } |
| 135 | return ""; |
| 136 | } |
| 137 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 138 | } // namespace anonymous |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 139 | |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 140 | ValidateLimitations::ValidateLimitations(sh::GLenum shaderType, |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 141 | TInfoSinkBase &sink) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 142 | : mShaderType(shaderType), |
| 143 | mSink(sink), |
| 144 | mNumErrors(0) |
| 145 | { |
| 146 | } |
| 147 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 148 | bool ValidateLimitations::visitBinary(Visit, TIntermBinary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 149 | { |
| 150 | // Check if loop index is modified in the loop body. |
| 151 | validateOperation(node, node->getLeft()); |
| 152 | |
| 153 | // Check indexing. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 154 | switch (node->getOp()) |
| 155 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 156 | case EOpIndexDirect: |
| 157 | case EOpIndexIndirect: |
| 158 | validateIndexing(node); |
| 159 | break; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 160 | default: |
| 161 | break; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 166 | bool ValidateLimitations::visitUnary(Visit, TIntermUnary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 167 | { |
| 168 | // Check if loop index is modified in the loop body. |
| 169 | validateOperation(node, node->getOperand()); |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 174 | bool ValidateLimitations::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 175 | { |
| 176 | switch (node->getOp()) { |
| 177 | case EOpFunctionCall: |
| 178 | validateFunctionCall(node); |
| 179 | break; |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 186 | bool ValidateLimitations::visitLoop(Visit, TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 187 | { |
| 188 | if (!validateLoopType(node)) |
| 189 | return false; |
| 190 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 191 | if (!validateForLoopHeader(node)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 192 | return false; |
| 193 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 194 | TIntermNode *body = node->getBody(); |
| 195 | if (body != NULL) |
| 196 | { |
| 197 | mLoopStack.push(node); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 198 | body->traverse(this); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 199 | mLoopStack.pop(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // The loop is fully processed - no need to visit children. |
| 203 | return false; |
| 204 | } |
| 205 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 206 | void ValidateLimitations::error(TSourceLoc loc, |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 207 | const char *reason, const char *token) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 208 | { |
| 209 | mSink.prefix(EPrefixError); |
| 210 | mSink.location(loc); |
| 211 | mSink << "'" << token << "' : " << reason << "\n"; |
| 212 | ++mNumErrors; |
| 213 | } |
| 214 | |
| 215 | bool ValidateLimitations::withinLoopBody() const |
| 216 | { |
| 217 | return !mLoopStack.empty(); |
| 218 | } |
| 219 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 220 | bool ValidateLimitations::isLoopIndex(TIntermSymbol *symbol) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 221 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 222 | return mLoopStack.findLoop(symbol) != NULL; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 225 | bool ValidateLimitations::validateLoopType(TIntermLoop *node) |
| 226 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 227 | TLoopType type = node->getType(); |
| 228 | if (type == ELoopFor) |
| 229 | return true; |
| 230 | |
| 231 | // Reject while and do-while loops. |
| 232 | error(node->getLine(), |
| 233 | "This type of loop is not allowed", |
| 234 | type == ELoopWhile ? "while" : "do"); |
| 235 | return false; |
| 236 | } |
| 237 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 238 | bool ValidateLimitations::validateForLoopHeader(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 239 | { |
| 240 | ASSERT(node->getType() == ELoopFor); |
| 241 | |
| 242 | // |
| 243 | // The for statement has the form: |
| 244 | // for ( init-declaration ; condition ; expression ) statement |
| 245 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 246 | int indexSymbolId = validateForLoopInit(node); |
| 247 | if (indexSymbolId < 0) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 248 | return false; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 249 | if (!validateForLoopCond(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 250 | return false; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 251 | if (!validateForLoopExpr(node, indexSymbolId)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 252 | return false; |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 257 | int ValidateLimitations::validateForLoopInit(TIntermLoop *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 258 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 259 | TIntermNode *init = node->getInit(); |
| 260 | if (init == NULL) |
| 261 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 262 | error(node->getLine(), "Missing init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 263 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // |
| 267 | // init-declaration has the form: |
| 268 | // type-specifier identifier = constant-expression |
| 269 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 270 | TIntermAggregate *decl = init->getAsAggregate(); |
| 271 | if ((decl == NULL) || (decl->getOp() != EOpDeclaration)) |
| 272 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 273 | error(init->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 274 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 275 | } |
| 276 | // To keep things simple do not allow declaration list. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 277 | TIntermSequence *declSeq = decl->getSequence(); |
| 278 | if (declSeq->size() != 1) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 279 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 280 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 281 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 282 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 283 | TIntermBinary *declInit = (*declSeq)[0]->getAsBinaryNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 284 | if ((declInit == NULL) || (declInit->getOp() != EOpInitialize)) |
| 285 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 286 | error(decl->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 287 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 288 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 289 | TIntermSymbol *symbol = declInit->getLeft()->getAsSymbolNode(); |
| 290 | if (symbol == NULL) |
| 291 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 292 | error(declInit->getLine(), "Invalid init declaration", "for"); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 293 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 294 | } |
| 295 | // The loop index has type int or float. |
| 296 | TBasicType type = symbol->getBasicType(); |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 297 | if ((type != EbtInt) && (type != EbtUInt) && (type != EbtFloat)) { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 298 | error(symbol->getLine(), |
| 299 | "Invalid type for loop index", getBasicString(type)); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 300 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 301 | } |
| 302 | // The loop index is initialized with constant expression. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 303 | if (!isConstExpr(declInit->getRight())) |
| 304 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 305 | error(declInit->getLine(), |
| 306 | "Loop index cannot be initialized with non-constant expression", |
| 307 | symbol->getSymbol().c_str()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 308 | return -1; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 311 | return symbol->getId(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 314 | bool ValidateLimitations::validateForLoopCond(TIntermLoop *node, |
| 315 | int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 316 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 317 | TIntermNode *cond = node->getCondition(); |
| 318 | if (cond == NULL) |
| 319 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 320 | error(node->getLine(), "Missing condition", "for"); |
| 321 | return false; |
| 322 | } |
| 323 | // |
| 324 | // condition has the form: |
| 325 | // loop_index relational_operator constant_expression |
| 326 | // |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 327 | TIntermBinary *binOp = cond->getAsBinaryNode(); |
| 328 | if (binOp == NULL) |
| 329 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 330 | error(node->getLine(), "Invalid condition", "for"); |
| 331 | return false; |
| 332 | } |
| 333 | // Loop index should be to the left of relational operator. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 334 | TIntermSymbol *symbol = binOp->getLeft()->getAsSymbolNode(); |
| 335 | if (symbol == NULL) |
| 336 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 337 | error(binOp->getLine(), "Invalid condition", "for"); |
| 338 | return false; |
| 339 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 340 | if (symbol->getId() != indexSymbolId) |
| 341 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 342 | error(symbol->getLine(), |
| 343 | "Expected loop index", symbol->getSymbol().c_str()); |
| 344 | return false; |
| 345 | } |
| 346 | // Relational operator is one of: > >= < <= == or !=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 347 | switch (binOp->getOp()) |
| 348 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 349 | case EOpEqual: |
| 350 | case EOpNotEqual: |
| 351 | case EOpLessThan: |
| 352 | case EOpGreaterThan: |
| 353 | case EOpLessThanEqual: |
| 354 | case EOpGreaterThanEqual: |
| 355 | break; |
| 356 | default: |
| 357 | error(binOp->getLine(), |
| 358 | "Invalid relational operator", |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 359 | GetOperatorString(binOp->getOp())); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | // Loop index must be compared with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 363 | if (!isConstExpr(binOp->getRight())) |
| 364 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 365 | error(binOp->getLine(), |
| 366 | "Loop index cannot be compared with non-constant expression", |
| 367 | symbol->getSymbol().c_str()); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 374 | bool ValidateLimitations::validateForLoopExpr(TIntermLoop *node, |
| 375 | int indexSymbolId) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 376 | { |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 377 | TIntermNode *expr = node->getExpression(); |
| 378 | if (expr == NULL) |
| 379 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 380 | error(node->getLine(), "Missing expression", "for"); |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | // for expression has one of the following forms: |
| 385 | // loop_index++ |
| 386 | // loop_index-- |
| 387 | // loop_index += constant_expression |
| 388 | // loop_index -= constant_expression |
| 389 | // ++loop_index |
| 390 | // --loop_index |
| 391 | // The last two forms are not specified in the spec, but I am assuming |
| 392 | // its an oversight. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 393 | TIntermUnary *unOp = expr->getAsUnaryNode(); |
| 394 | TIntermBinary *binOp = unOp ? NULL : expr->getAsBinaryNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 395 | |
| 396 | TOperator op = EOpNull; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 397 | TIntermSymbol *symbol = NULL; |
| 398 | if (unOp != NULL) |
| 399 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 400 | op = unOp->getOp(); |
| 401 | symbol = unOp->getOperand()->getAsSymbolNode(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 402 | } |
| 403 | else if (binOp != NULL) |
| 404 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 405 | op = binOp->getOp(); |
| 406 | symbol = binOp->getLeft()->getAsSymbolNode(); |
| 407 | } |
| 408 | |
| 409 | // The operand must be loop index. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 410 | if (symbol == NULL) |
| 411 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 412 | error(expr->getLine(), "Invalid expression", "for"); |
| 413 | return false; |
| 414 | } |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 415 | if (symbol->getId() != indexSymbolId) |
| 416 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 417 | error(symbol->getLine(), |
| 418 | "Expected loop index", symbol->getSymbol().c_str()); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | // The operator is one of: ++ -- += -=. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 423 | switch (op) |
| 424 | { |
| 425 | case EOpPostIncrement: |
| 426 | case EOpPostDecrement: |
| 427 | case EOpPreIncrement: |
| 428 | case EOpPreDecrement: |
| 429 | ASSERT((unOp != NULL) && (binOp == NULL)); |
| 430 | break; |
| 431 | case EOpAddAssign: |
| 432 | case EOpSubAssign: |
| 433 | ASSERT((unOp == NULL) && (binOp != NULL)); |
| 434 | break; |
| 435 | default: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 436 | error(expr->getLine(), "Invalid operator", GetOperatorString(op)); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 437 | return false; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Loop index must be incremented/decremented with a constant. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 441 | if (binOp != NULL) |
| 442 | { |
| 443 | if (!isConstExpr(binOp->getRight())) |
| 444 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 445 | error(binOp->getLine(), |
| 446 | "Loop index cannot be modified by non-constant expression", |
| 447 | symbol->getSymbol().c_str()); |
| 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 455 | bool ValidateLimitations::validateFunctionCall(TIntermAggregate *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 456 | { |
| 457 | ASSERT(node->getOp() == EOpFunctionCall); |
| 458 | |
| 459 | // If not within loop body, there is nothing to check. |
| 460 | if (!withinLoopBody()) |
| 461 | return true; |
| 462 | |
| 463 | // 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] | 464 | typedef std::vector<size_t> ParamIndex; |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 465 | ParamIndex pIndex; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 466 | TIntermSequence *params = node->getSequence(); |
| 467 | for (TIntermSequence::size_type i = 0; i < params->size(); ++i) |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 468 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 469 | TIntermSymbol *symbol = (*params)[i]->getAsSymbolNode(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 470 | if (symbol && isLoopIndex(symbol)) |
| 471 | pIndex.push_back(i); |
| 472 | } |
| 473 | // If none of the loop indices are used as arguments, |
| 474 | // there is nothing to check. |
| 475 | if (pIndex.empty()) |
| 476 | return true; |
| 477 | |
| 478 | bool valid = true; |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 479 | TSymbolTable& symbolTable = GetGlobalParseContext()->symbolTable; |
| 480 | TSymbol* symbol = symbolTable.find(node->getName(), GetGlobalParseContext()->shaderVersion); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 481 | ASSERT(symbol && symbol->isFunction()); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 482 | TFunction *function = static_cast<TFunction *>(symbol); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 483 | for (ParamIndex::const_iterator i = pIndex.begin(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 484 | i != pIndex.end(); ++i) |
| 485 | { |
| 486 | const TParameter ¶m = function->getParam(*i); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 487 | TQualifier qual = param.type->getQualifier(); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 488 | if ((qual == EvqOut) || (qual == EvqInOut)) |
| 489 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 490 | error((*params)[*i]->getLine(), |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 491 | "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] | 492 | (*params)[*i]->getAsSymbolNode()->getSymbol().c_str()); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 493 | valid = false; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return valid; |
| 498 | } |
| 499 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 500 | bool ValidateLimitations::validateOperation(TIntermOperator *node, |
| 501 | TIntermNode* operand) |
| 502 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 503 | // Check if loop index is modified in the loop body. |
Jamie Madill | f4b79ba | 2013-11-26 10:38:18 -0500 | [diff] [blame] | 504 | if (!withinLoopBody() || !node->isAssignment()) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 505 | return true; |
| 506 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 507 | TIntermSymbol *symbol = operand->getAsSymbolNode(); |
| 508 | if (symbol && isLoopIndex(symbol)) |
| 509 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 510 | error(node->getLine(), |
| 511 | "Loop index cannot be statically assigned to within the body of the loop", |
| 512 | symbol->getSymbol().c_str()); |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 517 | bool ValidateLimitations::isConstExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 518 | { |
| 519 | ASSERT(node != NULL); |
| 520 | return node->getAsConstantUnion() != NULL; |
| 521 | } |
| 522 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 523 | bool ValidateLimitations::isConstIndexExpr(TIntermNode *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 524 | { |
| 525 | ASSERT(node != NULL); |
| 526 | |
| 527 | ValidateConstIndexExpr validate(mLoopStack); |
| 528 | node->traverse(&validate); |
| 529 | return validate.isValid(); |
| 530 | } |
| 531 | |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 532 | bool ValidateLimitations::validateIndexing(TIntermBinary *node) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 533 | { |
| 534 | ASSERT((node->getOp() == EOpIndexDirect) || |
| 535 | (node->getOp() == EOpIndexIndirect)); |
| 536 | |
| 537 | bool valid = true; |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 538 | TIntermTyped *index = node->getRight(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 539 | // The index expression must have integral type. |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 540 | if (!index->isScalarInt()) { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 541 | error(index->getLine(), |
| 542 | "Index expression must have integral type", |
| 543 | index->getCompleteString().c_str()); |
| 544 | valid = false; |
| 545 | } |
| 546 | // The index expession must be a constant-index-expression unless |
| 547 | // the operand is a uniform in a vertex shader. |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 548 | TIntermTyped *operand = node->getLeft(); |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 549 | bool skip = (mShaderType == GL_VERTEX_SHADER) && |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 550 | (operand->getQualifier() == EvqUniform); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 551 | if (!skip && !isConstIndexExpr(index)) |
| 552 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 553 | error(index->getLine(), "Index expression must be constant", "[]"); |
| 554 | valid = false; |
| 555 | } |
| 556 | return valid; |
| 557 | } |
| 558 | |