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