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