blob: 277c4a3670ab93cd55c55d22832137eb1b0e9655 [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 Capense4b1b1d2015-02-17 17:26:01 -0500604 case EOpConstructUVec2:
605 case EOpConstructUVec3:
606 case EOpConstructUVec4:
Nicolas Capens3c20f802015-02-17 17:17:20 -0500607 case EOpConstructUInt:
John Bauman66b8ab22014-05-06 15:57:45 -0400608 case EOpConstructBVec2:
609 case EOpConstructBVec3:
610 case EOpConstructBVec4:
611 case EOpConstructBool:
612 case EOpConstructStruct:
613 return true;
614 default:
615 return false;
616 }
617}
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400618
John Bauman66b8ab22014-05-06 15:57:45 -0400619//
620// Make sure the type of a unary operator is appropriate for its
621// combination of operation and operand type.
622//
623// Returns false in nothing makes sense.
624//
625bool TIntermUnary::promote(TInfoSink&)
626{
627 switch (op) {
628 case EOpLogicalNot:
629 if (operand->getBasicType() != EbtBool)
630 return false;
631 break;
632 case EOpNegative:
633 case EOpPostIncrement:
634 case EOpPostDecrement:
635 case EOpPreIncrement:
636 case EOpPreDecrement:
637 if (operand->getBasicType() == EbtBool)
638 return false;
639 break;
640
641 // operators for built-ins are already type checked against their prototype
642 case EOpAny:
643 case EOpAll:
644 case EOpVectorLogicalNot:
645 return true;
646
647 default:
648 if (operand->getBasicType() != EbtFloat)
649 return false;
650 }
651
652 setType(operand->getType());
653
654 // Unary operations results in temporary variables unless const.
655 if (operand->getQualifier() != EvqConst) {
656 getTypePointer()->setQualifier(EvqTemporary);
657 }
658
659 return true;
660}
661
662//
663// Establishes the type of the resultant operation, as well as
664// makes the operator the correct one for the operands.
665//
666// Returns false if operator can't work on operands.
667//
668bool TIntermBinary::promote(TInfoSink& infoSink)
669{
670 // This function only handles scalars, vectors, and matrices.
671 if (left->isArray() || right->isArray()) {
672 infoSink.info.message(EPrefixInternalError, "Invalid operation for arrays", getLine());
673 return false;
674 }
675
676 // GLSL ES 2.0 does not support implicit type casting.
677 // So the basic type should always match.
678 if (left->getBasicType() != right->getBasicType())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400679 {
John Bauman66b8ab22014-05-06 15:57:45 -0400680 return false;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400681 }
John Bauman66b8ab22014-05-06 15:57:45 -0400682
683 //
684 // Base assumption: just make the type the same as the left
685 // operand. Then only deviations from this need be coded.
686 //
687 setType(left->getType());
688
689 // The result gets promoted to the highest precision.
690 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
691 getTypePointer()->setPrecision(higherPrecision);
692
693 // Binary operations results in temporary variables unless both
694 // operands are const.
695 if (left->getQualifier() != EvqConst || right->getQualifier() != EvqConst) {
696 getTypePointer()->setQualifier(EvqTemporary);
697 }
698
699 int size = std::max(left->getNominalSize(), right->getNominalSize());
700
701 //
702 // All scalars. Code after this test assumes this case is removed!
703 //
704 if (size == 1) {
705 switch (op) {
706 //
707 // Promote to conditional
708 //
709 case EOpEqual:
710 case EOpNotEqual:
711 case EOpLessThan:
712 case EOpGreaterThan:
713 case EOpLessThanEqual:
714 case EOpGreaterThanEqual:
715 setType(TType(EbtBool, EbpUndefined));
716 break;
717
718 //
719 // And and Or operate on conditionals
720 //
721 case EOpLogicalAnd:
722 case EOpLogicalOr:
723 // Both operands must be of type bool.
724 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
725 return false;
726 setType(TType(EbtBool, EbpUndefined));
727 break;
728
729 default:
730 break;
731 }
732 return true;
733 }
734
735 // If we reach here, at least one of the operands is vector or matrix.
736 // The other operand could be a scalar, vector, or matrix.
737 // Are the sizes compatible?
738 //
739 if (left->getNominalSize() != right->getNominalSize()) {
740 // If the nominal size of operands do not match:
741 // One of them must be scalar.
742 if (left->getNominalSize() != 1 && right->getNominalSize() != 1)
743 return false;
744 // Operator cannot be of type pure assignment.
745 if (op == EOpAssign || op == EOpInitialize)
746 return false;
747 }
748
749 //
750 // Can these two operands be combined?
751 //
752 TBasicType basicType = left->getBasicType();
753 switch (op) {
754 case EOpMul:
755 if (!left->isMatrix() && right->isMatrix()) {
756 if (left->isVector())
757 op = EOpVectorTimesMatrix;
758 else {
759 op = EOpMatrixTimesScalar;
760 setType(TType(basicType, higherPrecision, EvqTemporary, size, true));
761 }
762 } else if (left->isMatrix() && !right->isMatrix()) {
763 if (right->isVector()) {
764 op = EOpMatrixTimesVector;
765 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
766 } else {
767 op = EOpMatrixTimesScalar;
768 }
769 } else if (left->isMatrix() && right->isMatrix()) {
770 op = EOpMatrixTimesMatrix;
771 } else if (!left->isMatrix() && !right->isMatrix()) {
772 if (left->isVector() && right->isVector()) {
773 // leave as component product
774 } else if (left->isVector() || right->isVector()) {
775 op = EOpVectorTimesScalar;
776 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
777 }
778 } else {
779 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
780 return false;
781 }
782 break;
783 case EOpMulAssign:
784 if (!left->isMatrix() && right->isMatrix()) {
785 if (left->isVector())
786 op = EOpVectorTimesMatrixAssign;
787 else {
788 return false;
789 }
790 } else if (left->isMatrix() && !right->isMatrix()) {
791 if (right->isVector()) {
792 return false;
793 } else {
794 op = EOpMatrixTimesScalarAssign;
795 }
796 } else if (left->isMatrix() && right->isMatrix()) {
797 op = EOpMatrixTimesMatrixAssign;
798 } else if (!left->isMatrix() && !right->isMatrix()) {
799 if (left->isVector() && right->isVector()) {
800 // leave as component product
801 } else if (left->isVector() || right->isVector()) {
802 if (! left->isVector())
803 return false;
804 op = EOpVectorTimesScalarAssign;
805 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
806 }
807 } else {
808 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
809 return false;
810 }
811 break;
812
813 case EOpAssign:
814 case EOpInitialize:
815 case EOpAdd:
816 case EOpSub:
817 case EOpDiv:
818 case EOpAddAssign:
819 case EOpSubAssign:
820 case EOpDivAssign:
821 if ((left->isMatrix() && right->isVector()) ||
822 (left->isVector() && right->isMatrix()))
823 return false;
824 setType(TType(basicType, higherPrecision, EvqTemporary, size, left->isMatrix() || right->isMatrix()));
825 break;
826
827 case EOpEqual:
828 case EOpNotEqual:
829 case EOpLessThan:
830 case EOpGreaterThan:
831 case EOpLessThanEqual:
832 case EOpGreaterThanEqual:
833 if ((left->isMatrix() && right->isVector()) ||
834 (left->isVector() && right->isMatrix()))
835 return false;
836 setType(TType(EbtBool, EbpUndefined));
837 break;
838
839 default:
840 return false;
841 }
842
843 return true;
844}
845
846bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
847{
848 const TTypeList* fields = leftNodeType.getStruct();
849
850 size_t structSize = fields->size();
851 int index = 0;
852
853 for (size_t j = 0; j < structSize; j++) {
854 int size = (*fields)[j].type->getObjectSize();
855 for (int i = 0; i < size; i++) {
856 if ((*fields)[j].type->getBasicType() == EbtStruct) {
857 if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index]))
858 return false;
859 } else {
860 if (leftUnionArray[index] != rightUnionArray[index])
861 return false;
862 index++;
863 }
864
865 }
866 }
867 return true;
868}
869
870bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
871{
872 if (leftNodeType.isArray()) {
873 TType typeWithoutArrayness = leftNodeType;
874 typeWithoutArrayness.clearArrayness();
875
876 int arraySize = leftNodeType.getArraySize();
877
878 for (int i = 0; i < arraySize; ++i) {
879 int offset = typeWithoutArrayness.getObjectSize() * i;
880 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
881 return false;
882 }
883 } else
884 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
885
886 return true;
887}
888
889//
890// The fold functions see if an operation on a constant can be done in place,
891// without generating run-time code.
892//
893// Returns the node to keep using, which may or may not be the node passed in.
894//
895
896TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
897{
898 ConstantUnion *unionArray = getUnionArrayPointer();
899 int objectSize = getType().getObjectSize();
900
901 if (constantNode) { // binary operations
902 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
903 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
904 TType returnType = getType();
905
906 // for a case like float f = 1.2 + vec4(2,3,4,5);
907 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
908 rightUnionArray = new ConstantUnion[objectSize];
909 for (int i = 0; i < objectSize; ++i)
910 rightUnionArray[i] = *node->getUnionArrayPointer();
911 returnType = getType();
912 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
913 // for a case like float f = vec4(2,3,4,5) + 1.2;
914 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
915 for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
916 unionArray[i] = *getUnionArrayPointer();
917 returnType = node->getType();
918 objectSize = constantNode->getType().getObjectSize();
919 }
920
921 ConstantUnion* tempConstArray = 0;
922 TIntermConstantUnion *tempNode;
923
924 bool boolNodeFlag = false;
925 switch(op) {
926 case EOpAdd:
927 tempConstArray = new ConstantUnion[objectSize];
928 {// support MSVC++6.0
929 for (int i = 0; i < objectSize; i++)
930 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
931 }
932 break;
933 case EOpSub:
934 tempConstArray = new ConstantUnion[objectSize];
935 {// support MSVC++6.0
936 for (int i = 0; i < objectSize; i++)
937 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
938 }
939 break;
940
941 case EOpMul:
942 case EOpVectorTimesScalar:
943 case EOpMatrixTimesScalar:
944 tempConstArray = new ConstantUnion[objectSize];
945 {// support MSVC++6.0
946 for (int i = 0; i < objectSize; i++)
947 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
948 }
949 break;
950 case EOpMatrixTimesMatrix:
951 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
952 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
953 return 0;
954 }
955 {// support MSVC++6.0
956 int size = getNominalSize();
957 tempConstArray = new ConstantUnion[size*size];
958 for (int row = 0; row < size; row++) {
959 for (int column = 0; column < size; column++) {
960 tempConstArray[size * column + row].setFConst(0.0f);
961 for (int i = 0; i < size; i++) {
962 tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst()));
963 }
964 }
965 }
966 }
967 break;
968 case EOpDiv:
969 tempConstArray = new ConstantUnion[objectSize];
970 {// support MSVC++6.0
971 for (int i = 0; i < objectSize; i++) {
972 switch (getType().getBasicType()) {
973 case EbtFloat:
974 if (rightUnionArray[i] == 0.0f) {
975 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
976 tempConstArray[i].setFConst(FLT_MAX);
977 } else
978 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
979 break;
980
981 case EbtInt:
982 if (rightUnionArray[i] == 0) {
983 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
984 tempConstArray[i].setIConst(INT_MAX);
985 } else
986 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
987 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -0500988 case EbtUInt:
989 if (rightUnionArray[i] == 0) {
990 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
991 tempConstArray[i].setUConst(UINT_MAX);
992 } else
993 tempConstArray[i].setUConst(unionArray[i].getUConst() / rightUnionArray[i].getUConst());
994 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400995 default:
996 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
997 return 0;
998 }
999 }
1000 }
1001 break;
1002
1003 case EOpMatrixTimesVector:
1004 if (node->getBasicType() != EbtFloat) {
1005 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1006 return 0;
1007 }
1008 tempConstArray = new ConstantUnion[getNominalSize()];
1009
1010 {// support MSVC++6.0
1011 for (int size = getNominalSize(), i = 0; i < size; i++) {
1012 tempConstArray[i].setFConst(0.0f);
1013 for (int j = 0; j < size; j++) {
1014 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1015 }
1016 }
1017 }
1018
1019 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1020 tempNode->setLine(getLine());
1021
1022 return tempNode;
1023
1024 case EOpVectorTimesMatrix:
1025 if (getType().getBasicType() != EbtFloat) {
1026 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1027 return 0;
1028 }
1029
1030 tempConstArray = new ConstantUnion[getNominalSize()];
1031 {// support MSVC++6.0
1032 for (int size = getNominalSize(), i = 0; i < size; i++) {
1033 tempConstArray[i].setFConst(0.0f);
1034 for (int j = 0; j < size; j++) {
1035 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1036 }
1037 }
1038 }
1039 break;
1040
1041 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1042 tempConstArray = new ConstantUnion[objectSize];
1043 {// support MSVC++6.0
1044 for (int i = 0; i < objectSize; i++)
1045 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1046 }
1047 break;
1048
1049 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1050 tempConstArray = new ConstantUnion[objectSize];
1051 {// support MSVC++6.0
1052 for (int i = 0; i < objectSize; i++)
1053 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1054 }
1055 break;
1056
1057 case EOpLogicalXor:
1058 tempConstArray = new ConstantUnion[objectSize];
1059 {// support MSVC++6.0
1060 for (int i = 0; i < objectSize; i++)
1061 switch (getType().getBasicType()) {
1062 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1063 default: assert(false && "Default missing");
1064 }
1065 }
1066 break;
1067
1068 case EOpLessThan:
1069 assert(objectSize == 1);
1070 tempConstArray = new ConstantUnion[1];
1071 tempConstArray->setBConst(*unionArray < *rightUnionArray);
1072 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1073 break;
1074 case EOpGreaterThan:
1075 assert(objectSize == 1);
1076 tempConstArray = new ConstantUnion[1];
1077 tempConstArray->setBConst(*unionArray > *rightUnionArray);
1078 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1079 break;
1080 case EOpLessThanEqual:
1081 {
1082 assert(objectSize == 1);
1083 ConstantUnion constant;
1084 constant.setBConst(*unionArray > *rightUnionArray);
1085 tempConstArray = new ConstantUnion[1];
1086 tempConstArray->setBConst(!constant.getBConst());
1087 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1088 break;
1089 }
1090 case EOpGreaterThanEqual:
1091 {
1092 assert(objectSize == 1);
1093 ConstantUnion constant;
1094 constant.setBConst(*unionArray < *rightUnionArray);
1095 tempConstArray = new ConstantUnion[1];
1096 tempConstArray->setBConst(!constant.getBConst());
1097 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1098 break;
1099 }
1100
1101 case EOpEqual:
1102 if (getType().getBasicType() == EbtStruct) {
1103 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1104 boolNodeFlag = true;
1105 } else {
1106 for (int i = 0; i < objectSize; i++) {
1107 if (unionArray[i] != rightUnionArray[i]) {
1108 boolNodeFlag = true;
1109 break; // break out of for loop
1110 }
1111 }
1112 }
1113
1114 tempConstArray = new ConstantUnion[1];
1115 if (!boolNodeFlag) {
1116 tempConstArray->setBConst(true);
1117 }
1118 else {
1119 tempConstArray->setBConst(false);
1120 }
1121
1122 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1123 tempNode->setLine(getLine());
1124
1125 return tempNode;
1126
1127 case EOpNotEqual:
1128 if (getType().getBasicType() == EbtStruct) {
1129 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1130 boolNodeFlag = true;
1131 } else {
1132 for (int i = 0; i < objectSize; i++) {
1133 if (unionArray[i] == rightUnionArray[i]) {
1134 boolNodeFlag = true;
1135 break; // break out of for loop
1136 }
1137 }
1138 }
1139
1140 tempConstArray = new ConstantUnion[1];
1141 if (!boolNodeFlag) {
1142 tempConstArray->setBConst(true);
1143 }
1144 else {
1145 tempConstArray->setBConst(false);
1146 }
1147
1148 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1149 tempNode->setLine(getLine());
1150
1151 return tempNode;
1152
1153 default:
1154 infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine());
1155 return 0;
1156 }
1157 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1158 tempNode->setLine(getLine());
1159
1160 return tempNode;
1161 } else {
1162 //
1163 // Do unary operations
1164 //
1165 TIntermConstantUnion *newNode = 0;
1166 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1167 for (int i = 0; i < objectSize; i++) {
1168 switch(op) {
1169 case EOpNegative:
1170 switch (getType().getBasicType()) {
1171 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1172 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1173 default:
1174 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1175 return 0;
1176 }
1177 break;
1178 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1179 switch (getType().getBasicType()) {
1180 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1181 default:
1182 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1183 return 0;
1184 }
1185 break;
1186 default:
1187 return 0;
1188 }
1189 }
1190 newNode = new TIntermConstantUnion(tempConstArray, getType());
1191 newNode->setLine(getLine());
1192 return newNode;
1193 }
1194}
1195
1196TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1197{
John Bauman66b8ab22014-05-06 15:57:45 -04001198 int size = node->getType().getObjectSize();
1199
1200 ConstantUnion *leftUnionArray = new ConstantUnion[size];
1201
1202 for (int i=0; i < size; i++) {
1203
1204 switch (promoteTo) {
1205 case EbtFloat:
1206 switch (node->getType().getBasicType()) {
1207 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001208 leftUnionArray[i].setFConst(static_cast<float>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001209 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001210 case EbtUInt:
1211 leftUnionArray[i].setFConst(static_cast<float>(node->getUConst(i)));
1212 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001213 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001214 leftUnionArray[i].setFConst(static_cast<float>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001215 break;
1216 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001217 leftUnionArray[i].setFConst(static_cast<float>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001218 break;
1219 default:
1220 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1221 return 0;
1222 }
1223 break;
1224 case EbtInt:
1225 switch (node->getType().getBasicType()) {
1226 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001227 leftUnionArray[i].setIConst(static_cast<int>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001228 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001229 case EbtUInt:
1230 leftUnionArray[i].setIConst(static_cast<int>(node->getUConst(i)));
1231 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001232 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001233 leftUnionArray[i].setIConst(static_cast<int>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001234 break;
1235 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001236 leftUnionArray[i].setIConst(static_cast<int>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001237 break;
1238 default:
1239 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1240 return 0;
1241 }
1242 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001243 case EbtUInt:
1244 switch (node->getType().getBasicType()) {
1245 case EbtInt:
1246 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getIConst(i)));
1247 break;
1248 case EbtUInt:
1249 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getUConst(i)));
1250 break;
1251 case EbtBool:
1252 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getBConst(i)));
1253 break;
1254 case EbtFloat:
1255 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getFConst(i)));
1256 break;
1257 default:
1258 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1259 return 0;
1260 }
1261 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001262 case EbtBool:
1263 switch (node->getType().getBasicType()) {
1264 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001265 leftUnionArray[i].setBConst(node->getIConst(i) != 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001266 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001267 case EbtUInt:
1268 leftUnionArray[i].setBConst(node->getUConst(i) != 0);
1269 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001270 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001271 leftUnionArray[i].setBConst(node->getBConst(i));
John Bauman66b8ab22014-05-06 15:57:45 -04001272 break;
1273 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001274 leftUnionArray[i].setBConst(node->getFConst(i) != 0.0f);
John Bauman66b8ab22014-05-06 15:57:45 -04001275 break;
1276 default:
1277 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1278 return 0;
1279 }
1280
1281 break;
1282 default:
1283 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
1284 return 0;
1285 }
1286
1287 }
1288
1289 const TType& t = node->getType();
1290
1291 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.isMatrix(), t.isArray()), node->getLine());
1292}
1293