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