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