Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "src/sksl/SkSLAnalysis.h" |
| 9 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 10 | #include "include/private/SkSLSampleUsage.h" |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 11 | #include "src/sksl/SkSLErrorReporter.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLExpression.h" |
| 13 | #include "src/sksl/ir/SkSLProgram.h" |
| 14 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 15 | #include "src/sksl/ir/SkSLStatement.h" |
| 16 | |
| 17 | // ProgramElements |
| 18 | #include "src/sksl/ir/SkSLEnum.h" |
| 19 | #include "src/sksl/ir/SkSLExtension.h" |
| 20 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 21 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 22 | #include "src/sksl/ir/SkSLModifiers.h" |
| 23 | #include "src/sksl/ir/SkSLSection.h" |
| 24 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 25 | |
| 26 | // Statements |
| 27 | #include "src/sksl/ir/SkSLBlock.h" |
| 28 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 29 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 30 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 32 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 33 | #include "src/sksl/ir/SkSLForStatement.h" |
| 34 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 35 | #include "src/sksl/ir/SkSLNop.h" |
| 36 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 37 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 38 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 39 | |
| 40 | // Expressions |
| 41 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 42 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 43 | #include "src/sksl/ir/SkSLConstructor.h" |
| 44 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
| 45 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
| 46 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 47 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 48 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 49 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 50 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 51 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 52 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 53 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 54 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 55 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 56 | #include "src/sksl/ir/SkSLSetting.h" |
| 57 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 58 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 59 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 60 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 61 | |
| 62 | namespace SkSL { |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | static bool is_sample_call_to_fp(const FunctionCall& fc, const Variable& fp) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 67 | const FunctionDeclaration& f = fc.function(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 68 | return f.isBuiltin() && f.name() == "sample" && fc.arguments().size() >= 1 && |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 69 | fc.arguments()[0]->is<VariableReference>() && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 70 | fc.arguments()[0]->as<VariableReference>().variable() == &fp; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 73 | // Visitor that determines the merged SampleUsage for a given child 'fp' in the program. |
| 74 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 75 | public: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 76 | MergeSampleUsageVisitor(const Context& context, const Variable& fp) |
| 77 | : fContext(context), fFP(fp) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 78 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 79 | SampleUsage visit(const Program& program) { |
| 80 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 81 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 82 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 86 | const Context& fContext; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 87 | const Variable& fFP; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 88 | SampleUsage fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 89 | |
| 90 | bool visitExpression(const Expression& e) override { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 91 | // Looking for sample(fp, inColor?, ...) |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 92 | if (e.kind() == Expression::Kind::kFunctionCall) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 93 | const FunctionCall& fc = e.as<FunctionCall>(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 94 | if (is_sample_call_to_fp(fc, fFP)) { |
| 95 | // Determine the type of call at this site, and merge it with the accumulated state |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 96 | const Expression* lastArg = fc.arguments().back().get(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 97 | |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 98 | if (lastArg->type() == *fContext.fFloat2_Type) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 99 | fUsage.merge(SampleUsage::Explicit()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 100 | } else if (lastArg->type() == *fContext.fFloat3x3_Type) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 101 | // Determine the type of matrix for this call site |
| 102 | if (lastArg->isConstantOrUniform()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 103 | if (lastArg->kind() == Expression::Kind::kVariableReference || |
| 104 | lastArg->kind() == Expression::Kind::kConstructor) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 105 | // FIXME if this is a constant, we should parse the float3x3 constructor |
| 106 | // and determine if the resulting matrix introduces perspective. |
| 107 | fUsage.merge(SampleUsage::UniformMatrix(lastArg->description())); |
| 108 | } else { |
| 109 | // FIXME this is really to workaround a restriction of the downstream |
| 110 | // code that relies on the SampleUsage's fExpression to identify uniform |
| 111 | // names. Once they are tracked separately, any uniform expression can |
| 112 | // work, but right now this avoids issues from '0.5 * matrix' that is |
| 113 | // both a constant AND a uniform. |
| 114 | fUsage.merge(SampleUsage::VariableMatrix()); |
| 115 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 116 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 117 | fUsage.merge(SampleUsage::VariableMatrix()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 118 | } |
| 119 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 120 | // The only other signatures do pass-through sampling |
| 121 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 122 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 123 | // NOTE: we don't return true here just because we found a sample call. We need to |
| 124 | // process the entire program and merge across all encountered calls. |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 128 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 129 | } |
| 130 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 131 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 134 | // Visitor that searches through the program for references to a particular builtin variable |
| 135 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 136 | public: |
| 137 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 138 | |
| 139 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 140 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 141 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 142 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 143 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 144 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 147 | int fBuiltin; |
| 148 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 149 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 150 | }; |
| 151 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 152 | // Visitor that counts the number of nodes visited |
| 153 | class NodeCountVisitor : public ProgramVisitor { |
| 154 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 155 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 156 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 157 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 158 | this->visitStatement(s); |
| 159 | return fCount; |
| 160 | } |
| 161 | |
| 162 | bool visitExpression(const Expression& e) override { |
| 163 | ++fCount; |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 164 | return (fCount > fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool visitProgramElement(const ProgramElement& p) override { |
| 168 | ++fCount; |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 169 | return (fCount > fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool visitStatement(const Statement& s) override { |
| 173 | ++fCount; |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 174 | return (fCount > fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 178 | int fCount = 0; |
| 179 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 180 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 181 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 182 | }; |
| 183 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 184 | class VariableWriteVisitor : public ProgramVisitor { |
| 185 | public: |
| 186 | VariableWriteVisitor(const Variable* var) |
| 187 | : fVar(var) {} |
| 188 | |
| 189 | bool visit(const Statement& s) { |
| 190 | return this->visitStatement(s); |
| 191 | } |
| 192 | |
| 193 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 194 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 195 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 196 | if (ref.variable() == fVar && |
| 197 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 198 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 199 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 203 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | private: |
| 207 | const Variable* fVar; |
| 208 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 209 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 210 | }; |
| 211 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 212 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 213 | class TrivialErrorReporter : public ErrorReporter { |
| 214 | public: |
| 215 | void error(int offset, String) override { ++fErrorCount; } |
| 216 | int errorCount() override { return fErrorCount; } |
| 217 | |
| 218 | private: |
| 219 | int fErrorCount = 0; |
| 220 | }; |
| 221 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 222 | // This isn't actually using ProgramVisitor, because it only considers a subset of the fields for |
| 223 | // any given expression kind. For instance, when indexing an array (e.g. `x[1]`), we only want to |
| 224 | // know if the base (`x`) is assignable; the index expression (`1`) doesn't need to be. |
| 225 | class IsAssignableVisitor { |
| 226 | public: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 227 | IsAssignableVisitor(VariableReference** assignableVar, ErrorReporter* errors) |
| 228 | : fAssignableVar(assignableVar), fErrors(errors) { |
| 229 | if (fAssignableVar) { |
| 230 | *fAssignableVar = nullptr; |
| 231 | } |
| 232 | } |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 233 | |
| 234 | bool visit(Expression& expr) { |
| 235 | this->visitExpression(expr); |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 236 | return fErrors->errorCount() == 0; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void visitExpression(Expression& expr) { |
| 240 | switch (expr.kind()) { |
| 241 | case Expression::Kind::kVariableReference: { |
| 242 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 243 | const Variable* var = varRef.variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 244 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag | |
| 245 | Modifiers::kVarying_Flag)) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 246 | fErrors->error(expr.fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 247 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 248 | } else if (fAssignableVar) { |
| 249 | SkASSERT(*fAssignableVar == nullptr); |
| 250 | *fAssignableVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 251 | } |
| 252 | break; |
| 253 | } |
| 254 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 255 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case Expression::Kind::kSwizzle: { |
| 259 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 260 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame^] | 261 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | case Expression::Kind::kIndex: |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 265 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 266 | break; |
| 267 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 268 | case Expression::Kind::kExternalValue: { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 269 | const ExternalValue& var = expr.as<ExternalValueReference>().value(); |
| 270 | if (!var.canWrite()) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 271 | fErrors->error(expr.fOffset, |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 272 | "cannot modify immutable external value '" + var.name() + "'"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 273 | } |
| 274 | break; |
| 275 | } |
| 276 | default: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 277 | fErrors->error(expr.fOffset, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 284 | int bits = 0; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame^] | 285 | for (int idx : swizzle.components()) { |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 286 | SkASSERT(idx <= 3); |
| 287 | int bit = 1 << idx; |
| 288 | if (bits & bit) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 289 | fErrors->error(swizzle.fOffset, |
| 290 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | bits |= bit; |
| 294 | } |
| 295 | } |
| 296 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 297 | VariableReference** fAssignableVar; |
| 298 | ErrorReporter* fErrors; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 299 | |
| 300 | using INHERITED = ProgramVisitor; |
| 301 | }; |
| 302 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 303 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 304 | |
| 305 | //////////////////////////////////////////////////////////////////////////////// |
| 306 | // Analysis |
| 307 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 308 | SampleUsage Analysis::GetSampleUsage(const Program& program, const Variable& fp) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 309 | MergeSampleUsageVisitor visitor(*program.fContext, fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 310 | return visitor.visit(program); |
| 311 | } |
| 312 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 313 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 314 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 315 | return visitor.visit(program); |
| 316 | } |
| 317 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 318 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 319 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 320 | } |
| 321 | |
| 322 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 323 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 324 | } |
| 325 | |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 326 | bool Analysis::NodeCountExceeds(const FunctionDefinition& function, int limit) { |
| 327 | return NodeCountVisitor{limit}.visit(*function.fBody) > limit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 330 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 331 | return VariableWriteVisitor(&var).visit(stmt); |
| 332 | } |
| 333 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 334 | bool Analysis::IsAssignable(Expression& expr, VariableReference** assignableVar, |
| 335 | ErrorReporter* errors) { |
| 336 | TrivialErrorReporter trivialErrors; |
| 337 | return IsAssignableVisitor{assignableVar, errors ? errors : &trivialErrors}.visit(expr); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 340 | //////////////////////////////////////////////////////////////////////////////// |
| 341 | // ProgramVisitor |
| 342 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 343 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 344 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visit(PROG program) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 345 | for (const auto& pe : program.elements()) { |
| 346 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 347 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 348 | } |
| 349 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 350 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 351 | } |
| 352 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 353 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 354 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitExpression(EXPR e) { |
| 355 | switch (e.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 356 | case Expression::Kind::kBoolLiteral: |
| 357 | case Expression::Kind::kDefined: |
| 358 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 359 | case Expression::Kind::kFloatLiteral: |
| 360 | case Expression::Kind::kFunctionReference: |
| 361 | case Expression::Kind::kIntLiteral: |
| 362 | case Expression::Kind::kNullLiteral: |
| 363 | case Expression::Kind::kSetting: |
| 364 | case Expression::Kind::kTypeReference: |
| 365 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 366 | // Leaf expressions return false |
| 367 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 368 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 369 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 370 | auto& b = e.template as<BinaryExpression>(); |
| 371 | return this->visitExpression(b.left()) || this->visitExpression(b.right()); |
| 372 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 373 | case Expression::Kind::kConstructor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 374 | auto& c = e.template as<Constructor>(); |
| 375 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 376 | if (this->visitExpression(*arg)) { return true; } |
| 377 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 378 | return false; |
| 379 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 380 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 381 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 382 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 383 | if (this->visitExpression(*arg)) { return true; } |
| 384 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 385 | return false; |
| 386 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 387 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 388 | return this->visitExpression(*e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 389 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 390 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 391 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 392 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 393 | if (this->visitExpression(*arg)) { return true; } |
| 394 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 395 | return false; |
| 396 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 397 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 398 | auto& i = e.template as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 399 | return this->visitExpression(*i.base()) || this->visitExpression(*i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 400 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 401 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 402 | return this->visitExpression(*e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 403 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 404 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 405 | return this->visitExpression(*e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 406 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 407 | case Expression::Kind::kSwizzle: |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame^] | 408 | return this->visitExpression(*e.template as<Swizzle>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 409 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 410 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 411 | auto& t = e.template as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 412 | return this->visitExpression(*t.test()) || this->visitExpression(*t.ifTrue()) || |
| 413 | this->visitExpression(*t.ifFalse()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 414 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 415 | default: |
| 416 | SkUNREACHABLE; |
| 417 | } |
| 418 | } |
| 419 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 420 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 421 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitStatement(STMT s) { |
| 422 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 423 | case Statement::Kind::kBreak: |
| 424 | case Statement::Kind::kContinue: |
| 425 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 426 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 427 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 428 | // Leaf statements just return false |
| 429 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 430 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 431 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 432 | for (auto& stmt : s.template as<Block>().children()) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 433 | if (this->visitStatement(*stmt)) { |
| 434 | return true; |
| 435 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 436 | } |
| 437 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 438 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 439 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 440 | auto& d = s.template as<DoStatement>(); |
| 441 | return this->visitExpression(*d.test()) || this->visitStatement(*d.statement()); |
| 442 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 443 | case Statement::Kind::kExpression: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 444 | return this->visitExpression(*s.template as<ExpressionStatement>().expression()); |
| 445 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 446 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 447 | auto& f = s.template as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 448 | return (f.initializer() && this->visitStatement(*f.initializer())) || |
| 449 | (f.test() && this->visitExpression(*f.test())) || |
| 450 | (f.next() && this->visitExpression(*f.next())) || |
| 451 | this->visitStatement(*f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 452 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 453 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 454 | auto& i = s.template as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 455 | return this->visitExpression(*i.test()) || |
| 456 | this->visitStatement(*i.ifTrue()) || |
| 457 | (i.ifFalse() && this->visitStatement(*i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 458 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 459 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 460 | auto& r = s.template as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 461 | return r.expression() && this->visitExpression(*r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 462 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 463 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 464 | auto& sw = s.template as<SwitchStatement>(); |
| 465 | if (this->visitExpression(*sw.fValue)) { |
| 466 | return true; |
| 467 | } |
| 468 | for (auto& c : sw.fCases) { |
| 469 | if (c->fValue && this->visitExpression(*c->fValue)) { |
| 470 | return true; |
| 471 | } |
| 472 | for (auto& st : c->fStatements) { |
| 473 | if (this->visitStatement(*st)) { |
| 474 | return true; |
| 475 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 476 | } |
| 477 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 478 | return false; |
| 479 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 480 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 481 | auto& v = s.template as<VarDeclaration>(); |
| 482 | for (auto& sizeExpr : v.fSizes) { |
| 483 | if (sizeExpr && this->visitExpression(*sizeExpr)) { |
| 484 | return true; |
| 485 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 486 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 487 | return v.fValue && this->visitExpression(*v.fValue); |
| 488 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 489 | case Statement::Kind::kWhile: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 490 | auto& w = s.template as<WhileStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 491 | return this->visitExpression(*w.test()) || this->visitStatement(*w.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 492 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 493 | default: |
| 494 | SkUNREACHABLE; |
| 495 | } |
| 496 | } |
| 497 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 498 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 499 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitProgramElement(ELEM pe) { |
| 500 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 501 | case ProgramElement::Kind::kEnum: |
| 502 | case ProgramElement::Kind::kExtension: |
| 503 | case ProgramElement::Kind::kModifiers: |
| 504 | case ProgramElement::Kind::kSection: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 505 | // Leaf program elements just return false by default |
| 506 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 507 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 508 | case ProgramElement::Kind::kFunction: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 509 | return this->visitStatement(*pe.template as<FunctionDefinition>().fBody); |
| 510 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 511 | case ProgramElement::Kind::kInterfaceBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 512 | for (auto& e : pe.template as<InterfaceBlock>().fSizes) { |
Ethan Nicholas | 825268f | 2020-10-02 16:26:00 -0400 | [diff] [blame] | 513 | if (e && this->visitExpression(*e)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 514 | return true; |
| 515 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 516 | } |
| 517 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 518 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 519 | case ProgramElement::Kind::kGlobalVar: |
| 520 | if (this->visitStatement(*pe.template as<GlobalVarDeclaration>().fDecl)) { |
| 521 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 522 | } |
| 523 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 524 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 525 | default: |
| 526 | SkUNREACHABLE; |
| 527 | } |
| 528 | } |
| 529 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 530 | template class TProgramVisitor<const Program&, const Expression&, |
| 531 | const Statement&, const ProgramElement&>; |
| 532 | template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 533 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 534 | } // namespace SkSL |