blob: 779565b81fd058e73b788bb01c933d3a1a2c84ff [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
13#include <algorithm>
14
Nicolas Capenscc863da2015-01-21 15:50:55 -050015#include "localintermediate.h"
16#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040017
18bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray);
19
20static TPrecision GetHigherPrecision( TPrecision left, TPrecision right ){
21 return left > right ? left : right;
22}
23
Alexis Hetu00106d42015-04-23 11:45:35 -040024static bool ValidateMultiplication(TOperator op, const TType &left, const TType &right)
25{
26 switch(op)
27 {
28 case EOpMul:
29 case EOpMulAssign:
30 return left.getNominalSize() == right.getNominalSize() &&
31 left.getSecondarySize() == right.getSecondarySize();
32 case EOpVectorTimesScalar:
33 case EOpVectorTimesScalarAssign:
34 return true;
35 case EOpVectorTimesMatrix:
36 return left.getNominalSize() == right.getSecondarySize();
37 case EOpVectorTimesMatrixAssign:
38 return left.getNominalSize() == right.getSecondarySize() &&
39 left.getNominalSize() == right.getNominalSize();
40 case EOpMatrixTimesVector:
41 return left.getNominalSize() == right.getNominalSize();
42 case EOpMatrixTimesScalar:
43 case EOpMatrixTimesScalarAssign:
44 return true;
45 case EOpMatrixTimesMatrix:
46 return left.getNominalSize() == right.getSecondarySize();
47 case EOpMatrixTimesMatrixAssign:
48 return left.getNominalSize() == right.getNominalSize() &&
49 left.getSecondarySize() == right.getSecondarySize();
50 default:
51 UNREACHABLE();
52 return false;
53 }
54}
55
John Bauman66b8ab22014-05-06 15:57:45 -040056const char* getOperatorString(TOperator op) {
57 switch (op) {
58 case EOpInitialize: return "=";
59 case EOpAssign: return "=";
60 case EOpAddAssign: return "+=";
61 case EOpSubAssign: return "-=";
62 case EOpDivAssign: return "/=";
Alexis Hetu17809052015-05-13 11:28:22 -040063 case EOpIModAssign: return "%=";
64 case EOpBitShiftLeftAssign: return "<<=";
65 case EOpBitShiftRightAssign: return ">>=";
66 case EOpBitwiseAndAssign: return "&=";
67 case EOpBitwiseXorAssign: return "^=";
68 case EOpBitwiseOrAssign: return "|=";
John Bauman66b8ab22014-05-06 15:57:45 -040069
70 // Fall-through.
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040071 case EOpMulAssign:
John Bauman66b8ab22014-05-06 15:57:45 -040072 case EOpVectorTimesMatrixAssign:
73 case EOpVectorTimesScalarAssign:
74 case EOpMatrixTimesScalarAssign:
75 case EOpMatrixTimesMatrixAssign: return "*=";
76
77 // Fall-through.
78 case EOpIndexDirect:
79 case EOpIndexIndirect: return "[]";
80
81 case EOpIndexDirectStruct: return ".";
82 case EOpVectorSwizzle: return ".";
83 case EOpAdd: return "+";
84 case EOpSub: return "-";
85 case EOpMul: return "*";
86 case EOpDiv: return "/";
87 case EOpMod: UNIMPLEMENTED(); break;
88 case EOpEqual: return "==";
89 case EOpNotEqual: return "!=";
90 case EOpLessThan: return "<";
91 case EOpGreaterThan: return ">";
92 case EOpLessThanEqual: return "<=";
93 case EOpGreaterThanEqual: return ">=";
94
95 // Fall-through.
96 case EOpVectorTimesScalar:
97 case EOpVectorTimesMatrix:
98 case EOpMatrixTimesVector:
99 case EOpMatrixTimesScalar:
100 case EOpMatrixTimesMatrix: return "*";
101
102 case EOpLogicalOr: return "||";
103 case EOpLogicalXor: return "^^";
104 case EOpLogicalAnd: return "&&";
Alexis Hetu17809052015-05-13 11:28:22 -0400105 case EOpIMod: return "%";
106 case EOpBitShiftLeft: return "<<";
107 case EOpBitShiftRight: return ">>";
108 case EOpBitwiseAnd: return "&";
109 case EOpBitwiseXor: return "^";
110 case EOpBitwiseOr: return "|";
John Bauman66b8ab22014-05-06 15:57:45 -0400111 case EOpNegative: return "-";
112 case EOpVectorLogicalNot: return "not";
113 case EOpLogicalNot: return "!";
Alexis Hetu17809052015-05-13 11:28:22 -0400114 case EOpBitwiseNot: return "~";
John Bauman66b8ab22014-05-06 15:57:45 -0400115 case EOpPostIncrement: return "++";
116 case EOpPostDecrement: return "--";
117 case EOpPreIncrement: return "++";
118 case EOpPreDecrement: return "--";
119
John Bauman66b8ab22014-05-06 15:57:45 -0400120 case EOpRadians: return "radians";
121 case EOpDegrees: return "degrees";
122 case EOpSin: return "sin";
123 case EOpCos: return "cos";
124 case EOpTan: return "tan";
125 case EOpAsin: return "asin";
126 case EOpAcos: return "acos";
127 case EOpAtan: return "atan";
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400128 case EOpSinh: return "sinh";
129 case EOpCosh: return "cosh";
130 case EOpTanh: return "tanh";
131 case EOpAsinh: return "asinh";
132 case EOpAcosh: return "acosh";
133 case EOpAtanh: return "atanh";
John Bauman66b8ab22014-05-06 15:57:45 -0400134 case EOpExp: return "exp";
135 case EOpLog: return "log";
136 case EOpExp2: return "exp2";
137 case EOpLog2: return "log2";
138 case EOpSqrt: return "sqrt";
139 case EOpInverseSqrt: return "inversesqrt";
140 case EOpAbs: return "abs";
141 case EOpSign: return "sign";
142 case EOpFloor: return "floor";
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400143 case EOpTrunc: return "trunc";
144 case EOpRound: return "round";
145 case EOpRoundEven: return "roundEven";
John Bauman66b8ab22014-05-06 15:57:45 -0400146 case EOpCeil: return "ceil";
147 case EOpFract: return "fract";
148 case EOpLength: return "length";
149 case EOpNormalize: return "normalize";
150 case EOpDFdx: return "dFdx";
151 case EOpDFdy: return "dFdy";
152 case EOpFwidth: return "fwidth";
153 case EOpAny: return "any";
154 case EOpAll: return "all";
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400155 case EOpIsNan: return "isnan";
156 case EOpIsInf: return "isinf";
John Bauman66b8ab22014-05-06 15:57:45 -0400157
158 default: break;
159 }
160 return "";
161}
162
163////////////////////////////////////////////////////////////////////////////
164//
165// First set of functions are to help build the intermediate representation.
166// These functions are not member functions of the nodes.
167// They are called from parser productions.
168//
169/////////////////////////////////////////////////////////////////////////////
170
171//
172// Add a terminal node for an identifier in an expression.
173//
174// Returns the added node.
175//
176TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, TSourceLoc line)
177{
178 TIntermSymbol* node = new TIntermSymbol(id, name, type);
179 node->setLine(line);
180
181 return node;
182}
183
184//
185// Connect two nodes with a new parent that does a binary operation on the nodes.
186//
187// Returns the added node.
188//
John Baumand4ae8632014-05-06 16:18:33 -0400189TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -0400190{
191 switch (op) {
192 case EOpEqual:
193 case EOpNotEqual:
194 if (left->isArray())
195 return 0;
196 break;
197 case EOpLessThan:
198 case EOpGreaterThan:
199 case EOpLessThanEqual:
200 case EOpGreaterThanEqual:
201 if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) {
202 return 0;
203 }
204 break;
205 case EOpLogicalOr:
206 case EOpLogicalXor:
207 case EOpLogicalAnd:
208 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) {
209 return 0;
210 }
211 break;
Alexis Hetud061e422015-05-13 16:37:50 -0400212 case EOpBitwiseOr:
213 case EOpBitwiseXor:
214 case EOpBitwiseAnd:
Alexis Hetu9085c8d2015-06-01 13:48:07 -0400215 if (!IsInteger(left->getBasicType()) || left->isMatrix() || left->isArray()) {
Alexis Hetud061e422015-05-13 16:37:50 -0400216 return 0;
217 }
218 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400219 case EOpAdd:
220 case EOpSub:
221 case EOpDiv:
222 case EOpMul:
Alexis Hetud061e422015-05-13 16:37:50 -0400223 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool) {
John Bauman66b8ab22014-05-06 15:57:45 -0400224 return 0;
Alexis Hetud061e422015-05-13 16:37:50 -0400225 }
226 break;
227 case EOpIMod:
228 // Note that this is only for the % operator, not for mod()
229 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat) {
230 return 0;
231 }
232 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400233 default: break;
234 }
235
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400236 if (left->getBasicType() != right->getBasicType())
237 {
238 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400239 }
240
241 //
242 // Need a new node holding things together then. Make
243 // one and promote it to the right type.
244 //
245 TIntermBinary* node = new TIntermBinary(op);
246 if (line == 0)
247 line = right->getLine();
248 node->setLine(line);
249
250 node->setLeft(left);
251 node->setRight(right);
252 if (!node->promote(infoSink))
253 return 0;
254
255 //
256 // See if we can fold constants.
257 //
John Bauman66b8ab22014-05-06 15:57:45 -0400258 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
259 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
260 if (leftTempConstant && rightTempConstant) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400261 TIntermTyped *typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400262
263 if (typedReturnNode)
264 return typedReturnNode;
265 }
266
267 return node;
268}
269
270//
271// Connect two nodes through an assignment.
272//
273// Returns the added node.
274//
275TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
276{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400277 if (left->getType().getStruct() || right->getType().getStruct())
278 {
279 if (left->getType() != right->getType())
280 {
281 return 0;
282 }
283 }
284
John Bauman66b8ab22014-05-06 15:57:45 -0400285 TIntermBinary* node = new TIntermBinary(op);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400286 if(line == 0)
John Bauman66b8ab22014-05-06 15:57:45 -0400287 line = left->getLine();
288 node->setLine(line);
289
John Bauman66b8ab22014-05-06 15:57:45 -0400290 node->setLeft(left);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400291 node->setRight(right);
John Bauman66b8ab22014-05-06 15:57:45 -0400292 if (! node->promote(infoSink))
293 return 0;
294
295 return node;
296}
297
298//
299// Connect two nodes through an index operator, where the left node is the base
300// of an array or struct, and the right node is a direct or indirect offset.
301//
302// Returns the added node.
303// The caller should set the type of the returned node.
304//
305TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, TSourceLoc line)
306{
307 TIntermBinary* node = new TIntermBinary(op);
308 if (line == 0)
309 line = index->getLine();
310 node->setLine(line);
311 node->setLeft(base);
312 node->setRight(index);
313
314 // caller should set the type
315
316 return node;
317}
318
319//
320// Add one node as the parent of another that it operates on.
321//
322// Returns the added node.
323//
John Baumand4ae8632014-05-06 16:18:33 -0400324TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -0400325{
326 TIntermUnary* node;
327 TIntermTyped* child = childNode->getAsTyped();
328
329 if (child == 0) {
330 infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line);
331 return 0;
332 }
333
334 switch (op) {
Alexis Hetud061e422015-05-13 16:37:50 -0400335 case EOpBitwiseNot:
Alexis Hetu9085c8d2015-06-01 13:48:07 -0400336 if (!IsInteger(child->getType().getBasicType()) || child->getType().isMatrix() || child->getType().isArray()) {
Alexis Hetud061e422015-05-13 16:37:50 -0400337 return 0;
338 }
339 break;
340
John Bauman66b8ab22014-05-06 15:57:45 -0400341 case EOpLogicalNot:
342 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
343 return 0;
344 }
345 break;
346
347 case EOpPostIncrement:
348 case EOpPreIncrement:
349 case EOpPostDecrement:
350 case EOpPreDecrement:
351 case EOpNegative:
352 if (child->getType().getBasicType() == EbtStruct || child->getType().isArray())
353 return 0;
354 default: break;
355 }
356
John Bauman66b8ab22014-05-06 15:57:45 -0400357 TIntermConstantUnion *childTempConstant = 0;
358 if (child->getAsConstantUnion())
359 childTempConstant = child->getAsConstantUnion();
360
361 //
362 // Make a new node for the operator.
363 //
364 node = new TIntermUnary(op);
365 if (line == 0)
366 line = child->getLine();
367 node->setLine(line);
368 node->setOperand(child);
369
370 if (! node->promote(infoSink))
371 return 0;
372
373 if (childTempConstant) {
374 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
375
376 if (newChild)
377 return newChild;
378 }
379
380 return node;
381}
382
383//
384// This is the safe way to change the operator on an aggregate, as it
385// does lots of error checking and fixing. Especially for establishing
386// a function call's operation on it's set of parameters. Sequences
387// of instructions are also aggregates, but they just direnctly set
388// their operator to EOpSequence.
389//
390// Returns an aggregate node, which could be the one passed in if
391// it was already an aggregate but no operator was set.
392//
393TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, TSourceLoc line)
394{
395 TIntermAggregate* aggNode;
396
397 //
398 // Make sure we have an aggregate. If not turn it into one.
399 //
400 if (node) {
401 aggNode = node->getAsAggregate();
402 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
403 //
404 // Make an aggregate containing this node.
405 //
406 aggNode = new TIntermAggregate();
407 aggNode->getSequence().push_back(node);
408 if (line == 0)
409 line = node->getLine();
410 }
411 } else
412 aggNode = new TIntermAggregate();
413
414 //
415 // Set the operator.
416 //
417 aggNode->setOp(op);
418 if (line != 0)
419 aggNode->setLine(line);
420
421 return aggNode;
422}
423
424//
John Bauman66b8ab22014-05-06 15:57:45 -0400425// Safe way to combine two nodes into an aggregate. Works with null pointers,
426// a node that's not a aggregate yet, etc.
427//
428// Returns the resulting aggregate, unless 0 was passed in for
429// both existing nodes.
430//
431TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, TSourceLoc line)
432{
433 if (left == 0 && right == 0)
434 return 0;
435
436 TIntermAggregate* aggNode = 0;
437 if (left)
438 aggNode = left->getAsAggregate();
439 if (!aggNode || aggNode->getOp() != EOpNull) {
440 aggNode = new TIntermAggregate;
441 if (left)
442 aggNode->getSequence().push_back(left);
443 }
444
445 if (right)
446 aggNode->getSequence().push_back(right);
447
448 if (line != 0)
449 aggNode->setLine(line);
450
451 return aggNode;
452}
453
454//
455// Turn an existing node into an aggregate.
456//
457// Returns an aggregate, unless 0 was passed in for the existing node.
458//
459TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, TSourceLoc line)
460{
461 if (node == 0)
462 return 0;
463
464 TIntermAggregate* aggNode = new TIntermAggregate;
465 aggNode->getSequence().push_back(node);
466
467 if (line != 0)
468 aggNode->setLine(line);
469 else
470 aggNode->setLine(node->getLine());
471
472 return aggNode;
473}
474
475//
476// For "if" test nodes. There are three children; a condition,
477// a true path, and a false path. The two paths are in the
478// nodePair.
479//
480// Returns the selection node created.
481//
482TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, TSourceLoc line)
483{
484 //
485 // For compile time constant selections, prune the code and
486 // test now.
487 //
488
489 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500490 if (cond->getAsConstantUnion()->getBConst(0) == true)
John Bauman66b8ab22014-05-06 15:57:45 -0400491 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
492 else
493 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
494 }
495
496 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
497 node->setLine(line);
498
499 return node;
500}
501
502
503TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
504{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500505 if (left->getType().getQualifier() == EvqConstExpr && right->getType().getQualifier() == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400506 return right;
507 } else {
508 TIntermTyped *commaAggregate = growAggregate(left, right, line);
509 commaAggregate->getAsAggregate()->setOp(EOpComma);
510 commaAggregate->setType(right->getType());
511 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
512 return commaAggregate;
513 }
514}
515
516//
517// For "?:" test nodes. There are three children; a condition,
518// a true path, and a false path. The two paths are specified
519// as separate parameters.
520//
521// Returns the selection node created, or 0 if one could not be.
522//
523TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc line)
524{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400525 if (trueBlock->getType() != falseBlock->getType())
526 {
527 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400528 }
529
530 //
531 // See if all the operands are constant, then fold it otherwise not.
532 //
533
534 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500535 if (cond->getAsConstantUnion()->getBConst(0))
John Bauman66b8ab22014-05-06 15:57:45 -0400536 return trueBlock;
537 else
538 return falseBlock;
539 }
540
541 //
542 // Make a selection node.
543 //
544 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
545 node->getTypePointer()->setQualifier(EvqTemporary);
546 node->setLine(line);
547
548 return node;
549}
550
551//
552// Constant terminal nodes. Has a union that contains bool, float or int constants
553//
554// Returns the constant union node created.
555//
556
557TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, TSourceLoc line)
558{
559 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
560 node->setLine(line);
561
562 return node;
563}
564
565TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, TSourceLoc line)
566{
567
568 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
569
570 node->setLine(line);
571 TIntermConstantUnion* constIntNode;
572 TIntermSequence &sequenceVector = node->getSequence();
573 ConstantUnion* unionArray;
574
575 for (int i = 0; i < fields.num; i++) {
576 unionArray = new ConstantUnion[1];
577 unionArray->setIConst(fields.offsets[i]);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500578 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), line);
John Bauman66b8ab22014-05-06 15:57:45 -0400579 sequenceVector.push_back(constIntNode);
580 }
581
582 return node;
583}
584
585//
586// Create loop nodes.
587//
588TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, TSourceLoc line)
589{
590 TIntermNode* node = new TIntermLoop(type, init, cond, expr, body);
591 node->setLine(line);
592
593 return node;
594}
595
596//
597// Add branches.
598//
599TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TSourceLoc line)
600{
601 return addBranch(branchOp, 0, line);
602}
603
604TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, TSourceLoc line)
605{
606 TIntermBranch* node = new TIntermBranch(branchOp, expression);
607 node->setLine(line);
608
609 return node;
610}
611
612//
613// This is to be executed once the final root is put on top by the parsing
614// process.
615//
616bool TIntermediate::postProcess(TIntermNode* root)
617{
618 if (root == 0)
619 return true;
620
621 //
622 // First, finish off the top level sequence, if any
623 //
624 TIntermAggregate* aggRoot = root->getAsAggregate();
625 if (aggRoot && aggRoot->getOp() == EOpNull)
626 aggRoot->setOp(EOpSequence);
627
628 return true;
629}
630
John Bauman66b8ab22014-05-06 15:57:45 -0400631////////////////////////////////////////////////////////////////
632//
633// Member functions of the nodes used for building the tree.
634//
635////////////////////////////////////////////////////////////////
636
637//
638// Say whether or not an operation node changes the value of a variable.
639//
640// Returns true if state is modified.
641//
642bool TIntermOperator::modifiesState() const
643{
644 switch (op) {
645 case EOpPostIncrement:
646 case EOpPostDecrement:
647 case EOpPreIncrement:
648 case EOpPreDecrement:
649 case EOpAssign:
650 case EOpAddAssign:
651 case EOpSubAssign:
652 case EOpMulAssign:
653 case EOpVectorTimesMatrixAssign:
654 case EOpVectorTimesScalarAssign:
655 case EOpMatrixTimesScalarAssign:
656 case EOpMatrixTimesMatrixAssign:
657 case EOpDivAssign:
Alexis Hetu17809052015-05-13 11:28:22 -0400658 case EOpIModAssign:
659 case EOpBitShiftLeftAssign:
660 case EOpBitShiftRightAssign:
661 case EOpBitwiseAndAssign:
662 case EOpBitwiseXorAssign:
663 case EOpBitwiseOrAssign:
John Bauman66b8ab22014-05-06 15:57:45 -0400664 return true;
665 default:
666 return false;
667 }
668}
669
670//
671// returns true if the operator is for one of the constructors
672//
673bool TIntermOperator::isConstructor() const
674{
675 switch (op) {
676 case EOpConstructVec2:
677 case EOpConstructVec3:
678 case EOpConstructVec4:
679 case EOpConstructMat2:
Alexis Hetu00106d42015-04-23 11:45:35 -0400680 case EOpConstructMat2x3:
681 case EOpConstructMat2x4:
682 case EOpConstructMat3x2:
John Bauman66b8ab22014-05-06 15:57:45 -0400683 case EOpConstructMat3:
Alexis Hetu00106d42015-04-23 11:45:35 -0400684 case EOpConstructMat3x4:
685 case EOpConstructMat4x2:
686 case EOpConstructMat4x3:
John Bauman66b8ab22014-05-06 15:57:45 -0400687 case EOpConstructMat4:
688 case EOpConstructFloat:
689 case EOpConstructIVec2:
690 case EOpConstructIVec3:
691 case EOpConstructIVec4:
692 case EOpConstructInt:
Nicolas Capense4b1b1d2015-02-17 17:26:01 -0500693 case EOpConstructUVec2:
694 case EOpConstructUVec3:
695 case EOpConstructUVec4:
Nicolas Capens3c20f802015-02-17 17:17:20 -0500696 case EOpConstructUInt:
John Bauman66b8ab22014-05-06 15:57:45 -0400697 case EOpConstructBVec2:
698 case EOpConstructBVec3:
699 case EOpConstructBVec4:
700 case EOpConstructBool:
701 case EOpConstructStruct:
702 return true;
703 default:
704 return false;
705 }
706}
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400707
John Bauman66b8ab22014-05-06 15:57:45 -0400708//
709// Make sure the type of a unary operator is appropriate for its
710// combination of operation and operand type.
711//
712// Returns false in nothing makes sense.
713//
714bool TIntermUnary::promote(TInfoSink&)
715{
716 switch (op) {
717 case EOpLogicalNot:
718 if (operand->getBasicType() != EbtBool)
719 return false;
720 break;
Alexis Hetud061e422015-05-13 16:37:50 -0400721 case EOpBitwiseNot:
Alexis Hetu9085c8d2015-06-01 13:48:07 -0400722 if (!IsInteger(operand->getBasicType()))
Alexis Hetud061e422015-05-13 16:37:50 -0400723 return false;
724 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400725 case EOpNegative:
726 case EOpPostIncrement:
727 case EOpPostDecrement:
728 case EOpPreIncrement:
729 case EOpPreDecrement:
730 if (operand->getBasicType() == EbtBool)
731 return false;
732 break;
733
734 // operators for built-ins are already type checked against their prototype
735 case EOpAny:
736 case EOpAll:
737 case EOpVectorLogicalNot:
738 return true;
739
740 default:
741 if (operand->getBasicType() != EbtFloat)
742 return false;
743 }
744
745 setType(operand->getType());
746
747 // Unary operations results in temporary variables unless const.
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500748 if (operand->getQualifier() != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400749 getTypePointer()->setQualifier(EvqTemporary);
750 }
751
752 return true;
753}
754
755//
756// Establishes the type of the resultant operation, as well as
757// makes the operator the correct one for the operands.
758//
759// Returns false if operator can't work on operands.
760//
761bool TIntermBinary::promote(TInfoSink& infoSink)
762{
763 // This function only handles scalars, vectors, and matrices.
764 if (left->isArray() || right->isArray()) {
765 infoSink.info.message(EPrefixInternalError, "Invalid operation for arrays", getLine());
766 return false;
767 }
768
769 // GLSL ES 2.0 does not support implicit type casting.
770 // So the basic type should always match.
771 if (left->getBasicType() != right->getBasicType())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400772 {
John Bauman66b8ab22014-05-06 15:57:45 -0400773 return false;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400774 }
John Bauman66b8ab22014-05-06 15:57:45 -0400775
776 //
777 // Base assumption: just make the type the same as the left
778 // operand. Then only deviations from this need be coded.
779 //
780 setType(left->getType());
781
782 // The result gets promoted to the highest precision.
783 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
784 getTypePointer()->setPrecision(higherPrecision);
785
786 // Binary operations results in temporary variables unless both
787 // operands are const.
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500788 if (left->getQualifier() != EvqConstExpr || right->getQualifier() != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400789 getTypePointer()->setQualifier(EvqTemporary);
790 }
791
Alexis Hetud061e422015-05-13 16:37:50 -0400792 int primarySize = std::max(left->getNominalSize(), right->getNominalSize());
John Bauman66b8ab22014-05-06 15:57:45 -0400793
794 //
795 // All scalars. Code after this test assumes this case is removed!
796 //
Alexis Hetud061e422015-05-13 16:37:50 -0400797 if (primarySize == 1) {
John Bauman66b8ab22014-05-06 15:57:45 -0400798 switch (op) {
799 //
800 // Promote to conditional
801 //
802 case EOpEqual:
803 case EOpNotEqual:
804 case EOpLessThan:
805 case EOpGreaterThan:
806 case EOpLessThanEqual:
807 case EOpGreaterThanEqual:
808 setType(TType(EbtBool, EbpUndefined));
809 break;
810
811 //
812 // And and Or operate on conditionals
813 //
814 case EOpLogicalAnd:
815 case EOpLogicalOr:
Alexis Hetud061e422015-05-13 16:37:50 -0400816 case EOpLogicalXor:
John Bauman66b8ab22014-05-06 15:57:45 -0400817 // Both operands must be of type bool.
818 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
819 return false;
820 setType(TType(EbtBool, EbpUndefined));
821 break;
822
823 default:
824 break;
825 }
826 return true;
827 }
828
829 // If we reach here, at least one of the operands is vector or matrix.
830 // The other operand could be a scalar, vector, or matrix.
John Bauman66b8ab22014-05-06 15:57:45 -0400831 // Can these two operands be combined?
832 //
833 TBasicType basicType = left->getBasicType();
834 switch (op) {
835 case EOpMul:
836 if (!left->isMatrix() && right->isMatrix()) {
837 if (left->isVector())
Alexis Hetu00106d42015-04-23 11:45:35 -0400838 {
John Bauman66b8ab22014-05-06 15:57:45 -0400839 op = EOpVectorTimesMatrix;
Alexis Hetu00106d42015-04-23 11:45:35 -0400840 setType(TType(basicType, higherPrecision, EvqTemporary,
841 static_cast<unsigned char>(right->getNominalSize()), 1));
842 }
John Bauman66b8ab22014-05-06 15:57:45 -0400843 else {
844 op = EOpMatrixTimesScalar;
Alexis Hetu00106d42015-04-23 11:45:35 -0400845 setType(TType(basicType, higherPrecision, EvqTemporary,
846 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(right->getSecondarySize())));
John Bauman66b8ab22014-05-06 15:57:45 -0400847 }
848 } else if (left->isMatrix() && !right->isMatrix()) {
849 if (right->isVector()) {
850 op = EOpMatrixTimesVector;
Alexis Hetu00106d42015-04-23 11:45:35 -0400851 setType(TType(basicType, higherPrecision, EvqTemporary,
852 static_cast<unsigned char>(left->getSecondarySize()), 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400853 } else {
854 op = EOpMatrixTimesScalar;
855 }
856 } else if (left->isMatrix() && right->isMatrix()) {
857 op = EOpMatrixTimesMatrix;
Alexis Hetu00106d42015-04-23 11:45:35 -0400858 setType(TType(basicType, higherPrecision, EvqTemporary,
859 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(left->getSecondarySize())));
John Bauman66b8ab22014-05-06 15:57:45 -0400860 } else if (!left->isMatrix() && !right->isMatrix()) {
861 if (left->isVector() && right->isVector()) {
862 // leave as component product
863 } else if (left->isVector() || right->isVector()) {
864 op = EOpVectorTimesScalar;
Alexis Hetu00106d42015-04-23 11:45:35 -0400865 setType(TType(basicType, higherPrecision, EvqTemporary,
866 static_cast<unsigned char>(primarySize), 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400867 }
868 } else {
869 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
870 return false;
871 }
Alexis Hetu00106d42015-04-23 11:45:35 -0400872
873 if(!ValidateMultiplication(op, left->getType(), right->getType()))
874 {
875 return false;
876 }
John Bauman66b8ab22014-05-06 15:57:45 -0400877 break;
878 case EOpMulAssign:
879 if (!left->isMatrix() && right->isMatrix()) {
880 if (left->isVector())
881 op = EOpVectorTimesMatrixAssign;
882 else {
883 return false;
884 }
885 } else if (left->isMatrix() && !right->isMatrix()) {
886 if (right->isVector()) {
887 return false;
888 } else {
889 op = EOpMatrixTimesScalarAssign;
890 }
891 } else if (left->isMatrix() && right->isMatrix()) {
892 op = EOpMatrixTimesMatrixAssign;
Alexis Hetu00106d42015-04-23 11:45:35 -0400893 setType(TType(basicType, higherPrecision, EvqTemporary,
894 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(left->getSecondarySize())));
John Bauman66b8ab22014-05-06 15:57:45 -0400895 } else if (!left->isMatrix() && !right->isMatrix()) {
896 if (left->isVector() && right->isVector()) {
897 // leave as component product
898 } else if (left->isVector() || right->isVector()) {
899 if (! left->isVector())
900 return false;
901 op = EOpVectorTimesScalarAssign;
Alexis Hetu00106d42015-04-23 11:45:35 -0400902 setType(TType(basicType, higherPrecision, EvqTemporary,
903 static_cast<unsigned char>(left->getNominalSize()), 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400904 }
905 } else {
906 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
907 return false;
908 }
Alexis Hetu00106d42015-04-23 11:45:35 -0400909
910 if(!ValidateMultiplication(op, left->getType(), right->getType()))
911 {
912 return false;
913 }
John Bauman66b8ab22014-05-06 15:57:45 -0400914 break;
915
916 case EOpAssign:
917 case EOpInitialize:
Alexis Hetu00106d42015-04-23 11:45:35 -0400918 // No more additional checks are needed.
919 if ((left->getNominalSize() != right->getNominalSize()) ||
920 (left->getSecondarySize() != right->getSecondarySize()))
921 return false;
922 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400923 case EOpAdd:
924 case EOpSub:
925 case EOpDiv:
Alexis Hetud061e422015-05-13 16:37:50 -0400926 case EOpIMod:
927 case EOpBitShiftLeft:
928 case EOpBitShiftRight:
929 case EOpBitwiseAnd:
930 case EOpBitwiseXor:
931 case EOpBitwiseOr:
John Bauman66b8ab22014-05-06 15:57:45 -0400932 case EOpAddAssign:
933 case EOpSubAssign:
934 case EOpDivAssign:
Alexis Hetu17809052015-05-13 11:28:22 -0400935 case EOpIModAssign:
936 case EOpBitShiftLeftAssign:
937 case EOpBitShiftRightAssign:
938 case EOpBitwiseAndAssign:
939 case EOpBitwiseXorAssign:
940 case EOpBitwiseOrAssign:
John Bauman66b8ab22014-05-06 15:57:45 -0400941 if ((left->isMatrix() && right->isVector()) ||
942 (left->isVector() && right->isMatrix()))
943 return false;
Alexis Hetud061e422015-05-13 16:37:50 -0400944
945 // Are the sizes compatible?
946 if(left->getNominalSize() != right->getNominalSize() ||
947 left->getSecondarySize() != right->getSecondarySize())
948 {
949 // If the nominal sizes of operands do not match:
950 // One of them must be a scalar.
951 if(!left->isScalar() && !right->isScalar())
952 return false;
953
954 // In the case of compound assignment other than multiply-assign,
955 // the right side needs to be a scalar. Otherwise a vector/matrix
956 // would be assigned to a scalar. A scalar can't be shifted by a
957 // vector either.
958 if(!right->isScalar() && (modifiesState() || op == EOpBitShiftLeft || op == EOpBitShiftRight))
959 return false;
960 }
961
962 {
Alexis Hetu00106d42015-04-23 11:45:35 -0400963 const int secondarySize = std::max(
964 left->getSecondarySize(), right->getSecondarySize());
Alexis Hetud061e422015-05-13 16:37:50 -0400965 setType(TType(basicType, higherPrecision, EvqTemporary,
966 static_cast<unsigned char>(primarySize), static_cast<unsigned char>(secondarySize)));
967 if(left->isArray())
968 {
969 ASSERT(left->getArraySize() == right->getArraySize());
970 type.setArraySize(left->getArraySize());
971 }
972 }
John Bauman66b8ab22014-05-06 15:57:45 -0400973 break;
974
975 case EOpEqual:
976 case EOpNotEqual:
977 case EOpLessThan:
978 case EOpGreaterThan:
979 case EOpLessThanEqual:
980 case EOpGreaterThanEqual:
Alexis Hetu00106d42015-04-23 11:45:35 -0400981 if ((left->getNominalSize() != right->getNominalSize()) ||
982 (left->getSecondarySize() != right->getSecondarySize()))
John Bauman66b8ab22014-05-06 15:57:45 -0400983 return false;
984 setType(TType(EbtBool, EbpUndefined));
985 break;
986
987 default:
988 return false;
989 }
990
991 return true;
992}
993
994bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
995{
996 const TTypeList* fields = leftNodeType.getStruct();
997
998 size_t structSize = fields->size();
999 int index = 0;
1000
1001 for (size_t j = 0; j < structSize; j++) {
1002 int size = (*fields)[j].type->getObjectSize();
1003 for (int i = 0; i < size; i++) {
1004 if ((*fields)[j].type->getBasicType() == EbtStruct) {
1005 if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index]))
1006 return false;
1007 } else {
1008 if (leftUnionArray[index] != rightUnionArray[index])
1009 return false;
1010 index++;
1011 }
1012
1013 }
1014 }
1015 return true;
1016}
1017
1018bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1019{
1020 if (leftNodeType.isArray()) {
1021 TType typeWithoutArrayness = leftNodeType;
1022 typeWithoutArrayness.clearArrayness();
1023
1024 int arraySize = leftNodeType.getArraySize();
1025
1026 for (int i = 0; i < arraySize; ++i) {
1027 int offset = typeWithoutArrayness.getObjectSize() * i;
1028 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
1029 return false;
1030 }
1031 } else
1032 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
1033
1034 return true;
1035}
1036
1037//
1038// The fold functions see if an operation on a constant can be done in place,
1039// without generating run-time code.
1040//
1041// Returns the node to keep using, which may or may not be the node passed in.
1042//
1043
1044TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
1045{
1046 ConstantUnion *unionArray = getUnionArrayPointer();
1047 int objectSize = getType().getObjectSize();
1048
1049 if (constantNode) { // binary operations
1050 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
1051 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
1052 TType returnType = getType();
1053
1054 // for a case like float f = 1.2 + vec4(2,3,4,5);
1055 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
1056 rightUnionArray = new ConstantUnion[objectSize];
1057 for (int i = 0; i < objectSize; ++i)
1058 rightUnionArray[i] = *node->getUnionArrayPointer();
1059 returnType = getType();
1060 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
1061 // for a case like float f = vec4(2,3,4,5) + 1.2;
1062 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
1063 for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
1064 unionArray[i] = *getUnionArrayPointer();
1065 returnType = node->getType();
1066 objectSize = constantNode->getType().getObjectSize();
1067 }
1068
1069 ConstantUnion* tempConstArray = 0;
1070 TIntermConstantUnion *tempNode;
1071
1072 bool boolNodeFlag = false;
1073 switch(op) {
1074 case EOpAdd:
1075 tempConstArray = new ConstantUnion[objectSize];
1076 {// support MSVC++6.0
1077 for (int i = 0; i < objectSize; i++)
1078 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
1079 }
1080 break;
1081 case EOpSub:
1082 tempConstArray = new ConstantUnion[objectSize];
1083 {// support MSVC++6.0
1084 for (int i = 0; i < objectSize; i++)
1085 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
1086 }
1087 break;
1088
1089 case EOpMul:
1090 case EOpVectorTimesScalar:
1091 case EOpMatrixTimesScalar:
1092 tempConstArray = new ConstantUnion[objectSize];
1093 {// support MSVC++6.0
1094 for (int i = 0; i < objectSize; i++)
1095 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
1096 }
1097 break;
1098 case EOpMatrixTimesMatrix:
1099 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
1100 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1101 return 0;
1102 }
1103 {// support MSVC++6.0
Alexis Hetu00106d42015-04-23 11:45:35 -04001104 int leftNumCols = getNominalSize();
1105 int leftNumRows = getSecondarySize();
1106 int rightNumCols = node->getNominalSize();
1107 int rightNumRows = node->getSecondarySize();
1108 if(leftNumCols != rightNumRows) {
1109 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1110 return 0;
1111 }
1112 int tempNumCols = rightNumCols;
1113 int tempNumRows = leftNumRows;
1114 int tempNumAdds = leftNumCols;
1115 tempConstArray = new ConstantUnion[tempNumCols*tempNumRows];
1116 for (int row = 0; row < tempNumRows; row++) {
1117 for (int column = 0; column < tempNumCols; column++) {
1118 tempConstArray[tempNumRows * column + row].setFConst(0.0f);
1119 for (int i = 0; i < tempNumAdds; i++) {
1120 tempConstArray[tempNumRows * column + row].setFConst(tempConstArray[tempNumRows * column + row].getFConst() + unionArray[i * leftNumRows + row].getFConst() * (rightUnionArray[column * rightNumRows + i].getFConst()));
John Bauman66b8ab22014-05-06 15:57:45 -04001121 }
1122 }
1123 }
Alexis Hetu00106d42015-04-23 11:45:35 -04001124 // update return type for matrix product
1125 returnType.setNominalSize(static_cast<unsigned char>(tempNumCols));
1126 returnType.setSecondarySize(static_cast<unsigned char>(tempNumRows));
John Bauman66b8ab22014-05-06 15:57:45 -04001127 }
1128 break;
1129 case EOpDiv:
Alexis Hetud061e422015-05-13 16:37:50 -04001130 case EOpIMod:
John Bauman66b8ab22014-05-06 15:57:45 -04001131 tempConstArray = new ConstantUnion[objectSize];
1132 {// support MSVC++6.0
1133 for (int i = 0; i < objectSize; i++) {
1134 switch (getType().getBasicType()) {
Alexis Hetud061e422015-05-13 16:37:50 -04001135 case EbtFloat:
1136 if (rightUnionArray[i] == 0.0f) {
1137 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1138 tempConstArray[i].setFConst(FLT_MAX);
1139 } else {
1140 ASSERT(op == EOpDiv);
1141 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
1142 }
1143 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001144
Alexis Hetud061e422015-05-13 16:37:50 -04001145 case EbtInt:
1146 if (rightUnionArray[i] == 0) {
1147 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1148 tempConstArray[i].setIConst(INT_MAX);
1149 } else {
1150 if(op == EOpDiv) {
1151 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1152 } else {
1153 ASSERT(op == EOpIMod);
1154 tempConstArray[i].setIConst(unionArray[i].getIConst() % rightUnionArray[i].getIConst());
1155 }
1156 }
1157 break;
1158 case EbtUInt:
1159 if (rightUnionArray[i] == 0) {
1160 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1161 tempConstArray[i].setUConst(UINT_MAX);
1162 } else {
1163 if(op == EOpDiv) {
1164 tempConstArray[i].setUConst(unionArray[i].getUConst() / rightUnionArray[i].getUConst());
1165 } else {
1166 ASSERT(op == EOpIMod);
1167 tempConstArray[i].setUConst(unionArray[i].getUConst() % rightUnionArray[i].getUConst());
1168 }
1169 }
1170 break;
1171 default:
1172 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
1173 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -04001174 }
1175 }
1176 }
1177 break;
1178
1179 case EOpMatrixTimesVector:
1180 if (node->getBasicType() != EbtFloat) {
1181 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1182 return 0;
1183 }
1184 tempConstArray = new ConstantUnion[getNominalSize()];
1185
1186 {// support MSVC++6.0
1187 for (int size = getNominalSize(), i = 0; i < size; i++) {
1188 tempConstArray[i].setFConst(0.0f);
1189 for (int j = 0; j < size; j++) {
1190 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1191 }
1192 }
1193 }
1194
1195 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1196 tempNode->setLine(getLine());
1197
1198 return tempNode;
1199
1200 case EOpVectorTimesMatrix:
1201 if (getType().getBasicType() != EbtFloat) {
1202 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1203 return 0;
1204 }
1205
1206 tempConstArray = new ConstantUnion[getNominalSize()];
1207 {// support MSVC++6.0
1208 for (int size = getNominalSize(), i = 0; i < size; i++) {
1209 tempConstArray[i].setFConst(0.0f);
1210 for (int j = 0; j < size; j++) {
1211 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1212 }
1213 }
1214 }
1215 break;
1216
1217 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1218 tempConstArray = new ConstantUnion[objectSize];
1219 {// support MSVC++6.0
1220 for (int i = 0; i < objectSize; i++)
1221 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1222 }
1223 break;
1224
1225 case EOpLogicalOr: // 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 EOpLogicalXor:
1234 tempConstArray = new ConstantUnion[objectSize];
1235 {// support MSVC++6.0
1236 for (int i = 0; i < objectSize; i++)
1237 switch (getType().getBasicType()) {
Alexis Hetud061e422015-05-13 16:37:50 -04001238 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1239 default: assert(false && "Default missing");
John Bauman66b8ab22014-05-06 15:57:45 -04001240 }
1241 }
1242 break;
1243
Alexis Hetud061e422015-05-13 16:37:50 -04001244 case EOpBitwiseAnd:
1245 tempConstArray = new ConstantUnion[objectSize];
1246 for(int i = 0; i < objectSize; i++)
1247 tempConstArray[i] = unionArray[i] & rightUnionArray[i];
1248 break;
1249 case EOpBitwiseXor:
1250 tempConstArray = new ConstantUnion[objectSize];
1251 for(int i = 0; i < objectSize; i++)
1252 tempConstArray[i] = unionArray[i] ^ rightUnionArray[i];
1253 break;
1254 case EOpBitwiseOr:
1255 tempConstArray = new ConstantUnion[objectSize];
1256 for(int i = 0; i < objectSize; i++)
1257 tempConstArray[i] = unionArray[i] | rightUnionArray[i];
1258 break;
1259 case EOpBitShiftLeft:
1260 tempConstArray = new ConstantUnion[objectSize];
1261 for(int i = 0; i < objectSize; i++)
1262 tempConstArray[i] = unionArray[i] << rightUnionArray[i];
1263 break;
1264 case EOpBitShiftRight:
1265 tempConstArray = new ConstantUnion[objectSize];
1266 for(int i = 0; i < objectSize; i++)
1267 tempConstArray[i] = unionArray[i] >> rightUnionArray[i];
1268 break;
1269
John Bauman66b8ab22014-05-06 15:57:45 -04001270 case EOpLessThan:
1271 assert(objectSize == 1);
1272 tempConstArray = new ConstantUnion[1];
1273 tempConstArray->setBConst(*unionArray < *rightUnionArray);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001274 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001275 break;
1276 case EOpGreaterThan:
1277 assert(objectSize == 1);
1278 tempConstArray = new ConstantUnion[1];
1279 tempConstArray->setBConst(*unionArray > *rightUnionArray);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001280 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001281 break;
1282 case EOpLessThanEqual:
1283 {
1284 assert(objectSize == 1);
1285 ConstantUnion constant;
1286 constant.setBConst(*unionArray > *rightUnionArray);
1287 tempConstArray = new ConstantUnion[1];
1288 tempConstArray->setBConst(!constant.getBConst());
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001289 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001290 break;
1291 }
1292 case EOpGreaterThanEqual:
1293 {
1294 assert(objectSize == 1);
1295 ConstantUnion constant;
1296 constant.setBConst(*unionArray < *rightUnionArray);
1297 tempConstArray = new ConstantUnion[1];
1298 tempConstArray->setBConst(!constant.getBConst());
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001299 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001300 break;
1301 }
1302
1303 case EOpEqual:
1304 if (getType().getBasicType() == EbtStruct) {
1305 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1306 boolNodeFlag = true;
1307 } else {
1308 for (int i = 0; i < objectSize; i++) {
1309 if (unionArray[i] != rightUnionArray[i]) {
1310 boolNodeFlag = true;
1311 break; // break out of for loop
1312 }
1313 }
1314 }
1315
1316 tempConstArray = new ConstantUnion[1];
1317 if (!boolNodeFlag) {
1318 tempConstArray->setBConst(true);
1319 }
1320 else {
1321 tempConstArray->setBConst(false);
1322 }
1323
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001324 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
John Bauman66b8ab22014-05-06 15:57:45 -04001325 tempNode->setLine(getLine());
1326
1327 return tempNode;
1328
1329 case EOpNotEqual:
1330 if (getType().getBasicType() == EbtStruct) {
1331 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1332 boolNodeFlag = true;
1333 } else {
1334 for (int i = 0; i < objectSize; i++) {
1335 if (unionArray[i] == rightUnionArray[i]) {
1336 boolNodeFlag = true;
1337 break; // break out of for loop
1338 }
1339 }
1340 }
1341
1342 tempConstArray = new ConstantUnion[1];
1343 if (!boolNodeFlag) {
1344 tempConstArray->setBConst(true);
1345 }
1346 else {
1347 tempConstArray->setBConst(false);
1348 }
1349
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001350 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
John Bauman66b8ab22014-05-06 15:57:45 -04001351 tempNode->setLine(getLine());
1352
1353 return tempNode;
1354
1355 default:
1356 infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine());
1357 return 0;
1358 }
1359 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1360 tempNode->setLine(getLine());
1361
1362 return tempNode;
1363 } else {
1364 //
1365 // Do unary operations
1366 //
1367 TIntermConstantUnion *newNode = 0;
1368 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1369 for (int i = 0; i < objectSize; i++) {
1370 switch(op) {
1371 case EOpNegative:
1372 switch (getType().getBasicType()) {
1373 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1374 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1375 default:
1376 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1377 return 0;
1378 }
1379 break;
1380 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1381 switch (getType().getBasicType()) {
1382 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1383 default:
1384 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1385 return 0;
1386 }
1387 break;
Alexis Hetud061e422015-05-13 16:37:50 -04001388 case EOpBitwiseNot:
1389 switch(getType().getBasicType()) {
1390 case EbtInt: tempConstArray[i].setIConst(~unionArray[i].getIConst()); break;
1391 case EbtUInt: tempConstArray[i].setUConst(~unionArray[i].getUConst()); break;
1392 default:
1393 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1394 return 0;
1395 }
1396 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001397 default:
1398 return 0;
1399 }
1400 }
1401 newNode = new TIntermConstantUnion(tempConstArray, getType());
1402 newNode->setLine(getLine());
1403 return newNode;
1404 }
1405}
1406
1407TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1408{
John Bauman66b8ab22014-05-06 15:57:45 -04001409 int size = node->getType().getObjectSize();
1410
1411 ConstantUnion *leftUnionArray = new ConstantUnion[size];
1412
1413 for (int i=0; i < size; i++) {
1414
1415 switch (promoteTo) {
1416 case EbtFloat:
1417 switch (node->getType().getBasicType()) {
1418 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001419 leftUnionArray[i].setFConst(static_cast<float>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001420 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001421 case EbtUInt:
1422 leftUnionArray[i].setFConst(static_cast<float>(node->getUConst(i)));
1423 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001424 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001425 leftUnionArray[i].setFConst(static_cast<float>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001426 break;
1427 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001428 leftUnionArray[i].setFConst(static_cast<float>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001429 break;
1430 default:
1431 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1432 return 0;
1433 }
1434 break;
1435 case EbtInt:
1436 switch (node->getType().getBasicType()) {
1437 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001438 leftUnionArray[i].setIConst(static_cast<int>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001439 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001440 case EbtUInt:
1441 leftUnionArray[i].setIConst(static_cast<int>(node->getUConst(i)));
1442 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001443 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001444 leftUnionArray[i].setIConst(static_cast<int>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001445 break;
1446 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001447 leftUnionArray[i].setIConst(static_cast<int>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001448 break;
1449 default:
1450 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1451 return 0;
1452 }
1453 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001454 case EbtUInt:
1455 switch (node->getType().getBasicType()) {
1456 case EbtInt:
1457 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getIConst(i)));
1458 break;
1459 case EbtUInt:
1460 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getUConst(i)));
1461 break;
1462 case EbtBool:
1463 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getBConst(i)));
1464 break;
1465 case EbtFloat:
1466 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getFConst(i)));
1467 break;
1468 default:
1469 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1470 return 0;
1471 }
1472 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001473 case EbtBool:
1474 switch (node->getType().getBasicType()) {
1475 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001476 leftUnionArray[i].setBConst(node->getIConst(i) != 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001477 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001478 case EbtUInt:
1479 leftUnionArray[i].setBConst(node->getUConst(i) != 0);
1480 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001481 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001482 leftUnionArray[i].setBConst(node->getBConst(i));
John Bauman66b8ab22014-05-06 15:57:45 -04001483 break;
1484 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001485 leftUnionArray[i].setBConst(node->getFConst(i) != 0.0f);
John Bauman66b8ab22014-05-06 15:57:45 -04001486 break;
1487 default:
1488 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1489 return 0;
1490 }
1491
1492 break;
1493 default:
1494 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
1495 return 0;
1496 }
1497
1498 }
1499
1500 const TType& t = node->getType();
1501
Alexis Hetub14178b2015-04-13 13:23:20 -04001502 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.getSecondarySize(), t.isArray()), node->getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001503}
1504