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