blob: 41c11cf9aabbb8bfb0216700ddfff06c83d49a7b [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;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000134 case EOpModAssign:
135 out << "modulo second child into first child";
136 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700137 case EOpIndexDirect:
138 out << "direct index";
139 break;
140 case EOpIndexIndirect:
141 out << "indirect index";
142 break;
143 case EOpIndexDirectStruct:
144 out << "direct index for structure";
145 break;
146 case EOpIndexDirectInterfaceBlock:
147 out << "direct index for interface block";
148 break;
149 case EOpVectorSwizzle:
150 out << "vector swizzle";
151 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700153 case EOpAdd:
154 out << "add";
155 break;
156 case EOpSub:
157 out << "subtract";
158 break;
159 case EOpMul:
160 out << "component-wise multiply";
161 break;
162 case EOpDiv:
163 out << "divide";
164 break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +0000165 case EOpMod:
166 out << "modulo";
167 break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700168 case EOpEqual:
169 out << "Compare Equal";
170 break;
171 case EOpNotEqual:
172 out << "Compare Not Equal";
173 break;
174 case EOpLessThan:
175 out << "Compare Less Than";
176 break;
177 case EOpGreaterThan:
178 out << "Compare Greater Than";
179 break;
180 case EOpLessThanEqual:
181 out << "Compare Less Than or Equal";
182 break;
183 case EOpGreaterThanEqual:
184 out << "Compare Greater Than or Equal";
185 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700187 case EOpVectorTimesScalar:
188 out << "vector-scale";
189 break;
190 case EOpVectorTimesMatrix:
191 out << "vector-times-matrix";
192 break;
193 case EOpMatrixTimesVector:
194 out << "matrix-times-vector";
195 break;
196 case EOpMatrixTimesScalar:
197 out << "matrix-scale";
198 break;
199 case EOpMatrixTimesMatrix:
200 out << "matrix-multiply";
201 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700203 case EOpLogicalOr:
204 out << "logical-or";
205 break;
206 case EOpLogicalXor:
207 out << "logical-xor";
208 break;
209 case EOpLogicalAnd:
210 out << "logical-and";
211 break;
212 default:
213 out << "<unknown op>";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000214 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215
alokp@chromium.org7beea402010-09-15 21:18:34 +0000216 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217
alokp@chromium.org7beea402010-09-15 21:18:34 +0000218 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219
Jamie Madill60305f12014-11-18 13:25:26 -0500220 // Special handling for direct indexes. Because constant
221 // unions are not aware they are struct indexes, treat them
222 // here where we have that contextual knowledge.
223 if (node->getOp() == EOpIndexDirectStruct ||
224 node->getOp() == EOpIndexDirectInterfaceBlock)
225 {
226 mDepth++;
227 node->getLeft()->traverse(this);
228 mDepth--;
229
230 TIntermConstantUnion *intermConstantUnion = node->getRight()->getAsConstantUnion();
231 ASSERT(intermConstantUnion);
232
233 OutputTreeText(out, intermConstantUnion, mDepth + 1);
234
235 // The following code finds the field name from the constant union
236 const ConstantUnion *constantUnion = intermConstantUnion->getUnionArrayPointer();
237 const TStructure *structure = node->getLeft()->getType().getStruct();
238 const TInterfaceBlock *interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
239 ASSERT(structure || interfaceBlock);
240
241 const TFieldList &fields = structure ? structure->fields() : interfaceBlock->fields();
242
243 const TField *field = fields[constantUnion->getIConst()];
244
245 out << constantUnion->getIConst() << " (field '" << field->name() << "')";
246
247 return false;
248 }
249
alokp@chromium.org76b82082010-03-24 17:59:39 +0000250 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251}
252
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700253bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254{
alokp@chromium.org7beea402010-09-15 21:18:34 +0000255 TInfoSinkBase& out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700257 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700259 switch (node->getOp())
260 {
261 case EOpNegative: out << "Negate value"; break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700262 case EOpPositive: out << "Positive sign"; break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700263 case EOpVectorLogicalNot:
264 case EOpLogicalNot: out << "Negate conditional"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700266 case EOpPostIncrement: out << "Post-Increment"; break;
267 case EOpPostDecrement: out << "Post-Decrement"; break;
268 case EOpPreIncrement: out << "Pre-Increment"; break;
269 case EOpPreDecrement: out << "Pre-Decrement"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700271 case EOpRadians: out << "radians"; break;
272 case EOpDegrees: out << "degrees"; break;
273 case EOpSin: out << "sine"; break;
274 case EOpCos: out << "cosine"; break;
275 case EOpTan: out << "tangent"; break;
276 case EOpAsin: out << "arc sine"; break;
277 case EOpAcos: out << "arc cosine"; break;
278 case EOpAtan: out << "arc tangent"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700280 case EOpExp: out << "exp"; break;
281 case EOpLog: out << "log"; break;
282 case EOpExp2: out << "exp2"; break;
283 case EOpLog2: out << "log2"; break;
284 case EOpSqrt: out << "sqrt"; break;
285 case EOpInverseSqrt: out << "inverse sqrt"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700287 case EOpAbs: out << "Absolute value"; break;
288 case EOpSign: out << "Sign"; break;
289 case EOpFloor: out << "Floor"; break;
290 case EOpCeil: out << "Ceiling"; break;
291 case EOpFract: out << "Fraction"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700293 case EOpLength: out << "length"; break;
294 case EOpNormalize: out << "normalize"; break;
295 // case EOpDPdx: out << "dPdx"; break;
296 // case EOpDPdy: out << "dPdy"; break;
297 // case EOpFwidth: out << "fwidth"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700299 case EOpAny: out << "any"; break;
300 case EOpAll: out << "all"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700302 default:
303 out.prefix(EPrefixError);
304 out << "Bad unary op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
alokp@chromium.org7beea402010-09-15 21:18:34 +0000307 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
alokp@chromium.org7beea402010-09-15 21:18:34 +0000309 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
alokp@chromium.org76b82082010-03-24 17:59:39 +0000311 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312}
313
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700314bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700316 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700318 if (node->getOp() == EOpNull)
319 {
Jamie Madill075edd82013-07-08 13:30:19 -0400320 out.prefix(EPrefixError);
321 out << "node is still EOpNull!";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000322 return true;
323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700325 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700327 switch (node->getOp())
328 {
329 case EOpSequence: out << "Sequence\n"; return true;
330 case EOpComma: out << "Comma\n"; return true;
331 case EOpFunction: out << "Function Definition: " << node->getName(); break;
332 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
333 case EOpParameters: out << "Function Parameters: "; break;
Olli Etuaho76acee82014-11-04 13:44:03 +0200334 case EOpPrototype: out << "Function Prototype: " << node->getName(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700336 case EOpConstructFloat: out << "Construct float"; break;
337 case EOpConstructVec2: out << "Construct vec2"; break;
338 case EOpConstructVec3: out << "Construct vec3"; break;
339 case EOpConstructVec4: out << "Construct vec4"; break;
340 case EOpConstructBool: out << "Construct bool"; break;
341 case EOpConstructBVec2: out << "Construct bvec2"; break;
342 case EOpConstructBVec3: out << "Construct bvec3"; break;
343 case EOpConstructBVec4: out << "Construct bvec4"; break;
344 case EOpConstructInt: out << "Construct int"; break;
345 case EOpConstructIVec2: out << "Construct ivec2"; break;
346 case EOpConstructIVec3: out << "Construct ivec3"; break;
347 case EOpConstructIVec4: out << "Construct ivec4"; break;
348 case EOpConstructUInt: out << "Construct uint"; break;
349 case EOpConstructUVec2: out << "Construct uvec2"; break;
350 case EOpConstructUVec3: out << "Construct uvec3"; break;
351 case EOpConstructUVec4: out << "Construct uvec4"; break;
352 case EOpConstructMat2: out << "Construct mat2"; break;
353 case EOpConstructMat3: out << "Construct mat3"; break;
354 case EOpConstructMat4: out << "Construct mat4"; break;
355 case EOpConstructStruct: out << "Construct structure"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700357 case EOpLessThan: out << "Compare Less Than"; break;
358 case EOpGreaterThan: out << "Compare Greater Than"; break;
359 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
360 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
361 case EOpVectorEqual: out << "Equal"; break;
362 case EOpVectorNotEqual: out << "NotEqual"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700364 case EOpMod: out << "mod"; break;
365 case EOpPow: out << "pow"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700367 case EOpAtan: out << "arc tangent"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700369 case EOpMin: out << "min"; break;
370 case EOpMax: out << "max"; break;
371 case EOpClamp: out << "clamp"; break;
372 case EOpMix: out << "mix"; break;
373 case EOpStep: out << "step"; break;
374 case EOpSmoothStep: out << "smoothstep"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700376 case EOpDistance: out << "distance"; break;
377 case EOpDot: out << "dot-product"; break;
378 case EOpCross: out << "cross-product"; break;
379 case EOpFaceForward: out << "face-forward"; break;
380 case EOpReflect: out << "reflect"; break;
381 case EOpRefract: out << "refract"; break;
382 case EOpMul: out << "component-wise multiply"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700384 case EOpDeclaration: out << "Declaration: "; break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400385 case EOpInvariantDeclaration: out << "Invariant Declaration: "; break;
daniel@transgaming.coma9ae4aa2011-10-19 13:29:27 +0000386
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700387 default:
388 out.prefix(EPrefixError);
389 out << "Bad aggregation op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391
alokp@chromium.org76b82082010-03-24 17:59:39 +0000392 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000393 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394
alokp@chromium.org7beea402010-09-15 21:18:34 +0000395 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
alokp@chromium.org76b82082010-03-24 17:59:39 +0000397 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700400bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700402 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700404 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405
alokp@chromium.org7beea402010-09-15 21:18:34 +0000406 out << "Test condition and select";
407 out << " (" << node->getCompleteString() << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700409 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700411 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000412 out << "Condition\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000413 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700415 OutputTreeText(sink, node, mDepth);
416 if (node->getTrueBlock())
417 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000418 out << "true case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000419 node->getTrueBlock()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700420 }
421 else
422 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000423 out << "true case is null\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700424 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700426 if (node->getFalseBlock())
427 {
428 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000429 out << "false case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000430 node->getFalseBlock()->traverse(this);
431 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700433 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
alokp@chromium.org76b82082010-03-24 17:59:39 +0000435 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700438void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700440 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441
Jamie Madill94bf7f22013-07-08 13:31:15 -0400442 size_t size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700444 for (size_t i = 0; i < size; i++)
445 {
446 OutputTreeText(out, node, mDepth);
447 switch (node->getUnionArrayPointer()[i].getType())
448 {
449 case EbtBool:
450 if (node->getUnionArrayPointer()[i].getBConst())
451 out << "true";
452 else
453 out << "false";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700455 out << " (" << "const bool" << ")";
456 out << "\n";
457 break;
458 case EbtFloat:
459 out << node->getUnionArrayPointer()[i].getFConst();
460 out << " (const float)\n";
461 break;
462 case EbtInt:
463 out << node->getUnionArrayPointer()[i].getIConst();
464 out << " (const int)\n";
465 break;
466 case EbtUInt:
467 out << node->getUnionArrayPointer()[i].getUConst();
468 out << " (const uint)\n";
469 break;
470 default:
471 out.message(EPrefixInternalError, node->getLine(), "Unknown constant");
472 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000473 }
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475}
476
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700477bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700479 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700481 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
alokp@chromium.org7beea402010-09-15 21:18:34 +0000483 out << "Loop with condition ";
alokp@chromium.org52813552010-11-16 18:36:09 +0000484 if (node->getType() == ELoopDoWhile)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000485 out << "not ";
486 out << "tested first\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700488 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700490 OutputTreeText(sink, node, mDepth);
491 if (node->getCondition())
492 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000493 out << "Loop Condition\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000494 node->getCondition()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700495 }
496 else
497 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000498 out << "No loop condition\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700501 OutputTreeText(sink, node, mDepth);
502 if (node->getBody())
503 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000504 out << "Loop Body\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000505 node->getBody()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700506 }
507 else
508 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000509 out << "No loop body\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700512 if (node->getExpression())
513 {
514 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000515 out << "Loop Terminal Expression\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000516 node->getExpression()->traverse(this);
alokp@chromium.org76b82082010-03-24 17:59:39 +0000517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700519 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
alokp@chromium.org76b82082010-03-24 17:59:39 +0000521 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522}
523
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700524bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700526 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700528 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700530 switch (node->getFlowOp())
531 {
532 case EOpKill: out << "Branch: Kill"; break;
533 case EOpBreak: out << "Branch: Break"; break;
534 case EOpContinue: out << "Branch: Continue"; break;
535 case EOpReturn: out << "Branch: Return"; break;
536 default: out << "Branch: Unknown Branch"; break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700539 if (node->getExpression())
540 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000541 out << " with expression\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700542 ++mDepth;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000543 node->getExpression()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700544 --mDepth;
545 }
546 else
547 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000548 out << "\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
alokp@chromium.org76b82082010-03-24 17:59:39 +0000551 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552}
553
554//
555// This function is the one to call externally to start the traversal.
556// Individual functions can be initialized to 0 to skip processing of that
557// type of node. It's children will still be processed.
558//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700559void TIntermediate::outputTree(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700561 if (root == NULL)
alokp@chromium.org76b82082010-03-24 17:59:39 +0000562 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700564 TOutputTraverser it(mInfoSink.info);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
alokp@chromium.org76b82082010-03-24 17:59:39 +0000566 root->traverse(&it);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567}