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