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