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