2 // Copyright (c) 2002-2014 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 "compiler/translator/Intermediate.h"
8 #include "compiler/translator/SymbolTable.h"
15 // 1. Show an example of how to iterate tree. Functions can
16 // also directly call Traverse() on children themselves to
17 // have finer grained control over the process than shown here.
18 // See the last function for how to get started.
19 // 2. Print out a text based description of the tree.
23 // Use this class to carry along data from node to node in
26 class TOutputTraverser : public TIntermTraverser
29 TOutputTraverser(TInfoSinkBase &i)
34 void visitSymbol(TIntermSymbol *);
35 void visitConstantUnion(TIntermConstantUnion *);
36 bool visitBinary(Visit visit, TIntermBinary *);
37 bool visitUnary(Visit visit, TIntermUnary *);
38 bool visitSelection(Visit visit, TIntermSelection *);
39 bool visitAggregate(Visit visit, TIntermAggregate *);
40 bool visitLoop(Visit visit, TIntermLoop *);
41 bool visitBranch(Visit visit, TIntermBranch *);
45 // Helper functions for printing, not part of traversing.
47 void OutputTreeText(TInfoSinkBase &sink, TIntermNode *node, const int depth)
51 sink.location(node->getLine());
53 for (i = 0; i < depth; ++i)
57 } // namespace anonymous
60 TString TType::getCompleteString() const
64 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
65 stream << getQualifierString() << " " << getPrecisionString() << " ";
67 stream << "array[" << getArraySize() << "] of ";
69 stream << getCols() << "X" << getRows() << " matrix of ";
71 stream << getNominalSize() << "-component vector of ";
73 stream << getBasicString();
78 // The rest of the file are the traversal functions. The last one
79 // is the one that starts the traversal.
81 // Return true from interior nodes to have the external traversal
82 // continue on to children. If you process children yourself,
86 void TOutputTraverser::visitSymbol(TIntermSymbol *node)
88 OutputTreeText(sink, node, mDepth);
90 sink << "'" << node->getSymbol() << "' ";
91 sink << "(" << node->getCompleteString() << ")\n";
94 bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary *node)
96 TInfoSinkBase& out = sink;
98 OutputTreeText(out, node, mDepth);
100 switch (node->getOp())
103 out << "move second child to first child";
106 out << "initialize first child with second child";
109 out << "add second child into first child";
112 out << "subtract second child into first child";
115 out << "multiply second child into first child";
117 case EOpVectorTimesMatrixAssign:
118 out << "matrix mult second child into first child";
120 case EOpVectorTimesScalarAssign:
121 out << "vector scale second child into first child";
123 case EOpMatrixTimesScalarAssign:
124 out << "matrix scale second child into first child";
126 case EOpMatrixTimesMatrixAssign:
127 out << "matrix mult second child into first child";
130 out << "divide second child into first child";
133 out << "direct index";
135 case EOpIndexIndirect:
136 out << "indirect index";
138 case EOpIndexDirectStruct:
139 out << "direct index for structure";
141 case EOpIndexDirectInterfaceBlock:
142 out << "direct index for interface block";
144 case EOpVectorSwizzle:
145 out << "vector swizzle";
155 out << "component-wise multiply";
161 out << "Compare Equal";
164 out << "Compare Not Equal";
167 out << "Compare Less Than";
170 out << "Compare Greater Than";
172 case EOpLessThanEqual:
173 out << "Compare Less Than or Equal";
175 case EOpGreaterThanEqual:
176 out << "Compare Greater Than or Equal";
179 case EOpVectorTimesScalar:
180 out << "vector-scale";
182 case EOpVectorTimesMatrix:
183 out << "vector-times-matrix";
185 case EOpMatrixTimesVector:
186 out << "matrix-times-vector";
188 case EOpMatrixTimesScalar:
189 out << "matrix-scale";
191 case EOpMatrixTimesMatrix:
192 out << "matrix-multiply";
199 out << "logical-xor";
202 out << "logical-and";
205 out << "<unknown op>";
208 out << " (" << node->getCompleteString() << ")";
215 bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary *node)
217 TInfoSinkBase& out = sink;
219 OutputTreeText(out, node, mDepth);
221 switch (node->getOp())
223 case EOpNegative: out << "Negate value"; break;
224 case EOpVectorLogicalNot:
225 case EOpLogicalNot: out << "Negate conditional"; break;
227 case EOpPostIncrement: out << "Post-Increment"; break;
228 case EOpPostDecrement: out << "Post-Decrement"; break;
229 case EOpPreIncrement: out << "Pre-Increment"; break;
230 case EOpPreDecrement: out << "Pre-Decrement"; break;
232 case EOpRadians: out << "radians"; break;
233 case EOpDegrees: out << "degrees"; break;
234 case EOpSin: out << "sine"; break;
235 case EOpCos: out << "cosine"; break;
236 case EOpTan: out << "tangent"; break;
237 case EOpAsin: out << "arc sine"; break;
238 case EOpAcos: out << "arc cosine"; break;
239 case EOpAtan: out << "arc tangent"; break;
241 case EOpExp: out << "exp"; break;
242 case EOpLog: out << "log"; break;
243 case EOpExp2: out << "exp2"; break;
244 case EOpLog2: out << "log2"; break;
245 case EOpSqrt: out << "sqrt"; break;
246 case EOpInverseSqrt: out << "inverse sqrt"; break;
248 case EOpAbs: out << "Absolute value"; break;
249 case EOpSign: out << "Sign"; break;
250 case EOpFloor: out << "Floor"; break;
251 case EOpCeil: out << "Ceiling"; break;
252 case EOpFract: out << "Fraction"; break;
254 case EOpLength: out << "length"; break;
255 case EOpNormalize: out << "normalize"; break;
256 // case EOpDPdx: out << "dPdx"; break;
257 // case EOpDPdy: out << "dPdy"; break;
258 // case EOpFwidth: out << "fwidth"; break;
260 case EOpAny: out << "any"; break;
261 case EOpAll: out << "all"; break;
264 out.prefix(EPrefixError);
265 out << "Bad unary op";
268 out << " (" << node->getCompleteString() << ")";
275 bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
277 TInfoSinkBase &out = sink;
279 if (node->getOp() == EOpNull)
281 out.prefix(EPrefixError);
282 out << "node is still EOpNull!";
286 OutputTreeText(out, node, mDepth);
288 switch (node->getOp())
290 case EOpSequence: out << "Sequence\n"; return true;
291 case EOpComma: out << "Comma\n"; return true;
292 case EOpFunction: out << "Function Definition: " << node->getName(); break;
293 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
294 case EOpParameters: out << "Function Parameters: "; break;
296 case EOpConstructFloat: out << "Construct float"; break;
297 case EOpConstructVec2: out << "Construct vec2"; break;
298 case EOpConstructVec3: out << "Construct vec3"; break;
299 case EOpConstructVec4: out << "Construct vec4"; break;
300 case EOpConstructBool: out << "Construct bool"; break;
301 case EOpConstructBVec2: out << "Construct bvec2"; break;
302 case EOpConstructBVec3: out << "Construct bvec3"; break;
303 case EOpConstructBVec4: out << "Construct bvec4"; break;
304 case EOpConstructInt: out << "Construct int"; break;
305 case EOpConstructIVec2: out << "Construct ivec2"; break;
306 case EOpConstructIVec3: out << "Construct ivec3"; break;
307 case EOpConstructIVec4: out << "Construct ivec4"; break;
308 case EOpConstructUInt: out << "Construct uint"; break;
309 case EOpConstructUVec2: out << "Construct uvec2"; break;
310 case EOpConstructUVec3: out << "Construct uvec3"; break;
311 case EOpConstructUVec4: out << "Construct uvec4"; break;
312 case EOpConstructMat2: out << "Construct mat2"; break;
313 case EOpConstructMat3: out << "Construct mat3"; break;
314 case EOpConstructMat4: out << "Construct mat4"; break;
315 case EOpConstructStruct: out << "Construct structure"; break;
317 case EOpLessThan: out << "Compare Less Than"; break;
318 case EOpGreaterThan: out << "Compare Greater Than"; break;
319 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
320 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
321 case EOpVectorEqual: out << "Equal"; break;
322 case EOpVectorNotEqual: out << "NotEqual"; break;
324 case EOpMod: out << "mod"; break;
325 case EOpPow: out << "pow"; break;
327 case EOpAtan: out << "arc tangent"; break;
329 case EOpMin: out << "min"; break;
330 case EOpMax: out << "max"; break;
331 case EOpClamp: out << "clamp"; break;
332 case EOpMix: out << "mix"; break;
333 case EOpStep: out << "step"; break;
334 case EOpSmoothStep: out << "smoothstep"; break;
336 case EOpDistance: out << "distance"; break;
337 case EOpDot: out << "dot-product"; break;
338 case EOpCross: out << "cross-product"; break;
339 case EOpFaceForward: out << "face-forward"; break;
340 case EOpReflect: out << "reflect"; break;
341 case EOpRefract: out << "refract"; break;
342 case EOpMul: out << "component-wise multiply"; break;
344 case EOpDeclaration: out << "Declaration: "; break;
345 case EOpInvariantDeclaration: out << "Invariant Declaration: "; break;
348 out.prefix(EPrefixError);
349 out << "Bad aggregation op";
352 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
353 out << " (" << node->getCompleteString() << ")";
360 bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection *node)
362 TInfoSinkBase &out = sink;
364 OutputTreeText(out, node, mDepth);
366 out << "Test condition and select";
367 out << " (" << node->getCompleteString() << ")\n";
371 OutputTreeText(sink, node, mDepth);
372 out << "Condition\n";
373 node->getCondition()->traverse(this);
375 OutputTreeText(sink, node, mDepth);
376 if (node->getTrueBlock())
378 out << "true case\n";
379 node->getTrueBlock()->traverse(this);
383 out << "true case is null\n";
386 if (node->getFalseBlock())
388 OutputTreeText(sink, node, mDepth);
389 out << "false case\n";
390 node->getFalseBlock()->traverse(this);
398 void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
400 TInfoSinkBase &out = sink;
402 size_t size = node->getType().getObjectSize();
404 for (size_t i = 0; i < size; i++)
406 OutputTreeText(out, node, mDepth);
407 switch (node->getUnionArrayPointer()[i].getType())
410 if (node->getUnionArrayPointer()[i].getBConst())
415 out << " (" << "const bool" << ")";
419 out << node->getUnionArrayPointer()[i].getFConst();
420 out << " (const float)\n";
423 out << node->getUnionArrayPointer()[i].getIConst();
424 out << " (const int)\n";
427 out << node->getUnionArrayPointer()[i].getUConst();
428 out << " (const uint)\n";
431 out.message(EPrefixInternalError, node->getLine(), "Unknown constant");
437 bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
439 TInfoSinkBase &out = sink;
441 OutputTreeText(out, node, mDepth);
443 out << "Loop with condition ";
444 if (node->getType() == ELoopDoWhile)
446 out << "tested first\n";
450 OutputTreeText(sink, node, mDepth);
451 if (node->getCondition())
453 out << "Loop Condition\n";
454 node->getCondition()->traverse(this);
458 out << "No loop condition\n";
461 OutputTreeText(sink, node, mDepth);
464 out << "Loop Body\n";
465 node->getBody()->traverse(this);
469 out << "No loop body\n";
472 if (node->getExpression())
474 OutputTreeText(sink, node, mDepth);
475 out << "Loop Terminal Expression\n";
476 node->getExpression()->traverse(this);
484 bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
486 TInfoSinkBase &out = sink;
488 OutputTreeText(out, node, mDepth);
490 switch (node->getFlowOp())
492 case EOpKill: out << "Branch: Kill"; break;
493 case EOpBreak: out << "Branch: Break"; break;
494 case EOpContinue: out << "Branch: Continue"; break;
495 case EOpReturn: out << "Branch: Return"; break;
496 default: out << "Branch: Unknown Branch"; break;
499 if (node->getExpression())
501 out << " with expression\n";
503 node->getExpression()->traverse(this);
515 // This function is the one to call externally to start the traversal.
516 // Individual functions can be initialized to 0 to skip processing of that
517 // type of node. It's children will still be processed.
519 void TIntermediate::outputTree(TIntermNode *root)
524 TOutputTraverser it(mInfoSink.info);