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