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