Merge pull request #230 from dekimir/incresconst
[platform/upstream/glslang.git] / glslang / MachineIndependent / intermOut.cpp
1 //
2 //Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 //Copyright (C) 2012-2013 LunarG, Inc.
4 //
5 //All rights reserved.
6 //
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
9 //are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //POSSIBILITY OF SUCH DAMAGE.
35 //
36
37 #include "localintermediate.h"
38 #include "../Include/InfoSink.h"
39
40 #ifdef _MSC_VER
41 #include <float.h>
42 #else
43 #include <math.h>
44 #endif
45
46 namespace {
47
48 bool is_positive_infinity(double x) {
49 #ifdef _MSC_VER
50   return _fpclass(x) == _FPCLASS_PINF;
51 #elif defined __ANDROID__ || defined __linux__ || __MINGW32__ || __MINGW64__
52   return std::isinf(x) && (x >= 0);
53 #else
54   return isinf(x) && (x >= 0);
55 #endif
56 }
57
58 }
59
60 namespace glslang {
61
62 //
63 // Two purposes:
64 // 1.  Show an example of how to iterate tree.  Functions can
65 //     also directly call Traverse() on children themselves to
66 //     have finer grained control over the process than shown here.
67 //     See the last function for how to get started.
68 // 2.  Print out a text based description of the tree.
69 //
70
71 //
72 // Use this class to carry along data from node to node in
73 // the traversal
74 //
75 class TOutputTraverser : public TIntermTraverser {
76 public:
77     TOutputTraverser(TInfoSink& i) : infoSink(i) { }
78
79     virtual bool visitBinary(TVisit, TIntermBinary* node);
80     virtual bool visitUnary(TVisit, TIntermUnary* node);
81     virtual bool visitAggregate(TVisit, TIntermAggregate* node);
82     virtual bool visitSelection(TVisit, TIntermSelection* node);
83     virtual void visitConstantUnion(TIntermConstantUnion* node);
84     virtual void visitSymbol(TIntermSymbol* node);
85     virtual bool visitLoop(TVisit, TIntermLoop* node);
86     virtual bool visitBranch(TVisit, TIntermBranch* node);
87     virtual bool visitSwitch(TVisit, TIntermSwitch* node);
88
89     TInfoSink& infoSink;
90 protected:
91     TOutputTraverser(TOutputTraverser&);
92     TOutputTraverser& operator=(TOutputTraverser&);
93 };
94
95 //
96 // Helper functions for printing, not part of traversing.
97 //
98
99 static void OutputTreeText(TInfoSink& infoSink, const TIntermNode* node, const int depth)
100 {
101     int i;
102
103     infoSink.debug << node->getLoc().string << ":";
104     if (node->getLoc().line)
105         infoSink.debug << node->getLoc().line;
106     else
107         infoSink.debug << "? ";
108
109     for (i = 0; i < depth; ++i)
110         infoSink.debug << "  ";
111 }
112
113 //
114 // The rest of the file are the traversal functions.  The last one
115 // is the one that starts the traversal.
116 //
117 // Return true from interior nodes to have the external traversal
118 // continue on to children.  If you process children yourself,
119 // return false.
120 //
121
122 bool TOutputTraverser::visitBinary(TVisit /* visit */, TIntermBinary* node)
123 {
124     TInfoSink& out = infoSink;
125
126     OutputTreeText(out, node, depth);
127
128     switch (node->getOp()) {
129     case EOpAssign:                   out.debug << "move second child to first child";           break;
130     case EOpAddAssign:                out.debug << "add second child into first child";          break;
131     case EOpSubAssign:                out.debug << "subtract second child into first child";     break;
132     case EOpMulAssign:                out.debug << "multiply second child into first child";     break;
133     case EOpVectorTimesMatrixAssign:  out.debug << "matrix mult second child into first child";  break;
134     case EOpVectorTimesScalarAssign:  out.debug << "vector scale second child into first child"; break;
135     case EOpMatrixTimesScalarAssign:  out.debug << "matrix scale second child into first child"; break;
136     case EOpMatrixTimesMatrixAssign:  out.debug << "matrix mult second child into first child";  break;
137     case EOpDivAssign:                out.debug << "divide second child into first child";       break;
138     case EOpModAssign:                out.debug << "mod second child into first child";          break;
139     case EOpAndAssign:                out.debug << "and second child into first child";          break;
140     case EOpInclusiveOrAssign:        out.debug << "or second child into first child";           break;
141     case EOpExclusiveOrAssign:        out.debug << "exclusive or second child into first child"; break;
142     case EOpLeftShiftAssign:          out.debug << "left shift second child into first child";   break;
143     case EOpRightShiftAssign:         out.debug << "right shift second child into first child";  break;
144
145     case EOpIndexDirect:   out.debug << "direct index";   break;
146     case EOpIndexIndirect: out.debug << "indirect index"; break;
147     case EOpIndexDirectStruct:
148         out.debug << (*node->getLeft()->getType().getStruct())[node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst()].type->getFieldName();
149         out.debug << ": direct index for structure";      break;
150     case EOpVectorSwizzle: out.debug << "vector swizzle"; break;
151
152     case EOpAdd:    out.debug << "add";                     break;
153     case EOpSub:    out.debug << "subtract";                break;
154     case EOpMul:    out.debug << "component-wise multiply"; break;
155     case EOpDiv:    out.debug << "divide";                  break;
156     case EOpMod:    out.debug << "mod";                     break;
157     case EOpRightShift:  out.debug << "right-shift";  break;
158     case EOpLeftShift:   out.debug << "left-shift";   break;
159     case EOpAnd:         out.debug << "bitwise and";  break;
160     case EOpInclusiveOr: out.debug << "inclusive-or"; break;
161     case EOpExclusiveOr: out.debug << "exclusive-or"; break;
162     case EOpEqual:            out.debug << "Compare Equal";                 break;
163     case EOpNotEqual:         out.debug << "Compare Not Equal";             break;
164     case EOpLessThan:         out.debug << "Compare Less Than";             break;
165     case EOpGreaterThan:      out.debug << "Compare Greater Than";          break;
166     case EOpLessThanEqual:    out.debug << "Compare Less Than or Equal";    break;
167     case EOpGreaterThanEqual: out.debug << "Compare Greater Than or Equal"; break;
168
169     case EOpVectorTimesScalar: out.debug << "vector-scale";          break;
170     case EOpVectorTimesMatrix: out.debug << "vector-times-matrix";   break;
171     case EOpMatrixTimesVector: out.debug << "matrix-times-vector";   break;
172     case EOpMatrixTimesScalar: out.debug << "matrix-scale";          break;
173     case EOpMatrixTimesMatrix: out.debug << "matrix-multiply";       break;
174
175     case EOpLogicalOr:  out.debug << "logical-or";   break;
176     case EOpLogicalXor: out.debug << "logical-xor"; break;
177     case EOpLogicalAnd: out.debug << "logical-and"; break;
178     default: out.debug << "<unknown op>";
179     }
180
181     out.debug << " (" << node->getCompleteString() << ")";
182
183     out.debug << "\n";
184
185     return true;
186 }
187
188 bool TOutputTraverser::visitUnary(TVisit /* visit */, TIntermUnary* node)
189 {
190     TInfoSink& out = infoSink;
191
192     OutputTreeText(out, node, depth);
193
194     switch (node->getOp()) {
195     case EOpNegative:       out.debug << "Negate value";         break;
196     case EOpVectorLogicalNot:
197     case EOpLogicalNot:     out.debug << "Negate conditional";   break;
198     case EOpBitwiseNot:     out.debug << "Bitwise not";          break;
199
200     case EOpPostIncrement:  out.debug << "Post-Increment";       break;
201     case EOpPostDecrement:  out.debug << "Post-Decrement";       break;
202     case EOpPreIncrement:   out.debug << "Pre-Increment";        break;
203     case EOpPreDecrement:   out.debug << "Pre-Decrement";        break;
204
205     case EOpConvIntToBool:     out.debug << "Convert int to bool";     break;
206     case EOpConvUintToBool:    out.debug << "Convert uint to bool";    break;
207     case EOpConvFloatToBool:   out.debug << "Convert float to bool";   break;
208     case EOpConvDoubleToBool:  out.debug << "Convert double to bool";  break;
209     case EOpConvInt64ToBool:   out.debug << "Convert int64 to bool";   break;
210     case EOpConvUint64ToBool:  out.debug << "Convert uint64 to bool";  break;
211     case EOpConvIntToFloat:    out.debug << "Convert int to float";    break;
212     case EOpConvUintToFloat:   out.debug << "Convert uint to float";   break;
213     case EOpConvDoubleToFloat: out.debug << "Convert double to float"; break;
214     case EOpConvInt64ToFloat:  out.debug << "Convert int64 to float";  break;
215     case EOpConvUint64ToFloat: out.debug << "Convert uint64 to float"; break;
216     case EOpConvBoolToFloat:   out.debug << "Convert bool to float";   break;
217     case EOpConvUintToInt:     out.debug << "Convert uint to int";     break;
218     case EOpConvFloatToInt:    out.debug << "Convert float to int";    break;
219     case EOpConvDoubleToInt:   out.debug << "Convert double to int";   break;
220     case EOpConvBoolToInt:     out.debug << "Convert bool to int";     break;
221     case EOpConvInt64ToInt:    out.debug << "Convert int64 to int";    break;
222     case EOpConvUint64ToInt:   out.debug << "Convert uint64 to int";   break;
223     case EOpConvIntToUint:     out.debug << "Convert int to uint";     break;
224     case EOpConvFloatToUint:   out.debug << "Convert float to uint";   break;
225     case EOpConvDoubleToUint:  out.debug << "Convert double to uint";  break;
226     case EOpConvBoolToUint:    out.debug << "Convert bool to uint";    break;
227     case EOpConvInt64ToUint:   out.debug << "Convert int64 to uint";   break;
228     case EOpConvUint64ToUint:  out.debug << "Convert uint64 to uint";  break;
229     case EOpConvIntToDouble:   out.debug << "Convert int to double";   break;
230     case EOpConvUintToDouble:  out.debug << "Convert uint to double";  break;
231     case EOpConvFloatToDouble: out.debug << "Convert float to double"; break;
232     case EOpConvBoolToDouble:  out.debug << "Convert bool to double";  break;
233     case EOpConvInt64ToDouble: out.debug << "Convert int64 to double"; break;
234     case EOpConvUint64ToDouble: out.debug << "Convert uint64 to double";  break;
235     case EOpConvBoolToInt64:   out.debug << "Convert bool to int64";   break;
236     case EOpConvIntToInt64:    out.debug << "Convert int to int64";    break;
237     case EOpConvUintToInt64:   out.debug << "Convert uint to int64";   break;
238     case EOpConvFloatToInt64:  out.debug << "Convert float to int64";  break;
239     case EOpConvDoubleToInt64: out.debug << "Convert double to int64"; break;
240     case EOpConvUint64ToInt64: out.debug << "Convert uint64 to int64"; break;
241     case EOpConvBoolToUint64:  out.debug << "Convert bool to uint64";  break;
242     case EOpConvIntToUint64:   out.debug << "Convert int to uint64";   break;
243     case EOpConvUintToUint64:  out.debug << "Convert uint to uint64";  break;
244     case EOpConvFloatToUint64: out.debug << "Convert float to uint64"; break;
245     case EOpConvDoubleToUint64: out.debug << "Convert double to uint64"; break;
246     case EOpConvInt64ToUint64: out.debug << "Convert uint64 to uint64"; break;
247
248     case EOpRadians:        out.debug << "radians";              break;
249     case EOpDegrees:        out.debug << "degrees";              break;
250     case EOpSin:            out.debug << "sine";                 break;
251     case EOpCos:            out.debug << "cosine";               break;
252     case EOpTan:            out.debug << "tangent";              break;
253     case EOpAsin:           out.debug << "arc sine";             break;
254     case EOpAcos:           out.debug << "arc cosine";           break;
255     case EOpAtan:           out.debug << "arc tangent";          break;
256     case EOpSinh:           out.debug << "hyp. sine";            break;
257     case EOpCosh:           out.debug << "hyp. cosine";          break;
258     case EOpTanh:           out.debug << "hyp. tangent";         break;
259     case EOpAsinh:          out.debug << "arc hyp. sine";        break;
260     case EOpAcosh:          out.debug << "arc hyp. cosine";      break;
261     case EOpAtanh:          out.debug << "arc hyp. tangent";     break;
262
263     case EOpExp:            out.debug << "exp";                  break;
264     case EOpLog:            out.debug << "log";                  break;
265     case EOpExp2:           out.debug << "exp2";                 break;
266     case EOpLog2:           out.debug << "log2";                 break;
267     case EOpSqrt:           out.debug << "sqrt";                 break;
268     case EOpInverseSqrt:    out.debug << "inverse sqrt";         break;
269
270     case EOpAbs:            out.debug << "Absolute value";       break;
271     case EOpSign:           out.debug << "Sign";                 break;
272     case EOpFloor:          out.debug << "Floor";                break;
273     case EOpTrunc:          out.debug << "trunc";                break;
274     case EOpRound:          out.debug << "round";                break;
275     case EOpRoundEven:      out.debug << "roundEven";            break;
276     case EOpCeil:           out.debug << "Ceiling";              break;
277     case EOpFract:          out.debug << "Fraction";             break;
278
279     case EOpIsNan:          out.debug << "isnan";                break;
280     case EOpIsInf:          out.debug << "isinf";                break;
281
282     case EOpFloatBitsToInt: out.debug << "floatBitsToInt";       break;
283     case EOpFloatBitsToUint:out.debug << "floatBitsToUint";      break;
284     case EOpIntBitsToFloat: out.debug << "intBitsToFloat";       break;
285     case EOpUintBitsToFloat:out.debug << "uintBitsToFloat";      break;
286     case EOpDoubleBitsToInt64:  out.debug << "doubleBitsToInt64";  break;
287     case EOpDoubleBitsToUint64: out.debug << "doubleBitsToUint64"; break;
288     case EOpInt64BitsToDouble:  out.debug << "int64BitsToDouble";  break;
289     case EOpUint64BitsToDouble: out.debug << "uint64BitsToDouble"; break;
290     case EOpPackSnorm2x16:  out.debug << "packSnorm2x16";        break;
291     case EOpUnpackSnorm2x16:out.debug << "unpackSnorm2x16";      break;
292     case EOpPackUnorm2x16:  out.debug << "packUnorm2x16";        break;
293     case EOpUnpackUnorm2x16:out.debug << "unpackUnorm2x16";      break;
294     case EOpPackHalf2x16:   out.debug << "packHalf2x16";         break;
295     case EOpUnpackHalf2x16: out.debug << "unpackHalf2x16";       break;
296
297     case EOpPackSnorm4x8:     out.debug << "PackSnorm4x8";       break;
298     case EOpUnpackSnorm4x8:   out.debug << "UnpackSnorm4x8";     break;
299     case EOpPackUnorm4x8:     out.debug << "PackUnorm4x8";       break;
300     case EOpUnpackUnorm4x8:   out.debug << "UnpackUnorm4x8";     break;
301     case EOpPackDouble2x32:   out.debug << "PackDouble2x32";     break;
302     case EOpUnpackDouble2x32: out.debug << "UnpackDouble2x32";   break;
303
304     case EOpPackInt2x32:      out.debug << "packInt2x32";        break;
305     case EOpUnpackInt2x32:    out.debug << "unpackInt2x32";      break;
306     case EOpPackUint2x32:     out.debug << "packUint2x32";       break;
307     case EOpUnpackUint2x32:   out.debug << "unpackUint2x32";     break;
308
309     case EOpLength:         out.debug << "length";               break;
310     case EOpNormalize:      out.debug << "normalize";            break;
311     case EOpDPdx:           out.debug << "dPdx";                 break;
312     case EOpDPdy:           out.debug << "dPdy";                 break;
313     case EOpFwidth:         out.debug << "fwidth";               break;
314     case EOpDPdxFine:       out.debug << "dPdxFine";             break;
315     case EOpDPdyFine:       out.debug << "dPdyFine";             break;
316     case EOpFwidthFine:     out.debug << "fwidthFine";           break;
317     case EOpDPdxCoarse:     out.debug << "dPdxCoarse";           break;
318     case EOpDPdyCoarse:     out.debug << "dPdyCoarse";           break;
319     case EOpFwidthCoarse:   out.debug << "fwidthCoarse";         break;
320
321     case EOpInterpolateAtCentroid: out.debug << "interpolateAtCentroid";  break;
322
323     case EOpDeterminant:    out.debug << "determinant";          break;
324     case EOpMatrixInverse:  out.debug << "inverse";              break;
325     case EOpTranspose:      out.debug << "transpose";            break;
326
327     case EOpAny:            out.debug << "any";                  break;
328     case EOpAll:            out.debug << "all";                  break;
329
330     case EOpArrayLength:    out.debug << "array length";         break;
331
332     case EOpEmitStreamVertex:   out.debug << "EmitStreamVertex";   break;
333     case EOpEndStreamPrimitive: out.debug << "EndStreamPrimitive"; break;
334
335     case EOpAtomicCounterIncrement: out.debug << "AtomicCounterIncrement";break;
336     case EOpAtomicCounterDecrement: out.debug << "AtomicCounterDecrement";break;
337     case EOpAtomicCounter:          out.debug << "AtomicCounter";         break;
338
339     case EOpTextureQuerySize:       out.debug << "textureSize";           break;
340     case EOpTextureQueryLod:        out.debug << "textureQueryLod";       break;
341     case EOpTextureQueryLevels:     out.debug << "textureQueryLevels";    break;
342     case EOpTextureQuerySamples:    out.debug << "textureSamples";        break;
343     case EOpImageQuerySize:         out.debug << "imageQuerySize";        break;
344     case EOpImageQuerySamples:      out.debug << "imageQuerySamples";     break;
345     case EOpImageLoad:              out.debug << "imageLoad";             break;
346
347     case EOpBitFieldReverse:        out.debug << "bitFieldReverse";       break;
348     case EOpBitCount:               out.debug << "bitCount";              break;
349     case EOpFindLSB:                out.debug << "findLSB";               break;
350     case EOpFindMSB:                out.debug << "findMSB";               break;
351
352     case EOpNoise:                  out.debug << "noise";                 break;
353
354     case EOpBallot:                 out.debug << "ballot";                break;
355     case EOpReadFirstInvocation:    out.debug << "readFirstInvocation";   break;
356     case EOpAnyInvocation:          out.debug << "anyInvocation";         break;
357     case EOpAllInvocations:         out.debug << "allInvocations";        break;
358     case EOpAllInvocationsEqual:    out.debug << "allInvocationsEqual";   break;
359
360     default: out.debug.message(EPrefixError, "Bad unary op");
361     }
362
363     out.debug << " (" << node->getCompleteString() << ")";
364
365     out.debug << "\n";
366
367     return true;
368 }
369
370 bool TOutputTraverser::visitAggregate(TVisit /* visit */, TIntermAggregate* node)
371 {
372     TInfoSink& out = infoSink;
373
374     if (node->getOp() == EOpNull) {
375         out.debug.message(EPrefixError, "node is still EOpNull!");
376         return true;
377     }
378
379     OutputTreeText(out, node, depth);
380
381     switch (node->getOp()) {
382     case EOpSequence:      out.debug << "Sequence\n";       return true;
383     case EOpLinkerObjects: out.debug << "Linker Objects\n"; return true;
384     case EOpComma:         out.debug << "Comma";            break;
385     case EOpFunction:      out.debug << "Function Definition: " << node->getName(); break;
386     case EOpFunctionCall:  out.debug << "Function Call: "       << node->getName(); break;
387     case EOpParameters:    out.debug << "Function Parameters: ";                    break;
388
389     case EOpConstructFloat: out.debug << "Construct float"; break;
390     case EOpConstructDouble:out.debug << "Construct double"; break;
391     case EOpConstructVec2:  out.debug << "Construct vec2";  break;
392     case EOpConstructVec3:  out.debug << "Construct vec3";  break;
393     case EOpConstructVec4:  out.debug << "Construct vec4";  break;
394     case EOpConstructBool:  out.debug << "Construct bool";  break;
395     case EOpConstructBVec2: out.debug << "Construct bvec2"; break;
396     case EOpConstructBVec3: out.debug << "Construct bvec3"; break;
397     case EOpConstructBVec4: out.debug << "Construct bvec4"; break;
398     case EOpConstructInt:   out.debug << "Construct int";   break;
399     case EOpConstructIVec2: out.debug << "Construct ivec2"; break;
400     case EOpConstructIVec3: out.debug << "Construct ivec3"; break;
401     case EOpConstructIVec4: out.debug << "Construct ivec4"; break;
402     case EOpConstructUint:    out.debug << "Construct uint";    break;
403     case EOpConstructUVec2:   out.debug << "Construct uvec2";   break;
404     case EOpConstructUVec3:   out.debug << "Construct uvec3";   break;
405     case EOpConstructUVec4:   out.debug << "Construct uvec4";   break;
406     case EOpConstructInt64:   out.debug << "Construct int64_t"; break;
407     case EOpConstructI64Vec2: out.debug << "Construct i64vec2"; break;
408     case EOpConstructI64Vec3: out.debug << "Construct i64vec3"; break;
409     case EOpConstructI64Vec4: out.debug << "Construct i64vec4"; break;
410     case EOpConstructUint64:  out.debug << "Construct uint64_t"; break;
411     case EOpConstructU64Vec2: out.debug << "Construct u64vec2"; break;
412     case EOpConstructU64Vec3: out.debug << "Construct u64vec3"; break;
413     case EOpConstructU64Vec4: out.debug << "Construct u64vec4"; break;
414     case EOpConstructMat2x2:  out.debug << "Construct mat2";    break;
415     case EOpConstructMat2x3:  out.debug << "Construct mat2x3";  break;
416     case EOpConstructMat2x4:  out.debug << "Construct mat2x4";  break;
417     case EOpConstructMat3x2:  out.debug << "Construct mat3x2";  break;
418     case EOpConstructMat3x3:  out.debug << "Construct mat3";    break;
419     case EOpConstructMat3x4:  out.debug << "Construct mat3x4";  break;
420     case EOpConstructMat4x2:  out.debug << "Construct mat4x2";  break;
421     case EOpConstructMat4x3:  out.debug << "Construct mat4x3";  break;
422     case EOpConstructMat4x4:  out.debug << "Construct mat4";    break;
423     case EOpConstructDMat2x2: out.debug << "Construct dmat2";   break;
424     case EOpConstructDMat2x3: out.debug << "Construct dmat2x3"; break;
425     case EOpConstructDMat2x4: out.debug << "Construct dmat2x4"; break;
426     case EOpConstructDMat3x2: out.debug << "Construct dmat3x2"; break;
427     case EOpConstructDMat3x3: out.debug << "Construct dmat3";   break;
428     case EOpConstructDMat3x4: out.debug << "Construct dmat3x4"; break;
429     case EOpConstructDMat4x2: out.debug << "Construct dmat4x2"; break;
430     case EOpConstructDMat4x3: out.debug << "Construct dmat4x3"; break;
431     case EOpConstructDMat4x4: out.debug << "Construct dmat4";   break;
432     case EOpConstructStruct:  out.debug << "Construct structure";  break;
433     case EOpConstructTextureSampler: out.debug << "Construct combined texture-sampler"; break;
434
435     case EOpLessThan:         out.debug << "Compare Less Than";             break;
436     case EOpGreaterThan:      out.debug << "Compare Greater Than";          break;
437     case EOpLessThanEqual:    out.debug << "Compare Less Than or Equal";    break;
438     case EOpGreaterThanEqual: out.debug << "Compare Greater Than or Equal"; break;
439     case EOpVectorEqual:      out.debug << "Equal";                         break;
440     case EOpVectorNotEqual:   out.debug << "NotEqual";                      break;
441
442     case EOpMod:           out.debug << "mod";         break;
443     case EOpModf:          out.debug << "modf";        break;
444     case EOpPow:           out.debug << "pow";         break;
445
446     case EOpAtan:          out.debug << "arc tangent"; break;
447
448     case EOpMin:           out.debug << "min";         break;
449     case EOpMax:           out.debug << "max";         break;
450     case EOpClamp:         out.debug << "clamp";       break;
451     case EOpMix:           out.debug << "mix";         break;
452     case EOpStep:          out.debug << "step";        break;
453     case EOpSmoothStep:    out.debug << "smoothstep";  break;
454
455     case EOpDistance:      out.debug << "distance";                break;
456     case EOpDot:           out.debug << "dot-product";             break;
457     case EOpCross:         out.debug << "cross-product";           break;
458     case EOpFaceForward:   out.debug << "face-forward";            break;
459     case EOpReflect:       out.debug << "reflect";                 break;
460     case EOpRefract:       out.debug << "refract";                 break;
461     case EOpMul:           out.debug << "component-wise multiply"; break;
462     case EOpOuterProduct:  out.debug << "outer product";           break;
463
464     case EOpEmitVertex:    out.debug << "EmitVertex";              break;
465     case EOpEndPrimitive:  out.debug << "EndPrimitive";            break;
466
467     case EOpBarrier:                    out.debug << "Barrier";                    break;
468     case EOpMemoryBarrier:              out.debug << "MemoryBarrier";              break;
469     case EOpMemoryBarrierAtomicCounter: out.debug << "MemoryBarrierAtomicCounter"; break;
470     case EOpMemoryBarrierBuffer:        out.debug << "MemoryBarrierBuffer";        break;
471     case EOpMemoryBarrierImage:         out.debug << "MemoryBarrierImage";         break;
472     case EOpMemoryBarrierShared:        out.debug << "MemoryBarrierShared";        break;
473     case EOpGroupMemoryBarrier:         out.debug << "GroupMemoryBarrier";         break;
474
475     case EOpReadInvocation:             out.debug << "readInvocation";        break;
476
477     case EOpAtomicAdd:                  out.debug << "AtomicAdd";             break;
478     case EOpAtomicMin:                  out.debug << "AtomicMin";             break;
479     case EOpAtomicMax:                  out.debug << "AtomicMax";             break;
480     case EOpAtomicAnd:                  out.debug << "AtomicAnd";             break;
481     case EOpAtomicOr:                   out.debug << "AtomicOr";              break;
482     case EOpAtomicXor:                  out.debug << "AtomicXor";             break;
483     case EOpAtomicExchange:             out.debug << "AtomicExchange";        break;
484     case EOpAtomicCompSwap:             out.debug << "AtomicCompSwap";        break;
485
486     case EOpImageQuerySize:             out.debug << "imageQuerySize";        break;
487     case EOpImageQuerySamples:          out.debug << "imageQuerySamples";     break;
488     case EOpImageLoad:                  out.debug << "imageLoad";             break;
489     case EOpImageStore:                 out.debug << "imageStore";            break;
490     case EOpImageAtomicAdd:             out.debug << "imageAtomicAdd";        break;
491     case EOpImageAtomicMin:             out.debug << "imageAtomicMin";        break;
492     case EOpImageAtomicMax:             out.debug << "imageAtomicMax";        break;
493     case EOpImageAtomicAnd:             out.debug << "imageAtomicAnd";        break;
494     case EOpImageAtomicOr:              out.debug << "imageAtomicOr";         break;
495     case EOpImageAtomicXor:             out.debug << "imageAtomicXor";        break;
496     case EOpImageAtomicExchange:        out.debug << "imageAtomicExchange";   break;
497     case EOpImageAtomicCompSwap:        out.debug << "imageAtomicCompSwap";   break;
498
499     case EOpTextureQuerySize:           out.debug << "textureSize";           break;
500     case EOpTextureQueryLod:            out.debug << "textureQueryLod";       break;
501     case EOpTextureQueryLevels:         out.debug << "textureQueryLevels";    break;
502     case EOpTextureQuerySamples:        out.debug << "textureSamples";        break;
503     case EOpTexture:                    out.debug << "texture";               break;
504     case EOpTextureProj:                out.debug << "textureProj";           break;
505     case EOpTextureLod:                 out.debug << "textureLod";            break;
506     case EOpTextureOffset:              out.debug << "textureOffset";         break;
507     case EOpTextureFetch:               out.debug << "textureFetch";          break;
508     case EOpTextureFetchOffset:         out.debug << "textureFetchOffset";    break;
509     case EOpTextureProjOffset:          out.debug << "textureProjOffset";     break;
510     case EOpTextureLodOffset:           out.debug << "textureLodOffset";      break;
511     case EOpTextureProjLod:             out.debug << "textureProjLod";        break;
512     case EOpTextureProjLodOffset:       out.debug << "textureProjLodOffset";  break;
513     case EOpTextureGrad:                out.debug << "textureGrad";           break;
514     case EOpTextureGradOffset:          out.debug << "textureGradOffset";     break;
515     case EOpTextureProjGrad:            out.debug << "textureProjGrad";       break;
516     case EOpTextureProjGradOffset:      out.debug << "textureProjGradOffset"; break;
517     case EOpTextureGather:              out.debug << "textureGather";         break;
518     case EOpTextureGatherOffset:        out.debug << "textureGatherOffset";   break;
519     case EOpTextureGatherOffsets:       out.debug << "textureGatherOffsets";  break;
520
521     case EOpAddCarry:                   out.debug << "addCarry";              break;
522     case EOpSubBorrow:                  out.debug << "subBorrow";             break;
523     case EOpUMulExtended:               out.debug << "uMulExtended";          break;
524     case EOpIMulExtended:               out.debug << "iMulExtended";          break;
525     case EOpBitfieldExtract:            out.debug << "bitfieldExtract";       break;
526     case EOpBitfieldInsert:             out.debug << "bitfieldInsert";        break;
527
528     case EOpFma:                        out.debug << "fma";                   break;
529     case EOpFrexp:                      out.debug << "frexp";                 break;
530     case EOpLdexp:                      out.debug << "ldexp";                 break;
531
532     case EOpInterpolateAtSample:   out.debug << "interpolateAtSample";    break;
533     case EOpInterpolateAtOffset:   out.debug << "interpolateAtOffset";    break;
534
535     default: out.debug.message(EPrefixError, "Bad aggregation op");
536     }
537
538     if (node->getOp() != EOpSequence && node->getOp() != EOpParameters)
539         out.debug << " (" << node->getCompleteString() << ")";
540
541     out.debug << "\n";
542
543     return true;
544 }
545
546 bool TOutputTraverser::visitSelection(TVisit /* visit */, TIntermSelection* node)
547 {
548     TInfoSink& out = infoSink;
549
550     OutputTreeText(out, node, depth);
551
552     out.debug << "Test condition and select";
553     out.debug << " (" << node->getCompleteString() << ")\n";
554
555     ++depth;
556
557     OutputTreeText(out, node, depth);
558     out.debug << "Condition\n";
559     node->getCondition()->traverse(this);
560
561     OutputTreeText(out, node, depth);
562     if (node->getTrueBlock()) {
563         out.debug << "true case\n";
564         node->getTrueBlock()->traverse(this);
565     } else
566         out.debug << "true case is null\n";
567
568     if (node->getFalseBlock()) {
569         OutputTreeText(out, node, depth);
570         out.debug << "false case\n";
571         node->getFalseBlock()->traverse(this);
572     }
573
574     --depth;
575
576     return false;
577 }
578
579 static void OutputConstantUnion(TInfoSink& out, const TIntermTyped* node, const TConstUnionArray& constUnion, int depth)
580 {
581     int size = node->getType().computeNumComponents();
582
583     for (int i = 0; i < size; i++) {
584         OutputTreeText(out, node, depth);
585         switch (constUnion[i].getType()) {
586         case EbtBool:
587             if (constUnion[i].getBConst())
588                 out.debug << "true";
589             else
590                 out.debug << "false";
591
592             out.debug << " (" << "const bool" << ")";
593
594             out.debug << "\n";
595             break;
596         case EbtFloat:
597         case EbtDouble:
598             {
599                 const double value = constUnion[i].getDConst();
600                 // Print infinity in a portable way, for test stability.
601                 // Other cases may be needed in the future: negative infinity,
602                 // and NaNs.
603                 if (is_positive_infinity(value))
604                     out.debug << "inf\n";
605                 else {
606                     const int maxSize = 300;
607                     char buf[maxSize];
608                     snprintf(buf, maxSize, "%f", value);
609
610                     out.debug << buf << "\n";
611                 }
612             }
613             break;
614         case EbtInt:
615             {
616                 const int maxSize = 300;
617                 char buf[maxSize];
618                 snprintf(buf, maxSize, "%d (%s)", constUnion[i].getIConst(), "const int");
619
620                 out.debug << buf << "\n";
621             }
622             break;
623         case EbtUint:
624             {
625                 const int maxSize = 300;
626                 char buf[maxSize];
627                 snprintf(buf, maxSize, "%u (%s)", constUnion[i].getUConst(), "const uint");
628
629                 out.debug << buf << "\n";
630             }
631             break;
632         case EbtInt64:
633             {
634                 const int maxSize = 300;
635                 char buf[maxSize];
636                 snprintf(buf, maxSize, "%lld (%s)", constUnion[i].getI64Const(), "const int64_t");
637
638                 out.debug << buf << "\n";
639             }
640             break;
641         case EbtUint64:
642             {
643                 const int maxSize = 300;
644                 char buf[maxSize];
645                 snprintf(buf, maxSize, "%llu (%s)", constUnion[i].getU64Const(), "const uint64_t");
646
647                 out.debug << buf << "\n";
648             }
649             break;
650         default:
651             out.info.message(EPrefixInternalError, "Unknown constant", node->getLoc());
652             break;
653         }
654     }
655 }
656
657 void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node)
658 {
659     OutputTreeText(infoSink, node, depth);
660     infoSink.debug << "Constant:\n";
661
662     OutputConstantUnion(infoSink, node, node->getConstArray(), depth + 1);
663 }
664
665 void TOutputTraverser::visitSymbol(TIntermSymbol* node)
666 {
667     OutputTreeText(infoSink, node, depth);
668
669     infoSink.debug << "'" << node->getName() << "' (" << node->getCompleteString() << ")\n";
670
671     if (! node->getConstArray().empty())
672         OutputConstantUnion(infoSink, node, node->getConstArray(), depth + 1);
673     else if (node->getConstSubtree()) {
674         incrementDepth(node);
675         node->getConstSubtree()->traverse(this);
676         decrementDepth();
677     }
678 }
679
680 bool TOutputTraverser::visitLoop(TVisit /* visit */, TIntermLoop* node)
681 {
682     TInfoSink& out = infoSink;
683
684     OutputTreeText(out, node, depth);
685
686     out.debug << "Loop with condition ";
687     if (! node->testFirst())
688         out.debug << "not ";
689     out.debug << "tested first\n";
690
691     ++depth;
692
693     OutputTreeText(infoSink, node, depth);
694     if (node->getTest()) {
695         out.debug << "Loop Condition\n";
696         node->getTest()->traverse(this);
697     } else
698         out.debug << "No loop condition\n";
699
700     OutputTreeText(infoSink, node, depth);
701     if (node->getBody()) {
702         out.debug << "Loop Body\n";
703         node->getBody()->traverse(this);
704     } else
705         out.debug << "No loop body\n";
706
707     if (node->getTerminal()) {
708         OutputTreeText(infoSink, node, depth);
709         out.debug << "Loop Terminal Expression\n";
710         node->getTerminal()->traverse(this);
711     }
712
713     --depth;
714
715     return false;
716 }
717
718 bool TOutputTraverser::visitBranch(TVisit /* visit*/, TIntermBranch* node)
719 {
720     TInfoSink& out = infoSink;
721
722     OutputTreeText(out, node, depth);
723
724     switch (node->getFlowOp()) {
725     case EOpKill:      out.debug << "Branch: Kill";           break;
726     case EOpBreak:     out.debug << "Branch: Break";          break;
727     case EOpContinue:  out.debug << "Branch: Continue";       break;
728     case EOpReturn:    out.debug << "Branch: Return";         break;
729     case EOpCase:      out.debug << "case: ";                 break;
730     case EOpDefault:   out.debug << "default: ";              break;
731     default:               out.debug << "Branch: Unknown Branch"; break;
732     }
733
734     if (node->getExpression()) {
735         out.debug << " with expression\n";
736         ++depth;
737         node->getExpression()->traverse(this);
738         --depth;
739     } else
740         out.debug << "\n";
741
742     return false;
743 }
744
745 bool TOutputTraverser::visitSwitch(TVisit /* visit */, TIntermSwitch* node)
746 {
747     TInfoSink& out = infoSink;
748
749     OutputTreeText(out, node, depth);
750     out.debug << "switch\n";
751
752     OutputTreeText(out, node, depth);
753     out.debug << "condition\n";
754     ++depth;
755     node->getCondition()->traverse(this);
756
757     --depth;
758     OutputTreeText(out, node, depth);
759     out.debug << "body\n";
760     ++depth;
761     node->getBody()->traverse(this);
762
763     --depth;
764
765     return false;
766 }
767
768 //
769 // This function is the one to call externally to start the traversal.
770 // Individual functions can be initialized to 0 to skip processing of that
771 // type of node.  It's children will still be processed.
772 //
773 void TIntermediate::output(TInfoSink& infoSink, bool tree)
774 {
775     infoSink.debug << "Shader version: " << version << "\n";
776     if (requestedExtensions.size() > 0) {
777         for (auto extIt = requestedExtensions.begin(); extIt != requestedExtensions.end(); ++extIt)
778             infoSink.debug << "Requested " << *extIt << "\n";
779     }
780
781     if (xfbMode)
782         infoSink.debug << "in xfb mode\n";
783
784     switch (language) {
785     case EShLangVertex:
786         break;
787
788     case EShLangTessControl:
789         infoSink.debug << "vertices = " << vertices << "\n";
790         break;
791
792     case EShLangTessEvaluation:
793         infoSink.debug << "input primitive = " << TQualifier::getGeometryString(inputPrimitive) << "\n";
794         infoSink.debug << "vertex spacing = " << TQualifier::getVertexSpacingString(vertexSpacing) << "\n";
795         infoSink.debug << "triangle order = " << TQualifier::getVertexOrderString(vertexOrder) << "\n";
796         if (pointMode)
797             infoSink.debug << "using point mode\n";
798         break;
799
800     case EShLangGeometry:
801         infoSink.debug << "invocations = " << invocations << "\n";
802         infoSink.debug << "max_vertices = " << vertices << "\n";
803         infoSink.debug << "input primitive = " << TQualifier::getGeometryString(inputPrimitive) << "\n";
804         infoSink.debug << "output primitive = " << TQualifier::getGeometryString(outputPrimitive) << "\n";
805         break;
806
807     case EShLangFragment:
808         if (pixelCenterInteger)
809             infoSink.debug << "gl_FragCoord pixel center is integer\n";
810         if (originUpperLeft)
811             infoSink.debug << "gl_FragCoord origin is upper left\n";
812         if (earlyFragmentTests)
813             infoSink.debug << "using early_fragment_tests\n";
814         if (depthLayout != EldNone)
815             infoSink.debug << "using " << TQualifier::getLayoutDepthString(depthLayout) << "\n";
816         if (blendEquations != 0) {
817             infoSink.debug << "using";
818             // blendEquations is a mask, decode it
819             for (TBlendEquationShift be = (TBlendEquationShift)0; be < EBlendCount; be = (TBlendEquationShift)(be + 1)) {
820                 if (blendEquations & (1 << be))
821                     infoSink.debug << " " << TQualifier::getBlendEquationString(be);
822             }
823             infoSink.debug << "\n";
824         }
825         break;
826
827     case EShLangCompute:
828         infoSink.debug << "local_size = (" << localSize[0] << ", " << localSize[1] << ", " << localSize[2] << ")\n";
829         {
830             if (localSizeSpecId[0] != TQualifier::layoutNotSet ||
831                 localSizeSpecId[1] != TQualifier::layoutNotSet ||
832                 localSizeSpecId[2] != TQualifier::layoutNotSet) {
833                 infoSink.debug << "local_size ids = (" <<
834                     localSizeSpecId[0] << ", " <<
835                     localSizeSpecId[1] << ", " <<
836                     localSizeSpecId[2] << ")\n";
837             }
838         }
839         break;
840
841     default:
842         break;
843     }
844
845     if (treeRoot == 0 || ! tree)
846         return;
847
848     TOutputTraverser it(infoSink);
849
850     treeRoot->traverse(&it);
851 }
852
853 } // end namespace glslang