blob: 088448ba5f62416d80d07eceb4bf4ad0e2a636eb [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
Jamie Madill45bcc782016-11-07 13:58:48 -050010namespace sh
11{
12
Zhenyao Moe40d1e92014-07-16 17:40:36 -070013namespace
14{
15
Olli Etuaho336b1472016-10-05 16:37:55 +010016void OutputFunction(TInfoSinkBase &out, const char *str, TFunctionSymbolInfo *info)
Olli Etuaho5f579b12015-08-14 17:44:43 +030017{
Olli Etuaho336b1472016-10-05 16:37:55 +010018 const char *internal = info->getNameObj().isInternal() ? " (internal function)" : "";
19 out << str << internal << ": " << info->getNameObj().getString() << " (symbol id "
20 << info->getId() << ")";
Olli Etuaho5f579b12015-08-14 17:44:43 +030021}
22
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
24// Two purposes:
25// 1. Show an example of how to iterate tree. Functions can
26// also directly call Traverse() on children themselves to
27// have finer grained control over the process than shown here.
28// See the last function for how to get started.
29// 2. Print out a text based description of the tree.
30//
31
32//
33// Use this class to carry along data from node to node in
34// the traversal
35//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070036class TOutputTraverser : public TIntermTraverser
37{
38 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -050039 TOutputTraverser(TInfoSinkBase &i) : TIntermTraverser(true, false, false), sink(i) {}
40 TInfoSinkBase &sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041
Zhenyao Moe40d1e92014-07-16 17:40:36 -070042 protected:
Corentin Walleze5a1f272015-08-21 02:58:25 +020043 void visitSymbol(TIntermSymbol *) override;
44 void visitConstantUnion(TIntermConstantUnion *) override;
Olli Etuahob6fa0432016-09-28 16:28:05 +010045 bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
Corentin Walleze5a1f272015-08-21 02:58:25 +020046 bool visitBinary(Visit visit, TIntermBinary *) override;
47 bool visitUnary(Visit visit, TIntermUnary *) override;
Olli Etuahod0bad2c2016-09-09 18:01:16 +030048 bool visitTernary(Visit visit, TIntermTernary *node) override;
Olli Etuaho57961272016-09-14 13:57:46 +030049 bool visitIfElse(Visit visit, TIntermIfElse *node) override;
Olli Etuaho13e4d212016-10-13 11:50:27 +010050 bool visitSwitch(Visit visit, TIntermSwitch *node) override;
51 bool visitCase(Visit visit, TIntermCase *node) override;
Olli Etuaho336b1472016-10-05 16:37:55 +010052 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
Corentin Walleze5a1f272015-08-21 02:58:25 +020053 bool visitAggregate(Visit visit, TIntermAggregate *) override;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010054 bool visitBlock(Visit visit, TIntermBlock *) override;
Olli Etuahobf4e1b72016-12-09 11:30:15 +000055 bool visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node) override;
Olli Etuaho13389b62016-10-16 11:48:18 +010056 bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
Corentin Walleze5a1f272015-08-21 02:58:25 +020057 bool visitLoop(Visit visit, TIntermLoop *) override;
58 bool visitBranch(Visit visit, TIntermBranch *) override;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059};
60
Zhenyao Moe40d1e92014-07-16 17:40:36 -070061//
62// Helper functions for printing, not part of traversing.
63//
64void OutputTreeText(TInfoSinkBase &sink, TIntermNode *node, const int depth)
65{
66 int i;
67
Olli Etuaho77ba4082016-12-16 12:01:18 +000068 sink.location(node->getLine().first_file, node->getLine().first_line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070069
70 for (i = 0; i < depth; ++i)
71 sink << " ";
72}
73
74} // namespace anonymous
75
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077// The rest of the file are the traversal functions. The last one
78// is the one that starts the traversal.
79//
80// Return true from interior nodes to have the external traversal
81// continue on to children. If you process children yourself,
82// return false.
83//
84
Zhenyao Moe40d1e92014-07-16 17:40:36 -070085void TOutputTraverser::visitSymbol(TIntermSymbol *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086{
Zhenyao Moe40d1e92014-07-16 17:40:36 -070087 OutputTreeText(sink, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
alokp@chromium.org7beea402010-09-15 21:18:34 +000089 sink << "'" << node->getSymbol() << "' ";
90 sink << "(" << node->getCompleteString() << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091}
92
Olli Etuahob6fa0432016-09-28 16:28:05 +010093bool TOutputTraverser::visitSwizzle(Visit visit, TIntermSwizzle *node)
94{
95 TInfoSinkBase &out = sink;
96 OutputTreeText(out, node, mDepth);
97 out << "vector swizzle";
98 return true;
99}
100
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700101bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500103 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700105 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700107 switch (node->getOp())
108 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100109 case EOpComma:
110 out << "comma";
111 break;
112 case EOpAssign:
113 out << "move second child to first child";
114 break;
115 case EOpInitialize:
116 out << "initialize first child with second child";
117 break;
118 case EOpAddAssign:
119 out << "add second child into first child";
120 break;
121 case EOpSubAssign:
122 out << "subtract second child into first child";
123 break;
124 case EOpMulAssign:
125 out << "multiply second child into first child";
126 break;
127 case EOpVectorTimesMatrixAssign:
128 out << "matrix mult second child into first child";
129 break;
130 case EOpVectorTimesScalarAssign:
131 out << "vector scale second child into first child";
132 break;
133 case EOpMatrixTimesScalarAssign:
134 out << "matrix scale second child into first child";
135 break;
136 case EOpMatrixTimesMatrixAssign:
137 out << "matrix mult second child into first child";
138 break;
139 case EOpDivAssign:
140 out << "divide second child into first child";
141 break;
142 case EOpIModAssign:
143 out << "modulo second child into first child";
144 break;
145 case EOpBitShiftLeftAssign:
146 out << "bit-wise shift first child left by second child";
147 break;
148 case EOpBitShiftRightAssign:
149 out << "bit-wise shift first child right by second child";
150 break;
151 case EOpBitwiseAndAssign:
152 out << "bit-wise and second child into first child";
153 break;
154 case EOpBitwiseXorAssign:
155 out << "bit-wise xor second child into first child";
156 break;
157 case EOpBitwiseOrAssign:
158 out << "bit-wise or second child into first child";
159 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200160
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100161 case EOpIndexDirect:
162 out << "direct index";
163 break;
164 case EOpIndexIndirect:
165 out << "indirect index";
166 break;
167 case EOpIndexDirectStruct:
168 out << "direct index for structure";
169 break;
170 case EOpIndexDirectInterfaceBlock:
171 out << "direct index for interface block";
172 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100174 case EOpAdd:
175 out << "add";
176 break;
177 case EOpSub:
178 out << "subtract";
179 break;
180 case EOpMul:
181 out << "component-wise multiply";
182 break;
183 case EOpDiv:
184 out << "divide";
185 break;
186 case EOpIMod:
187 out << "modulo";
188 break;
189 case EOpBitShiftLeft:
190 out << "bit-wise shift left";
191 break;
192 case EOpBitShiftRight:
193 out << "bit-wise shift right";
194 break;
195 case EOpBitwiseAnd:
196 out << "bit-wise and";
197 break;
198 case EOpBitwiseXor:
199 out << "bit-wise xor";
200 break;
201 case EOpBitwiseOr:
202 out << "bit-wise or";
203 break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +0200204
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100205 case EOpEqual:
206 out << "Compare Equal";
207 break;
208 case EOpNotEqual:
209 out << "Compare Not Equal";
210 break;
211 case EOpLessThan:
212 out << "Compare Less Than";
213 break;
214 case EOpGreaterThan:
215 out << "Compare Greater Than";
216 break;
217 case EOpLessThanEqual:
218 out << "Compare Less Than or Equal";
219 break;
220 case EOpGreaterThanEqual:
221 out << "Compare Greater Than or Equal";
222 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100224 case EOpVectorTimesScalar:
225 out << "vector-scale";
226 break;
227 case EOpVectorTimesMatrix:
228 out << "vector-times-matrix";
229 break;
230 case EOpMatrixTimesVector:
231 out << "matrix-times-vector";
232 break;
233 case EOpMatrixTimesScalar:
234 out << "matrix-scale";
235 break;
236 case EOpMatrixTimesMatrix:
237 out << "matrix-multiply";
238 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100240 case EOpLogicalOr:
241 out << "logical-or";
242 break;
243 case EOpLogicalXor:
244 out << "logical-xor";
245 break;
246 case EOpLogicalAnd:
247 out << "logical-and";
248 break;
249 default:
250 out << "<unknown op>";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000251 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
alokp@chromium.org7beea402010-09-15 21:18:34 +0000253 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254
alokp@chromium.org7beea402010-09-15 21:18:34 +0000255 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
Jamie Madill60305f12014-11-18 13:25:26 -0500257 // Special handling for direct indexes. Because constant
258 // unions are not aware they are struct indexes, treat them
259 // here where we have that contextual knowledge.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500260 if (node->getOp() == EOpIndexDirectStruct || node->getOp() == EOpIndexDirectInterfaceBlock)
Jamie Madill60305f12014-11-18 13:25:26 -0500261 {
262 mDepth++;
263 node->getLeft()->traverse(this);
264 mDepth--;
265
266 TIntermConstantUnion *intermConstantUnion = node->getRight()->getAsConstantUnion();
267 ASSERT(intermConstantUnion);
268
269 OutputTreeText(out, intermConstantUnion, mDepth + 1);
270
271 // The following code finds the field name from the constant union
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500272 const TConstantUnion *constantUnion = intermConstantUnion->getUnionArrayPointer();
273 const TStructure *structure = node->getLeft()->getType().getStruct();
Jamie Madill60305f12014-11-18 13:25:26 -0500274 const TInterfaceBlock *interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
275 ASSERT(structure || interfaceBlock);
276
277 const TFieldList &fields = structure ? structure->fields() : interfaceBlock->fields();
278
279 const TField *field = fields[constantUnion->getIConst()];
280
281 out << constantUnion->getIConst() << " (field '" << field->name() << "')";
282
283 return false;
284 }
285
alokp@chromium.org76b82082010-03-24 17:59:39 +0000286 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287}
288
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700289bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500291 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700293 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700295 switch (node->getOp())
296 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500297 case EOpNegative:
298 out << "Negate value";
299 break;
300 case EOpPositive:
301 out << "Positive sign";
302 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500303 case EOpLogicalNot:
Olli Etuahod68924e2017-01-02 17:34:40 +0000304 out << "negation";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500305 break;
306 case EOpBitwiseNot:
307 out << "bit-wise not";
308 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 case EOpPostIncrement:
311 out << "Post-Increment";
312 break;
313 case EOpPostDecrement:
314 out << "Post-Decrement";
315 break;
316 case EOpPreIncrement:
317 out << "Pre-Increment";
318 break;
319 case EOpPreDecrement:
320 out << "Pre-Decrement";
321 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500323 case EOpRadians:
324 out << "radians";
325 break;
326 case EOpDegrees:
327 out << "degrees";
328 break;
329 case EOpSin:
330 out << "sine";
331 break;
332 case EOpCos:
333 out << "cosine";
334 break;
335 case EOpTan:
336 out << "tangent";
337 break;
338 case EOpAsin:
339 out << "arc sine";
340 break;
341 case EOpAcos:
342 out << "arc cosine";
343 break;
344 case EOpAtan:
345 out << "arc tangent";
346 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500348 case EOpSinh:
349 out << "hyperbolic sine";
350 break;
351 case EOpCosh:
352 out << "hyperbolic cosine";
353 break;
354 case EOpTanh:
355 out << "hyperbolic tangent";
356 break;
357 case EOpAsinh:
358 out << "arc hyperbolic sine";
359 break;
360 case EOpAcosh:
361 out << "arc hyperbolic cosine";
362 break;
363 case EOpAtanh:
364 out << "arc hyperbolic tangent";
365 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200366
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500367 case EOpExp:
368 out << "exp";
369 break;
370 case EOpLog:
371 out << "log";
372 break;
373 case EOpExp2:
374 out << "exp2";
375 break;
376 case EOpLog2:
377 out << "log2";
378 break;
379 case EOpSqrt:
380 out << "sqrt";
381 break;
382 case EOpInverseSqrt:
383 out << "inverse sqrt";
384 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500386 case EOpAbs:
387 out << "Absolute value";
388 break;
389 case EOpSign:
390 out << "Sign";
391 break;
392 case EOpFloor:
393 out << "Floor";
394 break;
395 case EOpTrunc:
396 out << "Truncate";
397 break;
398 case EOpRound:
399 out << "Round";
400 break;
401 case EOpRoundEven:
402 out << "Round half even";
403 break;
404 case EOpCeil:
405 out << "Ceiling";
406 break;
407 case EOpFract:
408 out << "Fraction";
409 break;
410 case EOpIsNan:
411 out << "Is not a number";
412 break;
413 case EOpIsInf:
414 out << "Is infinity";
415 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500417 case EOpFloatBitsToInt:
418 out << "float bits to int";
419 break;
420 case EOpFloatBitsToUint:
421 out << "float bits to uint";
422 break;
423 case EOpIntBitsToFloat:
424 out << "int bits to float";
425 break;
426 case EOpUintBitsToFloat:
427 out << "uint bits to float";
428 break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200429
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500430 case EOpPackSnorm2x16:
431 out << "pack Snorm 2x16";
432 break;
433 case EOpPackUnorm2x16:
434 out << "pack Unorm 2x16";
435 break;
436 case EOpPackHalf2x16:
437 out << "pack half 2x16";
438 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +0200439
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500440 case EOpUnpackSnorm2x16:
441 out << "unpack Snorm 2x16";
442 break;
443 case EOpUnpackUnorm2x16:
444 out << "unpack Unorm 2x16";
445 break;
446 case EOpUnpackHalf2x16:
447 out << "unpack half 2x16";
448 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +0200449
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500450 case EOpLength:
451 out << "length";
452 break;
453 case EOpNormalize:
454 out << "normalize";
455 break;
456 // case EOpDPdx: out << "dPdx"; break;
457 // case EOpDPdy: out << "dPdy"; break;
458 // case EOpFwidth: out << "fwidth"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500460 case EOpDeterminant:
461 out << "determinant";
462 break;
463 case EOpTranspose:
464 out << "transpose";
465 break;
466 case EOpInverse:
467 out << "inverse";
468 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200469
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500470 case EOpAny:
471 out << "any";
472 break;
473 case EOpAll:
474 out << "all";
475 break;
Olli Etuahod68924e2017-01-02 17:34:40 +0000476 case EOpLogicalNotComponentWise:
477 out << "component-wise not";
478 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500480 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000481 out.prefix(SH_ERROR);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500482 out << "Bad unary op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
alokp@chromium.org7beea402010-09-15 21:18:34 +0000485 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
alokp@chromium.org7beea402010-09-15 21:18:34 +0000487 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488
alokp@chromium.org76b82082010-03-24 17:59:39 +0000489 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490}
491
Olli Etuaho336b1472016-10-05 16:37:55 +0100492bool TOutputTraverser::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
493{
494 TInfoSinkBase &out = sink;
495 OutputTreeText(out, node, mDepth);
496 OutputFunction(out, "Function Definition", node->getFunctionSymbolInfo());
497 out << "\n";
498 return true;
499}
500
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000501bool TOutputTraverser::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
502{
503 TInfoSinkBase &out = sink;
504 OutputTreeText(out, node, mDepth);
505 out << "Invariant Declaration:\n";
506 return true;
507}
508
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700509bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700511 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100513 OutputTreeText(out, node, mDepth);
514
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700515 if (node->getOp() == EOpNull)
516 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000517 out.prefix(SH_ERROR);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100518 out << "node is still EOpNull!\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000519 return true;
520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700522 switch (node->getOp())
523 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500524 case EOpFunctionCall:
525 OutputFunction(out, "Function Call", node->getFunctionSymbolInfo());
526 break;
527 case EOpParameters:
528 out << "Function Parameters: ";
529 break;
530 case EOpPrototype:
531 OutputFunction(out, "Function Prototype", node->getFunctionSymbolInfo());
532 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500534 case EOpConstructFloat:
535 out << "Construct float";
536 break;
537 case EOpConstructVec2:
538 out << "Construct vec2";
539 break;
540 case EOpConstructVec3:
541 out << "Construct vec3";
542 break;
543 case EOpConstructVec4:
544 out << "Construct vec4";
545 break;
546 case EOpConstructBool:
547 out << "Construct bool";
548 break;
549 case EOpConstructBVec2:
550 out << "Construct bvec2";
551 break;
552 case EOpConstructBVec3:
553 out << "Construct bvec3";
554 break;
555 case EOpConstructBVec4:
556 out << "Construct bvec4";
557 break;
558 case EOpConstructInt:
559 out << "Construct int";
560 break;
561 case EOpConstructIVec2:
562 out << "Construct ivec2";
563 break;
564 case EOpConstructIVec3:
565 out << "Construct ivec3";
566 break;
567 case EOpConstructIVec4:
568 out << "Construct ivec4";
569 break;
570 case EOpConstructUInt:
571 out << "Construct uint";
572 break;
573 case EOpConstructUVec2:
574 out << "Construct uvec2";
575 break;
576 case EOpConstructUVec3:
577 out << "Construct uvec3";
578 break;
579 case EOpConstructUVec4:
580 out << "Construct uvec4";
581 break;
582 case EOpConstructMat2:
583 out << "Construct mat2";
584 break;
585 case EOpConstructMat2x3:
586 out << "Construct mat2x3";
587 break;
588 case EOpConstructMat2x4:
589 out << "Construct mat2x4";
590 break;
591 case EOpConstructMat3x2:
592 out << "Construct mat3x2";
593 break;
594 case EOpConstructMat3:
595 out << "Construct mat3";
596 break;
597 case EOpConstructMat3x4:
598 out << "Construct mat3x4";
599 break;
600 case EOpConstructMat4x2:
601 out << "Construct mat4x2";
602 break;
603 case EOpConstructMat4x3:
604 out << "Construct mat4x3";
605 break;
606 case EOpConstructMat4:
607 out << "Construct mat4";
608 break;
609 case EOpConstructStruct:
610 out << "Construct structure";
611 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
Olli Etuahoe1805592017-01-02 16:41:20 +0000613 case EOpEqualComponentWise:
614 out << "component-wise equal";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500615 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000616 case EOpNotEqualComponentWise:
617 out << "component-wise not equal";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500618 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000619 case EOpLessThanComponentWise:
620 out << "component-wise less than";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500621 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000622 case EOpGreaterThanComponentWise:
623 out << "component-wise greater than";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500624 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000625 case EOpLessThanEqualComponentWise:
626 out << "component-wise less than or equal";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500627 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000628 case EOpGreaterThanEqualComponentWise:
629 out << "component-wise greater than or equal";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500630 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500632 case EOpMod:
633 out << "mod";
634 break;
635 case EOpModf:
636 out << "modf";
637 break;
638 case EOpPow:
639 out << "pow";
640 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500642 case EOpAtan:
643 out << "arc tangent";
644 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500646 case EOpMin:
647 out << "min";
648 break;
649 case EOpMax:
650 out << "max";
651 break;
652 case EOpClamp:
653 out << "clamp";
654 break;
655 case EOpMix:
656 out << "mix";
657 break;
658 case EOpStep:
659 out << "step";
660 break;
661 case EOpSmoothStep:
662 out << "smoothstep";
663 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500665 case EOpDistance:
666 out << "distance";
667 break;
668 case EOpDot:
669 out << "dot-product";
670 break;
671 case EOpCross:
672 out << "cross-product";
673 break;
674 case EOpFaceForward:
675 out << "face-forward";
676 break;
677 case EOpReflect:
678 out << "reflect";
679 break;
680 case EOpRefract:
681 out << "refract";
682 break;
Olli Etuahoe1805592017-01-02 16:41:20 +0000683 case EOpMulMatrixComponentWise:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500684 out << "component-wise multiply";
685 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500687 case EOpOuterProduct:
688 out << "outer product";
689 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200690
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500691 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000692 out.prefix(SH_ERROR);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500693 out << "Bad aggregation op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000694 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100696 if (node->getOp() != EOpParameters)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000697 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698
alokp@chromium.org7beea402010-09-15 21:18:34 +0000699 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700
alokp@chromium.org76b82082010-03-24 17:59:39 +0000701 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702}
703
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100704bool TOutputTraverser::visitBlock(Visit visit, TIntermBlock *node)
705{
706 TInfoSinkBase &out = sink;
707
708 OutputTreeText(out, node, mDepth);
709 out << "Code block\n";
710
711 return true;
712}
713
Olli Etuaho13389b62016-10-16 11:48:18 +0100714bool TOutputTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
715{
716 TInfoSinkBase &out = sink;
717
718 OutputTreeText(out, node, mDepth);
719 out << "Declaration\n";
720
721 return true;
722}
723
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300724bool TOutputTraverser::visitTernary(Visit visit, TIntermTernary *node)
725{
726 TInfoSinkBase &out = sink;
727
728 OutputTreeText(out, node, mDepth);
729
730 out << "Ternary selection";
731 out << " (" << node->getCompleteString() << ")\n";
732
733 ++mDepth;
734
735 OutputTreeText(sink, node, mDepth);
736 out << "Condition\n";
737 node->getCondition()->traverse(this);
738
739 OutputTreeText(sink, node, mDepth);
740 if (node->getTrueExpression())
741 {
742 out << "true case\n";
743 node->getTrueExpression()->traverse(this);
744 }
745 if (node->getFalseExpression())
746 {
747 OutputTreeText(sink, node, mDepth);
748 out << "false case\n";
749 node->getFalseExpression()->traverse(this);
750 }
751
752 --mDepth;
753
754 return false;
755}
756
Olli Etuaho57961272016-09-14 13:57:46 +0300757bool TOutputTraverser::visitIfElse(Visit visit, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700759 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700761 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300763 out << "If test\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700765 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700767 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000768 out << "Condition\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000769 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700771 OutputTreeText(sink, node, mDepth);
772 if (node->getTrueBlock())
773 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000774 out << "true case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000775 node->getTrueBlock()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700776 }
777 else
778 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000779 out << "true case is null\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700782 if (node->getFalseBlock())
783 {
784 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000785 out << "false case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000786 node->getFalseBlock()->traverse(this);
787 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700789 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
alokp@chromium.org76b82082010-03-24 17:59:39 +0000791 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
Olli Etuaho13e4d212016-10-13 11:50:27 +0100794bool TOutputTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
795{
796 TInfoSinkBase &out = sink;
797
798 OutputTreeText(out, node, mDepth);
799
800 out << "Switch\n";
801
802 return true;
803}
804
805bool TOutputTraverser::visitCase(Visit visit, TIntermCase *node)
806{
807 TInfoSinkBase &out = sink;
808
809 OutputTreeText(out, node, mDepth);
810
811 if (node->getCondition() == nullptr)
812 {
813 out << "Default\n";
814 }
815 else
816 {
817 out << "Case\n";
818 }
819
820 return true;
821}
822
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700823void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700825 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
Jamie Madill94bf7f22013-07-08 13:31:15 -0400827 size_t size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700829 for (size_t i = 0; i < size; i++)
830 {
831 OutputTreeText(out, node, mDepth);
832 switch (node->getUnionArrayPointer()[i].getType())
833 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500834 case EbtBool:
835 if (node->getUnionArrayPointer()[i].getBConst())
836 out << "true";
837 else
838 out << "false";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500840 out << " ("
841 << "const bool"
842 << ")";
843 out << "\n";
844 break;
845 case EbtFloat:
846 out << node->getUnionArrayPointer()[i].getFConst();
847 out << " (const float)\n";
848 break;
849 case EbtInt:
850 out << node->getUnionArrayPointer()[i].getIConst();
851 out << " (const int)\n";
852 break;
853 case EbtUInt:
854 out << node->getUnionArrayPointer()[i].getUConst();
855 out << " (const uint)\n";
856 break;
857 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000858 out.prefix(SH_ERROR);
859 out << "Unknown constant";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500860 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000861 }
862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863}
864
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700865bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700867 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700869 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
alokp@chromium.org7beea402010-09-15 21:18:34 +0000871 out << "Loop with condition ";
alokp@chromium.org52813552010-11-16 18:36:09 +0000872 if (node->getType() == ELoopDoWhile)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000873 out << "not ";
874 out << "tested first\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700876 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700878 OutputTreeText(sink, node, mDepth);
879 if (node->getCondition())
880 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000881 out << "Loop Condition\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000882 node->getCondition()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700883 }
884 else
885 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000886 out << "No loop condition\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700887 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700889 OutputTreeText(sink, node, mDepth);
890 if (node->getBody())
891 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000892 out << "Loop Body\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000893 node->getBody()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700894 }
895 else
896 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000897 out << "No loop body\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700900 if (node->getExpression())
901 {
902 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000903 out << "Loop Terminal Expression\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000904 node->getExpression()->traverse(this);
alokp@chromium.org76b82082010-03-24 17:59:39 +0000905 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700907 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
alokp@chromium.org76b82082010-03-24 17:59:39 +0000909 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700912bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700914 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700916 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700918 switch (node->getFlowOp())
919 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500920 case EOpKill:
921 out << "Branch: Kill";
922 break;
923 case EOpBreak:
924 out << "Branch: Break";
925 break;
926 case EOpContinue:
927 out << "Branch: Continue";
928 break;
929 case EOpReturn:
930 out << "Branch: Return";
931 break;
932 default:
933 out << "Branch: Unknown Branch";
934 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000935 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700937 if (node->getExpression())
938 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000939 out << " with expression\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700940 ++mDepth;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000941 node->getExpression()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700942 --mDepth;
943 }
944 else
945 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000946 out << "\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700947 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948
alokp@chromium.org76b82082010-03-24 17:59:39 +0000949 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950}
951
952//
953// This function is the one to call externally to start the traversal.
954// Individual functions can be initialized to 0 to skip processing of that
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200955// type of node. Its children will still be processed.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956//
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200957void TIntermediate::outputTree(TIntermNode *root, TInfoSinkBase &infoSink)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200959 TOutputTraverser it(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200961 ASSERT(root);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
alokp@chromium.org76b82082010-03-24 17:59:39 +0000963 root->traverse(&it);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
Jamie Madill45bcc782016-11-07 13:58:48 -0500965
966} // namespace sh