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