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