blob: 2892830043cf5abd40a9410aaabf26492a085565 [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 ";
47 if (matrix)
48 stream << size << "X" << size << " matrix of ";
49 else if (size > 1)
50 stream << size << "-component vector of ";
51
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{
81 OutputTreeText(sink, node, depth);
82
83 sink << "'" << node->getSymbol() << "' ";
84 sink << "(" << node->getCompleteString() << ")\n";
85}
86
87bool TOutputTraverser::visitBinary(Visit visit, TIntermBinary* node)
88{
89 TInfoSinkBase& out = sink;
90
91 OutputTreeText(out, node, depth);
92
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;
102 case EOpMatrixTimesMatrixAssign: out << "matrix mult second child into first child"; break;
103 case EOpDivAssign: out << "divide second child into first child"; break;
104 case EOpIndexDirect: out << "direct index"; break;
105 case EOpIndexIndirect: out << "indirect index"; break;
106 case EOpIndexDirectStruct: out << "direct index for structure"; break;
107 case EOpVectorSwizzle: out << "vector swizzle"; break;
108
109 case EOpAdd: out << "add"; break;
110 case EOpSub: out << "subtract"; break;
111 case EOpMul: out << "component-wise multiply"; break;
112 case EOpDiv: out << "divide"; break;
113 case EOpEqual: out << "Compare Equal"; break;
114 case EOpNotEqual: out << "Compare Not Equal"; break;
115 case EOpLessThan: out << "Compare Less Than"; break;
116 case EOpGreaterThan: out << "Compare Greater Than"; break;
117 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
118 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
119
120 case EOpVectorTimesScalar: out << "vector-scale"; break;
121 case EOpVectorTimesMatrix: out << "vector-times-matrix"; break;
122 case EOpMatrixTimesVector: out << "matrix-times-vector"; break;
123 case EOpMatrixTimesScalar: out << "matrix-scale"; break;
124 case EOpMatrixTimesMatrix: out << "matrix-multiply"; break;
125
126 case EOpLogicalOr: out << "logical-or"; break;
127 case EOpLogicalXor: out << "logical-xor"; break;
128 case EOpLogicalAnd: out << "logical-and"; break;
129 default: out << "<unknown op>";
130 }
131
132 out << " (" << node->getCompleteString() << ")";
133
134 out << "\n";
135
136 return true;
137}
138
139bool TOutputTraverser::visitUnary(Visit visit, TIntermUnary* node)
140{
141 TInfoSinkBase& out = sink;
142
143 OutputTreeText(out, node, depth);
144
145 switch (node->getOp()) {
146 case EOpNegative: out << "Negate value"; break;
147 case EOpVectorLogicalNot:
148 case EOpLogicalNot: out << "Negate conditional"; break;
149
150 case EOpPostIncrement: out << "Post-Increment"; break;
151 case EOpPostDecrement: out << "Post-Decrement"; break;
152 case EOpPreIncrement: out << "Pre-Increment"; break;
153 case EOpPreDecrement: out << "Pre-Decrement"; break;
154
John Bauman89401822014-05-06 15:04:28 -0400155 case EOpRadians: out << "radians"; break;
156 case EOpDegrees: out << "degrees"; break;
157 case EOpSin: out << "sine"; break;
158 case EOpCos: out << "cosine"; break;
159 case EOpTan: out << "tangent"; break;
160 case EOpAsin: out << "arc sine"; break;
161 case EOpAcos: out << "arc cosine"; break;
162 case EOpAtan: out << "arc tangent"; break;
163
164 case EOpExp: out << "exp"; break;
165 case EOpLog: out << "log"; break;
166 case EOpExp2: out << "exp2"; break;
167 case EOpLog2: out << "log2"; break;
168 case EOpSqrt: out << "sqrt"; break;
169 case EOpInverseSqrt: out << "inverse sqrt"; break;
170
171 case EOpAbs: out << "Absolute value"; break;
172 case EOpSign: out << "Sign"; break;
173 case EOpFloor: out << "Floor"; break;
174 case EOpCeil: out << "Ceiling"; break;
175 case EOpFract: out << "Fraction"; break;
176
177 case EOpLength: out << "length"; break;
178 case EOpNormalize: out << "normalize"; break;
179 // case EOpDPdx: out << "dPdx"; break;
180 // case EOpDPdy: out << "dPdy"; break;
181 // case EOpFwidth: out << "fwidth"; break;
182
183 case EOpAny: out << "any"; break;
184 case EOpAll: out << "all"; break;
185
186 default: out.message(EPrefixError, "Bad unary op");
187 }
188
189 out << " (" << node->getCompleteString() << ")";
190
191 out << "\n";
192
193 return true;
194}
195
196bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
197{
198 TInfoSinkBase& out = sink;
199
200 if (node->getOp() == EOpNull) {
201 out.message(EPrefixError, "node is still EOpNull!");
202 return true;
203 }
204
205 OutputTreeText(out, node, depth);
206
207 switch (node->getOp()) {
208 case EOpSequence: out << "Sequence\n"; return true;
209 case EOpComma: out << "Comma\n"; return true;
210 case EOpFunction: out << "Function Definition: " << node->getName(); break;
211 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
212 case EOpParameters: out << "Function Parameters: "; break;
213
214 case EOpConstructFloat: out << "Construct float"; break;
215 case EOpConstructVec2: out << "Construct vec2"; break;
216 case EOpConstructVec3: out << "Construct vec3"; break;
217 case EOpConstructVec4: out << "Construct vec4"; break;
218 case EOpConstructBool: out << "Construct bool"; break;
219 case EOpConstructBVec2: out << "Construct bvec2"; break;
220 case EOpConstructBVec3: out << "Construct bvec3"; break;
221 case EOpConstructBVec4: out << "Construct bvec4"; break;
222 case EOpConstructInt: out << "Construct int"; break;
223 case EOpConstructIVec2: out << "Construct ivec2"; break;
224 case EOpConstructIVec3: out << "Construct ivec3"; break;
225 case EOpConstructIVec4: out << "Construct ivec4"; break;
Nicolas Capens3c20f802015-02-17 17:17:20 -0500226 case EOpConstructUInt: out << "Construct uint"; break;
John Bauman89401822014-05-06 15:04:28 -0400227 case EOpConstructMat2: out << "Construct mat2"; break;
228 case EOpConstructMat3: out << "Construct mat3"; break;
229 case EOpConstructMat4: out << "Construct mat4"; break;
230 case EOpConstructStruct: out << "Construct structure"; break;
231
232 case EOpLessThan: out << "Compare Less Than"; break;
233 case EOpGreaterThan: out << "Compare Greater Than"; break;
234 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
235 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
236 case EOpVectorEqual: out << "Equal"; break;
237 case EOpVectorNotEqual: out << "NotEqual"; break;
238
239 case EOpMod: out << "mod"; break;
240 case EOpPow: out << "pow"; break;
241
242 case EOpAtan: out << "arc tangent"; break;
243
244 case EOpMin: out << "min"; break;
245 case EOpMax: out << "max"; break;
246 case EOpClamp: out << "clamp"; break;
247 case EOpMix: out << "mix"; break;
248 case EOpStep: out << "step"; break;
249 case EOpSmoothStep: out << "smoothstep"; break;
250
251 case EOpDistance: out << "distance"; break;
252 case EOpDot: out << "dot-product"; break;
253 case EOpCross: out << "cross-product"; break;
254 case EOpFaceForward: out << "face-forward"; break;
255 case EOpReflect: out << "reflect"; break;
256 case EOpRefract: out << "refract"; break;
257 case EOpMul: out << "component-wise multiply"; break;
258
259 default: out.message(EPrefixError, "Bad aggregation op");
260 }
261
262 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
263 out << " (" << node->getCompleteString() << ")";
264
265 out << "\n";
266
267 return true;
268}
269
270bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node)
271{
272 TInfoSinkBase& out = sink;
273
274 OutputTreeText(out, node, depth);
275
276 out << "Test condition and select";
277 out << " (" << node->getCompleteString() << ")\n";
278
279 ++depth;
280
281 OutputTreeText(sink, node, depth);
282 out << "Condition\n";
283 node->getCondition()->traverse(this);
284
285 OutputTreeText(sink, node, depth);
286 if (node->getTrueBlock()) {
287 out << "true case\n";
288 node->getTrueBlock()->traverse(this);
289 } else
290 out << "true case is null\n";
291
292 if (node->getFalseBlock()) {
293 OutputTreeText(sink, node, depth);
294 out << "false case\n";
295 node->getFalseBlock()->traverse(this);
296 }
297
298 --depth;
299
300 return false;
301}
302
303void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
304{
305 TInfoSinkBase& out = sink;
306
307 int size = node->getType().getObjectSize();
308
309 for (int i = 0; i < size; i++) {
310 OutputTreeText(out, node, depth);
311 switch (node->getUnionArrayPointer()[i].getType()) {
312 case EbtBool:
313 if (node->getUnionArrayPointer()[i].getBConst())
314 out << "true";
315 else
316 out << "false";
317
318 out << " (" << "const bool" << ")";
319 out << "\n";
320 break;
321 case EbtFloat:
322 out << node->getUnionArrayPointer()[i].getFConst();
323 out << " (const float)\n";
324 break;
325 case EbtInt:
326 out << node->getUnionArrayPointer()[i].getIConst();
327 out << " (const int)\n";
328 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -0500329 case EbtUInt:
330 out << node->getUnionArrayPointer()[i].getUConst();
331 out << " (const uint)\n";
332 break;
John Bauman89401822014-05-06 15:04:28 -0400333 default:
334 out.message(EPrefixInternalError, "Unknown constant", node->getLine());
335 break;
336 }
337 }
338}
339
340bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node)
341{
342 TInfoSinkBase& out = sink;
343
344 OutputTreeText(out, node, depth);
345
346 out << "Loop with condition ";
347 if (node->getType() == ELoopDoWhile)
348 out << "not ";
349 out << "tested first\n";
350
351 ++depth;
352
353 OutputTreeText(sink, node, depth);
354 if (node->getCondition()) {
355 out << "Loop Condition\n";
356 node->getCondition()->traverse(this);
357 } else
358 out << "No loop condition\n";
359
360 OutputTreeText(sink, node, depth);
361 if (node->getBody()) {
362 out << "Loop Body\n";
363 node->getBody()->traverse(this);
364 } else
365 out << "No loop body\n";
366
367 if (node->getExpression()) {
368 OutputTreeText(sink, node, depth);
369 out << "Loop Terminal Expression\n";
370 node->getExpression()->traverse(this);
371 }
372
373 --depth;
374
375 return false;
376}
377
378bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node)
379{
380 TInfoSinkBase& out = sink;
381
382 OutputTreeText(out, node, depth);
383
384 switch (node->getFlowOp()) {
385 case EOpKill: out << "Branch: Kill"; break;
386 case EOpBreak: out << "Branch: Break"; break;
387 case EOpContinue: out << "Branch: Continue"; break;
388 case EOpReturn: out << "Branch: Return"; break;
389 default: out << "Branch: Unknown Branch"; break;
390 }
391
392 if (node->getExpression()) {
393 out << " with expression\n";
394 ++depth;
395 node->getExpression()->traverse(this);
396 --depth;
397 } else
398 out << "\n";
399
400 return false;
401}
402
403//
404// This function is the one to call externally to start the traversal.
405// Individual functions can be initialized to 0 to skip processing of that
406// type of node. It's children will still be processed.
407//
408void TIntermediate::outputTree(TIntermNode* root)
409{
410 if (root == 0)
411 return;
412
413 TOutputTraverser it(infoSink.info);
414
415 root->traverse(&it);
416}