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