John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 7 | #include "localintermediate.h" |
| 8 | #include "SymbolTable.h" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 9 | |
| 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 | // |
| 23 | class TOutputTraverser : public TIntermTraverser { |
| 24 | public: |
| 25 | TOutputTraverser(TInfoSinkBase& i) : sink(i) { } |
| 26 | TInfoSinkBase& sink; |
| 27 | |
| 28 | protected: |
| 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 | |
| 39 | TString 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 Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 47 | if (isMatrix()) |
| 48 | stream << primarySize << "X" << secondarySize << " matrix of "; |
| 49 | else if(primarySize > 1) |
| 50 | stream << primarySize << "-component vector of "; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 51 | |
| 52 | stream << getBasicString(); |
| 53 | return stream.str(); |
| 54 | } |
| 55 | |
| 56 | // |
| 57 | // Helper functions for printing, not part of traversing. |
| 58 | // |
| 59 | |
| 60 | void 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 | |
| 79 | void TOutputTraverser::visitSymbol(TIntermSymbol* node) |
| 80 | { |
| 81 | OutputTreeText(sink, node, depth); |
| 82 | |
| 83 | sink << "'" << node->getSymbol() << "' "; |
| 84 | sink << "(" << node->getCompleteString() << ")\n"; |
| 85 | } |
| 86 | |
| 87 | bool 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 | |
| 139 | bool 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 155 | 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; |
Alexis Hetu | af1970c | 2015-04-17 14:26:07 -0400 | [diff] [blame] | 163 | case EOpSinh: out << "hyperbolic sine"; break; |
| 164 | case EOpCosh: out << "hyperbolic cosine"; break; |
| 165 | case EOpTanh: out << "hyperbolic tangent"; break; |
| 166 | case EOpAsinh: out << "arc hyperbolic sine"; break; |
| 167 | case EOpAcosh: out << "arc hyperbolic cosine"; break; |
| 168 | case EOpAtanh: out << "arc hyperbolic tangent"; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 169 | |
| 170 | case EOpExp: out << "exp"; break; |
| 171 | case EOpLog: out << "log"; break; |
| 172 | case EOpExp2: out << "exp2"; break; |
| 173 | case EOpLog2: out << "log2"; break; |
| 174 | case EOpSqrt: out << "sqrt"; break; |
| 175 | case EOpInverseSqrt: out << "inverse sqrt"; break; |
| 176 | |
| 177 | case EOpAbs: out << "Absolute value"; break; |
| 178 | case EOpSign: out << "Sign"; break; |
| 179 | case EOpFloor: out << "Floor"; break; |
Alexis Hetu | af1970c | 2015-04-17 14:26:07 -0400 | [diff] [blame] | 180 | case EOpTrunc: out << "Trunc"; break; |
| 181 | case EOpRound: out << "Round"; break; |
| 182 | case EOpRoundEven: out << "RoundEven"; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 183 | case EOpCeil: out << "Ceiling"; break; |
| 184 | case EOpFract: out << "Fraction"; break; |
Alexis Hetu | af1970c | 2015-04-17 14:26:07 -0400 | [diff] [blame] | 185 | case EOpIsNan: out << "Is not a number"; break; |
| 186 | case EOpIsInf: out << "Is infinity"; break; |
| 187 | |
| 188 | case EOpFloatBitsToInt: out << "float bits to int"; break; |
| 189 | case EOpFloatBitsToUint: out << "float bits to uint"; break; |
| 190 | case EOpIntBitsToFloat: out << "int bits to float"; break; |
| 191 | case EOpUintBitsToFloat: out << "uint bits to float"; break; |
| 192 | |
| 193 | case EOpPackSnorm2x16: out << "pack Snorm 2x16"; break; |
| 194 | case EOpPackUnorm2x16: out << "pack Unorm 2x16"; break; |
| 195 | case EOpPackHalf2x16: out << "pack half 2x16"; break; |
| 196 | |
| 197 | case EOpUnpackSnorm2x16: out << "unpack Snorm 2x16"; break; |
| 198 | case EOpUnpackUnorm2x16: out << "unpack Unorm 2x16"; break; |
| 199 | case EOpUnpackHalf2x16: out << "unpack half 2x16"; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 200 | |
| 201 | case EOpLength: out << "length"; break; |
| 202 | case EOpNormalize: out << "normalize"; break; |
| 203 | // case EOpDPdx: out << "dPdx"; break; |
| 204 | // case EOpDPdy: out << "dPdy"; break; |
| 205 | // case EOpFwidth: out << "fwidth"; break; |
| 206 | |
Alexis Hetu | af1970c | 2015-04-17 14:26:07 -0400 | [diff] [blame] | 207 | case EOpDeterminant: out << "determinant"; break; |
| 208 | case EOpTranspose: out << "transpose"; break; |
| 209 | case EOpInverse: out << "inverse"; break; |
| 210 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 211 | case EOpAny: out << "any"; break; |
| 212 | case EOpAll: out << "all"; break; |
| 213 | |
| 214 | default: out.message(EPrefixError, "Bad unary op"); |
| 215 | } |
| 216 | |
| 217 | out << " (" << node->getCompleteString() << ")"; |
| 218 | |
| 219 | out << "\n"; |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool TOutputTraverser::visitAggregate(Visit visit, TIntermAggregate* node) |
| 225 | { |
| 226 | TInfoSinkBase& out = sink; |
| 227 | |
| 228 | if (node->getOp() == EOpNull) { |
| 229 | out.message(EPrefixError, "node is still EOpNull!"); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | OutputTreeText(out, node, depth); |
| 234 | |
| 235 | switch (node->getOp()) { |
| 236 | case EOpSequence: out << "Sequence\n"; return true; |
| 237 | case EOpComma: out << "Comma\n"; return true; |
| 238 | case EOpFunction: out << "Function Definition: " << node->getName(); break; |
| 239 | case EOpFunctionCall: out << "Function Call: " << node->getName(); break; |
| 240 | case EOpParameters: out << "Function Parameters: "; break; |
| 241 | |
| 242 | case EOpConstructFloat: out << "Construct float"; break; |
| 243 | case EOpConstructVec2: out << "Construct vec2"; break; |
| 244 | case EOpConstructVec3: out << "Construct vec3"; break; |
| 245 | case EOpConstructVec4: out << "Construct vec4"; break; |
| 246 | case EOpConstructBool: out << "Construct bool"; break; |
| 247 | case EOpConstructBVec2: out << "Construct bvec2"; break; |
| 248 | case EOpConstructBVec3: out << "Construct bvec3"; break; |
| 249 | case EOpConstructBVec4: out << "Construct bvec4"; break; |
| 250 | case EOpConstructInt: out << "Construct int"; break; |
| 251 | case EOpConstructIVec2: out << "Construct ivec2"; break; |
| 252 | case EOpConstructIVec3: out << "Construct ivec3"; break; |
| 253 | case EOpConstructIVec4: out << "Construct ivec4"; break; |
Nicolas Capens | e4b1b1d | 2015-02-17 17:26:01 -0500 | [diff] [blame] | 254 | case EOpConstructUInt: out << "Construct uint"; break; |
| 255 | case EOpConstructUVec2: out << "Construct uvec2"; break; |
| 256 | case EOpConstructUVec3: out << "Construct uvec3"; break; |
| 257 | case EOpConstructUVec4: out << "Construct uvec4"; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 258 | case EOpConstructMat2: out << "Construct mat2"; break; |
| 259 | case EOpConstructMat3: out << "Construct mat3"; break; |
| 260 | case EOpConstructMat4: out << "Construct mat4"; break; |
| 261 | case EOpConstructStruct: out << "Construct structure"; break; |
| 262 | |
| 263 | case EOpLessThan: out << "Compare Less Than"; break; |
| 264 | case EOpGreaterThan: out << "Compare Greater Than"; break; |
| 265 | case EOpLessThanEqual: out << "Compare Less Than or Equal"; break; |
| 266 | case EOpGreaterThanEqual: out << "Compare Greater Than or Equal"; break; |
| 267 | case EOpVectorEqual: out << "Equal"; break; |
| 268 | case EOpVectorNotEqual: out << "NotEqual"; break; |
| 269 | |
| 270 | case EOpMod: out << "mod"; break; |
| 271 | case EOpPow: out << "pow"; break; |
| 272 | |
| 273 | case EOpAtan: out << "arc tangent"; break; |
| 274 | |
| 275 | case EOpMin: out << "min"; break; |
| 276 | case EOpMax: out << "max"; break; |
| 277 | case EOpClamp: out << "clamp"; break; |
| 278 | case EOpMix: out << "mix"; break; |
| 279 | case EOpStep: out << "step"; break; |
| 280 | case EOpSmoothStep: out << "smoothstep"; break; |
| 281 | |
| 282 | case EOpDistance: out << "distance"; break; |
| 283 | case EOpDot: out << "dot-product"; break; |
| 284 | case EOpCross: out << "cross-product"; break; |
| 285 | case EOpFaceForward: out << "face-forward"; break; |
| 286 | case EOpReflect: out << "reflect"; break; |
| 287 | case EOpRefract: out << "refract"; break; |
| 288 | case EOpMul: out << "component-wise multiply"; break; |
Alexis Hetu | af1970c | 2015-04-17 14:26:07 -0400 | [diff] [blame] | 289 | case EOpOuterProduct: out << "outer product"; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 290 | |
| 291 | default: out.message(EPrefixError, "Bad aggregation op"); |
| 292 | } |
| 293 | |
| 294 | if (node->getOp() != EOpSequence && node->getOp() != EOpParameters) |
| 295 | out << " (" << node->getCompleteString() << ")"; |
| 296 | |
| 297 | out << "\n"; |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | bool TOutputTraverser::visitSelection(Visit visit, TIntermSelection* node) |
| 303 | { |
| 304 | TInfoSinkBase& out = sink; |
| 305 | |
| 306 | OutputTreeText(out, node, depth); |
| 307 | |
| 308 | out << "Test condition and select"; |
| 309 | out << " (" << node->getCompleteString() << ")\n"; |
| 310 | |
| 311 | ++depth; |
| 312 | |
| 313 | OutputTreeText(sink, node, depth); |
| 314 | out << "Condition\n"; |
| 315 | node->getCondition()->traverse(this); |
| 316 | |
| 317 | OutputTreeText(sink, node, depth); |
| 318 | if (node->getTrueBlock()) { |
| 319 | out << "true case\n"; |
| 320 | node->getTrueBlock()->traverse(this); |
| 321 | } else |
| 322 | out << "true case is null\n"; |
| 323 | |
| 324 | if (node->getFalseBlock()) { |
| 325 | OutputTreeText(sink, node, depth); |
| 326 | out << "false case\n"; |
| 327 | node->getFalseBlock()->traverse(this); |
| 328 | } |
| 329 | |
| 330 | --depth; |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | void TOutputTraverser::visitConstantUnion(TIntermConstantUnion* node) |
| 336 | { |
| 337 | TInfoSinkBase& out = sink; |
| 338 | |
| 339 | int size = node->getType().getObjectSize(); |
| 340 | |
| 341 | for (int i = 0; i < size; i++) { |
| 342 | OutputTreeText(out, node, depth); |
| 343 | switch (node->getUnionArrayPointer()[i].getType()) { |
| 344 | case EbtBool: |
| 345 | if (node->getUnionArrayPointer()[i].getBConst()) |
| 346 | out << "true"; |
| 347 | else |
| 348 | out << "false"; |
| 349 | |
| 350 | out << " (" << "const bool" << ")"; |
| 351 | out << "\n"; |
| 352 | break; |
| 353 | case EbtFloat: |
| 354 | out << node->getUnionArrayPointer()[i].getFConst(); |
| 355 | out << " (const float)\n"; |
| 356 | break; |
| 357 | case EbtInt: |
| 358 | out << node->getUnionArrayPointer()[i].getIConst(); |
| 359 | out << " (const int)\n"; |
| 360 | break; |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 361 | case EbtUInt: |
| 362 | out << node->getUnionArrayPointer()[i].getUConst(); |
| 363 | out << " (const uint)\n"; |
| 364 | break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 365 | default: |
| 366 | out.message(EPrefixInternalError, "Unknown constant", node->getLine()); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | bool TOutputTraverser::visitLoop(Visit visit, TIntermLoop* node) |
| 373 | { |
| 374 | TInfoSinkBase& out = sink; |
| 375 | |
| 376 | OutputTreeText(out, node, depth); |
| 377 | |
| 378 | out << "Loop with condition "; |
| 379 | if (node->getType() == ELoopDoWhile) |
| 380 | out << "not "; |
| 381 | out << "tested first\n"; |
| 382 | |
| 383 | ++depth; |
| 384 | |
| 385 | OutputTreeText(sink, node, depth); |
| 386 | if (node->getCondition()) { |
| 387 | out << "Loop Condition\n"; |
| 388 | node->getCondition()->traverse(this); |
| 389 | } else |
| 390 | out << "No loop condition\n"; |
| 391 | |
| 392 | OutputTreeText(sink, node, depth); |
| 393 | if (node->getBody()) { |
| 394 | out << "Loop Body\n"; |
| 395 | node->getBody()->traverse(this); |
| 396 | } else |
| 397 | out << "No loop body\n"; |
| 398 | |
| 399 | if (node->getExpression()) { |
| 400 | OutputTreeText(sink, node, depth); |
| 401 | out << "Loop Terminal Expression\n"; |
| 402 | node->getExpression()->traverse(this); |
| 403 | } |
| 404 | |
| 405 | --depth; |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | bool TOutputTraverser::visitBranch(Visit visit, TIntermBranch* node) |
| 411 | { |
| 412 | TInfoSinkBase& out = sink; |
| 413 | |
| 414 | OutputTreeText(out, node, depth); |
| 415 | |
| 416 | switch (node->getFlowOp()) { |
| 417 | case EOpKill: out << "Branch: Kill"; break; |
| 418 | case EOpBreak: out << "Branch: Break"; break; |
| 419 | case EOpContinue: out << "Branch: Continue"; break; |
| 420 | case EOpReturn: out << "Branch: Return"; break; |
| 421 | default: out << "Branch: Unknown Branch"; break; |
| 422 | } |
| 423 | |
| 424 | if (node->getExpression()) { |
| 425 | out << " with expression\n"; |
| 426 | ++depth; |
| 427 | node->getExpression()->traverse(this); |
| 428 | --depth; |
| 429 | } else |
| 430 | out << "\n"; |
| 431 | |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | // |
| 436 | // This function is the one to call externally to start the traversal. |
| 437 | // Individual functions can be initialized to 0 to skip processing of that |
| 438 | // type of node. It's children will still be processed. |
| 439 | // |
| 440 | void TIntermediate::outputTree(TIntermNode* root) |
| 441 | { |
| 442 | if (root == 0) |
| 443 | return; |
| 444 | |
| 445 | TOutputTraverser it(infoSink.info); |
| 446 | |
| 447 | root->traverse(&it); |
| 448 | } |