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