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" |
| 11 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 12 | #include "SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include "ast/SkSLASTBoolLiteral.h" |
| 14 | #include "ast/SkSLASTFieldSuffix.h" |
| 15 | #include "ast/SkSLASTFloatLiteral.h" |
| 16 | #include "ast/SkSLASTIndexSuffix.h" |
| 17 | #include "ast/SkSLASTIntLiteral.h" |
| 18 | #include "ir/SkSLBinaryExpression.h" |
| 19 | #include "ir/SkSLBoolLiteral.h" |
| 20 | #include "ir/SkSLBreakStatement.h" |
| 21 | #include "ir/SkSLConstructor.h" |
| 22 | #include "ir/SkSLContinueStatement.h" |
| 23 | #include "ir/SkSLDiscardStatement.h" |
| 24 | #include "ir/SkSLDoStatement.h" |
| 25 | #include "ir/SkSLExpressionStatement.h" |
| 26 | #include "ir/SkSLField.h" |
| 27 | #include "ir/SkSLFieldAccess.h" |
| 28 | #include "ir/SkSLFloatLiteral.h" |
| 29 | #include "ir/SkSLForStatement.h" |
| 30 | #include "ir/SkSLFunctionCall.h" |
| 31 | #include "ir/SkSLFunctionDeclaration.h" |
| 32 | #include "ir/SkSLFunctionDefinition.h" |
| 33 | #include "ir/SkSLFunctionReference.h" |
| 34 | #include "ir/SkSLIfStatement.h" |
| 35 | #include "ir/SkSLIndexExpression.h" |
| 36 | #include "ir/SkSLInterfaceBlock.h" |
| 37 | #include "ir/SkSLIntLiteral.h" |
| 38 | #include "ir/SkSLLayout.h" |
| 39 | #include "ir/SkSLPostfixExpression.h" |
| 40 | #include "ir/SkSLPrefixExpression.h" |
| 41 | #include "ir/SkSLReturnStatement.h" |
| 42 | #include "ir/SkSLSwizzle.h" |
| 43 | #include "ir/SkSLTernaryExpression.h" |
| 44 | #include "ir/SkSLUnresolvedFunction.h" |
| 45 | #include "ir/SkSLVariable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 46 | #include "ir/SkSLVarDeclarations.h" |
| 47 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | #include "ir/SkSLVariableReference.h" |
| 49 | #include "ir/SkSLWhileStatement.h" |
| 50 | |
| 51 | namespace SkSL { |
| 52 | |
| 53 | class AutoSymbolTable { |
| 54 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 55 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | : fIR(ir) |
| 57 | , fPrevious(fIR->fSymbolTable) { |
| 58 | fIR->pushSymbolTable(); |
| 59 | } |
| 60 | |
| 61 | ~AutoSymbolTable() { |
| 62 | fIR->popSymbolTable(); |
| 63 | ASSERT(fPrevious == fIR->fSymbolTable); |
| 64 | } |
| 65 | |
| 66 | IRGenerator* fIR; |
| 67 | std::shared_ptr<SymbolTable> fPrevious; |
| 68 | }; |
| 69 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 70 | class AutoLoopLevel { |
| 71 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 72 | AutoLoopLevel(IRGenerator* ir) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 73 | : fIR(ir) { |
| 74 | fIR->fLoopLevel++; |
| 75 | } |
| 76 | |
| 77 | ~AutoLoopLevel() { |
| 78 | fIR->fLoopLevel--; |
| 79 | } |
| 80 | |
| 81 | IRGenerator* fIR; |
| 82 | }; |
| 83 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 84 | IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | ErrorReporter& errorReporter) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 86 | : fContext(*context) |
| 87 | , fCurrentFunction(nullptr) |
| 88 | , fSymbolTable(std::move(symbolTable)) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 89 | , fLoopLevel(0) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 90 | , fErrors(errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 91 | |
| 92 | void IRGenerator::pushSymbolTable() { |
| 93 | fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors)); |
| 94 | } |
| 95 | |
| 96 | void IRGenerator::popSymbolTable() { |
| 97 | fSymbolTable = fSymbolTable->fParent; |
| 98 | } |
| 99 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 100 | static void fill_caps(const GrShaderCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) { |
| 101 | #define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name()))); |
| 102 | CAP(fbFetchSupport); |
| 103 | CAP(fbFetchNeedsCustomOutput); |
| 104 | CAP(bindlessTextureSupport); |
| 105 | CAP(dropsTileOnZeroDivide); |
| 106 | CAP(flatInterpolationSupport); |
| 107 | CAP(noperspectiveInterpolationSupport); |
| 108 | CAP(multisampleInterpolationSupport); |
| 109 | CAP(sampleVariablesSupport); |
| 110 | CAP(sampleMaskOverrideCoverageSupport); |
| 111 | CAP(externalTextureSupport); |
| 112 | CAP(texelFetchSupport); |
| 113 | CAP(imageLoadStoreSupport); |
| 114 | CAP(mustEnableAdvBlendEqs); |
| 115 | CAP(mustEnableSpecificAdvBlendEqs); |
| 116 | CAP(mustDeclareFragmentShaderOutput); |
| 117 | CAP(canUseAnyFunctionInShader); |
| 118 | #undef CAP |
| 119 | } |
| 120 | |
| 121 | void IRGenerator::start(const Program::Settings* settings) { |
| 122 | fSettings = settings; |
| 123 | fCapsMap.clear(); |
| 124 | if (settings->fCaps) { |
| 125 | fill_caps(*settings->fCaps, &fCapsMap); |
| 126 | } |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 127 | this->pushSymbolTable(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 128 | fInputs.reset(); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void IRGenerator::finish() { |
| 132 | this->popSymbolTable(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 133 | fSettings = nullptr; |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 134 | } |
| 135 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 136 | std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) { |
| 137 | return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName)); |
| 138 | } |
| 139 | |
| 140 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) { |
| 141 | switch (statement.fKind) { |
| 142 | case ASTStatement::kBlock_Kind: |
| 143 | return this->convertBlock((ASTBlock&) statement); |
| 144 | case ASTStatement::kVarDeclaration_Kind: |
| 145 | return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement); |
| 146 | case ASTStatement::kExpression_Kind: |
| 147 | return this->convertExpressionStatement((ASTExpressionStatement&) statement); |
| 148 | case ASTStatement::kIf_Kind: |
| 149 | return this->convertIf((ASTIfStatement&) statement); |
| 150 | case ASTStatement::kFor_Kind: |
| 151 | return this->convertFor((ASTForStatement&) statement); |
| 152 | case ASTStatement::kWhile_Kind: |
| 153 | return this->convertWhile((ASTWhileStatement&) statement); |
| 154 | case ASTStatement::kDo_Kind: |
| 155 | return this->convertDo((ASTDoStatement&) statement); |
| 156 | case ASTStatement::kReturn_Kind: |
| 157 | return this->convertReturn((ASTReturnStatement&) statement); |
| 158 | case ASTStatement::kBreak_Kind: |
| 159 | return this->convertBreak((ASTBreakStatement&) statement); |
| 160 | case ASTStatement::kContinue_Kind: |
| 161 | return this->convertContinue((ASTContinueStatement&) statement); |
| 162 | case ASTStatement::kDiscard_Kind: |
| 163 | return this->convertDiscard((ASTDiscardStatement&) statement); |
| 164 | default: |
| 165 | ABORT("unsupported statement type: %d\n", statement.fKind); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) { |
| 170 | AutoSymbolTable table(this); |
| 171 | std::vector<std::unique_ptr<Statement>> statements; |
| 172 | for (size_t i = 0; i < block.fStatements.size(); i++) { |
| 173 | std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]); |
| 174 | if (!statement) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | statements.push_back(std::move(statement)); |
| 178 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 179 | return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( |
| 183 | const ASTVarDeclarationStatement& s) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 184 | auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 185 | if (!decl) { |
| 186 | return nullptr; |
| 187 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 188 | return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | } |
| 190 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 191 | std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl, |
| 192 | Variable::Storage storage) { |
| 193 | std::vector<VarDeclaration> variables; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 194 | const Type* baseType = this->convertType(*decl.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | if (!baseType) { |
| 196 | return nullptr; |
| 197 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 198 | for (const auto& varDecl : decl.fVars) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 199 | const Type* type = baseType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 200 | ASSERT(type->kind() != Type::kArray_Kind); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 201 | std::vector<std::unique_ptr<Expression>> sizes; |
| 202 | for (const auto& rawSize : varDecl.fSizes) { |
| 203 | if (rawSize) { |
| 204 | auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 205 | if (!size) { |
| 206 | return nullptr; |
| 207 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 208 | SkString name = type->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 209 | uint64_t count; |
| 210 | if (size->fKind == Expression::kIntLiteral_Kind) { |
| 211 | count = ((IntLiteral&) *size).fValue; |
| 212 | if (count <= 0) { |
| 213 | fErrors.error(size->fPosition, "array size must be positive"); |
| 214 | } |
| 215 | name += "[" + to_string(count) + "]"; |
| 216 | } else { |
| 217 | count = -1; |
| 218 | name += "[]"; |
| 219 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 220 | type = new Type(name, Type::kArray_Kind, *type, (int) count); |
| 221 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 222 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 223 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 224 | type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1); |
| 225 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 226 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 229 | auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, decl.fModifiers, |
| 230 | varDecl.fName, *type, storage)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 231 | std::unique_ptr<Expression> value; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 232 | if (varDecl.fValue) { |
| 233 | value = this->convertExpression(*varDecl.fValue); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | if (!value) { |
| 235 | return nullptr; |
| 236 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 237 | value = this->coerce(std::move(value), *type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 238 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 239 | if (storage == Variable::kGlobal_Storage && varDecl.fName == SkString("sk_FragColor") && |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 240 | (*fSymbolTable)[varDecl.fName]) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 241 | // already defined, ignore |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 242 | } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] && |
| 243 | (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 244 | ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 245 | // already defined, just update the modifiers |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 246 | Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName]; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 247 | old->fModifiers = var->fModifiers; |
| 248 | } else { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 249 | variables.emplace_back(var.get(), std::move(sizes), std::move(value)); |
| 250 | fSymbolTable->add(varDecl.fName, std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 251 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 252 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 253 | return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 254 | baseType, |
| 255 | std::move(variables))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 258 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration( |
| 259 | const ASTModifiersDeclaration& m) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 260 | return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(m.fModifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 263 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 264 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 265 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | if (!test) { |
| 267 | return nullptr; |
| 268 | } |
| 269 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); |
| 270 | if (!ifTrue) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | std::unique_ptr<Statement> ifFalse; |
| 274 | if (s.fIfFalse) { |
| 275 | ifFalse = this->convertStatement(*s.fIfFalse); |
| 276 | if (!ifFalse) { |
| 277 | return nullptr; |
| 278 | } |
| 279 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 280 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 281 | // static boolean value, fold down to a single branch |
| 282 | if (((BoolLiteral&) *test).fValue) { |
| 283 | return ifTrue; |
| 284 | } else if (s.fIfFalse) { |
| 285 | return ifFalse; |
| 286 | } else { |
| 287 | // False & no else clause. Not an error, so don't return null! |
| 288 | std::vector<std::unique_ptr<Statement>> empty; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 289 | return std::unique_ptr<Statement>(new Block(s.fPosition, std::move(empty), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 290 | fSymbolTable)); |
| 291 | } |
| 292 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 293 | return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 294 | std::move(ifTrue), std::move(ifFalse))); |
| 295 | } |
| 296 | |
| 297 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 298 | AutoLoopLevel level(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 299 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 300 | std::unique_ptr<Statement> initializer; |
| 301 | if (f.fInitializer) { |
| 302 | initializer = this->convertStatement(*f.fInitializer); |
| 303 | if (!initializer) { |
| 304 | return nullptr; |
| 305 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 306 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 307 | std::unique_ptr<Expression> test; |
| 308 | if (f.fTest) { |
| 309 | test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type); |
| 310 | if (!test) { |
| 311 | return nullptr; |
| 312 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 313 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 314 | std::unique_ptr<Expression> next; |
| 315 | if (f.fNext) { |
| 316 | next = this->convertExpression(*f.fNext); |
| 317 | if (!next) { |
| 318 | return nullptr; |
| 319 | } |
| 320 | this->checkValid(*next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 321 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 322 | std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement); |
| 323 | if (!statement) { |
| 324 | return nullptr; |
| 325 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 326 | return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 327 | std::move(test), std::move(next), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 328 | std::move(statement), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 332 | AutoLoopLevel level(this); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 333 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 334 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 335 | if (!test) { |
| 336 | return nullptr; |
| 337 | } |
| 338 | std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement); |
| 339 | if (!statement) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test), |
| 343 | std::move(statement))); |
| 344 | } |
| 345 | |
| 346 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 347 | AutoLoopLevel level(this); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 348 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest), |
| 349 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 350 | if (!test) { |
| 351 | return nullptr; |
| 352 | } |
| 353 | std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement); |
| 354 | if (!statement) { |
| 355 | return nullptr; |
| 356 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 357 | return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 358 | std::move(test))); |
| 359 | } |
| 360 | |
| 361 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement( |
| 362 | const ASTExpressionStatement& s) { |
| 363 | std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression); |
| 364 | if (!e) { |
| 365 | return nullptr; |
| 366 | } |
| 367 | this->checkValid(*e); |
| 368 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 369 | } |
| 370 | |
| 371 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) { |
| 372 | ASSERT(fCurrentFunction); |
| 373 | if (r.fExpression) { |
| 374 | std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression); |
| 375 | if (!result) { |
| 376 | return nullptr; |
| 377 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 378 | if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 379 | fErrors.error(result->fPosition, "may not return a value from a void function"); |
| 380 | } else { |
| 381 | result = this->coerce(std::move(result), fCurrentFunction->fReturnType); |
| 382 | if (!result) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | } |
| 386 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 387 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 388 | if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | fErrors.error(r.fPosition, "expected function to return '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 390 | fCurrentFunction->fReturnType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 391 | } |
| 392 | return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 397 | if (fLoopLevel > 0) { |
| 398 | return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); |
| 399 | } else { |
| 400 | fErrors.error(b.fPosition, "break statement must be inside a loop"); |
| 401 | return nullptr; |
| 402 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 406 | if (fLoopLevel > 0) { |
| 407 | return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); |
| 408 | } else { |
| 409 | fErrors.error(c.fPosition, "continue statement must be inside a loop"); |
| 410 | return nullptr; |
| 411 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) { |
| 415 | return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); |
| 416 | } |
| 417 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 418 | std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 419 | const Type* returnType = this->convertType(*f.fReturnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 420 | if (!returnType) { |
| 421 | return nullptr; |
| 422 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 423 | std::vector<const Variable*> parameters; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 424 | for (const auto& param : f.fParameters) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 425 | const Type* type = this->convertType(*param->fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 426 | if (!type) { |
| 427 | return nullptr; |
| 428 | } |
| 429 | for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { |
| 430 | int size = param->fSizes[j]; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 431 | SkString name = type->name() + "[" + to_string(size) + "]"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 432 | Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size); |
| 433 | fSymbolTable->takeOwnership(newType); |
| 434 | type = newType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 435 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 436 | SkString name = param->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 437 | Position pos = param->fPosition; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 438 | Variable* var = new Variable(pos, param->fModifiers, std::move(name), *type, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 439 | Variable::kParameter_Storage); |
| 440 | fSymbolTable->takeOwnership(var); |
| 441 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 445 | const FunctionDeclaration* decl = nullptr; |
| 446 | auto entry = (*fSymbolTable)[f.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 447 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 448 | std::vector<const FunctionDeclaration*> functions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 449 | switch (entry->fKind) { |
| 450 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 451 | functions = ((UnresolvedFunction*) entry)->fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 452 | break; |
| 453 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 454 | functions.push_back((FunctionDeclaration*) entry); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 455 | break; |
| 456 | default: |
| 457 | fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); |
| 458 | return nullptr; |
| 459 | } |
| 460 | for (const auto& other : functions) { |
| 461 | ASSERT(other->fName == f.fName); |
| 462 | if (parameters.size() == other->fParameters.size()) { |
| 463 | bool match = true; |
| 464 | for (size_t i = 0; i < parameters.size(); i++) { |
| 465 | if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 466 | match = false; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if (match) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 471 | if (*returnType != other->fReturnType) { |
| 472 | FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 473 | fErrors.error(f.fPosition, "functions '" + newDecl.description() + |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 474 | "' and '" + other->description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 475 | "' differ only in return type"); |
| 476 | return nullptr; |
| 477 | } |
| 478 | decl = other; |
| 479 | for (size_t i = 0; i < parameters.size(); i++) { |
| 480 | if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 481 | fErrors.error(f.fPosition, "modifiers on parameter " + |
| 482 | to_string((uint64_t) i + 1) + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 483 | " differ between declaration and " |
| 484 | "definition"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 485 | return nullptr; |
| 486 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 487 | } |
| 488 | if (other->fDefined) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 489 | fErrors.error(f.fPosition, "duplicate definition of " + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 490 | other->description()); |
| 491 | } |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | if (!decl) { |
| 498 | // couldn't find an existing declaration |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 499 | auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fPosition, |
| 500 | f.fName, |
| 501 | parameters, |
| 502 | *returnType)); |
| 503 | decl = newDecl.get(); |
| 504 | fSymbolTable->add(decl->fName, std::move(newDecl)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 505 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 506 | if (f.fBody) { |
| 507 | ASSERT(!fCurrentFunction); |
| 508 | fCurrentFunction = decl; |
| 509 | decl->fDefined = true; |
| 510 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 511 | AutoSymbolTable table(this); |
| 512 | for (size_t i = 0; i < parameters.size(); i++) { |
| 513 | fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 514 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 515 | std::unique_ptr<Block> body = this->convertBlock(*f.fBody); |
| 516 | fCurrentFunction = nullptr; |
| 517 | if (!body) { |
| 518 | return nullptr; |
| 519 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 520 | return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 521 | std::move(body))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | } |
| 523 | return nullptr; |
| 524 | } |
| 525 | |
| 526 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) { |
| 527 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 528 | AutoSymbolTable table(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 529 | std::vector<Type::Field> fields; |
| 530 | for (size_t i = 0; i < intf.fDeclarations.size(); i++) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 531 | std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations( |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 532 | *intf.fDeclarations[i], |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 533 | Variable::kGlobal_Storage); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 534 | if (!decl) { |
| 535 | return nullptr; |
| 536 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 537 | for (const auto& var : decl->fVars) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 538 | fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 539 | &var.fVar->fType)); |
| 540 | if (var.fValue) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 541 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 542 | "initializers are not permitted on interface block fields"); |
| 543 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 544 | if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 545 | Modifiers::kOut_Flag | |
| 546 | Modifiers::kUniform_Flag | |
| 547 | Modifiers::kConst_Flag)) { |
| 548 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 549 | "interface block fields may not have storage qualifiers"); |
| 550 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 551 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 552 | } |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 553 | Type* type = new Type(intf.fPosition, intf.fInterfaceName, fields); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 554 | old->takeOwnership(type); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 555 | SkString name = intf.fValueName.size() > 0 ? intf.fValueName : intf.fInterfaceName; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 556 | Variable* var = new Variable(intf.fPosition, intf.fModifiers, name, *type, |
| 557 | Variable::kGlobal_Storage); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 558 | old->takeOwnership(var); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 559 | if (intf.fValueName.size()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 560 | old->addWithoutOwnership(intf.fValueName, var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 561 | } else { |
| 562 | for (size_t i = 0; i < fields.size(); i++) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 563 | old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 564 | (int) i))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 567 | return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 568 | } |
| 569 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 570 | const Type* IRGenerator::convertType(const ASTType& type) { |
| 571 | const Symbol* result = (*fSymbolTable)[type.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 572 | if (result && result->fKind == Symbol::kType_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 573 | return (const Type*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 574 | } |
| 575 | fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
| 579 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { |
| 580 | switch (expr.fKind) { |
| 581 | case ASTExpression::kIdentifier_Kind: |
| 582 | return this->convertIdentifier((ASTIdentifier&) expr); |
| 583 | case ASTExpression::kBool_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 584 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | ((ASTBoolLiteral&) expr).fValue)); |
| 586 | case ASTExpression::kInt_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 587 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 588 | ((ASTIntLiteral&) expr).fValue)); |
| 589 | case ASTExpression::kFloat_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 590 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 591 | ((ASTFloatLiteral&) expr).fValue)); |
| 592 | case ASTExpression::kBinary_Kind: |
| 593 | return this->convertBinaryExpression((ASTBinaryExpression&) expr); |
| 594 | case ASTExpression::kPrefix_Kind: |
| 595 | return this->convertPrefixExpression((ASTPrefixExpression&) expr); |
| 596 | case ASTExpression::kSuffix_Kind: |
| 597 | return this->convertSuffixExpression((ASTSuffixExpression&) expr); |
| 598 | case ASTExpression::kTernary_Kind: |
| 599 | return this->convertTernaryExpression((ASTTernaryExpression&) expr); |
| 600 | default: |
| 601 | ABORT("unsupported expression type: %d\n", expr.fKind); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 606 | const Symbol* result = (*fSymbolTable)[identifier.fText]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 607 | if (!result) { |
| 608 | fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'"); |
| 609 | return nullptr; |
| 610 | } |
| 611 | switch (result->fKind) { |
| 612 | case Symbol::kFunctionDeclaration_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 613 | std::vector<const FunctionDeclaration*> f = { |
| 614 | (const FunctionDeclaration*) result |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 615 | }; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 616 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 617 | identifier.fPosition, |
| 618 | f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 619 | } |
| 620 | case Symbol::kUnresolvedFunction_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 621 | const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 622 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 623 | identifier.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 624 | f->fFunctions)); |
| 625 | } |
| 626 | case Symbol::kVariable_Kind: { |
Ethan Nicholas | de4d301 | 2017-01-19 16:58:02 -0500 | [diff] [blame] | 627 | const Variable* var = (const Variable*) result; |
| 628 | if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) { |
| 629 | fInputs.fFlipY = true; |
| 630 | if (fSettings->fFlipY && |
| 631 | (!fSettings->fCaps || |
| 632 | !fSettings->fCaps->fragCoordConventionsExtensionString())) { |
| 633 | fInputs.fRTHeight = true; |
| 634 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 635 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 636 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
| 637 | return std::unique_ptr<VariableReference>(new VariableReference( |
| 638 | identifier.fPosition, |
| 639 | *var, |
| 640 | VariableReference::kRead_RefKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 641 | } |
| 642 | case Symbol::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 643 | const Field* field = (const Field*) result; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 644 | VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner, |
| 645 | VariableReference::kRead_RefKind); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 646 | return std::unique_ptr<Expression>(new FieldAccess( |
| 647 | std::unique_ptr<Expression>(base), |
| 648 | field->fFieldIndex, |
| 649 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 650 | } |
| 651 | case Symbol::kType_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 652 | const Type* t = (const Type*) result; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 653 | return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 654 | *t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 655 | } |
| 656 | default: |
| 657 | ABORT("unsupported symbol type %d\n", result->fKind); |
| 658 | } |
| 659 | |
| 660 | } |
| 661 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 662 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 663 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 664 | if (!expr) { |
| 665 | return nullptr; |
| 666 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 667 | if (expr->fType == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 668 | return expr; |
| 669 | } |
| 670 | this->checkValid(*expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 671 | if (expr->fType == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 672 | return nullptr; |
| 673 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 674 | if (!expr->fType.canCoerceTo(type)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 675 | fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 676 | expr->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 677 | return nullptr; |
| 678 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 679 | if (type.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 680 | std::vector<std::unique_ptr<Expression>> args; |
| 681 | args.push_back(std::move(expr)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 682 | ASTIdentifier id(Position(), type.description()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 683 | std::unique_ptr<Expression> ctor = this->convertIdentifier(id); |
| 684 | ASSERT(ctor); |
| 685 | return this->call(Position(), std::move(ctor), std::move(args)); |
| 686 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 687 | std::vector<std::unique_ptr<Expression>> args; |
| 688 | args.push_back(std::move(expr)); |
| 689 | return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 690 | } |
| 691 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 692 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
| 693 | if (left.kind() == Type::kMatrix_Kind) { |
| 694 | return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind; |
| 695 | } |
| 696 | return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind; |
| 697 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 698 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 699 | /** |
| 700 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 701 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 702 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 703 | static bool determine_binary_type(const Context& context, |
| 704 | Token::Kind op, |
| 705 | const Type& left, |
| 706 | const Type& right, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 707 | const Type** outLeftType, |
| 708 | const Type** outRightType, |
| 709 | const Type** outResultType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 710 | bool tryFlipped) { |
| 711 | bool isLogical; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 712 | bool validMatrixOrVectorOp; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 713 | switch (op) { |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 714 | case Token::EQ: |
| 715 | *outLeftType = &left; |
| 716 | *outRightType = &left; |
| 717 | *outResultType = &left; |
| 718 | return right.canCoerceTo(left); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 719 | case Token::EQEQ: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 720 | case Token::NEQ: |
| 721 | isLogical = true; |
| 722 | validMatrixOrVectorOp = true; |
| 723 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 724 | case Token::LT: // fall through |
| 725 | case Token::GT: // fall through |
| 726 | case Token::LTEQ: // fall through |
| 727 | case Token::GTEQ: |
| 728 | isLogical = true; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 729 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 730 | break; |
| 731 | case Token::LOGICALOR: // fall through |
| 732 | case Token::LOGICALAND: // fall through |
| 733 | case Token::LOGICALXOR: // fall through |
| 734 | case Token::LOGICALOREQ: // fall through |
| 735 | case Token::LOGICALANDEQ: // fall through |
| 736 | case Token::LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 737 | *outLeftType = context.fBool_Type.get(); |
| 738 | *outRightType = context.fBool_Type.get(); |
| 739 | *outResultType = context.fBool_Type.get(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 740 | return left.canCoerceTo(*context.fBool_Type) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 741 | right.canCoerceTo(*context.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 742 | case Token::STAR: // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 743 | case Token::STAREQ: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 744 | if (is_matrix_multiply(left, right)) { |
| 745 | // determine final component type |
| 746 | if (determine_binary_type(context, Token::STAR, left.componentType(), |
| 747 | right.componentType(), outLeftType, outRightType, |
| 748 | outResultType, false)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 749 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 750 | left.rows());; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 751 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 752 | right.rows());; |
| 753 | int leftColumns = left.columns(); |
| 754 | int leftRows = left.rows(); |
| 755 | int rightColumns; |
| 756 | int rightRows; |
| 757 | if (right.kind() == Type::kVector_Kind) { |
| 758 | // matrix * vector treats the vector as a column vector, so we need to |
| 759 | // transpose it |
| 760 | rightColumns = right.rows(); |
| 761 | rightRows = right.columns(); |
| 762 | ASSERT(rightColumns == 1); |
| 763 | } else { |
| 764 | rightColumns = right.columns(); |
| 765 | rightRows = right.rows(); |
| 766 | } |
| 767 | if (rightColumns > 1) { |
| 768 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 769 | leftRows); |
| 770 | } else { |
| 771 | // result was a column vector, transpose it back to a row |
| 772 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 773 | rightColumns); |
| 774 | } |
| 775 | return leftColumns == rightRows; |
| 776 | } else { |
| 777 | return false; |
| 778 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 779 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 780 | isLogical = false; |
| 781 | validMatrixOrVectorOp = true; |
| 782 | break; |
| 783 | case Token::PLUS: // fall through |
| 784 | case Token::PLUSEQ: // fall through |
| 785 | case Token::MINUS: // fall through |
| 786 | case Token::MINUSEQ: // fall through |
| 787 | case Token::SLASH: // fall through |
| 788 | case Token::SLASHEQ: |
| 789 | isLogical = false; |
| 790 | validMatrixOrVectorOp = true; |
| 791 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 792 | default: |
| 793 | isLogical = false; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 794 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 795 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 796 | bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind; |
| 797 | // FIXME: incorrect for shift |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 798 | if (right.canCoerceTo(left) && (left.kind() == Type::kScalar_Kind || |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 799 | (isVectorOrMatrix && validMatrixOrVectorOp))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 800 | *outLeftType = &left; |
| 801 | *outRightType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 802 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 803 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 804 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 805 | *outResultType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 806 | } |
| 807 | return true; |
| 808 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 809 | if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 810 | (right.kind() == Type::kScalar_Kind)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 811 | if (determine_binary_type(context, op, left.componentType(), right, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 812 | outRightType, outResultType, false)) { |
| 813 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 814 | if (!isLogical) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 815 | *outResultType = &(*outResultType)->toCompound(context, left.columns(), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 816 | left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 817 | } |
| 818 | return true; |
| 819 | } |
| 820 | return false; |
| 821 | } |
| 822 | if (tryFlipped) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 823 | return determine_binary_type(context, op, right, left, outRightType, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 824 | outResultType, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 825 | } |
| 826 | return false; |
| 827 | } |
| 828 | |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 829 | std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left, |
| 830 | Token::Kind op, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 831 | const Expression& right) const { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 832 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 833 | // precision to calculate the results and hope the result makes sense. The plan is to move the |
| 834 | // Skia caps into SkSL, so we have access to all of them including the precisions of the various |
| 835 | // types, which will let us be more intelligent about this. |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 836 | if (left.fKind == Expression::kBoolLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 837 | right.fKind == Expression::kBoolLiteral_Kind) { |
| 838 | bool leftVal = ((BoolLiteral&) left).fValue; |
| 839 | bool rightVal = ((BoolLiteral&) right).fValue; |
| 840 | bool result; |
| 841 | switch (op) { |
| 842 | case Token::LOGICALAND: result = leftVal && rightVal; break; |
| 843 | case Token::LOGICALOR: result = leftVal || rightVal; break; |
| 844 | case Token::LOGICALXOR: result = leftVal ^ rightVal; break; |
| 845 | default: return nullptr; |
| 846 | } |
| 847 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fPosition, result)); |
| 848 | } |
| 849 | #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fPosition, \ |
| 850 | leftVal op rightVal)) |
| 851 | if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) { |
| 852 | int64_t leftVal = ((IntLiteral&) left).fValue; |
| 853 | int64_t rightVal = ((IntLiteral&) right).fValue; |
| 854 | switch (op) { |
| 855 | case Token::PLUS: return RESULT(Int, +); |
| 856 | case Token::MINUS: return RESULT(Int, -); |
| 857 | case Token::STAR: return RESULT(Int, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 858 | case Token::SLASH: |
| 859 | if (rightVal) { |
| 860 | return RESULT(Int, /); |
| 861 | } |
| 862 | fErrors.error(right.fPosition, "division by zero"); |
| 863 | return nullptr; |
Ethan Nicholas | 2503ab6 | 2017-01-05 10:44:25 -0500 | [diff] [blame] | 864 | case Token::PERCENT: |
| 865 | if (rightVal) { |
| 866 | return RESULT(Int, %); |
| 867 | } |
| 868 | fErrors.error(right.fPosition, "division by zero"); |
| 869 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 870 | case Token::BITWISEAND: return RESULT(Int, &); |
| 871 | case Token::BITWISEOR: return RESULT(Int, |); |
| 872 | case Token::BITWISEXOR: return RESULT(Int, ^); |
| 873 | case Token::SHL: return RESULT(Int, <<); |
| 874 | case Token::SHR: return RESULT(Int, >>); |
| 875 | case Token::EQEQ: return RESULT(Bool, ==); |
| 876 | case Token::NEQ: return RESULT(Bool, !=); |
| 877 | case Token::GT: return RESULT(Bool, >); |
| 878 | case Token::GTEQ: return RESULT(Bool, >=); |
| 879 | case Token::LT: return RESULT(Bool, <); |
| 880 | case Token::LTEQ: return RESULT(Bool, <=); |
| 881 | default: return nullptr; |
| 882 | } |
| 883 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 884 | if (left.fKind == Expression::kFloatLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 885 | right.fKind == Expression::kFloatLiteral_Kind) { |
| 886 | double leftVal = ((FloatLiteral&) left).fValue; |
| 887 | double rightVal = ((FloatLiteral&) right).fValue; |
| 888 | switch (op) { |
| 889 | case Token::PLUS: return RESULT(Float, +); |
| 890 | case Token::MINUS: return RESULT(Float, -); |
| 891 | case Token::STAR: return RESULT(Float, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 892 | case Token::SLASH: |
| 893 | if (rightVal) { |
| 894 | return RESULT(Float, /); |
| 895 | } |
| 896 | fErrors.error(right.fPosition, "division by zero"); |
| 897 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 898 | case Token::EQEQ: return RESULT(Bool, ==); |
| 899 | case Token::NEQ: return RESULT(Bool, !=); |
| 900 | case Token::GT: return RESULT(Bool, >); |
| 901 | case Token::GTEQ: return RESULT(Bool, >=); |
| 902 | case Token::LT: return RESULT(Bool, <); |
| 903 | case Token::LTEQ: return RESULT(Bool, <=); |
| 904 | default: return nullptr; |
| 905 | } |
| 906 | } |
| 907 | #undef RESULT |
| 908 | return nullptr; |
| 909 | } |
| 910 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 911 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( |
| 912 | const ASTBinaryExpression& expression) { |
| 913 | std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft); |
| 914 | if (!left) { |
| 915 | return nullptr; |
| 916 | } |
| 917 | std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight); |
| 918 | if (!right) { |
| 919 | return nullptr; |
| 920 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 921 | const Type* leftType; |
| 922 | const Type* rightType; |
| 923 | const Type* resultType; |
| 924 | if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 925 | &rightType, &resultType, |
| 926 | !Token::IsAssignment(expression.fOperator))) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 927 | fErrors.error(expression.fPosition, "type mismatch: '" + |
| 928 | Token::OperatorName(expression.fOperator) + |
| 929 | "' cannot operate on '" + left->fType.fName + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 930 | "', '" + right->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 931 | return nullptr; |
| 932 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 933 | if (Token::IsAssignment(expression.fOperator)) { |
| 934 | this->markWrittenTo(*left, expression.fOperator != Token::EQ); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 935 | } |
| 936 | left = this->coerce(std::move(left), *leftType); |
| 937 | right = this->coerce(std::move(right), *rightType); |
| 938 | if (!left || !right) { |
| 939 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 940 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 941 | std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator, |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 942 | *right.get()); |
| 943 | if (!result) { |
| 944 | result = std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition, |
| 945 | std::move(left), |
| 946 | expression.fOperator, |
| 947 | std::move(right), |
| 948 | *resultType)); |
| 949 | } |
| 950 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 953 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 954 | const ASTTernaryExpression& expression) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 955 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 956 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 957 | if (!test) { |
| 958 | return nullptr; |
| 959 | } |
| 960 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue); |
| 961 | if (!ifTrue) { |
| 962 | return nullptr; |
| 963 | } |
| 964 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse); |
| 965 | if (!ifFalse) { |
| 966 | return nullptr; |
| 967 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 968 | const Type* trueType; |
| 969 | const Type* falseType; |
| 970 | const Type* resultType; |
| 971 | if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType, |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 972 | &falseType, &resultType, true) || trueType != falseType) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 973 | fErrors.error(expression.fPosition, "ternary operator result mismatch: '" + |
| 974 | ifTrue->fType.fName + "', '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 975 | ifFalse->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 976 | return nullptr; |
| 977 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 978 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 979 | if (!ifTrue) { |
| 980 | return nullptr; |
| 981 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 982 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 983 | if (!ifFalse) { |
| 984 | return nullptr; |
| 985 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 986 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 987 | // static boolean test, just return one of the branches |
| 988 | if (((BoolLiteral&) *test).fValue) { |
| 989 | return ifTrue; |
| 990 | } else { |
| 991 | return ifFalse; |
| 992 | } |
| 993 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 994 | return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 995 | std::move(test), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 996 | std::move(ifTrue), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 997 | std::move(ifFalse))); |
| 998 | } |
| 999 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1000 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 1001 | const FunctionDeclaration& function, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1002 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 1003 | if (function.fParameters.size() != arguments.size()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1004 | SkString msg = "call to '" + function.fName + "' expected " + |
| 1005 | to_string((uint64_t) function.fParameters.size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1006 | " argument"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1007 | if (function.fParameters.size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1008 | msg += "s"; |
| 1009 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1010 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1011 | fErrors.error(position, msg); |
| 1012 | return nullptr; |
| 1013 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1014 | std::vector<const Type*> types; |
| 1015 | const Type* returnType; |
| 1016 | if (!function.determineFinalTypes(arguments, &types, &returnType)) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1017 | SkString msg = "no match for " + function.fName + "("; |
| 1018 | SkString separator; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1019 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1020 | msg += separator; |
| 1021 | separator = ", "; |
| 1022 | msg += arguments[i]->fType.description(); |
| 1023 | } |
| 1024 | msg += ")"; |
| 1025 | fErrors.error(position, msg); |
| 1026 | return nullptr; |
| 1027 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1028 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1029 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1030 | if (!arguments[i]) { |
| 1031 | return nullptr; |
| 1032 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1033 | if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1034 | this->markWrittenTo(*arguments[i], true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1037 | return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType, function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1038 | std::move(arguments))); |
| 1039 | } |
| 1040 | |
| 1041 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1042 | * Determines the cost of coercing the arguments of a function to the required types. Returns true |
| 1043 | * if the cost could be computed, false if the call is not valid. Cost has no particular meaning |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1044 | * other than "lower costs are preferred". |
| 1045 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1046 | bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1047 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 1048 | int* outCost) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1049 | if (function.fParameters.size() != arguments.size()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1050 | return false; |
| 1051 | } |
| 1052 | int total = 0; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1053 | std::vector<const Type*> types; |
| 1054 | const Type* ignored; |
| 1055 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
| 1056 | return false; |
| 1057 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1058 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1059 | int cost; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1060 | if (arguments[i]->fType.determineCoercionCost(*types[i], &cost)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1061 | total += cost; |
| 1062 | } else { |
| 1063 | return false; |
| 1064 | } |
| 1065 | } |
| 1066 | *outCost = total; |
| 1067 | return true; |
| 1068 | } |
| 1069 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1070 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 1071 | std::unique_ptr<Expression> functionValue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1072 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 1073 | if (functionValue->fKind == Expression::kTypeReference_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1074 | return this->convertConstructor(position, |
| 1075 | ((TypeReference&) *functionValue).fValue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1076 | std::move(arguments)); |
| 1077 | } |
| 1078 | if (functionValue->fKind != Expression::kFunctionReference_Kind) { |
| 1079 | fErrors.error(position, "'" + functionValue->description() + "' is not a function"); |
| 1080 | return nullptr; |
| 1081 | } |
| 1082 | FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 1083 | int bestCost = INT_MAX; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1084 | const FunctionDeclaration* best = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1085 | if (ref->fFunctions.size() > 1) { |
| 1086 | for (const auto& f : ref->fFunctions) { |
| 1087 | int cost; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1088 | if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1089 | bestCost = cost; |
| 1090 | best = f; |
| 1091 | } |
| 1092 | } |
| 1093 | if (best) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1094 | return this->call(position, *best, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1095 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1096 | SkString msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 1097 | SkString separator; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1098 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1099 | msg += separator; |
| 1100 | separator = ", "; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1101 | msg += arguments[i]->fType.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1102 | } |
| 1103 | msg += ")"; |
| 1104 | fErrors.error(position, msg); |
| 1105 | return nullptr; |
| 1106 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1107 | return this->call(position, *ref->fFunctions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | std::unique_ptr<Expression> IRGenerator::convertConstructor( |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1111 | Position position, |
| 1112 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1113 | std::vector<std::unique_ptr<Expression>> args) { |
| 1114 | // FIXME: add support for structs and arrays |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1115 | Type::Kind kind = type.kind(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1116 | if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1117 | kind != Type::kArray_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1118 | fErrors.error(position, "cannot construct '" + type.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1119 | return nullptr; |
| 1120 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1121 | if (type == *fContext.fFloat_Type && args.size() == 1 && |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1122 | args[0]->fKind == Expression::kIntLiteral_Kind) { |
| 1123 | int64_t value = ((IntLiteral&) *args[0]).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1124 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1125 | } |
| 1126 | if (args.size() == 1 && args[0]->fType == type) { |
| 1127 | // argument is already the right type, just return it |
| 1128 | return std::move(args[0]); |
| 1129 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1130 | if (type.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1131 | if (args.size() != 1) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1132 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1133 | "' constructor, (expected exactly 1 argument, but found " + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1134 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1135 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1136 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1137 | if (args[0]->fType == *fContext.fBool_Type) { |
| 1138 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0)); |
| 1139 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1140 | return std::unique_ptr<Expression>( |
| 1141 | new TernaryExpression(position, std::move(args[0]), |
| 1142 | this->coerce(std::move(one), type), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1143 | this->coerce(std::move(zero), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1144 | type))); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1145 | } else if (!args[0]->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1146 | fErrors.error(position, "invalid argument to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1147 | "' constructor (expected a number or bool, but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1148 | args[0]->fType.description() + "')"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1149 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1150 | if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type || |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1151 | type == *fContext.fUInt_Type)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1152 | return std::unique_ptr<Expression>(new IntLiteral(fContext, |
| 1153 | position, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1154 | ((IntLiteral&) *args[0]).fValue, |
| 1155 | &type)); |
| 1156 | } |
| 1157 | } else if (kind == Type::kArray_Kind) { |
| 1158 | const Type& base = type.componentType(); |
| 1159 | for (size_t i = 0; i < args.size(); i++) { |
| 1160 | args[i] = this->coerce(std::move(args[i]), base); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1161 | if (!args[i]) { |
| 1162 | return nullptr; |
| 1163 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1164 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1165 | } else { |
| 1166 | ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); |
| 1167 | int actual = 0; |
| 1168 | for (size_t i = 0; i < args.size(); i++) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1169 | if (args[i]->fType.kind() == Type::kVector_Kind || |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1170 | args[i]->fType.kind() == Type::kMatrix_Kind) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1171 | if (type.componentType().isNumber() != |
| 1172 | args[i]->fType.componentType().isNumber()) { |
| 1173 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1174 | "parameter to '" + type.description() + |
| 1175 | "' constructor"); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1176 | return nullptr; |
| 1177 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1178 | actual += args[i]->fType.rows() * args[i]->fType.columns(); |
| 1179 | } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1180 | actual += 1; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1181 | if (type.kind() != Type::kScalar_Kind) { |
| 1182 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1183 | if (!args[i]) { |
| 1184 | return nullptr; |
| 1185 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1186 | } |
| 1187 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1188 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1189 | "parameter to '" + type.description() + "' constructor"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1190 | return nullptr; |
| 1191 | } |
| 1192 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1193 | int min = type.rows() * type.columns(); |
| 1194 | int max = type.columns() > 1 ? INT_MAX : min; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1195 | if ((actual < min || actual > max) && |
| 1196 | !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1197 | fErrors.error(position, "invalid arguments to '" + type.description() + |
| 1198 | "' constructor (expected " + to_string(min) + " scalar" + |
| 1199 | (min == 1 ? "" : "s") + ", but found " + to_string(actual) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1200 | ")"); |
| 1201 | return nullptr; |
| 1202 | } |
| 1203 | } |
| 1204 | return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args))); |
| 1205 | } |
| 1206 | |
| 1207 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( |
| 1208 | const ASTPrefixExpression& expression) { |
| 1209 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand); |
| 1210 | if (!base) { |
| 1211 | return nullptr; |
| 1212 | } |
| 1213 | switch (expression.fOperator) { |
| 1214 | case Token::PLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1215 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1216 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1217 | "'+' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1218 | return nullptr; |
| 1219 | } |
| 1220 | return base; |
| 1221 | case Token::MINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1222 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1223 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1224 | "'-' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1225 | return nullptr; |
| 1226 | } |
| 1227 | if (base->fKind == Expression::kIntLiteral_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1228 | return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1229 | -((IntLiteral&) *base).fValue)); |
| 1230 | } |
| 1231 | if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 1232 | double value = -((FloatLiteral&) *base).fValue; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1233 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1234 | value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1235 | } |
| 1236 | return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base))); |
| 1237 | case Token::PLUSPLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1238 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1239 | fErrors.error(expression.fPosition, |
| 1240 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1241 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1242 | return nullptr; |
| 1243 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1244 | this->markWrittenTo(*base, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1245 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1246 | case Token::MINUSMINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1247 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1248 | fErrors.error(expression.fPosition, |
| 1249 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1250 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1251 | return nullptr; |
| 1252 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1253 | this->markWrittenTo(*base, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1254 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1255 | case Token::LOGICALNOT: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1256 | if (base->fType != *fContext.fBool_Type) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1257 | fErrors.error(expression.fPosition, |
| 1258 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1259 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1260 | return nullptr; |
| 1261 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1262 | if (base->fKind == Expression::kBoolLiteral_Kind) { |
| 1263 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fPosition, |
| 1264 | !((BoolLiteral&) *base).fValue)); |
| 1265 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1266 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1267 | case Token::BITWISENOT: |
| 1268 | if (base->fType != *fContext.fInt_Type) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1269 | fErrors.error(expression.fPosition, |
| 1270 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1271 | "' cannot operate on '" + base->fType.description() + "'"); |
| 1272 | return nullptr; |
| 1273 | } |
| 1274 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1275 | default: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1276 | ABORT("unsupported prefix operator\n"); |
| 1277 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1278 | return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1279 | std::move(base))); |
| 1280 | } |
| 1281 | |
| 1282 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
| 1283 | const ASTExpression& index) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1284 | if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind && |
| 1285 | base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1286 | fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1287 | "'"); |
| 1288 | return nullptr; |
| 1289 | } |
| 1290 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 1291 | if (!converted) { |
| 1292 | return nullptr; |
| 1293 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1294 | if (converted->fType != *fContext.fUInt_Type) { |
| 1295 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 1296 | if (!converted) { |
| 1297 | return nullptr; |
| 1298 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1299 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1300 | return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1301 | std::move(converted))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1305 | const SkString& field) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1306 | auto fields = base->fType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1307 | for (size_t i = 0; i < fields.size(); i++) { |
| 1308 | if (fields[i].fName == field) { |
| 1309 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 1310 | } |
| 1311 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1312 | fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a " |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1313 | "field named '" + field + ""); |
| 1314 | return nullptr; |
| 1315 | } |
| 1316 | |
| 1317 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1318 | const SkString& fields) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1319 | if (base->fType.kind() != Type::kVector_Kind) { |
| 1320 | fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1321 | return nullptr; |
| 1322 | } |
| 1323 | std::vector<int> swizzleComponents; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1324 | for (size_t i = 0; i < fields.size(); i++) { |
| 1325 | switch (fields[i]) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1326 | case 'x': // fall through |
| 1327 | case 'r': // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1328 | case 's': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1329 | swizzleComponents.push_back(0); |
| 1330 | break; |
| 1331 | case 'y': // fall through |
| 1332 | case 'g': // fall through |
| 1333 | case 't': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1334 | if (base->fType.columns() >= 2) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1335 | swizzleComponents.push_back(1); |
| 1336 | break; |
| 1337 | } |
| 1338 | // fall through |
| 1339 | case 'z': // fall through |
| 1340 | case 'b': // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1341 | case 'p': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1342 | if (base->fType.columns() >= 3) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1343 | swizzleComponents.push_back(2); |
| 1344 | break; |
| 1345 | } |
| 1346 | // fall through |
| 1347 | case 'w': // fall through |
| 1348 | case 'a': // fall through |
| 1349 | case 'q': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1350 | if (base->fType.columns() >= 4) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1351 | swizzleComponents.push_back(3); |
| 1352 | break; |
| 1353 | } |
| 1354 | // fall through |
| 1355 | default: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1356 | fErrors.error(base->fPosition, SkStringPrintf("invalid swizzle component '%c'", |
| 1357 | fields[i])); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1358 | return nullptr; |
| 1359 | } |
| 1360 | } |
| 1361 | ASSERT(swizzleComponents.size() > 0); |
| 1362 | if (swizzleComponents.size() > 4) { |
| 1363 | fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'"); |
| 1364 | return nullptr; |
| 1365 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1366 | return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1369 | std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1370 | auto found = fCapsMap.find(name); |
| 1371 | if (found == fCapsMap.end()) { |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1372 | fErrors.error(position, "unknown capability flag '" + name + "'"); |
| 1373 | return nullptr; |
| 1374 | } |
| 1375 | switch (found->second.fKind) { |
| 1376 | case CapValue::kBool_Kind: |
| 1377 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, position, |
| 1378 | (bool) found->second.fValue)); |
| 1379 | case CapValue::kInt_Kind: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1380 | return std::unique_ptr<Expression>(new IntLiteral(fContext, position, |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1381 | found->second.fValue)); |
| 1382 | } |
| 1383 | ASSERT(false); |
| 1384 | return nullptr; |
| 1385 | } |
| 1386 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1387 | std::unique_ptr<Expression> IRGenerator::convertSuffixExpression( |
| 1388 | const ASTSuffixExpression& expression) { |
| 1389 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase); |
| 1390 | if (!base) { |
| 1391 | return nullptr; |
| 1392 | } |
| 1393 | switch (expression.fSuffix->fKind) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1394 | case ASTSuffix::kIndex_Kind: { |
| 1395 | const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get(); |
| 1396 | if (expr) { |
| 1397 | return this->convertIndex(std::move(base), *expr); |
| 1398 | } else if (base->fKind == Expression::kTypeReference_Kind) { |
| 1399 | const Type& oldType = ((TypeReference&) *base).fValue; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1400 | Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1401 | -1); |
| 1402 | fSymbolTable->takeOwnership(newType); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1403 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1404 | *newType)); |
| 1405 | } else { |
| 1406 | fErrors.error(expression.fPosition, "'[]' must follow a type name"); |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1407 | return nullptr; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1410 | case ASTSuffix::kCall_Kind: { |
| 1411 | auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments; |
| 1412 | std::vector<std::unique_ptr<Expression>> arguments; |
| 1413 | for (size_t i = 0; i < rawArguments->size(); i++) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1414 | std::unique_ptr<Expression> converted = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1415 | this->convertExpression(*(*rawArguments)[i]); |
| 1416 | if (!converted) { |
| 1417 | return nullptr; |
| 1418 | } |
| 1419 | arguments.push_back(std::move(converted)); |
| 1420 | } |
| 1421 | return this->call(expression.fPosition, std::move(base), std::move(arguments)); |
| 1422 | } |
| 1423 | case ASTSuffix::kField_Kind: { |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1424 | if (base->fType == *fContext.fSkCaps_Type) { |
| 1425 | return this->getCap(expression.fPosition, |
| 1426 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1427 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1428 | switch (base->fType.kind()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1429 | case Type::kVector_Kind: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1430 | return this->convertSwizzle(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1431 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1432 | case Type::kStruct_Kind: |
| 1433 | return this->convertField(std::move(base), |
| 1434 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1435 | default: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1436 | fErrors.error(base->fPosition, "cannot swizzle value of type '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1437 | base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1438 | return nullptr; |
| 1439 | } |
| 1440 | } |
| 1441 | case ASTSuffix::kPostIncrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1442 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1443 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1444 | "'++' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1445 | return nullptr; |
| 1446 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1447 | this->markWrittenTo(*base, true); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1448 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1449 | Token::PLUSPLUS)); |
| 1450 | case ASTSuffix::kPostDecrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1451 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1452 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1453 | "'--' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1454 | return nullptr; |
| 1455 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1456 | this->markWrittenTo(*base, true); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1457 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1458 | Token::MINUSMINUS)); |
| 1459 | default: |
| 1460 | ABORT("unsupported suffix operator"); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | void IRGenerator::checkValid(const Expression& expr) { |
| 1465 | switch (expr.fKind) { |
| 1466 | case Expression::kFunctionReference_Kind: |
| 1467 | fErrors.error(expr.fPosition, "expected '(' to begin function call"); |
| 1468 | break; |
| 1469 | case Expression::kTypeReference_Kind: |
| 1470 | fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation"); |
| 1471 | break; |
| 1472 | default: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1473 | if (expr.fType == *fContext.fInvalid_Type) { |
| 1474 | fErrors.error(expr.fPosition, "invalid expression"); |
| 1475 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1479 | static bool has_duplicates(const Swizzle& swizzle) { |
| 1480 | int bits = 0; |
| 1481 | for (int idx : swizzle.fComponents) { |
| 1482 | ASSERT(idx >= 0 && idx <= 3); |
| 1483 | int bit = 1 << idx; |
| 1484 | if (bits & bit) { |
| 1485 | return true; |
| 1486 | } |
| 1487 | bits |= bit; |
| 1488 | } |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1492 | void IRGenerator::markWrittenTo(const Expression& expr, bool readWrite) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1493 | switch (expr.fKind) { |
| 1494 | case Expression::kVariableReference_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1495 | const Variable& var = ((VariableReference&) expr).fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1496 | if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1497 | fErrors.error(expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1498 | "cannot modify immutable variable '" + var.fName + "'"); |
| 1499 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1500 | ((VariableReference&) expr).setRefKind(readWrite ? VariableReference::kReadWrite_RefKind |
| 1501 | : VariableReference::kWrite_RefKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1502 | break; |
| 1503 | } |
| 1504 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1505 | this->markWrittenTo(*((FieldAccess&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1506 | break; |
| 1507 | case Expression::kSwizzle_Kind: |
| 1508 | if (has_duplicates((Swizzle&) expr)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1509 | fErrors.error(expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1510 | "cannot write to the same swizzle field more than once"); |
| 1511 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1512 | this->markWrittenTo(*((Swizzle&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1513 | break; |
| 1514 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1515 | this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1516 | break; |
| 1517 | default: |
| 1518 | fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'"); |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | } |