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 | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCompiler.h" |
| 17 | #include "src/sksl/SkSLParser.h" |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLUtil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 20 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 21 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 22 | #include "src/sksl/ir/SkSLConstructor.h" |
| 23 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 24 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 25 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 26 | #include "src/sksl/ir/SkSLEnum.h" |
| 27 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLField.h" |
| 31 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 32 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 33 | #include "src/sksl/ir/SkSLForStatement.h" |
| 34 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 35 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 36 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 37 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 38 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 39 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 40 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 41 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 42 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 43 | #include "src/sksl/ir/SkSLLayout.h" |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLNop.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 46 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 47 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 48 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 49 | #include "src/sksl/ir/SkSLSetting.h" |
| 50 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 51 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 52 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 53 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 54 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 55 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 56 | #include "src/sksl/ir/SkSLVariable.h" |
| 57 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 58 | #include "src/sksl/ir/SkSLWhileStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | |
| 60 | namespace SkSL { |
| 61 | |
| 62 | class AutoSymbolTable { |
| 63 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 64 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 65 | : fIR(ir) |
| 66 | , fPrevious(fIR->fSymbolTable) { |
| 67 | fIR->pushSymbolTable(); |
| 68 | } |
| 69 | |
| 70 | ~AutoSymbolTable() { |
| 71 | fIR->popSymbolTable(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 72 | SkASSERT(fPrevious == fIR->fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | IRGenerator* fIR; |
| 76 | std::shared_ptr<SymbolTable> fPrevious; |
| 77 | }; |
| 78 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 79 | class AutoLoopLevel { |
| 80 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 81 | AutoLoopLevel(IRGenerator* ir) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 82 | : fIR(ir) { |
| 83 | fIR->fLoopLevel++; |
| 84 | } |
| 85 | |
| 86 | ~AutoLoopLevel() { |
| 87 | fIR->fLoopLevel--; |
| 88 | } |
| 89 | |
| 90 | IRGenerator* fIR; |
| 91 | }; |
| 92 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 93 | class AutoSwitchLevel { |
| 94 | public: |
| 95 | AutoSwitchLevel(IRGenerator* ir) |
| 96 | : fIR(ir) { |
| 97 | fIR->fSwitchLevel++; |
| 98 | } |
| 99 | |
| 100 | ~AutoSwitchLevel() { |
| 101 | fIR->fSwitchLevel--; |
| 102 | } |
| 103 | |
| 104 | IRGenerator* fIR; |
| 105 | }; |
| 106 | |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 107 | class AutoDisableInline { |
| 108 | public: |
| 109 | AutoDisableInline(IRGenerator* ir, bool canInline = false) |
| 110 | : fIR(ir) { |
| 111 | fOldCanInline = ir->fCanInline; |
| 112 | fIR->fCanInline &= canInline; |
| 113 | } |
| 114 | |
| 115 | ~AutoDisableInline() { |
| 116 | fIR->fCanInline = fOldCanInline; |
| 117 | } |
| 118 | |
| 119 | IRGenerator* fIR; |
| 120 | bool fOldCanInline; |
| 121 | }; |
| 122 | |
| 123 | IRGenerator::IRGenerator(const Context* context, Inliner* inliner, ErrorReporter& errorReporter) |
John Stiles | 7b46300 | 2020-08-31 17:29:21 -0400 | [diff] [blame] | 124 | : fContext(*context) |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 125 | , fInliner(inliner) |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 126 | , fErrors(errorReporter) |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 127 | , fModifiers(new ModifiersPool()) { |
| 128 | SkASSERT(fInliner); |
| 129 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | |
| 131 | void IRGenerator::pushSymbolTable() { |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 132 | auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode); |
| 133 | fSymbolTable = std::move(childSymTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void IRGenerator::popSymbolTable() { |
| 137 | fSymbolTable = fSymbolTable->fParent; |
| 138 | } |
| 139 | |
John Stiles | 194b9b9 | 2020-09-15 15:37:24 -0400 | [diff] [blame] | 140 | static void fill_caps(const SkSL::ShaderCapsClass& caps, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 141 | std::unordered_map<String, Program::Settings::Value>* capsMap) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 142 | #define CAP(name) capsMap->insert({String(#name), Program::Settings::Value(caps.name())}) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 143 | CAP(fbFetchSupport); |
Brian Salomon | d401330 | 2018-04-04 13:58:33 +0000 | [diff] [blame] | 144 | CAP(fbFetchNeedsCustomOutput); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 145 | CAP(flatInterpolationSupport); |
| 146 | CAP(noperspectiveInterpolationSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 147 | CAP(externalTextureSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 148 | CAP(mustEnableAdvBlendEqs); |
| 149 | CAP(mustEnableSpecificAdvBlendEqs); |
| 150 | CAP(mustDeclareFragmentShaderOutput); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 151 | CAP(mustDoOpBetweenFloorAndAbs); |
Brian Salomon | f8c187c | 2019-12-19 14:41:57 -0500 | [diff] [blame] | 152 | CAP(mustGuardDivisionEvenAfterExplicitZeroCheck); |
| 153 | CAP(inBlendModesFailRandomlyForAllZeroVec); |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 154 | CAP(atan2ImplementedAsAtanYOverX); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 155 | CAP(canUseAnyFunctionInShader); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 156 | CAP(floatIs32Bits); |
Ethan Nicholas | 07990de | 2017-07-18 09:47:43 -0400 | [diff] [blame] | 157 | CAP(integerSupport); |
John Stiles | 6f3015a | 2020-10-08 14:55:36 -0400 | [diff] [blame] | 158 | CAP(builtinFMASupport); |
| 159 | CAP(builtinDeterminantSupport); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 160 | #undef CAP |
| 161 | } |
| 162 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 163 | std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, StringFragment name) { |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 164 | if (fKind != Program::kFragment_Kind && |
| 165 | fKind != Program::kVertex_Kind && |
| 166 | fKind != Program::kGeometry_Kind) { |
| 167 | fErrors.error(offset, "extensions are not allowed here"); |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 171 | return std::make_unique<Extension>(offset, name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 174 | std::unique_ptr<ModifiersPool> IRGenerator::releaseModifiers() { |
| 175 | std::unique_ptr<ModifiersPool> result = std::move(fModifiers); |
| 176 | fModifiers = std::make_unique<ModifiersPool>(); |
| 177 | return result; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 180 | std::unique_ptr<Statement> IRGenerator::convertSingleStatement(const ASTNode& statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | switch (statement.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 182 | case ASTNode::Kind::kBlock: |
| 183 | return this->convertBlock(statement); |
| 184 | case ASTNode::Kind::kVarDeclarations: |
| 185 | return this->convertVarDeclarationStatement(statement); |
| 186 | case ASTNode::Kind::kIf: |
| 187 | return this->convertIf(statement); |
| 188 | case ASTNode::Kind::kFor: |
| 189 | return this->convertFor(statement); |
| 190 | case ASTNode::Kind::kWhile: |
| 191 | return this->convertWhile(statement); |
| 192 | case ASTNode::Kind::kDo: |
| 193 | return this->convertDo(statement); |
| 194 | case ASTNode::Kind::kSwitch: |
| 195 | return this->convertSwitch(statement); |
| 196 | case ASTNode::Kind::kReturn: |
| 197 | return this->convertReturn(statement); |
| 198 | case ASTNode::Kind::kBreak: |
| 199 | return this->convertBreak(statement); |
| 200 | case ASTNode::Kind::kContinue: |
| 201 | return this->convertContinue(statement); |
| 202 | case ASTNode::Kind::kDiscard: |
| 203 | return this->convertDiscard(statement); |
| 204 | default: |
| 205 | // it's an expression |
| 206 | std::unique_ptr<Statement> result = this->convertExpressionStatement(statement); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 207 | if (fRTAdjust && fKind == Program::kGeometry_Kind) { |
| 208 | SkASSERT(result->kind() == Statement::Kind::kExpression); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 209 | Expression& expr = *result->as<ExpressionStatement>().expression(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 210 | if (expr.kind() == Expression::Kind::kFunctionCall) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 211 | FunctionCall& fc = expr.as<FunctionCall>(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 212 | if (fc.function().isBuiltin() && fc.function().name() == "EmitVertex") { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 213 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 214 | statements.reserve_back(2); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 215 | statements.push_back(getNormalizeSkPositionCode()); |
| 216 | statements.push_back(std::move(result)); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 217 | return std::make_unique<Block>(statement.fOffset, std::move(statements), |
| 218 | fSymbolTable); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 226 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 227 | StatementArray oldExtraStatements = std::move(fExtraStatements); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 228 | std::unique_ptr<Statement> result = this->convertSingleStatement(statement); |
| 229 | if (!result) { |
| 230 | fExtraStatements = std::move(oldExtraStatements); |
| 231 | return nullptr; |
| 232 | } |
| 233 | if (fExtraStatements.size()) { |
| 234 | fExtraStatements.push_back(std::move(result)); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 235 | auto block = std::make_unique<Block>(/*offset=*/-1, std::move(fExtraStatements), |
| 236 | /*symbols=*/nullptr, /*isScope=*/false); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 237 | fExtraStatements = std::move(oldExtraStatements); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 238 | return std::move(block); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 239 | } |
| 240 | fExtraStatements = std::move(oldExtraStatements); |
| 241 | return result; |
| 242 | } |
| 243 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 244 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) { |
| 245 | SkASSERT(block.fKind == ASTNode::Kind::kBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 246 | AutoSymbolTable table(this); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 247 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 248 | for (const auto& child : block) { |
| 249 | std::unique_ptr<Statement> statement = this->convertStatement(child); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 250 | if (!statement) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | statements.push_back(std::move(statement)); |
| 254 | } |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 255 | return std::make_unique<Block>(block.fOffset, std::move(statements), fSymbolTable); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 258 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) { |
| 259 | SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 260 | auto decls = this->convertVarDeclarations(s, Variable::Storage::kLocal); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 261 | if (decls.empty()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 262 | return nullptr; |
| 263 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 264 | if (decls.size() == 1) { |
| 265 | return std::move(decls.front()); |
| 266 | } else { |
| 267 | return std::make_unique<Block>(s.fOffset, std::move(decls), /*symbols=*/nullptr, |
| 268 | /*isScope=*/false); |
| 269 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 270 | } |
| 271 | |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 272 | StatementArray IRGenerator::convertVarDeclarations(const ASTNode& decls, |
| 273 | Variable::Storage storage) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 274 | SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 275 | auto declarationsIter = decls.begin(); |
| 276 | const Modifiers& modifiers = declarationsIter++->getModifiers(); |
| 277 | const ASTNode& rawType = *(declarationsIter++); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 278 | const Type* baseType = this->convertType(rawType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 279 | if (!baseType) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 280 | return {}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 281 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 282 | if (baseType->nonnullable() == *fContext.fFragmentProcessor_Type && |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 283 | storage != Variable::Storage::kGlobal) { |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 284 | fErrors.error(decls.fOffset, |
| 285 | "variables of type '" + baseType->displayName() + "' must be global"); |
| 286 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 287 | if (fKind != Program::kFragmentProcessor_Kind) { |
| 288 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 289 | baseType->typeKind() == Type::TypeKind::kMatrix) { |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 290 | fErrors.error(decls.fOffset, "'in' variables may not have matrix type"); |
| 291 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 292 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
| 293 | (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 294 | fErrors.error(decls.fOffset, |
| 295 | "'in uniform' variables only permitted within fragment processors"); |
| 296 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 297 | if (modifiers.fLayout.fWhen.fLength) { |
| 298 | fErrors.error(decls.fOffset, "'when' is only permitted within fragment processors"); |
| 299 | } |
| 300 | if (modifiers.fLayout.fFlags & Layout::kTracked_Flag) { |
| 301 | fErrors.error(decls.fOffset, "'tracked' is only permitted within fragment processors"); |
| 302 | } |
| 303 | if (modifiers.fLayout.fCType != Layout::CType::kDefault) { |
| 304 | fErrors.error(decls.fOffset, "'ctype' is only permitted within fragment processors"); |
| 305 | } |
| 306 | if (modifiers.fLayout.fKey) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 307 | fErrors.error(decls.fOffset, "'key' is only permitted within fragment processors"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 308 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 309 | } |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 310 | if (fKind == Program::kPipelineStage_Kind) { |
| 311 | if ((modifiers.fFlags & Modifiers::kIn_Flag) && |
| 312 | baseType->nonnullable() != *fContext.fFragmentProcessor_Type) { |
| 313 | fErrors.error(decls.fOffset, "'in' variables not permitted in runtime effects"); |
| 314 | } |
| 315 | } |
Brian Osman | 2fe83fe | 2019-12-16 13:17:59 -0500 | [diff] [blame] | 316 | if (modifiers.fLayout.fKey && (modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 317 | fErrors.error(decls.fOffset, "'key' is not permitted on 'uniform' variables"); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 318 | } |
Brian Osman | f59a961 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 319 | if (modifiers.fLayout.fMarker.fLength) { |
| 320 | if (fKind != Program::kPipelineStage_Kind) { |
| 321 | fErrors.error(decls.fOffset, "'marker' is only permitted in runtime effects"); |
| 322 | } |
| 323 | if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 324 | fErrors.error(decls.fOffset, "'marker' is only permitted on 'uniform' variables"); |
| 325 | } |
| 326 | if (*baseType != *fContext.fFloat4x4_Type) { |
| 327 | fErrors.error(decls.fOffset, "'marker' is only permitted on float4x4 variables"); |
| 328 | } |
| 329 | } |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 330 | if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) { |
| 331 | if (fKind != Program::kPipelineStage_Kind) { |
| 332 | fErrors.error(decls.fOffset, "'srgb_unpremul' is only permitted in runtime effects"); |
| 333 | } |
| 334 | if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) { |
| 335 | fErrors.error(decls.fOffset, |
| 336 | "'srgb_unpremul' is only permitted on 'uniform' variables"); |
| 337 | } |
| 338 | auto validColorXformType = [](const Type& t) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 339 | return t.typeKind() == Type::TypeKind::kVector && t.componentType().isFloat() && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 340 | (t.columns() == 3 || t.columns() == 4); |
| 341 | }; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 342 | if (!validColorXformType(*baseType) && !(baseType->typeKind() == Type::TypeKind::kArray && |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 343 | validColorXformType(baseType->componentType()))) { |
| 344 | fErrors.error(decls.fOffset, |
| 345 | "'srgb_unpremul' is only permitted on half3, half4, float3, or float4 " |
| 346 | "variables"); |
| 347 | } |
| 348 | } |
Brian Osman | 3c35842 | 2020-03-23 10:44:12 -0400 | [diff] [blame] | 349 | if (modifiers.fFlags & Modifiers::kVarying_Flag) { |
| 350 | if (fKind != Program::kPipelineStage_Kind) { |
| 351 | fErrors.error(decls.fOffset, "'varying' is only permitted in runtime effects"); |
| 352 | } |
| 353 | if (!baseType->isFloat() && |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 354 | !(baseType->typeKind() == Type::TypeKind::kVector && |
| 355 | baseType->componentType().isFloat())) { |
Brian Osman | 3c35842 | 2020-03-23 10:44:12 -0400 | [diff] [blame] | 356 | fErrors.error(decls.fOffset, "'varying' must be float scalar or vector"); |
| 357 | } |
| 358 | } |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 359 | int permitted = Modifiers::kConst_Flag; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 360 | if (storage == Variable::Storage::kGlobal) { |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 361 | permitted |= Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
| 362 | Modifiers::kFlat_Flag | Modifiers::kVarying_Flag | |
| 363 | Modifiers::kNoPerspective_Flag | Modifiers::kPLS_Flag | |
| 364 | Modifiers::kPLSIn_Flag | Modifiers::kPLSOut_Flag | |
| 365 | Modifiers::kRestrict_Flag | Modifiers::kVolatile_Flag | |
| 366 | Modifiers::kReadOnly_Flag | Modifiers::kWriteOnly_Flag | |
| 367 | Modifiers::kCoherent_Flag | Modifiers::kBuffer_Flag; |
| 368 | } |
| 369 | this->checkModifiers(decls.fOffset, modifiers, permitted); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 370 | |
| 371 | StatementArray varDecls; |
John Stiles | 9cef66f | 2020-10-23 09:46:11 -0400 | [diff] [blame] | 372 | bool firstDecl = true; |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 373 | for (; declarationsIter != decls.end(); ++declarationsIter) { |
John Stiles | 9cef66f | 2020-10-23 09:46:11 -0400 | [diff] [blame] | 374 | AutoDisableInline disableInline(this, /*canInline=*/firstDecl); |
| 375 | firstDecl = false; |
| 376 | |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 377 | const ASTNode& varDecl = *declarationsIter; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 378 | if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 && |
| 379 | (modifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind && |
| 380 | varDecl.getVarData().fName != "sk_FragColor") { |
| 381 | fErrors.error(varDecl.fOffset, |
Ethan Nicholas | 6c94271 | 2018-03-16 09:45:11 -0400 | [diff] [blame] | 382 | "out location=0, index=0 is reserved for sk_FragColor"); |
| 383 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 384 | const ASTNode::VarData& varData = varDecl.getVarData(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 385 | const Type* type = baseType; |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 386 | ExpressionArray sizes; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 387 | sizes.reserve_back(varData.fSizeCount); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 388 | auto iter = varDecl.begin(); |
| 389 | for (size_t i = 0; i < varData.fSizeCount; ++i, ++iter) { |
| 390 | const ASTNode& rawSize = *iter; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 391 | if (rawSize) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 392 | auto size = this->coerce(this->convertExpression(rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 393 | if (!size) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 394 | return {}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 395 | } |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 396 | String name(type->name()); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 397 | int64_t count; |
John Stiles | 84d503b | 2020-11-09 11:19:02 -0500 | [diff] [blame] | 398 | if (size->is<IntLiteral>()) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 399 | count = size->as<IntLiteral>().value(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 400 | if (count <= 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 401 | fErrors.error(size->fOffset, "array size must be positive"); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 402 | return {}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 403 | } |
| 404 | name += "[" + to_string(count) + "]"; |
| 405 | } else { |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 406 | fErrors.error(size->fOffset, "array size must be specified"); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 407 | return {}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 408 | } |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 409 | type = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 410 | std::make_unique<Type>(name, Type::TypeKind::kArray, *type, (int)count)); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 411 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | } else { |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 413 | type = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Type>( |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 414 | type->name() + "[]", Type::TypeKind::kArray, *type, Type::kUnsizedArray)); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 415 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 418 | auto var = std::make_unique<Variable>(varDecl.fOffset, fModifiers->handle(modifiers), |
| 419 | varData.fName, type, fIsBuiltinCode, storage); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 420 | if (var->name() == Compiler::RTADJUST_NAME) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 421 | SkASSERT(!fRTAdjust); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 422 | SkASSERT(var->type() == *fContext.fFloat4_Type); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 423 | fRTAdjust = var.get(); |
| 424 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 425 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 426 | if (iter != varDecl.end()) { |
| 427 | value = this->convertExpression(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 428 | if (!value) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 429 | return {}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 430 | } |
John Stiles | 84d503b | 2020-11-09 11:19:02 -0500 | [diff] [blame] | 431 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
| 432 | fErrors.error(value->fOffset, "'in' variables cannot use initializer expressions"); |
| 433 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 434 | value = this->coerce(std::move(value), *type); |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 435 | if (!value) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 436 | return {}; |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 437 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 438 | var->setInitialValue(value.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 439 | } |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 440 | const Symbol* symbol = (*fSymbolTable)[var->name()]; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 441 | if (symbol && storage == Variable::Storage::kGlobal && var->name() == "sk_FragColor") { |
John Stiles | ce591b7 | 2020-08-27 11:47:30 -0400 | [diff] [blame] | 442 | // Already defined, ignore. |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 443 | } else { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 444 | varDecls.push_back(std::make_unique<VarDeclaration>( |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 445 | var.get(), baseType, std::move(sizes), std::move(value))); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 446 | fSymbolTable->add(std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 447 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 448 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 449 | return varDecls; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 452 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) { |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 453 | if (fKind != Program::kFragment_Kind && |
| 454 | fKind != Program::kVertex_Kind && |
| 455 | fKind != Program::kGeometry_Kind) { |
| 456 | fErrors.error(m.fOffset, "layout qualifiers are not allowed here"); |
| 457 | return nullptr; |
| 458 | } |
| 459 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 460 | SkASSERT(m.fKind == ASTNode::Kind::kModifiers); |
| 461 | Modifiers modifiers = m.getModifiers(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 462 | if (modifiers.fLayout.fInvocations != -1) { |
Ethan Nicholas | f06576b | 2019-04-03 15:45:25 -0400 | [diff] [blame] | 463 | if (fKind != Program::kGeometry_Kind) { |
| 464 | fErrors.error(m.fOffset, "'invocations' is only legal in geometry shaders"); |
| 465 | return nullptr; |
| 466 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 467 | fInvocations = modifiers.fLayout.fInvocations; |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 468 | if (fCaps && !fCaps->gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 469 | modifiers.fLayout.fInvocations = -1; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 470 | if (modifiers.fLayout.description() == "") { |
| 471 | return nullptr; |
| 472 | } |
| 473 | } |
| 474 | } |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 475 | if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fCaps && |
| 476 | !fCaps->gsInvocationsSupport()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 477 | modifiers.fLayout.fMaxVertices *= fInvocations; |
| 478 | } |
Ethan Nicholas | 077050b | 2020-10-13 10:30:20 -0400 | [diff] [blame] | 479 | return std::make_unique<ModifiersDeclaration>(fModifiers->handle(modifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 480 | } |
| 481 | |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 482 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) { |
| 483 | SkASSERT(n.fKind == ASTNode::Kind::kIf); |
| 484 | auto iter = n.begin(); |
| 485 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
| 486 | *fContext.fBool_Type); |
| 487 | if (!test) { |
| 488 | return nullptr; |
| 489 | } |
| 490 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++)); |
| 491 | if (!ifTrue) { |
| 492 | return nullptr; |
| 493 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 494 | std::unique_ptr<Statement> ifFalse; |
| 495 | if (iter != n.end()) { |
| 496 | ifFalse = this->convertStatement(*(iter++)); |
| 497 | if (!ifFalse) { |
| 498 | return nullptr; |
| 499 | } |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 500 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 501 | if (test->kind() == Expression::Kind::kBoolLiteral) { |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 502 | // static boolean value, fold down to a single branch |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 503 | if (test->as<BoolLiteral>().value()) { |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 504 | return ifTrue; |
| 505 | } else if (ifFalse) { |
| 506 | return ifFalse; |
| 507 | } else { |
| 508 | // False & no else clause. Not an error, so don't return null! |
John Stiles | 2ff9706 | 2020-09-02 15:02:01 -0400 | [diff] [blame] | 509 | return std::make_unique<Nop>(); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 510 | } |
| 511 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 512 | auto ifStmt = std::make_unique<IfStatement>(n.fOffset, n.getBool(), std::move(test), |
| 513 | std::move(ifTrue), std::move(ifFalse)); |
| 514 | fInliner->ensureScopedBlocks(ifStmt->ifTrue().get(), ifStmt.get()); |
| 515 | fInliner->ensureScopedBlocks(ifStmt->ifFalse().get(), ifStmt.get()); |
| 516 | return std::move(ifStmt); |
John Stiles | ad2319f | 2020-09-02 15:01:47 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 519 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) { |
| 520 | SkASSERT(f.fKind == ASTNode::Kind::kFor); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 521 | AutoLoopLevel level(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 523 | std::unique_ptr<Statement> initializer; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 524 | auto iter = f.begin(); |
| 525 | if (*iter) { |
| 526 | initializer = this->convertStatement(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 527 | if (!initializer) { |
| 528 | return nullptr; |
| 529 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 530 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 531 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 532 | std::unique_ptr<Expression> test; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 533 | if (*iter) { |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 534 | AutoDisableInline disableInline(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 535 | test = this->coerce(this->convertExpression(*iter), *fContext.fBool_Type); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 536 | if (!test) { |
| 537 | return nullptr; |
| 538 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 539 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 540 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 541 | ++iter; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 542 | std::unique_ptr<Expression> next; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 543 | if (*iter) { |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 544 | AutoDisableInline disableInline(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 545 | next = this->convertExpression(*iter); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 546 | if (!next) { |
| 547 | return nullptr; |
| 548 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 549 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 550 | ++iter; |
| 551 | std::unique_ptr<Statement> statement = this->convertStatement(*iter); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 552 | if (!statement) { |
| 553 | return nullptr; |
| 554 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 555 | auto forStmt = std::make_unique<ForStatement>(f.fOffset, std::move(initializer), |
| 556 | std::move(test), std::move(next), |
| 557 | std::move(statement), fSymbolTable); |
| 558 | fInliner->ensureScopedBlocks(forStmt->statement().get(), forStmt.get()); |
| 559 | return std::move(forStmt); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 562 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) { |
| 563 | SkASSERT(w.fKind == ASTNode::Kind::kWhile); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 564 | AutoLoopLevel level(this); |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 565 | std::unique_ptr<Expression> test; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 566 | auto iter = w.begin(); |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 567 | { |
| 568 | AutoDisableInline disableInline(this); |
| 569 | test = this->coerce(this->convertExpression(*(iter++)), *fContext.fBool_Type); |
| 570 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 571 | if (!test) { |
| 572 | return nullptr; |
| 573 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 574 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 575 | if (!statement) { |
| 576 | return nullptr; |
| 577 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 578 | auto whileStmt = std::make_unique<WhileStatement>(w.fOffset, std::move(test), |
| 579 | std::move(statement)); |
| 580 | fInliner->ensureScopedBlocks(whileStmt->statement().get(), whileStmt.get()); |
| 581 | return std::move(whileStmt); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 584 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) { |
| 585 | SkASSERT(d.fKind == ASTNode::Kind::kDo); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 586 | AutoLoopLevel level(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 587 | auto iter = d.begin(); |
| 588 | std::unique_ptr<Statement> statement = this->convertStatement(*(iter++)); |
| 589 | if (!statement) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 590 | return nullptr; |
| 591 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 592 | std::unique_ptr<Expression> test; |
| 593 | { |
| 594 | AutoDisableInline disableInline(this); |
| 595 | test = this->coerce(this->convertExpression(*(iter++)), *fContext.fBool_Type); |
| 596 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 597 | if (!test) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 598 | return nullptr; |
| 599 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 600 | auto doStmt = std::make_unique<DoStatement>(d.fOffset, std::move(statement), std::move(test)); |
| 601 | fInliner->ensureScopedBlocks(doStmt->statement().get(), doStmt.get()); |
| 602 | return std::move(doStmt); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 605 | std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) { |
| 606 | SkASSERT(s.fKind == ASTNode::Kind::kSwitch); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 607 | AutoSwitchLevel level(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 608 | auto iter = s.begin(); |
| 609 | std::unique_ptr<Expression> value = this->convertExpression(*(iter++)); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 610 | if (!value) { |
| 611 | return nullptr; |
| 612 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 613 | if (value->type() != *fContext.fUInt_Type && |
| 614 | value->type().typeKind() != Type::TypeKind::kEnum) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 615 | value = this->coerce(std::move(value), *fContext.fInt_Type); |
| 616 | if (!value) { |
| 617 | return nullptr; |
| 618 | } |
| 619 | } |
| 620 | AutoSymbolTable table(this); |
| 621 | std::unordered_set<int> caseValues; |
| 622 | std::vector<std::unique_ptr<SwitchCase>> cases; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 623 | for (; iter != s.end(); ++iter) { |
| 624 | const ASTNode& c = *iter; |
| 625 | SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 626 | std::unique_ptr<Expression> caseValue; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 627 | auto childIter = c.begin(); |
| 628 | if (*childIter) { |
| 629 | caseValue = this->convertExpression(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 630 | if (!caseValue) { |
| 631 | return nullptr; |
| 632 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 633 | caseValue = this->coerce(std::move(caseValue), value->type()); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 634 | if (!caseValue) { |
| 635 | return nullptr; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 636 | } |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 637 | int64_t v = 0; |
| 638 | if (!this->getConstantInt(*caseValue, &v)) { |
| 639 | fErrors.error(caseValue->fOffset, "case value must be a constant integer"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 640 | return nullptr; |
| 641 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 642 | if (caseValues.find(v) != caseValues.end()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 643 | fErrors.error(caseValue->fOffset, "duplicate case value"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 644 | } |
| 645 | caseValues.insert(v); |
| 646 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 647 | ++childIter; |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 648 | StatementArray statements; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 649 | for (; childIter != c.end(); ++childIter) { |
| 650 | std::unique_ptr<Statement> converted = this->convertStatement(*childIter); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 651 | if (!converted) { |
| 652 | return nullptr; |
| 653 | } |
| 654 | statements.push_back(std::move(converted)); |
| 655 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 656 | cases.push_back(std::make_unique<SwitchCase>(c.fOffset, std::move(caseValue), |
| 657 | std::move(statements))); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 658 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 659 | return std::make_unique<SwitchStatement>(s.fOffset, s.getBool(), std::move(value), |
| 660 | std::move(cases), fSymbolTable); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 661 | } |
| 662 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 663 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) { |
| 664 | std::unique_ptr<Expression> e = this->convertExpression(s); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 665 | if (!e) { |
| 666 | return nullptr; |
| 667 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 668 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 669 | } |
| 670 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 671 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) { |
| 672 | SkASSERT(r.fKind == ASTNode::Kind::kReturn); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 673 | SkASSERT(fCurrentFunction); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 674 | // early returns from a vertex main function will bypass the sk_Position normalization, so |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 675 | // SkASSERT that we aren't doing that. It is of course possible to fix this by adding a |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 676 | // normalization before each return, but it will probably never actually be necessary. |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 677 | SkASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->name()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 678 | if (r.begin() != r.end()) { |
| 679 | std::unique_ptr<Expression> result = this->convertExpression(*r.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 680 | if (!result) { |
| 681 | return nullptr; |
| 682 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 683 | if (fCurrentFunction->returnType() == *fContext.fVoid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 684 | fErrors.error(result->fOffset, "may not return a value from a void function"); |
Brian Osman | 5eea6ae | 2020-09-09 16:05:18 -0400 | [diff] [blame] | 685 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 686 | } else { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 687 | result = this->coerce(std::move(result), fCurrentFunction->returnType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 688 | if (!result) { |
| 689 | return nullptr; |
| 690 | } |
| 691 | } |
| 692 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 693 | } else { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 694 | if (fCurrentFunction->returnType() != *fContext.fVoid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 695 | fErrors.error(r.fOffset, "expected function to return '" + |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 696 | fCurrentFunction->returnType().displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 697 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 698 | return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 702 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) { |
| 703 | SkASSERT(b.fKind == ASTNode::Kind::kBreak); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 704 | if (fLoopLevel > 0 || fSwitchLevel > 0) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 705 | return std::make_unique<BreakStatement>(b.fOffset); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 706 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 707 | fErrors.error(b.fOffset, "break statement must be inside a loop or switch"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 708 | return nullptr; |
| 709 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 712 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) { |
| 713 | SkASSERT(c.fKind == ASTNode::Kind::kContinue); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 714 | if (fLoopLevel > 0) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 715 | return std::make_unique<ContinueStatement>(c.fOffset); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 716 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 717 | fErrors.error(c.fOffset, "continue statement must be inside a loop"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 718 | return nullptr; |
| 719 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 722 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) { |
| 723 | SkASSERT(d.fKind == ASTNode::Kind::kDiscard); |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 724 | return std::make_unique<DiscardStatement>(d.fOffset); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 727 | std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 728 | Layout invokeLayout; |
| 729 | Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 730 | const FunctionDeclaration* invokeDecl = |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 731 | fSymbolTable->add(std::make_unique<FunctionDeclaration>( |
| 732 | /*offset=*/-1, |
| 733 | fModifiers->handle(invokeModifiers), |
| 734 | "_invoke", |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 735 | std::vector<const Variable*>(), |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 736 | fContext.fVoid_Type.get(), |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 737 | fIsBuiltinCode)); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 738 | fProgramElements->push_back(std::make_unique<FunctionDefinition>(/*offset=*/-1, |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 739 | invokeDecl, fIsBuiltinCode, |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 740 | std::move(main))); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 741 | |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 742 | std::vector<std::unique_ptr<VarDeclaration>> variables; |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 743 | const Variable* loopIdx = &(*fSymbolTable)["sk_InvocationID"]->as<Variable>(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 744 | auto test = std::make_unique<BinaryExpression>(/*offset=*/-1, |
| 745 | std::make_unique<VariableReference>(/*offset=*/-1, loopIdx), |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 746 | Token::Kind::TK_LT, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 747 | std::make_unique<IntLiteral>(fContext, /*offset=*/-1, fInvocations), |
| 748 | fContext.fBool_Type.get()); |
| 749 | auto next = std::make_unique<PostfixExpression>( |
| 750 | std::make_unique<VariableReference>(/*offset=*/-1, loopIdx, |
| 751 | VariableReference::RefKind::kReadWrite), |
| 752 | Token::Kind::TK_PLUSPLUS); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 753 | ASTNode endPrimitiveID(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, "EndPrimitive"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 754 | std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 755 | SkASSERT(endPrimitive); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 756 | |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 757 | StatementArray loopBody; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 758 | loopBody.reserve_back(2); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 759 | loopBody.push_back(std::make_unique<ExpressionStatement>(this->call( |
| 760 | /*offset=*/-1, *invokeDecl, |
| 761 | ExpressionArray{}))); |
| 762 | loopBody.push_back(std::make_unique<ExpressionStatement>(this->call( |
| 763 | /*offset=*/-1, std::move(endPrimitive), |
| 764 | ExpressionArray{}))); |
| 765 | auto assignment = std::make_unique<BinaryExpression>(/*offset=*/-1, |
| 766 | std::make_unique<VariableReference>(/*offset=*/-1, loopIdx, |
| 767 | VariableReference::RefKind::kWrite), |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 768 | Token::Kind::TK_EQ, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 769 | std::make_unique<IntLiteral>(fContext, /*offset=*/-1, /*value=*/0), |
| 770 | fContext.fInt_Type.get()); |
| 771 | auto initializer = std::make_unique<ExpressionStatement>(std::move(assignment)); |
| 772 | auto loop = std::make_unique<ForStatement>(/*offset=*/-1, |
| 773 | std::move(initializer), |
| 774 | std::move(test), std::move(next), |
| 775 | std::make_unique<Block>(-1, std::move(loopBody)), |
| 776 | fSymbolTable); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 777 | StatementArray children; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 778 | children.push_back(std::move(loop)); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 779 | return std::make_unique<Block>(-1, std::move(children)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 780 | } |
| 781 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 782 | std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 783 | const Variable* skPerVertex = nullptr; |
| 784 | if (const ProgramElement* perVertexDecl = fIntrinsics->find(Compiler::PERVERTEX_NAME)) { |
| 785 | SkASSERT(perVertexDecl->is<InterfaceBlock>()); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 786 | skPerVertex = &perVertexDecl->as<InterfaceBlock>().variable(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 787 | } |
| 788 | |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 789 | // sk_Position = float4(sk_Position.xy * rtAdjust.xz + sk_Position.ww * rtAdjust.yw, |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 790 | // 0, |
| 791 | // sk_Position.w); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 792 | SkASSERT(skPerVertex && fRTAdjust); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 793 | #define REF(var) std::unique_ptr<Expression>(\ |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 794 | new VariableReference(-1, var, VariableReference::RefKind::kRead)) |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 795 | #define WREF(var) std::unique_ptr<Expression>(\ |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 796 | new VariableReference(-1, var, VariableReference::RefKind::kWrite)) |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 797 | #define FIELD(var, idx) std::unique_ptr<Expression>(\ |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 798 | new FieldAccess(REF(var), idx, FieldAccess::OwnerKind::kAnonymousInterfaceBlock)) |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 799 | #define POS std::unique_ptr<Expression>(new FieldAccess(WREF(skPerVertex), 0, \ |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 800 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock)) |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 801 | #define ADJUST (fRTAdjustInterfaceBlock ? \ |
| 802 | FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \ |
| 803 | REF(fRTAdjust)) |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 804 | #define SWIZZLE(expr, comp) std::unique_ptr<Expression>( \ |
| 805 | new Swizzle(fContext, expr, ComponentArray(comp, SK_ARRAY_COUNT(comp)))) |
Ethan Nicholas | b809efb | 2018-04-12 14:39:21 -0400 | [diff] [blame] | 806 | #define OP(left, op, right) std::unique_ptr<Expression>( \ |
| 807 | new BinaryExpression(-1, left, op, right, \ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 808 | fContext.fFloat2_Type.get())) |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 809 | |
| 810 | static constexpr int8_t kXYIndices[] = {0, 1}; |
| 811 | static constexpr int8_t kXZIndices[] = {0, 2}; |
| 812 | static constexpr int8_t kYWIndices[] = {1, 3}; |
| 813 | static constexpr int8_t kWWIndices[] = {3, 3}; |
| 814 | static constexpr int8_t kWIndex[] = {3}; |
| 815 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 816 | ExpressionArray children; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 817 | children.reserve_back(3); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 818 | children.push_back( |
| 819 | OP(OP(SWIZZLE(POS, kXYIndices), Token::Kind::TK_STAR, SWIZZLE(ADJUST, kXZIndices)), |
| 820 | Token::Kind::TK_PLUS, |
| 821 | OP(SWIZZLE(POS, kWWIndices), Token::Kind::TK_STAR, SWIZZLE(ADJUST, kYWIndices)))); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 822 | children.push_back(std::make_unique<FloatLiteral>(fContext, /*offset=*/-1, /*value=*/0.0)); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 823 | children.push_back(SWIZZLE(POS, kWIndex)); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 824 | std::unique_ptr<Expression> result = OP(POS, Token::Kind::TK_EQ, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 825 | std::make_unique<Constructor>(/*offset=*/-1, |
| 826 | fContext.fFloat4_Type.get(), |
| 827 | std::move(children))); |
| 828 | return std::make_unique<ExpressionStatement>(std::move(result)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 831 | template<typename T> |
| 832 | class AutoClear { |
| 833 | public: |
| 834 | AutoClear(T* container) |
| 835 | : fContainer(container) { |
| 836 | SkASSERT(container->empty()); |
| 837 | } |
| 838 | |
| 839 | ~AutoClear() { |
| 840 | fContainer->clear(); |
| 841 | } |
| 842 | |
| 843 | private: |
| 844 | T* fContainer; |
| 845 | }; |
| 846 | |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 847 | template <typename T> AutoClear(T* c) -> AutoClear<T>; |
| 848 | |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 849 | void IRGenerator::checkModifiers(int offset, const Modifiers& modifiers, int permitted) { |
| 850 | int flags = modifiers.fFlags; |
| 851 | #define CHECK(flag, name) \ |
| 852 | if (!flags) return; \ |
| 853 | if (flags & flag) { \ |
| 854 | if (!(permitted & flag)) { \ |
| 855 | fErrors.error(offset, "'" name "' is not permitted here"); \ |
| 856 | } \ |
| 857 | flags &= ~flag; \ |
| 858 | } |
| 859 | CHECK(Modifiers::kConst_Flag, "const") |
| 860 | CHECK(Modifiers::kIn_Flag, "in") |
| 861 | CHECK(Modifiers::kOut_Flag, "out") |
| 862 | CHECK(Modifiers::kUniform_Flag, "uniform") |
| 863 | CHECK(Modifiers::kFlat_Flag, "flat") |
| 864 | CHECK(Modifiers::kNoPerspective_Flag, "noperspective") |
| 865 | CHECK(Modifiers::kReadOnly_Flag, "readonly") |
| 866 | CHECK(Modifiers::kWriteOnly_Flag, "writeonly") |
| 867 | CHECK(Modifiers::kCoherent_Flag, "coherent") |
| 868 | CHECK(Modifiers::kVolatile_Flag, "volatile") |
| 869 | CHECK(Modifiers::kRestrict_Flag, "restrict") |
| 870 | CHECK(Modifiers::kBuffer_Flag, "buffer") |
| 871 | CHECK(Modifiers::kHasSideEffects_Flag, "sk_has_side_effects") |
| 872 | CHECK(Modifiers::kPLS_Flag, "__pixel_localEXT") |
| 873 | CHECK(Modifiers::kPLSIn_Flag, "__pixel_local_inEXT") |
| 874 | CHECK(Modifiers::kPLSOut_Flag, "__pixel_local_outEXT") |
| 875 | CHECK(Modifiers::kVarying_Flag, "varying") |
Ethan Nicholas | f3c8f5d | 2020-08-20 13:09:14 +0000 | [diff] [blame] | 876 | CHECK(Modifiers::kInline_Flag, "inline") |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 877 | SkASSERT(flags == 0); |
| 878 | } |
| 879 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 880 | void IRGenerator::convertFunction(const ASTNode& f) { |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 881 | AutoClear clear(&fReferencedIntrinsics); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 882 | auto iter = f.begin(); |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 883 | const Type* returnType = this->convertType(*(iter++), /*allowVoid=*/true); |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 884 | if (returnType == nullptr) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 885 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 886 | } |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 887 | auto type_is_allowed = [&](const Type* t) { |
| 888 | #if defined(SKSL_STANDALONE) |
| 889 | return true; |
| 890 | #else |
| 891 | GrSLType unusedSLType; |
| 892 | return fKind != Program::kPipelineStage_Kind || |
| 893 | type_to_grsltype(fContext, *t, &unusedSLType); |
| 894 | #endif |
| 895 | }; |
| 896 | if (returnType->nonnullable() == *fContext.fFragmentProcessor_Type || |
| 897 | !type_is_allowed(returnType)) { |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 898 | fErrors.error(f.fOffset, |
| 899 | "functions may not return type '" + returnType->displayName() + "'"); |
| 900 | return; |
| 901 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 902 | const ASTNode::FunctionData& funcData = f.getFunctionData(); |
| 903 | this->checkModifiers(f.fOffset, funcData.fModifiers, Modifiers::kHasSideEffects_Flag | |
| 904 | Modifiers::kInline_Flag); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 905 | std::vector<const Variable*> parameters; |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 906 | for (size_t i = 0; i < funcData.fParameterCount; ++i) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 907 | const ASTNode& param = *(iter++); |
| 908 | SkASSERT(param.fKind == ASTNode::Kind::kParameter); |
| 909 | ASTNode::ParameterData pd = param.getParameterData(); |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 910 | this->checkModifiers(param.fOffset, pd.fModifiers, Modifiers::kIn_Flag | |
| 911 | Modifiers::kOut_Flag); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 912 | auto paramIter = param.begin(); |
| 913 | const Type* type = this->convertType(*(paramIter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 914 | if (!type) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 915 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 916 | } |
John Stiles | 7f7b485 | 2020-11-10 11:25:35 -0500 | [diff] [blame^] | 917 | for (int j = 1; j <= (int) pd.fSizeCount; j++) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 918 | int size = (param.begin() + j)->getInt(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 919 | String name = type->name() + "[" + to_string(size) + "]"; |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 920 | type = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 921 | std::make_unique<Type>(std::move(name), Type::TypeKind::kArray, *type, size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 922 | } |
Brian Osman | 3000d6b | 2020-07-31 15:57:28 -0400 | [diff] [blame] | 923 | // Only the (builtin) declarations of 'sample' are allowed to have FP parameters |
| 924 | if ((type->nonnullable() == *fContext.fFragmentProcessor_Type && !fIsBuiltinCode) || |
| 925 | !type_is_allowed(type)) { |
| 926 | fErrors.error(param.fOffset, |
| 927 | "parameters of type '" + type->displayName() + "' not allowed"); |
| 928 | return; |
| 929 | } |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 930 | |
| 931 | Modifiers m = pd.fModifiers; |
| 932 | if (funcData.fName == "main" && (fKind == Program::kPipelineStage_Kind || |
| 933 | fKind == Program::kFragmentProcessor_Kind)) { |
| 934 | if (i == 0) { |
| 935 | // We verify that the type is correct later, for now, if there is a parameter to |
| 936 | // a .fp or runtime-effect main(), it's supposed to be the coords: |
| 937 | m.fLayout.fBuiltin = SK_MAIN_COORDS_BUILTIN; |
| 938 | } |
| 939 | } |
| 940 | |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 941 | const Variable* var = fSymbolTable->takeOwnershipOfSymbol( |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 942 | std::make_unique<Variable>(param.fOffset, fModifiers->handle(m), pd.fName, type, |
| 943 | fIsBuiltinCode, Variable::Storage::kParameter)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 944 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 947 | auto paramIsCoords = [&](int idx) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 948 | return parameters[idx]->type() == *fContext.fFloat2_Type && |
Brian Osman | 8dbdf23 | 2020-10-12 14:40:24 -0400 | [diff] [blame] | 949 | parameters[idx]->modifiers().fFlags == 0 && |
| 950 | parameters[idx]->modifiers().fLayout.fBuiltin == SK_MAIN_COORDS_BUILTIN; |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 951 | }; |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 952 | |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 953 | if (funcData.fName == "main") { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 954 | switch (fKind) { |
| 955 | case Program::kPipelineStage_Kind: { |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 956 | // (half4|float4) main() -or- (half4|float4) main(float2) |
| 957 | if (*returnType != *fContext.fHalf4_Type && *returnType != *fContext.fFloat4_Type) { |
| 958 | fErrors.error(f.fOffset, "'main' must return: 'vec4', 'float4', or 'half4'"); |
| 959 | return; |
| 960 | } |
| 961 | if (!(parameters.size() == 0 || (parameters.size() == 1 && paramIsCoords(0)))) { |
| 962 | fErrors.error(f.fOffset, "'main' parameters must be: (), (vec2), or (float2)"); |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | break; |
Brian Osman | 44820a9 | 2020-08-26 09:27:39 -0400 | [diff] [blame] | 966 | } |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 967 | case Program::kFragmentProcessor_Kind: { |
Brian Osman | 44820a9 | 2020-08-26 09:27:39 -0400 | [diff] [blame] | 968 | bool valid = (parameters.size() == 0) || |
| 969 | (parameters.size() == 1 && paramIsCoords(0)); |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 970 | if (!valid) { |
| 971 | fErrors.error(f.fOffset, ".fp 'main' must be declared main() or main(float2)"); |
| 972 | return; |
| 973 | } |
| 974 | break; |
| 975 | } |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 976 | case Program::kGeneric_Kind: |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 977 | break; |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 978 | default: |
| 979 | if (parameters.size()) { |
| 980 | fErrors.error(f.fOffset, "shader 'main' must have zero parameters"); |
| 981 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 985 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 986 | const FunctionDeclaration* decl = nullptr; |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 987 | const Symbol* entry = (*fSymbolTable)[funcData.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 988 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 989 | std::vector<const FunctionDeclaration*> functions; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 990 | switch (entry->kind()) { |
| 991 | case Symbol::Kind::kUnresolvedFunction: |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 992 | functions = entry->as<UnresolvedFunction>().functions(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 994 | case Symbol::Kind::kFunctionDeclaration: |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 995 | functions.push_back(&entry->as<FunctionDeclaration>()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 996 | break; |
| 997 | default: |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 998 | fErrors.error(f.fOffset, "symbol '" + funcData.fName + "' was already defined"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 999 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1000 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1001 | for (const FunctionDeclaration* other : functions) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1002 | SkASSERT(other->name() == funcData.fName); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1003 | if (parameters.size() == other->parameters().size()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1004 | bool match = true; |
| 1005 | for (size_t i = 0; i < parameters.size(); i++) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1006 | if (parameters[i]->type() != other->parameters()[i]->type()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1007 | match = false; |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | if (match) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1012 | if (*returnType != other->returnType()) { |
| 1013 | FunctionDeclaration newDecl(f.fOffset, |
| 1014 | fModifiers->handle(funcData.fModifiers), |
| 1015 | funcData.fName, |
| 1016 | parameters, |
| 1017 | returnType, |
| 1018 | fIsBuiltinCode); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1019 | fErrors.error(f.fOffset, "functions '" + newDecl.description() + |
| 1020 | "' and '" + other->description() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1021 | "' differ only in return type"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1022 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1023 | } |
| 1024 | decl = other; |
| 1025 | for (size_t i = 0; i < parameters.size(); i++) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1026 | if (parameters[i]->modifiers() != other->parameters()[i]->modifiers()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1027 | fErrors.error(f.fOffset, "modifiers on parameter " + |
| 1028 | to_string((uint64_t) i + 1) + |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1029 | " differ between declaration and definition"); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1030 | return; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1031 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1032 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1033 | if (other->definition() && !other->isBuiltin()) { |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1034 | fErrors.error(f.fOffset, "duplicate definition of " + other->description()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1035 | } |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | if (!decl) { |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1042 | // Conservatively assume all user-defined functions have side effects. |
| 1043 | Modifiers declModifiers = funcData.fModifiers; |
| 1044 | if (!fIsBuiltinCode) { |
| 1045 | declModifiers.fFlags |= Modifiers::kHasSideEffects_Flag; |
| 1046 | } |
| 1047 | |
| 1048 | // Create a new declaration. |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1049 | decl = fSymbolTable->add(std::make_unique<FunctionDeclaration>( |
| 1050 | f.fOffset, |
| 1051 | fModifiers->handle(declModifiers), |
| 1052 | funcData.fName, |
| 1053 | parameters, |
| 1054 | returnType, |
| 1055 | fIsBuiltinCode)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1056 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1057 | if (iter == f.end()) { |
| 1058 | // If there's no body, we've found a prototype. |
| 1059 | fProgramElements->push_back(std::make_unique<FunctionPrototype>(f.fOffset, decl, |
| 1060 | fIsBuiltinCode)); |
| 1061 | } else { |
| 1062 | // Compile function body. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1063 | SkASSERT(!fCurrentFunction); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1064 | fCurrentFunction = decl; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1065 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1066 | AutoSymbolTable table(this); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1067 | for (const Variable* param : decl->parameters()) { |
| 1068 | fSymbolTable->addWithoutOwnership(param); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1069 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1070 | bool needInvocationIDWorkaround = fInvocations != -1 && funcData.fName == "main" && |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 1071 | fCaps && !fCaps->gsInvocationsSupport(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1072 | std::unique_ptr<Block> body = this->convertBlock(*iter); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1073 | fCurrentFunction = nullptr; |
| 1074 | if (!body) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1075 | return; |
| 1076 | } |
| 1077 | if (needInvocationIDWorkaround) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1078 | body = this->applyInvocationIDWorkaround(std::move(body)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1079 | } |
John Stiles | b9af723 | 2020-08-20 15:57:48 -0400 | [diff] [blame] | 1080 | if (Program::kVertex_Kind == fKind && funcData.fName == "main" && fRTAdjust) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1081 | body->children().push_back(this->getNormalizeSkPositionCode()); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1082 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 1083 | auto result = std::make_unique<FunctionDefinition>( |
| 1084 | f.fOffset, decl, fIsBuiltinCode, std::move(body), std::move(fReferencedIntrinsics)); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1085 | decl->setDefinition(result.get()); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1086 | result->setSource(&f); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1087 | fProgramElements->push_back(std::move(result)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1088 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1091 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) { |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 1092 | if (fKind != Program::kFragment_Kind && |
| 1093 | fKind != Program::kVertex_Kind && |
| 1094 | fKind != Program::kGeometry_Kind) { |
| 1095 | fErrors.error(intf.fOffset, "interface block is not allowed here"); |
| 1096 | return nullptr; |
| 1097 | } |
| 1098 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1099 | SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock); |
| 1100 | ASTNode::InterfaceBlockData id = intf.getInterfaceBlockData(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1101 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1102 | std::shared_ptr<SymbolTable> symbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1103 | std::vector<Type::Field> fields; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1104 | bool foundRTAdjust = false; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1105 | auto iter = intf.begin(); |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1106 | { |
| 1107 | AutoSymbolTable table(this); |
| 1108 | symbols = fSymbolTable; |
| 1109 | bool haveRuntimeArray = false; |
| 1110 | for (size_t i = 0; i < id.fDeclarationCount; ++i) { |
| 1111 | StatementArray decls = this->convertVarDeclarations(*(iter++), |
| 1112 | Variable::Storage::kInterfaceBlock); |
| 1113 | if (decls.empty()) { |
| 1114 | return nullptr; |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 1115 | } |
John Stiles | 869cdef | 2020-10-30 14:24:24 -0400 | [diff] [blame] | 1116 | for (const auto& decl : decls) { |
| 1117 | const VarDeclaration& vd = decl->as<VarDeclaration>(); |
| 1118 | if (haveRuntimeArray) { |
| 1119 | fErrors.error(decl->fOffset, |
| 1120 | "only the last entry in an interface block may be a runtime-sized " |
| 1121 | "array"); |
| 1122 | } |
| 1123 | if (&vd.var() == fRTAdjust) { |
| 1124 | foundRTAdjust = true; |
| 1125 | SkASSERT(vd.var().type() == *fContext.fFloat4_Type); |
| 1126 | fRTAdjustFieldIndex = fields.size(); |
| 1127 | } |
| 1128 | fields.push_back(Type::Field(vd.var().modifiers(), vd.var().name(), |
| 1129 | &vd.var().type())); |
| 1130 | if (vd.value()) { |
| 1131 | fErrors.error(decl->fOffset, |
| 1132 | "initializers are not permitted on interface block fields"); |
| 1133 | } |
| 1134 | if (vd.var().type().typeKind() == Type::TypeKind::kArray && |
| 1135 | vd.var().type().columns() == Type::kUnsizedArray) { |
| 1136 | haveRuntimeArray = true; |
| 1137 | } |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 1138 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1139 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1140 | } |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1141 | const Type* type = |
| 1142 | old->takeOwnershipOfSymbol(std::make_unique<Type>(intf.fOffset, id.fTypeName, fields)); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 1143 | ExpressionArray sizes; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 1144 | sizes.reserve_back(id.fSizeCount); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1145 | for (size_t i = 0; i < id.fSizeCount; ++i) { |
| 1146 | const ASTNode& size = *(iter++); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1147 | if (size) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1148 | std::unique_ptr<Expression> converted = this->convertExpression(size); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1149 | if (!converted) { |
| 1150 | return nullptr; |
| 1151 | } |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1152 | String name = type->name(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1153 | int64_t count; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1154 | if (converted->kind() == Expression::Kind::kIntLiteral) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 1155 | count = converted->as<IntLiteral>().value(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1156 | if (count <= 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1157 | fErrors.error(converted->fOffset, "array size must be positive"); |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 1158 | return nullptr; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1159 | } |
| 1160 | name += "[" + to_string(count) + "]"; |
| 1161 | } else { |
Ethan Nicholas | 66d8006 | 2019-09-09 14:50:51 -0400 | [diff] [blame] | 1162 | fErrors.error(intf.fOffset, "array size must be specified"); |
| 1163 | return nullptr; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1164 | } |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1165 | type = symbols->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1166 | std::make_unique<Type>(name, Type::TypeKind::kArray, *type, (int)count)); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1167 | sizes.push_back(std::move(converted)); |
| 1168 | } else { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1169 | String name = String(type->name()) + "[]"; |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 1170 | type = symbols->takeOwnershipOfSymbol(std::make_unique<Type>( |
| 1171 | name, Type::TypeKind::kArray, *type, Type::kUnsizedArray)); |
| 1172 | sizes.push_back(nullptr); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1173 | } |
| 1174 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 1175 | const Variable* var = old->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1176 | std::make_unique<Variable>(intf.fOffset, |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1177 | fModifiers->handle(id.fModifiers), |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1178 | id.fInstanceName.fLength ? id.fInstanceName : id.fTypeName, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1179 | type, |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 1180 | fIsBuiltinCode, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1181 | Variable::Storage::kGlobal)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1182 | if (foundRTAdjust) { |
| 1183 | fRTAdjustInterfaceBlock = var; |
| 1184 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1185 | if (id.fInstanceName.fLength) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 1186 | old->addWithoutOwnership(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | for (size_t i = 0; i < fields.size(); i++) { |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 1189 | old->add(std::make_unique<Field>(intf.fOffset, var, (int)i)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1192 | return std::make_unique<InterfaceBlock>(intf.fOffset, |
| 1193 | var, |
| 1194 | id.fTypeName, |
| 1195 | id.fInstanceName, |
| 1196 | std::move(sizes), |
| 1197 | symbols); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1200 | bool IRGenerator::getConstantInt(const Expression& value, int64_t* out) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1201 | switch (value.kind()) { |
| 1202 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 1203 | *out = value.as<IntLiteral>().value(); |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1204 | return true; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1205 | case Expression::Kind::kVariableReference: { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1206 | const Variable& var = *value.as<VariableReference>().variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1207 | return (var.modifiers().fFlags & Modifiers::kConst_Flag) && |
| 1208 | var.initialValue() && this->getConstantInt(*var.initialValue(), out); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1209 | } |
| 1210 | default: |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1211 | return false; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1215 | void IRGenerator::convertEnum(const ASTNode& e) { |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 1216 | if (fKind == Program::kPipelineStage_Kind) { |
| 1217 | fErrors.error(e.fOffset, "enum is not allowed here"); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1221 | SkASSERT(e.fKind == ASTNode::Kind::kEnum); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1222 | int64_t currentValue = 0; |
| 1223 | Layout layout; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1224 | ASTNode enumType(e.fNodes, e.fOffset, ASTNode::Kind::kType, |
| 1225 | ASTNode::TypeData(e.getString(), false, false)); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1226 | const Type* type = this->convertType(enumType); |
| 1227 | Modifiers modifiers(layout, Modifiers::kConst_Flag); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 1228 | std::shared_ptr<SymbolTable> oldTable = fSymbolTable; |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 1229 | fSymbolTable = std::make_shared<SymbolTable>(fSymbolTable, fIsBuiltinCode); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1230 | for (auto iter = e.begin(); iter != e.end(); ++iter) { |
| 1231 | const ASTNode& child = *iter; |
| 1232 | SkASSERT(child.fKind == ASTNode::Kind::kEnumCase); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1233 | std::unique_ptr<Expression> value; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1234 | if (child.begin() != child.end()) { |
| 1235 | value = this->convertExpression(*child.begin()); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1236 | if (!value) { |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 1237 | fSymbolTable = oldTable; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1238 | return; |
| 1239 | } |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1240 | if (!this->getConstantInt(*value, ¤tValue)) { |
| 1241 | fErrors.error(value->fOffset, "enum value must be a constant integer"); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 1242 | fSymbolTable = oldTable; |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1243 | return; |
| 1244 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1245 | } |
| 1246 | value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue)); |
| 1247 | ++currentValue; |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 1248 | fSymbolTable->add(std::make_unique<Variable>(e.fOffset, fModifiers->handle(modifiers), |
| 1249 | child.getString(), type, fIsBuiltinCode, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1250 | Variable::Storage::kGlobal, value.get())); |
Brian Osman | 3e3db6c | 2020-08-14 09:42:12 -0400 | [diff] [blame] | 1251 | fSymbolTable->takeOwnershipOfIRNode(std::move(value)); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1252 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 1253 | // Now we orphanize the Enum's symbol table, so that future lookups in it are strict |
| 1254 | fSymbolTable->fParent = nullptr; |
John Stiles | 1c82367 | 2020-10-20 10:23:50 -0400 | [diff] [blame] | 1255 | fProgramElements->push_back(std::make_unique<Enum>(e.fOffset, e.getString(), fSymbolTable, |
| 1256 | /*isSharedWithCpp=*/fIsBuiltinCode, |
| 1257 | /*isBuiltin=*/fIsBuiltinCode)); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 1258 | fSymbolTable = oldTable; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1259 | } |
| 1260 | |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 1261 | const Type* IRGenerator::convertType(const ASTNode& type, bool allowVoid) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1262 | ASTNode::TypeData td = type.getTypeData(); |
| 1263 | const Symbol* result = (*fSymbolTable)[td.fName]; |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 1264 | if (result && result->is<Type>()) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1265 | if (td.fIsNullable) { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1266 | if (result->as<Type>() == *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1267 | if (type.begin() != type.end()) { |
| 1268 | fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in " |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1269 | "an array"); |
| 1270 | } |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1271 | result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Type>( |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1272 | String(result->name()) + "?", Type::TypeKind::kNullable, |
| 1273 | result->as<Type>())); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1274 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1275 | fErrors.error(type.fOffset, "type '" + td.fName + "' may not be nullable"); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1276 | } |
| 1277 | } |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 1278 | if (result->as<Type>() == *fContext.fVoid_Type) { |
| 1279 | if (!allowVoid) { |
| 1280 | fErrors.error(type.fOffset, "type '" + td.fName + "' not allowed in this context"); |
| 1281 | return nullptr; |
| 1282 | } |
| 1283 | if (type.begin() != type.end()) { |
| 1284 | fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in an array"); |
| 1285 | return nullptr; |
| 1286 | } |
| 1287 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1288 | for (const auto& size : type) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1289 | String name(result->name()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1290 | name += "["; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1291 | if (size) { |
| 1292 | name += to_string(size.getInt()); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1293 | } |
| 1294 | name += "]"; |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 1295 | result = fSymbolTable->takeOwnershipOfSymbol( |
| 1296 | std::make_unique<Type>(name, Type::TypeKind::kArray, result->as<Type>(), |
| 1297 | size ? size.getInt() : Type::kUnsizedArray)); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1298 | } |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1299 | return &result->as<Type>(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1300 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1301 | fErrors.error(type.fOffset, "unknown type '" + td.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1302 | return nullptr; |
| 1303 | } |
| 1304 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1305 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1306 | switch (expr.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1307 | case ASTNode::Kind::kBinary: |
| 1308 | return this->convertBinaryExpression(expr); |
| 1309 | case ASTNode::Kind::kBool: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1310 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1311 | expr.getBool())); |
| 1312 | case ASTNode::Kind::kCall: |
| 1313 | return this->convertCallExpression(expr); |
| 1314 | case ASTNode::Kind::kField: |
| 1315 | return this->convertFieldExpression(expr); |
| 1316 | case ASTNode::Kind::kFloat: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1317 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1318 | expr.getFloat())); |
| 1319 | case ASTNode::Kind::kIdentifier: |
| 1320 | return this->convertIdentifier(expr); |
| 1321 | case ASTNode::Kind::kIndex: |
| 1322 | return this->convertIndexExpression(expr); |
| 1323 | case ASTNode::Kind::kInt: |
| 1324 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset, |
| 1325 | expr.getInt())); |
| 1326 | case ASTNode::Kind::kNull: |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1327 | return std::unique_ptr<Expression>(new NullLiteral(fContext, expr.fOffset)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1328 | case ASTNode::Kind::kPostfix: |
| 1329 | return this->convertPostfixExpression(expr); |
| 1330 | case ASTNode::Kind::kPrefix: |
| 1331 | return this->convertPrefixExpression(expr); |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 1332 | case ASTNode::Kind::kScope: |
| 1333 | return this->convertScopeExpression(expr); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1334 | case ASTNode::Kind::kTernary: |
| 1335 | return this->convertTernaryExpression(expr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1336 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1337 | #ifdef SK_DEBUG |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1338 | ABORT("unsupported expression: %s\n", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1339 | #endif |
| 1340 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1344 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) { |
| 1345 | SkASSERT(identifier.fKind == ASTNode::Kind::kIdentifier); |
| 1346 | const Symbol* result = (*fSymbolTable)[identifier.getString()]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1347 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1348 | fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.getString() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1349 | return nullptr; |
| 1350 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1351 | switch (result->kind()) { |
| 1352 | case Symbol::Kind::kFunctionDeclaration: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1353 | std::vector<const FunctionDeclaration*> f = { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1354 | &result->as<FunctionDeclaration>() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1355 | }; |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1356 | return std::make_unique<FunctionReference>(fContext, identifier.fOffset, f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1357 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1358 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1359 | const UnresolvedFunction* f = &result->as<UnresolvedFunction>(); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 1360 | return std::make_unique<FunctionReference>(fContext, identifier.fOffset, |
| 1361 | f->functions()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1362 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1363 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1364 | const Variable* var = &result->as<Variable>(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1365 | const Modifiers& modifiers = var->modifiers(); |
| 1366 | switch (modifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1367 | case SK_WIDTH_BUILTIN: |
| 1368 | fInputs.fRTWidth = true; |
| 1369 | break; |
| 1370 | case SK_HEIGHT_BUILTIN: |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1371 | fInputs.fRTHeight = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1372 | break; |
| 1373 | #ifndef SKSL_STANDALONE |
| 1374 | case SK_FRAGCOORD_BUILTIN: |
Brian Osman | 9f313b6 | 2019-10-02 12:03:11 -0400 | [diff] [blame] | 1375 | fInputs.fFlipY = true; |
| 1376 | if (fSettings->fFlipY && |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 1377 | (!fCaps || !fCaps->fragCoordConventionsExtensionString())) { |
Brian Osman | 9f313b6 | 2019-10-02 12:03:11 -0400 | [diff] [blame] | 1378 | fInputs.fRTHeight = true; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1379 | } |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1380 | #endif |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 1381 | } |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 1382 | if (fKind == Program::kFragmentProcessor_Kind && |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1383 | (modifiers.fFlags & Modifiers::kIn_Flag) && |
| 1384 | !(modifiers.fFlags & Modifiers::kUniform_Flag) && |
| 1385 | !modifiers.fLayout.fKey && |
| 1386 | modifiers.fLayout.fBuiltin == -1 && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1387 | var->type().nonnullable() != *fContext.fFragmentProcessor_Type && |
| 1388 | var->type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 1389 | bool valid = false; |
| 1390 | for (const auto& decl : fFile->root()) { |
| 1391 | if (decl.fKind == ASTNode::Kind::kSection) { |
| 1392 | ASTNode::SectionData section = decl.getSectionData(); |
| 1393 | if (section.fName == "setData") { |
| 1394 | valid = true; |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | if (!valid) { |
| 1400 | fErrors.error(identifier.fOffset, "'in' variable must be either 'uniform' or " |
| 1401 | "'layout(key)', or there must be a custom " |
| 1402 | "@setData function"); |
| 1403 | } |
| 1404 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1405 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1406 | return std::make_unique<VariableReference>(identifier.fOffset, |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 1407 | var, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1408 | VariableReference::RefKind::kRead); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1409 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1410 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1411 | const Field* field = &result->as<Field>(); |
Brian Osman | 6a204db | 2020-10-08 09:29:02 -0400 | [diff] [blame] | 1412 | auto base = std::make_unique<VariableReference>(identifier.fOffset, &field->owner(), |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1413 | VariableReference::RefKind::kRead); |
Brian Osman | 6a204db | 2020-10-08 09:29:02 -0400 | [diff] [blame] | 1414 | return std::make_unique<FieldAccess>(std::move(base), |
| 1415 | field->fieldIndex(), |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1416 | FieldAccess::OwnerKind::kAnonymousInterfaceBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1417 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1418 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1419 | const Type* t = &result->as<Type>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1420 | return std::make_unique<TypeReference>(fContext, identifier.fOffset, t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1421 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1422 | case Symbol::Kind::kExternal: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 1423 | const ExternalValue* r = &result->as<ExternalValue>(); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1424 | return std::make_unique<ExternalValueReference>(identifier.fOffset, r); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1425 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1426 | default: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1427 | ABORT("unsupported symbol type %d\n", (int) result->kind()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1428 | } |
Ethan Nicholas | c070939 | 2017-06-27 11:20:22 -0400 | [diff] [blame] | 1429 | } |
| 1430 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1431 | std::unique_ptr<Section> IRGenerator::convertSection(const ASTNode& s) { |
Brian Osman | 16f376f | 2020-09-02 12:30:59 -0400 | [diff] [blame] | 1432 | if (fKind != Program::kFragmentProcessor_Kind) { |
| 1433 | fErrors.error(s.fOffset, "syntax error"); |
| 1434 | return nullptr; |
| 1435 | } |
| 1436 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1437 | ASTNode::SectionData section = s.getSectionData(); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1438 | return std::make_unique<Section>(s.fOffset, section.fName, section.fArgument, |
| 1439 | section.fText); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1442 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1443 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1444 | if (!expr) { |
| 1445 | return nullptr; |
| 1446 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1447 | if (expr->type() == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1448 | return expr; |
| 1449 | } |
| 1450 | this->checkValid(*expr); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1451 | if (expr->type() == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1452 | return nullptr; |
| 1453 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1454 | if (!expr->coercionCost(type).isPossible(fSettings->fAllowNarrowingConversions)) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1455 | fErrors.error(expr->fOffset, "expected '" + type.displayName() + "', but found '" + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1456 | expr->type().displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1457 | return nullptr; |
| 1458 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1459 | if (type.typeKind() == Type::TypeKind::kScalar) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1460 | ExpressionArray args; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1461 | args.push_back(std::move(expr)); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1462 | std::unique_ptr<Expression> ctor; |
| 1463 | if (type == *fContext.fFloatLiteral_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1464 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
| 1465 | "float")); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1466 | } else if (type == *fContext.fIntLiteral_Type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1467 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
| 1468 | "int")); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1469 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1470 | ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1471 | type.name())); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1472 | } |
| 1473 | if (!ctor) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1474 | printf("error, null identifier: %s\n", String(type.name()).c_str()); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1475 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1476 | SkASSERT(ctor); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1477 | return this->call(/*offset=*/-1, std::move(ctor), std::move(args)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1478 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1479 | if (expr->kind() == Expression::Kind::kNullLiteral) { |
| 1480 | SkASSERT(type.typeKind() == Type::TypeKind::kNullable); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1481 | return std::unique_ptr<Expression>(new NullLiteral(expr->fOffset, &type)); |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 1482 | } |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1483 | ExpressionArray args; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1484 | args.push_back(std::move(expr)); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1485 | return std::make_unique<Constructor>(/*offset=*/-1, &type, std::move(args)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1488 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1489 | if (left.typeKind() == Type::TypeKind::kMatrix) { |
| 1490 | return right.typeKind() == Type::TypeKind::kMatrix || |
| 1491 | right.typeKind() == Type::TypeKind::kVector; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1492 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1493 | return left.typeKind() == Type::TypeKind::kVector && |
| 1494 | right.typeKind() == Type::TypeKind::kMatrix; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1495 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1496 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1497 | /** |
| 1498 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 1499 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 1500 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1501 | static bool determine_binary_type(const Context& context, |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1502 | bool allowNarrowing, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1503 | Token::Kind op, |
| 1504 | const Type& left, |
| 1505 | const Type& right, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1506 | const Type** outLeftType, |
| 1507 | const Type** outRightType, |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1508 | const Type** outResultType) { |
| 1509 | bool isLogical = false; |
Brian Osman | bf2163f | 2020-09-16 16:21:40 -0400 | [diff] [blame] | 1510 | bool isBitwise = false; |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1511 | bool validMatrixOrVectorOp = false; |
| 1512 | bool isAssignment = Compiler::IsAssignment(op); |
| 1513 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1514 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1515 | case Token::Kind::TK_EQ: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1516 | *outLeftType = &left; |
| 1517 | *outRightType = &left; |
| 1518 | *outResultType = &left; |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1519 | return right.canCoerceTo(left, allowNarrowing); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1520 | case Token::Kind::TK_EQEQ: // fall through |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1521 | case Token::Kind::TK_NEQ: { |
| 1522 | CoercionCost rightToLeft = right.coercionCost(left), |
| 1523 | leftToRight = left.coercionCost(right); |
| 1524 | if (rightToLeft < leftToRight) { |
| 1525 | if (rightToLeft.isPossible(allowNarrowing)) { |
| 1526 | *outLeftType = &left; |
| 1527 | *outRightType = &left; |
| 1528 | *outResultType = context.fBool_Type.get(); |
| 1529 | return true; |
| 1530 | } |
| 1531 | } else { |
| 1532 | if (leftToRight.isPossible(allowNarrowing)) { |
| 1533 | *outLeftType = &right; |
| 1534 | *outRightType = &right; |
| 1535 | *outResultType = context.fBool_Type.get(); |
| 1536 | return true; |
| 1537 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1538 | } |
Ethan Nicholas | 2346300 | 2018-03-28 15:16:15 -0400 | [diff] [blame] | 1539 | return false; |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1540 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1541 | case Token::Kind::TK_LT: // fall through |
| 1542 | case Token::Kind::TK_GT: // fall through |
| 1543 | case Token::Kind::TK_LTEQ: // fall through |
| 1544 | case Token::Kind::TK_GTEQ: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1545 | isLogical = true; |
| 1546 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1547 | case Token::Kind::TK_LOGICALOR: // fall through |
| 1548 | case Token::Kind::TK_LOGICALAND: // fall through |
| 1549 | case Token::Kind::TK_LOGICALXOR: // fall through |
| 1550 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 1551 | case Token::Kind::TK_LOGICALANDEQ: // fall through |
| 1552 | case Token::Kind::TK_LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1553 | *outLeftType = context.fBool_Type.get(); |
| 1554 | *outRightType = context.fBool_Type.get(); |
| 1555 | *outResultType = context.fBool_Type.get(); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1556 | return left.canCoerceTo(*context.fBool_Type, allowNarrowing) && |
| 1557 | right.canCoerceTo(*context.fBool_Type, allowNarrowing); |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1558 | case Token::Kind::TK_STAREQ: // fall through |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1559 | case Token::Kind::TK_STAR: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1560 | if (is_matrix_multiply(left, right)) { |
| 1561 | // determine final component type |
Brian Osman | 3ed22a9 | 2020-09-17 15:10:25 -0400 | [diff] [blame] | 1562 | if (determine_binary_type(context, allowNarrowing, op, |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1563 | left.componentType(), right.componentType(), |
| 1564 | outLeftType, outRightType, outResultType)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1565 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1566 | left.rows()); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1567 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1568 | right.rows()); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1569 | int leftColumns = left.columns(); |
| 1570 | int leftRows = left.rows(); |
| 1571 | int rightColumns; |
| 1572 | int rightRows; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1573 | if (right.typeKind() == Type::TypeKind::kVector) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1574 | // matrix * vector treats the vector as a column vector, so we need to |
| 1575 | // transpose it |
| 1576 | rightColumns = right.rows(); |
| 1577 | rightRows = right.columns(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1578 | SkASSERT(rightColumns == 1); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1579 | } else { |
| 1580 | rightColumns = right.columns(); |
| 1581 | rightRows = right.rows(); |
| 1582 | } |
| 1583 | if (rightColumns > 1) { |
| 1584 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 1585 | leftRows); |
| 1586 | } else { |
| 1587 | // result was a column vector, transpose it back to a row |
| 1588 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 1589 | rightColumns); |
| 1590 | } |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1591 | if (isAssignment && ((*outResultType)->columns() != leftColumns || |
| 1592 | (*outResultType)->rows() != leftRows)) { |
| 1593 | return false; |
| 1594 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1595 | return leftColumns == rightRows; |
| 1596 | } else { |
| 1597 | return false; |
| 1598 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1599 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1600 | validMatrixOrVectorOp = true; |
| 1601 | break; |
Brian Osman | bf2163f | 2020-09-16 16:21:40 -0400 | [diff] [blame] | 1602 | case Token::Kind::TK_SHLEQ: |
| 1603 | case Token::Kind::TK_SHREQ: |
| 1604 | case Token::Kind::TK_BITWISEANDEQ: |
| 1605 | case Token::Kind::TK_BITWISEOREQ: |
| 1606 | case Token::Kind::TK_BITWISEXOREQ: |
| 1607 | case Token::Kind::TK_SHL: |
| 1608 | case Token::Kind::TK_SHR: |
| 1609 | case Token::Kind::TK_BITWISEAND: |
| 1610 | case Token::Kind::TK_BITWISEOR: |
| 1611 | case Token::Kind::TK_BITWISEXOR: |
| 1612 | isBitwise = true; |
| 1613 | validMatrixOrVectorOp = true; |
| 1614 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1615 | case Token::Kind::TK_PLUSEQ: |
| 1616 | case Token::Kind::TK_MINUSEQ: |
| 1617 | case Token::Kind::TK_SLASHEQ: |
| 1618 | case Token::Kind::TK_PERCENTEQ: |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1619 | case Token::Kind::TK_PLUS: |
| 1620 | case Token::Kind::TK_MINUS: |
| 1621 | case Token::Kind::TK_SLASH: |
| 1622 | case Token::Kind::TK_PERCENT: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1623 | validMatrixOrVectorOp = true; |
| 1624 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1625 | case Token::Kind::TK_COMMA: |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1626 | *outLeftType = &left; |
| 1627 | *outRightType = &right; |
| 1628 | *outResultType = &right; |
| 1629 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1630 | default: |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1631 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1632 | } |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1633 | |
| 1634 | bool leftIsVectorOrMatrix = left.typeKind() == Type::TypeKind::kVector || |
| 1635 | left.typeKind() == Type::TypeKind::kMatrix, |
| 1636 | rightIsVectorOrMatrix = right.typeKind() == Type::TypeKind::kVector || |
| 1637 | right.typeKind() == Type::TypeKind::kMatrix; |
| 1638 | |
| 1639 | if (leftIsVectorOrMatrix && validMatrixOrVectorOp && |
| 1640 | right.typeKind() == Type::TypeKind::kScalar) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1641 | if (determine_binary_type(context, allowNarrowing, op, left.componentType(), right, |
| 1642 | outLeftType, outRightType, outResultType)) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1643 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1644 | if (!isLogical) { |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1645 | *outResultType = |
| 1646 | &(*outResultType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1647 | } |
| 1648 | return true; |
| 1649 | } |
| 1650 | return false; |
| 1651 | } |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1652 | |
| 1653 | if (!isAssignment && rightIsVectorOrMatrix && validMatrixOrVectorOp && |
| 1654 | left.typeKind() == Type::TypeKind::kScalar) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1655 | if (determine_binary_type(context, allowNarrowing, op, left, right.componentType(), |
| 1656 | outLeftType, outRightType, outResultType)) { |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1657 | *outRightType = &(*outRightType)->toCompound(context, right.columns(), right.rows()); |
| 1658 | if (!isLogical) { |
| 1659 | *outResultType = |
| 1660 | &(*outResultType)->toCompound(context, right.columns(), right.rows()); |
| 1661 | } |
| 1662 | return true; |
| 1663 | } |
| 1664 | return false; |
| 1665 | } |
| 1666 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1667 | CoercionCost rightToLeftCost = right.coercionCost(left); |
| 1668 | CoercionCost leftToRightCost = isAssignment ? CoercionCost::Impossible() |
| 1669 | : left.coercionCost(right); |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1670 | |
| 1671 | if ((left.typeKind() == Type::TypeKind::kScalar && |
| 1672 | right.typeKind() == Type::TypeKind::kScalar) || |
| 1673 | (leftIsVectorOrMatrix && validMatrixOrVectorOp)) { |
Brian Osman | bf2163f | 2020-09-16 16:21:40 -0400 | [diff] [blame] | 1674 | if (isBitwise) { |
| 1675 | const Type& leftNumberType(leftIsVectorOrMatrix ? left.componentType() : left); |
| 1676 | const Type& rightNumberType(rightIsVectorOrMatrix ? right.componentType() : right); |
| 1677 | if (!leftNumberType.isInteger() || !rightNumberType.isInteger()) { |
| 1678 | return false; |
| 1679 | } |
| 1680 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1681 | if (rightToLeftCost.isPossible(allowNarrowing) && rightToLeftCost < leftToRightCost) { |
| 1682 | // Right-to-Left conversion is possible and cheaper |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1683 | *outLeftType = &left; |
| 1684 | *outRightType = &left; |
| 1685 | *outResultType = &left; |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1686 | } else if (leftToRightCost.isPossible(allowNarrowing)) { |
Brian Osman | c4cb3a6 | 2020-09-03 16:52:28 -0400 | [diff] [blame] | 1687 | // Left-to-Right conversion is possible (and at least as cheap as Right-to-Left) |
| 1688 | *outLeftType = &right; |
| 1689 | *outRightType = &right; |
| 1690 | *outResultType = &right; |
| 1691 | } else { |
| 1692 | return false; |
| 1693 | } |
| 1694 | if (isLogical) { |
| 1695 | *outResultType = context.fBool_Type.get(); |
| 1696 | } |
| 1697 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1698 | } |
| 1699 | return false; |
| 1700 | } |
| 1701 | |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1702 | static std::unique_ptr<Expression> short_circuit_boolean(const Context& context, |
| 1703 | const Expression& left, |
| 1704 | Token::Kind op, |
| 1705 | const Expression& right) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1706 | SkASSERT(left.kind() == Expression::Kind::kBoolLiteral); |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 1707 | bool leftVal = left.as<BoolLiteral>().value(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1708 | if (op == Token::Kind::TK_LOGICALAND) { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1709 | // (true && expr) -> (expr) and (false && expr) -> (false) |
| 1710 | return leftVal ? right.clone() |
| 1711 | : std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, false)); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1712 | } else if (op == Token::Kind::TK_LOGICALOR) { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1713 | // (true || expr) -> (true) and (false || expr) -> (expr) |
| 1714 | return leftVal ? std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, true)) |
| 1715 | : right.clone(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1716 | } else if (op == Token::Kind::TK_LOGICALXOR) { |
Noah Lavine | 334d0ba | 2019-12-18 23:03:49 -0500 | [diff] [blame] | 1717 | // (true ^^ expr) -> !(expr) and (false ^^ expr) -> (expr) |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1718 | return leftVal ? std::unique_ptr<Expression>(new PrefixExpression( |
| 1719 | Token::Kind::TK_LOGICALNOT, |
| 1720 | right.clone())) |
Noah Lavine | 334d0ba | 2019-12-18 23:03:49 -0500 | [diff] [blame] | 1721 | : right.clone(); |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1722 | } else { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1723 | return nullptr; |
| 1724 | } |
| 1725 | } |
| 1726 | |
John Stiles | cf27b4f | 2020-11-06 11:37:05 -0500 | [diff] [blame] | 1727 | template <typename T> |
| 1728 | std::unique_ptr<Expression> IRGenerator::constantFoldVector(const Expression& left, |
| 1729 | Token::Kind op, |
| 1730 | const Expression& right) const { |
| 1731 | SkASSERT(left.type() == right.type()); |
| 1732 | const Type& type = left.type(); |
| 1733 | |
| 1734 | // Handle boolean operations: == != |
| 1735 | if (op == Token::Kind::TK_EQEQ || op == Token::Kind::TK_NEQ) { |
| 1736 | if (left.kind() == right.kind()) { |
| 1737 | bool result = left.compareConstant(fContext, right) ^ (op == Token::Kind::TK_NEQ); |
| 1738 | return std::make_unique<BoolLiteral>(fContext, left.fOffset, result); |
| 1739 | } |
| 1740 | return nullptr; |
| 1741 | } |
| 1742 | |
| 1743 | // Handle floating-point arithmetic: + - * / |
| 1744 | const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Constructor> { |
| 1745 | ExpressionArray args; |
| 1746 | for (int i = 0; i < type.columns(); i++) { |
| 1747 | T value = foldFn(left.getVecComponent<T>(i), right.getVecComponent<T>(i)); |
| 1748 | args.push_back(std::make_unique<Literal<T>>(fContext, left.fOffset, value)); |
| 1749 | } |
| 1750 | return std::make_unique<Constructor>(left.fOffset, &type, std::move(args)); |
| 1751 | }; |
| 1752 | |
| 1753 | const auto isVectorDivisionByZero = [&]() -> bool { |
| 1754 | for (int i = 0; i < type.columns(); i++) { |
| 1755 | if (right.getVecComponent<T>(i) == 0) { |
| 1756 | return true; |
| 1757 | } |
| 1758 | } |
| 1759 | return false; |
| 1760 | }; |
| 1761 | |
| 1762 | switch (op) { |
| 1763 | case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](T a, T b) { return a + b; }); |
| 1764 | case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](T a, T b) { return a - b; }); |
| 1765 | case Token::Kind::TK_STAR: return vectorComponentwiseFold([](T a, T b) { return a * b; }); |
| 1766 | case Token::Kind::TK_SLASH: { |
| 1767 | if (isVectorDivisionByZero()) { |
| 1768 | fErrors.error(right.fOffset, "division by zero"); |
| 1769 | return nullptr; |
| 1770 | } |
| 1771 | return vectorComponentwiseFold([](T a, T b) { return a / b; }); |
| 1772 | } |
| 1773 | default: |
| 1774 | return nullptr; |
| 1775 | } |
| 1776 | } |
| 1777 | |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1778 | std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left, |
| 1779 | Token::Kind op, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1780 | const Expression& right) const { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1781 | // If the left side is a constant boolean literal, the right side does not need to be constant |
| 1782 | // for short circuit optimizations to allow the constant to be folded. |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1783 | if (left.is<BoolLiteral>() && !right.isCompileTimeConstant()) { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1784 | return short_circuit_boolean(fContext, left, op, right); |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1785 | } else if (right.is<BoolLiteral>() && !left.isCompileTimeConstant()) { |
Michael Ludwig | 7b429ae | 2018-09-06 17:01:38 -0400 | [diff] [blame] | 1786 | // There aren't side effects in SKSL within expressions, so (left OP right) is equivalent to |
| 1787 | // (right OP left) for short-circuit optimizations |
| 1788 | return short_circuit_boolean(fContext, right, op, left); |
| 1789 | } |
| 1790 | |
| 1791 | // Other than the short-circuit cases above, constant folding requires both sides to be constant |
Brian Osman | b6b9573 | 2020-06-30 11:44:27 -0400 | [diff] [blame] | 1792 | if (!left.isCompileTimeConstant() || !right.isCompileTimeConstant()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1793 | return nullptr; |
| 1794 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1795 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 1796 | // precision to calculate the results and hope the result makes sense. The plan is to move the |
| 1797 | // Skia caps into SkSL, so we have access to all of them including the precisions of the various |
| 1798 | // types, which will let us be more intelligent about this. |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1799 | if (left.is<BoolLiteral>() && right.is<BoolLiteral>()) { |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 1800 | bool leftVal = left.as<BoolLiteral>().value(); |
| 1801 | bool rightVal = right.as<BoolLiteral>().value(); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1802 | bool result; |
| 1803 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1804 | case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break; |
| 1805 | case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break; |
| 1806 | case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1807 | default: return nullptr; |
| 1808 | } |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1809 | return std::make_unique<BoolLiteral>(fContext, left.fOffset, result); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1810 | } |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1811 | #define RESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \ |
| 1812 | leftVal op rightVal) |
| 1813 | #define URESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \ |
| 1814 | (uint32_t) leftVal op \ |
| 1815 | (uint32_t) rightVal) |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1816 | if (left.is<IntLiteral>() && right.is<IntLiteral>()) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 1817 | int64_t leftVal = left.as<IntLiteral>().value(); |
| 1818 | int64_t rightVal = right.as<IntLiteral>().value(); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1819 | switch (op) { |
Ethan Nicholas | 66869e9 | 2020-04-30 09:27:54 -0400 | [diff] [blame] | 1820 | case Token::Kind::TK_PLUS: return URESULT(Int, +); |
| 1821 | case Token::Kind::TK_MINUS: return URESULT(Int, -); |
| 1822 | case Token::Kind::TK_STAR: return URESULT(Int, *); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1823 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | 66869e9 | 2020-04-30 09:27:54 -0400 | [diff] [blame] | 1824 | if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) { |
| 1825 | fErrors.error(right.fOffset, "arithmetic overflow"); |
| 1826 | return nullptr; |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1827 | } |
Ethan Nicholas | 66869e9 | 2020-04-30 09:27:54 -0400 | [diff] [blame] | 1828 | if (!rightVal) { |
| 1829 | fErrors.error(right.fOffset, "division by zero"); |
| 1830 | return nullptr; |
| 1831 | } |
| 1832 | return RESULT(Int, /); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1833 | case Token::Kind::TK_PERCENT: |
Ethan Nicholas | 66869e9 | 2020-04-30 09:27:54 -0400 | [diff] [blame] | 1834 | if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) { |
| 1835 | fErrors.error(right.fOffset, "arithmetic overflow"); |
| 1836 | return nullptr; |
Ethan Nicholas | 2503ab6 | 2017-01-05 10:44:25 -0500 | [diff] [blame] | 1837 | } |
Ethan Nicholas | 66869e9 | 2020-04-30 09:27:54 -0400 | [diff] [blame] | 1838 | if (!rightVal) { |
| 1839 | fErrors.error(right.fOffset, "division by zero"); |
| 1840 | return nullptr; |
| 1841 | } |
| 1842 | return RESULT(Int, %); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1843 | case Token::Kind::TK_BITWISEAND: return RESULT(Int, &); |
| 1844 | case Token::Kind::TK_BITWISEOR: return RESULT(Int, |); |
| 1845 | case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^); |
| 1846 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 1847 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 1848 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 1849 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 1850 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 1851 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 1852 | case Token::Kind::TK_SHL: |
Ethan Nicholas | feba68a | 2019-06-10 09:56:29 -0400 | [diff] [blame] | 1853 | if (rightVal >= 0 && rightVal <= 31) { |
Ethan Nicholas | e448900 | 2020-04-29 14:00:14 -0400 | [diff] [blame] | 1854 | return URESULT(Int, <<); |
Ethan Nicholas | feba68a | 2019-06-10 09:56:29 -0400 | [diff] [blame] | 1855 | } |
| 1856 | fErrors.error(right.fOffset, "shift value out of range"); |
| 1857 | return nullptr; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1858 | case Token::Kind::TK_SHR: |
Ethan Nicholas | feba68a | 2019-06-10 09:56:29 -0400 | [diff] [blame] | 1859 | if (rightVal >= 0 && rightVal <= 31) { |
Ethan Nicholas | e448900 | 2020-04-29 14:00:14 -0400 | [diff] [blame] | 1860 | return URESULT(Int, >>); |
Ethan Nicholas | feba68a | 2019-06-10 09:56:29 -0400 | [diff] [blame] | 1861 | } |
| 1862 | fErrors.error(right.fOffset, "shift value out of range"); |
| 1863 | return nullptr; |
| 1864 | |
| 1865 | default: |
| 1866 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1869 | if (left.is<FloatLiteral>() && right.is<FloatLiteral>()) { |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 1870 | SKSL_FLOAT leftVal = left.as<FloatLiteral>().value(); |
| 1871 | SKSL_FLOAT rightVal = right.as<FloatLiteral>().value(); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1872 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1873 | case Token::Kind::TK_PLUS: return RESULT(Float, +); |
| 1874 | case Token::Kind::TK_MINUS: return RESULT(Float, -); |
| 1875 | case Token::Kind::TK_STAR: return RESULT(Float, *); |
| 1876 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1877 | if (rightVal) { |
| 1878 | return RESULT(Float, /); |
| 1879 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1880 | fErrors.error(right.fOffset, "division by zero"); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 1881 | return nullptr; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1882 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 1883 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 1884 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 1885 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 1886 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 1887 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 1888 | default: return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1889 | } |
| 1890 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1891 | const Type& leftType = left.type(); |
| 1892 | const Type& rightType = right.type(); |
John Stiles | cf27b4f | 2020-11-06 11:37:05 -0500 | [diff] [blame] | 1893 | if (leftType.typeKind() == Type::TypeKind::kVector && leftType == rightType) { |
| 1894 | if (leftType.componentType().isFloat()) { |
| 1895 | return constantFoldVector<SKSL_FLOAT>(left, op, right); |
| 1896 | } else if (leftType.componentType().isInteger()) { |
| 1897 | return constantFoldVector<SKSL_INT>(left, op, right); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1898 | } |
| 1899 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1900 | if (leftType.typeKind() == Type::TypeKind::kMatrix && |
| 1901 | rightType.typeKind() == Type::TypeKind::kMatrix && |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1902 | left.kind() == right.kind()) { |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1903 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1904 | case Token::Kind::TK_EQEQ: |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1905 | return std::make_unique<BoolLiteral>(fContext, left.fOffset, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1906 | left.compareConstant(fContext, right)); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1907 | case Token::Kind::TK_NEQ: |
John Stiles | 95acbbc | 2020-11-04 16:23:26 -0500 | [diff] [blame] | 1908 | return std::make_unique<BoolLiteral>(fContext, left.fOffset, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1909 | !left.compareConstant(fContext, right)); |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 1910 | default: |
| 1911 | return nullptr; |
| 1912 | } |
| 1913 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1914 | #undef RESULT |
| 1915 | return nullptr; |
| 1916 | } |
| 1917 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1918 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) { |
| 1919 | SkASSERT(expression.fKind == ASTNode::Kind::kBinary); |
| 1920 | auto iter = expression.begin(); |
| 1921 | std::unique_ptr<Expression> left = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1922 | if (!left) { |
| 1923 | return nullptr; |
| 1924 | } |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1925 | Token::Kind op = expression.getToken().fKind; |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 1926 | std::unique_ptr<Expression> right; |
| 1927 | { |
| 1928 | // Can't inline the right side of a short-circuiting boolean, because our inlining |
| 1929 | // approach runs things out of order. |
| 1930 | AutoDisableInline disableInline(this, /*canInline=*/(op != Token::Kind::TK_LOGICALAND && |
| 1931 | op != Token::Kind::TK_LOGICALOR)); |
| 1932 | right = this->convertExpression(*(iter++)); |
| 1933 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1934 | if (!right) { |
| 1935 | return nullptr; |
| 1936 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1937 | const Type* leftType; |
| 1938 | const Type* rightType; |
| 1939 | const Type* resultType; |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1940 | const Type* rawLeftType; |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 1941 | if (left->is<IntLiteral>() && right->type().isInteger()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1942 | rawLeftType = &right->type(); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1943 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1944 | rawLeftType = &left->type(); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1945 | } |
| 1946 | const Type* rawRightType; |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 1947 | if (right->is<IntLiteral>() && left->type().isInteger()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1948 | rawRightType = &left->type(); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1949 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1950 | rawRightType = &right->type(); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1951 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 1952 | if (!determine_binary_type(fContext, fSettings->fAllowNarrowingConversions, op, |
| 1953 | *rawLeftType, *rawRightType, &leftType, &rightType, &resultType)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1954 | fErrors.error(expression.fOffset, String("type mismatch: '") + |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1955 | Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1956 | "' cannot operate on '" + left->type().displayName() + |
| 1957 | "', '" + right->type().displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1958 | return nullptr; |
| 1959 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1960 | if (Compiler::IsAssignment(op)) { |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 1961 | if (!this->setRefKind(*left, op != Token::Kind::TK_EQ |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1962 | ? VariableReference::RefKind::kReadWrite |
| 1963 | : VariableReference::RefKind::kWrite)) { |
Ethan Nicholas | 4fadce4 | 2020-07-30 13:29:30 -0400 | [diff] [blame] | 1964 | return nullptr; |
| 1965 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1966 | } |
| 1967 | left = this->coerce(std::move(left), *leftType); |
| 1968 | right = this->coerce(std::move(right), *rightType); |
| 1969 | if (!left || !right) { |
| 1970 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1971 | } |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 1972 | std::unique_ptr<Expression> result = this->constantFold(*left, op, *right); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1973 | if (!result) { |
John Stiles | d1c4dac | 2020-08-11 18:50:50 -0400 | [diff] [blame] | 1974 | result = std::make_unique<BinaryExpression>(expression.fOffset, std::move(left), op, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1975 | std::move(right), resultType); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1976 | } |
| 1977 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1980 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) { |
| 1981 | SkASSERT(node.fKind == ASTNode::Kind::kTernary); |
| 1982 | auto iter = node.begin(); |
| 1983 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1984 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1985 | if (!test) { |
| 1986 | return nullptr; |
| 1987 | } |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 1988 | std::unique_ptr<Expression> ifTrue; |
| 1989 | std::unique_ptr<Expression> ifFalse; |
| 1990 | { |
| 1991 | AutoDisableInline disableInline(this); |
| 1992 | ifTrue = this->convertExpression(*(iter++)); |
| 1993 | if (!ifTrue) { |
| 1994 | return nullptr; |
| 1995 | } |
| 1996 | ifFalse = this->convertExpression(*(iter++)); |
| 1997 | if (!ifFalse) { |
| 1998 | return nullptr; |
| 1999 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2000 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2001 | const Type* trueType; |
| 2002 | const Type* falseType; |
| 2003 | const Type* resultType; |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2004 | if (!determine_binary_type(fContext, fSettings->fAllowNarrowingConversions, |
| 2005 | Token::Kind::TK_EQEQ, ifTrue->type(), ifFalse->type(), |
| 2006 | &trueType, &falseType, &resultType) || |
| 2007 | trueType != falseType) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2008 | fErrors.error(node.fOffset, "ternary operator result mismatch: '" + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2009 | ifTrue->type().displayName() + "', '" + |
| 2010 | ifFalse->type().displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2011 | return nullptr; |
| 2012 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 2013 | if (trueType->nonnullable() == *fContext.fFragmentProcessor_Type) { |
| 2014 | fErrors.error(node.fOffset, |
| 2015 | "ternary expression of type '" + trueType->displayName() + "' not allowed"); |
| 2016 | return nullptr; |
| 2017 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2018 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 2019 | if (!ifTrue) { |
| 2020 | return nullptr; |
| 2021 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2022 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 2023 | if (!ifFalse) { |
| 2024 | return nullptr; |
| 2025 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2026 | if (test->kind() == Expression::Kind::kBoolLiteral) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 2027 | // static boolean test, just return one of the branches |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 2028 | if (test->as<BoolLiteral>().value()) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 2029 | return ifTrue; |
| 2030 | } else { |
| 2031 | return ifFalse; |
| 2032 | } |
| 2033 | } |
John Stiles | 8fa3b4e | 2020-09-02 11:27:23 -0400 | [diff] [blame] | 2034 | return std::make_unique<TernaryExpression>(node.fOffset, |
| 2035 | std::move(test), |
| 2036 | std::move(ifTrue), |
| 2037 | std::move(ifFalse)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2040 | void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2041 | if (const ProgramElement* found = fIntrinsics->findAndInclude(function.description())) { |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 2042 | const FunctionDefinition& original = found->as<FunctionDefinition>(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 2043 | |
| 2044 | // Sort the referenced intrinsics into a consistent order; otherwise our output will become |
| 2045 | // non-deterministic. |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2046 | std::vector<const FunctionDeclaration*> intrinsics(original.referencedIntrinsics().begin(), |
| 2047 | original.referencedIntrinsics().end()); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 2048 | std::sort(intrinsics.begin(), intrinsics.end(), |
| 2049 | [](const FunctionDeclaration* a, const FunctionDeclaration* b) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2050 | if (a->isBuiltin() != b->isBuiltin()) { |
| 2051 | return a->isBuiltin() < b->isBuiltin(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 2052 | } |
| 2053 | if (a->fOffset != b->fOffset) { |
| 2054 | return a->fOffset < b->fOffset; |
| 2055 | } |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2056 | if (a->name() != b->name()) { |
| 2057 | return a->name() < b->name(); |
John Stiles | 9878d9e | 2020-09-22 15:40:16 -0400 | [diff] [blame] | 2058 | } |
| 2059 | return a->description() < b->description(); |
| 2060 | }); |
| 2061 | for (const FunctionDeclaration* f : intrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2062 | this->copyIntrinsicIfNeeded(*f); |
| 2063 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 2064 | |
John Stiles | 1c82367 | 2020-10-20 10:23:50 -0400 | [diff] [blame] | 2065 | fProgramElements->push_back(original.clone()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2069 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2070 | const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2071 | ExpressionArray arguments) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2072 | if (function.isBuiltin()) { |
| 2073 | if (function.definition()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2074 | fReferencedIntrinsics.insert(&function); |
| 2075 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2076 | if (!fIsBuiltinCode && fIntrinsics) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2077 | this->copyIntrinsicIfNeeded(function); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 2078 | } |
| 2079 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2080 | if (function.parameters().size() != arguments.size()) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2081 | String msg = "call to '" + function.name() + "' expected " + |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2082 | to_string((uint64_t) function.parameters().size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2083 | " argument"; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2084 | if (function.parameters().size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2085 | msg += "s"; |
| 2086 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2087 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2088 | fErrors.error(offset, msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2089 | return nullptr; |
| 2090 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2091 | if (fKind == Program::kPipelineStage_Kind && !function.definition() && !function.isBuiltin()) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2092 | String msg = "call to undefined function '" + function.name() + "'"; |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 2093 | fErrors.error(offset, msg); |
| 2094 | return nullptr; |
| 2095 | } |
John Stiles | fa88911 | 2020-10-12 19:03:43 -0400 | [diff] [blame] | 2096 | FunctionDeclaration::ParamTypes types; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2097 | const Type* returnType; |
| 2098 | if (!function.determineFinalTypes(arguments, &types, &returnType)) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2099 | String msg = "no match for " + function.name() + "("; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2100 | String separator; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2101 | for (size_t i = 0; i < arguments.size(); i++) { |
| 2102 | msg += separator; |
| 2103 | separator = ", "; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2104 | msg += arguments[i]->type().displayName(); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2105 | } |
| 2106 | msg += ")"; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2107 | fErrors.error(offset, msg); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2108 | return nullptr; |
| 2109 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2110 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2111 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 2112 | if (!arguments[i]) { |
| 2113 | return nullptr; |
| 2114 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2115 | const Modifiers& paramModifiers = function.parameters()[i]->modifiers(); |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2116 | if (paramModifiers.fFlags & Modifiers::kOut_Flag) { |
| 2117 | if (!this->setRefKind(*arguments[i], paramModifiers.fFlags & Modifiers::kIn_Flag |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2118 | ? VariableReference::RefKind::kReadWrite |
| 2119 | : VariableReference::RefKind::kPointer)) { |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2120 | return nullptr; |
| 2121 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2122 | } |
| 2123 | } |
John Stiles | ea9ab82 | 2020-08-31 09:55:04 -0400 | [diff] [blame] | 2124 | |
John Stiles | 4c412bc | 2020-10-13 11:19:41 -0400 | [diff] [blame] | 2125 | auto funcCall = std::make_unique<FunctionCall>(offset, returnType, &function, |
| 2126 | std::move(arguments)); |
| 2127 | if (fCanInline && |
| 2128 | fInliner->isSafeToInline(funcCall->function().definition()) && |
| 2129 | !fInliner->isLargeFunction(funcCall->function().definition())) { |
| 2130 | Inliner::InlinedCall inlinedCall = fInliner->inlineCall(funcCall.get(), fSymbolTable.get(), |
| 2131 | fCurrentFunction); |
| 2132 | if (inlinedCall.fInlinedBody) { |
| 2133 | fExtraStatements.push_back(std::move(inlinedCall.fInlinedBody)); |
| 2134 | } |
| 2135 | return std::move(inlinedCall.fReplacementExpr); |
| 2136 | } |
| 2137 | |
| 2138 | return std::move(funcCall); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | /** |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 2142 | * 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] | 2143 | * particular meaning other than "lower costs are preferred". Returns CoercionCost::Impossible() if |
| 2144 | * the call is not valid. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2145 | */ |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2146 | CoercionCost IRGenerator::callCost(const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2147 | const ExpressionArray& arguments) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2148 | if (function.parameters().size() != arguments.size()) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2149 | return CoercionCost::Impossible(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2150 | } |
John Stiles | fa88911 | 2020-10-12 19:03:43 -0400 | [diff] [blame] | 2151 | FunctionDeclaration::ParamTypes types; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2152 | const Type* ignored; |
| 2153 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2154 | return CoercionCost::Impossible(); |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 2155 | } |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2156 | CoercionCost total = CoercionCost::Free(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2157 | for (size_t i = 0; i < arguments.size(); i++) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2158 | total = total + arguments[i]->coercionCost(*types[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2159 | } |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 2160 | return total; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2163 | std::unique_ptr<Expression> IRGenerator::call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2164 | std::unique_ptr<Expression> functionValue, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2165 | ExpressionArray arguments) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2166 | switch (functionValue->kind()) { |
| 2167 | case Expression::Kind::kTypeReference: |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2168 | return this->convertConstructor(offset, |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2169 | functionValue->as<TypeReference>().value(), |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2170 | std::move(arguments)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2171 | case Expression::Kind::kExternalValue: { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2172 | const ExternalValue& v = functionValue->as<ExternalValueReference>().value(); |
| 2173 | if (!v.canCall()) { |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2174 | fErrors.error(offset, "this external value is not a function"); |
| 2175 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2176 | } |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2177 | int count = v.callParameterCount(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2178 | if (count != (int) arguments.size()) { |
| 2179 | fErrors.error(offset, "external function expected " + to_string(count) + |
| 2180 | " arguments, but found " + to_string((int) arguments.size())); |
| 2181 | return nullptr; |
| 2182 | } |
| 2183 | static constexpr int PARAMETER_MAX = 16; |
| 2184 | SkASSERT(count < PARAMETER_MAX); |
| 2185 | const Type* types[PARAMETER_MAX]; |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2186 | v.getCallParameterTypes(types); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2187 | for (int i = 0; i < count; ++i) { |
| 2188 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
| 2189 | if (!arguments[i]) { |
| 2190 | return nullptr; |
| 2191 | } |
| 2192 | } |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2193 | return std::make_unique<ExternalFunctionCall>(offset, &v, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2194 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2195 | case Expression::Kind::kFunctionReference: { |
John Stiles | ce591b7 | 2020-08-27 11:47:30 -0400 | [diff] [blame] | 2196 | const FunctionReference& ref = functionValue->as<FunctionReference>(); |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2197 | const std::vector<const FunctionDeclaration*>& functions = ref.functions(); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2198 | CoercionCost bestCost = CoercionCost::Impossible(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2199 | const FunctionDeclaration* best = nullptr; |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2200 | if (functions.size() > 1) { |
| 2201 | for (const auto& f : functions) { |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 2202 | CoercionCost cost = this->callCost(*f, arguments); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2203 | if (cost < bestCost) { |
| 2204 | bestCost = cost; |
| 2205 | best = f; |
| 2206 | } |
| 2207 | } |
| 2208 | if (best) { |
| 2209 | return this->call(offset, *best, std::move(arguments)); |
| 2210 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2211 | String msg = "no match for " + functions[0]->name() + "("; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2212 | String separator; |
| 2213 | for (size_t i = 0; i < arguments.size(); i++) { |
| 2214 | msg += separator; |
| 2215 | separator = ", "; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2216 | msg += arguments[i]->type().displayName(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2217 | } |
| 2218 | msg += ")"; |
| 2219 | fErrors.error(offset, msg); |
| 2220 | return nullptr; |
| 2221 | } |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2222 | return this->call(offset, *functions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2223 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2224 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2225 | fErrors.error(offset, "not a function"); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 2226 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2227 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2230 | std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(int offset, |
| 2231 | const Type& type, |
| 2232 | ExpressionArray args) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2233 | SkASSERT(type.isNumber()); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2234 | if (args.size() != 1) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2235 | fErrors.error(offset, "invalid arguments to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2236 | "' constructor, (expected exactly 1 argument, but found " + |
| 2237 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2238 | return nullptr; |
| 2239 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2240 | const Type& argType = args[0]->type(); |
| 2241 | if (type == argType) { |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 2242 | return std::move(args[0]); |
| 2243 | } |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2244 | if (type.isFloat() && args.size() == 1 && args[0]->is<FloatLiteral>()) { |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 2245 | SKSL_FLOAT value = args[0]->as<FloatLiteral>().value(); |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2246 | return std::make_unique<FloatLiteral>(offset, value, &type); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 2247 | } |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2248 | if (type.isFloat() && args.size() == 1 && args[0]->is<IntLiteral>()) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 2249 | int64_t value = args[0]->as<IntLiteral>().value(); |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 2250 | return std::make_unique<FloatLiteral>(offset, (float)value, &type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2251 | } |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2252 | if (args[0]->is<IntLiteral>() && (type == *fContext.fInt_Type || |
| 2253 | type == *fContext.fUInt_Type)) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 2254 | return std::make_unique<IntLiteral>(offset, args[0]->as<IntLiteral>().value(), &type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2255 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2256 | if (argType == *fContext.fBool_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2257 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0)); |
| 2258 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1)); |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2259 | return std::make_unique<TernaryExpression>(offset, std::move(args[0]), |
| 2260 | this->coerce(std::move(one), type), |
| 2261 | this->coerce(std::move(zero), type)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2262 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2263 | if (!argType.isNumber()) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2264 | fErrors.error(offset, "invalid argument to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2265 | "' constructor (expected a number or bool, but found '" + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2266 | argType.displayName() + "')"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2267 | return nullptr; |
| 2268 | } |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2269 | return std::make_unique<Constructor>(offset, &type, std::move(args)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2270 | } |
| 2271 | |
John Stiles | 3637440 | 2020-08-13 12:16:44 -0400 | [diff] [blame] | 2272 | static int component_count(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2273 | switch (type.typeKind()) { |
| 2274 | case Type::TypeKind::kVector: |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2275 | return type.columns(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2276 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2277 | return type.columns() * type.rows(); |
| 2278 | default: |
| 2279 | return 1; |
| 2280 | } |
| 2281 | } |
| 2282 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2283 | std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(int offset, |
| 2284 | const Type& type, |
| 2285 | ExpressionArray args) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2286 | SkASSERT(type.typeKind() == Type::TypeKind::kVector || |
| 2287 | type.typeKind() == Type::TypeKind::kMatrix); |
| 2288 | if (type.typeKind() == Type::TypeKind::kMatrix && args.size() == 1 && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2289 | args[0]->type().typeKind() == Type::TypeKind::kMatrix) { |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2290 | // matrix from matrix is always legal |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2291 | return std::unique_ptr<Expression>(new Constructor(offset, &type, std::move(args))); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2292 | } |
| 2293 | int actual = 0; |
| 2294 | int expected = type.rows() * type.columns(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2295 | if (args.size() != 1 || expected != component_count(args[0]->type()) || |
| 2296 | type.componentType().isNumber() != args[0]->type().componentType().isNumber()) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2297 | for (size_t i = 0; i < args.size(); i++) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2298 | const Type& argType = args[i]->type(); |
| 2299 | if (argType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2300 | if (type.componentType().isNumber() != |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2301 | argType.componentType().isNumber()) { |
| 2302 | fErrors.error(offset, "'" + argType.displayName() + "' is not a valid " |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2303 | "parameter to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2304 | "' constructor"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2305 | return nullptr; |
| 2306 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2307 | actual += argType.columns(); |
| 2308 | } else if (argType.typeKind() == Type::TypeKind::kScalar) { |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2309 | actual += 1; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2310 | if (type.typeKind() != Type::TypeKind::kScalar) { |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2311 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
| 2312 | if (!args[i]) { |
| 2313 | return nullptr; |
| 2314 | } |
| 2315 | } |
| 2316 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2317 | fErrors.error(offset, "'" + argType.displayName() + "' is not a valid " |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2318 | "parameter to '" + type.displayName() + "' constructor"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2319 | return nullptr; |
| 2320 | } |
| 2321 | } |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2322 | if (actual != 1 && actual != expected) { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2323 | fErrors.error(offset, "invalid arguments to '" + type.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2324 | "' constructor (expected " + to_string(expected) + |
| 2325 | " scalars, but found " + to_string(actual) + ")"); |
Ethan Nicholas | 49a36ba | 2017-02-09 17:04:23 +0000 | [diff] [blame] | 2326 | return nullptr; |
| 2327 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2328 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2329 | return std::unique_ptr<Expression>(new Constructor(offset, &type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2332 | std::unique_ptr<Expression> IRGenerator::convertConstructor(int offset, |
| 2333 | const Type& type, |
| 2334 | ExpressionArray args) { |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2335 | // FIXME: add support for structs |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2336 | if (args.size() == 1 && args[0]->type() == type && |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 2337 | type.nonnullable() != *fContext.fFragmentProcessor_Type) { |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2338 | // argument is already the right type, just return it |
| 2339 | return std::move(args[0]); |
| 2340 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2341 | Type::TypeKind kind = type.typeKind(); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2342 | if (type.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2343 | return this->convertNumberConstructor(offset, type, std::move(args)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2344 | } else if (kind == Type::TypeKind::kArray) { |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2345 | const Type& base = type.componentType(); |
| 2346 | for (size_t i = 0; i < args.size(); i++) { |
| 2347 | args[i] = this->coerce(std::move(args[i]), base); |
| 2348 | if (!args[i]) { |
| 2349 | return nullptr; |
| 2350 | } |
| 2351 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2352 | return std::make_unique<Constructor>(offset, &type, std::move(args)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2353 | } else if (kind == Type::TypeKind::kVector || kind == Type::TypeKind::kMatrix) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2354 | return this->convertCompoundConstructor(offset, type, std::move(args)); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2355 | } else { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2356 | fErrors.error(offset, "cannot construct '" + type.displayName() + "'"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 2357 | return nullptr; |
| 2358 | } |
| 2359 | } |
| 2360 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2361 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) { |
| 2362 | SkASSERT(expression.fKind == ASTNode::Kind::kPrefix); |
| 2363 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2364 | if (!base) { |
| 2365 | return nullptr; |
| 2366 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2367 | const Type& baseType = base->type(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2368 | switch (expression.getToken().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2369 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2370 | if (!baseType.isNumber() && baseType.typeKind() != Type::TypeKind::kVector && |
| 2371 | baseType != *fContext.fFloatLiteral_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2372 | fErrors.error(expression.fOffset, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2373 | "'+' cannot operate on '" + baseType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2374 | return nullptr; |
| 2375 | } |
| 2376 | return base; |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2377 | |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2378 | case Token::Kind::TK_MINUS: |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2379 | if (base->is<IntLiteral>()) { |
| 2380 | return std::make_unique<IntLiteral>(fContext, base->fOffset, |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 2381 | -base->as<IntLiteral>().value()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2382 | } |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2383 | if (base->is<FloatLiteral>()) { |
| 2384 | return std::make_unique<FloatLiteral>(fContext, base->fOffset, |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 2385 | -base->as<FloatLiteral>().value()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2386 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2387 | if (!baseType.isNumber() && baseType.typeKind() != Type::TypeKind::kVector) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2388 | fErrors.error(expression.fOffset, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2389 | "'-' cannot operate on '" + baseType.displayName() + "'"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2390 | return nullptr; |
| 2391 | } |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2392 | return std::make_unique<PrefixExpression>(Token::Kind::TK_MINUS, std::move(base)); |
| 2393 | |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2394 | case Token::Kind::TK_PLUSPLUS: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2395 | if (!baseType.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2396 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2397 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2398 | "' cannot operate on '" + baseType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2399 | return nullptr; |
| 2400 | } |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2401 | if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) { |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2402 | return nullptr; |
| 2403 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2404 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2405 | case Token::Kind::TK_MINUSMINUS: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2406 | if (!baseType.isNumber()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2407 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2408 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2409 | "' cannot operate on '" + baseType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2410 | return nullptr; |
| 2411 | } |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2412 | if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) { |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2413 | return nullptr; |
| 2414 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2415 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2416 | case Token::Kind::TK_LOGICALNOT: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2417 | if (baseType != *fContext.fBool_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2418 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2419 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2420 | "' cannot operate on '" + baseType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2421 | return nullptr; |
| 2422 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2423 | if (base->kind() == Expression::Kind::kBoolLiteral) { |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2424 | return std::make_unique<BoolLiteral>(fContext, base->fOffset, |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 2425 | !base->as<BoolLiteral>().value()); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 2426 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2427 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2428 | case Token::Kind::TK_BITWISENOT: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2429 | if (baseType != *fContext.fInt_Type && baseType != *fContext.fUInt_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2430 | fErrors.error(expression.fOffset, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2431 | String("'") + Compiler::OperatorName(expression.getToken().fKind) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2432 | "' cannot operate on '" + baseType.displayName() + "'"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2433 | return nullptr; |
| 2434 | } |
| 2435 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2436 | default: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2437 | ABORT("unsupported prefix operator\n"); |
| 2438 | } |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2439 | return std::make_unique<PrefixExpression>(expression.getToken().fKind, std::move(base)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2443 | const ASTNode& index) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2444 | if (base->kind() == Expression::Kind::kTypeReference) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2445 | if (index.fKind == ASTNode::Kind::kInt) { |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2446 | const Type& oldType = base->as<TypeReference>().value(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2447 | SKSL_INT size = index.getInt(); |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 2448 | const Type* newType = fSymbolTable->takeOwnershipOfSymbol( |
| 2449 | std::make_unique<Type>(oldType.name() + "[" + to_string(size) + "]", |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2450 | Type::TypeKind::kArray, oldType, size)); |
| 2451 | return std::make_unique<TypeReference>(fContext, base->fOffset, newType); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2452 | |
| 2453 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2454 | fErrors.error(base->fOffset, "array size must be a constant"); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 2455 | return nullptr; |
| 2456 | } |
| 2457 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2458 | const Type& baseType = base->type(); |
| 2459 | if (baseType.typeKind() != Type::TypeKind::kArray && |
| 2460 | baseType.typeKind() != Type::TypeKind::kMatrix && |
| 2461 | baseType.typeKind() != Type::TypeKind::kVector) { |
| 2462 | fErrors.error(base->fOffset, "expected array, but found '" + baseType.displayName() + |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2463 | "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2464 | return nullptr; |
| 2465 | } |
| 2466 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 2467 | if (!converted) { |
| 2468 | return nullptr; |
| 2469 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2470 | if (converted->type() != *fContext.fUInt_Type) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2471 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 2472 | if (!converted) { |
| 2473 | return nullptr; |
| 2474 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2475 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2476 | return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(converted)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2480 | StringFragment field) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2481 | if (base->kind() == Expression::Kind::kExternalValue) { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2482 | const ExternalValue& ev = base->as<ExternalValueReference>().value(); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 2483 | ExternalValue* result = ev.getChild(String(field).c_str()); |
| 2484 | if (!result) { |
| 2485 | fErrors.error(base->fOffset, "external value does not have a child named '" + field + |
| 2486 | "'"); |
| 2487 | return nullptr; |
| 2488 | } |
| 2489 | return std::unique_ptr<Expression>(new ExternalValueReference(base->fOffset, result)); |
| 2490 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2491 | const Type& baseType = base->type(); |
| 2492 | auto fields = baseType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2493 | for (size_t i = 0; i < fields.size(); i++) { |
| 2494 | if (fields[i].fName == field) { |
| 2495 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 2496 | } |
| 2497 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2498 | fErrors.error(base->fOffset, "type '" + baseType.displayName() + "' does not have a field " |
John Stiles | 68861e3 | 2020-09-25 16:02:07 -0400 | [diff] [blame] | 2499 | "named '" + field + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2500 | return nullptr; |
| 2501 | } |
| 2502 | |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2503 | // Swizzles are complicated due to constant components. The most difficult case is a mask like |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2504 | // '.x1w0'. A naive approach might turn that into 'float4(base.x, 1, base.w, 0)', but that evaluates |
| 2505 | // 'base' twice. We instead group the swizzle mask ('xw') and constants ('1, 0') together and use a |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2506 | // secondary swizzle to put them back into the right order, so in this case we end up with |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2507 | // 'float4(base.xw, 1, 0).xzyw'. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2508 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2509 | StringFragment fields) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2510 | const int offset = base->fOffset; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2511 | const Type& baseType = base->type(); |
| 2512 | if (baseType.typeKind() != Type::TypeKind::kVector && !baseType.isNumber()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2513 | fErrors.error(offset, "cannot swizzle value of type '" + baseType.displayName() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2514 | return nullptr; |
| 2515 | } |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2516 | |
| 2517 | if (fields.fLength > 4) { |
| 2518 | fErrors.error(offset, "too many components in swizzle mask '" + fields + "'"); |
| 2519 | return nullptr; |
| 2520 | } |
| 2521 | |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 2522 | ComponentArray maskComponents; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2523 | for (size_t i = 0; i < fields.fLength; i++) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 2524 | switch (fields[i]) { |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2525 | case '0': |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2526 | case '1': |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2527 | // Skip over constant fields for now. |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 2528 | break; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2529 | case 'x': |
| 2530 | case 'r': |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2531 | case 's': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2532 | case 'L': |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2533 | maskComponents.push_back(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2534 | break; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2535 | case 'y': |
| 2536 | case 'g': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2537 | case 't': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2538 | case 'T': |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2539 | if (baseType.columns() >= 2) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2540 | maskComponents.push_back(1); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2541 | break; |
| 2542 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 2543 | [[fallthrough]]; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2544 | case 'z': |
| 2545 | case 'b': |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 2546 | case 'p': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2547 | case 'R': |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2548 | if (baseType.columns() >= 3) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2549 | maskComponents.push_back(2); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2550 | break; |
| 2551 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 2552 | [[fallthrough]]; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2553 | case 'w': |
| 2554 | case 'a': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2555 | case 'q': |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 2556 | case 'B': |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2557 | if (baseType.columns() >= 4) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2558 | maskComponents.push_back(3); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2559 | break; |
| 2560 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 2561 | [[fallthrough]]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2562 | default: |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2563 | fErrors.error(offset, String::printf("invalid swizzle component '%c'", fields[i])); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2564 | return nullptr; |
| 2565 | } |
| 2566 | } |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2567 | if (maskComponents.empty()) { |
| 2568 | fErrors.error(offset, "swizzle must refer to base expression"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2569 | return nullptr; |
| 2570 | } |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2571 | |
| 2572 | // First, we need a vector expression that is the non-constant portion of the swizzle, packed: |
| 2573 | // scalar.xxx -> type3(scalar) |
| 2574 | // scalar.x0x0 -> type2(scalar) |
| 2575 | // vector.zyx -> vector.zyx |
| 2576 | // vector.x0y0 -> vector.xy |
| 2577 | std::unique_ptr<Expression> expr; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2578 | if (baseType.isNumber()) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2579 | ExpressionArray scalarConstructorArgs; |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2580 | scalarConstructorArgs.push_back(std::move(base)); |
| 2581 | expr = std::make_unique<Constructor>( |
| 2582 | offset, &baseType.toCompound(fContext, maskComponents.size(), 1), |
| 2583 | std::move(scalarConstructorArgs)); |
| 2584 | } else { |
| 2585 | expr = std::make_unique<Swizzle>(fContext, std::move(base), maskComponents); |
Ethan Nicholas | 7b9da25 | 2020-08-03 13:43:50 -0400 | [diff] [blame] | 2586 | } |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2587 | |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2588 | // If we have processed the entire swizzle, we're done. |
| 2589 | if (maskComponents.size() == fields.fLength) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2590 | return expr; |
| 2591 | } |
| 2592 | |
| 2593 | // Now we create a constructor that has the correct number of elements for the final swizzle, |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2594 | // with all fields at the start. It's not finished yet; constants we need will be added below. |
| 2595 | // scalar.x0x0 -> type4(type2(x), ...) |
| 2596 | // vector.y111 -> type4(vector.y, ...) |
| 2597 | // vector.z10x -> type4(vector.zx, ...) |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2598 | // |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2599 | // We could create simpler IR in some cases by reordering here, if all fields are packed |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2600 | // contiguously. The benefits are minor, so skip the optimization to keep the algorithm simple. |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2601 | // The constructor will have at most three arguments: { base value, constant 0, constant 1 } |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2602 | ExpressionArray constructorArgs; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 2603 | constructorArgs.reserve_back(3); |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2604 | constructorArgs.push_back(std::move(expr)); |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2605 | |
| 2606 | // Apply another swizzle to shuffle the constants into the correct place. Any constant values we |
| 2607 | // need are also tacked on to the end of the constructor. |
| 2608 | // scalar.x0x0 -> type4(type2(x), 0).xyxy |
| 2609 | // vector.y111 -> type4(vector.y, 1).xyyy |
| 2610 | // vector.z10x -> type4(vector.zx, 1, 0).xzwy |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2611 | const Type* numberType = baseType.isNumber() ? &baseType : &baseType.componentType(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 2612 | ComponentArray swizzleComponents; |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2613 | int maskFieldIdx = 0; |
| 2614 | int constantFieldIdx = maskComponents.size(); |
| 2615 | int constantZeroIdx = -1, constantOneIdx = -1; |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2616 | |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2617 | for (size_t i = 0; i < fields.fLength; i++) { |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2618 | switch (fields[i]) { |
| 2619 | case '0': |
| 2620 | if (constantZeroIdx == -1) { |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2621 | // Synthesize a 'type(0)' argument at the end of the constructor. |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2622 | auto zero = std::make_unique<Constructor>(offset, numberType, |
| 2623 | ExpressionArray{}); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 2624 | zero->arguments().push_back(std::make_unique<IntLiteral>(fContext, offset, |
| 2625 | /*fValue=*/0)); |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2626 | constructorArgs.push_back(std::move(zero)); |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2627 | constantZeroIdx = constantFieldIdx++; |
| 2628 | } |
| 2629 | swizzleComponents.push_back(constantZeroIdx); |
| 2630 | break; |
| 2631 | case '1': |
| 2632 | if (constantOneIdx == -1) { |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2633 | // Synthesize a 'type(1)' argument at the end of the constructor. |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2634 | auto one = std::make_unique<Constructor>(offset, numberType, ExpressionArray{}); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 2635 | one->arguments().push_back(std::make_unique<IntLiteral>(fContext, offset, |
| 2636 | /*fValue=*/1)); |
John Stiles | d0e4840 | 2020-09-22 14:00:40 -0400 | [diff] [blame] | 2637 | constructorArgs.push_back(std::move(one)); |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2638 | constantOneIdx = constantFieldIdx++; |
| 2639 | } |
| 2640 | swizzleComponents.push_back(constantOneIdx); |
| 2641 | break; |
| 2642 | default: |
| 2643 | // The non-constant fields are already in the expected order. |
| 2644 | swizzleComponents.push_back(maskFieldIdx++); |
| 2645 | break; |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | |
John Stiles | 6e49a37 | 2020-09-16 13:40:54 -0400 | [diff] [blame] | 2649 | expr = std::make_unique<Constructor>(offset, |
| 2650 | &numberType->toCompound(fContext, constantFieldIdx, 1), |
| 2651 | std::move(constructorArgs)); |
| 2652 | |
John Stiles | b23ea38 | 2020-09-16 13:41:14 -0400 | [diff] [blame] | 2653 | // For some of our most common use cases ('.xyz0', '.xyz1'), we will now have an identity |
| 2654 | // swizzle; in those cases we can just return the constructor without the swizzle attached. |
| 2655 | for (size_t i = 0; i < swizzleComponents.size(); ++i) { |
| 2656 | if (swizzleComponents[i] != int(i)) { |
| 2657 | // The swizzle has an effect, so apply it. |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 2658 | return std::make_unique<Swizzle>(fContext, std::move(expr), swizzleComponents); |
John Stiles | b23ea38 | 2020-09-16 13:41:14 -0400 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | // The swizzle was a no-op; return the constructor expression directly. |
| 2663 | return expr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 2666 | const Type* IRGenerator::typeForSetting(int offset, String name) const { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2667 | auto found = fCapsMap.find(name); |
| 2668 | if (found == fCapsMap.end()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2669 | fErrors.error(offset, "unknown capability flag '" + name + "'"); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 2670 | return nullptr; |
| 2671 | } |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 2672 | switch (found->second.fKind) { |
| 2673 | case Program::Settings::Value::kBool_Kind: return fContext.fBool_Type.get(); |
| 2674 | case Program::Settings::Value::kFloat_Kind: return fContext.fFloat_Type.get(); |
| 2675 | case Program::Settings::Value::kInt_Kind: return fContext.fInt_Type.get(); |
| 2676 | } |
| 2677 | SkUNREACHABLE; |
| 2678 | return nullptr; |
| 2679 | } |
| 2680 | |
| 2681 | std::unique_ptr<Expression> IRGenerator::valueForSetting(int offset, String name) const { |
| 2682 | auto found = fCapsMap.find(name); |
| 2683 | if (found == fCapsMap.end()) { |
| 2684 | fErrors.error(offset, "unknown capability flag '" + name + "'"); |
| 2685 | return nullptr; |
| 2686 | } |
| 2687 | return found->second.literal(fContext, offset); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 2688 | } |
| 2689 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2690 | std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type, |
| 2691 | StringFragment field) { |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2692 | // Find the Enum element that this type refers to (if any) |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 2693 | const ProgramElement* enumElement = nullptr; |
| 2694 | for (const auto& e : *fProgramElements) { |
Ethan Nicholas | d83ded8 | 2020-09-29 17:05:54 -0400 | [diff] [blame] | 2695 | if (e->is<Enum>() && type.name() == e->as<Enum>().typeName()) { |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 2696 | enumElement = e.get(); |
| 2697 | break; |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2698 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 2699 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2700 | |
| 2701 | if (enumElement) { |
| 2702 | // We found the Enum element. Look for 'field' as a member. |
| 2703 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
Ethan Nicholas | d83ded8 | 2020-09-29 17:05:54 -0400 | [diff] [blame] | 2704 | fSymbolTable = enumElement->as<Enum>().symbols(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2705 | std::unique_ptr<Expression> result = convertIdentifier( |
| 2706 | ASTNode(&fFile->fNodes, offset, ASTNode::Kind::kIdentifier, field)); |
| 2707 | if (result) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2708 | const Variable& v = *result->as<VariableReference>().variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2709 | SkASSERT(v.initialValue()); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2710 | result = std::make_unique<IntLiteral>( |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2711 | offset, v.initialValue()->as<IntLiteral>().value(), &type); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2712 | } else { |
| 2713 | fErrors.error(offset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2714 | "type '" + type.name() + "' does not have a member named '" + field + |
| 2715 | "'"); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2716 | } |
| 2717 | fSymbolTable = old; |
| 2718 | return result; |
| 2719 | } else { |
| 2720 | // No Enum element? Check the intrinsics, clone it into the program, try again. |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2721 | if (!fIsBuiltinCode && fIntrinsics) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2722 | if (const ProgramElement* found = fIntrinsics->findAndInclude(type.name())) { |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2723 | fProgramElements->push_back(found->clone()); |
| 2724 | return this->convertTypeField(offset, type, field); |
| 2725 | } |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 2726 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2727 | fErrors.error(offset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2728 | "type '" + type.displayName() + "' does not have a member named '" + field + |
| 2729 | "'"); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 2730 | return nullptr; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2731 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 2732 | } |
| 2733 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2734 | std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) { |
| 2735 | SkASSERT(index.fKind == ASTNode::Kind::kIndex); |
| 2736 | auto iter = index.begin(); |
| 2737 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2738 | if (!base) { |
| 2739 | return nullptr; |
| 2740 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2741 | if (iter != index.end()) { |
| 2742 | return this->convertIndex(std::move(base), *(iter++)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2743 | } else if (base->kind() == Expression::Kind::kTypeReference) { |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2744 | const Type& oldType = base->as<TypeReference>().value(); |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 2745 | const Type* newType = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Type>( |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 2746 | oldType.name() + "[]", Type::TypeKind::kArray, oldType, Type::kUnsizedArray)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2747 | return std::make_unique<TypeReference>(fContext, base->fOffset, newType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2748 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2749 | fErrors.error(index.fOffset, "'[]' must follow a type name"); |
| 2750 | return nullptr; |
| 2751 | } |
| 2752 | |
| 2753 | std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) { |
| 2754 | SkASSERT(callNode.fKind == ASTNode::Kind::kCall); |
| 2755 | auto iter = callNode.begin(); |
| 2756 | std::unique_ptr<Expression> base = this->convertExpression(*(iter++)); |
| 2757 | if (!base) { |
| 2758 | return nullptr; |
| 2759 | } |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 2760 | ExpressionArray arguments; |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2761 | for (; iter != callNode.end(); ++iter) { |
| 2762 | std::unique_ptr<Expression> converted = this->convertExpression(*iter); |
| 2763 | if (!converted) { |
| 2764 | return nullptr; |
| 2765 | } |
| 2766 | arguments.push_back(std::move(converted)); |
| 2767 | } |
| 2768 | return this->call(callNode.fOffset, std::move(base), std::move(arguments)); |
| 2769 | } |
| 2770 | |
| 2771 | std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) { |
| 2772 | std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin()); |
| 2773 | if (!base) { |
| 2774 | return nullptr; |
| 2775 | } |
| 2776 | StringFragment field = fieldNode.getString(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2777 | const Type& baseType = base->type(); |
| 2778 | if (baseType == *fContext.fSkCaps_Type) { |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 2779 | const Type* type = this->typeForSetting(fieldNode.fOffset, field); |
| 2780 | if (!type) { |
| 2781 | return nullptr; |
| 2782 | } |
| 2783 | return std::make_unique<Setting>(fieldNode.fOffset, field, type); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2784 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2785 | if (base->kind() == Expression::Kind::kExternalValue) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2786 | return this->convertField(std::move(base), field); |
| 2787 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2788 | switch (baseType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2789 | case Type::TypeKind::kOther: |
| 2790 | case Type::TypeKind::kStruct: |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2791 | return this->convertField(std::move(base), field); |
| 2792 | default: |
Ethan Nicholas | 7b9da25 | 2020-08-03 13:43:50 -0400 | [diff] [blame] | 2793 | return this->convertSwizzle(std::move(base), field); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 2797 | std::unique_ptr<Expression> IRGenerator::convertScopeExpression(const ASTNode& scopeNode) { |
| 2798 | std::unique_ptr<Expression> base = this->convertExpression(*scopeNode.begin()); |
| 2799 | if (!base) { |
| 2800 | return nullptr; |
| 2801 | } |
| 2802 | if (!base->is<TypeReference>()) { |
| 2803 | fErrors.error(scopeNode.fOffset, "'::' must follow a type name"); |
| 2804 | return nullptr; |
| 2805 | } |
| 2806 | StringFragment member = scopeNode.getString(); |
Ethan Nicholas | 5194a70 | 2020-10-12 11:12:12 -0400 | [diff] [blame] | 2807 | return this->convertTypeField(base->fOffset, base->as<TypeReference>().value(), member); |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2810 | std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) { |
| 2811 | std::unique_ptr<Expression> base = this->convertExpression(*expression.begin()); |
| 2812 | if (!base) { |
| 2813 | return nullptr; |
| 2814 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2815 | const Type& baseType = base->type(); |
| 2816 | if (!baseType.isNumber()) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2817 | fErrors.error(expression.fOffset, |
| 2818 | "'" + String(Compiler::OperatorName(expression.getToken().fKind)) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2819 | "' cannot operate on '" + baseType.displayName() + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 2820 | return nullptr; |
| 2821 | } |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2822 | if (!this->setRefKind(*base, VariableReference::RefKind::kReadWrite)) { |
John Stiles | 978674a | 2020-09-23 15:24:51 -0400 | [diff] [blame] | 2823 | return nullptr; |
| 2824 | } |
| 2825 | return std::make_unique<PostfixExpression>(std::move(base), expression.getToken().fKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | void IRGenerator::checkValid(const Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2829 | switch (expr.kind()) { |
| 2830 | case Expression::Kind::kFunctionReference: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2831 | fErrors.error(expr.fOffset, "expected '(' to begin function call"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2832 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2833 | case Expression::Kind::kTypeReference: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2834 | fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | default: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2837 | if (expr.type() == *fContext.fInvalid_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2838 | fErrors.error(expr.fOffset, "invalid expression"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 2839 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 2843 | bool IRGenerator::setRefKind(Expression& expr, VariableReference::RefKind kind) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 2844 | VariableReference* assignableVar = nullptr; |
| 2845 | if (!Analysis::IsAssignable(expr, &assignableVar, &fErrors)) { |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 2846 | return false; |
| 2847 | } |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 2848 | if (assignableVar) { |
| 2849 | assignableVar->setRefKind(kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2850 | } |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 2851 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2854 | void IRGenerator::cloneBuiltinVariables() { |
| 2855 | class BuiltinVariableRemapper : public ProgramWriter { |
| 2856 | public: |
| 2857 | BuiltinVariableRemapper(IRGenerator* generator) : fGenerator(generator) {} |
| 2858 | |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2859 | void cloneVariable(const String& name) { |
| 2860 | // If this is the *first* time we've seen this builtin, findAndInclude will return |
| 2861 | // the corresponding ProgramElement. |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2862 | if (const ProgramElement* sharedDecl = fGenerator->fIntrinsics->findAndInclude(name)) { |
| 2863 | SkASSERT(sharedDecl->is<GlobalVarDeclaration>() || |
| 2864 | sharedDecl->is<InterfaceBlock>()); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2865 | |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2866 | // Clone the ProgramElement that declares this variable |
| 2867 | std::unique_ptr<ProgramElement> clonedDecl = sharedDecl->clone(); |
| 2868 | const Variable* sharedVar = nullptr; |
| 2869 | const Expression* initialValue = nullptr; |
| 2870 | |
| 2871 | if (clonedDecl->is<GlobalVarDeclaration>()) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2872 | GlobalVarDeclaration& global = clonedDecl->as<GlobalVarDeclaration>(); |
| 2873 | VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 2874 | sharedVar = &decl.var(); |
| 2875 | initialValue = decl.value().get(); |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2876 | } else { |
| 2877 | SkASSERT(clonedDecl->is<InterfaceBlock>()); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2878 | sharedVar = &clonedDecl->as<InterfaceBlock>().variable(); |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2879 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2880 | |
| 2881 | // Now clone the Variable, and add the clone to the Program's symbol table. |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2882 | // Any initial value expression was cloned as part of the GlobalVarDeclaration, |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2883 | // so we're pointing at a Program-owned expression. |
| 2884 | const Variable* clonedVar = |
| 2885 | fGenerator->fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>( |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2886 | sharedVar->fOffset, sharedVar->modifiersHandle(), sharedVar->name(), |
| 2887 | &sharedVar->type(), /*builtin=*/false, sharedVar->storage(), |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2888 | initialValue)); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2889 | |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2890 | // Go back and update the declaring element to point at the cloned Variable. |
| 2891 | if (clonedDecl->is<GlobalVarDeclaration>()) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2892 | GlobalVarDeclaration& global = clonedDecl->as<GlobalVarDeclaration>(); |
| 2893 | global.declaration()->as<VarDeclaration>().setVar(clonedVar); |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2894 | } else { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2895 | clonedDecl->as<InterfaceBlock>().setVariable(clonedVar); |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2896 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2897 | |
| 2898 | // Remember this new re-mapping... |
| 2899 | fRemap.insert({sharedVar, clonedVar}); |
| 2900 | |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2901 | // Add the declaring element to this Program |
| 2902 | fNewElements.push_back(std::move(clonedDecl)); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2906 | bool visitExpression(Expression& e) override { |
| 2907 | // Look for references to builtin variables. |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2908 | if (e.is<VariableReference>() && e.as<VariableReference>().variable()->isBuiltin()) { |
| 2909 | const Variable* sharedVar = e.as<VariableReference>().variable(); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2910 | |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2911 | this->cloneVariable(sharedVar->name()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2912 | |
| 2913 | // TODO: SkASSERT(found), once all pre-includes are converted? |
| 2914 | auto found = fRemap.find(sharedVar); |
| 2915 | if (found != fRemap.end()) { |
| 2916 | e.as<VariableReference>().setVariable(found->second); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | return INHERITED::visitExpression(e); |
| 2921 | } |
| 2922 | |
| 2923 | IRGenerator* fGenerator; |
| 2924 | std::unordered_map<const Variable*, const Variable*> fRemap; |
| 2925 | std::vector<std::unique_ptr<ProgramElement>> fNewElements; |
| 2926 | |
| 2927 | using INHERITED = ProgramWriter; |
| 2928 | using INHERITED::visitProgramElement; |
| 2929 | }; |
| 2930 | |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2931 | BuiltinVariableRemapper remapper(this); |
| 2932 | for (auto& e : *fProgramElements) { |
| 2933 | remapper.visitProgramElement(*e); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2934 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 2935 | |
| 2936 | // Vulkan requires certain builtin variables be present, even if they're unused. At one time, |
| 2937 | // validation errors would result if they were missing. Now, it's just (Adreno) driver bugs |
| 2938 | // that drop or corrupt draws if they're missing. |
| 2939 | switch (fKind) { |
| 2940 | case Program::kFragment_Kind: |
| 2941 | remapper.cloneVariable("sk_Clockwise"); |
| 2942 | break; |
| 2943 | default: |
| 2944 | break; |
| 2945 | } |
| 2946 | |
| 2947 | fProgramElements->insert(fProgramElements->begin(), |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 2948 | std::make_move_iterator(remapper.fNewElements.begin()), |
| 2949 | std::make_move_iterator(remapper.fNewElements.end())); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 2950 | } |
| 2951 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2952 | IRGenerator::IRBundle IRGenerator::convertProgram( |
| 2953 | Program::Kind kind, |
| 2954 | const Program::Settings* settings, |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 2955 | const ShaderCapsClass* caps, |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2956 | const ParsedModule& base, |
| 2957 | bool isBuiltinCode, |
| 2958 | const char* text, |
| 2959 | size_t length, |
| 2960 | const std::vector<std::unique_ptr<ExternalValue>>* externalValues) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 2961 | fKind = kind; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2962 | fSettings = settings; |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 2963 | fCaps = caps; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2964 | fSymbolTable = base.fSymbols; |
| 2965 | fIntrinsics = base.fIntrinsics.get(); |
| 2966 | if (fIntrinsics) { |
| 2967 | fIntrinsics->resetAlreadyIncluded(); |
| 2968 | } |
| 2969 | fIsBuiltinCode = isBuiltinCode; |
| 2970 | |
| 2971 | std::vector<std::unique_ptr<ProgramElement>> elements; |
| 2972 | fProgramElements = &elements; |
| 2973 | |
| 2974 | fInputs.reset(); |
| 2975 | fInvocations = -1; |
| 2976 | fRTAdjust = nullptr; |
| 2977 | fRTAdjustInterfaceBlock = nullptr; |
| 2978 | |
| 2979 | fCapsMap.clear(); |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 2980 | if (fCaps) { |
| 2981 | fill_caps(*fCaps, &fCapsMap); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2982 | } else { |
| 2983 | fCapsMap.insert({String("integerSupport"), Program::Settings::Value(true)}); |
| 2984 | } |
| 2985 | |
John Stiles | e51b6a3 | 2020-10-21 15:02:37 -0400 | [diff] [blame] | 2986 | AutoSymbolTable table(this); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 2987 | |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 2988 | if (kind == Program::kGeometry_Kind && !fIsBuiltinCode) { |
| 2989 | // Declare sk_InvocationID programmatically. With invocations support, it's an 'in' builtin. |
| 2990 | // If we're applying the workaround, then it's a plain global. |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 2991 | bool workaround = fCaps && !fCaps->gsInvocationsSupport(); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 2992 | Modifiers m; |
| 2993 | if (!workaround) { |
| 2994 | m.fFlags = Modifiers::kIn_Flag; |
| 2995 | m.fLayout.fBuiltin = SK_INVOCATIONID_BUILTIN; |
| 2996 | } |
| 2997 | auto var = std::make_unique<Variable>(-1, fModifiers->handle(m), "sk_InvocationID", |
| 2998 | fContext.fInt_Type.get(), false, |
| 2999 | Variable::Storage::kGlobal); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 3000 | auto decl = std::make_unique<VarDeclaration>(var.get(), fContext.fInt_Type.get(), |
| 3001 | /*sizes=*/ExpressionArray{}, |
| 3002 | /*value=*/nullptr); |
Brian Osman | 68c1d40 | 2020-10-12 16:36:38 -0400 | [diff] [blame] | 3003 | fSymbolTable->add(std::move(var)); |
| 3004 | fProgramElements->push_back( |
| 3005 | std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl))); |
| 3006 | } |
| 3007 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 3008 | if (externalValues) { |
| 3009 | // Add any external values to the new symbol table, so they're only visible to this Program |
| 3010 | for (const auto& ev : *externalValues) { |
| 3011 | fSymbolTable->addWithoutOwnership(ev.get()); |
| 3012 | } |
| 3013 | } |
| 3014 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 3015 | Parser parser(text, length, *fSymbolTable, fErrors); |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 3016 | fFile = parser.compilationUnit(); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3017 | if (fErrors.errorCount()) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 3018 | return {}; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3019 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3020 | SkASSERT(fFile); |
| 3021 | for (const auto& decl : fFile->root()) { |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3022 | switch (decl.fKind) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3023 | case ASTNode::Kind::kVarDeclarations: { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 3024 | StatementArray decls = this->convertVarDeclarations(decl, |
| 3025 | Variable::Storage::kGlobal); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 3026 | for (auto& varDecl : decls) { |
| 3027 | fProgramElements->push_back(std::make_unique<GlobalVarDeclaration>( |
| 3028 | decl.fOffset, std::move(varDecl))); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3029 | } |
| 3030 | break; |
| 3031 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3032 | case ASTNode::Kind::kEnum: { |
| 3033 | this->convertEnum(decl); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 3034 | break; |
| 3035 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3036 | case ASTNode::Kind::kFunction: { |
| 3037 | this->convertFunction(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3038 | break; |
| 3039 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3040 | case ASTNode::Kind::kModifiers: { |
| 3041 | std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3042 | if (f) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 3043 | fProgramElements->push_back(std::move(f)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3044 | } |
| 3045 | break; |
| 3046 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3047 | case ASTNode::Kind::kInterfaceBlock: { |
| 3048 | std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(decl); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3049 | if (i) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 3050 | fProgramElements->push_back(std::move(i)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3051 | } |
| 3052 | break; |
| 3053 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3054 | case ASTNode::Kind::kExtension: { |
| 3055 | std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset, |
| 3056 | decl.getString()); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3057 | if (e) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 3058 | fProgramElements->push_back(std::move(e)); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3059 | } |
| 3060 | break; |
| 3061 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3062 | case ASTNode::Kind::kSection: { |
| 3063 | std::unique_ptr<Section> s = this->convertSection(decl); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 3064 | if (s) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 3065 | fProgramElements->push_back(std::move(s)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 3066 | } |
| 3067 | break; |
| 3068 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3069 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 3070 | #ifdef SK_DEBUG |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3071 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 3072 | #endif |
| 3073 | break; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3074 | } |
| 3075 | } |
Brian Osman | c95d3a1 | 2020-09-09 10:56:27 -0400 | [diff] [blame] | 3076 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 3077 | // Any variables defined in the pre-includes need to be cloned into the Program |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 3078 | if (!fIsBuiltinCode && fIntrinsics) { |
| 3079 | this->cloneBuiltinVariables(); |
| 3080 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 3081 | |
Brian Osman | c95d3a1 | 2020-09-09 10:56:27 -0400 | [diff] [blame] | 3082 | // Do a final pass looking for dangling FunctionReference or TypeReference expressions |
| 3083 | class FindIllegalExpressions : public ProgramVisitor { |
| 3084 | public: |
| 3085 | FindIllegalExpressions(IRGenerator* generator) : fGenerator(generator) {} |
| 3086 | |
| 3087 | bool visitExpression(const Expression& e) override { |
| 3088 | fGenerator->checkValid(e); |
| 3089 | return INHERITED::visitExpression(e); |
| 3090 | } |
| 3091 | |
| 3092 | IRGenerator* fGenerator; |
| 3093 | using INHERITED = ProgramVisitor; |
| 3094 | using INHERITED::visitProgramElement; |
| 3095 | }; |
| 3096 | for (const auto& pe : *fProgramElements) { |
| 3097 | FindIllegalExpressions{this}.visitProgramElement(*pe); |
| 3098 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3099 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 3100 | fSettings = nullptr; |
| 3101 | |
John Stiles | e51b6a3 | 2020-10-21 15:02:37 -0400 | [diff] [blame] | 3102 | return IRBundle{std::move(elements), this->releaseModifiers(), fSymbolTable, fInputs}; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 3103 | } |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 3104 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 3105 | } // namespace SkSL |