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