2 // Copyright (c) 2002-2013 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 "localintermediate.h"
8 #include "SymbolTable.h"
12 // 1. Show an example of how to iterate tree. Functions can
13 // also directly call Traverse() on children themselves to
14 // have finer grained control over the process than shown here.
15 // See the last function for how to get started.
16 // 2. Print out a text based description of the tree.
20 // Use this class to carry along data from node to node in
23 class TOutputTraverser : public TIntermTraverser {
25 TOutputTraverser(TInfoSinkBase& i) : sink(i) { }
29 void visitSymbol(TIntermSymbol*);
30 void visitConstantUnion(TIntermConstantUnion*);
31 bool visitBinary(Visit visit, TIntermBinary*);
32 bool visitUnary(Visit visit, TIntermUnary*);
33 bool visitSelection(Visit visit, TIntermSelection*);
34 bool visitAggregate(Visit visit, TIntermAggregate*);
35 bool visitLoop(Visit visit, TIntermLoop*);
36 bool visitBranch(Visit visit, TIntermBranch*);
39 TString TType::getCompleteString() const
43 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
44 stream << getQualifierString() << " " << getPrecisionString() << " ";
46 stream << "array of ";
48 stream << size << "X" << size << " matrix of ";
50 stream << size << "-component vector of ";
52 stream << getBasicString();
57 // Helper functions for printing, not part of traversing.
60 void OutputTreeText(TInfoSinkBase& sink, TIntermNode* node, const int depth)
64 sink.location(node->getLine());
66 for (i = 0; i < depth; ++i)
71 // The rest of the file are the traversal functions. The last one
72 // is the one that starts the traversal.
74 // Return true from interior nodes to have the external traversal
75 // continue on to children. If you process children yourself,
79 void TOutputTraverser::visitSymbol(TIntermSymbol* node)
81 OutputTreeText(sink, node, depth);
83 sink << "'" << node->getSymbol() << "' ";
84 sink << "(" << node->getCompleteString() << ")\n";
87 bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary* node)
89 TInfoSinkBase& out = sink;
91 OutputTreeText(out, node, depth);
93 switch (node->getOp()) {
94 case EOpAssign: out << "move second child to first child"; break;
95 case EOpInitialize: out << "initialize first child with second child"; break;
96 case EOpAddAssign: out << "add second child into first child"; break;
97 case EOpSubAssign: out << "subtract second child into first child"; break;
98 case EOpMulAssign: out << "multiply second child into first child"; break;
99 case EOpVectorTimesMatrixAssign: out << "matrix mult second child into first child"; break;
100 case EOpVectorTimesScalarAssign: out << "vector scale second child into first child"; break;
101 case EOpMatrixTimesScalarAssign: out << "matrix scale second child into first child"; break;
102 case EOpMatrixTimesMatrixAssign: out << "matrix mult second child into first child"; break;
103 case EOpDivAssign: out << "divide second child into first child"; break;
104 case EOpIndexDirect: out << "direct index"; break;
105 case EOpIndexIndirect: out << "indirect index"; break;
106 case EOpIndexDirectStruct: out << "direct index for structure"; break;
107 case EOpVectorSwizzle: out << "vector swizzle"; break;
109 case EOpAdd: out << "add"; break;
110 case EOpSub: out << "subtract"; break;
111 case EOpMul: out << "component-wise multiply"; break;
112 case EOpDiv: out << "divide"; break;
113 case EOpEqual: out << "Compare Equal"; break;
114 case EOpNotEqual: out << "Compare Not Equal"; break;
115 case EOpLessThan: out << "Compare Less Than"; break;
116 case EOpGreaterThan: out << "Compare Greater Than"; break;
117 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
118 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
120 case EOpVectorTimesScalar: out << "vector-scale"; break;
121 case EOpVectorTimesMatrix: out << "vector-times-matrix"; break;
122 case EOpMatrixTimesVector: out << "matrix-times-vector"; break;
123 case EOpMatrixTimesScalar: out << "matrix-scale"; break;
124 case EOpMatrixTimesMatrix: out << "matrix-multiply"; break;
126 case EOpLogicalOr: out << "logical-or"; break;
127 case EOpLogicalXor: out << "logical-xor"; break;
128 case EOpLogicalAnd: out << "logical-and"; break;
129 default: out << "<unknown op>";
132 out << " (" << node->getCompleteString() << ")";
139 bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary* node)
141 TInfoSinkBase& out = sink;
143 OutputTreeText(out, node, depth);
145 switch (node->getOp()) {
146 case EOpNegative: out << "Negate value"; break;
147 case EOpVectorLogicalNot:
148 case EOpLogicalNot: out << "Negate conditional"; break;
150 case EOpPostIncrement: out << "Post-Increment"; break;
151 case EOpPostDecrement: out << "Post-Decrement"; break;
152 case EOpPreIncrement: out << "Pre-Increment"; break;
153 case EOpPreDecrement: out << "Pre-Decrement"; break;
155 case EOpRadians: out << "radians"; break;
156 case EOpDegrees: out << "degrees"; break;
157 case EOpSin: out << "sine"; break;
158 case EOpCos: out << "cosine"; break;
159 case EOpTan: out << "tangent"; break;
160 case EOpAsin: out << "arc sine"; break;
161 case EOpAcos: out << "arc cosine"; break;
162 case EOpAtan: out << "arc tangent"; break;
164 case EOpExp: out << "exp"; break;
165 case EOpLog: out << "log"; break;
166 case EOpExp2: out << "exp2"; break;
167 case EOpLog2: out << "log2"; break;
168 case EOpSqrt: out << "sqrt"; break;
169 case EOpInverseSqrt: out << "inverse sqrt"; break;
171 case EOpAbs: out << "Absolute value"; break;
172 case EOpSign: out << "Sign"; break;
173 case EOpFloor: out << "Floor"; break;
174 case EOpCeil: out << "Ceiling"; break;
175 case EOpFract: out << "Fraction"; break;
177 case EOpLength: out << "length"; break;
178 case EOpNormalize: out << "normalize"; break;
179 // case EOpDPdx: out << "dPdx"; break;
180 // case EOpDPdy: out << "dPdy"; break;
181 // case EOpFwidth: out << "fwidth"; break;
183 case EOpAny: out << "any"; break;
184 case EOpAll: out << "all"; break;
186 default: out.message(EPrefixError, "Bad unary op");
189 out << " (" << node->getCompleteString() << ")";
196 bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
198 TInfoSinkBase& out = sink;
200 if (node->getOp() == EOpNull) {
201 out.message(EPrefixError, "node is still EOpNull!");
205 OutputTreeText(out, node, depth);
207 switch (node->getOp()) {
208 case EOpSequence: out << "Sequence\n"; return true;
209 case EOpComma: out << "Comma\n"; return true;
210 case EOpFunction: out << "Function Definition: " << node->getName(); break;
211 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
212 case EOpParameters: out << "Function Parameters: "; break;
214 case EOpConstructFloat: out << "Construct float"; break;
215 case EOpConstructVec2: out << "Construct vec2"; break;
216 case EOpConstructVec3: out << "Construct vec3"; break;
217 case EOpConstructVec4: out << "Construct vec4"; break;
218 case EOpConstructBool: out << "Construct bool"; break;
219 case EOpConstructBVec2: out << "Construct bvec2"; break;
220 case EOpConstructBVec3: out << "Construct bvec3"; break;
221 case EOpConstructBVec4: out << "Construct bvec4"; break;
222 case EOpConstructInt: out << "Construct int"; break;
223 case EOpConstructIVec2: out << "Construct ivec2"; break;
224 case EOpConstructIVec3: out << "Construct ivec3"; break;
225 case EOpConstructIVec4: out << "Construct ivec4"; break;
226 case EOpConstructMat2: out << "Construct mat2"; break;
227 case EOpConstructMat3: out << "Construct mat3"; break;
228 case EOpConstructMat4: out << "Construct mat4"; break;
229 case EOpConstructStruct: out << "Construct structure"; break;
231 case EOpLessThan: out << "Compare Less Than"; break;
232 case EOpGreaterThan: out << "Compare Greater Than"; break;
233 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
234 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
235 case EOpVectorEqual: out << "Equal"; break;
236 case EOpVectorNotEqual: out << "NotEqual"; break;
238 case EOpMod: out << "mod"; break;
239 case EOpPow: out << "pow"; break;
241 case EOpAtan: out << "arc tangent"; break;
243 case EOpMin: out << "min"; break;
244 case EOpMax: out << "max"; break;
245 case EOpClamp: out << "clamp"; break;
246 case EOpMix: out << "mix"; break;
247 case EOpStep: out << "step"; break;
248 case EOpSmoothStep: out << "smoothstep"; break;
250 case EOpDistance: out << "distance"; break;
251 case EOpDot: out << "dot-product"; break;
252 case EOpCross: out << "cross-product"; break;
253 case EOpFaceForward: out << "face-forward"; break;
254 case EOpReflect: out << "reflect"; break;
255 case EOpRefract: out << "refract"; break;
256 case EOpMul: out << "component-wise multiply"; break;
258 default: out.message(EPrefixError, "Bad aggregation op");
261 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
262 out << " (" << node->getCompleteString() << ")";
269 bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node)
271 TInfoSinkBase& out = sink;
273 OutputTreeText(out, node, depth);
275 out << "Test condition and select";
276 out << " (" << node->getCompleteString() << ")\n";
280 OutputTreeText(sink, node, depth);
281 out << "Condition\n";
282 node->getCondition()->traverse(this);
284 OutputTreeText(sink, node, depth);
285 if (node->getTrueBlock()) {
286 out << "true case\n";
287 node->getTrueBlock()->traverse(this);
289 out << "true case is null\n";
291 if (node->getFalseBlock()) {
292 OutputTreeText(sink, node, depth);
293 out << "false case\n";
294 node->getFalseBlock()->traverse(this);
302 void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
304 TInfoSinkBase& out = sink;
306 int size = node->getType().getObjectSize();
308 for (int i = 0; i < size; i++) {
309 OutputTreeText(out, node, depth);
310 switch (node->getUnionArrayPointer()[i].getType()) {
312 if (node->getUnionArrayPointer()[i].getBConst())
317 out << " (" << "const bool" << ")";
321 out << node->getUnionArrayPointer()[i].getFConst();
322 out << " (const float)\n";
325 out << node->getUnionArrayPointer()[i].getIConst();
326 out << " (const int)\n";
329 out.message(EPrefixInternalError, "Unknown constant", node->getLine());
335 bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node)
337 TInfoSinkBase& out = sink;
339 OutputTreeText(out, node, depth);
341 out << "Loop with condition ";
342 if (node->getType() == ELoopDoWhile)
344 out << "tested first\n";
348 OutputTreeText(sink, node, depth);
349 if (node->getCondition()) {
350 out << "Loop Condition\n";
351 node->getCondition()->traverse(this);
353 out << "No loop condition\n";
355 OutputTreeText(sink, node, depth);
356 if (node->getBody()) {
357 out << "Loop Body\n";
358 node->getBody()->traverse(this);
360 out << "No loop body\n";
362 if (node->getExpression()) {
363 OutputTreeText(sink, node, depth);
364 out << "Loop Terminal Expression\n";
365 node->getExpression()->traverse(this);
373 bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node)
375 TInfoSinkBase& out = sink;
377 OutputTreeText(out, node, depth);
379 switch (node->getFlowOp()) {
380 case EOpKill: out << "Branch: Kill"; break;
381 case EOpBreak: out << "Branch: Break"; break;
382 case EOpContinue: out << "Branch: Continue"; break;
383 case EOpReturn: out << "Branch: Return"; break;
384 default: out << "Branch: Unknown Branch"; break;
387 if (node->getExpression()) {
388 out << " with expression\n";
390 node->getExpression()->traverse(this);
399 // This function is the one to call externally to start the traversal.
400 // Individual functions can be initialized to 0 to skip processing of that
401 // type of node. It's children will still be processed.
403 void TIntermediate::outputTree(TIntermNode* root)
408 TOutputTraverser it(infoSink.info);