ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLIRGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
| 10 | #include "limits.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 11 | #include <iterator> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | #include <memory> |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 13 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 15 | #include "include/private/SkSLLayout.h" |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 16 | #include "include/private/SkTArray.h" |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 17 | #include "include/sksl/DSLCore.h" |
John Stiles | b2705a1 | 2021-02-19 10:55:14 -0500 | [diff] [blame] | 18 | #include "src/core/SkScopeExit.h" |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 19 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLConstantFolder.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 22 | #include "src/sksl/SkSLOperators.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLParser.h" |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 24 | #include "src/sksl/SkSLUtil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 26 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 27 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 28 | #include "src/sksl/ir/SkSLConstructor.h" |
| 29 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 30 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDoStatement.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 34 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/sksl/ir/SkSLField.h" |
| 36 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 37 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 38 | #include "src/sksl/ir/SkSLForStatement.h" |
| 39 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 40 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 41 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 44 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 45 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 46 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 47 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLNop.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 49 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 50 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 51 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 52 | #include "src/sksl/ir/SkSLSetting.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 53 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 54 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 55 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 56 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 57 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 58 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 59 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 60 | #include "src/sksl/ir/SkSLVariable.h" |
| 61 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 62 | |
| 63 | namespace SkSL { |
| 64 | |
| 65 | class AutoSymbolTable { |
| 66 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 67 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | : fIR(ir) |
| 69 | , fPrevious(fIR->fSymbolTable) { |
| 70 | fIR->pushSymbolTable(); |
| 71 | } |
| 72 | |
| 73 | ~AutoSymbolTable() { |
| 74 | fIR->popSymbolTable(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 75 | SkASSERT(fPrevious == fIR->fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | IRGenerator* fIR; |
| 79 | std::shared_ptr<SymbolTable> fPrevious; |
| 80 | }; |
| 81 | |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 82 | IRGenerator::IRGenerator(const Context* context) |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 83 | : fContext(*context) {} |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 84 | |
| 85 | void IRGenerator::pushSymbolTable() { |
| 86 | auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode); |
| 87 | fSymbolTable = std::move(childSymTable); |
| 88 | } |
| 89 | |
| 90 | void IRGenerator::popSymbolTable() { |
| 91 | fSymbolTable = fSymbolTable->fParent; |
| 92 | } |
| 93 | |
John Stiles | e3a91cf | 2021-01-26 10:13:58 -0500 | [diff] [blame] | 94 | bool IRGenerator::detectVarDeclarationWithoutScope(const Statement& stmt) { |
| 95 | // Parsing an AST node containing a single variable declaration creates a lone VarDeclaration |
| 96 | // statement. An AST with multiple variable declarations creates an unscoped Block containing |
| 97 | // multiple VarDeclaration statements. We need to detect either case. |
| 98 | const Variable* var; |
| 99 | if (stmt.is<VarDeclaration>()) { |
| 100 | // The single-variable case. No blocks at all. |
| 101 | var = &stmt.as<VarDeclaration>().var(); |
| 102 | } else if (stmt.is<Block>()) { |
| 103 | // The multiple-variable case: an unscoped, non-empty block... |
| 104 | const Block& block = stmt.as<Block>(); |
| 105 | if (block.isScope() || block.children().empty()) { |
| 106 | return false; |
| 107 | } |
| 108 | // ... holding a variable declaration. |
| 109 | const Statement& innerStmt = *block.children().front(); |
| 110 | if (!innerStmt.is<VarDeclaration>()) { |
| 111 | return false; |
| 112 | } |
| 113 | var = &innerStmt.as<VarDeclaration>().var(); |
| 114 | } else { |
| 115 | // This statement wasn't a variable declaration. No problem. |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Report an error. |
| 120 | SkASSERT(var); |
| 121 | this->errorReporter().error(stmt.fOffset, |
| 122 | "variable '" + var->name() + "' must be created in a scope"); |
| 123 | return true; |
| 124 | } |
| 125 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 126 | std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 127 | if (this->programKind() != ProgramKind::kFragment && |
| 128 | this->programKind() != ProgramKind::kVertex && |
| 129 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 130 | this->errorReporter().error(offset, "extensions are not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 131 | return nullptr; |
| 132 | } |
| 133 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 134 | return std::make_unique<Extension>(offset, name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | } |
| 136 | |
John Stiles | 20c09f9 | 2021-03-12 09:25:04 -0500 | [diff] [blame] | 137 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | switch (statement.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 139 | case ASTNode::Kind::kBlock: |
| 140 | return this->convertBlock(statement); |
| 141 | case ASTNode::Kind::kVarDeclarations: |
| 142 | return this->convertVarDeclarationStatement(statement); |
| 143 | case ASTNode::Kind::kIf: |
| 144 | return this->convertIf(statement); |
| 145 | case ASTNode::Kind::kFor: |
| 146 | return this->convertFor(statement); |
| 147 | case ASTNode::Kind::kWhile: |
| 148 | return this->convertWhile(statement); |
| 149 | case ASTNode::Kind::kDo: |
| 150 | return this->convertDo(statement); |
| 151 | case ASTNode::Kind::kSwitch: |
| 152 | return this->convertSwitch(statement); |
| 153 | case ASTNode::Kind::kReturn: |
| 154 | return this->convertReturn(statement); |
| 155 | case ASTNode::Kind::kBreak: |
| 156 | return this->convertBreak(statement); |
| 157 | case ASTNode::Kind::kContinue: |
| 158 | return this->convertContinue(statement); |
| 159 | case ASTNode::Kind::kDiscard: |
| 160 | return this->convertDiscard(statement); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 161 | case ASTNode::Kind::kType: |
| 162 | // TODO: add IRNode for struct definition inside a function |
| 163 | return nullptr; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 164 | default: |
| 165 | // it's an expression |
| 166 | std::unique_ptr<Statement> result = this->convertExpressionStatement(statement); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 167 | if (fRTAdjust && this->programKind() == ProgramKind::kGeometry) { |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 168 | SkASSERT(result->is<ExpressionStatement>()); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 169 | Expression& expr = *result->as<ExpressionStatement>().expression(); |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 170 | if (expr.is<FunctionCall>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 171 | FunctionCall& fc = expr.as<FunctionCall>(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 172 | if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 173 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 174 | statements.reserve_back(2); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 175 | statements.push_back(getNormalizeSkPositionCode()); |
| 176 | statements.push_back(std::move(result)); |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 177 | return Block::Make(statement.fOffset, std::move(statements), |
| 178 | fSymbolTable, /*isScope=*/true); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 186 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) { |
| 187 | SkASSERT(block.fKind == ASTNode::Kind::kBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 188 | AutoSymbolTable table(this); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 189 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 190 | for (const auto& child : block) { |
| 191 | std::unique_ptr<Statement> statement = this->convertStatement(child); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 | if (!statement) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | statements.push_back(std::move(statement)); |
| 196 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 197 | return Block::Make(block.fOffset, std::move(statements), fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 200 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) { |
| 201 | SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 202 | auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 203 | if (decls.empty()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | return nullptr; |
| 205 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 206 | return Block::MakeUnscoped(s.fOffset, std::move(decls)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 207 | } |
| 208 | |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 209 | int IRGenerator::convertArraySize(const Type& type, int offset, const ASTNode& s) { |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 210 | if (!s) { |
| 211 | this->errorReporter().error(offset, "array must have a size"); |
| 212 | return 0; |
| 213 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 214 | auto size = this->convertExpression(s); |
| 215 | if (!size) { |
| 216 | return 0; |
| 217 | } |
John Stiles | 7253a20 | 2021-08-23 09:24:33 -0400 | [diff] [blame] | 218 | return type.convertArraySize(fContext, std::move(size)); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 221 | void IRGenerator::checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType, |
| 222 | Variable::Storage storage) { |
John Stiles | e609598 | 2021-02-04 12:42:01 -0500 | [diff] [blame] | 223 | if (this->strictES2Mode() && baseType->isArray()) { |
| 224 | this->errorReporter().error(offset, "array size must appear after variable name"); |
| 225 | } |
| 226 | |
John Stiles | 3624aba | 2020-12-22 16:29:53 -0500 | [diff] [blame] | 227 | if (baseType->componentType().isOpaque() && storage != Variable::Storage::kGlobal) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 228 | this->errorReporter().error( |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 229 | offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 230 | "variables of type '" + baseType->displayName() + "' must be global"); |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 231 | } |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 232 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && baseType->isMatrix()) { |
| 233 | this->errorReporter().error(offset, "'in' variables may not have matrix type"); |
| 234 | } |
| 235 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 236 | this->errorReporter().error(offset, "'in uniform' variables not permitted"); |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 237 | } |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 238 | if (this->isRuntimeEffect()) { |
Brian Osman | 5c62582 | 2021-04-06 14:28:07 -0400 | [diff] [blame] | 239 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Brian Osman | 3967588 | 2021-03-31 14:37:10 -0400 | [diff] [blame] | 240 | this->errorReporter().error(offset, "'in' variables not permitted in runtime effects"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 241 | } |
| 242 | } |
Brian Osman | 5c62582 | 2021-04-06 14:28:07 -0400 | [diff] [blame] | 243 | if (baseType->isEffectChild() && !(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 244 | this->errorReporter().error( |
| 245 | offset, "variables of type '" + baseType->displayName() + "' must be uniform"); |
| 246 | } |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 247 | if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) { |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 248 | if (!this->isRuntimeEffect()) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 249 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 250 | "'srgb_unpremul' is only permitted in runtime effects"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 251 | } |
| 252 | if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 253 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 254 | "'srgb_unpremul' is only permitted on 'uniform' variables"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 255 | } |
| 256 | auto validColorXformType = [](const Type& t) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 257 | return t.isVector() && t.componentType().isFloat() && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 258 | (t.columns() == 3 || t.columns() == 4); |
| 259 | }; |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 260 | if (!validColorXformType(*baseType) && !(baseType->isArray() && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 261 | validColorXformType(baseType->componentType()))) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 262 | this->errorReporter().error(offset, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 263 | "'srgb_unpremul' is only permitted on half3, half4, " |
| 264 | "float3, or float4 variables"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 265 | } |
| 266 | } |
John Stiles | b806da4 | 2021-08-04 16:26:47 -0400 | [diff] [blame] | 267 | int permitted = Modifiers::kConst_Flag | Modifiers::kHighp_Flag | Modifiers::kMediump_Flag | |
| 268 | Modifiers::kLowp_Flag; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 269 | if (storage == Variable::Storage::kGlobal) { |
Brian Osman | 9ca38ff | 2021-02-18 12:58:54 -0500 | [diff] [blame] | 270 | permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 271 | Modifiers::kFlat_Flag | Modifiers::kNoPerspective_Flag; |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 272 | } |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 273 | // TODO(skbug.com/11301): Migrate above checks into building a mask of permitted layout flags |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 274 | CheckModifiers(fContext, offset, modifiers, permitted, /*permittedLayoutFlags=*/~0); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 277 | std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& modifiers, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 278 | const Type* baseType, skstd::string_view name, |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 279 | bool isArray, |
| 280 | std::unique_ptr<Expression> arraySize, |
| 281 | Variable::Storage storage) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 282 | if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 283 | (modifiers.fFlags & Modifiers::kOut_Flag) && |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 284 | this->programKind() == ProgramKind::kFragment && name != Compiler::FRAGCOLOR_NAME) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 285 | this->errorReporter().error(offset, |
| 286 | "out location=0, index=0 is reserved for sk_FragColor"); |
| 287 | } |
| 288 | const Type* type = baseType; |
| 289 | int arraySizeValue = 0; |
| 290 | if (isArray) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 291 | SkASSERT(arraySize); |
John Stiles | 7253a20 | 2021-08-23 09:24:33 -0400 | [diff] [blame] | 292 | arraySizeValue = type->convertArraySize(fContext, std::move(arraySize)); |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 293 | if (!arraySizeValue) { |
| 294 | return {}; |
| 295 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 296 | type = fSymbolTable->addArrayDimension(type, arraySizeValue); |
| 297 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 298 | return std::make_unique<Variable>(offset, this->modifiersPool().add(modifiers), name, |
| 299 | type, fIsBuiltinCode, storage); |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var, |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 303 | std::unique_ptr<Expression> value, |
| 304 | bool addToSymbolTable) { |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 305 | std::unique_ptr<Statement> varDecl = VarDeclaration::Convert(fContext, var.get(), |
| 306 | std::move(value)); |
| 307 | if (!varDecl) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | // Detect the declaration of magical variables. |
| 312 | if ((var->storage() == Variable::Storage::kGlobal) && var->name() == Compiler::FRAGCOLOR_NAME) { |
| 313 | // Silently ignore duplicate definitions of `sk_FragColor`. |
| 314 | const Symbol* symbol = (*fSymbolTable)[var->name()]; |
| 315 | if (symbol) { |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 316 | return nullptr; |
| 317 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 318 | } else if ((var->storage() == Variable::Storage::kGlobal || |
| 319 | var->storage() == Variable::Storage::kInterfaceBlock) && |
| 320 | var->name() == Compiler::RTADJUST_NAME) { |
| 321 | // `sk_RTAdjust` is special, and makes the IR generator emit position-fixup expressions. |
| 322 | if (fRTAdjust) { |
| 323 | this->errorReporter().error(var->fOffset, "duplicate definition of 'sk_RTAdjust'"); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 324 | return nullptr; |
| 325 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 326 | if (var->type() != *fContext.fTypes.fFloat4) { |
| 327 | this->errorReporter().error(var->fOffset, "sk_RTAdjust must have type 'float4'"); |
Brian Osman | fc4b991 | 2021-03-05 16:45:06 -0500 | [diff] [blame] | 328 | return nullptr; |
| 329 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 330 | fRTAdjust = var.get(); |
| 331 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 332 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 333 | if (addToSymbolTable) { |
| 334 | fSymbolTable->add(std::move(var)); |
| 335 | } else { |
| 336 | fSymbolTable->takeOwnershipOfSymbol(std::move(var)); |
| 337 | } |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 338 | return varDecl; |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(int offset, |
| 342 | const Modifiers& modifiers, |
| 343 | const Type* baseType, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 344 | skstd::string_view name, |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 345 | bool isArray, |
| 346 | std::unique_ptr<Expression> arraySize, |
| 347 | std::unique_ptr<Expression> value, |
| 348 | Variable::Storage storage) { |
| 349 | std::unique_ptr<Variable> var = this->convertVar(offset, modifiers, baseType, name, isArray, |
| 350 | std::move(arraySize), storage); |
| 351 | if (!var) { |
| 352 | return nullptr; |
| 353 | } |
| 354 | return this->convertVarDeclaration(std::move(var), std::move(value)); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls, |
| 358 | Variable::Storage storage) { |
| 359 | SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations); |
| 360 | auto declarationsIter = decls.begin(); |
| 361 | const Modifiers& modifiers = declarationsIter++->getModifiers(); |
| 362 | const ASTNode& rawType = *(declarationsIter++); |
| 363 | const Type* baseType = this->convertType(rawType); |
| 364 | if (!baseType) { |
| 365 | return {}; |
| 366 | } |
John Stiles | b806da4 | 2021-08-04 16:26:47 -0400 | [diff] [blame] | 367 | baseType = baseType->applyPrecisionQualifiers(fContext, modifiers, fSymbolTable.get(), |
| 368 | decls.fOffset); |
| 369 | if (!baseType) { |
| 370 | return {}; |
| 371 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 372 | |
| 373 | this->checkVarDeclaration(decls.fOffset, modifiers, baseType, storage); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 374 | |
| 375 | StatementArray varDecls; |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 376 | for (; declarationsIter != decls.end(); ++declarationsIter) { |
| 377 | const ASTNode& varDecl = *declarationsIter; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 378 | const ASTNode::VarData& varData = varDecl.getVarData(); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 379 | std::unique_ptr<Expression> arraySize; |
| 380 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 381 | auto iter = varDecl.begin(); |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 382 | if (iter != varDecl.end() && varData.fIsArray) { |
John Stiles | 8f633ef | 2021-08-21 17:13:11 -0400 | [diff] [blame] | 383 | if (!*iter) { |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 384 | this->errorReporter().error(decls.fOffset, "array must have a size"); |
John Stiles | fabed8b | 2021-03-29 09:38:59 -0400 | [diff] [blame] | 385 | continue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 386 | } |
John Stiles | 8f633ef | 2021-08-21 17:13:11 -0400 | [diff] [blame] | 387 | arraySize = this->convertExpression(*iter++); |
| 388 | if (!arraySize) { |
| 389 | continue; |
| 390 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 391 | } |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 392 | if (iter != varDecl.end()) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 393 | value = this->convertExpression(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 394 | if (!value) { |
John Stiles | fabed8b | 2021-03-29 09:38:59 -0400 | [diff] [blame] | 395 | continue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 396 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 397 | } |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 398 | std::unique_ptr<Statement> varDeclStmt = this->convertVarDeclaration(varDecl.fOffset, |
| 399 | modifiers, |
| 400 | baseType, |
| 401 | varData.fName, |
| 402 | varData.fIsArray, |
| 403 | std::move(arraySize), |
| 404 | std::move(value), |
| 405 | storage); |
| 406 | if (varDeclStmt) { |
| 407 | varDecls.push_back(std::move(varDeclStmt)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 408 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 409 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 410 | return varDecls; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 413 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 414 | if (this->programKind() != ProgramKind::kFragment && |
| 415 | this->programKind() != ProgramKind::kVertex && |
| 416 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 417 | this->errorReporter().error(m.fOffset, "layout qualifiers are not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 418 | return nullptr; |
| 419 | } |
| 420 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 421 | SkASSERT(m.fKind == ASTNode::Kind::kModifiers); |
| 422 | Modifiers modifiers = m.getModifiers(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 423 | if (modifiers.fLayout.fInvocations != -1) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 424 | if (this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 425 | this->errorReporter().error(m.fOffset, |
| 426 | "'invocations' is only legal in geometry shaders"); |
Ethan Nicholas | f06576b | 2019-04-03 15:45:25 -0400 | [diff] [blame] | 427 | return nullptr; |
| 428 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 429 | fInvocations = modifiers.fLayout.fInvocations; |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 430 | if (!this->caps().gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 431 | modifiers.fLayout.fInvocations = -1; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 432 | if (modifiers.fLayout.description() == "") { |
| 433 | return nullptr; |
| 434 | } |
| 435 | } |
| 436 | } |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 437 | if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && |
| 438 | !this->caps().gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 439 | modifiers.fLayout.fMaxVertices *= fInvocations; |
| 440 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 441 | return std::make_unique<ModifiersDeclaration>(this->modifiersPool().add(modifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 442 | } |
| 443 | |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 444 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) { |
| 445 | SkASSERT(n.fKind == ASTNode::Kind::kIf); |
| 446 | auto iter = n.begin(); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 447 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 448 | if (!test) { |
| 449 | return nullptr; |
| 450 | } |
| 451 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++)); |
| 452 | if (!ifTrue) { |
| 453 | return nullptr; |
| 454 | } |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 455 | if (this->detectVarDeclarationWithoutScope(*ifTrue)) { |
| 456 | return nullptr; |
| 457 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 458 | std::unique_ptr<Statement> ifFalse; |
| 459 | if (iter != n.end()) { |
| 460 | ifFalse = this->convertStatement(*(iter++)); |
| 461 | if (!ifFalse) { |
| 462 | return nullptr; |
| 463 | } |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 464 | if (this->detectVarDeclarationWithoutScope(*ifFalse)) { |
| 465 | return nullptr; |
| 466 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 467 | } |
John Stiles | 5ede6e3 | 2021-01-11 13:00:49 -0500 | [diff] [blame] | 468 | bool isStatic = n.getBool(); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 469 | return IfStatement::Convert(fContext, n.fOffset, isStatic, std::move(test), |
| 470 | std::move(ifTrue), std::move(ifFalse)); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 473 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) { |
| 474 | SkASSERT(f.fKind == ASTNode::Kind::kFor); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 475 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 476 | std::unique_ptr<Statement> initializer; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 477 | auto iter = f.begin(); |
| 478 | if (*iter) { |
| 479 | initializer = this->convertStatement(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 480 | if (!initializer) { |
| 481 | return nullptr; |
| 482 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 483 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 484 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 485 | std::unique_ptr<Expression> test; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 486 | if (*iter) { |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 487 | test = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 488 | if (!test) { |
| 489 | return nullptr; |
| 490 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 491 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 492 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 493 | std::unique_ptr<Expression> next; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 494 | if (*iter) { |
| 495 | next = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 496 | if (!next) { |
| 497 | return nullptr; |
| 498 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 499 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 500 | ++iter; |
| 501 | std::unique_ptr<Statement> statement = this->convertStatement(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 502 | if (!statement) { |
| 503 | return nullptr; |
| 504 | } |
John Stiles | 2b6ec98 | 2021-03-04 10:46:39 -0500 | [diff] [blame] | 505 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 506 | return nullptr; |
| 507 | } |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 508 | |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 509 | return ForStatement::Convert(fContext, f.fOffset, std::move(initializer), std::move(test), |
| 510 | std::move(next), std::move(statement), fSymbolTable); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) { |
| 514 | SkASSERT(w.fKind == ASTNode::Kind::kWhile); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 515 | auto iter = w.begin(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 516 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 517 | if (!test) { |
| 518 | return nullptr; |
| 519 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 520 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 521 | if (!statement) { |
| 522 | return nullptr; |
| 523 | } |
John Stiles | b321a07 | 2021-02-25 16:24:19 -0500 | [diff] [blame] | 524 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 525 | return nullptr; |
| 526 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 527 | return ForStatement::ConvertWhile(fContext, w.fOffset, std::move(test), std::move(statement), |
| 528 | fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 531 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) { |
| 532 | SkASSERT(d.fKind == ASTNode::Kind::kDo); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 533 | auto iter = d.begin(); |
| 534 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
| 535 | if (!statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 536 | return nullptr; |
| 537 | } |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 538 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 539 | if (!test) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 540 | return nullptr; |
| 541 | } |
John Stiles | ea5822e | 2021-02-26 11:18:20 -0500 | [diff] [blame] | 542 | if (this->detectVarDeclarationWithoutScope(*statement)) { |
| 543 | return nullptr; |
| 544 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 545 | return DoStatement::Convert(fContext, std::move(statement), std::move(test)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 548 | std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) { |
| 549 | SkASSERT(s.fKind == ASTNode::Kind::kSwitch); |
| 550 | |
| 551 | auto iter = s.begin(); |
| 552 | std::unique_ptr<Expression> value = this->convertExpression(*(iter++)); |
| 553 | if (!value) { |
| 554 | return nullptr; |
| 555 | } |
| 556 | AutoSymbolTable table(this); |
| 557 | ExpressionArray caseValues; |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 558 | StatementArray caseStatements; |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 559 | for (; iter != s.end(); ++iter) { |
| 560 | const ASTNode& c = *iter; |
| 561 | SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase); |
| 562 | std::unique_ptr<Expression>& caseValue = caseValues.emplace_back(); |
| 563 | auto childIter = c.begin(); |
| 564 | if (*childIter) { |
| 565 | caseValue = this->convertExpression(*childIter); |
| 566 | if (!caseValue) { |
| 567 | return nullptr; |
| 568 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 569 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 570 | ++childIter; |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 571 | |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 572 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 573 | for (; childIter != c.end(); ++childIter) { |
| 574 | std::unique_ptr<Statement> converted = this->convertStatement(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 575 | if (!converted) { |
| 576 | return nullptr; |
| 577 | } |
| 578 | statements.push_back(std::move(converted)); |
| 579 | } |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 580 | |
| 581 | caseStatements.push_back(Block::MakeUnscoped(c.fOffset, std::move(statements))); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 582 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 583 | return SwitchStatement::Convert(fContext, s.fOffset, s.getBool(), std::move(value), |
| 584 | std::move(caseValues), std::move(caseStatements), fSymbolTable); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 587 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) { |
| 588 | std::unique_ptr<Expression> e = this->convertExpression(s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 589 | if (!e) { |
| 590 | return nullptr; |
| 591 | } |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 592 | return ExpressionStatement::Make(fContext, std::move(e)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 595 | std::unique_ptr<Statement> IRGenerator::convertReturn(int offset, |
| 596 | std::unique_ptr<Expression> result) { |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 597 | return ReturnStatement::Make(offset, std::move(result)); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) { |
| 601 | SkASSERT(r.fKind == ASTNode::Kind::kReturn); |
| 602 | if (r.begin() != r.end()) { |
| 603 | std::unique_ptr<Expression> value = this->convertExpression(*r.begin()); |
| 604 | if (!value) { |
| 605 | return nullptr; |
| 606 | } |
| 607 | return this->convertReturn(r.fOffset, std::move(value)); |
| 608 | } else { |
| 609 | return this->convertReturn(r.fOffset, /*result=*/nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 613 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) { |
| 614 | SkASSERT(b.fKind == ASTNode::Kind::kBreak); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 615 | return BreakStatement::Make(b.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 618 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) { |
| 619 | SkASSERT(c.fKind == ASTNode::Kind::kContinue); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 620 | return ContinueStatement::Make(c.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 623 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) { |
| 624 | SkASSERT(d.fKind == ASTNode::Kind::kDiscard); |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 625 | if (this->programKind() != ProgramKind::kFragment) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 626 | this->errorReporter().error(d.fOffset, |
| 627 | "discard statement is only permitted in fragment shaders"); |
Brian Osman | f2876b0 | 2020-12-15 15:40:32 -0500 | [diff] [blame] | 628 | return nullptr; |
| 629 | } |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 630 | return DiscardStatement::Make(d.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 633 | std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 634 | Layout invokeLayout; |
| 635 | Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag); |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 636 | const FunctionDeclaration* invokeDecl = fSymbolTable->add(std::make_unique<FunctionDeclaration>( |
| 637 | /*offset=*/-1, |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 638 | this->modifiersPool().add(invokeModifiers), |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 639 | "_invoke", |
| 640 | std::vector<const Variable*>(), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 641 | fContext.fTypes.fVoid.get(), |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 642 | fIsBuiltinCode)); |
John Stiles | 842ee5b | 2021-08-27 12:37:27 -0400 | [diff] [blame] | 643 | IntrinsicSet referencedIntrinsics; |
| 644 | main = this->finalizeFunction(*invokeDecl, std::move(main), &referencedIntrinsics); |
| 645 | auto invokeDef = std::make_unique<FunctionDefinition>(/*offset=*/-1, invokeDecl, |
| 646 | fIsBuiltinCode, std::move(main), |
| 647 | std::move(referencedIntrinsics)); |
John Stiles | 0d07e14 | 2020-12-30 18:03:09 -0500 | [diff] [blame] | 648 | invokeDecl->setDefinition(invokeDef.get()); |
| 649 | fProgramElements->push_back(std::move(invokeDef)); |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 650 | |
| 651 | using namespace SkSL::dsl; |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 652 | DSLGlobalVar loopIdx("sk_InvocationID"); |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 653 | std::unique_ptr<Expression> endPrimitive = this->convertIdentifier(/*offset=*/-1, |
| 654 | "EndPrimitive"); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 655 | SkASSERT(endPrimitive); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 656 | |
Ethan Nicholas | be65cd5 | 2021-05-18 12:09:01 -0400 | [diff] [blame] | 657 | std::unique_ptr<Statement> block = DSLBlock( |
| 658 | For(loopIdx = 0, loopIdx < fInvocations, loopIdx++, DSLBlock( |
| 659 | DSLFunction(invokeDecl)(), |
| 660 | DSLExpression(std::move(endPrimitive))({}) |
| 661 | )) |
| 662 | ).release(); |
| 663 | return std::unique_ptr<Block>(&block.release()->as<Block>()); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 664 | } |
| 665 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 666 | std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() { |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 667 | using namespace SkSL::dsl; |
| 668 | using SkSL::dsl::Swizzle; // disambiguate from SkSL::Swizzle |
| 669 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 670 | const Variable* skPerVertex = nullptr; |
| 671 | if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 672 | SkASSERT(perVertexDecl->is<SkSL::InterfaceBlock>()); |
| 673 | skPerVertex = &perVertexDecl->as<SkSL::InterfaceBlock>().variable(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 674 | } |
| 675 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 676 | SkASSERT(skPerVertex && fRTAdjust); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 677 | auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> { |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 678 | return VariableReference::Make(/*offset=*/-1, var); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 679 | }; |
| 680 | auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> { |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 681 | return FieldAccess::Make(fContext, Ref(var), idx, |
| 682 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 683 | }; |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 684 | auto Pos = [&]() -> DSLExpression { |
| 685 | return DSLExpression(FieldAccess::Make(fContext, Ref(skPerVertex), /*fieldIndex=*/0, |
| 686 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock)); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 687 | }; |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 688 | auto Adjust = [&]() -> DSLExpression { |
| 689 | return DSLExpression(fRTAdjustInterfaceBlock |
| 690 | ? Field(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) |
| 691 | : Ref(fRTAdjust)); |
John Stiles | 5acf6a8 | 2020-11-10 22:38:01 -0500 | [diff] [blame] | 692 | }; |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 693 | |
John Stiles | d45ebbc | 2021-05-18 16:54:36 -0400 | [diff] [blame] | 694 | return DSLStatement( |
| 695 | Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) + |
| 696 | Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W), |
| 697 | 0, |
| 698 | Pos().w()) |
| 699 | ).release(); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 702 | void IRGenerator::CheckModifiers(const Context& context, |
| 703 | int offset, |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 704 | const Modifiers& modifiers, |
| 705 | int permittedModifierFlags, |
| 706 | int permittedLayoutFlags) { |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 707 | static constexpr struct { Modifiers::Flag flag; const char* name; } kModifierFlags[] = { |
| 708 | { Modifiers::kConst_Flag, "const" }, |
| 709 | { Modifiers::kIn_Flag, "in" }, |
| 710 | { Modifiers::kOut_Flag, "out" }, |
| 711 | { Modifiers::kUniform_Flag, "uniform" }, |
| 712 | { Modifiers::kFlat_Flag, "flat" }, |
| 713 | { Modifiers::kNoPerspective_Flag, "noperspective" }, |
| 714 | { Modifiers::kHasSideEffects_Flag, "sk_has_side_effects" }, |
| 715 | { Modifiers::kInline_Flag, "inline" }, |
| 716 | { Modifiers::kNoInline_Flag, "noinline" }, |
| 717 | { Modifiers::kHighp_Flag, "highp" }, |
| 718 | { Modifiers::kMediump_Flag, "mediump" }, |
| 719 | { Modifiers::kLowp_Flag, "lowp" }, |
John Stiles | efde90d | 2021-08-12 23:06:24 -0400 | [diff] [blame] | 720 | { Modifiers::kES3_Flag, "$es3" }, |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 721 | }; |
| 722 | |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 723 | int modifierFlags = modifiers.fFlags; |
| 724 | for (const auto& f : kModifierFlags) { |
| 725 | if (modifierFlags & f.flag) { |
| 726 | if (!(permittedModifierFlags & f.flag)) { |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame] | 727 | context.fErrors->error(offset, "'" + String(f.name) + "' is not permitted here"); |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 728 | } |
| 729 | modifierFlags &= ~f.flag; |
| 730 | } |
| 731 | } |
| 732 | SkASSERT(modifierFlags == 0); |
| 733 | |
| 734 | static constexpr struct { Layout::Flag flag; const char* name; } kLayoutFlags[] = { |
| 735 | { Layout::kOriginUpperLeft_Flag, "origin_upper_left"}, |
| 736 | { Layout::kPushConstant_Flag, "push_constant"}, |
| 737 | { Layout::kBlendSupportAllEquations_Flag, "blend_support_all_equations"}, |
| 738 | { Layout::kSRGBUnpremul_Flag, "srgb_unpremul"}, |
| 739 | { Layout::kLocation_Flag, "location"}, |
| 740 | { Layout::kOffset_Flag, "offset"}, |
| 741 | { Layout::kBinding_Flag, "binding"}, |
| 742 | { Layout::kIndex_Flag, "index"}, |
| 743 | { Layout::kSet_Flag, "set"}, |
| 744 | { Layout::kBuiltin_Flag, "builtin"}, |
| 745 | { Layout::kInputAttachmentIndex_Flag, "input_attachment_index"}, |
| 746 | { Layout::kPrimitive_Flag, "primitive-type"}, |
| 747 | { Layout::kMaxVertices_Flag, "max_vertices"}, |
| 748 | { Layout::kInvocations_Flag, "invocations"}, |
| 749 | }; |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 750 | |
| 751 | int layoutFlags = modifiers.fLayout.fFlags; |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 752 | for (const auto& lf : kLayoutFlags) { |
| 753 | if (layoutFlags & lf.flag) { |
| 754 | if (!(permittedLayoutFlags & lf.flag)) { |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame] | 755 | context.fErrors->error( |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 756 | offset, "layout qualifier '" + String(lf.name) + "' is not permitted here"); |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 757 | } |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 758 | layoutFlags &= ~lf.flag; |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 759 | } |
Brian Osman | b5de6be | 2021-08-10 15:16:34 -0400 | [diff] [blame] | 760 | } |
Brian Osman | a654faa | 2021-02-26 11:52:59 -0500 | [diff] [blame] | 761 | SkASSERT(layoutFlags == 0); |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 764 | std::unique_ptr<Block> IRGenerator::finalizeFunction(const FunctionDeclaration& funcDecl, |
John Stiles | 842ee5b | 2021-08-27 12:37:27 -0400 | [diff] [blame] | 765 | std::unique_ptr<Block> body, |
| 766 | IntrinsicSet* referencedIntrinsics) { |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 767 | bool isMain = funcDecl.isMain(); |
| 768 | bool needInvocationIDWorkaround = fInvocations != -1 && isMain && |
| 769 | !this->caps().gsInvocationsSupport(); |
| 770 | if (needInvocationIDWorkaround) { |
| 771 | body = this->applyInvocationIDWorkaround(std::move(body)); |
| 772 | } |
| 773 | if (ProgramKind::kVertex == this->programKind() && isMain && fRTAdjust) { |
| 774 | body->children().push_back(this->getNormalizeSkPositionCode()); |
| 775 | } |
| 776 | |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame^] | 777 | FunctionDefinition::FinalizeFunctionBody(fContext, funcDecl, body.get(), referencedIntrinsics); |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 778 | return body; |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 781 | void IRGenerator::convertFunction(const ASTNode& f) { |
| 782 | auto iter = f.begin(); |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 783 | const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 784 | if (returnType == nullptr) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 785 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 786 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 787 | const ASTNode::FunctionData& funcData = f.getFunctionData(); |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 788 | std::vector<std::unique_ptr<Variable>> parameters; |
| 789 | parameters.reserve(funcData.fParameterCount); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 790 | for (size_t i = 0; i < funcData.fParameterCount; ++i) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 791 | const ASTNode& param = *(iter++); |
| 792 | SkASSERT(param.fKind == ASTNode::Kind::kParameter); |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 793 | const ASTNode::ParameterData& pd = param.getParameterData(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 794 | auto paramIter = param.begin(); |
| 795 | const Type* type = this->convertType(*(paramIter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 796 | if (!type) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 797 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 798 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 799 | if (pd.fIsArray) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 800 | int arraySize = this->convertArraySize(*type, param.fOffset, *paramIter++); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 801 | if (!arraySize) { |
| 802 | return; |
| 803 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 804 | type = fSymbolTable->addArrayDimension(type, arraySize); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 805 | } |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 806 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 807 | parameters.push_back(std::make_unique<Variable>(param.fOffset, |
| 808 | this->modifiersPool().add(pd.fModifiers), |
| 809 | pd.fName, |
| 810 | type, |
| 811 | fIsBuiltinCode, |
| 812 | Variable::Storage::kParameter)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 815 | // Conservatively assume all user-defined functions have side effects. |
| 816 | Modifiers declModifiers = funcData.fModifiers; |
| 817 | if (!fIsBuiltinCode) { |
| 818 | declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 819 | } |
| 820 | |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 821 | if (fContext.fConfig->fSettings.fForceNoInline) { |
| 822 | // Apply the `noinline` modifier to every function. This allows us to test Runtime |
| 823 | // Effects without any inlining, even when the code is later added to a paint. |
| 824 | declModifiers.fFlags &= ~Modifiers::kInline_Flag; |
| 825 | declModifiers.fFlags |= Modifiers::kNoInline_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 826 | } |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 827 | |
| 828 | const FunctionDeclaration* decl = FunctionDeclaration::Convert( |
| 829 | fContext, |
| 830 | *fSymbolTable, |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 831 | f.fOffset, |
| 832 | this->modifiersPool().add(declModifiers), |
| 833 | funcData.fName, |
| 834 | std::move(parameters), |
| 835 | returnType, |
| 836 | fIsBuiltinCode); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 837 | if (!decl) { |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 838 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 839 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 840 | if (iter == f.end()) { |
| 841 | // If there's no body, we've found a prototype. |
| 842 | fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl, |
| 843 | fIsBuiltinCode)); |
| 844 | } else { |
| 845 | // Compile function body. |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 846 | AutoSymbolTable table(this); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 847 | for (const Variable* param : decl->parameters()) { |
| 848 | fSymbolTable->addWithoutOwnership(param); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 849 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 850 | std::unique_ptr<Block> body = this->convertBlock(*iter); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 851 | if (!body) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 852 | return; |
| 853 | } |
John Stiles | 842ee5b | 2021-08-27 12:37:27 -0400 | [diff] [blame] | 854 | IntrinsicSet referencedIntrinsics; |
| 855 | body = this->finalizeFunction(*decl, std::move(body), &referencedIntrinsics); |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 856 | auto result = std::make_unique<FunctionDefinition>( |
John Stiles | 842ee5b | 2021-08-27 12:37:27 -0400 | [diff] [blame] | 857 | f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(referencedIntrinsics)); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 858 | decl->setDefinition(result.get()); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 859 | result->setSource(&f); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 860 | fProgramElements->push_back(std::move(result)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 861 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 862 | } |
| 863 | |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 864 | std::unique_ptr<StructDefinition> IRGenerator::convertStructDefinition(const ASTNode& node) { |
| 865 | SkASSERT(node.fKind == ASTNode::Kind::kType); |
| 866 | |
| 867 | const Type* type = this->convertType(node); |
| 868 | if (!type) { |
| 869 | return nullptr; |
| 870 | } |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 871 | if (!type->isStruct()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 872 | this->errorReporter().error(node.fOffset, |
| 873 | "expected a struct here, found '" + type->name() + "'"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 874 | return nullptr; |
| 875 | } |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 876 | SkDEBUGCODE(auto [iter, wasInserted] =) fDefinedStructs.insert(type); |
| 877 | SkASSERT(wasInserted); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 878 | return std::make_unique<StructDefinition>(node.fOffset, *type); |
| 879 | } |
| 880 | |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 881 | std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) { |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 882 | if (this->programKind() != ProgramKind::kFragment && |
| 883 | this->programKind() != ProgramKind::kVertex && |
| 884 | this->programKind() != ProgramKind::kGeometry) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 885 | this->errorReporter().error(intf.fOffset, "interface block is not allowed here"); |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 886 | return nullptr; |
| 887 | } |
| 888 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 889 | SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock); |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 890 | const ASTNode::InterfaceBlockData& id = intf.getInterfaceBlockData(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 891 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 892 | std::shared_ptr<SymbolTable> symbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 893 | std::vector<Type::Field> fields; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 894 | bool foundRTAdjust = false; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 895 | auto iter = intf.begin(); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 896 | { |
| 897 | AutoSymbolTable table(this); |
| 898 | symbols = fSymbolTable; |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 899 | for (size_t i = 0; i < id.fDeclarationCount; ++i) { |
| 900 | StatementArray decls = this->convertVarDeclarations(*(iter++), |
| 901 | Variable::Storage::kInterfaceBlock); |
| 902 | if (decls.empty()) { |
| 903 | return nullptr; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 904 | } |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 905 | for (const auto& decl : decls) { |
| 906 | const VarDeclaration& vd = decl->as<VarDeclaration>(); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 907 | if (&vd.var() == fRTAdjust) { |
| 908 | foundRTAdjust = true; |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 909 | SkASSERT(vd.var().type() == *fContext.fTypes.fFloat4); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 910 | fRTAdjustFieldIndex = fields.size(); |
| 911 | } |
| 912 | fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(), |
| 913 | &vd.var().type())); |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 914 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 915 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 916 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 917 | const Type* type = old->takeOwnershipOfSymbol(Type::MakeStructType(intf.fOffset, |
Ethan Nicholas | 27f06eb | 2021-07-26 16:39:40 -0400 | [diff] [blame] | 918 | id.fTypeName, |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 919 | fields)); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 920 | int arraySize = 0; |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 921 | if (id.fIsArray) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 922 | const ASTNode& size = *(iter++); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 923 | if (size) { |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 924 | // convertArraySize rejects unsized arrays. This is the one place we allow those, but |
| 925 | // we've already checked for that, so this is verifying the other aspects (constant, |
| 926 | // positive, not too large). |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 927 | arraySize = this->convertArraySize(*type, size.fOffset, size); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 928 | if (!arraySize) { |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 929 | return nullptr; |
| 930 | } |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 931 | } else { |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 932 | arraySize = Type::kUnsizedArray; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 933 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 934 | type = symbols->addArrayDimension(type, arraySize); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 935 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 936 | const Variable* var = old->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 937 | std::make_unique<Variable>(intf.fOffset, |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 938 | this->modifiersPool().add(id.fModifiers), |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 939 | id.fInstanceName.length() ? id.fInstanceName : id.fTypeName, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 940 | type, |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 941 | fIsBuiltinCode, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 942 | Variable::Storage::kGlobal)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 943 | if (foundRTAdjust) { |
| 944 | fRTAdjustInterfaceBlock = var; |
| 945 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 946 | if (id.fInstanceName.length()) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 947 | old->addWithoutOwnership(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 948 | } else { |
| 949 | for (size_t i = 0; i < fields.size(); i++) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 950 | old->add(std::make_unique<Field>(intf.fOffset, var, (int)i)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 953 | return std::make_unique<SkSL::InterfaceBlock>(intf.fOffset, |
| 954 | var, |
Ethan Nicholas | 3533ff1 | 2021-08-02 12:53:29 -0400 | [diff] [blame] | 955 | id.fTypeName, |
| 956 | id.fInstanceName, |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 957 | arraySize, |
| 958 | symbols); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 959 | } |
| 960 | |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 961 | void IRGenerator::convertGlobalVarDeclarations(const ASTNode& decl) { |
| 962 | StatementArray decls = this->convertVarDeclarations(decl, Variable::Storage::kGlobal); |
| 963 | for (std::unique_ptr<Statement>& stmt : decls) { |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 964 | const Type* type = &stmt->as<VarDeclaration>().baseType(); |
| 965 | if (type->isStruct()) { |
| 966 | auto [iter, wasInserted] = fDefinedStructs.insert(type); |
| 967 | if (wasInserted) { |
| 968 | fProgramElements->push_back( |
| 969 | std::make_unique<StructDefinition>(decl.fOffset, *type)); |
| 970 | } |
| 971 | } |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 972 | fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(stmt))); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 976 | static bool type_contains_private_fields(const Type& type) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 977 | // Checks for usage of private types, including fields inside a struct. |
| 978 | if (type.isPrivate()) { |
| 979 | return true; |
| 980 | } |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 981 | if (type.isStruct()) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 982 | for (const auto& f : type.fields()) { |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 983 | if (type_contains_private_fields(*f.fType)) { |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | return false; |
| 989 | } |
| 990 | |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 991 | const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 992 | skstd::string_view name = type.getStringView(); |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 993 | const Symbol* symbol = (*fSymbolTable)[name]; |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 994 | if (!symbol || !symbol->is<Type>()) { |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 995 | this->errorReporter().error(type.fOffset, "unknown type '" + name + "'"); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 996 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 997 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 998 | const Type* result = &symbol->as<Type>(); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 999 | const bool isArray = (type.begin() != type.end()); |
John Stiles | 2558c46 | 2021-03-16 17:49:20 -0400 | [diff] [blame] | 1000 | if (result->isVoid() && !allowVoid) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 1001 | this->errorReporter().error(type.fOffset, |
| 1002 | "type '" + name + "' not allowed in this context"); |
| 1003 | return nullptr; |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1004 | } |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 1005 | if (!fIsBuiltinCode) { |
| 1006 | if (type_contains_private_fields(*result)) { |
| 1007 | this->errorReporter().error(type.fOffset, "type '" + name + "' is private"); |
| 1008 | return nullptr; |
| 1009 | } |
| 1010 | if (this->strictES2Mode() && !result->allowedInES2()) { |
| 1011 | this->errorReporter().error(type.fOffset, "type '" + name + "' is not supported"); |
| 1012 | return nullptr; |
| 1013 | } |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1014 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1015 | if (isArray) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 1016 | auto iter = type.begin(); |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 1017 | int arraySize = this->convertArraySize(*result, type.fOffset, *iter); |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1018 | if (!arraySize) { |
| 1019 | return nullptr; |
| 1020 | } |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 1021 | result = fSymbolTable->addArrayDimension(result, arraySize); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 1022 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 1023 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1026 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1027 | switch (expr.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1028 | case ASTNode::Kind::kBinary: |
| 1029 | return this->convertBinaryExpression(expr); |
| 1030 | case ASTNode::Kind::kBool: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1031 | return BoolLiteral::Make(fContext, expr.fOffset, expr.getBool()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1032 | case ASTNode::Kind::kCall: |
| 1033 | return this->convertCallExpression(expr); |
| 1034 | case ASTNode::Kind::kField: |
| 1035 | return this->convertFieldExpression(expr); |
| 1036 | case ASTNode::Kind::kFloat: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1037 | return FloatLiteral::Make(fContext, expr.fOffset, expr.getFloat()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1038 | case ASTNode::Kind::kIdentifier: |
| 1039 | return this->convertIdentifier(expr); |
| 1040 | case ASTNode::Kind::kIndex: |
| 1041 | return this->convertIndexExpression(expr); |
| 1042 | case ASTNode::Kind::kInt: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 1043 | return IntLiteral::Make(fContext, expr.fOffset, expr.getInt()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1044 | case ASTNode::Kind::kPostfix: |
| 1045 | return this->convertPostfixExpression(expr); |
| 1046 | case ASTNode::Kind::kPrefix: |
| 1047 | return this->convertPrefixExpression(expr); |
| 1048 | case ASTNode::Kind::kTernary: |
| 1049 | return this->convertTernaryExpression(expr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1050 | default: |
John Stiles | fbb905e | 2021-08-25 15:51:50 -0400 | [diff] [blame] | 1051 | SkDEBUGFAIL("unsupported expression"); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1052 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1056 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, skstd::string_view name) { |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1057 | const Symbol* result = (*fSymbolTable)[name]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1058 | if (!result) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1059 | this->errorReporter().error(offset, "unknown identifier '" + name + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1060 | return nullptr; |
| 1061 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1062 | switch (result->kind()) { |
| 1063 | case Symbol::Kind::kFunctionDeclaration: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1064 | std::vector<const FunctionDeclaration*> f = { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1065 | &result->as<FunctionDeclaration>() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1066 | }; |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1067 | return std::make_unique<FunctionReference>(fContext, offset, f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1068 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1069 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1070 | const UnresolvedFunction* f = &result->as<UnresolvedFunction>(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1071 | return std::make_unique<FunctionReference>(fContext, offset, f->functions()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1072 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1073 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1074 | const Variable* var = &result->as<Variable>(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1075 | const Modifiers& modifiers = var->modifiers(); |
| 1076 | switch (modifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1077 | case SK_FRAGCOORD_BUILTIN: |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1078 | if (caps().canUseFragCoord()) { |
| 1079 | fInputs.fUseFlipRTUniform = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1080 | } |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1081 | break; |
| 1082 | case SK_CLOCKWISE_BUILTIN: |
| 1083 | fInputs.fUseFlipRTUniform = true; |
| 1084 | break; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1085 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1086 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
John Stiles | a94e026 | 2021-04-27 17:29:59 -0400 | [diff] [blame] | 1087 | return VariableReference::Make(offset, var, VariableReference::RefKind::kRead); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1088 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1089 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1090 | const Field* field = &result->as<Field>(); |
John Stiles | a94e026 | 2021-04-27 17:29:59 -0400 | [diff] [blame] | 1091 | auto base = VariableReference::Make(offset, &field->owner(), |
| 1092 | VariableReference::RefKind::kRead); |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1093 | return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(), |
| 1094 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1095 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1096 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1097 | const Type* t = &result->as<Type>(); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1098 | return std::make_unique<TypeReference>(fContext, offset, t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1099 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1100 | case Symbol::Kind::kExternal: { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1101 | const ExternalFunction* r = &result->as<ExternalFunction>(); |
| 1102 | return std::make_unique<ExternalFunctionReference>(offset, r); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1103 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1104 | default: |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 1105 | SK_ABORT("unsupported symbol type %d\n", (int) result->kind()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1106 | } |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1109 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1110 | return this->convertIdentifier(identifier.fOffset, identifier.getStringView()); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1111 | } |
| 1112 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1113 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1114 | const Type& type) { |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1115 | return type.coerceExpression(std::move(expr), fContext); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1118 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) { |
| 1119 | SkASSERT(expression.fKind == ASTNode::Kind::kBinary); |
| 1120 | auto iter = expression.begin(); |
| 1121 | std::unique_ptr<Expression> left = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1122 | if (!left) { |
| 1123 | return nullptr; |
| 1124 | } |
John Stiles | 0f46450 | 2020-11-20 12:52:22 -0500 | [diff] [blame] | 1125 | std::unique_ptr<Expression> right = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1126 | if (!right) { |
| 1127 | return nullptr; |
| 1128 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1129 | return BinaryExpression::Convert(fContext, std::move(left), expression.getOperator(), |
| 1130 | std::move(right)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1133 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) { |
| 1134 | SkASSERT(node.fKind == ASTNode::Kind::kTernary); |
| 1135 | auto iter = node.begin(); |
| 1136 | std::unique_ptr<Expression> test = this->convertExpression(*(iter++)); |
| 1137 | if (!test) { |
| 1138 | return nullptr; |
| 1139 | } |
| 1140 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++)); |
| 1141 | if (!ifTrue) { |
| 1142 | return nullptr; |
| 1143 | } |
| 1144 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++)); |
| 1145 | if (!ifFalse) { |
| 1146 | return nullptr; |
| 1147 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1148 | return TernaryExpression::Convert(fContext, std::move(test), |
| 1149 | std::move(ifTrue), std::move(ifFalse)); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1152 | void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1153 | if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) { |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 1154 | const FunctionDefinition& original = found->as<FunctionDefinition>(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1155 | |
| 1156 | // Sort the referenced intrinsics into a consistent order; otherwise our output will become |
| 1157 | // non-deterministic. |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1158 | std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(), |
| 1159 | original.referencedIntrinsics().end()); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1160 | std::sort(intrinsics.begin(), intrinsics.end(), |
| 1161 | [](const FunctionDeclaration* a, const FunctionDeclaration* b) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1162 | if (a->isBuiltin() != b->isBuiltin()) { |
| 1163 | return a->isBuiltin() < b->isBuiltin(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1164 | } |
| 1165 | if (a->fOffset != b->fOffset) { |
| 1166 | return a->fOffset < b->fOffset; |
| 1167 | } |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1168 | if (a->name() != b->name()) { |
| 1169 | return a->name() < b->name(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 1170 | } |
| 1171 | return a->description() < b->description(); |
| 1172 | }); |
| 1173 | for (const FunctionDeclaration* f : intrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1174 | this->copyIntrinsicIfNeeded(*f); |
| 1175 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 1176 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1177 | fSharedElements->push_back(found); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1181 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1182 | const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1183 | ExpressionArray arguments) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1184 | if (function.isBuiltin()) { |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1185 | if (function.intrinsicKind() == k_dFdy_IntrinsicKind) { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1186 | fInputs.fUseFlipRTUniform = true; |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1187 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1188 | if (!fIsBuiltinCode && fIntrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1189 | this->copyIntrinsicIfNeeded(function); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1190 | } |
| 1191 | } |
John Stiles | ea9ab82 | 2020-08-31 09:55:04 -0400 | [diff] [blame] | 1192 | |
John Stiles | cd7ba50 | 2021-03-19 10:54:59 -0400 | [diff] [blame] | 1193 | return FunctionCall::Convert(fContext, offset, function, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /** |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1197 | * Determines the cost of coercing the arguments of a function to the required types. Cost has no |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1198 | * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if |
| 1199 | * the call is not valid. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1200 | */ |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1201 | CoercionCost IRGenerator::callCost(const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1202 | const ExpressionArray& arguments) { |
John Stiles | efde90d | 2021-08-12 23:06:24 -0400 | [diff] [blame] | 1203 | if (this->strictES2Mode() && (function.modifiers().fFlags & Modifiers::kES3_Flag)) { |
| 1204 | return CoercionCost::Impossible(); |
| 1205 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1206 | if (function.parameters().size() != arguments.size()) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1207 | return CoercionCost::Impossible(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1208 | } |
John Stiles | fa88911 | 2020-10-12 19:03:43 -0400 | [diff] [blame] | 1209 | FunctionDeclaration::ParamTypes types; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1210 | const Type* ignored; |
| 1211 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1212 | return CoercionCost::Impossible(); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1213 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1214 | CoercionCost total = CoercionCost::Free(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1215 | for (size_t i = 0; i < arguments.size(); i++) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1216 | total = total + arguments[i]->coercionCost(*types[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1217 | } |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1218 | return total; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1221 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1222 | std::unique_ptr<Expression> functionValue, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1223 | ExpressionArray arguments) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1224 | switch (functionValue->kind()) { |
| 1225 | case Expression::Kind::kTypeReference: |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1226 | return Constructor::Convert(fContext, |
| 1227 | offset, |
| 1228 | functionValue->as<TypeReference>().value(), |
| 1229 | std::move(arguments)); |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1230 | case Expression::Kind::kExternalFunctionReference: { |
| 1231 | const ExternalFunction& f = functionValue->as<ExternalFunctionReference>().function(); |
| 1232 | int count = f.callParameterCount(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1233 | if (count != (int) arguments.size()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1234 | this->errorReporter().error(offset, "external function expected " + |
| 1235 | to_string(count) + " arguments, but found " + |
| 1236 | to_string((int)arguments.size())); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1237 | return nullptr; |
| 1238 | } |
| 1239 | static constexpr int PARAMETER_MAX = 16; |
| 1240 | SkASSERT(count < PARAMETER_MAX); |
| 1241 | const Type* types[PARAMETER_MAX]; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1242 | f.getCallParameterTypes(types); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1243 | for (int i = 0; i < count; ++i) { |
| 1244 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
| 1245 | if (!arguments[i]) { |
| 1246 | return nullptr; |
| 1247 | } |
| 1248 | } |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1249 | return std::make_unique<ExternalFunctionCall>(offset, &f, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1250 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1251 | case Expression::Kind::kFunctionReference: { |
John Stiles | ce591b7 | 2020-08-27 11:47:30 -0400 | [diff] [blame] | 1252 | const FunctionReference& ref = functionValue->as<FunctionReference>(); |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1253 | const std::vector<const FunctionDeclaration*>& functions = ref.functions(); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1254 | CoercionCost bestCost = CoercionCost::Impossible(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1255 | const FunctionDeclaration* best = nullptr; |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1256 | if (functions.size() > 1) { |
| 1257 | for (const auto& f : functions) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1258 | CoercionCost cost = this->callCost(*f, arguments); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1259 | if (cost < bestCost) { |
| 1260 | bestCost = cost; |
| 1261 | best = f; |
| 1262 | } |
| 1263 | } |
| 1264 | if (best) { |
| 1265 | return this->call(offset, *best, std::move(arguments)); |
| 1266 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1267 | String msg = "no match for " + functions[0]->name() + "("; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1268 | String separator; |
| 1269 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1270 | msg += separator; |
| 1271 | separator = ", "; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1272 | msg += arguments[i]->type().displayName(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1273 | } |
| 1274 | msg += ")"; |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1275 | this->errorReporter().error(offset, msg); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1276 | return nullptr; |
| 1277 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 1278 | return this->call(offset, *functions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1279 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1280 | default: |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1281 | this->errorReporter().error(offset, "not a function"); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1282 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1283 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1286 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) { |
| 1287 | SkASSERT(expression.fKind == ASTNode::Kind::kPrefix); |
| 1288 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1289 | if (!base) { |
| 1290 | return nullptr; |
| 1291 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1292 | return PrefixExpression::Convert(fContext, expression.getOperator(), std::move(base)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1295 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1296 | skstd::string_view fields) { |
Ethan Nicholas | 59ff4e2 | 2021-07-12 12:28:57 -0400 | [diff] [blame] | 1297 | return Swizzle::Convert(fContext, std::move(base), fields); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1300 | std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) { |
| 1301 | SkASSERT(index.fKind == ASTNode::Kind::kIndex); |
| 1302 | auto iter = index.begin(); |
| 1303 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1304 | if (!base) { |
| 1305 | return nullptr; |
| 1306 | } |
Ethan Nicholas | 4d2bbbb | 2021-01-12 11:56:23 -0500 | [diff] [blame] | 1307 | if (iter == index.end()) { |
Ethan Nicholas | 251e634 | 2021-07-20 13:16:19 -0400 | [diff] [blame] | 1308 | if (base->is<TypeReference>()) { |
| 1309 | this->errorReporter().error(index.fOffset, "array must have a size"); |
| 1310 | } else { |
| 1311 | this->errorReporter().error(base->fOffset, "missing index in '[]'"); |
| 1312 | } |
Brian Osman | 4cf8507 | 2021-01-14 14:18:10 -0500 | [diff] [blame] | 1313 | return nullptr; |
Ethan Nicholas | 4d2bbbb | 2021-01-12 11:56:23 -0500 | [diff] [blame] | 1314 | } |
| 1315 | std::unique_ptr<Expression> converted = this->convertExpression(*(iter++)); |
| 1316 | if (!converted) { |
| 1317 | return nullptr; |
| 1318 | } |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 1319 | return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base), std::move(converted)); |
John Stiles | 1b27c3d | 2020-12-07 12:14:55 -0500 | [diff] [blame] | 1320 | } |
| 1321 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1322 | std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) { |
| 1323 | SkASSERT(callNode.fKind == ASTNode::Kind::kCall); |
| 1324 | auto iter = callNode.begin(); |
| 1325 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
| 1326 | if (!base) { |
| 1327 | return nullptr; |
| 1328 | } |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1329 | ExpressionArray arguments; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1330 | for (; iter != callNode.end(); ++iter) { |
| 1331 | std::unique_ptr<Expression> converted = this->convertExpression(*iter); |
| 1332 | if (!converted) { |
| 1333 | return nullptr; |
| 1334 | } |
| 1335 | arguments.push_back(std::move(converted)); |
| 1336 | } |
| 1337 | return this->call(callNode.fOffset, std::move(base), std::move(arguments)); |
| 1338 | } |
| 1339 | |
| 1340 | std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) { |
| 1341 | std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin()); |
| 1342 | if (!base) { |
| 1343 | return nullptr; |
| 1344 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1345 | const skstd::string_view& field = fieldNode.getStringView(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1346 | const Type& baseType = base->type(); |
Ethan Nicholas | 5c75e83 | 2021-06-10 18:28:19 -0400 | [diff] [blame] | 1347 | if (baseType == *fContext.fTypes.fSkCaps || baseType.isStruct()) { |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1348 | return FieldAccess::Convert(fContext, std::move(base), field); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1349 | } |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 1350 | return this->convertSwizzle(std::move(base), field); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) { |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 1354 | SkASSERT(expression.fKind == ASTNode::Kind::kPostfix); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1355 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
| 1356 | if (!base) { |
| 1357 | return nullptr; |
| 1358 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 1359 | return PostfixExpression::Convert(fContext, std::move(base), expression.getOperator()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | void IRGenerator::checkValid(const Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1363 | switch (expr.kind()) { |
John Stiles | dc435fa | 2020-12-30 18:07:10 -0500 | [diff] [blame] | 1364 | case Expression::Kind::kFunctionCall: { |
| 1365 | const FunctionDeclaration& decl = expr.as<FunctionCall>().function(); |
| 1366 | if (!decl.isBuiltin() && !decl.definition()) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1367 | this->errorReporter().error(expr.fOffset, |
| 1368 | "function '" + decl.description() + "' is not defined"); |
John Stiles | dc435fa | 2020-12-30 18:07:10 -0500 | [diff] [blame] | 1369 | } |
| 1370 | break; |
| 1371 | } |
John Stiles | 973009b | 2021-02-17 17:18:01 -0500 | [diff] [blame] | 1372 | case Expression::Kind::kFunctionReference: |
| 1373 | case Expression::Kind::kTypeReference: |
| 1374 | SkDEBUGFAIL("invalid reference-expression, should have been reported by coerce()"); |
| 1375 | this->errorReporter().error(expr.fOffset, "invalid expression"); |
| 1376 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1377 | default: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1378 | if (expr.type() == *fContext.fTypes.fInvalid) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 1379 | this->errorReporter().error(expr.fOffset, "invalid expression"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1380 | } |
John Stiles | 973009b | 2021-02-17 17:18:01 -0500 | [diff] [blame] | 1381 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1385 | void IRGenerator::findAndDeclareBuiltinVariables() { |
| 1386 | class BuiltinVariableScanner : public ProgramVisitor { |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1387 | public: |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1388 | BuiltinVariableScanner(IRGenerator* generator) : fGenerator(generator) {} |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1389 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1390 | void addDeclaringElement(const String& name) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1391 | // If this is the *first* time we've seen this builtin, findAndInclude will return |
| 1392 | // the corresponding ProgramElement. |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1393 | if (const ProgramElement* decl = fGenerator->fIntrinsics->findAndInclude(name)) { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1394 | SkASSERT(decl->is<GlobalVarDeclaration>() || decl->is<SkSL::InterfaceBlock>()); |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1395 | fNewElements.push_back(decl); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1399 | bool visitProgramElement(const ProgramElement& pe) override { |
| 1400 | if (pe.is<FunctionDefinition>()) { |
| 1401 | const FunctionDefinition& funcDef = pe.as<FunctionDefinition>(); |
| 1402 | // We synthesize writes to sk_FragColor if main() returns a color, even if it's |
| 1403 | // otherwise unreferenced. Check main's return type to see if it's half4. |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1404 | if (funcDef.declaration().isMain() && |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1405 | funcDef.declaration().returnType() == *fGenerator->fContext.fTypes.fHalf4) { |
| 1406 | fPreserveFragColor = true; |
| 1407 | } |
| 1408 | } |
| 1409 | return INHERITED::visitProgramElement(pe); |
| 1410 | } |
| 1411 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1412 | bool visitExpression(const Expression& e) override { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1413 | if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1414 | this->addDeclaringElement(String(e.as<VariableReference>().variable()->name())); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1415 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1416 | return INHERITED::visitExpression(e); |
| 1417 | } |
| 1418 | |
| 1419 | IRGenerator* fGenerator; |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1420 | std::vector<const ProgramElement*> fNewElements; |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1421 | bool fPreserveFragColor = false; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1422 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1423 | using INHERITED = ProgramVisitor; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1424 | using INHERITED::visitProgramElement; |
| 1425 | }; |
| 1426 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1427 | BuiltinVariableScanner scanner(this); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1428 | SkASSERT(fProgramElements); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1429 | for (auto& e : *fProgramElements) { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1430 | scanner.visitProgramElement(*e); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1431 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1432 | |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1433 | if (scanner.fPreserveFragColor) { |
| 1434 | // main() returns a half4, so make sure we don't dead-strip sk_FragColor. |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 1435 | scanner.addDeclaringElement(Compiler::FRAGCOLOR_NAME); |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1436 | } |
| 1437 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1438 | switch (this->programKind()) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1439 | case ProgramKind::kFragment: |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 1440 | // Vulkan requires certain builtin variables be present, even if they're unused. At one |
| 1441 | // time, validation errors would result if sk_Clockwise was missing. Now, it's just |
| 1442 | // (Adreno) driver bugs that drop or corrupt draws if they're missing. |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1443 | scanner.addDeclaringElement("sk_Clockwise"); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1444 | break; |
| 1445 | default: |
| 1446 | break; |
| 1447 | } |
| 1448 | |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1449 | fSharedElements->insert( |
| 1450 | fSharedElements->begin(), scanner.fNewElements.begin(), scanner.fNewElements.end()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1451 | } |
| 1452 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1453 | void IRGenerator::start(const ParsedModule& base, |
| 1454 | bool isBuiltinCode, |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1455 | std::vector<std::unique_ptr<ProgramElement>>* elements, |
| 1456 | std::vector<const ProgramElement*>* sharedElements) { |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1457 | fProgramElements = elements; |
| 1458 | fSharedElements = sharedElements; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1459 | fSymbolTable = base.fSymbols; |
| 1460 | fIntrinsics = base.fIntrinsics.get(); |
| 1461 | if (fIntrinsics) { |
| 1462 | fIntrinsics->resetAlreadyIncluded(); |
| 1463 | } |
| 1464 | fIsBuiltinCode = isBuiltinCode; |
| 1465 | |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1466 | fInputs = {}; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1467 | fInvocations = -1; |
| 1468 | fRTAdjust = nullptr; |
| 1469 | fRTAdjustInterfaceBlock = nullptr; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 1470 | fDefinedStructs.clear(); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1471 | this->pushSymbolTable(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1472 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1473 | if (this->programKind() == ProgramKind::kGeometry && !fIsBuiltinCode) { |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1474 | // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin. |
| 1475 | // If we're applying the workaround, then it's a plain global. |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1476 | bool workaround = !this->caps().gsInvocationsSupport(); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1477 | Modifiers m; |
| 1478 | if (!workaround) { |
| 1479 | m.fFlags = Modifiers::kIn_Flag; |
| 1480 | m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN; |
| 1481 | } |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1482 | auto var = std::make_unique<Variable>(/*offset=*/-1, this->modifiersPool().add(m), |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 1483 | "sk_InvocationID", fContext.fTypes.fInt.get(), |
| 1484 | /*builtin=*/false, Variable::Storage::kGlobal); |
| 1485 | auto decl = VarDeclaration::Make(fContext, var.get(), fContext.fTypes.fInt.get(), |
| 1486 | /*arraySize=*/0, /*value=*/nullptr); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1487 | fSymbolTable->add(std::move(var)); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1488 | fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>(std::move(decl))); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1491 | if (this->settings().fExternalFunctions) { |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1492 | // Add any external values to the new symbol table, so they're only visible to this Program. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1493 | for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1494 | fSymbolTable->addWithoutOwnership(ef.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1495 | } |
| 1496 | } |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1497 | |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 1498 | if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) { |
John Stiles | 325c808 | 2021-05-17 14:49:05 -0400 | [diff] [blame] | 1499 | // We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various |
John Stiles | 12eb0ee | 2021-06-01 14:14:50 -0400 | [diff] [blame] | 1500 | // non-ES2 types to our symbol table to allow them to be tested. |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1501 | fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get()); |
| 1502 | fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get()); |
| 1503 | fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get()); |
| 1504 | fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get()); |
| 1505 | fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get()); |
| 1506 | fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get()); |
| 1507 | fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get()); |
| 1508 | fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get()); |
| 1509 | fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get()); |
| 1510 | |
| 1511 | fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get()); |
| 1512 | fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get()); |
| 1513 | fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get()); |
| 1514 | fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get()); |
| 1515 | fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get()); |
| 1516 | fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get()); |
| 1517 | |
| 1518 | fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get()); |
| 1519 | fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get()); |
| 1520 | fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get()); |
| 1521 | fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get()); |
| 1522 | fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get()); |
| 1523 | fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get()); |
John Stiles | 325c808 | 2021-05-17 14:49:05 -0400 | [diff] [blame] | 1524 | |
| 1525 | fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get()); |
| 1526 | fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get()); |
| 1527 | fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get()); |
| 1528 | fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get()); |
John Stiles | 2648716 | 2021-08-10 16:03:44 -0400 | [diff] [blame] | 1529 | |
| 1530 | fSymbolTable->addAlias("short", fContext.fTypes.fShort.get()); |
| 1531 | fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get()); |
| 1532 | fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get()); |
| 1533 | fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get()); |
| 1534 | |
| 1535 | fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get()); |
| 1536 | fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get()); |
| 1537 | fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get()); |
| 1538 | fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get()); |
John Stiles | aecf8d5 | 2021-05-14 12:15:01 -0400 | [diff] [blame] | 1539 | } |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1540 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1541 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1542 | IRGenerator::IRBundle IRGenerator::finish() { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1543 | // Variables defined in the pre-includes need their declaring elements added to the program |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1544 | if (!fIsBuiltinCode && fIntrinsics) { |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 1545 | this->findAndDeclareBuiltinVariables(); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1546 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1547 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1548 | // Do a pass looking for dangling FunctionReference or TypeReference expressions |
Brian Osman | c95d3a1 | 2020-09-09 10:56:27 -0400 | [diff] [blame] | 1549 | class FindIllegalExpressions : public ProgramVisitor { |
| 1550 | public: |
| 1551 | FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {} |
| 1552 | |
| 1553 | bool visitExpression(const Expression& e) override { |
| 1554 | fGenerator->checkValid(e); |
| 1555 | return INHERITED::visitExpression(e); |
| 1556 | } |
| 1557 | |
| 1558 | IRGenerator* fGenerator; |
| 1559 | using INHERITED = ProgramVisitor; |
| 1560 | using INHERITED::visitProgramElement; |
| 1561 | }; |
| 1562 | for (const auto& pe : *fProgramElements) { |
| 1563 | FindIllegalExpressions{this}.visitProgramElement(*pe); |
| 1564 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1565 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1566 | // If we're in ES2 mode (runtime effects), do a pass to enforce Appendix A, Section 5 of the |
| 1567 | // GLSL ES 1.00 spec -- Indexing. Don't bother if we've already found errors - this logic |
| 1568 | // assumes that all loops meet the criteria of Section 4, and if they don't, could crash. |
| 1569 | if (this->strictES2Mode() && this->errorReporter().errorCount() == 0) { |
| 1570 | for (const auto& pe : *fProgramElements) { |
| 1571 | Analysis::ValidateIndexingForES2(*pe, this->errorReporter()); |
| 1572 | } |
| 1573 | } |
| 1574 | |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 1575 | if (this->strictES2Mode()) { |
| 1576 | Analysis::DetectStaticRecursion(SkMakeSpan(*fProgramElements), this->errorReporter()); |
| 1577 | } |
| 1578 | |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1579 | return IRBundle{std::move(*fProgramElements), |
| 1580 | std::move(*fSharedElements), |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 1581 | std::move(fSymbolTable), |
| 1582 | fInputs}; |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | IRGenerator::IRBundle IRGenerator::convertProgram( |
| 1586 | const ParsedModule& base, |
| 1587 | bool isBuiltinCode, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 1588 | skstd::string_view text) { |
| 1589 | Parser parser(text, *fSymbolTable, this->errorReporter()); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1590 | fFile = parser.compilationUnit(); |
| 1591 | if (this->errorReporter().errorCount() == 0) { |
| 1592 | SkASSERT(fFile); |
| 1593 | for (const auto& decl : fFile->root()) { |
| 1594 | switch (decl.fKind) { |
| 1595 | case ASTNode::Kind::kVarDeclarations: |
| 1596 | this->convertGlobalVarDeclarations(decl); |
| 1597 | break; |
| 1598 | |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1599 | case ASTNode::Kind::kFunction: |
| 1600 | this->convertFunction(decl); |
| 1601 | break; |
| 1602 | |
| 1603 | case ASTNode::Kind::kModifiers: { |
| 1604 | std::unique_ptr<ModifiersDeclaration> f = |
| 1605 | this->convertModifiersDeclaration(decl); |
| 1606 | if (f) { |
| 1607 | fProgramElements->push_back(std::move(f)); |
| 1608 | } |
| 1609 | break; |
| 1610 | } |
| 1611 | case ASTNode::Kind::kInterfaceBlock: { |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1612 | std::unique_ptr<SkSL::InterfaceBlock> i = this->convertInterfaceBlock(decl); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1613 | if (i) { |
| 1614 | fProgramElements->push_back(std::move(i)); |
| 1615 | } |
| 1616 | break; |
| 1617 | } |
| 1618 | case ASTNode::Kind::kExtension: { |
| 1619 | std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1620 | decl.getStringView()); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1621 | if (e) { |
| 1622 | fProgramElements->push_back(std::move(e)); |
| 1623 | } |
| 1624 | break; |
| 1625 | } |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1626 | case ASTNode::Kind::kType: { |
| 1627 | std::unique_ptr<StructDefinition> s = this->convertStructDefinition(decl); |
| 1628 | if (s) { |
| 1629 | fProgramElements->push_back(std::move(s)); |
| 1630 | } |
| 1631 | break; |
| 1632 | } |
| 1633 | default: |
John Stiles | fbb905e | 2021-08-25 15:51:50 -0400 | [diff] [blame] | 1634 | SkDEBUGFAIL("unsupported declaration"); |
Ethan Nicholas | 8b3dd34 | 2021-03-23 12:32:56 -0400 | [diff] [blame] | 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | return this->finish(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1640 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1641 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1642 | } // namespace SkSL |