blob: 31b0fdd5d9268e23f0b2ed2cca19026b9274d537 [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;
303 case EOpVectorLogicalNot:
304 case EOpLogicalNot:
305 out << "Negate conditional";
306 break;
307 case EOpBitwiseNot:
308 out << "bit-wise not";
309 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500311 case EOpPostIncrement:
312 out << "Post-Increment";
313 break;
314 case EOpPostDecrement:
315 out << "Post-Decrement";
316 break;
317 case EOpPreIncrement:
318 out << "Pre-Increment";
319 break;
320 case EOpPreDecrement:
321 out << "Pre-Decrement";
322 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 case EOpRadians:
325 out << "radians";
326 break;
327 case EOpDegrees:
328 out << "degrees";
329 break;
330 case EOpSin:
331 out << "sine";
332 break;
333 case EOpCos:
334 out << "cosine";
335 break;
336 case EOpTan:
337 out << "tangent";
338 break;
339 case EOpAsin:
340 out << "arc sine";
341 break;
342 case EOpAcos:
343 out << "arc cosine";
344 break;
345 case EOpAtan:
346 out << "arc tangent";
347 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500349 case EOpSinh:
350 out << "hyperbolic sine";
351 break;
352 case EOpCosh:
353 out << "hyperbolic cosine";
354 break;
355 case EOpTanh:
356 out << "hyperbolic tangent";
357 break;
358 case EOpAsinh:
359 out << "arc hyperbolic sine";
360 break;
361 case EOpAcosh:
362 out << "arc hyperbolic cosine";
363 break;
364 case EOpAtanh:
365 out << "arc hyperbolic tangent";
366 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200367
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500368 case EOpExp:
369 out << "exp";
370 break;
371 case EOpLog:
372 out << "log";
373 break;
374 case EOpExp2:
375 out << "exp2";
376 break;
377 case EOpLog2:
378 out << "log2";
379 break;
380 case EOpSqrt:
381 out << "sqrt";
382 break;
383 case EOpInverseSqrt:
384 out << "inverse sqrt";
385 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 case EOpAbs:
388 out << "Absolute value";
389 break;
390 case EOpSign:
391 out << "Sign";
392 break;
393 case EOpFloor:
394 out << "Floor";
395 break;
396 case EOpTrunc:
397 out << "Truncate";
398 break;
399 case EOpRound:
400 out << "Round";
401 break;
402 case EOpRoundEven:
403 out << "Round half even";
404 break;
405 case EOpCeil:
406 out << "Ceiling";
407 break;
408 case EOpFract:
409 out << "Fraction";
410 break;
411 case EOpIsNan:
412 out << "Is not a number";
413 break;
414 case EOpIsInf:
415 out << "Is infinity";
416 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500418 case EOpFloatBitsToInt:
419 out << "float bits to int";
420 break;
421 case EOpFloatBitsToUint:
422 out << "float bits to uint";
423 break;
424 case EOpIntBitsToFloat:
425 out << "int bits to float";
426 break;
427 case EOpUintBitsToFloat:
428 out << "uint bits to float";
429 break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +0200430
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500431 case EOpPackSnorm2x16:
432 out << "pack Snorm 2x16";
433 break;
434 case EOpPackUnorm2x16:
435 out << "pack Unorm 2x16";
436 break;
437 case EOpPackHalf2x16:
438 out << "pack half 2x16";
439 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +0200440
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500441 case EOpUnpackSnorm2x16:
442 out << "unpack Snorm 2x16";
443 break;
444 case EOpUnpackUnorm2x16:
445 out << "unpack Unorm 2x16";
446 break;
447 case EOpUnpackHalf2x16:
448 out << "unpack half 2x16";
449 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +0200450
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500451 case EOpLength:
452 out << "length";
453 break;
454 case EOpNormalize:
455 out << "normalize";
456 break;
457 // case EOpDPdx: out << "dPdx"; break;
458 // case EOpDPdy: out << "dPdy"; break;
459 // case EOpFwidth: out << "fwidth"; break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 case EOpDeterminant:
462 out << "determinant";
463 break;
464 case EOpTranspose:
465 out << "transpose";
466 break;
467 case EOpInverse:
468 out << "inverse";
469 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200470
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500471 case EOpAny:
472 out << "any";
473 break;
474 case EOpAll:
475 out << "all";
476 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000477
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500478 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000479 out.prefix(SH_ERROR);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500480 out << "Bad unary op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000481 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
alokp@chromium.org7beea402010-09-15 21:18:34 +0000483 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
alokp@chromium.org7beea402010-09-15 21:18:34 +0000485 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
alokp@chromium.org76b82082010-03-24 17:59:39 +0000487 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488}
489
Olli Etuaho336b1472016-10-05 16:37:55 +0100490bool TOutputTraverser::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
491{
492 TInfoSinkBase &out = sink;
493 OutputTreeText(out, node, mDepth);
494 OutputFunction(out, "Function Definition", node->getFunctionSymbolInfo());
495 out << "\n";
496 return true;
497}
498
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000499bool TOutputTraverser::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
500{
501 TInfoSinkBase &out = sink;
502 OutputTreeText(out, node, mDepth);
503 out << "Invariant Declaration:\n";
504 return true;
505}
506
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700507bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700509 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100511 OutputTreeText(out, node, mDepth);
512
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700513 if (node->getOp() == EOpNull)
514 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000515 out.prefix(SH_ERROR);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100516 out << "node is still EOpNull!\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000517 return true;
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700520 switch (node->getOp())
521 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500522 case EOpFunctionCall:
523 OutputFunction(out, "Function Call", node->getFunctionSymbolInfo());
524 break;
525 case EOpParameters:
526 out << "Function Parameters: ";
527 break;
528 case EOpPrototype:
529 OutputFunction(out, "Function Prototype", node->getFunctionSymbolInfo());
530 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500532 case EOpConstructFloat:
533 out << "Construct float";
534 break;
535 case EOpConstructVec2:
536 out << "Construct vec2";
537 break;
538 case EOpConstructVec3:
539 out << "Construct vec3";
540 break;
541 case EOpConstructVec4:
542 out << "Construct vec4";
543 break;
544 case EOpConstructBool:
545 out << "Construct bool";
546 break;
547 case EOpConstructBVec2:
548 out << "Construct bvec2";
549 break;
550 case EOpConstructBVec3:
551 out << "Construct bvec3";
552 break;
553 case EOpConstructBVec4:
554 out << "Construct bvec4";
555 break;
556 case EOpConstructInt:
557 out << "Construct int";
558 break;
559 case EOpConstructIVec2:
560 out << "Construct ivec2";
561 break;
562 case EOpConstructIVec3:
563 out << "Construct ivec3";
564 break;
565 case EOpConstructIVec4:
566 out << "Construct ivec4";
567 break;
568 case EOpConstructUInt:
569 out << "Construct uint";
570 break;
571 case EOpConstructUVec2:
572 out << "Construct uvec2";
573 break;
574 case EOpConstructUVec3:
575 out << "Construct uvec3";
576 break;
577 case EOpConstructUVec4:
578 out << "Construct uvec4";
579 break;
580 case EOpConstructMat2:
581 out << "Construct mat2";
582 break;
583 case EOpConstructMat2x3:
584 out << "Construct mat2x3";
585 break;
586 case EOpConstructMat2x4:
587 out << "Construct mat2x4";
588 break;
589 case EOpConstructMat3x2:
590 out << "Construct mat3x2";
591 break;
592 case EOpConstructMat3:
593 out << "Construct mat3";
594 break;
595 case EOpConstructMat3x4:
596 out << "Construct mat3x4";
597 break;
598 case EOpConstructMat4x2:
599 out << "Construct mat4x2";
600 break;
601 case EOpConstructMat4x3:
602 out << "Construct mat4x3";
603 break;
604 case EOpConstructMat4:
605 out << "Construct mat4";
606 break;
607 case EOpConstructStruct:
608 out << "Construct structure";
609 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500611 case EOpLessThan:
612 out << "Compare Less Than";
613 break;
614 case EOpGreaterThan:
615 out << "Compare Greater Than";
616 break;
617 case EOpLessThanEqual:
618 out << "Compare Less Than or Equal";
619 break;
620 case EOpGreaterThanEqual:
621 out << "Compare Greater Than or Equal";
622 break;
623 case EOpVectorEqual:
624 out << "Equal";
625 break;
626 case EOpVectorNotEqual:
627 out << "NotEqual";
628 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500630 case EOpMod:
631 out << "mod";
632 break;
633 case EOpModf:
634 out << "modf";
635 break;
636 case EOpPow:
637 out << "pow";
638 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500640 case EOpAtan:
641 out << "arc tangent";
642 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500644 case EOpMin:
645 out << "min";
646 break;
647 case EOpMax:
648 out << "max";
649 break;
650 case EOpClamp:
651 out << "clamp";
652 break;
653 case EOpMix:
654 out << "mix";
655 break;
656 case EOpStep:
657 out << "step";
658 break;
659 case EOpSmoothStep:
660 out << "smoothstep";
661 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500663 case EOpDistance:
664 out << "distance";
665 break;
666 case EOpDot:
667 out << "dot-product";
668 break;
669 case EOpCross:
670 out << "cross-product";
671 break;
672 case EOpFaceForward:
673 out << "face-forward";
674 break;
675 case EOpReflect:
676 out << "reflect";
677 break;
678 case EOpRefract:
679 out << "refract";
680 break;
681 case EOpMul:
682 out << "component-wise multiply";
683 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500685 case EOpOuterProduct:
686 out << "outer product";
687 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +0200688
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500689 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000690 out.prefix(SH_ERROR);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500691 out << "Bad aggregation op";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100694 if (node->getOp() != EOpParameters)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000695 out << " (" << node->getCompleteString() << ")";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
alokp@chromium.org7beea402010-09-15 21:18:34 +0000697 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698
alokp@chromium.org76b82082010-03-24 17:59:39 +0000699 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700}
701
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100702bool TOutputTraverser::visitBlock(Visit visit, TIntermBlock *node)
703{
704 TInfoSinkBase &out = sink;
705
706 OutputTreeText(out, node, mDepth);
707 out << "Code block\n";
708
709 return true;
710}
711
Olli Etuaho13389b62016-10-16 11:48:18 +0100712bool TOutputTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
713{
714 TInfoSinkBase &out = sink;
715
716 OutputTreeText(out, node, mDepth);
717 out << "Declaration\n";
718
719 return true;
720}
721
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300722bool TOutputTraverser::visitTernary(Visit visit, TIntermTernary *node)
723{
724 TInfoSinkBase &out = sink;
725
726 OutputTreeText(out, node, mDepth);
727
728 out << "Ternary selection";
729 out << " (" << node->getCompleteString() << ")\n";
730
731 ++mDepth;
732
733 OutputTreeText(sink, node, mDepth);
734 out << "Condition\n";
735 node->getCondition()->traverse(this);
736
737 OutputTreeText(sink, node, mDepth);
738 if (node->getTrueExpression())
739 {
740 out << "true case\n";
741 node->getTrueExpression()->traverse(this);
742 }
743 if (node->getFalseExpression())
744 {
745 OutputTreeText(sink, node, mDepth);
746 out << "false case\n";
747 node->getFalseExpression()->traverse(this);
748 }
749
750 --mDepth;
751
752 return false;
753}
754
Olli Etuaho57961272016-09-14 13:57:46 +0300755bool TOutputTraverser::visitIfElse(Visit visit, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700757 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700759 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300761 out << "If test\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700763 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700765 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000766 out << "Condition\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000767 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700769 OutputTreeText(sink, node, mDepth);
770 if (node->getTrueBlock())
771 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000772 out << "true case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000773 node->getTrueBlock()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700774 }
775 else
776 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000777 out << "true case is null\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700780 if (node->getFalseBlock())
781 {
782 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000783 out << "false case\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000784 node->getFalseBlock()->traverse(this);
785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700787 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
alokp@chromium.org76b82082010-03-24 17:59:39 +0000789 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790}
791
Olli Etuaho13e4d212016-10-13 11:50:27 +0100792bool TOutputTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
793{
794 TInfoSinkBase &out = sink;
795
796 OutputTreeText(out, node, mDepth);
797
798 out << "Switch\n";
799
800 return true;
801}
802
803bool TOutputTraverser::visitCase(Visit visit, TIntermCase *node)
804{
805 TInfoSinkBase &out = sink;
806
807 OutputTreeText(out, node, mDepth);
808
809 if (node->getCondition() == nullptr)
810 {
811 out << "Default\n";
812 }
813 else
814 {
815 out << "Case\n";
816 }
817
818 return true;
819}
820
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700821void TOutputTraverser::visitConstantUnion(TIntermConstantUnion *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700823 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824
Jamie Madill94bf7f22013-07-08 13:31:15 -0400825 size_t size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700827 for (size_t i = 0; i < size; i++)
828 {
829 OutputTreeText(out, node, mDepth);
830 switch (node->getUnionArrayPointer()[i].getType())
831 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500832 case EbtBool:
833 if (node->getUnionArrayPointer()[i].getBConst())
834 out << "true";
835 else
836 out << "false";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500838 out << " ("
839 << "const bool"
840 << ")";
841 out << "\n";
842 break;
843 case EbtFloat:
844 out << node->getUnionArrayPointer()[i].getFConst();
845 out << " (const float)\n";
846 break;
847 case EbtInt:
848 out << node->getUnionArrayPointer()[i].getIConst();
849 out << " (const int)\n";
850 break;
851 case EbtUInt:
852 out << node->getUnionArrayPointer()[i].getUConst();
853 out << " (const uint)\n";
854 break;
855 default:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000856 out.prefix(SH_ERROR);
857 out << "Unknown constant";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500858 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000859 }
860 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700863bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700865 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700867 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
alokp@chromium.org7beea402010-09-15 21:18:34 +0000869 out << "Loop with condition ";
alokp@chromium.org52813552010-11-16 18:36:09 +0000870 if (node->getType() == ELoopDoWhile)
alokp@chromium.org7beea402010-09-15 21:18:34 +0000871 out << "not ";
872 out << "tested first\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700874 ++mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700876 OutputTreeText(sink, node, mDepth);
877 if (node->getCondition())
878 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000879 out << "Loop Condition\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000880 node->getCondition()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700881 }
882 else
883 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000884 out << "No loop condition\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700887 OutputTreeText(sink, node, mDepth);
888 if (node->getBody())
889 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000890 out << "Loop Body\n";
alokp@chromium.org76b82082010-03-24 17:59:39 +0000891 node->getBody()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700892 }
893 else
894 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000895 out << "No loop body\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700896 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700898 if (node->getExpression())
899 {
900 OutputTreeText(sink, node, mDepth);
alokp@chromium.org7beea402010-09-15 21:18:34 +0000901 out << "Loop Terminal Expression\n";
alokp@chromium.org52813552010-11-16 18:36:09 +0000902 node->getExpression()->traverse(this);
alokp@chromium.org76b82082010-03-24 17:59:39 +0000903 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700905 --mDepth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
alokp@chromium.org76b82082010-03-24 17:59:39 +0000907 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908}
909
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700910bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700912 TInfoSinkBase &out = sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700914 OutputTreeText(out, node, mDepth);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700916 switch (node->getFlowOp())
917 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500918 case EOpKill:
919 out << "Branch: Kill";
920 break;
921 case EOpBreak:
922 out << "Branch: Break";
923 break;
924 case EOpContinue:
925 out << "Branch: Continue";
926 break;
927 case EOpReturn:
928 out << "Branch: Return";
929 break;
930 default:
931 out << "Branch: Unknown Branch";
932 break;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000933 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700935 if (node->getExpression())
936 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000937 out << " with expression\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700938 ++mDepth;
alokp@chromium.org76b82082010-03-24 17:59:39 +0000939 node->getExpression()->traverse(this);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700940 --mDepth;
941 }
942 else
943 {
alokp@chromium.org7beea402010-09-15 21:18:34 +0000944 out << "\n";
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700945 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
alokp@chromium.org76b82082010-03-24 17:59:39 +0000947 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
950//
951// This function is the one to call externally to start the traversal.
952// Individual functions can be initialized to 0 to skip processing of that
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200953// type of node. Its children will still be processed.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954//
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200955void TIntermediate::outputTree(TIntermNode *root, TInfoSinkBase &infoSink)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200957 TOutputTraverser it(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200959 ASSERT(root);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
alokp@chromium.org76b82082010-03-24 17:59:39 +0000961 root->traverse(&it);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
Jamie Madill45bcc782016-11-07 13:58:48 -0500963
964} // namespace sh