blob: 857233015d59c4d06e1e56437592e34af5d0ed72 [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
7#include "compiler/localintermediate.h"
John Baumand4ae8632014-05-06 16:18:33 -04008#include "compiler/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
155 case EOpConvIntToBool: out << "Convert int to bool"; break;
156 case EOpConvFloatToBool:out << "Convert float to bool";break;
157 case EOpConvBoolToFloat:out << "Convert bool to float";break;
158 case EOpConvIntToFloat: out << "Convert int to float"; break;
159 case EOpConvFloatToInt: out << "Convert float to int"; break;
160 case EOpConvBoolToInt: out << "Convert bool to int"; break;
161
162 case EOpRadians: out << "radians"; break;
163 case EOpDegrees: out << "degrees"; break;
164 case EOpSin: out << "sine"; break;
165 case EOpCos: out << "cosine"; break;
166 case EOpTan: out << "tangent"; break;
167 case EOpAsin: out << "arc sine"; break;
168 case EOpAcos: out << "arc cosine"; break;
169 case EOpAtan: out << "arc tangent"; break;
170
171 case EOpExp: out << "exp"; break;
172 case EOpLog: out << "log"; break;
173 case EOpExp2: out << "exp2"; break;
174 case EOpLog2: out << "log2"; break;
175 case EOpSqrt: out << "sqrt"; break;
176 case EOpInverseSqrt: out << "inverse sqrt"; break;
177
178 case EOpAbs: out << "Absolute value"; break;
179 case EOpSign: out << "Sign"; break;
180 case EOpFloor: out << "Floor"; break;
181 case EOpCeil: out << "Ceiling"; break;
182 case EOpFract: out << "Fraction"; break;
183
184 case EOpLength: out << "length"; break;
185 case EOpNormalize: out << "normalize"; break;
186 // case EOpDPdx: out << "dPdx"; break;
187 // case EOpDPdy: out << "dPdy"; break;
188 // case EOpFwidth: out << "fwidth"; break;
189
190 case EOpAny: out << "any"; break;
191 case EOpAll: out << "all"; break;
192
193 default: out.message(EPrefixError, "Bad unary op");
194 }
195
196 out << " (" << node->getCompleteString() << ")";
197
198 out << "\n";
199
200 return true;
201}
202
203bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
204{
205 TInfoSinkBase& out = sink;
206
207 if (node->getOp() == EOpNull) {
208 out.message(EPrefixError, "node is still EOpNull!");
209 return true;
210 }
211
212 OutputTreeText(out, node, depth);
213
214 switch (node->getOp()) {
215 case EOpSequence: out << "Sequence\n"; return true;
216 case EOpComma: out << "Comma\n"; return true;
217 case EOpFunction: out << "Function Definition: " << node->getName(); break;
218 case EOpFunctionCall: out << "Function Call: " << node->getName(); break;
219 case EOpParameters: out << "Function Parameters: "; break;
220
221 case EOpConstructFloat: out << "Construct float"; break;
222 case EOpConstructVec2: out << "Construct vec2"; break;
223 case EOpConstructVec3: out << "Construct vec3"; break;
224 case EOpConstructVec4: out << "Construct vec4"; break;
225 case EOpConstructBool: out << "Construct bool"; break;
226 case EOpConstructBVec2: out << "Construct bvec2"; break;
227 case EOpConstructBVec3: out << "Construct bvec3"; break;
228 case EOpConstructBVec4: out << "Construct bvec4"; break;
229 case EOpConstructInt: out << "Construct int"; break;
230 case EOpConstructIVec2: out << "Construct ivec2"; break;
231 case EOpConstructIVec3: out << "Construct ivec3"; break;
232 case EOpConstructIVec4: out << "Construct ivec4"; break;
233 case EOpConstructMat2: out << "Construct mat2"; break;
234 case EOpConstructMat3: out << "Construct mat3"; break;
235 case EOpConstructMat4: out << "Construct mat4"; break;
236 case EOpConstructStruct: out << "Construct structure"; break;
237
238 case EOpLessThan: out << "Compare Less Than"; break;
239 case EOpGreaterThan: out << "Compare Greater Than"; break;
240 case EOpLessThanEqual: out << "Compare Less Than or Equal"; break;
241 case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break;
242 case EOpVectorEqual: out << "Equal"; break;
243 case EOpVectorNotEqual: out << "NotEqual"; break;
244
245 case EOpMod: out << "mod"; break;
246 case EOpPow: out << "pow"; break;
247
248 case EOpAtan: out << "arc tangent"; break;
249
250 case EOpMin: out << "min"; break;
251 case EOpMax: out << "max"; break;
252 case EOpClamp: out << "clamp"; break;
253 case EOpMix: out << "mix"; break;
254 case EOpStep: out << "step"; break;
255 case EOpSmoothStep: out << "smoothstep"; break;
256
257 case EOpDistance: out << "distance"; break;
258 case EOpDot: out << "dot-product"; break;
259 case EOpCross: out << "cross-product"; break;
260 case EOpFaceForward: out << "face-forward"; break;
261 case EOpReflect: out << "reflect"; break;
262 case EOpRefract: out << "refract"; break;
263 case EOpMul: out << "component-wise multiply"; break;
264
265 default: out.message(EPrefixError, "Bad aggregation op");
266 }
267
268 if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
269 out << " (" << node->getCompleteString() << ")";
270
271 out << "\n";
272
273 return true;
274}
275
276bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node)
277{
278 TInfoSinkBase& out = sink;
279
280 OutputTreeText(out, node, depth);
281
282 out << "Test condition and select";
283 out << " (" << node->getCompleteString() << ")\n";
284
285 ++depth;
286
287 OutputTreeText(sink, node, depth);
288 out << "Condition\n";
289 node->getCondition()->traverse(this);
290
291 OutputTreeText(sink, node, depth);
292 if (node->getTrueBlock()) {
293 out << "true case\n";
294 node->getTrueBlock()->traverse(this);
295 } else
296 out << "true case is null\n";
297
298 if (node->getFalseBlock()) {
299 OutputTreeText(sink, node, depth);
300 out << "false case\n";
301 node->getFalseBlock()->traverse(this);
302 }
303
304 --depth;
305
306 return false;
307}
308
309void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
310{
311 TInfoSinkBase& out = sink;
312
313 int size = node->getType().getObjectSize();
314
315 for (int i = 0; i < size; i++) {
316 OutputTreeText(out, node, depth);
317 switch (node->getUnionArrayPointer()[i].getType()) {
318 case EbtBool:
319 if (node->getUnionArrayPointer()[i].getBConst())
320 out << "true";
321 else
322 out << "false";
323
324 out << " (" << "const bool" << ")";
325 out << "\n";
326 break;
327 case EbtFloat:
328 out << node->getUnionArrayPointer()[i].getFConst();
329 out << " (const float)\n";
330 break;
331 case EbtInt:
332 out << node->getUnionArrayPointer()[i].getIConst();
333 out << " (const int)\n";
334 break;
335 default:
336 out.message(EPrefixInternalError, "Unknown constant", node->getLine());
337 break;
338 }
339 }
340}
341
342bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node)
343{
344 TInfoSinkBase& out = sink;
345
346 OutputTreeText(out, node, depth);
347
348 out << "Loop with condition ";
349 if (node->getType() == ELoopDoWhile)
350 out << "not ";
351 out << "tested first\n";
352
353 ++depth;
354
355 OutputTreeText(sink, node, depth);
356 if (node->getCondition()) {
357 out << "Loop Condition\n";
358 node->getCondition()->traverse(this);
359 } else
360 out << "No loop condition\n";
361
362 OutputTreeText(sink, node, depth);
363 if (node->getBody()) {
364 out << "Loop Body\n";
365 node->getBody()->traverse(this);
366 } else
367 out << "No loop body\n";
368
369 if (node->getExpression()) {
370 OutputTreeText(sink, node, depth);
371 out << "Loop Terminal Expression\n";
372 node->getExpression()->traverse(this);
373 }
374
375 --depth;
376
377 return false;
378}
379
380bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node)
381{
382 TInfoSinkBase& out = sink;
383
384 OutputTreeText(out, node, depth);
385
386 switch (node->getFlowOp()) {
387 case EOpKill: out << "Branch: Kill"; break;
388 case EOpBreak: out << "Branch: Break"; break;
389 case EOpContinue: out << "Branch: Continue"; break;
390 case EOpReturn: out << "Branch: Return"; break;
391 default: out << "Branch: Unknown Branch"; break;
392 }
393
394 if (node->getExpression()) {
395 out << " with expression\n";
396 ++depth;
397 node->getExpression()->traverse(this);
398 --depth;
399 } else
400 out << "\n";
401
402 return false;
403}
404
405//
406// This function is the one to call externally to start the traversal.
407// Individual functions can be initialized to 0 to skip processing of that
408// type of node. It's children will still be processed.
409//
410void TIntermediate::outputTree(TIntermNode* root)
411{
412 if (root == 0)
413 return;
414
415 TOutputTraverser it(infoSink.info);
416
417 root->traverse(&it);
418}