1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/sksl/SkSLOperator.h"
9
10 #include "include/core/SkTypes.h"
11 #include "src/base/SkStringView.h"
12 #include "src/sksl/SkSLBuiltinTypes.h"
13 #include "src/sksl/SkSLContext.h"
14 #include "src/sksl/SkSLProgramSettings.h"
15 #include "src/sksl/ir/SkSLType.h"
16
17 #include <memory>
18
19 namespace SkSL {
20
getBinaryPrecedence() const21 OperatorPrecedence Operator::getBinaryPrecedence() const {
22 switch (this->kind()) {
23 case Kind::STAR: // fall through
24 case Kind::SLASH: // fall through
25 case Kind::PERCENT: return OperatorPrecedence::kMultiplicative;
26 case Kind::PLUS: // fall through
27 case Kind::MINUS: return OperatorPrecedence::kAdditive;
28 case Kind::SHL: // fall through
29 case Kind::SHR: return OperatorPrecedence::kShift;
30 case Kind::LT: // fall through
31 case Kind::GT: // fall through
32 case Kind::LTEQ: // fall through
33 case Kind::GTEQ: return OperatorPrecedence::kRelational;
34 case Kind::EQEQ: // fall through
35 case Kind::NEQ: return OperatorPrecedence::kEquality;
36 case Kind::BITWISEAND: return OperatorPrecedence::kBitwiseAnd;
37 case Kind::BITWISEXOR: return OperatorPrecedence::kBitwiseXor;
38 case Kind::BITWISEOR: return OperatorPrecedence::kBitwiseOr;
39 case Kind::LOGICALAND: return OperatorPrecedence::kLogicalAnd;
40 case Kind::LOGICALXOR: return OperatorPrecedence::kLogicalXor;
41 case Kind::LOGICALOR: return OperatorPrecedence::kLogicalOr;
42 case Kind::EQ: // fall through
43 case Kind::PLUSEQ: // fall through
44 case Kind::MINUSEQ: // fall through
45 case Kind::STAREQ: // fall through
46 case Kind::SLASHEQ: // fall through
47 case Kind::PERCENTEQ: // fall through
48 case Kind::SHLEQ: // fall through
49 case Kind::SHREQ: // fall through
50 case Kind::BITWISEANDEQ: // fall through
51 case Kind::BITWISEXOREQ: // fall through
52 case Kind::BITWISEOREQ: return OperatorPrecedence::kAssignment;
53 case Kind::COMMA: return OperatorPrecedence::kSequence;
54 default: SK_ABORT("unsupported binary operator");
55 }
56 }
57
operatorName() const58 const char* Operator::operatorName() const {
59 switch (this->kind()) {
60 case Kind::PLUS: return " + ";
61 case Kind::MINUS: return " - ";
62 case Kind::STAR: return " * ";
63 case Kind::SLASH: return " / ";
64 case Kind::PERCENT: return " % ";
65 case Kind::SHL: return " << ";
66 case Kind::SHR: return " >> ";
67 case Kind::LOGICALNOT: return "!";
68 case Kind::LOGICALAND: return " && ";
69 case Kind::LOGICALOR: return " || ";
70 case Kind::LOGICALXOR: return " ^^ ";
71 case Kind::BITWISENOT: return "~";
72 case Kind::BITWISEAND: return " & ";
73 case Kind::BITWISEOR: return " | ";
74 case Kind::BITWISEXOR: return " ^ ";
75 case Kind::EQ: return " = ";
76 case Kind::EQEQ: return " == ";
77 case Kind::NEQ: return " != ";
78 case Kind::LT: return " < ";
79 case Kind::GT: return " > ";
80 case Kind::LTEQ: return " <= ";
81 case Kind::GTEQ: return " >= ";
82 case Kind::PLUSEQ: return " += ";
83 case Kind::MINUSEQ: return " -= ";
84 case Kind::STAREQ: return " *= ";
85 case Kind::SLASHEQ: return " /= ";
86 case Kind::PERCENTEQ: return " %= ";
87 case Kind::SHLEQ: return " <<= ";
88 case Kind::SHREQ: return " >>= ";
89 case Kind::BITWISEANDEQ: return " &= ";
90 case Kind::BITWISEOREQ: return " |= ";
91 case Kind::BITWISEXOREQ: return " ^= ";
92 case Kind::PLUSPLUS: return "++";
93 case Kind::MINUSMINUS: return "--";
94 case Kind::COMMA: return ", ";
95 default: SkUNREACHABLE;
96 }
97 }
98
tightOperatorName() const99 std::string_view Operator::tightOperatorName() const {
100 std::string_view name = this->operatorName();
101 if (skstd::starts_with(name, ' ')) {
102 name.remove_prefix(1);
103 }
104 if (skstd::ends_with(name, ' ')) {
105 name.remove_suffix(1);
106 }
107 return name;
108 }
109
isAssignment() const110 bool Operator::isAssignment() const {
111 switch (this->kind()) {
112 case Kind::EQ: // fall through
113 case Kind::PLUSEQ: // fall through
114 case Kind::MINUSEQ: // fall through
115 case Kind::STAREQ: // fall through
116 case Kind::SLASHEQ: // fall through
117 case Kind::PERCENTEQ: // fall through
118 case Kind::SHLEQ: // fall through
119 case Kind::SHREQ: // fall through
120 case Kind::BITWISEOREQ: // fall through
121 case Kind::BITWISEXOREQ: // fall through
122 case Kind::BITWISEANDEQ:
123 return true;
124 default:
125 return false;
126 }
127 }
128
removeAssignment() const129 Operator Operator::removeAssignment() const {
130 switch (this->kind()) {
131 case Kind::PLUSEQ: return Kind::PLUS;
132 case Kind::MINUSEQ: return Kind::MINUS;
133 case Kind::STAREQ: return Kind::STAR;
134 case Kind::SLASHEQ: return Kind::SLASH;
135 case Kind::PERCENTEQ: return Kind::PERCENT;
136 case Kind::SHLEQ: return Kind::SHL;
137 case Kind::SHREQ: return Kind::SHR;
138 case Kind::BITWISEOREQ: return Kind::BITWISEOR;
139 case Kind::BITWISEXOREQ: return Kind::BITWISEXOR;
140 case Kind::BITWISEANDEQ: return Kind::BITWISEAND;
141 default: return *this;
142 }
143 }
144
isRelational() const145 bool Operator::isRelational() const {
146 switch (this->kind()) {
147 case Kind::LT:
148 case Kind::GT:
149 case Kind::LTEQ:
150 case Kind::GTEQ:
151 return true;
152 default:
153 return false;
154 }
155 }
156
isOnlyValidForIntegralTypes() const157 bool Operator::isOnlyValidForIntegralTypes() const {
158 switch (this->kind()) {
159 case Kind::SHL:
160 case Kind::SHR:
161 case Kind::BITWISEAND:
162 case Kind::BITWISEOR:
163 case Kind::BITWISEXOR:
164 case Kind::PERCENT:
165 case Kind::SHLEQ:
166 case Kind::SHREQ:
167 case Kind::BITWISEANDEQ:
168 case Kind::BITWISEOREQ:
169 case Kind::BITWISEXOREQ:
170 case Kind::PERCENTEQ:
171 return true;
172 default:
173 return false;
174 }
175 }
176
isValidForMatrixOrVector() const177 bool Operator::isValidForMatrixOrVector() const {
178 switch (this->kind()) {
179 case Kind::PLUS:
180 case Kind::MINUS:
181 case Kind::STAR:
182 case Kind::SLASH:
183 case Kind::PERCENT:
184 case Kind::SHL:
185 case Kind::SHR:
186 case Kind::BITWISEAND:
187 case Kind::BITWISEOR:
188 case Kind::BITWISEXOR:
189 case Kind::PLUSEQ:
190 case Kind::MINUSEQ:
191 case Kind::STAREQ:
192 case Kind::SLASHEQ:
193 case Kind::PERCENTEQ:
194 case Kind::SHLEQ:
195 case Kind::SHREQ:
196 case Kind::BITWISEANDEQ:
197 case Kind::BITWISEOREQ:
198 case Kind::BITWISEXOREQ:
199 return true;
200 default:
201 return false;
202 }
203 }
204
isMatrixMultiply(const Type & left,const Type & right) const205 bool Operator::isMatrixMultiply(const Type& left, const Type& right) const {
206 if (this->kind() != Kind::STAR && this->kind() != Kind::STAREQ) {
207 return false;
208 }
209 if (left.isMatrix()) {
210 return right.isMatrix() || right.isVector();
211 }
212 return left.isVector() && right.isMatrix();
213 }
214
215 /**
216 * Determines the operand and result types of a binary expression. Returns true if the expression is
217 * legal, false otherwise. If false, the values of the out parameters are undefined.
218 */
determineBinaryType(const Context & context,const Type & left,const Type & right,const Type ** outLeftType,const Type ** outRightType,const Type ** outResultType) const219 bool Operator::determineBinaryType(const Context& context,
220 const Type& left,
221 const Type& right,
222 const Type** outLeftType,
223 const Type** outRightType,
224 const Type** outResultType) const {
225 const bool allowNarrowing = context.fConfig->fSettings.fAllowNarrowingConversions;
226 switch (this->kind()) {
227 case Kind::EQ: // left = right
228 if (left.isVoid()) {
229 return false;
230 }
231 *outLeftType = &left;
232 *outRightType = &left;
233 *outResultType = &left;
234 return right.canCoerceTo(left, allowNarrowing);
235
236 case Kind::EQEQ: // left == right
237 case Kind::NEQ: { // left != right
238 if (left.isVoid() || left.isOpaque()) {
239 return false;
240 }
241 CoercionCost rightToLeft = right.coercionCost(left),
242 leftToRight = left.coercionCost(right);
243 if (rightToLeft < leftToRight) {
244 if (rightToLeft.isPossible(allowNarrowing)) {
245 *outLeftType = &left;
246 *outRightType = &left;
247 *outResultType = context.fTypes.fBool.get();
248 return true;
249 }
250 } else {
251 if (leftToRight.isPossible(allowNarrowing)) {
252 *outLeftType = &right;
253 *outRightType = &right;
254 *outResultType = context.fTypes.fBool.get();
255 return true;
256 }
257 }
258 return false;
259 }
260 case Kind::LOGICALOR: // left || right
261 case Kind::LOGICALAND: // left && right
262 case Kind::LOGICALXOR: // left ^^ right
263 *outLeftType = context.fTypes.fBool.get();
264 *outRightType = context.fTypes.fBool.get();
265 *outResultType = context.fTypes.fBool.get();
266 return left.canCoerceTo(*context.fTypes.fBool, allowNarrowing) &&
267 right.canCoerceTo(*context.fTypes.fBool, allowNarrowing);
268
269 case Operator::Kind::COMMA: // left, right
270 if (left.isOpaque() || right.isOpaque()) {
271 return false;
272 }
273 *outLeftType = &left;
274 *outRightType = &right;
275 *outResultType = &right;
276 return true;
277
278 default:
279 break;
280 }
281
282 // Boolean types only support the operators listed above (, = == != || && ^^).
283 // If we've gotten this far with a boolean, we have an unsupported operator.
284 const Type& leftComponentType = left.componentType();
285 const Type& rightComponentType = right.componentType();
286 if (leftComponentType.isBoolean() || rightComponentType.isBoolean()) {
287 return false;
288 }
289
290 bool isAssignment = this->isAssignment();
291 if (this->isMatrixMultiply(left, right)) { // left * right
292 // Determine final component type.
293 if (!this->determineBinaryType(context, left.componentType(), right.componentType(),
294 outLeftType, outRightType, outResultType)) {
295 return false;
296 }
297 // Convert component type to compound.
298 *outLeftType = &(*outResultType)->toCompound(context, left.columns(), left.rows());
299 *outRightType = &(*outResultType)->toCompound(context, right.columns(), right.rows());
300 int leftColumns = left.columns(), leftRows = left.rows();
301 int rightColumns = right.columns(), rightRows = right.rows();
302 if (right.isVector()) {
303 // `matrix * vector` treats the vector as a column vector; we need to transpose it.
304 std::swap(rightColumns, rightRows);
305 SkASSERT(rightColumns == 1);
306 }
307 if (rightColumns > 1) {
308 *outResultType = &(*outResultType)->toCompound(context, rightColumns, leftRows);
309 } else {
310 // The result was a column vector. Transpose it back to a row.
311 *outResultType = &(*outResultType)->toCompound(context, leftRows, rightColumns);
312 }
313 if (isAssignment && ((*outResultType)->columns() != leftColumns ||
314 (*outResultType)->rows() != leftRows)) {
315 return false;
316 }
317 return leftColumns == rightRows;
318 }
319
320 bool leftIsVectorOrMatrix = left.isVector() || left.isMatrix();
321 bool validMatrixOrVectorOp = this->isValidForMatrixOrVector();
322
323 if (leftIsVectorOrMatrix && validMatrixOrVectorOp && right.isScalar()) {
324 // Determine final component type.
325 if (!this->determineBinaryType(context, left.componentType(), right,
326 outLeftType, outRightType, outResultType)) {
327 return false;
328 }
329 // Convert component type to compound.
330 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
331 if (!this->isRelational()) {
332 *outResultType = &(*outResultType)->toCompound(context, left.columns(), left.rows());
333 }
334 return true;
335 }
336
337 bool rightIsVectorOrMatrix = right.isVector() || right.isMatrix();
338
339 if (!isAssignment && rightIsVectorOrMatrix && validMatrixOrVectorOp && left.isScalar()) {
340 // Determine final component type.
341 if (!this->determineBinaryType(context, left, right.componentType(),
342 outLeftType, outRightType, outResultType)) {
343 return false;
344 }
345 // Convert component type to compound.
346 *outRightType = &(*outRightType)->toCompound(context, right.columns(), right.rows());
347 if (!this->isRelational()) {
348 *outResultType = &(*outResultType)->toCompound(context, right.columns(), right.rows());
349 }
350 return true;
351 }
352
353 CoercionCost rightToLeftCost = right.coercionCost(left);
354 CoercionCost leftToRightCost = isAssignment ? CoercionCost::Impossible()
355 : left.coercionCost(right);
356
357 if ((left.isScalar() && right.isScalar()) || (leftIsVectorOrMatrix && validMatrixOrVectorOp)) {
358 if (this->isOnlyValidForIntegralTypes()) {
359 if (!leftComponentType.isInteger() || !rightComponentType.isInteger()) {
360 return false;
361 }
362 }
363 if (rightToLeftCost.isPossible(allowNarrowing) && rightToLeftCost < leftToRightCost) {
364 // Right-to-Left conversion is possible and cheaper
365 *outLeftType = &left;
366 *outRightType = &left;
367 *outResultType = &left;
368 } else if (leftToRightCost.isPossible(allowNarrowing)) {
369 // Left-to-Right conversion is possible (and at least as cheap as Right-to-Left)
370 *outLeftType = &right;
371 *outRightType = &right;
372 *outResultType = &right;
373 } else {
374 return false;
375 }
376 if (this->isRelational()) {
377 *outResultType = context.fTypes.fBool.get();
378 }
379 return true;
380 }
381 return false;
382 }
383
384 } // namespace SkSL
385