blob: 9677dc80d805eb578f7dbeb609266a99b497b992 [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
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200293 case EOpFloatBitsToInt: out << "float bits to int"; break;
294 case EOpFloatBitsToUint: out << "float bits to uint"; break;
295 case EOpIntBitsToFloat: out << "int bits to float"; break;
296 case EOpUintBitsToFloat: out << "uint bits to float"; break;
297
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700298 case EOpLength: out << "length"; break;
299 case EOpNormalize: out << "normalize"; break;
300 // case EOpDPdx: out << "dPdx"; break;
301 // case EOpDPdy: out << "dPdy"; break;
302 // case EOpFwidth: out << "fwidth"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700304 case EOpAny: out << "any"; break;
305 case EOpAll: out << "all"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700307 default:
308 out.prefix(EPrefixError);
309 out << "Bad unary op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
alokp@chromium.org7beea402010-09-15 21:18:34 +0000312 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
alokp@chromium.org7beea402010-09-15 21:18:34 +0000314 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
alokp@chromium.org76b82082010-03-24 17:59:39 +0000316 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317}
318
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700321 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700323 if (node->getOp() == EOpNull)
324 {
Jamie Madill075edd82013-07-08 13:30:19 -0400325 out.prefix(EPrefixError);
326 out << "node is still EOpNull!";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000327 return true;
328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700330 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700332 switch (node->getOp())
333 {
334 case EOpSequence: out << "Sequence\n"; return true;
335 case EOpComma: out << "Comma\n"; return true;
336 case EOpFunction: out << "Function Definition: " << node->getName(); break;
337 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
338 case EOpParameters: out << "Function Parameters: "; break;
Olli Etuaho76acee82014-11-04 13:44:03 +0200339 case EOpPrototype: out << "Function Prototype: " << node->getName(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700341 case EOpConstructFloat: out << "Construct float"; break;
342 case EOpConstructVec2: out << "Construct vec2"; break;
343 case EOpConstructVec3: out << "Construct vec3"; break;
344 case EOpConstructVec4: out << "Construct vec4"; break;
345 case EOpConstructBool: out << "Construct bool"; break;
346 case EOpConstructBVec2: out << "Construct bvec2"; break;
347 case EOpConstructBVec3: out << "Construct bvec3"; break;
348 case EOpConstructBVec4: out << "Construct bvec4"; break;
349 case EOpConstructInt: out << "Construct int"; break;
350 case EOpConstructIVec2: out << "Construct ivec2"; break;
351 case EOpConstructIVec3: out << "Construct ivec3"; break;
352 case EOpConstructIVec4: out << "Construct ivec4"; break;
353 case EOpConstructUInt: out << "Construct uint"; break;
354 case EOpConstructUVec2: out << "Construct uvec2"; break;
355 case EOpConstructUVec3: out << "Construct uvec3"; break;
356 case EOpConstructUVec4: out << "Construct uvec4"; break;
357 case EOpConstructMat2: out << "Construct mat2"; break;
358 case EOpConstructMat3: out << "Construct mat3"; break;
359 case EOpConstructMat4: out << "Construct mat4"; break;
360 case EOpConstructStruct: out << "Construct structure"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700362 case EOpLessThan: out << "Compare Less Than"; break;
363 case EOpGreaterThan: out << "Compare Greater Than"; break;
364 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
365 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
366 case EOpVectorEqual: out << "Equal"; break;
367 case EOpVectorNotEqual: out << "NotEqual"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700369 case EOpMod: out << "mod"; break;
370 case EOpPow: out << "pow"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700372 case EOpAtan: out << "arc tangent"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700374 case EOpMin: out << "min"; break;
375 case EOpMax: out << "max"; break;
376 case EOpClamp: out << "clamp"; break;
377 case EOpMix: out << "mix"; break;
378 case EOpStep: out << "step"; break;
379 case EOpSmoothStep: out << "smoothstep"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700381 case EOpDistance: out << "distance"; break;
382 case EOpDot: out << "dot-product"; break;
383 case EOpCross: out << "cross-product"; break;
384 case EOpFaceForward: out << "face-forward"; break;
385 case EOpReflect: out << "reflect"; break;
386 case EOpRefract: out << "refract"; break;
387 case EOpMul: out << "component-wise multiply"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700389 case EOpDeclaration: out << "Declaration: "; break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400390 case EOpInvariantDeclaration: out << "Invariant Declaration: "; break;
daniel@transgaming.coma9ae4aa2011-10-19 13:29:27 +0000391
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700392 default:
393 out.prefix(EPrefixError);
394 out << "Bad aggregation op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
alokp@chromium.org76b82082010-03-24 17:59:39 +0000397 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000398 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
alokp@chromium.org7beea402010-09-15 21:18:34 +0000400 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
alokp@chromium.org76b82082010-03-24 17:59:39 +0000402 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700405bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700407 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700409 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410
alokp@chromium.org7beea402010-09-15 21:18:34 +0000411 out << "Test condition and select";
412 out << " (" << node->getCompleteString() << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700414 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700416 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000417 out << "Condition\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000418 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700420 OutputTreeText(sink, node, mDepth);
421 if (node->getTrueBlock())
422 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000423 out << "true case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000424 node->getTrueBlock()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700425 }
426 else
427 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000428 out << "true case is null\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700429 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700431 if (node->getFalseBlock())
432 {
433 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000434 out << "false case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000435 node->getFalseBlock()->traverse(this);
436 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700438 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439
alokp@chromium.org76b82082010-03-24 17:59:39 +0000440 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441}
442
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700443void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700445 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
Jamie Madill94bf7f22013-07-08 13:31:15 -0400447 size_t size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700449 for (size_t i = 0; i < size; i++)
450 {
451 OutputTreeText(out, node, mDepth);
452 switch (node->getUnionArrayPointer()[i].getType())
453 {
454 case EbtBool:
455 if (node->getUnionArrayPointer()[i].getBConst())
456 out << "true";
457 else
458 out << "false";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700460 out << " (" << "const bool" << ")";
461 out << "\n";
462 break;
463 case EbtFloat:
464 out << node->getUnionArrayPointer()[i].getFConst();
465 out << " (const float)\n";
466 break;
467 case EbtInt:
468 out << node->getUnionArrayPointer()[i].getIConst();
469 out << " (const int)\n";
470 break;
471 case EbtUInt:
472 out << node->getUnionArrayPointer()[i].getUConst();
473 out << " (const uint)\n";
474 break;
475 default:
476 out.message(EPrefixInternalError, node->getLine(), "Unknown constant");
477 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000478 }
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480}
481
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700482bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700484 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700486 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
alokp@chromium.org7beea402010-09-15 21:18:34 +0000488 out << "Loop with condition ";
alokp@chromium.org52813552010-11-16 18:36:09 +0000489 if (node->getType() == ELoopDoWhile)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000490 out << "not ";
491 out << "tested first\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700493 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700495 OutputTreeText(sink, node, mDepth);
496 if (node->getCondition())
497 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000498 out << "Loop Condition\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000499 node->getCondition()->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 condition\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 OutputTreeText(sink, node, mDepth);
507 if (node->getBody())
508 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000509 out << "Loop Body\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000510 node->getBody()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700511 }
512 else
513 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000514 out << "No loop body\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700517 if (node->getExpression())
518 {
519 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000520 out << "Loop Terminal Expression\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000521 node->getExpression()->traverse(this);
alokp@chromium.org76b82082010-03-24 17:59:39 +0000522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700524 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
alokp@chromium.org76b82082010-03-24 17:59:39 +0000526 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527}
528
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700529bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700531 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700533 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700535 switch (node->getFlowOp())
536 {
537 case EOpKill: out << "Branch: Kill"; break;
538 case EOpBreak: out << "Branch: Break"; break;
539 case EOpContinue: out << "Branch: Continue"; break;
540 case EOpReturn: out << "Branch: Return"; break;
541 default: out << "Branch: Unknown Branch"; break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700544 if (node->getExpression())
545 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000546 out << " with expression\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700547 ++mDepth;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000548 node->getExpression()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700549 --mDepth;
550 }
551 else
552 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000553 out << "\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700554 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555
alokp@chromium.org76b82082010-03-24 17:59:39 +0000556 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557}
558
559//
560// This function is the one to call externally to start the traversal.
561// Individual functions can be initialized to 0 to skip processing of that
562// type of node. It's children will still be processed.
563//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700564void TIntermediate::outputTree(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700566 if (root == NULL)
alokp@chromium.org76b82082010-03-24 17:59:39 +0000567 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700569 TOutputTraverser it(mInfoSink.info);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570
alokp@chromium.org76b82082010-03-24 17:59:39 +0000571 root->traverse(&it);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572}