2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 #include "ValidateLimitations.h"
9 #include "InitializeParseContext.h"
10 #include "ParseHelper.h"
13 bool IsLoopIndex(const TIntermSymbol* symbol, const TLoopStack& stack) {
14 for (TLoopStack::const_iterator i = stack.begin(); i != stack.end(); ++i) {
15 if (i->index.id == symbol->getId())
21 void MarkLoopForUnroll(const TIntermSymbol* symbol, TLoopStack& stack) {
22 for (TLoopStack::iterator i = stack.begin(); i != stack.end(); ++i) {
23 if (i->index.id == symbol->getId()) {
24 ASSERT(i->loop != NULL);
25 i->loop->setUnrollFlag(true);
32 // Traverses a node to check if it represents a constant index expression.
34 // constant-index-expressions are a superset of constant-expressions.
35 // Constant-index-expressions can include loop indices as defined in
36 // GLSL ES 1.0 spec, Appendix A, section 4.
37 // The following are constant-index-expressions:
38 // - Constant expressions
39 // - Loop indices as defined in section 4
40 // - Expressions composed of both of the above
41 class ValidateConstIndexExpr : public TIntermTraverser {
43 ValidateConstIndexExpr(const TLoopStack& stack)
44 : mValid(true), mLoopStack(stack) {}
46 // Returns true if the parsed node represents a constant index expression.
47 bool isValid() const { return mValid; }
49 virtual void visitSymbol(TIntermSymbol* symbol) {
50 // Only constants and loop indices are allowed in a
51 // constant index expression.
53 mValid = (symbol->getQualifier() == EvqConst) ||
54 IsLoopIndex(symbol, mLoopStack);
60 const TLoopStack& mLoopStack;
63 // Traverses a node to check if it uses a loop index.
64 // If an int loop index is used in its body as a sampler array index,
65 // mark the loop for unroll.
66 class ValidateLoopIndexExpr : public TIntermTraverser {
68 ValidateLoopIndexExpr(TLoopStack& stack)
69 : mUsesFloatLoopIndex(false),
70 mUsesIntLoopIndex(false),
73 bool usesFloatLoopIndex() const { return mUsesFloatLoopIndex; }
74 bool usesIntLoopIndex() const { return mUsesIntLoopIndex; }
76 virtual void visitSymbol(TIntermSymbol* symbol) {
77 if (IsLoopIndex(symbol, mLoopStack)) {
78 switch (symbol->getBasicType()) {
80 mUsesFloatLoopIndex = true;
83 mUsesIntLoopIndex = true;
84 MarkLoopForUnroll(symbol, mLoopStack);
93 bool mUsesFloatLoopIndex;
94 bool mUsesIntLoopIndex;
95 TLoopStack& mLoopStack;
99 ValidateLimitations::ValidateLimitations(ShShaderType shaderType,
101 : mShaderType(shaderType),
107 bool ValidateLimitations::visitBinary(Visit, TIntermBinary* node)
109 // Check if loop index is modified in the loop body.
110 validateOperation(node, node->getLeft());
113 switch (node->getOp()) {
115 validateIndexing(node);
117 case EOpIndexIndirect:
118 #if defined(__APPLE__)
119 // Loop unrolling is a work-around for a Mac Cg compiler bug where it
120 // crashes when a sampler array's index is also the loop index.
121 // Once Apple fixes this bug, we should remove the code in this CL.
122 // See http://codereview.appspot.com/4331048/.
123 if ((node->getLeft() != NULL) && (node->getRight() != NULL) &&
124 (node->getLeft()->getAsSymbolNode())) {
125 TIntermSymbol* symbol = node->getLeft()->getAsSymbolNode();
126 if (IsSampler(symbol->getBasicType()) && symbol->isArray()) {
127 ValidateLoopIndexExpr validate(mLoopStack);
128 node->getRight()->traverse(&validate);
129 if (validate.usesFloatLoopIndex()) {
130 error(node->getLine(),
131 "sampler array index is float loop index",
137 validateIndexing(node);
144 bool ValidateLimitations::visitUnary(Visit, TIntermUnary* node)
146 // Check if loop index is modified in the loop body.
147 validateOperation(node, node->getOperand());
152 bool ValidateLimitations::visitAggregate(Visit, TIntermAggregate* node)
154 switch (node->getOp()) {
155 case EOpFunctionCall:
156 validateFunctionCall(node);
164 bool ValidateLimitations::visitLoop(Visit, TIntermLoop* node)
166 if (!validateLoopType(node))
170 memset(&info, 0, sizeof(TLoopInfo));
172 if (!validateForLoopHeader(node, &info))
175 TIntermNode* body = node->getBody();
177 mLoopStack.push_back(info);
178 body->traverse(this);
179 mLoopStack.pop_back();
182 // The loop is fully processed - no need to visit children.
186 void ValidateLimitations::error(TSourceLoc loc,
187 const char *reason, const char* token)
189 mSink.prefix(EPrefixError);
191 mSink << "'" << token << "' : " << reason << "\n";
195 bool ValidateLimitations::withinLoopBody() const
197 return !mLoopStack.empty();
200 bool ValidateLimitations::isLoopIndex(const TIntermSymbol* symbol) const
202 return IsLoopIndex(symbol, mLoopStack);
205 bool ValidateLimitations::validateLoopType(TIntermLoop* node) {
206 TLoopType type = node->getType();
207 if (type == ELoopFor)
210 // Reject while and do-while loops.
211 error(node->getLine(),
212 "This type of loop is not allowed",
213 type == ELoopWhile ? "while" : "do");
217 bool ValidateLimitations::validateForLoopHeader(TIntermLoop* node,
220 ASSERT(node->getType() == ELoopFor);
223 // The for statement has the form:
224 // for ( init-declaration ; condition ; expression ) statement
226 if (!validateForLoopInit(node, info))
228 if (!validateForLoopCond(node, info))
230 if (!validateForLoopExpr(node, info))
236 bool ValidateLimitations::validateForLoopInit(TIntermLoop* node,
239 TIntermNode* init = node->getInit();
241 error(node->getLine(), "Missing init declaration", "for");
246 // init-declaration has the form:
247 // type-specifier identifier = constant-expression
249 TIntermAggregate* decl = init->getAsAggregate();
250 if ((decl == NULL) || (decl->getOp() != EOpDeclaration)) {
251 error(init->getLine(), "Invalid init declaration", "for");
254 // To keep things simple do not allow declaration list.
255 TIntermSequence& declSeq = decl->getSequence();
256 if (declSeq.size() != 1) {
257 error(decl->getLine(), "Invalid init declaration", "for");
260 TIntermBinary* declInit = declSeq[0]->getAsBinaryNode();
261 if ((declInit == NULL) || (declInit->getOp() != EOpInitialize)) {
262 error(decl->getLine(), "Invalid init declaration", "for");
265 TIntermSymbol* symbol = declInit->getLeft()->getAsSymbolNode();
266 if (symbol == NULL) {
267 error(declInit->getLine(), "Invalid init declaration", "for");
270 // The loop index has type int or float.
271 TBasicType type = symbol->getBasicType();
272 if ((type != EbtInt) && (type != EbtFloat)) {
273 error(symbol->getLine(),
274 "Invalid type for loop index", getBasicString(type));
277 // The loop index is initialized with constant expression.
278 if (!isConstExpr(declInit->getRight())) {
279 error(declInit->getLine(),
280 "Loop index cannot be initialized with non-constant expression",
281 symbol->getSymbol().c_str());
285 info->index.id = symbol->getId();
289 bool ValidateLimitations::validateForLoopCond(TIntermLoop* node,
292 TIntermNode* cond = node->getCondition();
294 error(node->getLine(), "Missing condition", "for");
298 // condition has the form:
299 // loop_index relational_operator constant_expression
301 TIntermBinary* binOp = cond->getAsBinaryNode();
303 error(node->getLine(), "Invalid condition", "for");
306 // Loop index should be to the left of relational operator.
307 TIntermSymbol* symbol = binOp->getLeft()->getAsSymbolNode();
308 if (symbol == NULL) {
309 error(binOp->getLine(), "Invalid condition", "for");
312 if (symbol->getId() != info->index.id) {
313 error(symbol->getLine(),
314 "Expected loop index", symbol->getSymbol().c_str());
317 // Relational operator is one of: > >= < <= == or !=.
318 switch (binOp->getOp()) {
323 case EOpLessThanEqual:
324 case EOpGreaterThanEqual:
327 error(binOp->getLine(),
328 "Invalid relational operator",
329 getOperatorString(binOp->getOp()));
332 // Loop index must be compared with a constant.
333 if (!isConstExpr(binOp->getRight())) {
334 error(binOp->getLine(),
335 "Loop index cannot be compared with non-constant expression",
336 symbol->getSymbol().c_str());
343 bool ValidateLimitations::validateForLoopExpr(TIntermLoop* node,
346 TIntermNode* expr = node->getExpression();
348 error(node->getLine(), "Missing expression", "for");
352 // for expression has one of the following forms:
355 // loop_index += constant_expression
356 // loop_index -= constant_expression
359 // The last two forms are not specified in the spec, but I am assuming
361 TIntermUnary* unOp = expr->getAsUnaryNode();
362 TIntermBinary* binOp = unOp ? NULL : expr->getAsBinaryNode();
364 TOperator op = EOpNull;
365 TIntermSymbol* symbol = NULL;
368 symbol = unOp->getOperand()->getAsSymbolNode();
369 } else if (binOp != NULL) {
371 symbol = binOp->getLeft()->getAsSymbolNode();
374 // The operand must be loop index.
375 if (symbol == NULL) {
376 error(expr->getLine(), "Invalid expression", "for");
379 if (symbol->getId() != info->index.id) {
380 error(symbol->getLine(),
381 "Expected loop index", symbol->getSymbol().c_str());
385 // The operator is one of: ++ -- += -=.
387 case EOpPostIncrement:
388 case EOpPostDecrement:
389 case EOpPreIncrement:
390 case EOpPreDecrement:
391 ASSERT((unOp != NULL) && (binOp == NULL));
395 ASSERT((unOp == NULL) && (binOp != NULL));
398 error(expr->getLine(), "Invalid operator", getOperatorString(op));
402 // Loop index must be incremented/decremented with a constant.
404 if (!isConstExpr(binOp->getRight())) {
405 error(binOp->getLine(),
406 "Loop index cannot be modified by non-constant expression",
407 symbol->getSymbol().c_str());
415 bool ValidateLimitations::validateFunctionCall(TIntermAggregate* node)
417 ASSERT(node->getOp() == EOpFunctionCall);
419 // If not within loop body, there is nothing to check.
420 if (!withinLoopBody())
423 // List of param indices for which loop indices are used as argument.
424 typedef std::vector<int> ParamIndex;
426 TIntermSequence& params = node->getSequence();
427 for (TIntermSequence::size_type i = 0; i < params.size(); ++i) {
428 TIntermSymbol* symbol = params[i]->getAsSymbolNode();
429 if (symbol && isLoopIndex(symbol))
432 // If none of the loop indices are used as arguments,
433 // there is nothing to check.
438 TSymbolTable& symbolTable = GetGlobalParseContext()->symbolTable;
439 TSymbol* symbol = symbolTable.find(node->getName());
440 ASSERT(symbol && symbol->isFunction());
441 TFunction* function = static_cast<TFunction*>(symbol);
442 for (ParamIndex::const_iterator i = pIndex.begin();
443 i != pIndex.end(); ++i) {
444 const TParameter& param = function->getParam(*i);
445 TQualifier qual = param.type->getQualifier();
446 if ((qual == EvqOut) || (qual == EvqInOut)) {
447 error(params[*i]->getLine(),
448 "Loop index cannot be used as argument to a function out or inout parameter",
449 params[*i]->getAsSymbolNode()->getSymbol().c_str());
457 bool ValidateLimitations::validateOperation(TIntermOperator* node,
458 TIntermNode* operand) {
459 // Check if loop index is modified in the loop body.
460 if (!withinLoopBody() || !node->modifiesState())
463 const TIntermSymbol* symbol = operand->getAsSymbolNode();
464 if (symbol && isLoopIndex(symbol)) {
465 error(node->getLine(),
466 "Loop index cannot be statically assigned to within the body of the loop",
467 symbol->getSymbol().c_str());
472 bool ValidateLimitations::isConstExpr(TIntermNode* node)
474 ASSERT(node != NULL);
475 return node->getAsConstantUnion() != NULL;
478 bool ValidateLimitations::isConstIndexExpr(TIntermNode* node)
480 ASSERT(node != NULL);
482 ValidateConstIndexExpr validate(mLoopStack);
483 node->traverse(&validate);
484 return validate.isValid();
487 bool ValidateLimitations::validateIndexing(TIntermBinary* node)
489 ASSERT((node->getOp() == EOpIndexDirect) ||
490 (node->getOp() == EOpIndexIndirect));
493 TIntermTyped* index = node->getRight();
494 // The index expression must have integral type.
495 if (!index->isScalar() || (index->getBasicType() != EbtInt)) {
496 error(index->getLine(),
497 "Index expression must have integral type",
498 index->getCompleteString().c_str());
501 // The index expession must be a constant-index-expression unless
502 // the operand is a uniform in a vertex shader.
503 TIntermTyped* operand = node->getLeft();
504 bool skip = (mShaderType == SH_VERTEX_SHADER) &&
505 (operand->getQualifier() == EvqUniform);
506 if (!skip && !isConstIndexExpr(index)) {
507 error(index->getLine(), "Index expression must be constant", "[]");