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