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