blob: c5fb3a2e45d8424cbb3bfc075b82e324afbff343 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman66b8ab22014-05-06 15:57:45 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman66b8ab22014-05-06 15:57:45 -040014
15//
16// Build the intermediate representation.
17//
18
19#include <float.h>
20#include <limits.h>
21#include <algorithm>
22
Nicolas Capenscc863da2015-01-21 15:50:55 -050023#include "localintermediate.h"
24#include "SymbolTable.h"
Alexis Hetu2ce222c2016-05-02 11:20:52 -040025#include "Common/Math.hpp"
John Bauman66b8ab22014-05-06 15:57:45 -040026
27bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray);
28
29static TPrecision GetHigherPrecision( TPrecision left, TPrecision right ){
Nicolas Capens0bac2852016-05-07 06:09:58 -040030 return left > right ? left : right;
John Bauman66b8ab22014-05-06 15:57:45 -040031}
32
Alexis Hetu00106d42015-04-23 11:45:35 -040033static bool ValidateMultiplication(TOperator op, const TType &left, const TType &right)
34{
35 switch(op)
36 {
37 case EOpMul:
38 case EOpMulAssign:
39 return left.getNominalSize() == right.getNominalSize() &&
40 left.getSecondarySize() == right.getSecondarySize();
41 case EOpVectorTimesScalar:
42 case EOpVectorTimesScalarAssign:
43 return true;
44 case EOpVectorTimesMatrix:
45 return left.getNominalSize() == right.getSecondarySize();
46 case EOpVectorTimesMatrixAssign:
47 return left.getNominalSize() == right.getSecondarySize() &&
48 left.getNominalSize() == right.getNominalSize();
49 case EOpMatrixTimesVector:
50 return left.getNominalSize() == right.getNominalSize();
51 case EOpMatrixTimesScalar:
52 case EOpMatrixTimesScalarAssign:
53 return true;
54 case EOpMatrixTimesMatrix:
55 return left.getNominalSize() == right.getSecondarySize();
56 case EOpMatrixTimesMatrixAssign:
57 return left.getNominalSize() == right.getNominalSize() &&
58 left.getSecondarySize() == right.getSecondarySize();
59 default:
Nicolas Capens3713cd42015-06-22 10:41:54 -040060 UNREACHABLE(op);
Alexis Hetu00106d42015-04-23 11:45:35 -040061 return false;
62 }
63}
64
Alexis Hetuec93b1d2016-12-09 16:01:29 -050065TOperator TypeToConstructorOperator(const TType &type)
66{
67 switch(type.getBasicType())
68 {
69 case EbtFloat:
70 if(type.isMatrix())
71 {
72 switch(type.getNominalSize())
73 {
74 case 2:
75 switch(type.getSecondarySize())
76 {
77 case 2:
78 return EOpConstructMat2;
79 case 3:
80 return EOpConstructMat2x3;
81 case 4:
82 return EOpConstructMat2x4;
83 default:
84 break;
85 }
86 break;
87
88 case 3:
89 switch(type.getSecondarySize())
90 {
91 case 2:
92 return EOpConstructMat3x2;
93 case 3:
94 return EOpConstructMat3;
95 case 4:
96 return EOpConstructMat3x4;
97 default:
98 break;
99 }
100 break;
101
102 case 4:
103 switch(type.getSecondarySize())
104 {
105 case 2:
106 return EOpConstructMat4x2;
107 case 3:
108 return EOpConstructMat4x3;
109 case 4:
110 return EOpConstructMat4;
111 default:
112 break;
113 }
114 break;
115 }
116 }
117 else
118 {
119 switch(type.getNominalSize())
120 {
121 case 1:
122 return EOpConstructFloat;
123 case 2:
124 return EOpConstructVec2;
125 case 3:
126 return EOpConstructVec3;
127 case 4:
128 return EOpConstructVec4;
129 default:
130 break;
131 }
132 }
133 break;
134
135 case EbtInt:
136 switch(type.getNominalSize())
137 {
138 case 1:
139 return EOpConstructInt;
140 case 2:
141 return EOpConstructIVec2;
142 case 3:
143 return EOpConstructIVec3;
144 case 4:
145 return EOpConstructIVec4;
146 default:
147 break;
148 }
149 break;
150
151 case EbtUInt:
152 switch(type.getNominalSize())
153 {
154 case 1:
155 return EOpConstructUInt;
156 case 2:
157 return EOpConstructUVec2;
158 case 3:
159 return EOpConstructUVec3;
160 case 4:
161 return EOpConstructUVec4;
162 default:
163 break;
164 }
165 break;
166
167 case EbtBool:
168 switch(type.getNominalSize())
169 {
170 case 1:
171 return EOpConstructBool;
172 case 2:
173 return EOpConstructBVec2;
174 case 3:
175 return EOpConstructBVec3;
176 case 4:
177 return EOpConstructBVec4;
178 default:
179 break;
180 }
181 break;
182
183 case EbtStruct:
184 return EOpConstructStruct;
185
186 default:
187 break;
188 }
189
190 return EOpNull;
191}
192
John Bauman66b8ab22014-05-06 15:57:45 -0400193const char* getOperatorString(TOperator op) {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400194 switch (op) {
195 case EOpInitialize: return "=";
196 case EOpAssign: return "=";
197 case EOpAddAssign: return "+=";
198 case EOpSubAssign: return "-=";
199 case EOpDivAssign: return "/=";
200 case EOpIModAssign: return "%=";
201 case EOpBitShiftLeftAssign: return "<<=";
202 case EOpBitShiftRightAssign: return ">>=";
203 case EOpBitwiseAndAssign: return "&=";
204 case EOpBitwiseXorAssign: return "^=";
205 case EOpBitwiseOrAssign: return "|=";
John Bauman66b8ab22014-05-06 15:57:45 -0400206
Nicolas Capens0bac2852016-05-07 06:09:58 -0400207 // Fall-through.
208 case EOpMulAssign:
209 case EOpVectorTimesMatrixAssign:
210 case EOpVectorTimesScalarAssign:
211 case EOpMatrixTimesScalarAssign:
212 case EOpMatrixTimesMatrixAssign: return "*=";
John Bauman66b8ab22014-05-06 15:57:45 -0400213
Nicolas Capens0bac2852016-05-07 06:09:58 -0400214 // Fall-through.
215 case EOpIndexDirect:
216 case EOpIndexIndirect: return "[]";
John Bauman66b8ab22014-05-06 15:57:45 -0400217
Nicolas Capens0bac2852016-05-07 06:09:58 -0400218 case EOpIndexDirectStruct: return ".";
219 case EOpVectorSwizzle: return ".";
220 case EOpAdd: return "+";
221 case EOpSub: return "-";
222 case EOpMul: return "*";
223 case EOpDiv: return "/";
224 case EOpMod: UNIMPLEMENTED(); break;
225 case EOpEqual: return "==";
226 case EOpNotEqual: return "!=";
227 case EOpLessThan: return "<";
228 case EOpGreaterThan: return ">";
229 case EOpLessThanEqual: return "<=";
230 case EOpGreaterThanEqual: return ">=";
John Bauman66b8ab22014-05-06 15:57:45 -0400231
Nicolas Capens0bac2852016-05-07 06:09:58 -0400232 // Fall-through.
233 case EOpVectorTimesScalar:
234 case EOpVectorTimesMatrix:
235 case EOpMatrixTimesVector:
236 case EOpMatrixTimesScalar:
237 case EOpMatrixTimesMatrix: return "*";
John Bauman66b8ab22014-05-06 15:57:45 -0400238
Nicolas Capens0bac2852016-05-07 06:09:58 -0400239 case EOpLogicalOr: return "||";
240 case EOpLogicalXor: return "^^";
241 case EOpLogicalAnd: return "&&";
242 case EOpIMod: return "%";
243 case EOpBitShiftLeft: return "<<";
244 case EOpBitShiftRight: return ">>";
245 case EOpBitwiseAnd: return "&";
246 case EOpBitwiseXor: return "^";
247 case EOpBitwiseOr: return "|";
248 case EOpNegative: return "-";
249 case EOpVectorLogicalNot: return "not";
250 case EOpLogicalNot: return "!";
251 case EOpBitwiseNot: return "~";
252 case EOpPostIncrement: return "++";
253 case EOpPostDecrement: return "--";
254 case EOpPreIncrement: return "++";
255 case EOpPreDecrement: return "--";
John Bauman66b8ab22014-05-06 15:57:45 -0400256
Nicolas Capens0bac2852016-05-07 06:09:58 -0400257 case EOpRadians: return "radians";
258 case EOpDegrees: return "degrees";
259 case EOpSin: return "sin";
260 case EOpCos: return "cos";
261 case EOpTan: return "tan";
262 case EOpAsin: return "asin";
263 case EOpAcos: return "acos";
264 case EOpAtan: return "atan";
265 case EOpSinh: return "sinh";
266 case EOpCosh: return "cosh";
267 case EOpTanh: return "tanh";
268 case EOpAsinh: return "asinh";
269 case EOpAcosh: return "acosh";
270 case EOpAtanh: return "atanh";
271 case EOpExp: return "exp";
272 case EOpLog: return "log";
273 case EOpExp2: return "exp2";
274 case EOpLog2: return "log2";
275 case EOpSqrt: return "sqrt";
276 case EOpInverseSqrt: return "inversesqrt";
277 case EOpAbs: return "abs";
278 case EOpSign: return "sign";
279 case EOpFloor: return "floor";
280 case EOpTrunc: return "trunc";
281 case EOpRound: return "round";
282 case EOpRoundEven: return "roundEven";
283 case EOpCeil: return "ceil";
284 case EOpFract: return "fract";
285 case EOpLength: return "length";
286 case EOpNormalize: return "normalize";
287 case EOpDFdx: return "dFdx";
288 case EOpDFdy: return "dFdy";
289 case EOpFwidth: return "fwidth";
290 case EOpAny: return "any";
291 case EOpAll: return "all";
292 case EOpIsNan: return "isnan";
293 case EOpIsInf: return "isinf";
294 case EOpOuterProduct: return "outerProduct";
295 case EOpTranspose: return "transpose";
296 case EOpDeterminant: return "determinant";
297 case EOpInverse: return "inverse";
John Bauman66b8ab22014-05-06 15:57:45 -0400298
Nicolas Capens0bac2852016-05-07 06:09:58 -0400299 default: break;
300 }
301 return "";
John Bauman66b8ab22014-05-06 15:57:45 -0400302}
303
304////////////////////////////////////////////////////////////////////////////
305//
306// First set of functions are to help build the intermediate representation.
307// These functions are not member functions of the nodes.
308// They are called from parser productions.
309//
310/////////////////////////////////////////////////////////////////////////////
311
312//
313// Add a terminal node for an identifier in an expression.
314//
315// Returns the added node.
316//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400317TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400318{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400319 TIntermSymbol* node = new TIntermSymbol(id, name, type);
320 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400321
Nicolas Capens0bac2852016-05-07 06:09:58 -0400322 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400323}
324
325//
326// Connect two nodes with a new parent that does a binary operation on the nodes.
327//
328// Returns the added node.
329//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400330TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400331{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400332 bool isBitShift = false;
333 switch (op) {
334 case EOpEqual:
335 case EOpNotEqual:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400336 break;
337 case EOpLessThan:
338 case EOpGreaterThan:
339 case EOpLessThanEqual:
340 case EOpGreaterThanEqual:
341 if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) {
342 return 0;
343 }
344 break;
345 case EOpLogicalOr:
346 case EOpLogicalXor:
347 case EOpLogicalAnd:
348 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) {
349 return 0;
350 }
351 break;
352 case EOpBitwiseOr:
353 case EOpBitwiseXor:
354 case EOpBitwiseAnd:
355 if (!IsInteger(left->getBasicType()) || left->isMatrix() || left->isArray()) {
356 return 0;
357 }
358 break;
359 case EOpAdd:
360 case EOpSub:
361 case EOpDiv:
362 case EOpMul:
363 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool) {
364 return 0;
365 }
366 break;
367 case EOpIMod:
368 // Note that this is only for the % operator, not for mod()
369 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat) {
370 return 0;
371 }
372 break;
373 case EOpBitShiftLeft:
374 case EOpBitShiftRight:
375 case EOpBitShiftLeftAssign:
376 case EOpBitShiftRightAssign:
377 // Unsigned can be bit-shifted by signed and vice versa, but we need to
378 // check that the basic type is an integer type.
379 isBitShift = true;
380 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
381 {
382 return 0;
383 }
384 break;
385 default: break;
386 }
John Bauman66b8ab22014-05-06 15:57:45 -0400387
Nicolas Capens0bac2852016-05-07 06:09:58 -0400388 if(!isBitShift && left->getBasicType() != right->getBasicType())
389 {
390 return 0;
391 }
John Bauman66b8ab22014-05-06 15:57:45 -0400392
Nicolas Capens0bac2852016-05-07 06:09:58 -0400393 //
394 // Need a new node holding things together then. Make
395 // one and promote it to the right type.
396 //
397 TIntermBinary* node = new TIntermBinary(op);
398 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400399
Nicolas Capens0bac2852016-05-07 06:09:58 -0400400 node->setLeft(left);
401 node->setRight(right);
402 if (!node->promote(infoSink))
Nicolas Capensaa028482018-01-26 01:35:42 +0000403 {
404 delete node;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400405 return 0;
Nicolas Capensaa028482018-01-26 01:35:42 +0000406 }
John Bauman66b8ab22014-05-06 15:57:45 -0400407
Nicolas Capens0bac2852016-05-07 06:09:58 -0400408 //
409 // See if we can fold constants.
410 //
411 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
412 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
413 if (leftTempConstant && rightTempConstant) {
414 TIntermTyped *typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400415
Nicolas Capens0bac2852016-05-07 06:09:58 -0400416 if (typedReturnNode)
417 return typedReturnNode;
418 }
John Bauman66b8ab22014-05-06 15:57:45 -0400419
Nicolas Capens0bac2852016-05-07 06:09:58 -0400420 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400421}
422
423//
424// Connect two nodes through an assignment.
425//
426// Returns the added node.
427//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400428TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400429{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400430 if (left->getType().getStruct() || right->getType().getStruct())
431 {
432 if (left->getType() != right->getType())
433 {
434 return 0;
435 }
436 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400437
Nicolas Capens0bac2852016-05-07 06:09:58 -0400438 TIntermBinary* node = new TIntermBinary(op);
439 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400440
Nicolas Capens0bac2852016-05-07 06:09:58 -0400441 node->setLeft(left);
442 node->setRight(right);
443 if (! node->promote(infoSink))
444 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400445
Nicolas Capens0bac2852016-05-07 06:09:58 -0400446 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400447}
448
449//
450// Connect two nodes through an index operator, where the left node is the base
451// of an array or struct, and the right node is a direct or indirect offset.
452//
453// Returns the added node.
454// The caller should set the type of the returned node.
455//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400456TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400457{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400458 TIntermBinary* node = new TIntermBinary(op);
459 node->setLine(line);
460 node->setLeft(base);
461 node->setRight(index);
John Bauman66b8ab22014-05-06 15:57:45 -0400462
Nicolas Capens0bac2852016-05-07 06:09:58 -0400463 // caller should set the type
John Bauman66b8ab22014-05-06 15:57:45 -0400464
Nicolas Capens0bac2852016-05-07 06:09:58 -0400465 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400466}
467
468//
469// Add one node as the parent of another that it operates on.
470//
471// Returns the added node.
472//
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -0400473TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermTyped* child, const TSourceLoc &line, const TType *funcReturnType)
John Bauman66b8ab22014-05-06 15:57:45 -0400474{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400475 if (child == 0) {
476 infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line);
477 return 0;
478 }
John Bauman66b8ab22014-05-06 15:57:45 -0400479
Nicolas Capens0bac2852016-05-07 06:09:58 -0400480 switch (op) {
481 case EOpBitwiseNot:
482 if (!IsInteger(child->getType().getBasicType()) || child->getType().isMatrix() || child->getType().isArray()) {
483 return 0;
484 }
485 break;
Alexis Hetud061e422015-05-13 16:37:50 -0400486
Nicolas Capens0bac2852016-05-07 06:09:58 -0400487 case EOpLogicalNot:
488 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
489 return 0;
490 }
491 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400492
Nicolas Capens0bac2852016-05-07 06:09:58 -0400493 case EOpPostIncrement:
494 case EOpPreIncrement:
495 case EOpPostDecrement:
496 case EOpPreDecrement:
497 case EOpNegative:
Sean Risser39f02472019-06-05 16:28:47 -0400498 if (!child->getType().isScalar() && !child->getType().isVector() && !child->getType().isMatrix())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400499 return 0;
500 default: break;
501 }
John Bauman66b8ab22014-05-06 15:57:45 -0400502
Nicolas Capens0bac2852016-05-07 06:09:58 -0400503 TIntermConstantUnion *childTempConstant = 0;
504 if (child->getAsConstantUnion())
505 childTempConstant = child->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -0400506
Nicolas Capens0bac2852016-05-07 06:09:58 -0400507 //
508 // Make a new node for the operator.
509 //
510 TIntermUnary *node = new TIntermUnary(op);
511 node->setLine(line);
512 node->setOperand(child);
John Bauman66b8ab22014-05-06 15:57:45 -0400513
Nicolas Capens0bac2852016-05-07 06:09:58 -0400514 if (! node->promote(infoSink, funcReturnType))
515 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400516
Nicolas Capens0bac2852016-05-07 06:09:58 -0400517 if (childTempConstant) {
518 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400519
Nicolas Capens0bac2852016-05-07 06:09:58 -0400520 if (newChild)
521 return newChild;
522 }
John Bauman66b8ab22014-05-06 15:57:45 -0400523
Nicolas Capens0bac2852016-05-07 06:09:58 -0400524 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400525}
526
527//
528// This is the safe way to change the operator on an aggregate, as it
529// does lots of error checking and fixing. Especially for establishing
Nicolas Capens41320522018-06-06 13:47:19 -0400530// a function call's operation on its set of parameters. Sequences
531// of instructions are also aggregates, but they just directly set
John Bauman66b8ab22014-05-06 15:57:45 -0400532// their operator to EOpSequence.
533//
534// Returns an aggregate node, which could be the one passed in if
535// it was already an aggregate but no operator was set.
536//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400537TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400538{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400539 TIntermAggregate* aggNode;
John Bauman66b8ab22014-05-06 15:57:45 -0400540
Nicolas Capens0bac2852016-05-07 06:09:58 -0400541 //
542 // Make sure we have an aggregate. If not turn it into one.
543 //
544 if (node) {
545 aggNode = node->getAsAggregate();
546 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
547 //
548 // Make an aggregate containing this node.
549 //
550 aggNode = new TIntermAggregate();
551 aggNode->getSequence().push_back(node);
552 }
553 } else
554 aggNode = new TIntermAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -0400555
Nicolas Capens0bac2852016-05-07 06:09:58 -0400556 //
557 // Set the operator.
558 //
559 aggNode->setOp(op);
John Bauman66b8ab22014-05-06 15:57:45 -0400560
Nicolas Capens0bac2852016-05-07 06:09:58 -0400561 return aggNode;
John Bauman66b8ab22014-05-06 15:57:45 -0400562}
563
564//
John Bauman66b8ab22014-05-06 15:57:45 -0400565// Safe way to combine two nodes into an aggregate. Works with null pointers,
566// a node that's not a aggregate yet, etc.
567//
568// Returns the resulting aggregate, unless 0 was passed in for
569// both existing nodes.
570//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400571TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400572{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400573 if (left == 0 && right == 0)
574 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400575
Nicolas Capens0bac2852016-05-07 06:09:58 -0400576 TIntermAggregate* aggNode = 0;
577 if (left)
578 aggNode = left->getAsAggregate();
579 if (!aggNode || aggNode->getOp() != EOpNull) {
580 aggNode = new TIntermAggregate;
581 if (left)
582 aggNode->getSequence().push_back(left);
583 }
John Bauman66b8ab22014-05-06 15:57:45 -0400584
Nicolas Capens0bac2852016-05-07 06:09:58 -0400585 if (right)
586 aggNode->getSequence().push_back(right);
John Bauman66b8ab22014-05-06 15:57:45 -0400587
Nicolas Capens0bac2852016-05-07 06:09:58 -0400588 aggNode->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400589
Nicolas Capens0bac2852016-05-07 06:09:58 -0400590 return aggNode;
John Bauman66b8ab22014-05-06 15:57:45 -0400591}
592
593//
594// Turn an existing node into an aggregate.
595//
596// Returns an aggregate, unless 0 was passed in for the existing node.
597//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400598TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400599{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400600 if (node == 0)
601 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400602
Nicolas Capens0bac2852016-05-07 06:09:58 -0400603 TIntermAggregate* aggNode = new TIntermAggregate;
604 aggNode->getSequence().push_back(node);
John Bauman66b8ab22014-05-06 15:57:45 -0400605
Nicolas Capens0bac2852016-05-07 06:09:58 -0400606 aggNode->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400607
Nicolas Capens0bac2852016-05-07 06:09:58 -0400608 return aggNode;
John Bauman66b8ab22014-05-06 15:57:45 -0400609}
610
611//
612// For "if" test nodes. There are three children; a condition,
613// a true path, and a false path. The two paths are in the
614// nodePair.
615//
616// Returns the selection node created.
617//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400618TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400619{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400620 //
621 // For compile time constant selections, prune the code and
622 // test now.
623 //
John Bauman66b8ab22014-05-06 15:57:45 -0400624
Nicolas Capens0bac2852016-05-07 06:09:58 -0400625 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
626 if (cond->getAsConstantUnion()->getBConst(0) == true)
627 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence, nodePair.node1->getLine()) : nullptr;
628 else
629 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence, nodePair.node2->getLine()) : nullptr;
630 }
John Bauman66b8ab22014-05-06 15:57:45 -0400631
Nicolas Capens0bac2852016-05-07 06:09:58 -0400632 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
633 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400634
Nicolas Capens0bac2852016-05-07 06:09:58 -0400635 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400636}
637
638
Alexis Hetu253fdd12015-07-07 15:12:46 -0400639TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400640{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400641 if (left->getType().getQualifier() == EvqConstExpr && right->getType().getQualifier() == EvqConstExpr) {
642 return right;
643 } else {
644 TIntermTyped *commaAggregate = growAggregate(left, right, line);
645 commaAggregate->getAsAggregate()->setOp(EOpComma);
646 commaAggregate->setType(right->getType());
647 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
648 return commaAggregate;
649 }
John Bauman66b8ab22014-05-06 15:57:45 -0400650}
651
652//
653// For "?:" test nodes. There are three children; a condition,
654// a true path, and a false path. The two paths are specified
655// as separate parameters.
656//
657// Returns the selection node created, or 0 if one could not be.
658//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400659TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400660{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400661 if (trueBlock->getType() != falseBlock->getType())
662 {
663 return 0;
664 }
John Bauman66b8ab22014-05-06 15:57:45 -0400665
Nicolas Capens0bac2852016-05-07 06:09:58 -0400666 //
667 // See if all the operands are constant, then fold it otherwise not.
668 //
John Bauman66b8ab22014-05-06 15:57:45 -0400669
Nicolas Capens0bac2852016-05-07 06:09:58 -0400670 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
671 if (cond->getAsConstantUnion()->getBConst(0))
672 return trueBlock;
673 else
674 return falseBlock;
675 }
John Bauman66b8ab22014-05-06 15:57:45 -0400676
Nicolas Capens0bac2852016-05-07 06:09:58 -0400677 //
678 // Make a selection node.
679 //
680 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
681 node->getTypePointer()->setQualifier(EvqTemporary);
682 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400683
Nicolas Capens0bac2852016-05-07 06:09:58 -0400684 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400685}
686
Alexis Hetu76a343a2015-06-04 17:21:22 -0400687TIntermSwitch *TIntermediate::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
688{
689 TIntermSwitch *node = new TIntermSwitch(init, statementList);
690 node->setLine(line);
691
692 return node;
693}
694
695TIntermCase *TIntermediate::addCase(TIntermTyped *condition, const TSourceLoc &line)
696{
697 TIntermCase *node = new TIntermCase(condition);
698 node->setLine(line);
699
700 return node;
701}
702
John Bauman66b8ab22014-05-06 15:57:45 -0400703//
704// Constant terminal nodes. Has a union that contains bool, float or int constants
705//
706// Returns the constant union node created.
707//
708
Alexis Hetu253fdd12015-07-07 15:12:46 -0400709TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400710{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400711 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
712 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400713
Nicolas Capens0bac2852016-05-07 06:09:58 -0400714 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400715}
716
Alexis Hetu253fdd12015-07-07 15:12:46 -0400717TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400718{
719
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
John Bauman66b8ab22014-05-06 15:57:45 -0400721
Nicolas Capens0bac2852016-05-07 06:09:58 -0400722 node->setLine(line);
723 TIntermConstantUnion* constIntNode;
724 TIntermSequence &sequenceVector = node->getSequence();
725 ConstantUnion* unionArray;
John Bauman66b8ab22014-05-06 15:57:45 -0400726
Nicolas Capens0bac2852016-05-07 06:09:58 -0400727 for (int i = 0; i < fields.num; i++) {
728 unionArray = new ConstantUnion[1];
729 unionArray->setIConst(fields.offsets[i]);
730 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), line);
731 sequenceVector.push_back(constIntNode);
732 }
John Bauman66b8ab22014-05-06 15:57:45 -0400733
Nicolas Capens0bac2852016-05-07 06:09:58 -0400734 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400735}
736
737//
738// Create loop nodes.
739//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400740TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400741{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400742 TIntermNode* node = new TIntermLoop(type, init, cond, expr, body);
743 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400744
Nicolas Capens0bac2852016-05-07 06:09:58 -0400745 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400746}
747
748//
749// Add branches.
750//
Alexis Hetu253fdd12015-07-07 15:12:46 -0400751TIntermBranch* TIntermediate::addBranch(TOperator branchOp, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400752{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400753 return addBranch(branchOp, 0, line);
John Bauman66b8ab22014-05-06 15:57:45 -0400754}
755
Alexis Hetu253fdd12015-07-07 15:12:46 -0400756TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400757{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400758 TIntermBranch* node = new TIntermBranch(branchOp, expression);
759 node->setLine(line);
John Bauman66b8ab22014-05-06 15:57:45 -0400760
Nicolas Capens0bac2852016-05-07 06:09:58 -0400761 return node;
John Bauman66b8ab22014-05-06 15:57:45 -0400762}
763
764//
765// This is to be executed once the final root is put on top by the parsing
766// process.
767//
768bool TIntermediate::postProcess(TIntermNode* root)
769{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400770 if (root == 0)
771 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400772
Nicolas Capens0bac2852016-05-07 06:09:58 -0400773 //
774 // First, finish off the top level sequence, if any
775 //
776 TIntermAggregate* aggRoot = root->getAsAggregate();
777 if (aggRoot && aggRoot->getOp() == EOpNull)
778 aggRoot->setOp(EOpSequence);
John Bauman66b8ab22014-05-06 15:57:45 -0400779
Nicolas Capens0bac2852016-05-07 06:09:58 -0400780 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400781}
782
John Bauman66b8ab22014-05-06 15:57:45 -0400783////////////////////////////////////////////////////////////////
784//
785// Member functions of the nodes used for building the tree.
786//
787////////////////////////////////////////////////////////////////
788
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500789// static
790TIntermTyped *TIntermTyped::CreateIndexNode(int index)
791{
792 ConstantUnion *u = new ConstantUnion[1];
793 u[0].setIConst(index);
794
795 TType type(EbtInt, EbpUndefined, EvqConstExpr, 1);
796 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
797 return node;
798}
799
John Bauman66b8ab22014-05-06 15:57:45 -0400800//
801// Say whether or not an operation node changes the value of a variable.
802//
803// Returns true if state is modified.
804//
805bool TIntermOperator::modifiesState() const
806{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400807 switch (op) {
808 case EOpPostIncrement:
809 case EOpPostDecrement:
810 case EOpPreIncrement:
811 case EOpPreDecrement:
812 case EOpAssign:
813 case EOpAddAssign:
814 case EOpSubAssign:
815 case EOpMulAssign:
816 case EOpVectorTimesMatrixAssign:
817 case EOpVectorTimesScalarAssign:
818 case EOpMatrixTimesScalarAssign:
819 case EOpMatrixTimesMatrixAssign:
820 case EOpDivAssign:
821 case EOpIModAssign:
822 case EOpBitShiftLeftAssign:
823 case EOpBitShiftRightAssign:
824 case EOpBitwiseAndAssign:
825 case EOpBitwiseXorAssign:
826 case EOpBitwiseOrAssign:
827 return true;
828 default:
829 return false;
830 }
John Bauman66b8ab22014-05-06 15:57:45 -0400831}
832
833//
834// returns true if the operator is for one of the constructors
835//
836bool TIntermOperator::isConstructor() const
837{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400838 switch (op) {
839 case EOpConstructVec2:
840 case EOpConstructVec3:
841 case EOpConstructVec4:
842 case EOpConstructMat2:
843 case EOpConstructMat2x3:
844 case EOpConstructMat2x4:
845 case EOpConstructMat3x2:
846 case EOpConstructMat3:
847 case EOpConstructMat3x4:
848 case EOpConstructMat4x2:
849 case EOpConstructMat4x3:
850 case EOpConstructMat4:
851 case EOpConstructFloat:
852 case EOpConstructIVec2:
853 case EOpConstructIVec3:
854 case EOpConstructIVec4:
855 case EOpConstructInt:
856 case EOpConstructUVec2:
857 case EOpConstructUVec3:
858 case EOpConstructUVec4:
859 case EOpConstructUInt:
860 case EOpConstructBVec2:
861 case EOpConstructBVec3:
862 case EOpConstructBVec4:
863 case EOpConstructBool:
864 case EOpConstructStruct:
865 return true;
866 default:
867 return false;
868 }
John Bauman66b8ab22014-05-06 15:57:45 -0400869}
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400870
John Bauman66b8ab22014-05-06 15:57:45 -0400871//
872// Make sure the type of a unary operator is appropriate for its
873// combination of operation and operand type.
874//
875// Returns false in nothing makes sense.
876//
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -0400877bool TIntermUnary::promote(TInfoSink&, const TType *funcReturnType)
John Bauman66b8ab22014-05-06 15:57:45 -0400878{
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -0400879 setType(funcReturnType ? *funcReturnType : operand->getType());
880
881 // Unary operations result in temporary variables unless const.
Nicolas Capens0bac2852016-05-07 06:09:58 -0400882 if(type.getQualifier() != EvqConstExpr)
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -0400883 {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400884 type.setQualifier(EvqTemporary);
885 }
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -0400886
Nicolas Capens0bac2852016-05-07 06:09:58 -0400887 switch (op) {
888 case EOpLogicalNot:
889 if (operand->getBasicType() != EbtBool)
890 return false;
891 break;
892 case EOpBitwiseNot:
893 if (!IsInteger(operand->getBasicType()))
894 return false;
895 break;
896 case EOpNegative:
897 case EOpPostIncrement:
898 case EOpPostDecrement:
899 case EOpPreIncrement:
900 case EOpPreDecrement:
901 if (operand->getBasicType() == EbtBool)
902 return false;
903 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400904
Nicolas Capens0bac2852016-05-07 06:09:58 -0400905 // operators for built-ins are already type checked against their prototype
906 case EOpAny:
907 case EOpAll:
908 case EOpVectorLogicalNot:
Alexis Hetu0f448072016-03-18 10:56:08 -0400909 case EOpAbs:
910 case EOpSign:
911 case EOpIsNan:
912 case EOpIsInf:
913 case EOpFloatBitsToInt:
914 case EOpFloatBitsToUint:
915 case EOpIntBitsToFloat:
916 case EOpUintBitsToFloat:
917 case EOpPackSnorm2x16:
918 case EOpPackUnorm2x16:
919 case EOpPackHalf2x16:
920 case EOpUnpackSnorm2x16:
921 case EOpUnpackUnorm2x16:
922 case EOpUnpackHalf2x16:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400923 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400924
Nicolas Capens0bac2852016-05-07 06:09:58 -0400925 default:
926 if (operand->getBasicType() != EbtFloat)
927 return false;
928 }
John Bauman66b8ab22014-05-06 15:57:45 -0400929
Nicolas Capens0bac2852016-05-07 06:09:58 -0400930 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400931}
932
933//
934// Establishes the type of the resultant operation, as well as
935// makes the operator the correct one for the operands.
936//
937// Returns false if operator can't work on operands.
938//
939bool TIntermBinary::promote(TInfoSink& infoSink)
940{
Alexis Hetue5246692015-06-18 12:34:52 -0400941 ASSERT(left->isArray() == right->isArray());
John Bauman66b8ab22014-05-06 15:57:45 -0400942
Nicolas Capens0bac2852016-05-07 06:09:58 -0400943 // GLSL ES 2.0 does not support implicit type casting.
944 // So the basic type should always match.
Alexis Hetu92ab1982015-08-18 15:28:27 -0400945 // GLSL ES 3.0 supports integer shift operands of different signedness.
946 if(op != EOpBitShiftLeft &&
947 op != EOpBitShiftRight &&
948 op != EOpBitShiftLeftAssign &&
949 op != EOpBitShiftRightAssign &&
950 left->getBasicType() != right->getBasicType())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400951 {
952 return false;
953 }
John Bauman66b8ab22014-05-06 15:57:45 -0400954
Nicolas Capens0bac2852016-05-07 06:09:58 -0400955 //
956 // Base assumption: just make the type the same as the left
957 // operand. Then only deviations from this need be coded.
958 //
959 setType(left->getType());
John Bauman66b8ab22014-05-06 15:57:45 -0400960
Nicolas Capens0bac2852016-05-07 06:09:58 -0400961 // The result gets promoted to the highest precision.
962 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
963 getTypePointer()->setPrecision(higherPrecision);
John Bauman66b8ab22014-05-06 15:57:45 -0400964
Nicolas Capens0bac2852016-05-07 06:09:58 -0400965 // Binary operations results in temporary variables unless both
966 // operands are const.
967 if (left->getQualifier() != EvqConstExpr || right->getQualifier() != EvqConstExpr) {
968 getTypePointer()->setQualifier(EvqTemporary);
969 }
John Bauman66b8ab22014-05-06 15:57:45 -0400970
Nicolas Capens0bac2852016-05-07 06:09:58 -0400971 int primarySize = std::max(left->getNominalSize(), right->getNominalSize());
John Bauman66b8ab22014-05-06 15:57:45 -0400972
Nicolas Capens0bac2852016-05-07 06:09:58 -0400973 //
974 // All scalars. Code after this test assumes this case is removed!
975 //
976 if (primarySize == 1) {
977 switch (op) {
978 //
979 // Promote to conditional
980 //
981 case EOpEqual:
982 case EOpNotEqual:
983 case EOpLessThan:
984 case EOpGreaterThan:
985 case EOpLessThanEqual:
986 case EOpGreaterThanEqual:
987 setType(TType(EbtBool, EbpUndefined));
988 break;
John Bauman66b8ab22014-05-06 15:57:45 -0400989
Nicolas Capens0bac2852016-05-07 06:09:58 -0400990 //
991 // And and Or operate on conditionals
992 //
993 case EOpLogicalAnd:
994 case EOpLogicalOr:
995 case EOpLogicalXor:
996 // Both operands must be of type bool.
997 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
998 return false;
999 setType(TType(EbtBool, EbpUndefined));
1000 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001001
Nicolas Capens0bac2852016-05-07 06:09:58 -04001002 default:
1003 break;
1004 }
1005 return true;
1006 }
John Bauman66b8ab22014-05-06 15:57:45 -04001007
Nicolas Capens0bac2852016-05-07 06:09:58 -04001008 // If we reach here, at least one of the operands is vector or matrix.
1009 // The other operand could be a scalar, vector, or matrix.
1010 // Can these two operands be combined?
1011 //
1012 TBasicType basicType = left->getBasicType();
1013 switch (op) {
1014 case EOpMul:
1015 if (!left->isMatrix() && right->isMatrix()) {
1016 if (left->isVector())
1017 {
1018 op = EOpVectorTimesMatrix;
1019 setType(TType(basicType, higherPrecision, EvqTemporary,
1020 static_cast<unsigned char>(right->getNominalSize()), 1));
1021 }
1022 else {
1023 op = EOpMatrixTimesScalar;
1024 setType(TType(basicType, higherPrecision, EvqTemporary,
1025 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(right->getSecondarySize())));
1026 }
1027 } else if (left->isMatrix() && !right->isMatrix()) {
1028 if (right->isVector()) {
1029 op = EOpMatrixTimesVector;
1030 setType(TType(basicType, higherPrecision, EvqTemporary,
1031 static_cast<unsigned char>(left->getSecondarySize()), 1));
1032 } else {
1033 op = EOpMatrixTimesScalar;
1034 }
1035 } else if (left->isMatrix() && right->isMatrix()) {
1036 op = EOpMatrixTimesMatrix;
1037 setType(TType(basicType, higherPrecision, EvqTemporary,
1038 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(left->getSecondarySize())));
1039 } else if (!left->isMatrix() && !right->isMatrix()) {
1040 if (left->isVector() && right->isVector()) {
1041 // leave as component product
1042 } else if (left->isVector() || right->isVector()) {
1043 op = EOpVectorTimesScalar;
1044 setType(TType(basicType, higherPrecision, EvqTemporary,
1045 static_cast<unsigned char>(primarySize), 1));
1046 }
1047 } else {
1048 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
1049 return false;
1050 }
Alexis Hetu00106d42015-04-23 11:45:35 -04001051
Nicolas Capens0bac2852016-05-07 06:09:58 -04001052 if(!ValidateMultiplication(op, left->getType(), right->getType()))
1053 {
1054 return false;
1055 }
1056 break;
1057 case EOpMulAssign:
1058 if (!left->isMatrix() && right->isMatrix()) {
1059 if (left->isVector())
1060 op = EOpVectorTimesMatrixAssign;
1061 else {
1062 return false;
1063 }
1064 } else if (left->isMatrix() && !right->isMatrix()) {
1065 if (right->isVector()) {
1066 return false;
1067 } else {
1068 op = EOpMatrixTimesScalarAssign;
1069 }
1070 } else if (left->isMatrix() && right->isMatrix()) {
1071 op = EOpMatrixTimesMatrixAssign;
1072 setType(TType(basicType, higherPrecision, EvqTemporary,
1073 static_cast<unsigned char>(right->getNominalSize()), static_cast<unsigned char>(left->getSecondarySize())));
1074 } else if (!left->isMatrix() && !right->isMatrix()) {
1075 if (left->isVector() && right->isVector()) {
1076 // leave as component product
1077 } else if (left->isVector() || right->isVector()) {
1078 if (! left->isVector())
1079 return false;
1080 op = EOpVectorTimesScalarAssign;
1081 setType(TType(basicType, higherPrecision, EvqTemporary,
1082 static_cast<unsigned char>(left->getNominalSize()), 1));
1083 }
1084 } else {
1085 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
1086 return false;
1087 }
Alexis Hetu00106d42015-04-23 11:45:35 -04001088
Nicolas Capens0bac2852016-05-07 06:09:58 -04001089 if(!ValidateMultiplication(op, left->getType(), right->getType()))
1090 {
1091 return false;
1092 }
1093 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001094
Nicolas Capens0bac2852016-05-07 06:09:58 -04001095 case EOpAssign:
1096 case EOpInitialize:
1097 // No more additional checks are needed.
1098 if ((left->getNominalSize() != right->getNominalSize()) ||
1099 (left->getSecondarySize() != right->getSecondarySize()))
1100 return false;
1101 break;
1102 case EOpAdd:
1103 case EOpSub:
1104 case EOpDiv:
1105 case EOpIMod:
1106 case EOpBitShiftLeft:
1107 case EOpBitShiftRight:
1108 case EOpBitwiseAnd:
1109 case EOpBitwiseXor:
1110 case EOpBitwiseOr:
1111 case EOpAddAssign:
1112 case EOpSubAssign:
1113 case EOpDivAssign:
1114 case EOpIModAssign:
1115 case EOpBitShiftLeftAssign:
1116 case EOpBitShiftRightAssign:
1117 case EOpBitwiseAndAssign:
1118 case EOpBitwiseXorAssign:
1119 case EOpBitwiseOrAssign:
1120 if ((left->isMatrix() && right->isVector()) ||
1121 (left->isVector() && right->isMatrix()))
1122 return false;
Alexis Hetud061e422015-05-13 16:37:50 -04001123
Nicolas Capens0bac2852016-05-07 06:09:58 -04001124 // Are the sizes compatible?
1125 if(left->getNominalSize() != right->getNominalSize() ||
1126 left->getSecondarySize() != right->getSecondarySize())
1127 {
1128 // If the nominal sizes of operands do not match:
1129 // One of them must be a scalar.
1130 if(!left->isScalar() && !right->isScalar())
1131 return false;
Alexis Hetud061e422015-05-13 16:37:50 -04001132
Nicolas Capens0bac2852016-05-07 06:09:58 -04001133 // In the case of compound assignment other than multiply-assign,
1134 // the right side needs to be a scalar. Otherwise a vector/matrix
1135 // would be assigned to a scalar. A scalar can't be shifted by a
1136 // vector either.
1137 if(!right->isScalar() && (modifiesState() || op == EOpBitShiftLeft || op == EOpBitShiftRight))
1138 return false;
1139 }
Alexis Hetud061e422015-05-13 16:37:50 -04001140
Nicolas Capens0bac2852016-05-07 06:09:58 -04001141 {
1142 const int secondarySize = std::max(
1143 left->getSecondarySize(), right->getSecondarySize());
1144 setType(TType(basicType, higherPrecision, EvqTemporary,
1145 static_cast<unsigned char>(primarySize), static_cast<unsigned char>(secondarySize)));
1146 if(left->isArray())
1147 {
1148 ASSERT(left->getArraySize() == right->getArraySize());
1149 type.setArraySize(left->getArraySize());
1150 }
1151 }
1152 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001153
Nicolas Capens0bac2852016-05-07 06:09:58 -04001154 case EOpEqual:
1155 case EOpNotEqual:
1156 case EOpLessThan:
1157 case EOpGreaterThan:
1158 case EOpLessThanEqual:
1159 case EOpGreaterThanEqual:
1160 if ((left->getNominalSize() != right->getNominalSize()) ||
1161 (left->getSecondarySize() != right->getSecondarySize()))
1162 return false;
1163 setType(TType(EbtBool, EbpUndefined));
1164 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001165
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001166 case EOpOuterProduct:
1167 if(!left->isVector() || !right->isVector())
1168 return false;
1169 setType(TType(EbtFloat, right->getNominalSize(), left->getNominalSize()));
1170 break;
1171
1172 case EOpTranspose:
1173 if(!right->isMatrix())
1174 return false;
1175 setType(TType(EbtFloat, right->getSecondarySize(), right->getNominalSize()));
1176 break;
1177
1178 case EOpDeterminant:
1179 if(!right->isMatrix())
1180 return false;
1181 setType(TType(EbtFloat));
1182 break;
1183
1184 case EOpInverse:
1185 if(!right->isMatrix() || right->getNominalSize() != right->getSecondarySize())
1186 return false;
1187 setType(right->getType());
1188 break;
1189
Nicolas Capens0bac2852016-05-07 06:09:58 -04001190 default:
1191 return false;
1192 }
Nicolas Capens5e0c3542016-04-05 17:18:24 -04001193
Nicolas Capens0bac2852016-05-07 06:09:58 -04001194 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001195}
1196
1197bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1198{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001199 const TFieldList& fields = leftNodeType.getStruct()->fields();
John Bauman66b8ab22014-05-06 15:57:45 -04001200
Nicolas Capens0bac2852016-05-07 06:09:58 -04001201 size_t structSize = fields.size();
1202 int index = 0;
John Bauman66b8ab22014-05-06 15:57:45 -04001203
Nicolas Capens0bac2852016-05-07 06:09:58 -04001204 for (size_t j = 0; j < structSize; j++) {
1205 size_t size = fields[j]->type()->getObjectSize();
1206 for(size_t i = 0; i < size; i++) {
1207 if (fields[j]->type()->getBasicType() == EbtStruct) {
1208 if (!CompareStructure(*(fields[j]->type()), &rightUnionArray[index], &leftUnionArray[index]))
1209 return false;
1210 } else {
1211 if (leftUnionArray[index] != rightUnionArray[index])
1212 return false;
1213 index++;
1214 }
John Bauman66b8ab22014-05-06 15:57:45 -04001215
Nicolas Capens0bac2852016-05-07 06:09:58 -04001216 }
1217 }
1218 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001219}
1220
1221bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1222{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001223 if (leftNodeType.isArray()) {
1224 TType typeWithoutArrayness = leftNodeType;
1225 typeWithoutArrayness.clearArrayness();
John Bauman66b8ab22014-05-06 15:57:45 -04001226
Nicolas Capens0bac2852016-05-07 06:09:58 -04001227 int arraySize = leftNodeType.getArraySize();
John Bauman66b8ab22014-05-06 15:57:45 -04001228
Nicolas Capens0bac2852016-05-07 06:09:58 -04001229 for (int i = 0; i < arraySize; ++i) {
1230 size_t offset = typeWithoutArrayness.getObjectSize() * i;
1231 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
1232 return false;
1233 }
1234 } else
1235 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
John Bauman66b8ab22014-05-06 15:57:45 -04001236
Nicolas Capens0bac2852016-05-07 06:09:58 -04001237 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001238}
1239
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001240float determinant2(float m00, float m01, float m10, float m11)
1241{
1242 return m00 * m11 - m01 * m10;
1243}
1244
1245float determinant3(float m00, float m01, float m02,
Nicolas Capens0bac2852016-05-07 06:09:58 -04001246 float m10, float m11, float m12,
1247 float m20, float m21, float m22)
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001248{
1249 return m00 * determinant2(m11, m12, m21, m22) -
Nicolas Capens0bac2852016-05-07 06:09:58 -04001250 m10 * determinant2(m01, m02, m21, m22) +
1251 m20 * determinant2(m01, m02, m11, m12);
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001252}
1253
1254float determinant4(float m00, float m01, float m02, float m03,
Nicolas Capens0bac2852016-05-07 06:09:58 -04001255 float m10, float m11, float m12, float m13,
1256 float m20, float m21, float m22, float m23,
1257 float m30, float m31, float m32, float m33)
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001258{
1259 return m00 * determinant3(m11, m12, m13, m21, m22, m23, m31, m32, m33) -
Nicolas Capens0bac2852016-05-07 06:09:58 -04001260 m10 * determinant3(m01, m02, m03, m21, m22, m23, m31, m32, m33) +
1261 m20 * determinant3(m01, m02, m03, m11, m12, m13, m31, m32, m33) -
1262 m30 * determinant3(m01, m02, m03, m11, m12, m13, m21, m22, m23);
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001263}
1264
1265float ComputeDeterminant(int size, ConstantUnion* unionArray)
1266{
1267 switch(size)
1268 {
1269 case 2:
1270 return determinant2(unionArray[0].getFConst(),
Nicolas Capens0bac2852016-05-07 06:09:58 -04001271 unionArray[1].getFConst(),
1272 unionArray[2].getFConst(),
1273 unionArray[3].getFConst());
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001274 case 3:
1275 return determinant3(unionArray[0].getFConst(),
Nicolas Capens0bac2852016-05-07 06:09:58 -04001276 unionArray[1].getFConst(),
1277 unionArray[2].getFConst(),
1278 unionArray[3].getFConst(),
1279 unionArray[4].getFConst(),
1280 unionArray[5].getFConst(),
1281 unionArray[6].getFConst(),
1282 unionArray[7].getFConst(),
1283 unionArray[8].getFConst());
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001284 case 4:
1285 return determinant4(unionArray[0].getFConst(),
Nicolas Capens0bac2852016-05-07 06:09:58 -04001286 unionArray[1].getFConst(),
1287 unionArray[2].getFConst(),
1288 unionArray[3].getFConst(),
1289 unionArray[4].getFConst(),
1290 unionArray[5].getFConst(),
1291 unionArray[6].getFConst(),
1292 unionArray[7].getFConst(),
1293 unionArray[8].getFConst(),
1294 unionArray[9].getFConst(),
1295 unionArray[10].getFConst(),
1296 unionArray[11].getFConst(),
1297 unionArray[12].getFConst(),
1298 unionArray[13].getFConst(),
1299 unionArray[14].getFConst(),
1300 unionArray[15].getFConst());
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001301 default:
Nicolas Capens3713cd42015-06-22 10:41:54 -04001302 UNREACHABLE(size);
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001303 return 0.0f;
1304 }
1305}
1306
1307ConstantUnion* CreateInverse(TIntermConstantUnion* node, ConstantUnion* unionArray)
1308{
1309 ConstantUnion* tempConstArray = 0;
1310 int size = node->getNominalSize();
1311 float determinant = ComputeDeterminant(size, unionArray);
1312 if(determinant != 0.0f)
1313 {
1314 float invDet = 1.0f / determinant;
1315 tempConstArray = new ConstantUnion[size*size];
1316 switch(size)
1317 {
1318 case 2:
1319 {
1320 float m00 = unionArray[0].getFConst(); // Matrix is:
1321 float m01 = unionArray[1].getFConst(); // (m00, m01)
1322 float m10 = unionArray[2].getFConst(); // (m10, m11)
1323 float m11 = unionArray[3].getFConst();
1324 tempConstArray[0].setFConst( invDet * m11);
1325 tempConstArray[1].setFConst(-invDet * m01);
1326 tempConstArray[2].setFConst(-invDet * m10);
1327 tempConstArray[3].setFConst( invDet * m00);
1328 }
1329 break;
1330 case 3:
1331 {
1332 float m00 = unionArray[0].getFConst(); // Matrix is:
1333 float m01 = unionArray[1].getFConst(); // (m00, m01, m02)
1334 float m02 = unionArray[2].getFConst(); // (m10, m11, m12)
1335 float m10 = unionArray[3].getFConst(); // (m20, m21, m22)
1336 float m11 = unionArray[4].getFConst();
1337 float m12 = unionArray[5].getFConst();
1338 float m20 = unionArray[6].getFConst();
1339 float m21 = unionArray[7].getFConst();
1340 float m22 = unionArray[8].getFConst();
1341 tempConstArray[0].setFConst(invDet * determinant2(m11, m12, m21, m22)); // m00 = invDet * (m11 * m22 - m12 * m21)
1342 tempConstArray[1].setFConst(invDet * determinant2(m12, m10, m22, m20)); // m01 = -invDet * (m10 * m22 - m12 * m20)
1343 tempConstArray[2].setFConst(invDet * determinant2(m10, m11, m20, m21)); // m02 = invDet * (m10 * m21 - m11 * m20)
1344 tempConstArray[3].setFConst(invDet * determinant2(m21, m22, m01, m02)); // m10 = -invDet * (m01 * m22 - m02 * m21)
1345 tempConstArray[4].setFConst(invDet * determinant2(m00, m02, m20, m22)); // m11 = invDet * (m00 * m22 - m02 * m20)
1346 tempConstArray[5].setFConst(invDet * determinant2(m20, m21, m00, m01)); // m12 = -invDet * (m00 * m21 - m01 * m20)
1347 tempConstArray[6].setFConst(invDet * determinant2(m01, m02, m11, m12)); // m20 = invDet * (m01 * m12 - m02 * m11)
1348 tempConstArray[7].setFConst(invDet * determinant2(m10, m12, m00, m02)); // m21 = -invDet * (m00 * m12 - m02 * m10)
1349 tempConstArray[8].setFConst(invDet * determinant2(m00, m01, m10, m11)); // m22 = invDet * (m00 * m11 - m01 * m10)
1350 }
1351 break;
1352 case 4:
1353 {
1354 float m00 = unionArray[0].getFConst(); // Matrix is:
1355 float m01 = unionArray[1].getFConst(); // (m00, m01, m02, m03)
1356 float m02 = unionArray[2].getFConst(); // (m10, m11, m12, m13)
1357 float m03 = unionArray[3].getFConst(); // (m20, m21, m22, m23)
1358 float m10 = unionArray[4].getFConst(); // (m30, m31, m32, m33)
1359 float m11 = unionArray[5].getFConst();
1360 float m12 = unionArray[6].getFConst();
1361 float m13 = unionArray[7].getFConst();
1362 float m20 = unionArray[8].getFConst();
1363 float m21 = unionArray[9].getFConst();
1364 float m22 = unionArray[10].getFConst();
1365 float m23 = unionArray[11].getFConst();
1366 float m30 = unionArray[12].getFConst();
1367 float m31 = unionArray[13].getFConst();
1368 float m32 = unionArray[14].getFConst();
1369 float m33 = unionArray[15].getFConst();
1370 tempConstArray[ 0].setFConst( invDet * determinant3(m11, m12, m13, m21, m22, m23, m31, m32, m33)); // m00
1371 tempConstArray[ 1].setFConst(-invDet * determinant3(m10, m12, m13, m20, m22, m23, m30, m32, m33)); // m01
1372 tempConstArray[ 2].setFConst( invDet * determinant3(m10, m11, m13, m20, m21, m23, m30, m31, m33)); // m02
1373 tempConstArray[ 3].setFConst(-invDet * determinant3(m10, m11, m12, m20, m21, m22, m30, m31, m32)); // m03
1374 tempConstArray[ 4].setFConst( invDet * determinant3(m01, m02, m03, m21, m22, m23, m31, m32, m33)); // m10
1375 tempConstArray[ 5].setFConst(-invDet * determinant3(m00, m02, m03, m20, m22, m23, m30, m32, m33)); // m11
1376 tempConstArray[ 6].setFConst( invDet * determinant3(m00, m01, m03, m20, m21, m23, m30, m31, m33)); // m12
1377 tempConstArray[ 7].setFConst(-invDet * determinant3(m00, m01, m02, m20, m21, m22, m30, m31, m32)); // m13
1378 tempConstArray[ 8].setFConst( invDet * determinant3(m01, m02, m03, m11, m12, m13, m31, m32, m33)); // m20
1379 tempConstArray[ 9].setFConst(-invDet * determinant3(m00, m02, m03, m10, m12, m13, m30, m32, m33)); // m21
1380 tempConstArray[10].setFConst( invDet * determinant3(m00, m01, m03, m10, m11, m13, m30, m31, m33)); // m22
1381 tempConstArray[11].setFConst(-invDet * determinant3(m00, m01, m02, m10, m11, m12, m30, m31, m32)); // m23
1382 tempConstArray[12].setFConst( invDet * determinant3(m01, m02, m03, m11, m12, m13, m21, m22, m23)); // m30
1383 tempConstArray[13].setFConst(-invDet * determinant3(m00, m02, m03, m10, m12, m13, m20, m22, m23)); // m31
1384 tempConstArray[14].setFConst( invDet * determinant3(m00, m01, m03, m10, m11, m13, m20, m21, m23)); // m32
1385 tempConstArray[15].setFConst(-invDet * determinant3(m00, m01, m02, m10, m11, m12, m20, m21, m22)); // m33
1386 }
1387 break;
1388 default:
Nicolas Capens3713cd42015-06-22 10:41:54 -04001389 UNREACHABLE(size);
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001390 }
1391 }
1392 return tempConstArray;
1393}
1394
John Bauman66b8ab22014-05-06 15:57:45 -04001395//
1396// The fold functions see if an operation on a constant can be done in place,
1397// without generating run-time code.
1398//
1399// Returns the node to keep using, which may or may not be the node passed in.
1400//
1401
1402TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
1403{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001404 ConstantUnion *unionArray = getUnionArrayPointer();
1405 size_t objectSize = getType().getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04001406
Nicolas Capens0bac2852016-05-07 06:09:58 -04001407 if (constantNode) { // binary operations
1408 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
1409 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
1410 TType returnType = getType();
John Bauman66b8ab22014-05-06 15:57:45 -04001411
Nicolas Capens0bac2852016-05-07 06:09:58 -04001412 // for a case like float f = 1.2 + vec4(2,3,4,5);
1413 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
1414 rightUnionArray = new ConstantUnion[objectSize];
1415 for (size_t i = 0; i < objectSize; ++i)
1416 rightUnionArray[i] = *node->getUnionArrayPointer();
1417 returnType = getType();
1418 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
1419 // for a case like float f = vec4(2,3,4,5) + 1.2;
1420 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
1421 for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i)
1422 unionArray[i] = *getUnionArrayPointer();
1423 returnType = node->getType();
1424 objectSize = constantNode->getType().getObjectSize();
1425 }
John Bauman66b8ab22014-05-06 15:57:45 -04001426
Nicolas Capens0bac2852016-05-07 06:09:58 -04001427 ConstantUnion* tempConstArray = 0;
1428 TIntermConstantUnion *tempNode;
John Bauman66b8ab22014-05-06 15:57:45 -04001429
Nicolas Capens0bac2852016-05-07 06:09:58 -04001430 switch(op) {
1431 case EOpAdd:
1432 tempConstArray = new ConstantUnion[objectSize];
1433 {// support MSVC++6.0
1434 for (size_t i = 0; i < objectSize; i++)
1435 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
1436 }
1437 break;
1438 case EOpSub:
1439 tempConstArray = new ConstantUnion[objectSize];
1440 {// support MSVC++6.0
1441 for (size_t i = 0; i < objectSize; i++)
1442 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
1443 }
1444 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001445
Nicolas Capens0bac2852016-05-07 06:09:58 -04001446 case EOpMul:
1447 case EOpVectorTimesScalar:
1448 case EOpMatrixTimesScalar:
1449 tempConstArray = new ConstantUnion[objectSize];
1450 {// support MSVC++6.0
1451 for (size_t i = 0; i < objectSize; i++)
1452 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
1453 }
1454 break;
1455 case EOpMatrixTimesMatrix:
1456 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
1457 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1458 return 0;
1459 }
1460 {// support MSVC++6.0
1461 int leftNumCols = getNominalSize();
1462 int leftNumRows = getSecondarySize();
1463 int rightNumCols = node->getNominalSize();
1464 int rightNumRows = node->getSecondarySize();
1465 if(leftNumCols != rightNumRows) {
1466 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1467 return 0;
1468 }
1469 int tempNumCols = rightNumCols;
1470 int tempNumRows = leftNumRows;
1471 int tempNumAdds = leftNumCols;
1472 tempConstArray = new ConstantUnion[tempNumCols*tempNumRows];
1473 for (int row = 0; row < tempNumRows; row++) {
1474 for (int column = 0; column < tempNumCols; column++) {
1475 tempConstArray[tempNumRows * column + row].setFConst(0.0f);
1476 for (int i = 0; i < tempNumAdds; i++) {
1477 tempConstArray[tempNumRows * column + row].setFConst(tempConstArray[tempNumRows * column + row].getFConst() + unionArray[i * leftNumRows + row].getFConst() * (rightUnionArray[column * rightNumRows + i].getFConst()));
1478 }
1479 }
1480 }
1481 // update return type for matrix product
1482 returnType.setNominalSize(static_cast<unsigned char>(tempNumCols));
1483 returnType.setSecondarySize(static_cast<unsigned char>(tempNumRows));
1484 }
1485 break;
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001486
1487 case EOpOuterProduct:
1488 {
1489 int leftSize = getNominalSize();
1490 int rightSize = node->getNominalSize();
1491 tempConstArray = new ConstantUnion[leftSize*rightSize];
1492 for(int row = 0; row < leftSize; row++) {
1493 for(int column = 0; column < rightSize; column++) {
1494 tempConstArray[leftSize * column + row].setFConst(unionArray[row].getFConst() * rightUnionArray[column].getFConst());
1495 }
1496 }
1497 // update return type for outer product
1498 returnType.setNominalSize(static_cast<unsigned char>(rightSize));
1499 returnType.setSecondarySize(static_cast<unsigned char>(leftSize));
1500 }
1501 break;
1502
1503 case EOpTranspose:
1504 {
1505 int rightCol = node->getNominalSize();
1506 int rightRow = node->getSecondarySize();
1507 tempConstArray = new ConstantUnion[rightCol*rightRow];
1508 for(int row = 0; row < rightRow; row++) {
1509 for(int column = 0; column < rightCol; column++) {
1510 tempConstArray[rightRow * column + row].setFConst(rightUnionArray[rightCol * row + column].getFConst());
1511 }
1512 }
1513 // update return type for transpose
1514 returnType.setNominalSize(static_cast<unsigned char>(rightRow));
1515 returnType.setSecondarySize(static_cast<unsigned char>(rightCol));
1516 }
1517 break;
1518
1519 case EOpDeterminant:
1520 {
1521 ASSERT(node->getNominalSize() == node->getSecondarySize());
1522
1523 tempConstArray = new ConstantUnion[1];
1524 tempConstArray[0].setFConst(ComputeDeterminant(node->getNominalSize(), rightUnionArray));
1525 // update return type for determinant
1526 returnType.setNominalSize(1);
1527 returnType.setSecondarySize(1);
1528 }
1529 break;
1530
1531 case EOpInverse:
1532 {
1533 ASSERT(node->getNominalSize() == node->getSecondarySize());
1534
1535 tempConstArray = CreateInverse(node, rightUnionArray);
1536 if(!tempConstArray)
1537 {
1538 // Singular matrix, just copy
1539 tempConstArray = new ConstantUnion[objectSize];
Alexis Hetuab752792016-04-21 16:11:31 -04001540 for(size_t i = 0; i < objectSize; i++)
Alexis Hetu3815b6c2015-05-27 11:21:37 -04001541 tempConstArray[i] = rightUnionArray[i];
1542 }
1543 }
1544 break;
1545
Nicolas Capens0bac2852016-05-07 06:09:58 -04001546 case EOpDiv:
1547 case EOpIMod:
Nicolas Capens5e0c3542016-04-05 17:18:24 -04001548 tempConstArray = new ConstantUnion[objectSize];
Nicolas Capens0bac2852016-05-07 06:09:58 -04001549 {// support MSVC++6.0
1550 for (size_t i = 0; i < objectSize; i++) {
1551 switch (getType().getBasicType()) {
1552 case EbtFloat:
1553 if (rightUnionArray[i] == 0.0f) {
1554 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1555 tempConstArray[i].setFConst(FLT_MAX);
1556 } else {
1557 ASSERT(op == EOpDiv);
1558 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
1559 }
1560 break;
1561
1562 case EbtInt:
1563 if (rightUnionArray[i] == 0) {
1564 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1565 tempConstArray[i].setIConst(INT_MAX);
1566 } else {
1567 if(op == EOpDiv) {
1568 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1569 } else {
1570 ASSERT(op == EOpIMod);
1571 tempConstArray[i].setIConst(unionArray[i].getIConst() % rightUnionArray[i].getIConst());
1572 }
1573 }
1574 break;
1575 case EbtUInt:
1576 if (rightUnionArray[i] == 0) {
1577 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1578 tempConstArray[i].setUConst(UINT_MAX);
1579 } else {
1580 if(op == EOpDiv) {
1581 tempConstArray[i].setUConst(unionArray[i].getUConst() / rightUnionArray[i].getUConst());
1582 } else {
1583 ASSERT(op == EOpIMod);
1584 tempConstArray[i].setUConst(unionArray[i].getUConst() % rightUnionArray[i].getUConst());
1585 }
1586 }
1587 break;
1588 default:
1589 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
1590 return 0;
1591 }
1592 }
1593 }
1594 break;
1595
1596 case EOpMatrixTimesVector:
1597 if (node->getBasicType() != EbtFloat) {
1598 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1599 return 0;
1600 }
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001601 tempConstArray = new ConstantUnion[getSecondarySize()];
Nicolas Capens0bac2852016-05-07 06:09:58 -04001602
1603 {// support MSVC++6.0
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001604 for (int rows = getSecondarySize(), i = 0; i < rows; i++) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001605 tempConstArray[i].setFConst(0.0f);
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001606 for (int cols = getNominalSize(), j = 0; j < cols; j++) {
1607 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*rows + i].getFConst()) * rightUnionArray[j].getFConst()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001608 }
1609 }
1610 }
1611
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001612 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtFloat, EbpUndefined, EvqConstExpr, getSecondarySize()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001613 tempNode->setLine(getLine());
1614
1615 return tempNode;
1616
1617 case EOpVectorTimesMatrix:
1618 if (getType().getBasicType() != EbtFloat) {
1619 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1620 return 0;
1621 }
1622
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001623 tempConstArray = new ConstantUnion[node->getNominalSize()];
Nicolas Capens0bac2852016-05-07 06:09:58 -04001624 {// support MSVC++6.0
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001625 for (int cols = node->getNominalSize(), i = 0; i < cols; i++) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001626 tempConstArray[i].setFConst(0.0f);
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001627 for (int rows = node->getSecondarySize(), j = 0; j < rows; j++) {
1628 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*rows + j].getFConst()));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001629 }
1630 }
1631 }
Chris Forbesc5e68ab2018-09-21 13:24:17 -07001632
1633 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtFloat, EbpUndefined, EvqConstExpr, node->getNominalSize()));
1634 tempNode->setLine(getLine());
1635 return tempNode;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001636
1637 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1638 tempConstArray = new ConstantUnion[objectSize];
1639 {// support MSVC++6.0
1640 for (size_t i = 0; i < objectSize; i++)
1641 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1642 }
1643 break;
1644
1645 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1646 tempConstArray = new ConstantUnion[objectSize];
1647 {// support MSVC++6.0
1648 for (size_t i = 0; i < objectSize; i++)
1649 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1650 }
1651 break;
1652
1653 case EOpLogicalXor:
1654 tempConstArray = new ConstantUnion[objectSize];
1655 {// support MSVC++6.0
1656 for (size_t i = 0; i < objectSize; i++)
1657 switch (getType().getBasicType()) {
1658 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1659 default: assert(false && "Default missing");
1660 }
1661 }
1662 break;
1663
1664 case EOpBitwiseAnd:
1665 tempConstArray = new ConstantUnion[objectSize];
1666 for(size_t i = 0; i < objectSize; i++)
1667 tempConstArray[i] = unionArray[i] & rightUnionArray[i];
1668 break;
1669 case EOpBitwiseXor:
1670 tempConstArray = new ConstantUnion[objectSize];
1671 for(size_t i = 0; i < objectSize; i++)
1672 tempConstArray[i] = unionArray[i] ^ rightUnionArray[i];
1673 break;
1674 case EOpBitwiseOr:
1675 tempConstArray = new ConstantUnion[objectSize];
1676 for(size_t i = 0; i < objectSize; i++)
1677 tempConstArray[i] = unionArray[i] | rightUnionArray[i];
1678 break;
1679 case EOpBitShiftLeft:
1680 tempConstArray = new ConstantUnion[objectSize];
1681 for(size_t i = 0; i < objectSize; i++)
1682 tempConstArray[i] = unionArray[i] << rightUnionArray[i];
1683 break;
1684 case EOpBitShiftRight:
1685 tempConstArray = new ConstantUnion[objectSize];
1686 for(size_t i = 0; i < objectSize; i++)
1687 tempConstArray[i] = unionArray[i] >> rightUnionArray[i];
1688 break;
1689
1690 case EOpLessThan:
1691 tempConstArray = new ConstantUnion[objectSize];
1692 for(size_t i = 0; i < objectSize; i++)
1693 tempConstArray[i].setBConst(unionArray[i] < rightUnionArray[i]);
1694 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr, objectSize);
1695 break;
1696 case EOpGreaterThan:
1697 tempConstArray = new ConstantUnion[objectSize];
1698 for(size_t i = 0; i < objectSize; i++)
Nicolas Capens5e0c3542016-04-05 17:18:24 -04001699 tempConstArray[i].setBConst(unionArray[i] > rightUnionArray[i]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001700 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr, objectSize);
1701 break;
1702 case EOpLessThanEqual:
1703 tempConstArray = new ConstantUnion[objectSize];
1704 for(size_t i = 0; i < objectSize; i++)
Nicolas Capens5e0c3542016-04-05 17:18:24 -04001705 tempConstArray[i].setBConst(unionArray[i] <= rightUnionArray[i]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001706 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr, objectSize);
1707 break;
1708 case EOpGreaterThanEqual:
1709 tempConstArray = new ConstantUnion[objectSize];
1710 for(size_t i = 0; i < objectSize; i++)
Nicolas Capens5e0c3542016-04-05 17:18:24 -04001711 tempConstArray[i].setBConst(unionArray[i] >= rightUnionArray[i]);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001712 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr, objectSize);
1713 break;
1714 case EOpEqual:
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001715 tempConstArray = new ConstantUnion[1];
1716
1717 if(getType().getBasicType() == EbtStruct) {
1718 tempConstArray->setBConst(CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001719 } else {
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001720 bool boolNodeFlag = true;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001721 for (size_t i = 0; i < objectSize; i++) {
1722 if (unionArray[i] != rightUnionArray[i]) {
1723 boolNodeFlag = false;
1724 break; // break out of for loop
1725 }
1726 }
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001727 tempConstArray->setBConst(boolNodeFlag);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001728 }
John Bauman66b8ab22014-05-06 15:57:45 -04001729
Nicolas Capens0bac2852016-05-07 06:09:58 -04001730 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
1731 tempNode->setLine(getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001732
Nicolas Capens0bac2852016-05-07 06:09:58 -04001733 return tempNode;
John Bauman66b8ab22014-05-06 15:57:45 -04001734
Nicolas Capens0bac2852016-05-07 06:09:58 -04001735 case EOpNotEqual:
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001736 tempConstArray = new ConstantUnion[1];
1737
1738 if(getType().getBasicType() == EbtStruct) {
1739 tempConstArray->setBConst(!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray));
Nicolas Capens0bac2852016-05-07 06:09:58 -04001740 } else {
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001741 bool boolNodeFlag = false;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001742 for (size_t i = 0; i < objectSize; i++) {
1743 if (unionArray[i] != rightUnionArray[i]) {
1744 boolNodeFlag = true;
1745 break; // break out of for loop
1746 }
1747 }
Alexis Hetu26ea8f52016-04-28 12:45:48 -04001748 tempConstArray->setBConst(boolNodeFlag);
Nicolas Capens0bac2852016-05-07 06:09:58 -04001749 }
John Bauman66b8ab22014-05-06 15:57:45 -04001750
Nicolas Capens0bac2852016-05-07 06:09:58 -04001751 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
1752 tempNode->setLine(getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001753
Nicolas Capens0bac2852016-05-07 06:09:58 -04001754 return tempNode;
Nicolas Capens91dfb972016-04-09 23:45:12 -04001755 case EOpMax:
Nicolas Capens0bac2852016-05-07 06:09:58 -04001756 tempConstArray = new ConstantUnion[objectSize];
1757 {// support MSVC++6.0
1758 for (size_t i = 0; i < objectSize; i++)
1759 tempConstArray[i] = unionArray[i] > rightUnionArray[i] ? unionArray[i] : rightUnionArray[i];
1760 }
1761 break;
1762 case EOpMin:
1763 tempConstArray = new ConstantUnion[objectSize];
1764 {// support MSVC++6.0
1765 for (size_t i = 0; i < objectSize; i++)
1766 tempConstArray[i] = unionArray[i] < rightUnionArray[i] ? unionArray[i] : rightUnionArray[i];
1767 }
1768 break;
1769 default:
1770 return 0;
1771 }
1772 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1773 tempNode->setLine(getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001774
Nicolas Capens0bac2852016-05-07 06:09:58 -04001775 return tempNode;
1776 } else {
1777 //
1778 // Do unary operations
1779 //
1780 TIntermConstantUnion *newNode = 0;
1781 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001782 TType type = getType();
1783 TBasicType basicType = type.getBasicType();
Nicolas Capens0bac2852016-05-07 06:09:58 -04001784 for (size_t i = 0; i < objectSize; i++) {
1785 switch(op) {
1786 case EOpNegative:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001787 switch (basicType) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001788 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1789 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1790 default:
1791 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1792 return 0;
1793 }
1794 break;
1795 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001796 switch (basicType) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001797 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1798 default:
1799 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1800 return 0;
1801 }
1802 break;
1803 case EOpBitwiseNot:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001804 switch(basicType) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001805 case EbtInt: tempConstArray[i].setIConst(~unionArray[i].getIConst()); break;
1806 case EbtUInt: tempConstArray[i].setUConst(~unionArray[i].getUConst()); break;
1807 default:
1808 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1809 return 0;
1810 }
1811 break;
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001812 case EOpRadians:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001813 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001814 case EbtFloat: tempConstArray[i].setFConst(unionArray[i].getFConst() * 1.74532925e-2f); break;
1815 default:
1816 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1817 return 0;
1818 }
1819 break;
1820 case EOpDegrees:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001821 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001822 case EbtFloat: tempConstArray[i].setFConst(unionArray[i].getFConst() * 5.72957795e+1f); break;
1823 default:
1824 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1825 return 0;
1826 }
1827 break;
1828 case EOpSin:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001829 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001830 case EbtFloat: tempConstArray[i].setFConst(sinf(unionArray[i].getFConst())); break;
1831 default:
1832 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1833 return 0;
1834 }
1835 break;
1836 case EOpCos:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001837 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001838 case EbtFloat: tempConstArray[i].setFConst(cosf(unionArray[i].getFConst())); break;
1839 default:
1840 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1841 return 0;
1842 }
1843 break;
1844 case EOpTan:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001845 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001846 case EbtFloat: tempConstArray[i].setFConst(tanf(unionArray[i].getFConst())); break;
1847 default:
1848 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1849 return 0;
1850 }
1851 break;
1852 case EOpAsin:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001853 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001854 case EbtFloat: tempConstArray[i].setFConst(asinf(unionArray[i].getFConst())); break;
1855 default:
1856 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1857 return 0;
1858 }
1859 break;
1860 case EOpAcos:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001861 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001862 case EbtFloat: tempConstArray[i].setFConst(acosf(unionArray[i].getFConst())); break;
1863 default:
1864 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1865 return 0;
1866 }
1867 break;
1868 case EOpAtan:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001869 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001870 case EbtFloat: tempConstArray[i].setFConst(atanf(unionArray[i].getFConst())); break;
1871 default:
1872 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1873 return 0;
1874 }
1875 break;
1876 case EOpSinh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001877 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001878 case EbtFloat: tempConstArray[i].setFConst(sinhf(unionArray[i].getFConst())); break;
1879 default:
1880 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1881 return 0;
1882 }
1883 break;
1884 case EOpCosh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001885 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001886 case EbtFloat: tempConstArray[i].setFConst(coshf(unionArray[i].getFConst())); break;
1887 default:
1888 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1889 return 0;
1890 }
1891 break;
1892 case EOpTanh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001893 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001894 case EbtFloat: tempConstArray[i].setFConst(tanhf(unionArray[i].getFConst())); break;
1895 default:
1896 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1897 return 0;
1898 }
1899 break;
1900 case EOpAsinh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001901 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001902 case EbtFloat: tempConstArray[i].setFConst(asinhf(unionArray[i].getFConst())); break;
1903 default:
1904 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1905 return 0;
1906 }
1907 break;
1908 case EOpAcosh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001909 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001910 case EbtFloat: tempConstArray[i].setFConst(acoshf(unionArray[i].getFConst())); break;
1911 default:
1912 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1913 return 0;
1914 }
1915 break;
1916 case EOpAtanh:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001917 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001918 case EbtFloat: tempConstArray[i].setFConst(atanhf(unionArray[i].getFConst())); break;
1919 default:
1920 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1921 return 0;
1922 }
1923 break;
1924 case EOpLog:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001925 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001926 case EbtFloat: tempConstArray[i].setFConst(logf(unionArray[i].getFConst())); break;
1927 default:
1928 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1929 return 0;
1930 }
1931 break;
1932 case EOpLog2:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001933 switch(basicType) {
Alexis Hetu2ce222c2016-05-02 11:20:52 -04001934 case EbtFloat: tempConstArray[i].setFConst(sw::log2(unionArray[i].getFConst())); break;
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001935 default:
1936 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1937 return 0;
1938 }
1939 break;
1940 case EOpExp:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001941 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001942 case EbtFloat: tempConstArray[i].setFConst(expf(unionArray[i].getFConst())); break;
1943 default:
1944 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1945 return 0;
1946 }
1947 break;
1948 case EOpExp2:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001949 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001950 case EbtFloat: tempConstArray[i].setFConst(exp2f(unionArray[i].getFConst())); break;
1951 default:
1952 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1953 return 0;
1954 }
1955 break;
1956 case EOpSqrt:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001957 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001958 case EbtFloat: tempConstArray[i].setFConst(sqrtf(unionArray[i].getFConst())); break;
1959 default:
1960 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1961 return 0;
1962 }
1963 break;
1964 case EOpInverseSqrt:
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001965 switch(basicType) {
Alexis Hetu6c6b4702016-04-26 16:30:22 -04001966 case EbtFloat: tempConstArray[i].setFConst(1.0f / sqrtf(unionArray[i].getFConst())); break;
1967 default:
1968 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1969 return 0;
1970 }
1971 break;
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001972 case EOpFloatBitsToInt:
1973 switch(basicType) {
1974 case EbtFloat:
Nicolas Capensd55d9972018-10-18 13:47:29 -04001975 tempConstArray[i].setIConst(sw::bit_cast<int>(unionArray[i].getFConst()));
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001976 type.setBasicType(EbtInt);
1977 break;
1978 default:
1979 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1980 return 0;
1981 }
1982 break;
1983 break;
1984 case EOpFloatBitsToUint:
1985 switch(basicType) {
1986 case EbtFloat:
Nicolas Capensd55d9972018-10-18 13:47:29 -04001987 tempConstArray[i].setUConst(sw::bit_cast<unsigned int>(unionArray[i].getFConst()));
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001988 type.setBasicType(EbtUInt);
1989 break;
1990 default:
1991 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1992 return 0;
1993 }
1994 break;
1995 case EOpIntBitsToFloat:
1996 switch(basicType) {
1997 case EbtInt:
Nicolas Capensd55d9972018-10-18 13:47:29 -04001998 tempConstArray[i].setFConst(sw::bit_cast<float>(unionArray[i].getIConst()));
Alexis Hetu40f48ab2018-01-24 15:58:05 -05001999 type.setBasicType(EbtFloat);
2000 break;
2001 default:
2002 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
2003 return 0;
2004 }
2005 break;
2006 case EOpUintBitsToFloat:
2007 switch(basicType) {
2008 case EbtUInt:
Nicolas Capensd55d9972018-10-18 13:47:29 -04002009 tempConstArray[i].setFConst(sw::bit_cast<float>(unionArray[i].getUConst()));
Alexis Hetu40f48ab2018-01-24 15:58:05 -05002010 type.setBasicType(EbtFloat);
2011 break;
2012 default:
2013 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
2014 return 0;
2015 }
2016 break;
Alexis Hetu6c6b4702016-04-26 16:30:22 -04002017 default:
Nicolas Capens0bac2852016-05-07 06:09:58 -04002018 return 0;
2019 }
2020 }
Alexis Hetu40f48ab2018-01-24 15:58:05 -05002021 newNode = new TIntermConstantUnion(tempConstArray, type);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002022 newNode->setLine(getLine());
2023 return newNode;
2024 }
John Bauman66b8ab22014-05-06 15:57:45 -04002025}
2026
2027TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
2028{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002029 size_t size = node->getType().getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002030
Nicolas Capens0bac2852016-05-07 06:09:58 -04002031 ConstantUnion *leftUnionArray = new ConstantUnion[size];
John Bauman66b8ab22014-05-06 15:57:45 -04002032
Nicolas Capens0bac2852016-05-07 06:09:58 -04002033 for(size_t i = 0; i < size; i++) {
2034 switch (promoteTo) {
2035 case EbtFloat:
2036 switch (node->getType().getBasicType()) {
2037 case EbtInt:
2038 leftUnionArray[i].setFConst(static_cast<float>(node->getIConst(i)));
2039 break;
2040 case EbtUInt:
2041 leftUnionArray[i].setFConst(static_cast<float>(node->getUConst(i)));
2042 break;
2043 case EbtBool:
2044 leftUnionArray[i].setFConst(static_cast<float>(node->getBConst(i)));
2045 break;
2046 case EbtFloat:
2047 leftUnionArray[i].setFConst(static_cast<float>(node->getFConst(i)));
2048 break;
2049 default:
2050 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
2051 return 0;
2052 }
2053 break;
2054 case EbtInt:
2055 switch (node->getType().getBasicType()) {
2056 case EbtInt:
2057 leftUnionArray[i].setIConst(static_cast<int>(node->getIConst(i)));
2058 break;
2059 case EbtUInt:
2060 leftUnionArray[i].setIConst(static_cast<int>(node->getUConst(i)));
2061 break;
2062 case EbtBool:
2063 leftUnionArray[i].setIConst(static_cast<int>(node->getBConst(i)));
2064 break;
2065 case EbtFloat:
2066 leftUnionArray[i].setIConst(static_cast<int>(node->getFConst(i)));
2067 break;
2068 default:
2069 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
2070 return 0;
2071 }
2072 break;
2073 case EbtUInt:
2074 switch (node->getType().getBasicType()) {
2075 case EbtInt:
2076 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getIConst(i)));
2077 break;
2078 case EbtUInt:
2079 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getUConst(i)));
2080 break;
2081 case EbtBool:
2082 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getBConst(i)));
2083 break;
2084 case EbtFloat:
2085 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getFConst(i)));
2086 break;
2087 default:
2088 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
2089 return 0;
2090 }
2091 break;
2092 case EbtBool:
2093 switch (node->getType().getBasicType()) {
2094 case EbtInt:
2095 leftUnionArray[i].setBConst(node->getIConst(i) != 0);
2096 break;
2097 case EbtUInt:
2098 leftUnionArray[i].setBConst(node->getUConst(i) != 0);
2099 break;
2100 case EbtBool:
2101 leftUnionArray[i].setBConst(node->getBConst(i));
2102 break;
2103 case EbtFloat:
2104 leftUnionArray[i].setBConst(node->getFConst(i) != 0.0f);
2105 break;
2106 default:
2107 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
2108 return 0;
2109 }
John Bauman66b8ab22014-05-06 15:57:45 -04002110
Nicolas Capens0bac2852016-05-07 06:09:58 -04002111 break;
2112 default:
2113 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
2114 return 0;
2115 }
John Bauman66b8ab22014-05-06 15:57:45 -04002116
Nicolas Capens0bac2852016-05-07 06:09:58 -04002117 }
John Bauman66b8ab22014-05-06 15:57:45 -04002118
Nicolas Capens0bac2852016-05-07 06:09:58 -04002119 const TType& t = node->getType();
John Bauman66b8ab22014-05-06 15:57:45 -04002120
Alexis Hetub14178b2015-04-13 13:23:20 -04002121 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.getSecondarySize(), t.isArray()), node->getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04002122}
2123