blob: f71eea43b5bcbf622bce1e06a0173ea306adfd16 [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())
48 stream << primarySize << "X" << secondarySize << " matrix of ";
49 else if(primarySize > 1)
50 stream << 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{
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 Capense4b1b1d2015-02-17 17:26:01 -0500226 case EOpConstructUInt: out << "Construct uint"; break;
227 case EOpConstructUVec2: out << "Construct uvec2"; break;
228 case EOpConstructUVec3: out << "Construct uvec3"; break;
229 case EOpConstructUVec4: out << "Construct uvec4"; break;
John Bauman89401822014-05-06 15:04:28 -0400230 case EOpConstructMat2: out << "Construct mat2"; break;
231 case EOpConstructMat3: out << "Construct mat3"; break;
232 case EOpConstructMat4: out << "Construct mat4"; break;
233 case EOpConstructStruct: out << "Construct structure"; break;
234
235 case EOpLessThan: out << "Compare Less Than"; break;
236 case EOpGreaterThan: out << "Compare Greater Than"; break;
237 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
238 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
239 case EOpVectorEqual: out << "Equal"; break;
240 case EOpVectorNotEqual: out << "NotEqual"; break;
241
242 case EOpMod: out << "mod"; break;
243 case EOpPow: out << "pow"; break;
244
245 case EOpAtan: out << "arc tangent"; break;
246
247 case EOpMin: out << "min"; break;
248 case EOpMax: out << "max"; break;
249 case EOpClamp: out << "clamp"; break;
250 case EOpMix: out << "mix"; break;
251 case EOpStep: out << "step"; break;
252 case EOpSmoothStep: out << "smoothstep"; break;
253
254 case EOpDistance: out << "distance"; break;
255 case EOpDot: out << "dot-product"; break;
256 case EOpCross: out << "cross-product"; break;
257 case EOpFaceForward: out << "face-forward"; break;
258 case EOpReflect: out << "reflect"; break;
259 case EOpRefract: out << "refract"; break;
260 case EOpMul: out << "component-wise multiply"; break;
261
262 default: out.message(EPrefixError, "Bad aggregation op");
263 }
264
265 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
266 out << " (" << node->getCompleteString() << ")";
267
268 out << "\n";
269
270 return true;
271}
272
273bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node)
274{
275 TInfoSinkBase& out = sink;
276
277 OutputTreeText(out, node, depth);
278
279 out << "Test condition and select";
280 out << " (" << node->getCompleteString() << ")\n";
281
282 ++depth;
283
284 OutputTreeText(sink, node, depth);
285 out << "Condition\n";
286 node->getCondition()->traverse(this);
287
288 OutputTreeText(sink, node, depth);
289 if (node->getTrueBlock()) {
290 out << "true case\n";
291 node->getTrueBlock()->traverse(this);
292 } else
293 out << "true case is null\n";
294
295 if (node->getFalseBlock()) {
296 OutputTreeText(sink, node, depth);
297 out << "false case\n";
298 node->getFalseBlock()->traverse(this);
299 }
300
301 --depth;
302
303 return false;
304}
305
306void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
307{
308 TInfoSinkBase& out = sink;
309
310 int size = node->getType().getObjectSize();
311
312 for (int i = 0; i < size; i++) {
313 OutputTreeText(out, node, depth);
314 switch (node->getUnionArrayPointer()[i].getType()) {
315 case EbtBool:
316 if (node->getUnionArrayPointer()[i].getBConst())
317 out << "true";
318 else
319 out << "false";
320
321 out << " (" << "const bool" << ")";
322 out << "\n";
323 break;
324 case EbtFloat:
325 out << node->getUnionArrayPointer()[i].getFConst();
326 out << " (const float)\n";
327 break;
328 case EbtInt:
329 out << node->getUnionArrayPointer()[i].getIConst();
330 out << " (const int)\n";
331 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -0500332 case EbtUInt:
333 out << node->getUnionArrayPointer()[i].getUConst();
334 out << " (const uint)\n";
335 break;
John Bauman89401822014-05-06 15:04:28 -0400336 default:
337 out.message(EPrefixInternalError, "Unknown constant", node->getLine());
338 break;
339 }
340 }
341}
342
343bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node)
344{
345 TInfoSinkBase& out = sink;
346
347 OutputTreeText(out, node, depth);
348
349 out << "Loop with condition ";
350 if (node->getType() == ELoopDoWhile)
351 out << "not ";
352 out << "tested first\n";
353
354 ++depth;
355
356 OutputTreeText(sink, node, depth);
357 if (node->getCondition()) {
358 out << "Loop Condition\n";
359 node->getCondition()->traverse(this);
360 } else
361 out << "No loop condition\n";
362
363 OutputTreeText(sink, node, depth);
364 if (node->getBody()) {
365 out << "Loop Body\n";
366 node->getBody()->traverse(this);
367 } else
368 out << "No loop body\n";
369
370 if (node->getExpression()) {
371 OutputTreeText(sink, node, depth);
372 out << "Loop Terminal Expression\n";
373 node->getExpression()->traverse(this);
374 }
375
376 --depth;
377
378 return false;
379}
380
381bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node)
382{
383 TInfoSinkBase& out = sink;
384
385 OutputTreeText(out, node, depth);
386
387 switch (node->getFlowOp()) {
388 case EOpKill: out << "Branch: Kill"; break;
389 case EOpBreak: out << "Branch: Break"; break;
390 case EOpContinue: out << "Branch: Continue"; break;
391 case EOpReturn: out << "Branch: Return"; break;
392 default: out << "Branch: Unknown Branch"; break;
393 }
394
395 if (node->getExpression()) {
396 out << " with expression\n";
397 ++depth;
398 node->getExpression()->traverse(this);
399 --depth;
400 } else
401 out << "\n";
402
403 return false;
404}
405
406//
407// This function is the one to call externally to start the traversal.
408// Individual functions can be initialized to 0 to skip processing of that
409// type of node. It's children will still be processed.
410//
411void TIntermediate::outputTree(TIntermNode* root)
412{
413 if (root == 0)
414 return;
415
416 TOutputTraverser it(infoSink.info);
417
418 root->traverse(&it);
419}