Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
| 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/translator/HashNames.h" |
| 16 | #include "compiler/translator/IntermNode.h" |
| 17 | #include "compiler/translator/SymbolTable.h" |
| 18 | |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | TPrecision GetHigherPrecision(TPrecision left, TPrecision right) |
| 23 | { |
| 24 | return left > right ? left : right; |
| 25 | } |
| 26 | |
| 27 | bool ValidateMultiplication(TOperator op, const TType &left, const TType &right) |
| 28 | { |
| 29 | switch (op) |
| 30 | { |
| 31 | case EOpMul: |
| 32 | case EOpMulAssign: |
| 33 | return left.getNominalSize() == right.getNominalSize() && |
| 34 | left.getSecondarySize() == right.getSecondarySize(); |
| 35 | case EOpVectorTimesScalar: |
| 36 | case EOpVectorTimesScalarAssign: |
| 37 | return true; |
| 38 | case EOpVectorTimesMatrix: |
| 39 | return left.getNominalSize() == right.getRows(); |
| 40 | case EOpVectorTimesMatrixAssign: |
| 41 | return left.getNominalSize() == right.getRows() && |
| 42 | left.getNominalSize() == right.getCols(); |
| 43 | case EOpMatrixTimesVector: |
| 44 | return left.getCols() == right.getNominalSize(); |
| 45 | case EOpMatrixTimesScalar: |
| 46 | case EOpMatrixTimesScalarAssign: |
| 47 | return true; |
| 48 | case EOpMatrixTimesMatrix: |
| 49 | return left.getCols() == right.getRows(); |
| 50 | case EOpMatrixTimesMatrixAssign: |
| 51 | return left.getCols() == right.getCols() && |
| 52 | left.getRows() == right.getRows(); |
| 53 | |
| 54 | default: |
| 55 | UNREACHABLE(); |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool CompareStructure(const TType& leftNodeType, |
| 61 | ConstantUnion *rightUnionArray, |
| 62 | ConstantUnion *leftUnionArray); |
| 63 | |
| 64 | bool CompareStruct(const TType &leftNodeType, |
| 65 | ConstantUnion *rightUnionArray, |
| 66 | ConstantUnion *leftUnionArray) |
| 67 | { |
| 68 | const TFieldList &fields = leftNodeType.getStruct()->fields(); |
| 69 | |
| 70 | size_t structSize = fields.size(); |
| 71 | size_t index = 0; |
| 72 | |
| 73 | for (size_t j = 0; j < structSize; j++) |
| 74 | { |
| 75 | size_t size = fields[j]->type()->getObjectSize(); |
| 76 | for (size_t i = 0; i < size; i++) |
| 77 | { |
| 78 | if (fields[j]->type()->getBasicType() == EbtStruct) |
| 79 | { |
| 80 | if (!CompareStructure(*fields[j]->type(), |
| 81 | &rightUnionArray[index], |
| 82 | &leftUnionArray[index])) |
| 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | if (leftUnionArray[index] != rightUnionArray[index]) |
| 90 | return false; |
| 91 | index++; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool CompareStructure(const TType &leftNodeType, |
| 99 | ConstantUnion *rightUnionArray, |
| 100 | ConstantUnion *leftUnionArray) |
| 101 | { |
| 102 | if (leftNodeType.isArray()) |
| 103 | { |
| 104 | TType typeWithoutArrayness = leftNodeType; |
| 105 | typeWithoutArrayness.clearArrayness(); |
| 106 | |
| 107 | size_t arraySize = leftNodeType.getArraySize(); |
| 108 | |
| 109 | for (size_t i = 0; i < arraySize; ++i) |
| 110 | { |
| 111 | size_t offset = typeWithoutArrayness.getObjectSize() * i; |
| 112 | if (!CompareStruct(typeWithoutArrayness, |
| 113 | &rightUnionArray[offset], |
| 114 | &leftUnionArray[offset])) |
| 115 | { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray); |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | } // namespace anonymous |
| 128 | |
| 129 | |
| 130 | //////////////////////////////////////////////////////////////// |
| 131 | // |
| 132 | // Member functions of the nodes used for building the tree. |
| 133 | // |
| 134 | //////////////////////////////////////////////////////////////// |
| 135 | |
Olli Etuaho | d2a67b9 | 2014-10-21 16:42:57 +0300 | [diff] [blame] | 136 | void TIntermTyped::setTypePreservePrecision(const TType &t) |
| 137 | { |
| 138 | TPrecision precision = getPrecision(); |
| 139 | mType = t; |
| 140 | ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined); |
| 141 | mType.setPrecision(precision); |
| 142 | } |
| 143 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 144 | #define REPLACE_IF_IS(node, type, original, replacement) \ |
| 145 | if (node == original) { \ |
| 146 | node = static_cast<type *>(replacement); \ |
| 147 | return true; \ |
| 148 | } |
| 149 | |
| 150 | bool TIntermLoop::replaceChildNode( |
| 151 | TIntermNode *original, TIntermNode *replacement) |
| 152 | { |
| 153 | REPLACE_IF_IS(mInit, TIntermNode, original, replacement); |
| 154 | REPLACE_IF_IS(mCond, TIntermTyped, original, replacement); |
| 155 | REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement); |
| 156 | REPLACE_IF_IS(mBody, TIntermNode, original, replacement); |
| 157 | return false; |
| 158 | } |
| 159 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 160 | bool TIntermBranch::replaceChildNode( |
| 161 | TIntermNode *original, TIntermNode *replacement) |
| 162 | { |
| 163 | REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement); |
| 164 | return false; |
| 165 | } |
| 166 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 167 | bool TIntermBinary::replaceChildNode( |
| 168 | TIntermNode *original, TIntermNode *replacement) |
| 169 | { |
| 170 | REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement); |
| 171 | REPLACE_IF_IS(mRight, TIntermTyped, original, replacement); |
| 172 | return false; |
| 173 | } |
| 174 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 175 | bool TIntermUnary::replaceChildNode( |
| 176 | TIntermNode *original, TIntermNode *replacement) |
| 177 | { |
| 178 | REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement); |
| 179 | return false; |
| 180 | } |
| 181 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 182 | bool TIntermAggregate::replaceChildNode( |
| 183 | TIntermNode *original, TIntermNode *replacement) |
| 184 | { |
| 185 | for (size_t ii = 0; ii < mSequence.size(); ++ii) |
| 186 | { |
| 187 | REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement); |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
Olli Etuaho | d2a67b9 | 2014-10-21 16:42:57 +0300 | [diff] [blame] | 192 | void TIntermAggregate::setPrecisionFromChildren() |
| 193 | { |
| 194 | if (getBasicType() == EbtBool) |
| 195 | { |
| 196 | mType.setPrecision(EbpUndefined); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | TPrecision precision = EbpUndefined; |
| 201 | TIntermSequence::iterator childIter = mSequence.begin(); |
| 202 | while (childIter != mSequence.end()) |
| 203 | { |
| 204 | TIntermTyped *typed = (*childIter)->getAsTyped(); |
| 205 | if (typed) |
| 206 | precision = GetHigherPrecision(typed->getPrecision(), precision); |
| 207 | ++childIter; |
| 208 | } |
| 209 | mType.setPrecision(precision); |
| 210 | } |
| 211 | |
| 212 | void TIntermAggregate::setBuiltInFunctionPrecision() |
| 213 | { |
| 214 | // All built-ins returning bool should be handled as ops, not functions. |
| 215 | ASSERT(getBasicType() != EbtBool); |
| 216 | |
| 217 | TPrecision precision = EbpUndefined; |
| 218 | TIntermSequence::iterator childIter = mSequence.begin(); |
| 219 | while (childIter != mSequence.end()) |
| 220 | { |
| 221 | TIntermTyped *typed = (*childIter)->getAsTyped(); |
| 222 | // ESSL spec section 8: texture functions get their precision from the sampler. |
| 223 | if (typed && IsSampler(typed->getBasicType())) |
| 224 | { |
| 225 | precision = typed->getPrecision(); |
| 226 | break; |
| 227 | } |
| 228 | ++childIter; |
| 229 | } |
| 230 | // ESSL 3.0 spec section 8: textureSize always gets highp precision. |
| 231 | // All other functions that take a sampler are assumed to be texture functions. |
| 232 | if (mName.find("textureSize") == 0) |
| 233 | mType.setPrecision(EbpHigh); |
| 234 | else |
| 235 | mType.setPrecision(precision); |
| 236 | } |
| 237 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 238 | bool TIntermSelection::replaceChildNode( |
| 239 | TIntermNode *original, TIntermNode *replacement) |
| 240 | { |
| 241 | REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement); |
| 242 | REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement); |
| 243 | REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement); |
| 244 | return false; |
| 245 | } |
| 246 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 247 | // |
| 248 | // Say whether or not an operation node changes the value of a variable. |
| 249 | // |
| 250 | bool TIntermOperator::isAssignment() const |
| 251 | { |
| 252 | switch (mOp) |
| 253 | { |
| 254 | case EOpPostIncrement: |
| 255 | case EOpPostDecrement: |
| 256 | case EOpPreIncrement: |
| 257 | case EOpPreDecrement: |
| 258 | case EOpAssign: |
| 259 | case EOpAddAssign: |
| 260 | case EOpSubAssign: |
| 261 | case EOpMulAssign: |
| 262 | case EOpVectorTimesMatrixAssign: |
| 263 | case EOpVectorTimesScalarAssign: |
| 264 | case EOpMatrixTimesScalarAssign: |
| 265 | case EOpMatrixTimesMatrixAssign: |
| 266 | case EOpDivAssign: |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame^] | 267 | case EOpIModAssign: |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 268 | case EOpBitShiftLeftAssign: |
| 269 | case EOpBitShiftRightAssign: |
| 270 | case EOpBitwiseAndAssign: |
| 271 | case EOpBitwiseXorAssign: |
| 272 | case EOpBitwiseOrAssign: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 273 | return true; |
| 274 | default: |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // |
| 280 | // returns true if the operator is for one of the constructors |
| 281 | // |
| 282 | bool TIntermOperator::isConstructor() const |
| 283 | { |
| 284 | switch (mOp) |
| 285 | { |
| 286 | case EOpConstructVec2: |
| 287 | case EOpConstructVec3: |
| 288 | case EOpConstructVec4: |
| 289 | case EOpConstructMat2: |
| 290 | case EOpConstructMat3: |
| 291 | case EOpConstructMat4: |
| 292 | case EOpConstructFloat: |
| 293 | case EOpConstructIVec2: |
| 294 | case EOpConstructIVec3: |
| 295 | case EOpConstructIVec4: |
| 296 | case EOpConstructInt: |
| 297 | case EOpConstructUVec2: |
| 298 | case EOpConstructUVec3: |
| 299 | case EOpConstructUVec4: |
| 300 | case EOpConstructUInt: |
| 301 | case EOpConstructBVec2: |
| 302 | case EOpConstructBVec3: |
| 303 | case EOpConstructBVec4: |
| 304 | case EOpConstructBool: |
| 305 | case EOpConstructStruct: |
| 306 | return true; |
| 307 | default: |
| 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // |
| 313 | // Make sure the type of a unary operator is appropriate for its |
| 314 | // combination of operation and operand type. |
| 315 | // |
| 316 | // Returns false in nothing makes sense. |
| 317 | // |
| 318 | bool TIntermUnary::promote(TInfoSink &) |
| 319 | { |
| 320 | switch (mOp) |
| 321 | { |
| 322 | case EOpLogicalNot: |
| 323 | if (mOperand->getBasicType() != EbtBool) |
| 324 | return false; |
| 325 | break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 326 | // bit-wise not is already checked |
| 327 | case EOpBitwiseNot: |
| 328 | break; |
| 329 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 330 | case EOpNegative: |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 331 | case EOpPositive: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 332 | case EOpPostIncrement: |
| 333 | case EOpPostDecrement: |
| 334 | case EOpPreIncrement: |
| 335 | case EOpPreDecrement: |
| 336 | if (mOperand->getBasicType() == EbtBool) |
| 337 | return false; |
| 338 | break; |
| 339 | |
| 340 | // operators for built-ins are already type checked against their prototype |
| 341 | case EOpAny: |
| 342 | case EOpAll: |
| 343 | case EOpVectorLogicalNot: |
Olli Etuaho | e8d2c07 | 2015-01-08 16:33:54 +0200 | [diff] [blame] | 344 | case EOpIntBitsToFloat: |
| 345 | case EOpUintBitsToFloat: |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 346 | case EOpUnpackSnorm2x16: |
| 347 | case EOpUnpackUnorm2x16: |
| 348 | case EOpUnpackHalf2x16: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 349 | return true; |
| 350 | |
| 351 | default: |
| 352 | if (mOperand->getBasicType() != EbtFloat) |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | setType(mOperand->getType()); |
| 357 | mType.setQualifier(EvqTemporary); |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | // |
| 363 | // Establishes the type of the resultant operation, as well as |
| 364 | // makes the operator the correct one for the operands. |
| 365 | // |
| 366 | // Returns false if operator can't work on operands. |
| 367 | // |
| 368 | bool TIntermBinary::promote(TInfoSink &infoSink) |
| 369 | { |
| 370 | // This function only handles scalars, vectors, and matrices. |
| 371 | if (mLeft->isArray() || mRight->isArray()) |
| 372 | { |
| 373 | infoSink.info.message(EPrefixInternalError, getLine(), |
| 374 | "Invalid operation for arrays"); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // GLSL ES 2.0 does not support implicit type casting. |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 379 | // So the basic type should usually match. |
| 380 | bool basicTypesMustMatch = true; |
| 381 | |
| 382 | // Check ops which require integer / ivec parameters |
| 383 | switch (mOp) |
| 384 | { |
| 385 | case EOpBitShiftLeft: |
| 386 | case EOpBitShiftRight: |
| 387 | case EOpBitShiftLeftAssign: |
| 388 | case EOpBitShiftRightAssign: |
| 389 | // Unsigned can be bit-shifted by signed and vice versa, but we need to |
| 390 | // check that the basic type is an integer type. |
| 391 | basicTypesMustMatch = false; |
| 392 | if (!IsInteger(mLeft->getBasicType()) || !IsInteger(mRight->getBasicType())) |
| 393 | { |
| 394 | return false; |
| 395 | } |
| 396 | break; |
| 397 | case EOpBitwiseAnd: |
| 398 | case EOpBitwiseXor: |
| 399 | case EOpBitwiseOr: |
| 400 | case EOpBitwiseAndAssign: |
| 401 | case EOpBitwiseXorAssign: |
| 402 | case EOpBitwiseOrAssign: |
| 403 | // It is enough to check the type of only one operand, since later it |
| 404 | // is checked that the operand types match. |
| 405 | if (!IsInteger(mLeft->getBasicType())) |
| 406 | { |
| 407 | return false; |
| 408 | } |
| 409 | break; |
| 410 | default: |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | if (basicTypesMustMatch && mLeft->getBasicType() != mRight->getBasicType()) |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 415 | { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | // |
| 420 | // Base assumption: just make the type the same as the left |
| 421 | // operand. Then only deviations from this need be coded. |
| 422 | // |
| 423 | setType(mLeft->getType()); |
| 424 | |
| 425 | // The result gets promoted to the highest precision. |
| 426 | TPrecision higherPrecision = GetHigherPrecision( |
| 427 | mLeft->getPrecision(), mRight->getPrecision()); |
| 428 | getTypePointer()->setPrecision(higherPrecision); |
| 429 | |
| 430 | // Binary operations results in temporary variables unless both |
| 431 | // operands are const. |
| 432 | if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst) |
| 433 | { |
| 434 | getTypePointer()->setQualifier(EvqTemporary); |
| 435 | } |
| 436 | |
| 437 | const int nominalSize = |
| 438 | std::max(mLeft->getNominalSize(), mRight->getNominalSize()); |
| 439 | |
| 440 | // |
| 441 | // All scalars or structs. Code after this test assumes this case is removed! |
| 442 | // |
| 443 | if (nominalSize == 1) |
| 444 | { |
| 445 | switch (mOp) |
| 446 | { |
| 447 | // |
| 448 | // Promote to conditional |
| 449 | // |
| 450 | case EOpEqual: |
| 451 | case EOpNotEqual: |
| 452 | case EOpLessThan: |
| 453 | case EOpGreaterThan: |
| 454 | case EOpLessThanEqual: |
| 455 | case EOpGreaterThanEqual: |
| 456 | setType(TType(EbtBool, EbpUndefined)); |
| 457 | break; |
| 458 | |
| 459 | // |
| 460 | // And and Or operate on conditionals |
| 461 | // |
| 462 | case EOpLogicalAnd: |
| 463 | case EOpLogicalOr: |
| 464 | // Both operands must be of type bool. |
| 465 | if (mLeft->getBasicType() != EbtBool || mRight->getBasicType() != EbtBool) |
| 466 | { |
| 467 | return false; |
| 468 | } |
| 469 | setType(TType(EbtBool, EbpUndefined)); |
| 470 | break; |
| 471 | |
| 472 | default: |
| 473 | break; |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | // If we reach here, at least one of the operands is vector or matrix. |
| 479 | // The other operand could be a scalar, vector, or matrix. |
| 480 | // Can these two operands be combined? |
| 481 | // |
| 482 | TBasicType basicType = mLeft->getBasicType(); |
| 483 | switch (mOp) |
| 484 | { |
| 485 | case EOpMul: |
| 486 | if (!mLeft->isMatrix() && mRight->isMatrix()) |
| 487 | { |
| 488 | if (mLeft->isVector()) |
| 489 | { |
| 490 | mOp = EOpVectorTimesMatrix; |
| 491 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 492 | mRight->getCols(), 1)); |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | mOp = EOpMatrixTimesScalar; |
| 497 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 498 | mRight->getCols(), mRight->getRows())); |
| 499 | } |
| 500 | } |
| 501 | else if (mLeft->isMatrix() && !mRight->isMatrix()) |
| 502 | { |
| 503 | if (mRight->isVector()) |
| 504 | { |
| 505 | mOp = EOpMatrixTimesVector; |
| 506 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 507 | mLeft->getRows(), 1)); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | mOp = EOpMatrixTimesScalar; |
| 512 | } |
| 513 | } |
| 514 | else if (mLeft->isMatrix() && mRight->isMatrix()) |
| 515 | { |
| 516 | mOp = EOpMatrixTimesMatrix; |
| 517 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 518 | mRight->getCols(), mLeft->getRows())); |
| 519 | } |
| 520 | else if (!mLeft->isMatrix() && !mRight->isMatrix()) |
| 521 | { |
| 522 | if (mLeft->isVector() && mRight->isVector()) |
| 523 | { |
| 524 | // leave as component product |
| 525 | } |
| 526 | else if (mLeft->isVector() || mRight->isVector()) |
| 527 | { |
| 528 | mOp = EOpVectorTimesScalar; |
| 529 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 530 | nominalSize, 1)); |
| 531 | } |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | infoSink.info.message(EPrefixInternalError, getLine(), |
| 536 | "Missing elses"); |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType())) |
| 541 | { |
| 542 | return false; |
| 543 | } |
| 544 | break; |
| 545 | |
| 546 | case EOpMulAssign: |
| 547 | if (!mLeft->isMatrix() && mRight->isMatrix()) |
| 548 | { |
| 549 | if (mLeft->isVector()) |
| 550 | { |
| 551 | mOp = EOpVectorTimesMatrixAssign; |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | else if (mLeft->isMatrix() && !mRight->isMatrix()) |
| 559 | { |
| 560 | if (mRight->isVector()) |
| 561 | { |
| 562 | return false; |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | mOp = EOpMatrixTimesScalarAssign; |
| 567 | } |
| 568 | } |
| 569 | else if (mLeft->isMatrix() && mRight->isMatrix()) |
| 570 | { |
| 571 | mOp = EOpMatrixTimesMatrixAssign; |
| 572 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 573 | mRight->getCols(), mLeft->getRows())); |
| 574 | } |
| 575 | else if (!mLeft->isMatrix() && !mRight->isMatrix()) |
| 576 | { |
| 577 | if (mLeft->isVector() && mRight->isVector()) |
| 578 | { |
| 579 | // leave as component product |
| 580 | } |
| 581 | else if (mLeft->isVector() || mRight->isVector()) |
| 582 | { |
| 583 | if (!mLeft->isVector()) |
| 584 | return false; |
| 585 | mOp = EOpVectorTimesScalarAssign; |
| 586 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 587 | mLeft->getNominalSize(), 1)); |
| 588 | } |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | infoSink.info.message(EPrefixInternalError, getLine(), |
| 593 | "Missing elses"); |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType())) |
| 598 | { |
| 599 | return false; |
| 600 | } |
| 601 | break; |
| 602 | |
| 603 | case EOpAssign: |
| 604 | case EOpInitialize: |
| 605 | case EOpAdd: |
| 606 | case EOpSub: |
| 607 | case EOpDiv: |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame^] | 608 | case EOpIMod: |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 609 | case EOpBitShiftLeft: |
| 610 | case EOpBitShiftRight: |
| 611 | case EOpBitwiseAnd: |
| 612 | case EOpBitwiseXor: |
| 613 | case EOpBitwiseOr: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 614 | case EOpAddAssign: |
| 615 | case EOpSubAssign: |
| 616 | case EOpDivAssign: |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame^] | 617 | case EOpIModAssign: |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 618 | case EOpBitShiftLeftAssign: |
| 619 | case EOpBitShiftRightAssign: |
| 620 | case EOpBitwiseAndAssign: |
| 621 | case EOpBitwiseXorAssign: |
| 622 | case EOpBitwiseOrAssign: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 623 | if ((mLeft->isMatrix() && mRight->isVector()) || |
| 624 | (mLeft->isVector() && mRight->isMatrix())) |
| 625 | { |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | // Are the sizes compatible? |
| 630 | if (mLeft->getNominalSize() != mRight->getNominalSize() || |
| 631 | mLeft->getSecondarySize() != mRight->getSecondarySize()) |
| 632 | { |
Olli Etuaho | 6c85047 | 2014-12-02 16:23:17 +0200 | [diff] [blame] | 633 | // If the nominal sizes of operands do not match: |
| 634 | // One of them must be a scalar. |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 635 | if (!mLeft->isScalar() && !mRight->isScalar()) |
| 636 | return false; |
| 637 | |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 638 | // In the case of compound assignment other than multiply-assign, |
| 639 | // the right side needs to be a scalar. Otherwise a vector/matrix |
| 640 | // would be assigned to a scalar. A scalar can't be shifted by a |
| 641 | // vector either. |
Olli Etuaho | 6c85047 | 2014-12-02 16:23:17 +0200 | [diff] [blame] | 642 | if (!mRight->isScalar() && |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 643 | (isAssignment() || |
| 644 | mOp == EOpBitShiftLeft || |
| 645 | mOp == EOpBitShiftRight)) |
Olli Etuaho | 6c85047 | 2014-12-02 16:23:17 +0200 | [diff] [blame] | 646 | return false; |
| 647 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 648 | // Operator cannot be of type pure assignment. |
| 649 | if (mOp == EOpAssign || mOp == EOpInitialize) |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | { |
| 654 | const int secondarySize = std::max( |
| 655 | mLeft->getSecondarySize(), mRight->getSecondarySize()); |
| 656 | setType(TType(basicType, higherPrecision, EvqTemporary, |
| 657 | nominalSize, secondarySize)); |
| 658 | } |
| 659 | break; |
| 660 | |
| 661 | case EOpEqual: |
| 662 | case EOpNotEqual: |
| 663 | case EOpLessThan: |
| 664 | case EOpGreaterThan: |
| 665 | case EOpLessThanEqual: |
| 666 | case EOpGreaterThanEqual: |
| 667 | if ((mLeft->getNominalSize() != mRight->getNominalSize()) || |
| 668 | (mLeft->getSecondarySize() != mRight->getSecondarySize())) |
| 669 | { |
| 670 | return false; |
| 671 | } |
| 672 | setType(TType(EbtBool, EbpUndefined)); |
| 673 | break; |
| 674 | |
| 675 | default: |
| 676 | return false; |
| 677 | } |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | // |
| 682 | // The fold functions see if an operation on a constant can be done in place, |
| 683 | // without generating run-time code. |
| 684 | // |
| 685 | // Returns the node to keep using, which may or may not be the node passed in. |
| 686 | // |
| 687 | TIntermTyped *TIntermConstantUnion::fold( |
| 688 | TOperator op, TIntermTyped *constantNode, TInfoSink &infoSink) |
| 689 | { |
| 690 | ConstantUnion *unionArray = getUnionArrayPointer(); |
| 691 | |
| 692 | if (!unionArray) |
| 693 | return NULL; |
| 694 | |
| 695 | size_t objectSize = getType().getObjectSize(); |
| 696 | |
| 697 | if (constantNode) |
| 698 | { |
| 699 | // binary operations |
| 700 | TIntermConstantUnion *node = constantNode->getAsConstantUnion(); |
| 701 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
| 702 | TType returnType = getType(); |
| 703 | |
| 704 | if (!rightUnionArray) |
| 705 | return NULL; |
| 706 | |
| 707 | // for a case like float f = 1.2 + vec4(2,3,4,5); |
| 708 | if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) |
| 709 | { |
| 710 | rightUnionArray = new ConstantUnion[objectSize]; |
| 711 | for (size_t i = 0; i < objectSize; ++i) |
| 712 | { |
| 713 | rightUnionArray[i] = *node->getUnionArrayPointer(); |
| 714 | } |
| 715 | returnType = getType(); |
| 716 | } |
| 717 | else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) |
| 718 | { |
| 719 | // for a case like float f = vec4(2,3,4,5) + 1.2; |
| 720 | unionArray = new ConstantUnion[constantNode->getType().getObjectSize()]; |
| 721 | for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i) |
| 722 | { |
| 723 | unionArray[i] = *getUnionArrayPointer(); |
| 724 | } |
| 725 | returnType = node->getType(); |
| 726 | objectSize = constantNode->getType().getObjectSize(); |
| 727 | } |
| 728 | |
| 729 | ConstantUnion *tempConstArray = NULL; |
| 730 | TIntermConstantUnion *tempNode; |
| 731 | |
| 732 | bool boolNodeFlag = false; |
| 733 | switch(op) |
| 734 | { |
| 735 | case EOpAdd: |
| 736 | tempConstArray = new ConstantUnion[objectSize]; |
| 737 | for (size_t i = 0; i < objectSize; i++) |
| 738 | tempConstArray[i] = unionArray[i] + rightUnionArray[i]; |
| 739 | break; |
| 740 | case EOpSub: |
| 741 | tempConstArray = new ConstantUnion[objectSize]; |
| 742 | for (size_t i = 0; i < objectSize; i++) |
| 743 | tempConstArray[i] = unionArray[i] - rightUnionArray[i]; |
| 744 | break; |
| 745 | |
| 746 | case EOpMul: |
| 747 | case EOpVectorTimesScalar: |
| 748 | case EOpMatrixTimesScalar: |
| 749 | tempConstArray = new ConstantUnion[objectSize]; |
| 750 | for (size_t i = 0; i < objectSize; i++) |
| 751 | tempConstArray[i] = unionArray[i] * rightUnionArray[i]; |
| 752 | break; |
| 753 | |
| 754 | case EOpMatrixTimesMatrix: |
| 755 | { |
| 756 | if (getType().getBasicType() != EbtFloat || |
| 757 | node->getBasicType() != EbtFloat) |
| 758 | { |
| 759 | infoSink.info.message( |
| 760 | EPrefixInternalError, getLine(), |
| 761 | "Constant Folding cannot be done for matrix multiply"); |
| 762 | return NULL; |
| 763 | } |
| 764 | |
| 765 | const int leftCols = getCols(); |
| 766 | const int leftRows = getRows(); |
| 767 | const int rightCols = constantNode->getType().getCols(); |
| 768 | const int rightRows = constantNode->getType().getRows(); |
| 769 | const int resultCols = rightCols; |
| 770 | const int resultRows = leftRows; |
| 771 | |
| 772 | tempConstArray = new ConstantUnion[resultCols*resultRows]; |
| 773 | for (int row = 0; row < resultRows; row++) |
| 774 | { |
| 775 | for (int column = 0; column < resultCols; column++) |
| 776 | { |
| 777 | tempConstArray[resultRows * column + row].setFConst(0.0f); |
| 778 | for (int i = 0; i < leftCols; i++) |
| 779 | { |
| 780 | tempConstArray[resultRows * column + row].setFConst( |
| 781 | tempConstArray[resultRows * column + row].getFConst() + |
| 782 | unionArray[i * leftRows + row].getFConst() * |
| 783 | rightUnionArray[column * rightRows + i].getFConst()); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // update return type for matrix product |
| 789 | returnType.setPrimarySize(resultCols); |
| 790 | returnType.setSecondarySize(resultRows); |
| 791 | } |
| 792 | break; |
| 793 | |
| 794 | case EOpDiv: |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame^] | 795 | case EOpIMod: |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 796 | { |
| 797 | tempConstArray = new ConstantUnion[objectSize]; |
| 798 | for (size_t i = 0; i < objectSize; i++) |
| 799 | { |
| 800 | switch (getType().getBasicType()) |
| 801 | { |
| 802 | case EbtFloat: |
| 803 | if (rightUnionArray[i] == 0.0f) |
| 804 | { |
| 805 | infoSink.info.message( |
| 806 | EPrefixWarning, getLine(), |
| 807 | "Divide by zero error during constant folding"); |
| 808 | tempConstArray[i].setFConst( |
| 809 | unionArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX); |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | tempConstArray[i].setFConst( |
| 814 | unionArray[i].getFConst() / |
| 815 | rightUnionArray[i].getFConst()); |
| 816 | } |
| 817 | break; |
| 818 | |
| 819 | case EbtInt: |
| 820 | if (rightUnionArray[i] == 0) |
| 821 | { |
| 822 | infoSink.info.message( |
| 823 | EPrefixWarning, getLine(), |
| 824 | "Divide by zero error during constant folding"); |
| 825 | tempConstArray[i].setIConst(INT_MAX); |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | tempConstArray[i].setIConst( |
| 830 | unionArray[i].getIConst() / |
| 831 | rightUnionArray[i].getIConst()); |
| 832 | } |
| 833 | break; |
| 834 | |
| 835 | case EbtUInt: |
| 836 | if (rightUnionArray[i] == 0) |
| 837 | { |
| 838 | infoSink.info.message( |
| 839 | EPrefixWarning, getLine(), |
| 840 | "Divide by zero error during constant folding"); |
| 841 | tempConstArray[i].setUConst(UINT_MAX); |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | tempConstArray[i].setUConst( |
| 846 | unionArray[i].getUConst() / |
| 847 | rightUnionArray[i].getUConst()); |
| 848 | } |
| 849 | break; |
| 850 | |
| 851 | default: |
| 852 | infoSink.info.message( |
| 853 | EPrefixInternalError, getLine(), |
| 854 | "Constant folding cannot be done for \"/\""); |
| 855 | return NULL; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | break; |
| 860 | |
| 861 | case EOpMatrixTimesVector: |
| 862 | { |
| 863 | if (node->getBasicType() != EbtFloat) |
| 864 | { |
| 865 | infoSink.info.message( |
| 866 | EPrefixInternalError, getLine(), |
| 867 | "Constant Folding cannot be done for matrix times vector"); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | const int matrixCols = getCols(); |
| 872 | const int matrixRows = getRows(); |
| 873 | |
| 874 | tempConstArray = new ConstantUnion[matrixRows]; |
| 875 | |
| 876 | for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++) |
| 877 | { |
| 878 | tempConstArray[matrixRow].setFConst(0.0f); |
| 879 | for (int col = 0; col < matrixCols; col++) |
| 880 | { |
| 881 | tempConstArray[matrixRow].setFConst( |
| 882 | tempConstArray[matrixRow].getFConst() + |
| 883 | unionArray[col * matrixRows + matrixRow].getFConst() * |
| 884 | rightUnionArray[col].getFConst()); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | returnType = node->getType(); |
| 889 | returnType.setPrimarySize(matrixRows); |
| 890 | |
| 891 | tempNode = new TIntermConstantUnion(tempConstArray, returnType); |
| 892 | tempNode->setLine(getLine()); |
| 893 | |
| 894 | return tempNode; |
| 895 | } |
| 896 | |
| 897 | case EOpVectorTimesMatrix: |
| 898 | { |
| 899 | if (getType().getBasicType() != EbtFloat) |
| 900 | { |
| 901 | infoSink.info.message( |
| 902 | EPrefixInternalError, getLine(), |
| 903 | "Constant Folding cannot be done for vector times matrix"); |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | const int matrixCols = constantNode->getType().getCols(); |
| 908 | const int matrixRows = constantNode->getType().getRows(); |
| 909 | |
| 910 | tempConstArray = new ConstantUnion[matrixCols]; |
| 911 | |
| 912 | for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++) |
| 913 | { |
| 914 | tempConstArray[matrixCol].setFConst(0.0f); |
| 915 | for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++) |
| 916 | { |
| 917 | tempConstArray[matrixCol].setFConst( |
| 918 | tempConstArray[matrixCol].getFConst() + |
| 919 | unionArray[matrixRow].getFConst() * |
| 920 | rightUnionArray[matrixCol * matrixRows + matrixRow].getFConst()); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | returnType.setPrimarySize(matrixCols); |
| 925 | } |
| 926 | break; |
| 927 | |
| 928 | case EOpLogicalAnd: |
| 929 | // this code is written for possible future use, |
| 930 | // will not get executed currently |
| 931 | { |
| 932 | tempConstArray = new ConstantUnion[objectSize]; |
| 933 | for (size_t i = 0; i < objectSize; i++) |
| 934 | { |
| 935 | tempConstArray[i] = unionArray[i] && rightUnionArray[i]; |
| 936 | } |
| 937 | } |
| 938 | break; |
| 939 | |
| 940 | case EOpLogicalOr: |
| 941 | // this code is written for possible future use, |
| 942 | // will not get executed currently |
| 943 | { |
| 944 | tempConstArray = new ConstantUnion[objectSize]; |
| 945 | for (size_t i = 0; i < objectSize; i++) |
| 946 | { |
| 947 | tempConstArray[i] = unionArray[i] || rightUnionArray[i]; |
| 948 | } |
| 949 | } |
| 950 | break; |
| 951 | |
| 952 | case EOpLogicalXor: |
| 953 | { |
| 954 | tempConstArray = new ConstantUnion[objectSize]; |
| 955 | for (size_t i = 0; i < objectSize; i++) |
| 956 | { |
| 957 | switch (getType().getBasicType()) |
| 958 | { |
| 959 | case EbtBool: |
| 960 | tempConstArray[i].setBConst( |
| 961 | unionArray[i] == rightUnionArray[i] ? false : true); |
| 962 | break; |
| 963 | default: |
| 964 | UNREACHABLE(); |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | break; |
| 970 | |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 971 | case EOpBitwiseAnd: |
| 972 | tempConstArray = new ConstantUnion[objectSize]; |
| 973 | for (size_t i = 0; i < objectSize; i++) |
| 974 | tempConstArray[i] = unionArray[i] & rightUnionArray[i]; |
| 975 | break; |
| 976 | case EOpBitwiseXor: |
| 977 | tempConstArray = new ConstantUnion[objectSize]; |
| 978 | for (size_t i = 0; i < objectSize; i++) |
| 979 | tempConstArray[i] = unionArray[i] ^ rightUnionArray[i]; |
| 980 | break; |
| 981 | case EOpBitwiseOr: |
| 982 | tempConstArray = new ConstantUnion[objectSize]; |
| 983 | for (size_t i = 0; i < objectSize; i++) |
| 984 | tempConstArray[i] = unionArray[i] | rightUnionArray[i]; |
| 985 | break; |
| 986 | case EOpBitShiftLeft: |
| 987 | tempConstArray = new ConstantUnion[objectSize]; |
| 988 | for (size_t i = 0; i < objectSize; i++) |
| 989 | tempConstArray[i] = unionArray[i] << rightUnionArray[i]; |
| 990 | break; |
| 991 | case EOpBitShiftRight: |
| 992 | tempConstArray = new ConstantUnion[objectSize]; |
| 993 | for (size_t i = 0; i < objectSize; i++) |
| 994 | tempConstArray[i] = unionArray[i] >> rightUnionArray[i]; |
| 995 | break; |
| 996 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 997 | case EOpLessThan: |
| 998 | ASSERT(objectSize == 1); |
| 999 | tempConstArray = new ConstantUnion[1]; |
| 1000 | tempConstArray->setBConst(*unionArray < *rightUnionArray); |
| 1001 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1002 | break; |
| 1003 | |
| 1004 | case EOpGreaterThan: |
| 1005 | ASSERT(objectSize == 1); |
| 1006 | tempConstArray = new ConstantUnion[1]; |
| 1007 | tempConstArray->setBConst(*unionArray > *rightUnionArray); |
| 1008 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1009 | break; |
| 1010 | |
| 1011 | case EOpLessThanEqual: |
| 1012 | { |
| 1013 | ASSERT(objectSize == 1); |
| 1014 | ConstantUnion constant; |
| 1015 | constant.setBConst(*unionArray > *rightUnionArray); |
| 1016 | tempConstArray = new ConstantUnion[1]; |
| 1017 | tempConstArray->setBConst(!constant.getBConst()); |
| 1018 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | case EOpGreaterThanEqual: |
| 1023 | { |
| 1024 | ASSERT(objectSize == 1); |
| 1025 | ConstantUnion constant; |
| 1026 | constant.setBConst(*unionArray < *rightUnionArray); |
| 1027 | tempConstArray = new ConstantUnion[1]; |
| 1028 | tempConstArray->setBConst(!constant.getBConst()); |
| 1029 | returnType = TType(EbtBool, EbpUndefined, EvqConst); |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | case EOpEqual: |
| 1034 | if (getType().getBasicType() == EbtStruct) |
| 1035 | { |
| 1036 | if (!CompareStructure(node->getType(), |
| 1037 | node->getUnionArrayPointer(), |
| 1038 | unionArray)) |
| 1039 | { |
| 1040 | boolNodeFlag = true; |
| 1041 | } |
| 1042 | } |
| 1043 | else |
| 1044 | { |
| 1045 | for (size_t i = 0; i < objectSize; i++) |
| 1046 | { |
| 1047 | if (unionArray[i] != rightUnionArray[i]) |
| 1048 | { |
| 1049 | boolNodeFlag = true; |
| 1050 | break; // break out of for loop |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | tempConstArray = new ConstantUnion[1]; |
| 1056 | if (!boolNodeFlag) |
| 1057 | { |
| 1058 | tempConstArray->setBConst(true); |
| 1059 | } |
| 1060 | else |
| 1061 | { |
| 1062 | tempConstArray->setBConst(false); |
| 1063 | } |
| 1064 | |
| 1065 | tempNode = new TIntermConstantUnion( |
| 1066 | tempConstArray, TType(EbtBool, EbpUndefined, EvqConst)); |
| 1067 | tempNode->setLine(getLine()); |
| 1068 | |
| 1069 | return tempNode; |
| 1070 | |
| 1071 | case EOpNotEqual: |
| 1072 | if (getType().getBasicType() == EbtStruct) |
| 1073 | { |
| 1074 | if (CompareStructure(node->getType(), |
| 1075 | node->getUnionArrayPointer(), |
| 1076 | unionArray)) |
| 1077 | { |
| 1078 | boolNodeFlag = true; |
| 1079 | } |
| 1080 | } |
| 1081 | else |
| 1082 | { |
| 1083 | for (size_t i = 0; i < objectSize; i++) |
| 1084 | { |
| 1085 | if (unionArray[i] == rightUnionArray[i]) |
| 1086 | { |
| 1087 | boolNodeFlag = true; |
| 1088 | break; // break out of for loop |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | tempConstArray = new ConstantUnion[1]; |
| 1094 | if (!boolNodeFlag) |
| 1095 | { |
| 1096 | tempConstArray->setBConst(true); |
| 1097 | } |
| 1098 | else |
| 1099 | { |
| 1100 | tempConstArray->setBConst(false); |
| 1101 | } |
| 1102 | |
| 1103 | tempNode = new TIntermConstantUnion( |
| 1104 | tempConstArray, TType(EbtBool, EbpUndefined, EvqConst)); |
| 1105 | tempNode->setLine(getLine()); |
| 1106 | |
| 1107 | return tempNode; |
| 1108 | |
| 1109 | default: |
| 1110 | infoSink.info.message( |
| 1111 | EPrefixInternalError, getLine(), |
| 1112 | "Invalid operator for constant folding"); |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | tempNode = new TIntermConstantUnion(tempConstArray, returnType); |
| 1116 | tempNode->setLine(getLine()); |
| 1117 | |
| 1118 | return tempNode; |
| 1119 | } |
| 1120 | else |
| 1121 | { |
| 1122 | // |
| 1123 | // Do unary operations |
| 1124 | // |
| 1125 | TIntermConstantUnion *newNode = 0; |
| 1126 | ConstantUnion* tempConstArray = new ConstantUnion[objectSize]; |
| 1127 | for (size_t i = 0; i < objectSize; i++) |
| 1128 | { |
| 1129 | switch(op) |
| 1130 | { |
| 1131 | case EOpNegative: |
| 1132 | switch (getType().getBasicType()) |
| 1133 | { |
| 1134 | case EbtFloat: |
| 1135 | tempConstArray[i].setFConst(-unionArray[i].getFConst()); |
| 1136 | break; |
| 1137 | case EbtInt: |
| 1138 | tempConstArray[i].setIConst(-unionArray[i].getIConst()); |
| 1139 | break; |
| 1140 | case EbtUInt: |
| 1141 | tempConstArray[i].setUConst(static_cast<unsigned int>( |
| 1142 | -static_cast<int>(unionArray[i].getUConst()))); |
| 1143 | break; |
| 1144 | default: |
| 1145 | infoSink.info.message( |
| 1146 | EPrefixInternalError, getLine(), |
| 1147 | "Unary operation not folded into constant"); |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | break; |
| 1151 | |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 1152 | case EOpPositive: |
| 1153 | switch (getType().getBasicType()) |
| 1154 | { |
| 1155 | case EbtFloat: |
| 1156 | tempConstArray[i].setFConst(unionArray[i].getFConst()); |
| 1157 | break; |
| 1158 | case EbtInt: |
| 1159 | tempConstArray[i].setIConst(unionArray[i].getIConst()); |
| 1160 | break; |
| 1161 | case EbtUInt: |
| 1162 | tempConstArray[i].setUConst(static_cast<unsigned int>( |
| 1163 | static_cast<int>(unionArray[i].getUConst()))); |
| 1164 | break; |
| 1165 | default: |
| 1166 | infoSink.info.message( |
| 1167 | EPrefixInternalError, getLine(), |
| 1168 | "Unary operation not folded into constant"); |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | break; |
| 1172 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 1173 | case EOpLogicalNot: |
| 1174 | // this code is written for possible future use, |
| 1175 | // will not get executed currently |
| 1176 | switch (getType().getBasicType()) |
| 1177 | { |
| 1178 | case EbtBool: |
| 1179 | tempConstArray[i].setBConst(!unionArray[i].getBConst()); |
| 1180 | break; |
| 1181 | default: |
| 1182 | infoSink.info.message( |
| 1183 | EPrefixInternalError, getLine(), |
| 1184 | "Unary operation not folded into constant"); |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | break; |
| 1188 | |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1189 | case EOpBitwiseNot: |
| 1190 | switch (getType().getBasicType()) |
| 1191 | { |
| 1192 | case EbtInt: |
| 1193 | tempConstArray[i].setIConst(~unionArray[i].getIConst()); |
| 1194 | break; |
| 1195 | case EbtUInt: |
| 1196 | tempConstArray[i].setUConst(~unionArray[i].getUConst()); |
| 1197 | break; |
| 1198 | default: |
| 1199 | infoSink.info.message( |
| 1200 | EPrefixInternalError, getLine(), |
| 1201 | "Unary operation not folded into constant"); |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | break; |
| 1205 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 1206 | default: |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | } |
| 1210 | newNode = new TIntermConstantUnion(tempConstArray, getType()); |
| 1211 | newNode->setLine(getLine()); |
| 1212 | return newNode; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // static |
| 1217 | TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction) |
| 1218 | { |
| 1219 | if (hashFunction == NULL || name.empty()) |
| 1220 | return name; |
| 1221 | khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length()); |
| 1222 | TStringStream stream; |
| 1223 | stream << HASHED_NAME_PREFIX << std::hex << number; |
| 1224 | TString hashedName = stream.str(); |
| 1225 | return hashedName; |
| 1226 | } |
Olli Etuaho | 853dc1a | 2014-11-06 17:25:48 +0200 | [diff] [blame] | 1227 | |
| 1228 | void TIntermTraverser::updateTree() |
| 1229 | { |
| 1230 | for (size_t ii = 0; ii < mReplacements.size(); ++ii) |
| 1231 | { |
| 1232 | const NodeUpdateEntry& entry = mReplacements[ii]; |
| 1233 | ASSERT(entry.parent); |
| 1234 | bool replaced = entry.parent->replaceChildNode( |
| 1235 | entry.original, entry.replacement); |
| 1236 | ASSERT(replaced); |
| 1237 | |
| 1238 | if (!entry.originalBecomesChildOfReplacement) |
| 1239 | { |
| 1240 | // In AST traversing, a parent is visited before its children. |
| 1241 | // After we replace a node, if an immediate child is to |
| 1242 | // be replaced, we need to make sure we don't update the replaced |
| 1243 | // node; instead, we update the replacement node. |
| 1244 | for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj) |
| 1245 | { |
| 1246 | NodeUpdateEntry& entry2 = mReplacements[jj]; |
| 1247 | if (entry2.parent == entry.original) |
| 1248 | entry2.parent = entry.replacement; |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | } |