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