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