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