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 | */ |
| 7 | |
| 8 | #include "SkSLIRGenerator.h" |
| 9 | |
| 10 | #include "limits.h" |
| 11 | |
| 12 | #include "ast/SkSLASTBoolLiteral.h" |
| 13 | #include "ast/SkSLASTFieldSuffix.h" |
| 14 | #include "ast/SkSLASTFloatLiteral.h" |
| 15 | #include "ast/SkSLASTIndexSuffix.h" |
| 16 | #include "ast/SkSLASTIntLiteral.h" |
| 17 | #include "ir/SkSLBinaryExpression.h" |
| 18 | #include "ir/SkSLBoolLiteral.h" |
| 19 | #include "ir/SkSLBreakStatement.h" |
| 20 | #include "ir/SkSLConstructor.h" |
| 21 | #include "ir/SkSLContinueStatement.h" |
| 22 | #include "ir/SkSLDiscardStatement.h" |
| 23 | #include "ir/SkSLDoStatement.h" |
| 24 | #include "ir/SkSLExpressionStatement.h" |
| 25 | #include "ir/SkSLField.h" |
| 26 | #include "ir/SkSLFieldAccess.h" |
| 27 | #include "ir/SkSLFloatLiteral.h" |
| 28 | #include "ir/SkSLForStatement.h" |
| 29 | #include "ir/SkSLFunctionCall.h" |
| 30 | #include "ir/SkSLFunctionDeclaration.h" |
| 31 | #include "ir/SkSLFunctionDefinition.h" |
| 32 | #include "ir/SkSLFunctionReference.h" |
| 33 | #include "ir/SkSLIfStatement.h" |
| 34 | #include "ir/SkSLIndexExpression.h" |
| 35 | #include "ir/SkSLInterfaceBlock.h" |
| 36 | #include "ir/SkSLIntLiteral.h" |
| 37 | #include "ir/SkSLLayout.h" |
| 38 | #include "ir/SkSLPostfixExpression.h" |
| 39 | #include "ir/SkSLPrefixExpression.h" |
| 40 | #include "ir/SkSLReturnStatement.h" |
| 41 | #include "ir/SkSLSwizzle.h" |
| 42 | #include "ir/SkSLTernaryExpression.h" |
| 43 | #include "ir/SkSLUnresolvedFunction.h" |
| 44 | #include "ir/SkSLVariable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 45 | #include "ir/SkSLVarDeclarations.h" |
| 46 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | #include "ir/SkSLVariableReference.h" |
| 48 | #include "ir/SkSLWhileStatement.h" |
| 49 | |
| 50 | namespace SkSL { |
| 51 | |
| 52 | class AutoSymbolTable { |
| 53 | public: |
| 54 | AutoSymbolTable(IRGenerator* ir) |
| 55 | : fIR(ir) |
| 56 | , fPrevious(fIR->fSymbolTable) { |
| 57 | fIR->pushSymbolTable(); |
| 58 | } |
| 59 | |
| 60 | ~AutoSymbolTable() { |
| 61 | fIR->popSymbolTable(); |
| 62 | ASSERT(fPrevious == fIR->fSymbolTable); |
| 63 | } |
| 64 | |
| 65 | IRGenerator* fIR; |
| 66 | std::shared_ptr<SymbolTable> fPrevious; |
| 67 | }; |
| 68 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 69 | class AutoLoopLevel { |
| 70 | public: |
| 71 | AutoLoopLevel(IRGenerator* ir) |
| 72 | : fIR(ir) { |
| 73 | fIR->fLoopLevel++; |
| 74 | } |
| 75 | |
| 76 | ~AutoLoopLevel() { |
| 77 | fIR->fLoopLevel--; |
| 78 | } |
| 79 | |
| 80 | IRGenerator* fIR; |
| 81 | }; |
| 82 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 83 | IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 84 | ErrorReporter& errorReporter) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 85 | : fContext(*context) |
| 86 | , fCurrentFunction(nullptr) |
| 87 | , fSymbolTable(std::move(symbolTable)) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 88 | , fLoopLevel(0) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 89 | , fErrors(errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 90 | |
| 91 | void IRGenerator::pushSymbolTable() { |
| 92 | fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors)); |
| 93 | } |
| 94 | |
| 95 | void IRGenerator::popSymbolTable() { |
| 96 | fSymbolTable = fSymbolTable->fParent; |
| 97 | } |
| 98 | |
| 99 | std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) { |
| 100 | return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName)); |
| 101 | } |
| 102 | |
| 103 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) { |
| 104 | switch (statement.fKind) { |
| 105 | case ASTStatement::kBlock_Kind: |
| 106 | return this->convertBlock((ASTBlock&) statement); |
| 107 | case ASTStatement::kVarDeclaration_Kind: |
| 108 | return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement); |
| 109 | case ASTStatement::kExpression_Kind: |
| 110 | return this->convertExpressionStatement((ASTExpressionStatement&) statement); |
| 111 | case ASTStatement::kIf_Kind: |
| 112 | return this->convertIf((ASTIfStatement&) statement); |
| 113 | case ASTStatement::kFor_Kind: |
| 114 | return this->convertFor((ASTForStatement&) statement); |
| 115 | case ASTStatement::kWhile_Kind: |
| 116 | return this->convertWhile((ASTWhileStatement&) statement); |
| 117 | case ASTStatement::kDo_Kind: |
| 118 | return this->convertDo((ASTDoStatement&) statement); |
| 119 | case ASTStatement::kReturn_Kind: |
| 120 | return this->convertReturn((ASTReturnStatement&) statement); |
| 121 | case ASTStatement::kBreak_Kind: |
| 122 | return this->convertBreak((ASTBreakStatement&) statement); |
| 123 | case ASTStatement::kContinue_Kind: |
| 124 | return this->convertContinue((ASTContinueStatement&) statement); |
| 125 | case ASTStatement::kDiscard_Kind: |
| 126 | return this->convertDiscard((ASTDiscardStatement&) statement); |
| 127 | default: |
| 128 | ABORT("unsupported statement type: %d\n", statement.fKind); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) { |
| 133 | AutoSymbolTable table(this); |
| 134 | std::vector<std::unique_ptr<Statement>> statements; |
| 135 | for (size_t i = 0; i < block.fStatements.size(); i++) { |
| 136 | std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]); |
| 137 | if (!statement) { |
| 138 | return nullptr; |
| 139 | } |
| 140 | statements.push_back(std::move(statement)); |
| 141 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 142 | return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( |
| 146 | const ASTVarDeclarationStatement& s) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 147 | auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 148 | if (!decl) { |
| 149 | return nullptr; |
| 150 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 151 | return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { |
| 155 | return Modifiers(modifiers); |
| 156 | } |
| 157 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 158 | std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl, |
| 159 | Variable::Storage storage) { |
| 160 | std::vector<VarDeclaration> variables; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 161 | const Type* baseType = this->convertType(*decl.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | if (!baseType) { |
| 163 | return nullptr; |
| 164 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 165 | for (const auto& varDecl : decl.fVars) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | Modifiers modifiers = this->convertModifiers(decl.fModifiers); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 167 | const Type* type = baseType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | ASSERT(type->kind() != Type::kArray_Kind); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 169 | std::vector<std::unique_ptr<Expression>> sizes; |
| 170 | for (const auto& rawSize : varDecl.fSizes) { |
| 171 | if (rawSize) { |
| 172 | auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 173 | if (!size) { |
| 174 | return nullptr; |
| 175 | } |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 176 | std::string name = type->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | uint64_t count; |
| 178 | if (size->fKind == Expression::kIntLiteral_Kind) { |
| 179 | count = ((IntLiteral&) *size).fValue; |
| 180 | if (count <= 0) { |
| 181 | fErrors.error(size->fPosition, "array size must be positive"); |
| 182 | } |
| 183 | name += "[" + to_string(count) + "]"; |
| 184 | } else { |
| 185 | count = -1; |
| 186 | name += "[]"; |
| 187 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 188 | type = new Type(name, Type::kArray_Kind, *type, (int) count); |
| 189 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 190 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 191 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 192 | type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1); |
| 193 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 194 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 197 | auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, modifiers, varDecl.fName, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 198 | *type, storage)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 199 | std::unique_ptr<Expression> value; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 200 | if (varDecl.fValue) { |
| 201 | value = this->convertExpression(*varDecl.fValue); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 202 | if (!value) { |
| 203 | return nullptr; |
| 204 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 205 | value = this->coerce(std::move(value), *type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 206 | } |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 207 | if (storage == Variable::kGlobal_Storage && "sk_FragColor" == varDecl.fName && |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 208 | (*fSymbolTable)[varDecl.fName]) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 209 | // already defined, ignore |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 210 | } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] && |
| 211 | (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 212 | ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 213 | // already defined, just update the modifiers |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 214 | Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName]; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 215 | old->fModifiers = var->fModifiers; |
| 216 | } else { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 217 | variables.emplace_back(var.get(), std::move(sizes), std::move(value)); |
| 218 | fSymbolTable->add(varDecl.fName, std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 219 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 220 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 221 | return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition, |
| 222 | baseType, |
| 223 | std::move(variables))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 224 | } |
| 225 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 226 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration( |
| 227 | const ASTModifiersDeclaration& m) { |
| 228 | Modifiers modifiers = this->convertModifiers(m.fModifiers); |
| 229 | return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers)); |
| 230 | } |
| 231 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 232 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 233 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest), |
| 234 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 235 | if (!test) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); |
| 239 | if (!ifTrue) { |
| 240 | return nullptr; |
| 241 | } |
| 242 | std::unique_ptr<Statement> ifFalse; |
| 243 | if (s.fIfFalse) { |
| 244 | ifFalse = this->convertStatement(*s.fIfFalse); |
| 245 | if (!ifFalse) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 249 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 250 | // static boolean value, fold down to a single branch |
| 251 | if (((BoolLiteral&) *test).fValue) { |
| 252 | return ifTrue; |
| 253 | } else if (s.fIfFalse) { |
| 254 | return ifFalse; |
| 255 | } else { |
| 256 | // False & no else clause. Not an error, so don't return null! |
| 257 | std::vector<std::unique_ptr<Statement>> empty; |
| 258 | return std::unique_ptr<Statement>(new Block(s.fPosition, std::move(empty), |
| 259 | fSymbolTable)); |
| 260 | } |
| 261 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 262 | return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test), |
| 263 | std::move(ifTrue), std::move(ifFalse))); |
| 264 | } |
| 265 | |
| 266 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 267 | AutoLoopLevel level(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 268 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 269 | std::unique_ptr<Statement> initializer; |
| 270 | if (f.fInitializer) { |
| 271 | initializer = this->convertStatement(*f.fInitializer); |
| 272 | if (!initializer) { |
| 273 | return nullptr; |
| 274 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 275 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 276 | std::unique_ptr<Expression> test; |
| 277 | if (f.fTest) { |
| 278 | test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type); |
| 279 | if (!test) { |
| 280 | return nullptr; |
| 281 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 282 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 283 | std::unique_ptr<Expression> next; |
| 284 | if (f.fNext) { |
| 285 | next = this->convertExpression(*f.fNext); |
| 286 | if (!next) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | this->checkValid(*next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 290 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 291 | std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement); |
| 292 | if (!statement) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer), |
| 296 | std::move(test), std::move(next), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 297 | std::move(statement), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 301 | AutoLoopLevel level(this); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 302 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest), |
| 303 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 304 | if (!test) { |
| 305 | return nullptr; |
| 306 | } |
| 307 | std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement); |
| 308 | if (!statement) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test), |
| 312 | std::move(statement))); |
| 313 | } |
| 314 | |
| 315 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 316 | AutoLoopLevel level(this); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 317 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest), |
| 318 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 319 | if (!test) { |
| 320 | return nullptr; |
| 321 | } |
| 322 | std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement); |
| 323 | if (!statement) { |
| 324 | return nullptr; |
| 325 | } |
| 326 | return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement), |
| 327 | std::move(test))); |
| 328 | } |
| 329 | |
| 330 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement( |
| 331 | const ASTExpressionStatement& s) { |
| 332 | std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression); |
| 333 | if (!e) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | this->checkValid(*e); |
| 337 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 338 | } |
| 339 | |
| 340 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) { |
| 341 | ASSERT(fCurrentFunction); |
| 342 | if (r.fExpression) { |
| 343 | std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression); |
| 344 | if (!result) { |
| 345 | return nullptr; |
| 346 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 347 | if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 348 | fErrors.error(result->fPosition, "may not return a value from a void function"); |
| 349 | } else { |
| 350 | result = this->coerce(std::move(result), fCurrentFunction->fReturnType); |
| 351 | if (!result) { |
| 352 | return nullptr; |
| 353 | } |
| 354 | } |
| 355 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 356 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 357 | if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 358 | fErrors.error(r.fPosition, "expected function to return '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 359 | fCurrentFunction->fReturnType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 360 | } |
| 361 | return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 366 | if (fLoopLevel > 0) { |
| 367 | return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); |
| 368 | } else { |
| 369 | fErrors.error(b.fPosition, "break statement must be inside a loop"); |
| 370 | return nullptr; |
| 371 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 375 | if (fLoopLevel > 0) { |
| 376 | return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); |
| 377 | } else { |
| 378 | fErrors.error(c.fPosition, "continue statement must be inside a loop"); |
| 379 | return nullptr; |
| 380 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) { |
| 384 | return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); |
| 385 | } |
| 386 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 387 | std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 388 | const Type* returnType = this->convertType(*f.fReturnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | if (!returnType) { |
| 390 | return nullptr; |
| 391 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 392 | std::vector<const Variable*> parameters; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 393 | for (const auto& param : f.fParameters) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 394 | const Type* type = this->convertType(*param->fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 395 | if (!type) { |
| 396 | return nullptr; |
| 397 | } |
| 398 | for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { |
| 399 | int size = param->fSizes[j]; |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 400 | std::string name = type->name() + "[" + to_string(size) + "]"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 401 | Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size); |
| 402 | fSymbolTable->takeOwnership(newType); |
| 403 | type = newType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 404 | } |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 405 | std::string name = param->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 406 | Modifiers modifiers = this->convertModifiers(param->fModifiers); |
| 407 | Position pos = param->fPosition; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 408 | Variable* var = new Variable(pos, modifiers, std::move(name), *type, |
| 409 | Variable::kParameter_Storage); |
| 410 | fSymbolTable->takeOwnership(var); |
| 411 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 415 | const FunctionDeclaration* decl = nullptr; |
| 416 | auto entry = (*fSymbolTable)[f.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 417 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 418 | std::vector<const FunctionDeclaration*> functions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 419 | switch (entry->fKind) { |
| 420 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 421 | functions = ((UnresolvedFunction*) entry)->fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 422 | break; |
| 423 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 424 | functions.push_back((FunctionDeclaration*) entry); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 425 | break; |
| 426 | default: |
| 427 | fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); |
| 428 | return nullptr; |
| 429 | } |
| 430 | for (const auto& other : functions) { |
| 431 | ASSERT(other->fName == f.fName); |
| 432 | if (parameters.size() == other->fParameters.size()) { |
| 433 | bool match = true; |
| 434 | for (size_t i = 0; i < parameters.size(); i++) { |
| 435 | if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 436 | match = false; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | if (match) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 441 | if (*returnType != other->fReturnType) { |
| 442 | FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 443 | fErrors.error(f.fPosition, "functions '" + newDecl.description() + |
| 444 | "' and '" + other->description() + |
| 445 | "' differ only in return type"); |
| 446 | return nullptr; |
| 447 | } |
| 448 | decl = other; |
| 449 | for (size_t i = 0; i < parameters.size(); i++) { |
| 450 | if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) { |
| 451 | fErrors.error(f.fPosition, "modifiers on parameter " + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 452 | to_string((uint64_t) i + 1) + |
| 453 | " differ between declaration and " |
| 454 | "definition"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 455 | return nullptr; |
| 456 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 457 | } |
| 458 | if (other->fDefined) { |
| 459 | fErrors.error(f.fPosition, "duplicate definition of " + |
| 460 | other->description()); |
| 461 | } |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | if (!decl) { |
| 468 | // couldn't find an existing declaration |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 469 | auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fPosition, |
| 470 | f.fName, |
| 471 | parameters, |
| 472 | *returnType)); |
| 473 | decl = newDecl.get(); |
| 474 | fSymbolTable->add(decl->fName, std::move(newDecl)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 475 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 476 | if (f.fBody) { |
| 477 | ASSERT(!fCurrentFunction); |
| 478 | fCurrentFunction = decl; |
| 479 | decl->fDefined = true; |
| 480 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 481 | AutoSymbolTable table(this); |
| 482 | for (size_t i = 0; i < parameters.size(); i++) { |
| 483 | fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 484 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 485 | std::unique_ptr<Block> body = this->convertBlock(*f.fBody); |
| 486 | fCurrentFunction = nullptr; |
| 487 | if (!body) { |
| 488 | return nullptr; |
| 489 | } |
| 490 | return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl, |
| 491 | std::move(body))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 492 | } |
| 493 | return nullptr; |
| 494 | } |
| 495 | |
| 496 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) { |
| 497 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 498 | AutoSymbolTable table(this); |
| 499 | Modifiers mods = this->convertModifiers(intf.fModifiers); |
| 500 | std::vector<Type::Field> fields; |
| 501 | for (size_t i = 0; i < intf.fDeclarations.size(); i++) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 502 | std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations( |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 503 | *intf.fDeclarations[i], |
| 504 | Variable::kGlobal_Storage); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 505 | if (!decl) { |
| 506 | return nullptr; |
| 507 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 508 | for (const auto& var : decl->fVars) { |
| 509 | fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName, |
| 510 | &var.fVar->fType)); |
| 511 | if (var.fValue) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 512 | fErrors.error(decl->fPosition, |
| 513 | "initializers are not permitted on interface block fields"); |
| 514 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 515 | if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 516 | Modifiers::kOut_Flag | |
| 517 | Modifiers::kUniform_Flag | |
| 518 | Modifiers::kConst_Flag)) { |
| 519 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 520 | "interface block fields may not have storage qualifiers"); |
| 521 | } |
| 522 | } |
| 523 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 524 | Type* type = new Type(intf.fInterfaceName, fields); |
| 525 | fSymbolTable->takeOwnership(type); |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 526 | std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fInterfaceName; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 527 | Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kGlobal_Storage); |
| 528 | fSymbolTable->takeOwnership(var); |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 529 | if (intf.fValueName.length()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 530 | old->addWithoutOwnership(intf.fValueName, var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 531 | } else { |
| 532 | for (size_t i = 0; i < fields.size(); i++) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 533 | old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var, |
| 534 | (int) i))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 537 | return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 538 | } |
| 539 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 540 | const Type* IRGenerator::convertType(const ASTType& type) { |
| 541 | const Symbol* result = (*fSymbolTable)[type.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 542 | if (result && result->fKind == Symbol::kType_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 543 | return (const Type*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 544 | } |
| 545 | fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); |
| 546 | return nullptr; |
| 547 | } |
| 548 | |
| 549 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { |
| 550 | switch (expr.fKind) { |
| 551 | case ASTExpression::kIdentifier_Kind: |
| 552 | return this->convertIdentifier((ASTIdentifier&) expr); |
| 553 | case ASTExpression::kBool_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 554 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 555 | ((ASTBoolLiteral&) expr).fValue)); |
| 556 | case ASTExpression::kInt_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 557 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 558 | ((ASTIntLiteral&) expr).fValue)); |
| 559 | case ASTExpression::kFloat_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 560 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 561 | ((ASTFloatLiteral&) expr).fValue)); |
| 562 | case ASTExpression::kBinary_Kind: |
| 563 | return this->convertBinaryExpression((ASTBinaryExpression&) expr); |
| 564 | case ASTExpression::kPrefix_Kind: |
| 565 | return this->convertPrefixExpression((ASTPrefixExpression&) expr); |
| 566 | case ASTExpression::kSuffix_Kind: |
| 567 | return this->convertSuffixExpression((ASTSuffixExpression&) expr); |
| 568 | case ASTExpression::kTernary_Kind: |
| 569 | return this->convertTernaryExpression((ASTTernaryExpression&) expr); |
| 570 | default: |
| 571 | ABORT("unsupported expression type: %d\n", expr.fKind); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 576 | const Symbol* result = (*fSymbolTable)[identifier.fText]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 577 | if (!result) { |
| 578 | fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'"); |
| 579 | return nullptr; |
| 580 | } |
| 581 | switch (result->fKind) { |
| 582 | case Symbol::kFunctionDeclaration_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 583 | std::vector<const FunctionDeclaration*> f = { |
| 584 | (const FunctionDeclaration*) result |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | }; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 586 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 587 | identifier.fPosition, |
| 588 | f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 589 | } |
| 590 | case Symbol::kUnresolvedFunction_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 591 | const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 592 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 593 | identifier.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 594 | f->fFunctions)); |
| 595 | } |
| 596 | case Symbol::kVariable_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 597 | const Variable* var = (const Variable*) result; |
| 598 | this->markReadFrom(*var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 599 | return std::unique_ptr<VariableReference>(new VariableReference(identifier.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 600 | *var)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 601 | } |
| 602 | case Symbol::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 603 | const Field* field = (const Field*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 604 | VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 605 | return std::unique_ptr<Expression>(new FieldAccess( |
| 606 | std::unique_ptr<Expression>(base), |
| 607 | field->fFieldIndex, |
| 608 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 609 | } |
| 610 | case Symbol::kType_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 611 | const Type* t = (const Type*) result; |
| 612 | return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition, |
| 613 | *t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 614 | } |
| 615 | default: |
| 616 | ABORT("unsupported symbol type %d\n", result->fKind); |
| 617 | } |
| 618 | |
| 619 | } |
| 620 | |
| 621 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 622 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 623 | if (!expr) { |
| 624 | return nullptr; |
| 625 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 626 | if (expr->fType == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 627 | return expr; |
| 628 | } |
| 629 | this->checkValid(*expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 630 | if (expr->fType == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 631 | return nullptr; |
| 632 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 633 | if (!expr->fType.canCoerceTo(type)) { |
| 634 | fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" + |
| 635 | expr->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 636 | return nullptr; |
| 637 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 638 | if (type.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 639 | std::vector<std::unique_ptr<Expression>> args; |
| 640 | args.push_back(std::move(expr)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 641 | ASTIdentifier id(Position(), type.description()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 642 | std::unique_ptr<Expression> ctor = this->convertIdentifier(id); |
| 643 | ASSERT(ctor); |
| 644 | return this->call(Position(), std::move(ctor), std::move(args)); |
| 645 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 646 | std::vector<std::unique_ptr<Expression>> args; |
| 647 | args.push_back(std::move(expr)); |
| 648 | return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 649 | } |
| 650 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 651 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
| 652 | if (left.kind() == Type::kMatrix_Kind) { |
| 653 | return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind; |
| 654 | } |
| 655 | return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind; |
| 656 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 657 | |
| 658 | static bool is_assignment(Token::Kind op) { |
| 659 | switch (op) { |
| 660 | case Token::EQ: // fall through |
| 661 | case Token::PLUSEQ: // fall through |
| 662 | case Token::MINUSEQ: // fall through |
| 663 | case Token::STAREQ: // fall through |
| 664 | case Token::SLASHEQ: // fall through |
| 665 | case Token::PERCENTEQ: // fall through |
| 666 | case Token::SHLEQ: // fall through |
| 667 | case Token::SHREQ: // fall through |
| 668 | case Token::BITWISEOREQ: // fall through |
| 669 | case Token::BITWISEXOREQ: // fall through |
| 670 | case Token::BITWISEANDEQ: // fall through |
| 671 | case Token::LOGICALOREQ: // fall through |
| 672 | case Token::LOGICALXOREQ: // fall through |
| 673 | case Token::LOGICALANDEQ: |
| 674 | return true; |
| 675 | default: |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 680 | /** |
| 681 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 682 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 683 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 684 | static bool determine_binary_type(const Context& context, |
| 685 | Token::Kind op, |
| 686 | const Type& left, |
| 687 | const Type& right, |
| 688 | const Type** outLeftType, |
| 689 | const Type** outRightType, |
| 690 | const Type** outResultType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 691 | bool tryFlipped) { |
| 692 | bool isLogical; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 693 | bool validMatrixOrVectorOp; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 694 | switch (op) { |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 695 | case Token::EQ: |
| 696 | *outLeftType = &left; |
| 697 | *outRightType = &left; |
| 698 | *outResultType = &left; |
| 699 | return right.canCoerceTo(left); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 700 | case Token::EQEQ: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 701 | case Token::NEQ: |
| 702 | isLogical = true; |
| 703 | validMatrixOrVectorOp = true; |
| 704 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 705 | case Token::LT: // fall through |
| 706 | case Token::GT: // fall through |
| 707 | case Token::LTEQ: // fall through |
| 708 | case Token::GTEQ: |
| 709 | isLogical = true; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 710 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 711 | break; |
| 712 | case Token::LOGICALOR: // fall through |
| 713 | case Token::LOGICALAND: // fall through |
| 714 | case Token::LOGICALXOR: // fall through |
| 715 | case Token::LOGICALOREQ: // fall through |
| 716 | case Token::LOGICALANDEQ: // fall through |
| 717 | case Token::LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 718 | *outLeftType = context.fBool_Type.get(); |
| 719 | *outRightType = context.fBool_Type.get(); |
| 720 | *outResultType = context.fBool_Type.get(); |
| 721 | return left.canCoerceTo(*context.fBool_Type) && |
| 722 | right.canCoerceTo(*context.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 723 | case Token::STAR: // fall through |
| 724 | case Token::STAREQ: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 725 | if (is_matrix_multiply(left, right)) { |
| 726 | // determine final component type |
| 727 | if (determine_binary_type(context, Token::STAR, left.componentType(), |
| 728 | right.componentType(), outLeftType, outRightType, |
| 729 | outResultType, false)) { |
| 730 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
| 731 | left.rows());; |
| 732 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
| 733 | right.rows());; |
| 734 | int leftColumns = left.columns(); |
| 735 | int leftRows = left.rows(); |
| 736 | int rightColumns; |
| 737 | int rightRows; |
| 738 | if (right.kind() == Type::kVector_Kind) { |
| 739 | // matrix * vector treats the vector as a column vector, so we need to |
| 740 | // transpose it |
| 741 | rightColumns = right.rows(); |
| 742 | rightRows = right.columns(); |
| 743 | ASSERT(rightColumns == 1); |
| 744 | } else { |
| 745 | rightColumns = right.columns(); |
| 746 | rightRows = right.rows(); |
| 747 | } |
| 748 | if (rightColumns > 1) { |
| 749 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 750 | leftRows); |
| 751 | } else { |
| 752 | // result was a column vector, transpose it back to a row |
| 753 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 754 | rightColumns); |
| 755 | } |
| 756 | return leftColumns == rightRows; |
| 757 | } else { |
| 758 | return false; |
| 759 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 760 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 761 | isLogical = false; |
| 762 | validMatrixOrVectorOp = true; |
| 763 | break; |
| 764 | case Token::PLUS: // fall through |
| 765 | case Token::PLUSEQ: // fall through |
| 766 | case Token::MINUS: // fall through |
| 767 | case Token::MINUSEQ: // fall through |
| 768 | case Token::SLASH: // fall through |
| 769 | case Token::SLASHEQ: |
| 770 | isLogical = false; |
| 771 | validMatrixOrVectorOp = true; |
| 772 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 773 | default: |
| 774 | isLogical = false; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 775 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 776 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 777 | bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind; |
| 778 | // FIXME: incorrect for shift |
| 779 | if (right.canCoerceTo(left) && (left.kind() == Type::kScalar_Kind || |
| 780 | (isVectorOrMatrix && validMatrixOrVectorOp))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 781 | *outLeftType = &left; |
| 782 | *outRightType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 783 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 784 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 785 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 786 | *outResultType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 787 | } |
| 788 | return true; |
| 789 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 790 | if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
| 791 | (right.kind() == Type::kScalar_Kind)) { |
| 792 | if (determine_binary_type(context, op, left.componentType(), right, outLeftType, |
| 793 | outRightType, outResultType, false)) { |
| 794 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 795 | if (!isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 796 | *outResultType = &(*outResultType)->toCompound(context, left.columns(), |
| 797 | left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 798 | } |
| 799 | return true; |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | if (tryFlipped) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 804 | return determine_binary_type(context, op, right, left, outRightType, outLeftType, |
| 805 | outResultType, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 806 | } |
| 807 | return false; |
| 808 | } |
| 809 | |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 810 | /** |
| 811 | * If both operands are compile-time constants and can be folded, returns an expression representing |
| 812 | * the folded value. Otherwise, returns null. Note that unlike most other functions here, null does |
| 813 | * not represent a compilation error. |
| 814 | */ |
| 815 | std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left, |
| 816 | Token::Kind op, |
| 817 | const Expression& right) { |
| 818 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 819 | // precision to calculate the results and hope the result makes sense. The plan is to move the |
| 820 | // Skia caps into SkSL, so we have access to all of them including the precisions of the various |
| 821 | // types, which will let us be more intelligent about this. |
| 822 | if (left.fKind == Expression::kBoolLiteral_Kind && |
| 823 | right.fKind == Expression::kBoolLiteral_Kind) { |
| 824 | bool leftVal = ((BoolLiteral&) left).fValue; |
| 825 | bool rightVal = ((BoolLiteral&) right).fValue; |
| 826 | bool result; |
| 827 | switch (op) { |
| 828 | case Token::LOGICALAND: result = leftVal && rightVal; break; |
| 829 | case Token::LOGICALOR: result = leftVal || rightVal; break; |
| 830 | case Token::LOGICALXOR: result = leftVal ^ rightVal; break; |
| 831 | default: return nullptr; |
| 832 | } |
| 833 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fPosition, result)); |
| 834 | } |
| 835 | #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fPosition, \ |
| 836 | leftVal op rightVal)) |
| 837 | if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) { |
| 838 | int64_t leftVal = ((IntLiteral&) left).fValue; |
| 839 | int64_t rightVal = ((IntLiteral&) right).fValue; |
| 840 | switch (op) { |
| 841 | case Token::PLUS: return RESULT(Int, +); |
| 842 | case Token::MINUS: return RESULT(Int, -); |
| 843 | case Token::STAR: return RESULT(Int, *); |
| 844 | case Token::SLASH: return RESULT(Int, /); |
| 845 | case Token::PERCENT: return RESULT(Int, %); |
| 846 | case Token::BITWISEAND: return RESULT(Int, &); |
| 847 | case Token::BITWISEOR: return RESULT(Int, |); |
| 848 | case Token::BITWISEXOR: return RESULT(Int, ^); |
| 849 | case Token::SHL: return RESULT(Int, <<); |
| 850 | case Token::SHR: return RESULT(Int, >>); |
| 851 | case Token::EQEQ: return RESULT(Bool, ==); |
| 852 | case Token::NEQ: return RESULT(Bool, !=); |
| 853 | case Token::GT: return RESULT(Bool, >); |
| 854 | case Token::GTEQ: return RESULT(Bool, >=); |
| 855 | case Token::LT: return RESULT(Bool, <); |
| 856 | case Token::LTEQ: return RESULT(Bool, <=); |
| 857 | default: return nullptr; |
| 858 | } |
| 859 | } |
| 860 | if (left.fKind == Expression::kFloatLiteral_Kind && |
| 861 | right.fKind == Expression::kFloatLiteral_Kind) { |
| 862 | double leftVal = ((FloatLiteral&) left).fValue; |
| 863 | double rightVal = ((FloatLiteral&) right).fValue; |
| 864 | switch (op) { |
| 865 | case Token::PLUS: return RESULT(Float, +); |
| 866 | case Token::MINUS: return RESULT(Float, -); |
| 867 | case Token::STAR: return RESULT(Float, *); |
| 868 | case Token::SLASH: return RESULT(Float, /); |
| 869 | case Token::EQEQ: return RESULT(Bool, ==); |
| 870 | case Token::NEQ: return RESULT(Bool, !=); |
| 871 | case Token::GT: return RESULT(Bool, >); |
| 872 | case Token::GTEQ: return RESULT(Bool, >=); |
| 873 | case Token::LT: return RESULT(Bool, <); |
| 874 | case Token::LTEQ: return RESULT(Bool, <=); |
| 875 | default: return nullptr; |
| 876 | } |
| 877 | } |
| 878 | #undef RESULT |
| 879 | return nullptr; |
| 880 | } |
| 881 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 882 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( |
| 883 | const ASTBinaryExpression& expression) { |
| 884 | std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft); |
| 885 | if (!left) { |
| 886 | return nullptr; |
| 887 | } |
| 888 | std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight); |
| 889 | if (!right) { |
| 890 | return nullptr; |
| 891 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 892 | const Type* leftType; |
| 893 | const Type* rightType; |
| 894 | const Type* resultType; |
| 895 | if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType, |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 896 | &rightType, &resultType, !is_assignment(expression.fOperator))) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 897 | fErrors.error(expression.fPosition, "type mismatch: '" + |
| 898 | Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 899 | "' cannot operate on '" + left->fType.fName + |
| 900 | "', '" + right->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 901 | return nullptr; |
| 902 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 903 | if (is_assignment(expression.fOperator)) { |
| 904 | this->markWrittenTo(*left); |
| 905 | } |
| 906 | left = this->coerce(std::move(left), *leftType); |
| 907 | right = this->coerce(std::move(right), *rightType); |
| 908 | if (!left || !right) { |
| 909 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 910 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 911 | std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator, |
| 912 | *right.get()); |
| 913 | if (!result) { |
| 914 | result = std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition, |
| 915 | std::move(left), |
| 916 | expression.fOperator, |
| 917 | std::move(right), |
| 918 | *resultType)); |
| 919 | } |
| 920 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( |
| 924 | const ASTTernaryExpression& expression) { |
| 925 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 926 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 927 | if (!test) { |
| 928 | return nullptr; |
| 929 | } |
| 930 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue); |
| 931 | if (!ifTrue) { |
| 932 | return nullptr; |
| 933 | } |
| 934 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse); |
| 935 | if (!ifFalse) { |
| 936 | return nullptr; |
| 937 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 938 | const Type* trueType; |
| 939 | const Type* falseType; |
| 940 | const Type* resultType; |
| 941 | if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 942 | &falseType, &resultType, true)) { |
| 943 | fErrors.error(expression.fPosition, "ternary operator result mismatch: '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 944 | ifTrue->fType.fName + "', '" + |
| 945 | ifFalse->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 946 | return nullptr; |
| 947 | } |
| 948 | ASSERT(trueType == falseType); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 949 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
| 950 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 951 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 952 | // static boolean test, just return one of the branches |
| 953 | if (((BoolLiteral&) *test).fValue) { |
| 954 | return ifTrue; |
| 955 | } else { |
| 956 | return ifFalse; |
| 957 | } |
| 958 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 959 | return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition, |
| 960 | std::move(test), |
| 961 | std::move(ifTrue), |
| 962 | std::move(ifFalse))); |
| 963 | } |
| 964 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 965 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 966 | const FunctionDeclaration& function, |
| 967 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 968 | if (function.fParameters.size() != arguments.size()) { |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 969 | std::string msg = "call to '" + function.fName + "' expected " + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 970 | to_string((uint64_t) function.fParameters.size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 971 | " argument"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 972 | if (function.fParameters.size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 973 | msg += "s"; |
| 974 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 975 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 976 | fErrors.error(position, msg); |
| 977 | return nullptr; |
| 978 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 979 | std::vector<const Type*> types; |
| 980 | const Type* returnType; |
| 981 | if (!function.determineFinalTypes(arguments, &types, &returnType)) { |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 982 | std::string msg = "no match for " + function.fName + "("; |
| 983 | std::string separator = ""; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 984 | for (size_t i = 0; i < arguments.size(); i++) { |
| 985 | msg += separator; |
| 986 | separator = ", "; |
| 987 | msg += arguments[i]->fType.description(); |
| 988 | } |
| 989 | msg += ")"; |
| 990 | fErrors.error(position, msg); |
| 991 | return nullptr; |
| 992 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 994 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 995 | if (!arguments[i]) { |
| 996 | return nullptr; |
| 997 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 998 | if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 999 | this->markWrittenTo(*arguments[i]); |
| 1000 | } |
| 1001 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1002 | return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType, function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1003 | std::move(arguments))); |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Determines the cost of coercing the arguments of a function to the required types. Returns true |
| 1008 | * if the cost could be computed, false if the call is not valid. Cost has no particular meaning |
| 1009 | * other than "lower costs are preferred". |
| 1010 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1011 | bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1012 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 1013 | int* outCost) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1014 | if (function.fParameters.size() != arguments.size()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1015 | return false; |
| 1016 | } |
| 1017 | int total = 0; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1018 | std::vector<const Type*> types; |
| 1019 | const Type* ignored; |
| 1020 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
| 1021 | return false; |
| 1022 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1023 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1024 | int cost; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1025 | if (arguments[i]->fType.determineCoercionCost(*types[i], &cost)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1026 | total += cost; |
| 1027 | } else { |
| 1028 | return false; |
| 1029 | } |
| 1030 | } |
| 1031 | *outCost = total; |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 1036 | std::unique_ptr<Expression> functionValue, |
| 1037 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 1038 | if (functionValue->fKind == Expression::kTypeReference_Kind) { |
| 1039 | return this->convertConstructor(position, |
| 1040 | ((TypeReference&) *functionValue).fValue, |
| 1041 | std::move(arguments)); |
| 1042 | } |
| 1043 | if (functionValue->fKind != Expression::kFunctionReference_Kind) { |
| 1044 | fErrors.error(position, "'" + functionValue->description() + "' is not a function"); |
| 1045 | return nullptr; |
| 1046 | } |
| 1047 | FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 1048 | int bestCost = INT_MAX; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1049 | const FunctionDeclaration* best = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1050 | if (ref->fFunctions.size() > 1) { |
| 1051 | for (const auto& f : ref->fFunctions) { |
| 1052 | int cost; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1053 | if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1054 | bestCost = cost; |
| 1055 | best = f; |
| 1056 | } |
| 1057 | } |
| 1058 | if (best) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1059 | return this->call(position, *best, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1060 | } |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 1061 | std::string msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 1062 | std::string separator = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1063 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1064 | msg += separator; |
| 1065 | separator = ", "; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1066 | msg += arguments[i]->fType.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1067 | } |
| 1068 | msg += ")"; |
| 1069 | fErrors.error(position, msg); |
| 1070 | return nullptr; |
| 1071 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1072 | return this->call(position, *ref->fFunctions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | std::unique_ptr<Expression> IRGenerator::convertConstructor( |
| 1076 | Position position, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1077 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1078 | std::vector<std::unique_ptr<Expression>> args) { |
| 1079 | // FIXME: add support for structs and arrays |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1080 | Type::Kind kind = type.kind(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1081 | if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind && |
| 1082 | kind != Type::kArray_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1083 | fErrors.error(position, "cannot construct '" + type.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1084 | return nullptr; |
| 1085 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1086 | if (type == *fContext.fFloat_Type && args.size() == 1 && |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1087 | args[0]->fKind == Expression::kIntLiteral_Kind) { |
| 1088 | int64_t value = ((IntLiteral&) *args[0]).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1089 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1090 | } |
| 1091 | if (args.size() == 1 && args[0]->fType == type) { |
| 1092 | // argument is already the right type, just return it |
| 1093 | return std::move(args[0]); |
| 1094 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1095 | if (type.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1096 | if (args.size() != 1) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1097 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1098 | "' constructor, (expected exactly 1 argument, but found " + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1099 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1100 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1101 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1102 | if (args[0]->fType == *fContext.fBool_Type) { |
| 1103 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0)); |
| 1104 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1105 | return std::unique_ptr<Expression>( |
| 1106 | new TernaryExpression(position, std::move(args[0]), |
| 1107 | this->coerce(std::move(one), type), |
| 1108 | this->coerce(std::move(zero), |
| 1109 | type))); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1110 | } else if (!args[0]->fType.isNumber()) { |
| 1111 | fErrors.error(position, "invalid argument to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1112 | "' constructor (expected a number or bool, but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1113 | args[0]->fType.description() + "')"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1114 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1115 | if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type || |
| 1116 | type == *fContext.fUInt_Type)) { |
| 1117 | return std::unique_ptr<Expression>(new IntLiteral(fContext, |
| 1118 | position, |
| 1119 | ((IntLiteral&) *args[0]).fValue, |
| 1120 | &type)); |
| 1121 | } |
| 1122 | } else if (kind == Type::kArray_Kind) { |
| 1123 | const Type& base = type.componentType(); |
| 1124 | for (size_t i = 0; i < args.size(); i++) { |
| 1125 | args[i] = this->coerce(std::move(args[i]), base); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1126 | if (!args[i]) { |
| 1127 | return nullptr; |
| 1128 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1129 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1130 | } else { |
| 1131 | ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); |
| 1132 | int actual = 0; |
| 1133 | for (size_t i = 0; i < args.size(); i++) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1134 | if (args[i]->fType.kind() == Type::kVector_Kind || |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1135 | args[i]->fType.kind() == Type::kMatrix_Kind) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1136 | if (type.componentType().isNumber() != |
| 1137 | args[i]->fType.componentType().isNumber()) { |
| 1138 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1139 | "parameter to '" + type.description() + |
| 1140 | "' constructor"); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1141 | return nullptr; |
| 1142 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1143 | actual += args[i]->fType.rows() * args[i]->fType.columns(); |
| 1144 | } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1145 | actual += 1; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1146 | if (type.kind() != Type::kScalar_Kind) { |
| 1147 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1148 | if (!args[i]) { |
| 1149 | return nullptr; |
| 1150 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1151 | } |
| 1152 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1153 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1154 | "parameter to '" + type.description() + "' constructor"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1155 | return nullptr; |
| 1156 | } |
| 1157 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1158 | int min = type.rows() * type.columns(); |
| 1159 | int max = type.columns() > 1 ? INT_MAX : min; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1160 | if ((actual < min || actual > max) && |
| 1161 | !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1162 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1163 | "' constructor (expected " + to_string(min) + " scalar" + |
| 1164 | (min == 1 ? "" : "s") + ", but found " + to_string(actual) + |
| 1165 | ")"); |
| 1166 | return nullptr; |
| 1167 | } |
| 1168 | } |
| 1169 | return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args))); |
| 1170 | } |
| 1171 | |
| 1172 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( |
| 1173 | const ASTPrefixExpression& expression) { |
| 1174 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand); |
| 1175 | if (!base) { |
| 1176 | return nullptr; |
| 1177 | } |
| 1178 | switch (expression.fOperator) { |
| 1179 | case Token::PLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1180 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1181 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1182 | "'+' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1183 | return nullptr; |
| 1184 | } |
| 1185 | return base; |
| 1186 | case Token::MINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1187 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1188 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1189 | "'-' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1190 | return nullptr; |
| 1191 | } |
| 1192 | if (base->fKind == Expression::kIntLiteral_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1193 | return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1194 | -((IntLiteral&) *base).fValue)); |
| 1195 | } |
| 1196 | if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 1197 | double value = -((FloatLiteral&) *base).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1198 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition, |
| 1199 | value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1200 | } |
| 1201 | return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base))); |
| 1202 | case Token::PLUSPLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1203 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1204 | fErrors.error(expression.fPosition, |
| 1205 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1206 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1207 | return nullptr; |
| 1208 | } |
| 1209 | this->markWrittenTo(*base); |
| 1210 | break; |
| 1211 | case Token::MINUSMINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1212 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1213 | fErrors.error(expression.fPosition, |
| 1214 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1215 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1216 | return nullptr; |
| 1217 | } |
| 1218 | this->markWrittenTo(*base); |
| 1219 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1220 | case Token::LOGICALNOT: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1221 | if (base->fType != *fContext.fBool_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1222 | fErrors.error(expression.fPosition, |
| 1223 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1224 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1225 | return nullptr; |
| 1226 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1227 | if (base->fKind == Expression::kBoolLiteral_Kind) { |
| 1228 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fPosition, |
| 1229 | !((BoolLiteral&) *base).fValue)); |
| 1230 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1231 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1232 | case Token::BITWISENOT: |
| 1233 | if (base->fType != *fContext.fInt_Type) { |
| 1234 | fErrors.error(expression.fPosition, |
| 1235 | "'" + Token::OperatorName(expression.fOperator) + |
| 1236 | "' cannot operate on '" + base->fType.description() + "'"); |
| 1237 | return nullptr; |
| 1238 | } |
| 1239 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1240 | default: |
| 1241 | ABORT("unsupported prefix operator\n"); |
| 1242 | } |
| 1243 | return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator, |
| 1244 | std::move(base))); |
| 1245 | } |
| 1246 | |
| 1247 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
| 1248 | const ASTExpression& index) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1249 | if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind && |
| 1250 | base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1251 | fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1252 | "'"); |
| 1253 | return nullptr; |
| 1254 | } |
| 1255 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 1256 | if (!converted) { |
| 1257 | return nullptr; |
| 1258 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1259 | if (converted->fType != *fContext.fUInt_Type) { |
| 1260 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 1261 | if (!converted) { |
| 1262 | return nullptr; |
| 1263 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1264 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1265 | return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base), |
| 1266 | std::move(converted))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 1270 | const std::string& field) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1271 | auto fields = base->fType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1272 | for (size_t i = 0; i < fields.size(); i++) { |
| 1273 | if (fields[i].fName == field) { |
| 1274 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 1275 | } |
| 1276 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1277 | fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a " |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1278 | "field named '" + field + ""); |
| 1279 | return nullptr; |
| 1280 | } |
| 1281 | |
| 1282 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 1283 | const std::string& fields) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1284 | if (base->fType.kind() != Type::kVector_Kind) { |
| 1285 | fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1286 | return nullptr; |
| 1287 | } |
| 1288 | std::vector<int> swizzleComponents; |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 1289 | for (char c : fields) { |
| 1290 | switch (c) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1291 | case 'x': // fall through |
| 1292 | case 'r': // fall through |
| 1293 | case 's': |
| 1294 | swizzleComponents.push_back(0); |
| 1295 | break; |
| 1296 | case 'y': // fall through |
| 1297 | case 'g': // fall through |
| 1298 | case 't': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1299 | if (base->fType.columns() >= 2) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1300 | swizzleComponents.push_back(1); |
| 1301 | break; |
| 1302 | } |
| 1303 | // fall through |
| 1304 | case 'z': // fall through |
| 1305 | case 'b': // fall through |
| 1306 | case 'p': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1307 | if (base->fType.columns() >= 3) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1308 | swizzleComponents.push_back(2); |
| 1309 | break; |
| 1310 | } |
| 1311 | // fall through |
| 1312 | case 'w': // fall through |
| 1313 | case 'a': // fall through |
| 1314 | case 'q': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1315 | if (base->fType.columns() >= 4) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1316 | swizzleComponents.push_back(3); |
| 1317 | break; |
| 1318 | } |
| 1319 | // fall through |
| 1320 | default: |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 1321 | fErrors.error(base->fPosition, "invalid swizzle component '" + std::string(1, c) + |
| 1322 | "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1323 | return nullptr; |
| 1324 | } |
| 1325 | } |
| 1326 | ASSERT(swizzleComponents.size() > 0); |
| 1327 | if (swizzleComponents.size() > 4) { |
| 1328 | fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'"); |
| 1329 | return nullptr; |
| 1330 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1331 | return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | std::unique_ptr<Expression> IRGenerator::convertSuffixExpression( |
| 1335 | const ASTSuffixExpression& expression) { |
| 1336 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase); |
| 1337 | if (!base) { |
| 1338 | return nullptr; |
| 1339 | } |
| 1340 | switch (expression.fSuffix->fKind) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1341 | case ASTSuffix::kIndex_Kind: { |
| 1342 | const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get(); |
| 1343 | if (expr) { |
| 1344 | return this->convertIndex(std::move(base), *expr); |
| 1345 | } else if (base->fKind == Expression::kTypeReference_Kind) { |
| 1346 | const Type& oldType = ((TypeReference&) *base).fValue; |
| 1347 | Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType, |
| 1348 | -1); |
| 1349 | fSymbolTable->takeOwnership(newType); |
| 1350 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition, |
| 1351 | *newType)); |
| 1352 | } else { |
| 1353 | fErrors.error(expression.fPosition, "'[]' must follow a type name"); |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1354 | return nullptr; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1355 | } |
| 1356 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1357 | case ASTSuffix::kCall_Kind: { |
| 1358 | auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments; |
| 1359 | std::vector<std::unique_ptr<Expression>> arguments; |
| 1360 | for (size_t i = 0; i < rawArguments->size(); i++) { |
| 1361 | std::unique_ptr<Expression> converted = |
| 1362 | this->convertExpression(*(*rawArguments)[i]); |
| 1363 | if (!converted) { |
| 1364 | return nullptr; |
| 1365 | } |
| 1366 | arguments.push_back(std::move(converted)); |
| 1367 | } |
| 1368 | return this->call(expression.fPosition, std::move(base), std::move(arguments)); |
| 1369 | } |
| 1370 | case ASTSuffix::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1371 | switch (base->fType.kind()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1372 | case Type::kVector_Kind: |
| 1373 | return this->convertSwizzle(std::move(base), |
| 1374 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1375 | case Type::kStruct_Kind: |
| 1376 | return this->convertField(std::move(base), |
| 1377 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1378 | default: |
| 1379 | fErrors.error(base->fPosition, "cannot swizzle value of type '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1380 | base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1381 | return nullptr; |
| 1382 | } |
| 1383 | } |
| 1384 | case ASTSuffix::kPostIncrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1385 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1386 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1387 | "'++' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1388 | return nullptr; |
| 1389 | } |
| 1390 | this->markWrittenTo(*base); |
| 1391 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
| 1392 | Token::PLUSPLUS)); |
| 1393 | case ASTSuffix::kPostDecrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1394 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1395 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1396 | "'--' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1397 | return nullptr; |
| 1398 | } |
| 1399 | this->markWrittenTo(*base); |
| 1400 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
| 1401 | Token::MINUSMINUS)); |
| 1402 | default: |
| 1403 | ABORT("unsupported suffix operator"); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | void IRGenerator::checkValid(const Expression& expr) { |
| 1408 | switch (expr.fKind) { |
| 1409 | case Expression::kFunctionReference_Kind: |
| 1410 | fErrors.error(expr.fPosition, "expected '(' to begin function call"); |
| 1411 | break; |
| 1412 | case Expression::kTypeReference_Kind: |
| 1413 | fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation"); |
| 1414 | break; |
| 1415 | default: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1416 | if (expr.fType == *fContext.fInvalid_Type) { |
| 1417 | fErrors.error(expr.fPosition, "invalid expression"); |
| 1418 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1422 | void IRGenerator::markReadFrom(const Variable& var) { |
| 1423 | var.fIsReadFrom = true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static bool has_duplicates(const Swizzle& swizzle) { |
| 1427 | int bits = 0; |
| 1428 | for (int idx : swizzle.fComponents) { |
| 1429 | ASSERT(idx >= 0 && idx <= 3); |
| 1430 | int bit = 1 << idx; |
| 1431 | if (bits & bit) { |
| 1432 | return true; |
| 1433 | } |
| 1434 | bits |= bit; |
| 1435 | } |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
| 1439 | void IRGenerator::markWrittenTo(const Expression& expr) { |
| 1440 | switch (expr.fKind) { |
| 1441 | case Expression::kVariableReference_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1442 | const Variable& var = ((VariableReference&) expr).fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1443 | if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
| 1444 | fErrors.error(expr.fPosition, |
| 1445 | "cannot modify immutable variable '" + var.fName + "'"); |
| 1446 | } |
| 1447 | var.fIsWrittenTo = true; |
| 1448 | break; |
| 1449 | } |
| 1450 | case Expression::kFieldAccess_Kind: |
| 1451 | this->markWrittenTo(*((FieldAccess&) expr).fBase); |
| 1452 | break; |
| 1453 | case Expression::kSwizzle_Kind: |
| 1454 | if (has_duplicates((Swizzle&) expr)) { |
| 1455 | fErrors.error(expr.fPosition, |
| 1456 | "cannot write to the same swizzle field more than once"); |
| 1457 | } |
| 1458 | this->markWrittenTo(*((Swizzle&) expr).fBase); |
| 1459 | break; |
| 1460 | case Expression::kIndex_Kind: |
| 1461 | this->markWrittenTo(*((IndexExpression&) expr).fBase); |
| 1462 | break; |
| 1463 | default: |
| 1464 | fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'"); |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | } |