John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -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 | 66b8ab2 | 2014-05-06 15:57:45 -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 | |
| 7 | // |
| 8 | // Build the intermediate representation. |
| 9 | // |
| 10 | |
| 11 | #include <float.h> |
| 12 | #include <limits.h> |
| 13 | #include <algorithm> |
| 14 | |
| 15 | #include "compiler/localintermediate.h" |
| 16 | #include "compiler/QualifierAlive.h" |
| 17 | #include "compiler/RemoveTree.h" |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 18 | #include "compiler/SymbolTable.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 19 | |
| 20 | bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray); |
| 21 | |
| 22 | static TPrecision GetHigherPrecision( TPrecision left, TPrecision right ){ |
| 23 | return left > right ? left : right; |
| 24 | } |
| 25 | |
| 26 | const char* getOperatorString(TOperator op) { |
| 27 | switch (op) { |
| 28 | case EOpInitialize: return "="; |
| 29 | case EOpAssign: return "="; |
| 30 | case EOpAddAssign: return "+="; |
| 31 | case EOpSubAssign: return "-="; |
| 32 | case EOpDivAssign: return "/="; |
| 33 | |
| 34 | // Fall-through. |
| 35 | case EOpMulAssign: |
| 36 | case EOpVectorTimesMatrixAssign: |
| 37 | case EOpVectorTimesScalarAssign: |
| 38 | case EOpMatrixTimesScalarAssign: |
| 39 | case EOpMatrixTimesMatrixAssign: return "*="; |
| 40 | |
| 41 | // Fall-through. |
| 42 | case EOpIndexDirect: |
| 43 | case EOpIndexIndirect: return "[]"; |
| 44 | |
| 45 | case EOpIndexDirectStruct: return "."; |
| 46 | case EOpVectorSwizzle: return "."; |
| 47 | case EOpAdd: return "+"; |
| 48 | case EOpSub: return "-"; |
| 49 | case EOpMul: return "*"; |
| 50 | case EOpDiv: return "/"; |
| 51 | case EOpMod: UNIMPLEMENTED(); break; |
| 52 | case EOpEqual: return "=="; |
| 53 | case EOpNotEqual: return "!="; |
| 54 | case EOpLessThan: return "<"; |
| 55 | case EOpGreaterThan: return ">"; |
| 56 | case EOpLessThanEqual: return "<="; |
| 57 | case EOpGreaterThanEqual: return ">="; |
| 58 | |
| 59 | // Fall-through. |
| 60 | case EOpVectorTimesScalar: |
| 61 | case EOpVectorTimesMatrix: |
| 62 | case EOpMatrixTimesVector: |
| 63 | case EOpMatrixTimesScalar: |
| 64 | case EOpMatrixTimesMatrix: return "*"; |
| 65 | |
| 66 | case EOpLogicalOr: return "||"; |
| 67 | case EOpLogicalXor: return "^^"; |
| 68 | case EOpLogicalAnd: return "&&"; |
| 69 | case EOpNegative: return "-"; |
| 70 | case EOpVectorLogicalNot: return "not"; |
| 71 | case EOpLogicalNot: return "!"; |
| 72 | case EOpPostIncrement: return "++"; |
| 73 | case EOpPostDecrement: return "--"; |
| 74 | case EOpPreIncrement: return "++"; |
| 75 | case EOpPreDecrement: return "--"; |
| 76 | |
| 77 | // Fall-through. |
| 78 | case EOpConvIntToBool: |
| 79 | case EOpConvFloatToBool: return "bool"; |
| 80 | |
| 81 | // Fall-through. |
| 82 | case EOpConvBoolToFloat: |
| 83 | case EOpConvIntToFloat: return "float"; |
| 84 | |
| 85 | // Fall-through. |
| 86 | case EOpConvFloatToInt: |
| 87 | case EOpConvBoolToInt: return "int"; |
| 88 | |
| 89 | case EOpRadians: return "radians"; |
| 90 | case EOpDegrees: return "degrees"; |
| 91 | case EOpSin: return "sin"; |
| 92 | case EOpCos: return "cos"; |
| 93 | case EOpTan: return "tan"; |
| 94 | case EOpAsin: return "asin"; |
| 95 | case EOpAcos: return "acos"; |
| 96 | case EOpAtan: return "atan"; |
| 97 | case EOpExp: return "exp"; |
| 98 | case EOpLog: return "log"; |
| 99 | case EOpExp2: return "exp2"; |
| 100 | case EOpLog2: return "log2"; |
| 101 | case EOpSqrt: return "sqrt"; |
| 102 | case EOpInverseSqrt: return "inversesqrt"; |
| 103 | case EOpAbs: return "abs"; |
| 104 | case EOpSign: return "sign"; |
| 105 | case EOpFloor: return "floor"; |
| 106 | case EOpCeil: return "ceil"; |
| 107 | case EOpFract: return "fract"; |
| 108 | case EOpLength: return "length"; |
| 109 | case EOpNormalize: return "normalize"; |
| 110 | case EOpDFdx: return "dFdx"; |
| 111 | case EOpDFdy: return "dFdy"; |
| 112 | case EOpFwidth: return "fwidth"; |
| 113 | case EOpAny: return "any"; |
| 114 | case EOpAll: return "all"; |
| 115 | |
| 116 | default: break; |
| 117 | } |
| 118 | return ""; |
| 119 | } |
| 120 | |
| 121 | //////////////////////////////////////////////////////////////////////////// |
| 122 | // |
| 123 | // First set of functions are to help build the intermediate representation. |
| 124 | // These functions are not member functions of the nodes. |
| 125 | // They are called from parser productions. |
| 126 | // |
| 127 | ///////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | // |
| 130 | // Add a terminal node for an identifier in an expression. |
| 131 | // |
| 132 | // Returns the added node. |
| 133 | // |
| 134 | TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, TSourceLoc line) |
| 135 | { |
| 136 | TIntermSymbol* node = new TIntermSymbol(id, name, type); |
| 137 | node->setLine(line); |
| 138 | |
| 139 | return node; |
| 140 | } |
| 141 | |
| 142 | // |
| 143 | // Connect two nodes with a new parent that does a binary operation on the nodes. |
| 144 | // |
| 145 | // Returns the added node. |
| 146 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 147 | TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 148 | { |
| 149 | switch (op) { |
| 150 | case EOpEqual: |
| 151 | case EOpNotEqual: |
| 152 | if (left->isArray()) |
| 153 | return 0; |
| 154 | break; |
| 155 | case EOpLessThan: |
| 156 | case EOpGreaterThan: |
| 157 | case EOpLessThanEqual: |
| 158 | case EOpGreaterThanEqual: |
| 159 | if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) { |
| 160 | return 0; |
| 161 | } |
| 162 | break; |
| 163 | case EOpLogicalOr: |
| 164 | case EOpLogicalXor: |
| 165 | case EOpLogicalAnd: |
| 166 | if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) { |
| 167 | return 0; |
| 168 | } |
| 169 | break; |
| 170 | case EOpAdd: |
| 171 | case EOpSub: |
| 172 | case EOpDiv: |
| 173 | case EOpMul: |
| 174 | if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool) |
| 175 | return 0; |
| 176 | default: break; |
| 177 | } |
| 178 | |
| 179 | // |
| 180 | // First try converting the children to compatible types. |
| 181 | // |
| 182 | if (left->getType().getStruct() && right->getType().getStruct()) { |
| 183 | if (left->getType() != right->getType()) |
| 184 | return 0; |
| 185 | } else { |
| 186 | TIntermTyped* child = addConversion(op, left->getType(), right); |
| 187 | if (child) |
| 188 | right = child; |
| 189 | else { |
| 190 | child = addConversion(op, right->getType(), left); |
| 191 | if (child) |
| 192 | left = child; |
| 193 | else |
| 194 | return 0; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // |
| 199 | // Need a new node holding things together then. Make |
| 200 | // one and promote it to the right type. |
| 201 | // |
| 202 | TIntermBinary* node = new TIntermBinary(op); |
| 203 | if (line == 0) |
| 204 | line = right->getLine(); |
| 205 | node->setLine(line); |
| 206 | |
| 207 | node->setLeft(left); |
| 208 | node->setRight(right); |
| 209 | if (!node->promote(infoSink)) |
| 210 | return 0; |
| 211 | |
| 212 | // |
| 213 | // See if we can fold constants. |
| 214 | // |
| 215 | TIntermTyped* typedReturnNode = 0; |
| 216 | TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion(); |
| 217 | TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion(); |
| 218 | if (leftTempConstant && rightTempConstant) { |
| 219 | typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink); |
| 220 | |
| 221 | if (typedReturnNode) |
| 222 | return typedReturnNode; |
| 223 | } |
| 224 | |
| 225 | return node; |
| 226 | } |
| 227 | |
| 228 | // |
| 229 | // Connect two nodes through an assignment. |
| 230 | // |
| 231 | // Returns the added node. |
| 232 | // |
| 233 | TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line) |
| 234 | { |
| 235 | // |
| 236 | // Like adding binary math, except the conversion can only go |
| 237 | // from right to left. |
| 238 | // |
| 239 | TIntermBinary* node = new TIntermBinary(op); |
| 240 | if (line == 0) |
| 241 | line = left->getLine(); |
| 242 | node->setLine(line); |
| 243 | |
| 244 | TIntermTyped* child = addConversion(op, left->getType(), right); |
| 245 | if (child == 0) |
| 246 | return 0; |
| 247 | |
| 248 | node->setLeft(left); |
| 249 | node->setRight(child); |
| 250 | if (! node->promote(infoSink)) |
| 251 | return 0; |
| 252 | |
| 253 | return node; |
| 254 | } |
| 255 | |
| 256 | // |
| 257 | // Connect two nodes through an index operator, where the left node is the base |
| 258 | // of an array or struct, and the right node is a direct or indirect offset. |
| 259 | // |
| 260 | // Returns the added node. |
| 261 | // The caller should set the type of the returned node. |
| 262 | // |
| 263 | TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, TSourceLoc line) |
| 264 | { |
| 265 | TIntermBinary* node = new TIntermBinary(op); |
| 266 | if (line == 0) |
| 267 | line = index->getLine(); |
| 268 | node->setLine(line); |
| 269 | node->setLeft(base); |
| 270 | node->setRight(index); |
| 271 | |
| 272 | // caller should set the type |
| 273 | |
| 274 | return node; |
| 275 | } |
| 276 | |
| 277 | // |
| 278 | // Add one node as the parent of another that it operates on. |
| 279 | // |
| 280 | // Returns the added node. |
| 281 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 282 | TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, TSourceLoc line) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 283 | { |
| 284 | TIntermUnary* node; |
| 285 | TIntermTyped* child = childNode->getAsTyped(); |
| 286 | |
| 287 | if (child == 0) { |
| 288 | infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | switch (op) { |
| 293 | case EOpLogicalNot: |
| 294 | if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) { |
| 295 | return 0; |
| 296 | } |
| 297 | break; |
| 298 | |
| 299 | case EOpPostIncrement: |
| 300 | case EOpPreIncrement: |
| 301 | case EOpPostDecrement: |
| 302 | case EOpPreDecrement: |
| 303 | case EOpNegative: |
| 304 | if (child->getType().getBasicType() == EbtStruct || child->getType().isArray()) |
| 305 | return 0; |
| 306 | default: break; |
| 307 | } |
| 308 | |
| 309 | // |
| 310 | // Do we need to promote the operand? |
| 311 | // |
| 312 | // Note: Implicit promotions were removed from the language. |
| 313 | // |
| 314 | TBasicType newType = EbtVoid; |
| 315 | switch (op) { |
| 316 | case EOpConstructInt: newType = EbtInt; break; |
| 317 | case EOpConstructBool: newType = EbtBool; break; |
| 318 | case EOpConstructFloat: newType = EbtFloat; break; |
| 319 | default: break; |
| 320 | } |
| 321 | |
| 322 | if (newType != EbtVoid) { |
| 323 | child = addConversion(op, TType(newType, child->getPrecision(), EvqTemporary, |
| 324 | child->getNominalSize(), |
| 325 | child->isMatrix(), |
| 326 | child->isArray()), |
| 327 | child); |
| 328 | if (child == 0) |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | // |
| 333 | // For constructors, we are now done, it's all in the conversion. |
| 334 | // |
| 335 | switch (op) { |
| 336 | case EOpConstructInt: |
| 337 | case EOpConstructBool: |
| 338 | case EOpConstructFloat: |
| 339 | return child; |
| 340 | default: break; |
| 341 | } |
| 342 | |
| 343 | TIntermConstantUnion *childTempConstant = 0; |
| 344 | if (child->getAsConstantUnion()) |
| 345 | childTempConstant = child->getAsConstantUnion(); |
| 346 | |
| 347 | // |
| 348 | // Make a new node for the operator. |
| 349 | // |
| 350 | node = new TIntermUnary(op); |
| 351 | if (line == 0) |
| 352 | line = child->getLine(); |
| 353 | node->setLine(line); |
| 354 | node->setOperand(child); |
| 355 | |
| 356 | if (! node->promote(infoSink)) |
| 357 | return 0; |
| 358 | |
| 359 | if (childTempConstant) { |
| 360 | TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink); |
| 361 | |
| 362 | if (newChild) |
| 363 | return newChild; |
| 364 | } |
| 365 | |
| 366 | return node; |
| 367 | } |
| 368 | |
| 369 | // |
| 370 | // This is the safe way to change the operator on an aggregate, as it |
| 371 | // does lots of error checking and fixing. Especially for establishing |
| 372 | // a function call's operation on it's set of parameters. Sequences |
| 373 | // of instructions are also aggregates, but they just direnctly set |
| 374 | // their operator to EOpSequence. |
| 375 | // |
| 376 | // Returns an aggregate node, which could be the one passed in if |
| 377 | // it was already an aggregate but no operator was set. |
| 378 | // |
| 379 | TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, TSourceLoc line) |
| 380 | { |
| 381 | TIntermAggregate* aggNode; |
| 382 | |
| 383 | // |
| 384 | // Make sure we have an aggregate. If not turn it into one. |
| 385 | // |
| 386 | if (node) { |
| 387 | aggNode = node->getAsAggregate(); |
| 388 | if (aggNode == 0 || aggNode->getOp() != EOpNull) { |
| 389 | // |
| 390 | // Make an aggregate containing this node. |
| 391 | // |
| 392 | aggNode = new TIntermAggregate(); |
| 393 | aggNode->getSequence().push_back(node); |
| 394 | if (line == 0) |
| 395 | line = node->getLine(); |
| 396 | } |
| 397 | } else |
| 398 | aggNode = new TIntermAggregate(); |
| 399 | |
| 400 | // |
| 401 | // Set the operator. |
| 402 | // |
| 403 | aggNode->setOp(op); |
| 404 | if (line != 0) |
| 405 | aggNode->setLine(line); |
| 406 | |
| 407 | return aggNode; |
| 408 | } |
| 409 | |
| 410 | // |
| 411 | // Convert one type to another. |
| 412 | // |
| 413 | // Returns the node representing the conversion, which could be the same |
| 414 | // node passed in if no conversion was needed. |
| 415 | // |
| 416 | // Return 0 if a conversion can't be done. |
| 417 | // |
| 418 | TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TIntermTyped* node) |
| 419 | { |
| 420 | // |
| 421 | // Does the base type allow operation? |
| 422 | // |
Nicolas Capens | e9c5e4f | 2014-05-28 22:46:43 -0400 | [diff] [blame^] | 423 | if (node->getBasicType() == EbtVoid || IsSampler(node->getBasicType())) |
| 424 | { |
| 425 | return 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // |
| 429 | // Otherwise, if types are identical, no problem |
| 430 | // |
| 431 | if (type == node->getType()) |
| 432 | return node; |
| 433 | |
| 434 | // |
| 435 | // If one's a structure, then no conversions. |
| 436 | // |
| 437 | if (type.getStruct() || node->getType().getStruct()) |
| 438 | return 0; |
| 439 | |
| 440 | // |
| 441 | // If one's an array, then no conversions. |
| 442 | // |
| 443 | if (type.isArray() || node->getType().isArray()) |
| 444 | return 0; |
| 445 | |
| 446 | TBasicType promoteTo; |
| 447 | |
| 448 | switch (op) { |
| 449 | // |
| 450 | // Explicit conversions |
| 451 | // |
| 452 | case EOpConstructBool: |
| 453 | promoteTo = EbtBool; |
| 454 | break; |
| 455 | case EOpConstructFloat: |
| 456 | promoteTo = EbtFloat; |
| 457 | break; |
| 458 | case EOpConstructInt: |
| 459 | promoteTo = EbtInt; |
| 460 | break; |
| 461 | default: |
| 462 | // |
| 463 | // implicit conversions were removed from the language. |
| 464 | // |
| 465 | if (type.getBasicType() != node->getType().getBasicType()) |
| 466 | return 0; |
| 467 | // |
| 468 | // Size and structure could still differ, but that's |
| 469 | // handled by operator promotion. |
| 470 | // |
| 471 | return node; |
| 472 | } |
| 473 | |
| 474 | if (node->getAsConstantUnion()) { |
| 475 | |
| 476 | return (promoteConstantUnion(promoteTo, node->getAsConstantUnion())); |
| 477 | } else { |
| 478 | |
| 479 | // |
| 480 | // Add a new newNode for the conversion. |
| 481 | // |
| 482 | TIntermUnary* newNode = 0; |
| 483 | |
| 484 | TOperator newOp = EOpNull; |
| 485 | switch (promoteTo) { |
| 486 | case EbtFloat: |
| 487 | switch (node->getBasicType()) { |
| 488 | case EbtInt: newOp = EOpConvIntToFloat; break; |
| 489 | case EbtBool: newOp = EOpConvBoolToFloat; break; |
| 490 | default: |
| 491 | infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine()); |
| 492 | return 0; |
| 493 | } |
| 494 | break; |
| 495 | case EbtBool: |
| 496 | switch (node->getBasicType()) { |
| 497 | case EbtInt: newOp = EOpConvIntToBool; break; |
| 498 | case EbtFloat: newOp = EOpConvFloatToBool; break; |
| 499 | default: |
| 500 | infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine()); |
| 501 | return 0; |
| 502 | } |
| 503 | break; |
| 504 | case EbtInt: |
| 505 | switch (node->getBasicType()) { |
| 506 | case EbtBool: newOp = EOpConvBoolToInt; break; |
| 507 | case EbtFloat: newOp = EOpConvFloatToInt; break; |
| 508 | default: |
| 509 | infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine()); |
| 510 | return 0; |
| 511 | } |
| 512 | break; |
| 513 | default: |
| 514 | infoSink.info.message(EPrefixInternalError, "Bad promotion type", node->getLine()); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | TType type(promoteTo, node->getPrecision(), EvqTemporary, node->getNominalSize(), node->isMatrix(), node->isArray()); |
| 519 | newNode = new TIntermUnary(newOp, type); |
| 520 | newNode->setLine(node->getLine()); |
| 521 | newNode->setOperand(node); |
| 522 | |
| 523 | return newNode; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // |
| 528 | // Safe way to combine two nodes into an aggregate. Works with null pointers, |
| 529 | // a node that's not a aggregate yet, etc. |
| 530 | // |
| 531 | // Returns the resulting aggregate, unless 0 was passed in for |
| 532 | // both existing nodes. |
| 533 | // |
| 534 | TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, TSourceLoc line) |
| 535 | { |
| 536 | if (left == 0 && right == 0) |
| 537 | return 0; |
| 538 | |
| 539 | TIntermAggregate* aggNode = 0; |
| 540 | if (left) |
| 541 | aggNode = left->getAsAggregate(); |
| 542 | if (!aggNode || aggNode->getOp() != EOpNull) { |
| 543 | aggNode = new TIntermAggregate; |
| 544 | if (left) |
| 545 | aggNode->getSequence().push_back(left); |
| 546 | } |
| 547 | |
| 548 | if (right) |
| 549 | aggNode->getSequence().push_back(right); |
| 550 | |
| 551 | if (line != 0) |
| 552 | aggNode->setLine(line); |
| 553 | |
| 554 | return aggNode; |
| 555 | } |
| 556 | |
| 557 | // |
| 558 | // Turn an existing node into an aggregate. |
| 559 | // |
| 560 | // Returns an aggregate, unless 0 was passed in for the existing node. |
| 561 | // |
| 562 | TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, TSourceLoc line) |
| 563 | { |
| 564 | if (node == 0) |
| 565 | return 0; |
| 566 | |
| 567 | TIntermAggregate* aggNode = new TIntermAggregate; |
| 568 | aggNode->getSequence().push_back(node); |
| 569 | |
| 570 | if (line != 0) |
| 571 | aggNode->setLine(line); |
| 572 | else |
| 573 | aggNode->setLine(node->getLine()); |
| 574 | |
| 575 | return aggNode; |
| 576 | } |
| 577 | |
| 578 | // |
| 579 | // For "if" test nodes. There are three children; a condition, |
| 580 | // a true path, and a false path. The two paths are in the |
| 581 | // nodePair. |
| 582 | // |
| 583 | // Returns the selection node created. |
| 584 | // |
| 585 | TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, TSourceLoc line) |
| 586 | { |
| 587 | // |
| 588 | // For compile time constant selections, prune the code and |
| 589 | // test now. |
| 590 | // |
| 591 | |
| 592 | if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) { |
| 593 | if (cond->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getBConst() == true) |
| 594 | return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL; |
| 595 | else |
| 596 | return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL; |
| 597 | } |
| 598 | |
| 599 | TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2); |
| 600 | node->setLine(line); |
| 601 | |
| 602 | return node; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc line) |
| 607 | { |
| 608 | if (left->getType().getQualifier() == EvqConst && right->getType().getQualifier() == EvqConst) { |
| 609 | return right; |
| 610 | } else { |
| 611 | TIntermTyped *commaAggregate = growAggregate(left, right, line); |
| 612 | commaAggregate->getAsAggregate()->setOp(EOpComma); |
| 613 | commaAggregate->setType(right->getType()); |
| 614 | commaAggregate->getTypePointer()->setQualifier(EvqTemporary); |
| 615 | return commaAggregate; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // |
| 620 | // For "?:" test nodes. There are three children; a condition, |
| 621 | // a true path, and a false path. The two paths are specified |
| 622 | // as separate parameters. |
| 623 | // |
| 624 | // Returns the selection node created, or 0 if one could not be. |
| 625 | // |
| 626 | TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc line) |
| 627 | { |
| 628 | // |
| 629 | // Get compatible types. |
| 630 | // |
| 631 | TIntermTyped* child = addConversion(EOpSequence, trueBlock->getType(), falseBlock); |
| 632 | if (child) |
| 633 | falseBlock = child; |
| 634 | else { |
| 635 | child = addConversion(EOpSequence, falseBlock->getType(), trueBlock); |
| 636 | if (child) |
| 637 | trueBlock = child; |
| 638 | else |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | // |
| 643 | // See if all the operands are constant, then fold it otherwise not. |
| 644 | // |
| 645 | |
| 646 | if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) { |
| 647 | if (cond->getAsConstantUnion()->getUnionArrayPointer()->getBConst()) |
| 648 | return trueBlock; |
| 649 | else |
| 650 | return falseBlock; |
| 651 | } |
| 652 | |
| 653 | // |
| 654 | // Make a selection node. |
| 655 | // |
| 656 | TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType()); |
| 657 | node->getTypePointer()->setQualifier(EvqTemporary); |
| 658 | node->setLine(line); |
| 659 | |
| 660 | return node; |
| 661 | } |
| 662 | |
| 663 | // |
| 664 | // Constant terminal nodes. Has a union that contains bool, float or int constants |
| 665 | // |
| 666 | // Returns the constant union node created. |
| 667 | // |
| 668 | |
| 669 | TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, TSourceLoc line) |
| 670 | { |
| 671 | TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t); |
| 672 | node->setLine(line); |
| 673 | |
| 674 | return node; |
| 675 | } |
| 676 | |
| 677 | TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, TSourceLoc line) |
| 678 | { |
| 679 | |
| 680 | TIntermAggregate* node = new TIntermAggregate(EOpSequence); |
| 681 | |
| 682 | node->setLine(line); |
| 683 | TIntermConstantUnion* constIntNode; |
| 684 | TIntermSequence &sequenceVector = node->getSequence(); |
| 685 | ConstantUnion* unionArray; |
| 686 | |
| 687 | for (int i = 0; i < fields.num; i++) { |
| 688 | unionArray = new ConstantUnion[1]; |
| 689 | unionArray->setIConst(fields.offsets[i]); |
| 690 | constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), line); |
| 691 | sequenceVector.push_back(constIntNode); |
| 692 | } |
| 693 | |
| 694 | return node; |
| 695 | } |
| 696 | |
| 697 | // |
| 698 | // Create loop nodes. |
| 699 | // |
| 700 | TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, TSourceLoc line) |
| 701 | { |
| 702 | TIntermNode* node = new TIntermLoop(type, init, cond, expr, body); |
| 703 | node->setLine(line); |
| 704 | |
| 705 | return node; |
| 706 | } |
| 707 | |
| 708 | // |
| 709 | // Add branches. |
| 710 | // |
| 711 | TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TSourceLoc line) |
| 712 | { |
| 713 | return addBranch(branchOp, 0, line); |
| 714 | } |
| 715 | |
| 716 | TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, TSourceLoc line) |
| 717 | { |
| 718 | TIntermBranch* node = new TIntermBranch(branchOp, expression); |
| 719 | node->setLine(line); |
| 720 | |
| 721 | return node; |
| 722 | } |
| 723 | |
| 724 | // |
| 725 | // This is to be executed once the final root is put on top by the parsing |
| 726 | // process. |
| 727 | // |
| 728 | bool TIntermediate::postProcess(TIntermNode* root) |
| 729 | { |
| 730 | if (root == 0) |
| 731 | return true; |
| 732 | |
| 733 | // |
| 734 | // First, finish off the top level sequence, if any |
| 735 | // |
| 736 | TIntermAggregate* aggRoot = root->getAsAggregate(); |
| 737 | if (aggRoot && aggRoot->getOp() == EOpNull) |
| 738 | aggRoot->setOp(EOpSequence); |
| 739 | |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | // |
| 744 | // This deletes the tree. |
| 745 | // |
| 746 | void TIntermediate::remove(TIntermNode* root) |
| 747 | { |
| 748 | if (root) |
| 749 | RemoveAllTreeNodes(root); |
| 750 | } |
| 751 | |
| 752 | //////////////////////////////////////////////////////////////// |
| 753 | // |
| 754 | // Member functions of the nodes used for building the tree. |
| 755 | // |
| 756 | //////////////////////////////////////////////////////////////// |
| 757 | |
| 758 | // |
| 759 | // Say whether or not an operation node changes the value of a variable. |
| 760 | // |
| 761 | // Returns true if state is modified. |
| 762 | // |
| 763 | bool TIntermOperator::modifiesState() const |
| 764 | { |
| 765 | switch (op) { |
| 766 | case EOpPostIncrement: |
| 767 | case EOpPostDecrement: |
| 768 | case EOpPreIncrement: |
| 769 | case EOpPreDecrement: |
| 770 | case EOpAssign: |
| 771 | case EOpAddAssign: |
| 772 | case EOpSubAssign: |
| 773 | case EOpMulAssign: |
| 774 | case EOpVectorTimesMatrixAssign: |
| 775 | case EOpVectorTimesScalarAssign: |
| 776 | case EOpMatrixTimesScalarAssign: |
| 777 | case EOpMatrixTimesMatrixAssign: |
| 778 | case EOpDivAssign: |
| 779 | return true; |
| 780 | default: |
| 781 | return false; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // |
| 786 | // returns true if the operator is for one of the constructors |
| 787 | // |
| 788 | bool TIntermOperator::isConstructor() const |
| 789 | { |
| 790 | switch (op) { |
| 791 | case EOpConstructVec2: |
| 792 | case EOpConstructVec3: |
| 793 | case EOpConstructVec4: |
| 794 | case EOpConstructMat2: |
| 795 | case EOpConstructMat3: |
| 796 | case EOpConstructMat4: |
| 797 | case EOpConstructFloat: |
| 798 | case EOpConstructIVec2: |
| 799 | case EOpConstructIVec3: |
| 800 | case EOpConstructIVec4: |
| 801 | case EOpConstructInt: |
| 802 | case EOpConstructBVec2: |
| 803 | case EOpConstructBVec3: |
| 804 | case EOpConstructBVec4: |
| 805 | case EOpConstructBool: |
| 806 | case EOpConstructStruct: |
| 807 | return true; |
| 808 | default: |
| 809 | return false; |
| 810 | } |
| 811 | } |
| 812 | // |
| 813 | // Make sure the type of a unary operator is appropriate for its |
| 814 | // combination of operation and operand type. |
| 815 | // |
| 816 | // Returns false in nothing makes sense. |
| 817 | // |
| 818 | bool TIntermUnary::promote(TInfoSink&) |
| 819 | { |
| 820 | switch (op) { |
| 821 | case EOpLogicalNot: |
| 822 | if (operand->getBasicType() != EbtBool) |
| 823 | return false; |
| 824 | break; |
| 825 | case EOpNegative: |
| 826 | case EOpPostIncrement: |
| 827 | case EOpPostDecrement: |
| 828 | case EOpPreIncrement: |
| 829 | case EOpPreDecrement: |
| 830 | if (operand->getBasicType() == EbtBool) |
| 831 | return false; |
| 832 | break; |
| 833 | |
| 834 | // operators for built-ins are already type checked against their prototype |
| 835 | case EOpAny: |
| 836 | case EOpAll: |
| 837 | case EOpVectorLogicalNot: |
| 838 | return true; |
| 839 | |
| 840 | default: |
| 841 | if (operand->getBasicType() != EbtFloat) |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | setType(operand->getType()); |
| 846 | |
| 847 | // Unary operations results in temporary variables unless const. |
| 848 | if (operand->getQualifier() != EvqConst) { |
| 849 | getTypePointer()->setQualifier(EvqTemporary); |
| 850 | } |
| 851 | |
| 852 | return true; |
| 853 | } |
| 854 | |
| 855 | // |
| 856 | // Establishes the type of the resultant operation, as well as |
| 857 | // makes the operator the correct one for the operands. |
| 858 | // |
| 859 | // Returns false if operator can't work on operands. |
| 860 | // |
| 861 | bool TIntermBinary::promote(TInfoSink& infoSink) |
| 862 | { |
| 863 | // This function only handles scalars, vectors, and matrices. |
| 864 | if (left->isArray() || right->isArray()) { |
| 865 | infoSink.info.message(EPrefixInternalError, "Invalid operation for arrays", getLine()); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | // GLSL ES 2.0 does not support implicit type casting. |
| 870 | // So the basic type should always match. |
| 871 | if (left->getBasicType() != right->getBasicType()) |
| 872 | return false; |
| 873 | |
| 874 | // |
| 875 | // Base assumption: just make the type the same as the left |
| 876 | // operand. Then only deviations from this need be coded. |
| 877 | // |
| 878 | setType(left->getType()); |
| 879 | |
| 880 | // The result gets promoted to the highest precision. |
| 881 | TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision()); |
| 882 | getTypePointer()->setPrecision(higherPrecision); |
| 883 | |
| 884 | // Binary operations results in temporary variables unless both |
| 885 | // operands are const. |
| 886 | if (left->getQualifier() != EvqConst || right->getQualifier() != EvqConst) { |
| 887 | getTypePointer()->setQualifier(EvqTemporary); |
| 888 | } |
| 889 | |
| 890 | int size = std::max(left->getNominalSize(), right->getNominalSize()); |
| 891 | |
| 892 | // |
| 893 | // All scalars. Code after this test assumes this case is removed! |
| 894 | // |
| 895 | if (size == 1) { |
| 896 | switch (op) { |
| 897 | // |
| 898 | // Promote to conditional |
| 899 | // |
| 900 | case EOpEqual: |
| 901 | case EOpNotEqual: |
| 902 | case EOpLessThan: |
| 903 | case EOpGreaterThan: |
| 904 | case EOpLessThanEqual: |
| 905 | case EOpGreaterThanEqual: |
| 906 | setType(TType(EbtBool, EbpUndefined)); |
| 907 | break; |
| 908 | |
| 909 | // |
| 910 | // And and Or operate on conditionals |
| 911 | // |
| 912 | case EOpLogicalAnd: |
| 913 | case EOpLogicalOr: |
| 914 | // Both operands must be of type bool. |
| 915 | if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool) |
| 916 | return false; |
| 917 | setType(TType(EbtBool, EbpUndefined)); |
| 918 | break; |
| 919 | |
| 920 | default: |
| 921 | break; |
| 922 | } |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | // If we reach here, at least one of the operands is vector or matrix. |
| 927 | // The other operand could be a scalar, vector, or matrix. |
| 928 | // Are the sizes compatible? |
| 929 | // |
| 930 | if (left->getNominalSize() != right->getNominalSize()) { |
| 931 | // If the nominal size of operands do not match: |
| 932 | // One of them must be scalar. |
| 933 | if (left->getNominalSize() != 1 && right->getNominalSize() != 1) |
| 934 | return false; |
| 935 | // Operator cannot be of type pure assignment. |
| 936 | if (op == EOpAssign || op == EOpInitialize) |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | // |
| 941 | // Can these two operands be combined? |
| 942 | // |
| 943 | TBasicType basicType = left->getBasicType(); |
| 944 | switch (op) { |
| 945 | case EOpMul: |
| 946 | if (!left->isMatrix() && right->isMatrix()) { |
| 947 | if (left->isVector()) |
| 948 | op = EOpVectorTimesMatrix; |
| 949 | else { |
| 950 | op = EOpMatrixTimesScalar; |
| 951 | setType(TType(basicType, higherPrecision, EvqTemporary, size, true)); |
| 952 | } |
| 953 | } else if (left->isMatrix() && !right->isMatrix()) { |
| 954 | if (right->isVector()) { |
| 955 | op = EOpMatrixTimesVector; |
| 956 | setType(TType(basicType, higherPrecision, EvqTemporary, size, false)); |
| 957 | } else { |
| 958 | op = EOpMatrixTimesScalar; |
| 959 | } |
| 960 | } else if (left->isMatrix() && right->isMatrix()) { |
| 961 | op = EOpMatrixTimesMatrix; |
| 962 | } else if (!left->isMatrix() && !right->isMatrix()) { |
| 963 | if (left->isVector() && right->isVector()) { |
| 964 | // leave as component product |
| 965 | } else if (left->isVector() || right->isVector()) { |
| 966 | op = EOpVectorTimesScalar; |
| 967 | setType(TType(basicType, higherPrecision, EvqTemporary, size, false)); |
| 968 | } |
| 969 | } else { |
| 970 | infoSink.info.message(EPrefixInternalError, "Missing elses", getLine()); |
| 971 | return false; |
| 972 | } |
| 973 | break; |
| 974 | case EOpMulAssign: |
| 975 | if (!left->isMatrix() && right->isMatrix()) { |
| 976 | if (left->isVector()) |
| 977 | op = EOpVectorTimesMatrixAssign; |
| 978 | else { |
| 979 | return false; |
| 980 | } |
| 981 | } else if (left->isMatrix() && !right->isMatrix()) { |
| 982 | if (right->isVector()) { |
| 983 | return false; |
| 984 | } else { |
| 985 | op = EOpMatrixTimesScalarAssign; |
| 986 | } |
| 987 | } else if (left->isMatrix() && right->isMatrix()) { |
| 988 | op = EOpMatrixTimesMatrixAssign; |
| 989 | } else if (!left->isMatrix() && !right->isMatrix()) { |
| 990 | if (left->isVector() && right->isVector()) { |
| 991 | // leave as component product |
| 992 | } else if (left->isVector() || right->isVector()) { |
| 993 | if (! left->isVector()) |
| 994 | return false; |
| 995 | op = EOpVectorTimesScalarAssign; |
| 996 | setType(TType(basicType, higherPrecision, EvqTemporary, size, false)); |
| 997 | } |
| 998 | } else { |
| 999 | infoSink.info.message(EPrefixInternalError, "Missing elses", getLine()); |
| 1000 | return false; |
| 1001 | } |
| 1002 | break; |
| 1003 | |
| 1004 | case EOpAssign: |
| 1005 | case EOpInitialize: |
| 1006 | case EOpAdd: |
| 1007 | case EOpSub: |
| 1008 | case EOpDiv: |
| 1009 | case EOpAddAssign: |
| 1010 | case EOpSubAssign: |
| 1011 | case EOpDivAssign: |
| 1012 | if ((left->isMatrix() && right->isVector()) || |
| 1013 | (left->isVector() && right->isMatrix())) |
| 1014 | return false; |
| 1015 | setType(TType(basicType, higherPrecision, EvqTemporary, size, left->isMatrix() || right->isMatrix())); |
| 1016 | break; |
| 1017 | |
| 1018 | case EOpEqual: |
| 1019 | case EOpNotEqual: |
| 1020 | case EOpLessThan: |
| 1021 | case EOpGreaterThan: |
| 1022 | case EOpLessThanEqual: |
| 1023 | case EOpGreaterThanEqual: |
| 1024 | if ((left->isMatrix() && right->isVector()) || |
| 1025 | (left->isVector() && right->isMatrix())) |
| 1026 | return false; |
| 1027 | setType(TType(EbtBool, EbpUndefined)); |
| 1028 | break; |
| 1029 | |
| 1030 | default: |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray) |
| 1038 | { |
| 1039 | const TTypeList* fields = leftNodeType.getStruct(); |
| 1040 | |
| 1041 | size_t structSize = fields->size(); |
| 1042 | int index = 0; |
| 1043 | |
| 1044 | for (size_t j = 0; j < structSize; j++) { |
| 1045 | int size = (*fields)[j].type->getObjectSize(); |
| 1046 | for (int i = 0; i < size; i++) { |
| 1047 | if ((*fields)[j].type->getBasicType() == EbtStruct) { |
| 1048 | if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index])) |
| 1049 | return false; |
| 1050 | } else { |
| 1051 | if (leftUnionArray[index] != rightUnionArray[index]) |
| 1052 | return false; |
| 1053 | index++; |
| 1054 | } |
| 1055 | |
| 1056 | } |
| 1057 | } |
| 1058 | return true; |
| 1059 | } |
| 1060 | |
| 1061 | bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray) |
| 1062 | { |
| 1063 | if (leftNodeType.isArray()) { |
| 1064 | TType typeWithoutArrayness = leftNodeType; |
| 1065 | typeWithoutArrayness.clearArrayness(); |
| 1066 | |
| 1067 | int arraySize = leftNodeType.getArraySize(); |
| 1068 | |
| 1069 | for (int i = 0; i < arraySize; ++i) { |
| 1070 | int offset = typeWithoutArrayness.getObjectSize() * i; |
| 1071 | if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset])) |
| 1072 | return false; |
| 1073 | } |
| 1074 | } else |
| 1075 | return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray); |
| 1076 | |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | // |
| 1081 | // The fold functions see if an operation on a constant can be done in place, |
| 1082 | // without generating run-time code. |
| 1083 | // |
| 1084 | // Returns the node to keep using, which may or may not be the node passed in. |
| 1085 | // |
| 1086 | |
| 1087 | TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink) |
| 1088 | { |
| 1089 | ConstantUnion *unionArray = getUnionArrayPointer(); |
| 1090 | int objectSize = getType().getObjectSize(); |
| 1091 | |
| 1092 | if (constantNode) { // binary operations |
| 1093 | TIntermConstantUnion *node = constantNode->getAsConstantUnion(); |
| 1094 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
| 1095 | TType returnType = getType(); |
| 1096 | |
| 1097 | // for a case like float f = 1.2 + vec4(2,3,4,5); |
| 1098 | if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) { |
| 1099 | rightUnionArray = new ConstantUnion[objectSize]; |
| 1100 | for (int i = 0; i < objectSize; ++i) |
| 1101 | rightUnionArray[i] = *node->getUnionArrayPointer(); |
| 1102 | returnType = getType(); |
| 1103 | } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) { |
| 1104 | // for a case like float f = vec4(2,3,4,5) + 1.2; |
| 1105 | unionArray = new ConstantUnion[constantNode->getType().getObjectSize()]; |
| 1106 | for (int i = 0; i < constantNode->getType().getObjectSize(); ++i) |
| 1107 | unionArray[i] = *getUnionArrayPointer(); |
| 1108 | returnType = node->getType(); |
| 1109 | objectSize = constantNode->getType().getObjectSize(); |
| 1110 | } |
| 1111 | |
| 1112 | ConstantUnion* tempConstArray = 0; |
| 1113 | TIntermConstantUnion *tempNode; |
| 1114 | |
| 1115 | bool boolNodeFlag = false; |
| 1116 | switch(op) { |
| 1117 | case EOpAdd: |
| 1118 | tempConstArray = new ConstantUnion[objectSize]; |
| 1119 | {// support MSVC++6.0 |
| 1120 | for (int i = 0; i < objectSize; i++) |
| 1121 | tempConstArray[i] = unionArray[i] + rightUnionArray[i]; |
| 1122 | } |
| 1123 | break; |
| 1124 | case EOpSub: |
| 1125 | tempConstArray = new ConstantUnion[objectSize]; |
| 1126 | {// support MSVC++6.0 |
| 1127 | for (int i = 0; i < objectSize; i++) |
| 1128 | tempConstArray[i] = unionArray[i] - rightUnionArray[i]; |
| 1129 | } |
| 1130 | break; |
| 1131 | |
| 1132 | case EOpMul: |
| 1133 | case EOpVectorTimesScalar: |
| 1134 | case EOpMatrixTimesScalar: |
| 1135 | tempConstArray = new ConstantUnion[objectSize]; |
| 1136 | {// support MSVC++6.0 |
| 1137 | for (int i = 0; i < objectSize; i++) |
| 1138 | tempConstArray[i] = unionArray[i] * rightUnionArray[i]; |
| 1139 | } |
| 1140 | break; |
| 1141 | case EOpMatrixTimesMatrix: |
| 1142 | if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) { |
| 1143 | infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine()); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | {// support MSVC++6.0 |
| 1147 | int size = getNominalSize(); |
| 1148 | tempConstArray = new ConstantUnion[size*size]; |
| 1149 | for (int row = 0; row < size; row++) { |
| 1150 | for (int column = 0; column < size; column++) { |
| 1151 | tempConstArray[size * column + row].setFConst(0.0f); |
| 1152 | for (int i = 0; i < size; i++) { |
| 1153 | tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst())); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | break; |
| 1159 | case EOpDiv: |
| 1160 | tempConstArray = new ConstantUnion[objectSize]; |
| 1161 | {// support MSVC++6.0 |
| 1162 | for (int i = 0; i < objectSize; i++) { |
| 1163 | switch (getType().getBasicType()) { |
| 1164 | case EbtFloat: |
| 1165 | if (rightUnionArray[i] == 0.0f) { |
| 1166 | infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine()); |
| 1167 | tempConstArray[i].setFConst(FLT_MAX); |
| 1168 | } else |
| 1169 | tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst()); |
| 1170 | break; |
| 1171 | |
| 1172 | case EbtInt: |
| 1173 | if (rightUnionArray[i] == 0) { |
| 1174 | infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine()); |
| 1175 | tempConstArray[i].setIConst(INT_MAX); |
| 1176 | } else |
| 1177 | tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst()); |
| 1178 | break; |
| 1179 | default: |
| 1180 | infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine()); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | break; |
| 1186 | |
| 1187 | case EOpMatrixTimesVector: |
| 1188 | if (node->getBasicType() != EbtFloat) { |
| 1189 | infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine()); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | tempConstArray = new ConstantUnion[getNominalSize()]; |
| 1193 | |
| 1194 | {// support MSVC++6.0 |
| 1195 | for (int size = getNominalSize(), i = 0; i < size; i++) { |
| 1196 | tempConstArray[i].setFConst(0.0f); |
| 1197 | for (int j = 0; j < size; j++) { |
| 1198 | tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst())); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | tempNode = new TIntermConstantUnion(tempConstArray, node->getType()); |
| 1204 | tempNode->setLine(getLine()); |
| 1205 | |
| 1206 | return tempNode; |
| 1207 | |
| 1208 | case EOpVectorTimesMatrix: |
| 1209 | if (getType().getBasicType() != EbtFloat) { |
| 1210 | infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine()); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | tempConstArray = new ConstantUnion[getNominalSize()]; |
| 1215 | {// support MSVC++6.0 |
| 1216 | for (int size = getNominalSize(), i = 0; i < size; i++) { |
| 1217 | tempConstArray[i].setFConst(0.0f); |
| 1218 | for (int j = 0; j < size; j++) { |
| 1219 | tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst())); |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | break; |
| 1224 | |
| 1225 | case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently |
| 1226 | tempConstArray = new ConstantUnion[objectSize]; |
| 1227 | {// support MSVC++6.0 |
| 1228 | for (int i = 0; i < objectSize; i++) |
| 1229 | tempConstArray[i] = unionArray[i] && rightUnionArray[i]; |
| 1230 | } |
| 1231 | break; |
| 1232 | |
| 1233 | case EOpLogicalOr: // this code is written for possible future use, will not get executed currently |
| 1234 | tempConstArray = new ConstantUnion[objectSize]; |
| 1235 | {// support MSVC++6.0 |
| 1236 | for (int i = 0; i < objectSize; i++) |
| 1237 | tempConstArray[i] = unionArray[i] || rightUnionArray[i]; |
| 1238 | } |
| 1239 | break; |
| 1240 | |
| 1241 | case EOpLogicalXor: |
| 1242 | tempConstArray = new ConstantUnion[objectSize]; |
| 1243 | {// support MSVC++6.0 |
| 1244 | for (int i = 0; i < objectSize; i++) |
| 1245 | switch (getType().getBasicType()) { |
| 1246 | case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break; |
| 1247 | default: assert(false && "Default missing"); |
| 1248 | } |
| 1249 | } |
| 1250 | break; |
| 1251 | |
| 1252 | case EOpLessThan: |
| 1253 | assert(objectSize == 1); |
| 1254 | tempConstArray = new ConstantUnion[1]; |
| 1255 | tempConstArray->setBConst(*unionArray < *rightUnionArray); |
| 1256 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1257 | break; |
| 1258 | case EOpGreaterThan: |
| 1259 | assert(objectSize == 1); |
| 1260 | tempConstArray = new ConstantUnion[1]; |
| 1261 | tempConstArray->setBConst(*unionArray > *rightUnionArray); |
| 1262 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1263 | break; |
| 1264 | case EOpLessThanEqual: |
| 1265 | { |
| 1266 | assert(objectSize == 1); |
| 1267 | ConstantUnion constant; |
| 1268 | constant.setBConst(*unionArray > *rightUnionArray); |
| 1269 | tempConstArray = new ConstantUnion[1]; |
| 1270 | tempConstArray->setBConst(!constant.getBConst()); |
| 1271 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1272 | break; |
| 1273 | } |
| 1274 | case EOpGreaterThanEqual: |
| 1275 | { |
| 1276 | assert(objectSize == 1); |
| 1277 | ConstantUnion constant; |
| 1278 | constant.setBConst(*unionArray < *rightUnionArray); |
| 1279 | tempConstArray = new ConstantUnion[1]; |
| 1280 | tempConstArray->setBConst(!constant.getBConst()); |
| 1281 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | case EOpEqual: |
| 1286 | if (getType().getBasicType() == EbtStruct) { |
| 1287 | if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray)) |
| 1288 | boolNodeFlag = true; |
| 1289 | } else { |
| 1290 | for (int i = 0; i < objectSize; i++) { |
| 1291 | if (unionArray[i] != rightUnionArray[i]) { |
| 1292 | boolNodeFlag = true; |
| 1293 | break; // break out of for loop |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | tempConstArray = new ConstantUnion[1]; |
| 1299 | if (!boolNodeFlag) { |
| 1300 | tempConstArray->setBConst(true); |
| 1301 | } |
| 1302 | else { |
| 1303 | tempConstArray->setBConst(false); |
| 1304 | } |
| 1305 | |
| 1306 | tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst)); |
| 1307 | tempNode->setLine(getLine()); |
| 1308 | |
| 1309 | return tempNode; |
| 1310 | |
| 1311 | case EOpNotEqual: |
| 1312 | if (getType().getBasicType() == EbtStruct) { |
| 1313 | if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray)) |
| 1314 | boolNodeFlag = true; |
| 1315 | } else { |
| 1316 | for (int i = 0; i < objectSize; i++) { |
| 1317 | if (unionArray[i] == rightUnionArray[i]) { |
| 1318 | boolNodeFlag = true; |
| 1319 | break; // break out of for loop |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | tempConstArray = new ConstantUnion[1]; |
| 1325 | if (!boolNodeFlag) { |
| 1326 | tempConstArray->setBConst(true); |
| 1327 | } |
| 1328 | else { |
| 1329 | tempConstArray->setBConst(false); |
| 1330 | } |
| 1331 | |
| 1332 | tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst)); |
| 1333 | tempNode->setLine(getLine()); |
| 1334 | |
| 1335 | return tempNode; |
| 1336 | |
| 1337 | default: |
| 1338 | infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine()); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | tempNode = new TIntermConstantUnion(tempConstArray, returnType); |
| 1342 | tempNode->setLine(getLine()); |
| 1343 | |
| 1344 | return tempNode; |
| 1345 | } else { |
| 1346 | // |
| 1347 | // Do unary operations |
| 1348 | // |
| 1349 | TIntermConstantUnion *newNode = 0; |
| 1350 | ConstantUnion* tempConstArray = new ConstantUnion[objectSize]; |
| 1351 | for (int i = 0; i < objectSize; i++) { |
| 1352 | switch(op) { |
| 1353 | case EOpNegative: |
| 1354 | switch (getType().getBasicType()) { |
| 1355 | case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break; |
| 1356 | case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break; |
| 1357 | default: |
| 1358 | infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine()); |
| 1359 | return 0; |
| 1360 | } |
| 1361 | break; |
| 1362 | case EOpLogicalNot: // this code is written for possible future use, will not get executed currently |
| 1363 | switch (getType().getBasicType()) { |
| 1364 | case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break; |
| 1365 | default: |
| 1366 | infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine()); |
| 1367 | return 0; |
| 1368 | } |
| 1369 | break; |
| 1370 | default: |
| 1371 | return 0; |
| 1372 | } |
| 1373 | } |
| 1374 | newNode = new TIntermConstantUnion(tempConstArray, getType()); |
| 1375 | newNode->setLine(getLine()); |
| 1376 | return newNode; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node) |
| 1381 | { |
| 1382 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
| 1383 | int size = node->getType().getObjectSize(); |
| 1384 | |
| 1385 | ConstantUnion *leftUnionArray = new ConstantUnion[size]; |
| 1386 | |
| 1387 | for (int i=0; i < size; i++) { |
| 1388 | |
| 1389 | switch (promoteTo) { |
| 1390 | case EbtFloat: |
| 1391 | switch (node->getType().getBasicType()) { |
| 1392 | case EbtInt: |
| 1393 | leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getIConst())); |
| 1394 | break; |
| 1395 | case EbtBool: |
| 1396 | leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getBConst())); |
| 1397 | break; |
| 1398 | case EbtFloat: |
| 1399 | leftUnionArray[i] = rightUnionArray[i]; |
| 1400 | break; |
| 1401 | default: |
| 1402 | infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine()); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | break; |
| 1406 | case EbtInt: |
| 1407 | switch (node->getType().getBasicType()) { |
| 1408 | case EbtInt: |
| 1409 | leftUnionArray[i] = rightUnionArray[i]; |
| 1410 | break; |
| 1411 | case EbtBool: |
| 1412 | leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getBConst())); |
| 1413 | break; |
| 1414 | case EbtFloat: |
| 1415 | leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getFConst())); |
| 1416 | break; |
| 1417 | default: |
| 1418 | infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine()); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | break; |
| 1422 | case EbtBool: |
| 1423 | switch (node->getType().getBasicType()) { |
| 1424 | case EbtInt: |
| 1425 | leftUnionArray[i].setBConst(rightUnionArray[i].getIConst() != 0); |
| 1426 | break; |
| 1427 | case EbtBool: |
| 1428 | leftUnionArray[i] = rightUnionArray[i]; |
| 1429 | break; |
| 1430 | case EbtFloat: |
| 1431 | leftUnionArray[i].setBConst(rightUnionArray[i].getFConst() != 0.0f); |
| 1432 | break; |
| 1433 | default: |
| 1434 | infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine()); |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | break; |
| 1439 | default: |
| 1440 | infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine()); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | } |
| 1445 | |
| 1446 | const TType& t = node->getType(); |
| 1447 | |
| 1448 | return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.isMatrix(), t.isArray()), node->getLine()); |
| 1449 | } |
| 1450 | |