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