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