blob: ba885d5ffd5f9ff49f0e4f93abd3cd0e98295f55 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens16004fc2014-06-11 11:29:11 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madillb1a85f42014-08-19 15:23:24 -04007#include "compiler/translator/Intermediate.h"
Geoff Lang17732822013-08-29 13:46:49 -04008#include "compiler/translator/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
Zhenyao Moe40d1e92014-07-16 17:40:36 -070010namespace
11{
12
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013//
14// Two purposes:
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.
20//
21
22//
23// Use this class to carry along data from node to node in
24// the traversal
25//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070026class TOutputTraverser : public TIntermTraverser
27{
28 public:
29 TOutputTraverser(TInfoSinkBase &i)
30 : sink(i) { }
alokp@chromium.org7beea402010-09-15 21:18:34 +000031 TInfoSinkBase& sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
Zhenyao Moe40d1e92014-07-16 17:40:36 -070033 protected:
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 *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042};
43
Zhenyao Moe40d1e92014-07-16 17:40:36 -070044//
45// Helper functions for printing, not part of traversing.
46//
47void OutputTreeText(TInfoSinkBase &sink, TIntermNode *node, const int depth)
48{
49 int i;
50
51 sink.location(node->getLine());
52
53 for (i = 0; i < depth; ++i)
54 sink << " ";
55}
56
57} // namespace anonymous
58
59
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060TString TType::getCompleteString() const
61{
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000062 TStringStream stream;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063
alokp@chromium.org76b82082010-03-24 17:59:39 +000064 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
Olli Etuahof9004132014-10-20 18:26:48 +030065 stream << getQualifierString() << " ";
66 if (precision != EbpUndefined)
67 stream << getPrecisionString() << " ";
alokp@chromium.org76b82082010-03-24 17:59:39 +000068 if (array)
daniel@transgaming.com4167cc92013-01-11 04:11:53 +000069 stream << "array[" << getArraySize() << "] of ";
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000070 if (isMatrix())
71 stream << getCols() << "X" << getRows() << " matrix of ";
72 else if (isVector())
73 stream << getNominalSize() << "-component vector of ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000075 stream << getBasicString();
76 return stream.str();
77}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078
79//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080// The rest of the file are the traversal functions. The last one
81// is the one that starts the traversal.
82//
83// Return true from interior nodes to have the external traversal
84// continue on to children. If you process children yourself,
85// return false.
86//
87
Zhenyao Moe40d1e92014-07-16 17:40:36 -070088void TOutputTraverser::visitSymbol(TIntermSymbol *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089{
Zhenyao Moe40d1e92014-07-16 17:40:36 -070090 OutputTreeText(sink, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091
alokp@chromium.org7beea402010-09-15 21:18:34 +000092 sink << "'" << node->getSymbol() << "' ";
93 sink << "(" << node->getCompleteString() << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094}
95
Zhenyao Moe40d1e92014-07-16 17:40:36 -070096bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097{
alokp@chromium.org7beea402010-09-15 21:18:34 +000098 TInfoSinkBase& out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700100 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700102 switch (node->getOp())
103 {
104 case EOpAssign:
105 out << "move second child to first child";
106 break;
107 case EOpInitialize:
108 out << "initialize first child with second child";
109 break;
110 case EOpAddAssign:
111 out << "add second child into first child";
112 break;
113 case EOpSubAssign:
114 out << "subtract second child into first child";
115 break;
116 case EOpMulAssign:
117 out << "multiply second child into first child";
118 break;
119 case EOpVectorTimesMatrixAssign:
120 out << "matrix mult second child into first child";
121 break;
122 case EOpVectorTimesScalarAssign:
123 out << "vector scale second child into first child";
124 break;
125 case EOpMatrixTimesScalarAssign:
126 out << "matrix scale second child into first child";
127 break;
128 case EOpMatrixTimesMatrixAssign:
129 out << "matrix mult second child into first child";
130 break;
131 case EOpDivAssign:
132 out << "divide second child into first child";
133 break;
134 case EOpIndexDirect:
135 out << "direct index";
136 break;
137 case EOpIndexIndirect:
138 out << "indirect index";
139 break;
140 case EOpIndexDirectStruct:
141 out << "direct index for structure";
142 break;
143 case EOpIndexDirectInterfaceBlock:
144 out << "direct index for interface block";
145 break;
146 case EOpVectorSwizzle:
147 out << "vector swizzle";
148 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700150 case EOpAdd:
151 out << "add";
152 break;
153 case EOpSub:
154 out << "subtract";
155 break;
156 case EOpMul:
157 out << "component-wise multiply";
158 break;
159 case EOpDiv:
160 out << "divide";
161 break;
162 case EOpEqual:
163 out << "Compare Equal";
164 break;
165 case EOpNotEqual:
166 out << "Compare Not Equal";
167 break;
168 case EOpLessThan:
169 out << "Compare Less Than";
170 break;
171 case EOpGreaterThan:
172 out << "Compare Greater Than";
173 break;
174 case EOpLessThanEqual:
175 out << "Compare Less Than or Equal";
176 break;
177 case EOpGreaterThanEqual:
178 out << "Compare Greater Than or Equal";
179 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700181 case EOpVectorTimesScalar:
182 out << "vector-scale";
183 break;
184 case EOpVectorTimesMatrix:
185 out << "vector-times-matrix";
186 break;
187 case EOpMatrixTimesVector:
188 out << "matrix-times-vector";
189 break;
190 case EOpMatrixTimesScalar:
191 out << "matrix-scale";
192 break;
193 case EOpMatrixTimesMatrix:
194 out << "matrix-multiply";
195 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700197 case EOpLogicalOr:
198 out << "logical-or";
199 break;
200 case EOpLogicalXor:
201 out << "logical-xor";
202 break;
203 case EOpLogicalAnd:
204 out << "logical-and";
205 break;
206 default:
207 out << "<unknown op>";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000208 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209
alokp@chromium.org7beea402010-09-15 21:18:34 +0000210 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
alokp@chromium.org7beea402010-09-15 21:18:34 +0000212 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213
Jamie Madill60305f12014-11-18 13:25:26 -0500214 // Special handling for direct indexes. Because constant
215 // unions are not aware they are struct indexes, treat them
216 // here where we have that contextual knowledge.
217 if (node->getOp() == EOpIndexDirectStruct ||
218 node->getOp() == EOpIndexDirectInterfaceBlock)
219 {
220 mDepth++;
221 node->getLeft()->traverse(this);
222 mDepth--;
223
224 TIntermConstantUnion *intermConstantUnion = node->getRight()->getAsConstantUnion();
225 ASSERT(intermConstantUnion);
226
227 OutputTreeText(out, intermConstantUnion, mDepth + 1);
228
229 // The following code finds the field name from the constant union
230 const ConstantUnion *constantUnion = intermConstantUnion->getUnionArrayPointer();
231 const TStructure *structure = node->getLeft()->getType().getStruct();
232 const TInterfaceBlock *interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
233 ASSERT(structure || interfaceBlock);
234
235 const TFieldList &fields = structure ? structure->fields() : interfaceBlock->fields();
236
237 const TField *field = fields[constantUnion->getIConst()];
238
239 out << constantUnion->getIConst() << " (field '" << field->name() << "')";
240
241 return false;
242 }
243
alokp@chromium.org76b82082010-03-24 17:59:39 +0000244 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245}
246
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700247bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000249 TInfoSinkBase& out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700251 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700253 switch (node->getOp())
254 {
255 case EOpNegative: out << "Negate value"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700256 case EOpPositive: out << "Positive sign"; break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700257 case EOpVectorLogicalNot:
258 case EOpLogicalNot: out << "Negate conditional"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700260 case EOpPostIncrement: out << "Post-Increment"; break;
261 case EOpPostDecrement: out << "Post-Decrement"; break;
262 case EOpPreIncrement: out << "Pre-Increment"; break;
263 case EOpPreDecrement: out << "Pre-Decrement"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700265 case EOpRadians: out << "radians"; break;
266 case EOpDegrees: out << "degrees"; break;
267 case EOpSin: out << "sine"; break;
268 case EOpCos: out << "cosine"; break;
269 case EOpTan: out << "tangent"; break;
270 case EOpAsin: out << "arc sine"; break;
271 case EOpAcos: out << "arc cosine"; break;
272 case EOpAtan: out << "arc tangent"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700274 case EOpExp: out << "exp"; break;
275 case EOpLog: out << "log"; break;
276 case EOpExp2: out << "exp2"; break;
277 case EOpLog2: out << "log2"; break;
278 case EOpSqrt: out << "sqrt"; break;
279 case EOpInverseSqrt: out << "inverse sqrt"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700281 case EOpAbs: out << "Absolute value"; break;
282 case EOpSign: out << "Sign"; break;
283 case EOpFloor: out << "Floor"; break;
284 case EOpCeil: out << "Ceiling"; break;
285 case EOpFract: out << "Fraction"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700287 case EOpLength: out << "length"; break;
288 case EOpNormalize: out << "normalize"; break;
289 // case EOpDPdx: out << "dPdx"; break;
290 // case EOpDPdy: out << "dPdy"; break;
291 // case EOpFwidth: out << "fwidth"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700293 case EOpAny: out << "any"; break;
294 case EOpAll: out << "all"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700296 default:
297 out.prefix(EPrefixError);
298 out << "Bad unary op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
alokp@chromium.org7beea402010-09-15 21:18:34 +0000301 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
alokp@chromium.org7beea402010-09-15 21:18:34 +0000303 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
alokp@chromium.org76b82082010-03-24 17:59:39 +0000305 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306}
307
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700308bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700310 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700312 if (node->getOp() == EOpNull)
313 {
Jamie Madill075edd82013-07-08 13:30:19 -0400314 out.prefix(EPrefixError);
315 out << "node is still EOpNull!";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000316 return true;
317 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700321 switch (node->getOp())
322 {
323 case EOpSequence: out << "Sequence\n"; return true;
324 case EOpComma: out << "Comma\n"; return true;
325 case EOpFunction: out << "Function Definition: " << node->getName(); break;
326 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
327 case EOpParameters: out << "Function Parameters: "; break;
Olli Etuaho76acee82014-11-04 13:44:03 +0200328 case EOpPrototype: out << "Function Prototype: " << node->getName(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700330 case EOpConstructFloat: out << "Construct float"; break;
331 case EOpConstructVec2: out << "Construct vec2"; break;
332 case EOpConstructVec3: out << "Construct vec3"; break;
333 case EOpConstructVec4: out << "Construct vec4"; break;
334 case EOpConstructBool: out << "Construct bool"; break;
335 case EOpConstructBVec2: out << "Construct bvec2"; break;
336 case EOpConstructBVec3: out << "Construct bvec3"; break;
337 case EOpConstructBVec4: out << "Construct bvec4"; break;
338 case EOpConstructInt: out << "Construct int"; break;
339 case EOpConstructIVec2: out << "Construct ivec2"; break;
340 case EOpConstructIVec3: out << "Construct ivec3"; break;
341 case EOpConstructIVec4: out << "Construct ivec4"; break;
342 case EOpConstructUInt: out << "Construct uint"; break;
343 case EOpConstructUVec2: out << "Construct uvec2"; break;
344 case EOpConstructUVec3: out << "Construct uvec3"; break;
345 case EOpConstructUVec4: out << "Construct uvec4"; break;
346 case EOpConstructMat2: out << "Construct mat2"; break;
347 case EOpConstructMat3: out << "Construct mat3"; break;
348 case EOpConstructMat4: out << "Construct mat4"; break;
349 case EOpConstructStruct: out << "Construct structure"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700351 case EOpLessThan: out << "Compare Less Than"; break;
352 case EOpGreaterThan: out << "Compare Greater Than"; break;
353 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
354 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
355 case EOpVectorEqual: out << "Equal"; break;
356 case EOpVectorNotEqual: out << "NotEqual"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700358 case EOpMod: out << "mod"; break;
359 case EOpPow: out << "pow"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700361 case EOpAtan: out << "arc tangent"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700363 case EOpMin: out << "min"; break;
364 case EOpMax: out << "max"; break;
365 case EOpClamp: out << "clamp"; break;
366 case EOpMix: out << "mix"; break;
367 case EOpStep: out << "step"; break;
368 case EOpSmoothStep: out << "smoothstep"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700370 case EOpDistance: out << "distance"; break;
371 case EOpDot: out << "dot-product"; break;
372 case EOpCross: out << "cross-product"; break;
373 case EOpFaceForward: out << "face-forward"; break;
374 case EOpReflect: out << "reflect"; break;
375 case EOpRefract: out << "refract"; break;
376 case EOpMul: out << "component-wise multiply"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700378 case EOpDeclaration: out << "Declaration: "; break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400379 case EOpInvariantDeclaration: out << "Invariant Declaration: "; break;
daniel@transgaming.coma9ae4aa2011-10-19 13:29:27 +0000380
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700381 default:
382 out.prefix(EPrefixError);
383 out << "Bad aggregation op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
alokp@chromium.org76b82082010-03-24 17:59:39 +0000386 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000387 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
alokp@chromium.org7beea402010-09-15 21:18:34 +0000389 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
alokp@chromium.org76b82082010-03-24 17:59:39 +0000391 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700394bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700396 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700398 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
alokp@chromium.org7beea402010-09-15 21:18:34 +0000400 out << "Test condition and select";
401 out << " (" << node->getCompleteString() << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700403 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700405 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000406 out << "Condition\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000407 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700409 OutputTreeText(sink, node, mDepth);
410 if (node->getTrueBlock())
411 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000412 out << "true case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000413 node->getTrueBlock()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700414 }
415 else
416 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000417 out << "true case is null\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700420 if (node->getFalseBlock())
421 {
422 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000423 out << "false case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000424 node->getFalseBlock()->traverse(this);
425 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700427 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428
alokp@chromium.org76b82082010-03-24 17:59:39 +0000429 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430}
431
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700432void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700434 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
Jamie Madill94bf7f22013-07-08 13:31:15 -0400436 size_t size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700438 for (size_t i = 0; i < size; i++)
439 {
440 OutputTreeText(out, node, mDepth);
441 switch (node->getUnionArrayPointer()[i].getType())
442 {
443 case EbtBool:
444 if (node->getUnionArrayPointer()[i].getBConst())
445 out << "true";
446 else
447 out << "false";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700449 out << " (" << "const bool" << ")";
450 out << "\n";
451 break;
452 case EbtFloat:
453 out << node->getUnionArrayPointer()[i].getFConst();
454 out << " (const float)\n";
455 break;
456 case EbtInt:
457 out << node->getUnionArrayPointer()[i].getIConst();
458 out << " (const int)\n";
459 break;
460 case EbtUInt:
461 out << node->getUnionArrayPointer()[i].getUConst();
462 out << " (const uint)\n";
463 break;
464 default:
465 out.message(EPrefixInternalError, node->getLine(), "Unknown constant");
466 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000467 }
468 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469}
470
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700471bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700473 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700475 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476
alokp@chromium.org7beea402010-09-15 21:18:34 +0000477 out << "Loop with condition ";
alokp@chromium.org52813552010-11-16 18:36:09 +0000478 if (node->getType() == ELoopDoWhile)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000479 out << "not ";
480 out << "tested first\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700482 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700484 OutputTreeText(sink, node, mDepth);
485 if (node->getCondition())
486 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000487 out << "Loop Condition\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000488 node->getCondition()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700489 }
490 else
491 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000492 out << "No loop condition\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700493 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700495 OutputTreeText(sink, node, mDepth);
496 if (node->getBody())
497 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000498 out << "Loop Body\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000499 node->getBody()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700500 }
501 else
502 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000503 out << "No loop body\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700506 if (node->getExpression())
507 {
508 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000509 out << "Loop Terminal Expression\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000510 node->getExpression()->traverse(this);
alokp@chromium.org76b82082010-03-24 17:59:39 +0000511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700513 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514
alokp@chromium.org76b82082010-03-24 17:59:39 +0000515 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516}
517
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700518bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700520 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700522 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700524 switch (node->getFlowOp())
525 {
526 case EOpKill: out << "Branch: Kill"; break;
527 case EOpBreak: out << "Branch: Break"; break;
528 case EOpContinue: out << "Branch: Continue"; break;
529 case EOpReturn: out << "Branch: Return"; break;
530 default: out << "Branch: Unknown Branch"; break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700533 if (node->getExpression())
534 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000535 out << " with expression\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700536 ++mDepth;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000537 node->getExpression()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700538 --mDepth;
539 }
540 else
541 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000542 out << "\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
alokp@chromium.org76b82082010-03-24 17:59:39 +0000545 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546}
547
548//
549// This function is the one to call externally to start the traversal.
550// Individual functions can be initialized to 0 to skip processing of that
551// type of node. It's children will still be processed.
552//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700553void TIntermediate::outputTree(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700555 if (root == NULL)
alokp@chromium.org76b82082010-03-24 17:59:39 +0000556 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700558 TOutputTraverser it(mInfoSink.info);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559
alokp@chromium.org76b82082010-03-24 17:59:39 +0000560 root->traverse(&it);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561}