blob: 0b219b9b66ba70567f126f0b697a907b9ce1ed77 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman89401822014-05-06 15:04:28 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capenscc863da2015-01-21 15:50:55 -05007#include "localintermediate.h"
8#include "SymbolTable.h"
John Bauman89401822014-05-06 15:04:28 -04009
10//
11// Two purposes:
12// 1. Show an example of how to iterate tree. Functions can
13// also directly call Traverse() on children themselves to
14// have finer grained control over the process than shown here.
15// See the last function for how to get started.
16// 2. Print out a text based description of the tree.
17//
18
19//
20// Use this class to carry along data from node to node in
21// the traversal
22//
23class TOutputTraverser : public TIntermTraverser {
24public:
25 TOutputTraverser(TInfoSinkBase& i) : sink(i) { }
26 TInfoSinkBase& sink;
27
28protected:
29 void visitSymbol(TIntermSymbol*);
30 void visitConstantUnion(TIntermConstantUnion*);
31 bool visitBinary(Visit visit, TIntermBinary*);
32 bool visitUnary(Visit visit, TIntermUnary*);
33 bool visitSelection(Visit visit, TIntermSelection*);
34 bool visitAggregate(Visit visit, TIntermAggregate*);
35 bool visitLoop(Visit visit, TIntermLoop*);
36 bool visitBranch(Visit visit, TIntermBranch*);
37};
38
39TString TType::getCompleteString() const
40{
41 TStringStream stream;
42
43 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
44 stream << getQualifierString() << " " << getPrecisionString() << " ";
45 if (array)
46 stream << "array of ";
Alexis Hetub14178b2015-04-13 13:23:20 -040047 if (isMatrix())
Alexis Hetuf78e9632015-05-11 16:42:43 -040048 stream << static_cast<int>(primarySize) << "X" << static_cast<int>(secondarySize) << " matrix of ";
Alexis Hetub14178b2015-04-13 13:23:20 -040049 else if(primarySize > 1)
Alexis Hetuf78e9632015-05-11 16:42:43 -040050 stream << static_cast<int>(primarySize) << "-component vector of ";
John Bauman89401822014-05-06 15:04:28 -040051
52 stream << getBasicString();
53 return stream.str();
54}
55
56//
57// Helper functions for printing, not part of traversing.
58//
59
60void OutputTreeText(TInfoSinkBase& sink, TIntermNode* node, const int depth)
61{
62 int i;
63
64 sink.location(node->getLine());
65
66 for (i = 0; i < depth; ++i)
67 sink << " ";
68}
69
70//
71// The rest of the file are the traversal functions. The last one
72// is the one that starts the traversal.
73//
74// Return true from interior nodes to have the external traversal
75// continue on to children. If you process children yourself,
76// return false.
77//
78
79void TOutputTraverser::visitSymbol(TIntermSymbol* node)
80{
Alexis Hetu76a343a2015-06-04 17:21:22 -040081 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -040082
83 sink << "'" << node->getSymbol() << "' ";
84 sink << "(" << node->getCompleteString() << ")\n";
85}
86
87bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary* node)
88{
89 TInfoSinkBase& out = sink;
90
Alexis Hetu76a343a2015-06-04 17:21:22 -040091 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -040092
93 switch (node->getOp()) {
94 case EOpAssign: out << "move second child to first child"; break;
95 case EOpInitialize: out << "initialize first child with second child"; break;
96 case EOpAddAssign: out << "add second child into first child"; break;
97 case EOpSubAssign: out << "subtract second child into first child"; break;
98 case EOpMulAssign: out << "multiply second child into first child"; break;
99 case EOpVectorTimesMatrixAssign: out << "matrix mult second child into first child"; break;
100 case EOpVectorTimesScalarAssign: out << "vector scale second child into first child"; break;
101 case EOpMatrixTimesScalarAssign: out << "matrix scale second child into first child"; break;
Alexis Hetu17809052015-05-13 11:28:22 -0400102 case EOpMatrixTimesMatrixAssign: out << "matrix mult second child into first child"; break;
John Bauman89401822014-05-06 15:04:28 -0400103 case EOpDivAssign: out << "divide second child into first child"; break;
Alexis Hetu17809052015-05-13 11:28:22 -0400104 case EOpIModAssign: out << "modulo second child into first child"; break;
105 case EOpBitShiftLeftAssign: out << "bit-wise shift first child left by second child"; break;
106 case EOpBitShiftRightAssign: out << "bit-wise shift first child right by second child"; break;
107 case EOpBitwiseAndAssign: out << "bit-wise and second child into first child"; break;
108 case EOpBitwiseXorAssign: out << "bit-wise xor second child into first child"; break;
109 case EOpBitwiseOrAssign: out << "bit-wise or second child into first child"; break;
John Bauman89401822014-05-06 15:04:28 -0400110 case EOpIndexDirect: out << "direct index"; break;
111 case EOpIndexIndirect: out << "indirect index"; break;
112 case EOpIndexDirectStruct: out << "direct index for structure"; break;
113 case EOpVectorSwizzle: out << "vector swizzle"; break;
114
115 case EOpAdd: out << "add"; break;
116 case EOpSub: out << "subtract"; break;
117 case EOpMul: out << "component-wise multiply"; break;
118 case EOpDiv: out << "divide"; break;
Alexis Hetu17809052015-05-13 11:28:22 -0400119 case EOpIMod: out << "modulo"; break;
120 case EOpBitShiftLeft: out << "bit-wise shift left"; break;
121 case EOpBitShiftRight: out << "bit-wise shift right"; break;
122 case EOpBitwiseAnd: out << "bit-wise and"; break;
123 case EOpBitwiseXor: out << "bit-wise xor"; break;
124 case EOpBitwiseOr: out << "bit-wise or"; break;
John Bauman89401822014-05-06 15:04:28 -0400125 case EOpEqual: out << "Compare Equal"; break;
126 case EOpNotEqual: out << "Compare Not Equal"; break;
127 case EOpLessThan: out << "Compare Less Than"; break;
128 case EOpGreaterThan: out << "Compare Greater Than"; break;
129 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
130 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
131
132 case EOpVectorTimesScalar: out << "vector-scale"; break;
133 case EOpVectorTimesMatrix: out << "vector-times-matrix"; break;
134 case EOpMatrixTimesVector: out << "matrix-times-vector"; break;
135 case EOpMatrixTimesScalar: out << "matrix-scale"; break;
136 case EOpMatrixTimesMatrix: out << "matrix-multiply"; break;
137
138 case EOpLogicalOr: out << "logical-or"; break;
139 case EOpLogicalXor: out << "logical-xor"; break;
140 case EOpLogicalAnd: out << "logical-and"; break;
141 default: out << "<unknown op>";
142 }
143
144 out << " (" << node->getCompleteString() << ")";
145
146 out << "\n";
147
148 return true;
149}
150
151bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary* node)
152{
153 TInfoSinkBase& out = sink;
154
Alexis Hetu76a343a2015-06-04 17:21:22 -0400155 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400156
157 switch (node->getOp()) {
158 case EOpNegative: out << "Negate value"; break;
159 case EOpVectorLogicalNot:
160 case EOpLogicalNot: out << "Negate conditional"; break;
Alexis Hetu17809052015-05-13 11:28:22 -0400161 case EOpBitwiseNot: out << "bit-wise not"; break;
John Bauman89401822014-05-06 15:04:28 -0400162
163 case EOpPostIncrement: out << "Post-Increment"; break;
164 case EOpPostDecrement: out << "Post-Decrement"; break;
165 case EOpPreIncrement: out << "Pre-Increment"; break;
166 case EOpPreDecrement: out << "Pre-Decrement"; break;
167
John Bauman89401822014-05-06 15:04:28 -0400168 case EOpRadians: out << "radians"; break;
169 case EOpDegrees: out << "degrees"; break;
170 case EOpSin: out << "sine"; break;
171 case EOpCos: out << "cosine"; break;
172 case EOpTan: out << "tangent"; break;
173 case EOpAsin: out << "arc sine"; break;
174 case EOpAcos: out << "arc cosine"; break;
175 case EOpAtan: out << "arc tangent"; break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400176 case EOpSinh: out << "hyperbolic sine"; break;
177 case EOpCosh: out << "hyperbolic cosine"; break;
178 case EOpTanh: out << "hyperbolic tangent"; break;
179 case EOpAsinh: out << "arc hyperbolic sine"; break;
180 case EOpAcosh: out << "arc hyperbolic cosine"; break;
181 case EOpAtanh: out << "arc hyperbolic tangent"; break;
John Bauman89401822014-05-06 15:04:28 -0400182
183 case EOpExp: out << "exp"; break;
184 case EOpLog: out << "log"; break;
185 case EOpExp2: out << "exp2"; break;
186 case EOpLog2: out << "log2"; break;
187 case EOpSqrt: out << "sqrt"; break;
188 case EOpInverseSqrt: out << "inverse sqrt"; break;
189
190 case EOpAbs: out << "Absolute value"; break;
191 case EOpSign: out << "Sign"; break;
192 case EOpFloor: out << "Floor"; break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400193 case EOpTrunc: out << "Trunc"; break;
194 case EOpRound: out << "Round"; break;
195 case EOpRoundEven: out << "RoundEven"; break;
John Bauman89401822014-05-06 15:04:28 -0400196 case EOpCeil: out << "Ceiling"; break;
197 case EOpFract: out << "Fraction"; break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400198 case EOpIsNan: out << "Is not a number"; break;
199 case EOpIsInf: out << "Is infinity"; break;
200
201 case EOpFloatBitsToInt: out << "float bits to int"; break;
202 case EOpFloatBitsToUint: out << "float bits to uint"; break;
203 case EOpIntBitsToFloat: out << "int bits to float"; break;
204 case EOpUintBitsToFloat: out << "uint bits to float"; break;
205
206 case EOpPackSnorm2x16: out << "pack Snorm 2x16"; break;
207 case EOpPackUnorm2x16: out << "pack Unorm 2x16"; break;
208 case EOpPackHalf2x16: out << "pack half 2x16"; break;
209
210 case EOpUnpackSnorm2x16: out << "unpack Snorm 2x16"; break;
211 case EOpUnpackUnorm2x16: out << "unpack Unorm 2x16"; break;
212 case EOpUnpackHalf2x16: out << "unpack half 2x16"; break;
John Bauman89401822014-05-06 15:04:28 -0400213
214 case EOpLength: out << "length"; break;
215 case EOpNormalize: out << "normalize"; break;
216 // case EOpDPdx: out << "dPdx"; break;
217 // case EOpDPdy: out << "dPdy"; break;
218 // case EOpFwidth: out << "fwidth"; break;
219
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400220 case EOpDeterminant: out << "determinant"; break;
221 case EOpTranspose: out << "transpose"; break;
222 case EOpInverse: out << "inverse"; break;
223
John Bauman89401822014-05-06 15:04:28 -0400224 case EOpAny: out << "any"; break;
225 case EOpAll: out << "all"; break;
226
227 default: out.message(EPrefixError, "Bad unary op");
228 }
229
230 out << " (" << node->getCompleteString() << ")";
231
232 out << "\n";
233
234 return true;
235}
236
237bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
238{
239 TInfoSinkBase& out = sink;
240
241 if (node->getOp() == EOpNull) {
242 out.message(EPrefixError, "node is still EOpNull!");
243 return true;
244 }
245
Alexis Hetu76a343a2015-06-04 17:21:22 -0400246 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400247
248 switch (node->getOp()) {
249 case EOpSequence: out << "Sequence\n"; return true;
250 case EOpComma: out << "Comma\n"; return true;
251 case EOpFunction: out << "Function Definition: " << node->getName(); break;
252 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
253 case EOpParameters: out << "Function Parameters: "; break;
254
255 case EOpConstructFloat: out << "Construct float"; break;
256 case EOpConstructVec2: out << "Construct vec2"; break;
257 case EOpConstructVec3: out << "Construct vec3"; break;
258 case EOpConstructVec4: out << "Construct vec4"; break;
259 case EOpConstructBool: out << "Construct bool"; break;
260 case EOpConstructBVec2: out << "Construct bvec2"; break;
261 case EOpConstructBVec3: out << "Construct bvec3"; break;
262 case EOpConstructBVec4: out << "Construct bvec4"; break;
263 case EOpConstructInt: out << "Construct int"; break;
264 case EOpConstructIVec2: out << "Construct ivec2"; break;
265 case EOpConstructIVec3: out << "Construct ivec3"; break;
266 case EOpConstructIVec4: out << "Construct ivec4"; break;
Nicolas Capense4b1b1d2015-02-17 17:26:01 -0500267 case EOpConstructUInt: out << "Construct uint"; break;
268 case EOpConstructUVec2: out << "Construct uvec2"; break;
269 case EOpConstructUVec3: out << "Construct uvec3"; break;
270 case EOpConstructUVec4: out << "Construct uvec4"; break;
John Bauman89401822014-05-06 15:04:28 -0400271 case EOpConstructMat2: out << "Construct mat2"; break;
272 case EOpConstructMat3: out << "Construct mat3"; break;
273 case EOpConstructMat4: out << "Construct mat4"; break;
274 case EOpConstructStruct: out << "Construct structure"; break;
275
276 case EOpLessThan: out << "Compare Less Than"; break;
277 case EOpGreaterThan: out << "Compare Greater Than"; break;
278 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
279 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
280 case EOpVectorEqual: out << "Equal"; break;
281 case EOpVectorNotEqual: out << "NotEqual"; break;
282
283 case EOpMod: out << "mod"; break;
Alexis Hetu7d6b34d2016-05-03 11:30:57 -0400284 case EOpModf: out << "modf"; break;
John Bauman89401822014-05-06 15:04:28 -0400285 case EOpPow: out << "pow"; break;
286
287 case EOpAtan: out << "arc tangent"; break;
288
289 case EOpMin: out << "min"; break;
290 case EOpMax: out << "max"; break;
291 case EOpClamp: out << "clamp"; break;
292 case EOpMix: out << "mix"; break;
293 case EOpStep: out << "step"; break;
294 case EOpSmoothStep: out << "smoothstep"; break;
295
296 case EOpDistance: out << "distance"; break;
297 case EOpDot: out << "dot-product"; break;
298 case EOpCross: out << "cross-product"; break;
299 case EOpFaceForward: out << "face-forward"; break;
300 case EOpReflect: out << "reflect"; break;
301 case EOpRefract: out << "refract"; break;
302 case EOpMul: out << "component-wise multiply"; break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400303 case EOpOuterProduct: out << "outer product"; break;
John Bauman89401822014-05-06 15:04:28 -0400304
305 default: out.message(EPrefixError, "Bad aggregation op");
306 }
307
308 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
309 out << " (" << node->getCompleteString() << ")";
310
311 out << "\n";
312
313 return true;
314}
315
316bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node)
317{
318 TInfoSinkBase& out = sink;
319
Alexis Hetu76a343a2015-06-04 17:21:22 -0400320 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400321
322 out << "Test condition and select";
323 out << " (" << node->getCompleteString() << ")\n";
324
Alexis Hetu76a343a2015-06-04 17:21:22 -0400325 ++mDepth;
John Bauman89401822014-05-06 15:04:28 -0400326
Alexis Hetu76a343a2015-06-04 17:21:22 -0400327 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400328 out << "Condition\n";
329 node->getCondition()->traverse(this);
330
Alexis Hetu76a343a2015-06-04 17:21:22 -0400331 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400332 if (node->getTrueBlock()) {
333 out << "true case\n";
334 node->getTrueBlock()->traverse(this);
335 } else
336 out << "true case is null\n";
337
338 if (node->getFalseBlock()) {
Alexis Hetu76a343a2015-06-04 17:21:22 -0400339 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400340 out << "false case\n";
341 node->getFalseBlock()->traverse(this);
342 }
343
Alexis Hetu76a343a2015-06-04 17:21:22 -0400344 --mDepth;
John Bauman89401822014-05-06 15:04:28 -0400345
346 return false;
347}
348
349void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
350{
351 TInfoSinkBase& out = sink;
352
Alexis Hetuab752792016-04-21 16:11:31 -0400353 size_t size = node->getType().getObjectSize();
John Bauman89401822014-05-06 15:04:28 -0400354
Alexis Hetuab752792016-04-21 16:11:31 -0400355 for(size_t i = 0; i < size; i++) {
Alexis Hetu76a343a2015-06-04 17:21:22 -0400356 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400357 switch (node->getUnionArrayPointer()[i].getType()) {
358 case EbtBool:
359 if (node->getUnionArrayPointer()[i].getBConst())
360 out << "true";
361 else
362 out << "false";
363
364 out << " (" << "const bool" << ")";
365 out << "\n";
366 break;
367 case EbtFloat:
368 out << node->getUnionArrayPointer()[i].getFConst();
369 out << " (const float)\n";
370 break;
371 case EbtInt:
372 out << node->getUnionArrayPointer()[i].getIConst();
373 out << " (const int)\n";
374 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -0500375 case EbtUInt:
376 out << node->getUnionArrayPointer()[i].getUConst();
377 out << " (const uint)\n";
378 break;
John Bauman89401822014-05-06 15:04:28 -0400379 default:
380 out.message(EPrefixInternalError, "Unknown constant", node->getLine());
381 break;
382 }
383 }
384}
385
386bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node)
387{
388 TInfoSinkBase& out = sink;
389
Alexis Hetu76a343a2015-06-04 17:21:22 -0400390 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400391
392 out << "Loop with condition ";
393 if (node->getType() == ELoopDoWhile)
394 out << "not ";
395 out << "tested first\n";
396
Alexis Hetu76a343a2015-06-04 17:21:22 -0400397 ++mDepth;
John Bauman89401822014-05-06 15:04:28 -0400398
Alexis Hetu76a343a2015-06-04 17:21:22 -0400399 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400400 if (node->getCondition()) {
401 out << "Loop Condition\n";
402 node->getCondition()->traverse(this);
403 } else
404 out << "No loop condition\n";
405
Alexis Hetu76a343a2015-06-04 17:21:22 -0400406 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400407 if (node->getBody()) {
408 out << "Loop Body\n";
409 node->getBody()->traverse(this);
410 } else
411 out << "No loop body\n";
412
413 if (node->getExpression()) {
Alexis Hetu76a343a2015-06-04 17:21:22 -0400414 OutputTreeText(sink, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400415 out << "Loop Terminal Expression\n";
416 node->getExpression()->traverse(this);
417 }
418
Alexis Hetu76a343a2015-06-04 17:21:22 -0400419 --mDepth;
John Bauman89401822014-05-06 15:04:28 -0400420
421 return false;
422}
423
424bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node)
425{
426 TInfoSinkBase& out = sink;
427
Alexis Hetu76a343a2015-06-04 17:21:22 -0400428 OutputTreeText(out, node, mDepth);
John Bauman89401822014-05-06 15:04:28 -0400429
430 switch (node->getFlowOp()) {
431 case EOpKill: out << "Branch: Kill"; break;
432 case EOpBreak: out << "Branch: Break"; break;
433 case EOpContinue: out << "Branch: Continue"; break;
434 case EOpReturn: out << "Branch: Return"; break;
435 default: out << "Branch: Unknown Branch"; break;
436 }
437
438 if (node->getExpression()) {
439 out << " with expression\n";
Alexis Hetu76a343a2015-06-04 17:21:22 -0400440 ++mDepth;
John Bauman89401822014-05-06 15:04:28 -0400441 node->getExpression()->traverse(this);
Alexis Hetu76a343a2015-06-04 17:21:22 -0400442 --mDepth;
John Bauman89401822014-05-06 15:04:28 -0400443 } else
444 out << "\n";
445
446 return false;
447}
448
449//
450// This function is the one to call externally to start the traversal.
451// Individual functions can be initialized to 0 to skip processing of that
452// type of node. It's children will still be processed.
453//
454void TIntermediate::outputTree(TIntermNode* root)
455{
456 if (root == 0)
457 return;
458
459 TOutputTraverser it(infoSink.info);
460
461 root->traverse(&it);
462}