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