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