blob: 55d9d2c8d655c54b2018f3c36b92e7d7ffba3ad8 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas11d53972016-11-28 11:23:23 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#include "SkSLIRGenerator.h"
9
10#include "limits.h"
11
Ethan Nicholas941e7e22016-12-12 15:33:30 -050012#include "SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070013#include "ast/SkSLASTBoolLiteral.h"
14#include "ast/SkSLASTFieldSuffix.h"
15#include "ast/SkSLASTFloatLiteral.h"
16#include "ast/SkSLASTIndexSuffix.h"
17#include "ast/SkSLASTIntLiteral.h"
18#include "ir/SkSLBinaryExpression.h"
19#include "ir/SkSLBoolLiteral.h"
20#include "ir/SkSLBreakStatement.h"
21#include "ir/SkSLConstructor.h"
22#include "ir/SkSLContinueStatement.h"
23#include "ir/SkSLDiscardStatement.h"
24#include "ir/SkSLDoStatement.h"
25#include "ir/SkSLExpressionStatement.h"
26#include "ir/SkSLField.h"
27#include "ir/SkSLFieldAccess.h"
28#include "ir/SkSLFloatLiteral.h"
29#include "ir/SkSLForStatement.h"
30#include "ir/SkSLFunctionCall.h"
31#include "ir/SkSLFunctionDeclaration.h"
32#include "ir/SkSLFunctionDefinition.h"
33#include "ir/SkSLFunctionReference.h"
34#include "ir/SkSLIfStatement.h"
35#include "ir/SkSLIndexExpression.h"
36#include "ir/SkSLInterfaceBlock.h"
37#include "ir/SkSLIntLiteral.h"
38#include "ir/SkSLLayout.h"
39#include "ir/SkSLPostfixExpression.h"
40#include "ir/SkSLPrefixExpression.h"
41#include "ir/SkSLReturnStatement.h"
42#include "ir/SkSLSwizzle.h"
43#include "ir/SkSLTernaryExpression.h"
44#include "ir/SkSLUnresolvedFunction.h"
45#include "ir/SkSLVariable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070046#include "ir/SkSLVarDeclarations.h"
47#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070048#include "ir/SkSLVariableReference.h"
49#include "ir/SkSLWhileStatement.h"
50
51namespace SkSL {
52
53class AutoSymbolTable {
54public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050055 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070056 : fIR(ir)
57 , fPrevious(fIR->fSymbolTable) {
58 fIR->pushSymbolTable();
59 }
60
61 ~AutoSymbolTable() {
62 fIR->popSymbolTable();
63 ASSERT(fPrevious == fIR->fSymbolTable);
64 }
65
66 IRGenerator* fIR;
67 std::shared_ptr<SymbolTable> fPrevious;
68};
69
ethannicholas22f939e2016-10-13 13:25:34 -070070class AutoLoopLevel {
71public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050072 AutoLoopLevel(IRGenerator* ir)
ethannicholas22f939e2016-10-13 13:25:34 -070073 : fIR(ir) {
74 fIR->fLoopLevel++;
75 }
76
77 ~AutoLoopLevel() {
78 fIR->fLoopLevel--;
79 }
80
81 IRGenerator* fIR;
82};
83
Ethan Nicholas11d53972016-11-28 11:23:23 -050084IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
ethannicholasb3058bd2016-07-01 08:22:01 -070085 ErrorReporter& errorReporter)
ethannicholasd598f792016-07-25 10:08:54 -070086: fContext(*context)
87, fCurrentFunction(nullptr)
88, fSymbolTable(std::move(symbolTable))
ethannicholas22f939e2016-10-13 13:25:34 -070089, fLoopLevel(0)
ethannicholasd598f792016-07-25 10:08:54 -070090, fErrors(errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070091
92void IRGenerator::pushSymbolTable() {
93 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors));
94}
95
96void IRGenerator::popSymbolTable() {
97 fSymbolTable = fSymbolTable->fParent;
98}
99
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500100static void fill_caps(const GrShaderCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) {
101#define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name())));
102 CAP(fbFetchSupport);
103 CAP(fbFetchNeedsCustomOutput);
104 CAP(bindlessTextureSupport);
105 CAP(dropsTileOnZeroDivide);
106 CAP(flatInterpolationSupport);
107 CAP(noperspectiveInterpolationSupport);
108 CAP(multisampleInterpolationSupport);
109 CAP(sampleVariablesSupport);
110 CAP(sampleMaskOverrideCoverageSupport);
111 CAP(externalTextureSupport);
112 CAP(texelFetchSupport);
113 CAP(imageLoadStoreSupport);
114 CAP(mustEnableAdvBlendEqs);
115 CAP(mustEnableSpecificAdvBlendEqs);
116 CAP(mustDeclareFragmentShaderOutput);
117 CAP(canUseAnyFunctionInShader);
118#undef CAP
119}
120
121void IRGenerator::start(const Program::Settings* settings) {
122 fSettings = settings;
123 fCapsMap.clear();
124 if (settings->fCaps) {
125 fill_caps(*settings->fCaps, &fCapsMap);
126 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500127 this->pushSymbolTable();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500128 fInputs.reset();
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500129}
130
131void IRGenerator::finish() {
132 this->popSymbolTable();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500133 fSettings = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500134}
135
ethannicholasb3058bd2016-07-01 08:22:01 -0700136std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
137 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName));
138}
139
140std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) {
141 switch (statement.fKind) {
142 case ASTStatement::kBlock_Kind:
143 return this->convertBlock((ASTBlock&) statement);
144 case ASTStatement::kVarDeclaration_Kind:
145 return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement);
146 case ASTStatement::kExpression_Kind:
147 return this->convertExpressionStatement((ASTExpressionStatement&) statement);
148 case ASTStatement::kIf_Kind:
149 return this->convertIf((ASTIfStatement&) statement);
150 case ASTStatement::kFor_Kind:
151 return this->convertFor((ASTForStatement&) statement);
152 case ASTStatement::kWhile_Kind:
153 return this->convertWhile((ASTWhileStatement&) statement);
154 case ASTStatement::kDo_Kind:
155 return this->convertDo((ASTDoStatement&) statement);
156 case ASTStatement::kReturn_Kind:
157 return this->convertReturn((ASTReturnStatement&) statement);
158 case ASTStatement::kBreak_Kind:
159 return this->convertBreak((ASTBreakStatement&) statement);
160 case ASTStatement::kContinue_Kind:
161 return this->convertContinue((ASTContinueStatement&) statement);
162 case ASTStatement::kDiscard_Kind:
163 return this->convertDiscard((ASTDiscardStatement&) statement);
164 default:
165 ABORT("unsupported statement type: %d\n", statement.fKind);
166 }
167}
168
169std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) {
170 AutoSymbolTable table(this);
171 std::vector<std::unique_ptr<Statement>> statements;
172 for (size_t i = 0; i < block.fStatements.size(); i++) {
173 std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]);
174 if (!statement) {
175 return nullptr;
176 }
177 statements.push_back(std::move(statement));
178 }
ethannicholasd598f792016-07-25 10:08:54 -0700179 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700180}
181
182std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
183 const ASTVarDeclarationStatement& s) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700184 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700185 if (!decl) {
186 return nullptr;
187 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700188 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700189}
190
ethannicholas14fe8cc2016-09-07 13:37:16 -0700191std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl,
192 Variable::Storage storage) {
193 std::vector<VarDeclaration> variables;
ethannicholasd598f792016-07-25 10:08:54 -0700194 const Type* baseType = this->convertType(*decl.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195 if (!baseType) {
196 return nullptr;
197 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700198 for (const auto& varDecl : decl.fVars) {
ethannicholasd598f792016-07-25 10:08:54 -0700199 const Type* type = baseType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700200 ASSERT(type->kind() != Type::kArray_Kind);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700201 std::vector<std::unique_ptr<Expression>> sizes;
202 for (const auto& rawSize : varDecl.fSizes) {
203 if (rawSize) {
204 auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 if (!size) {
206 return nullptr;
207 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500208 SkString name = type->fName;
ethannicholasb3058bd2016-07-01 08:22:01 -0700209 uint64_t count;
210 if (size->fKind == Expression::kIntLiteral_Kind) {
211 count = ((IntLiteral&) *size).fValue;
212 if (count <= 0) {
213 fErrors.error(size->fPosition, "array size must be positive");
214 }
215 name += "[" + to_string(count) + "]";
216 } else {
217 count = -1;
218 name += "[]";
219 }
ethannicholasd598f792016-07-25 10:08:54 -0700220 type = new Type(name, Type::kArray_Kind, *type, (int) count);
221 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700222 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700223 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700224 type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1);
225 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700226 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 }
228 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500229 auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, decl.fModifiers,
230 varDecl.fName, *type, storage));
ethannicholasb3058bd2016-07-01 08:22:01 -0700231 std::unique_ptr<Expression> value;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700232 if (varDecl.fValue) {
233 value = this->convertExpression(*varDecl.fValue);
ethannicholasb3058bd2016-07-01 08:22:01 -0700234 if (!value) {
235 return nullptr;
236 }
ethannicholasd598f792016-07-25 10:08:54 -0700237 value = this->coerce(std::move(value), *type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700238 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500239 if (storage == Variable::kGlobal_Storage && varDecl.fName == SkString("sk_FragColor") &&
ethannicholasea4567c2016-10-17 11:24:37 -0700240 (*fSymbolTable)[varDecl.fName]) {
ethannicholas5961bc92016-10-12 06:39:56 -0700241 // already defined, ignore
ethannicholasea4567c2016-10-17 11:24:37 -0700242 } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] &&
243 (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -0700244 ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700245 // already defined, just update the modifiers
ethannicholas14fe8cc2016-09-07 13:37:16 -0700246 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
ethannicholasf789b382016-08-03 12:43:36 -0700247 old->fModifiers = var->fModifiers;
248 } else {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700249 variables.emplace_back(var.get(), std::move(sizes), std::move(value));
250 fSymbolTable->add(varDecl.fName, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700251 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700252 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500253 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700254 baseType,
255 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700256}
257
ethannicholas5961bc92016-10-12 06:39:56 -0700258std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(
259 const ASTModifiersDeclaration& m) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500260 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(m.fModifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700261}
262
ethannicholasb3058bd2016-07-01 08:22:01 -0700263std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500264 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700265 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700266 if (!test) {
267 return nullptr;
268 }
269 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
270 if (!ifTrue) {
271 return nullptr;
272 }
273 std::unique_ptr<Statement> ifFalse;
274 if (s.fIfFalse) {
275 ifFalse = this->convertStatement(*s.fIfFalse);
276 if (!ifFalse) {
277 return nullptr;
278 }
279 }
ethannicholas08a92112016-11-09 13:26:45 -0800280 if (test->fKind == Expression::kBoolLiteral_Kind) {
281 // static boolean value, fold down to a single branch
282 if (((BoolLiteral&) *test).fValue) {
283 return ifTrue;
284 } else if (s.fIfFalse) {
285 return ifFalse;
286 } else {
287 // False & no else clause. Not an error, so don't return null!
288 std::vector<std::unique_ptr<Statement>> empty;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500289 return std::unique_ptr<Statement>(new Block(s.fPosition, std::move(empty),
ethannicholas08a92112016-11-09 13:26:45 -0800290 fSymbolTable));
291 }
292 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500293 return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700294 std::move(ifTrue), std::move(ifFalse)));
295}
296
297std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) {
ethannicholas22f939e2016-10-13 13:25:34 -0700298 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700299 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700300 std::unique_ptr<Statement> initializer;
301 if (f.fInitializer) {
302 initializer = this->convertStatement(*f.fInitializer);
303 if (!initializer) {
304 return nullptr;
305 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700306 }
ethannicholas22f939e2016-10-13 13:25:34 -0700307 std::unique_ptr<Expression> test;
308 if (f.fTest) {
309 test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type);
310 if (!test) {
311 return nullptr;
312 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700313 }
ethannicholas22f939e2016-10-13 13:25:34 -0700314 std::unique_ptr<Expression> next;
315 if (f.fNext) {
316 next = this->convertExpression(*f.fNext);
317 if (!next) {
318 return nullptr;
319 }
320 this->checkValid(*next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700321 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700322 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement);
323 if (!statement) {
324 return nullptr;
325 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500326 return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer),
ethannicholasb3058bd2016-07-01 08:22:01 -0700327 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700328 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700329}
330
331std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) {
ethannicholas22f939e2016-10-13 13:25:34 -0700332 AutoLoopLevel level(this);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500333 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700334 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700335 if (!test) {
336 return nullptr;
337 }
338 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement);
339 if (!statement) {
340 return nullptr;
341 }
342 return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test),
343 std::move(statement)));
344}
345
346std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) {
ethannicholas22f939e2016-10-13 13:25:34 -0700347 AutoLoopLevel level(this);
ethannicholasd598f792016-07-25 10:08:54 -0700348 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest),
349 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700350 if (!test) {
351 return nullptr;
352 }
353 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement);
354 if (!statement) {
355 return nullptr;
356 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500357 return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement),
ethannicholasb3058bd2016-07-01 08:22:01 -0700358 std::move(test)));
359}
360
361std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(
362 const ASTExpressionStatement& s) {
363 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
364 if (!e) {
365 return nullptr;
366 }
367 this->checkValid(*e);
368 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
369}
370
371std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) {
372 ASSERT(fCurrentFunction);
373 if (r.fExpression) {
374 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression);
375 if (!result) {
376 return nullptr;
377 }
ethannicholasd598f792016-07-25 10:08:54 -0700378 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700379 fErrors.error(result->fPosition, "may not return a value from a void function");
380 } else {
381 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
382 if (!result) {
383 return nullptr;
384 }
385 }
386 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
387 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700388 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700389 fErrors.error(r.fPosition, "expected function to return '" +
ethannicholasd598f792016-07-25 10:08:54 -0700390 fCurrentFunction->fReturnType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 }
392 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition));
393 }
394}
395
396std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) {
ethannicholas22f939e2016-10-13 13:25:34 -0700397 if (fLoopLevel > 0) {
398 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition));
399 } else {
400 fErrors.error(b.fPosition, "break statement must be inside a loop");
401 return nullptr;
402 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700403}
404
405std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) {
ethannicholas22f939e2016-10-13 13:25:34 -0700406 if (fLoopLevel > 0) {
407 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition));
408 } else {
409 fErrors.error(c.fPosition, "continue statement must be inside a loop");
410 return nullptr;
411 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700412}
413
414std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) {
415 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition));
416}
417
ethannicholasb3058bd2016-07-01 08:22:01 -0700418std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) {
ethannicholasd598f792016-07-25 10:08:54 -0700419 const Type* returnType = this->convertType(*f.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700420 if (!returnType) {
421 return nullptr;
422 }
ethannicholasd598f792016-07-25 10:08:54 -0700423 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700424 for (const auto& param : f.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700425 const Type* type = this->convertType(*param->fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700426 if (!type) {
427 return nullptr;
428 }
429 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) {
430 int size = param->fSizes[j];
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500431 SkString name = type->name() + "[" + to_string(size) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700432 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size);
433 fSymbolTable->takeOwnership(newType);
434 type = newType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700435 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500436 SkString name = param->fName;
ethannicholasb3058bd2016-07-01 08:22:01 -0700437 Position pos = param->fPosition;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500438 Variable* var = new Variable(pos, param->fModifiers, std::move(name), *type,
ethannicholasd598f792016-07-25 10:08:54 -0700439 Variable::kParameter_Storage);
440 fSymbolTable->takeOwnership(var);
441 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700442 }
443
444 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700445 const FunctionDeclaration* decl = nullptr;
446 auto entry = (*fSymbolTable)[f.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700447 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700448 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700449 switch (entry->fKind) {
450 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700451 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700452 break;
453 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700454 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700455 break;
456 default:
457 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined");
458 return nullptr;
459 }
460 for (const auto& other : functions) {
461 ASSERT(other->fName == f.fName);
462 if (parameters.size() == other->fParameters.size()) {
463 bool match = true;
464 for (size_t i = 0; i < parameters.size(); i++) {
465 if (parameters[i]->fType != other->fParameters[i]->fType) {
466 match = false;
467 break;
468 }
469 }
470 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700471 if (*returnType != other->fReturnType) {
472 FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700473 fErrors.error(f.fPosition, "functions '" + newDecl.description() +
Ethan Nicholas11d53972016-11-28 11:23:23 -0500474 "' and '" + other->description() +
ethannicholasb3058bd2016-07-01 08:22:01 -0700475 "' differ only in return type");
476 return nullptr;
477 }
478 decl = other;
479 for (size_t i = 0; i < parameters.size(); i++) {
480 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500481 fErrors.error(f.fPosition, "modifiers on parameter " +
482 to_string((uint64_t) i + 1) +
ethannicholas5961bc92016-10-12 06:39:56 -0700483 " differ between declaration and "
484 "definition");
ethannicholasb3058bd2016-07-01 08:22:01 -0700485 return nullptr;
486 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700487 }
488 if (other->fDefined) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500489 fErrors.error(f.fPosition, "duplicate definition of " +
ethannicholasb3058bd2016-07-01 08:22:01 -0700490 other->description());
491 }
492 break;
493 }
494 }
495 }
496 }
497 if (!decl) {
498 // couldn't find an existing declaration
ethannicholas471e8942016-10-28 09:02:46 -0700499 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fPosition,
500 f.fName,
501 parameters,
502 *returnType));
503 decl = newDecl.get();
504 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700505 }
ethannicholasd598f792016-07-25 10:08:54 -0700506 if (f.fBody) {
507 ASSERT(!fCurrentFunction);
508 fCurrentFunction = decl;
509 decl->fDefined = true;
510 std::shared_ptr<SymbolTable> old = fSymbolTable;
511 AutoSymbolTable table(this);
512 for (size_t i = 0; i < parameters.size(); i++) {
513 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700514 }
ethannicholasd598f792016-07-25 10:08:54 -0700515 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
516 fCurrentFunction = nullptr;
517 if (!body) {
518 return nullptr;
519 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500520 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl,
ethannicholasd598f792016-07-25 10:08:54 -0700521 std::move(body)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700522 }
523 return nullptr;
524}
525
526std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) {
527 std::shared_ptr<SymbolTable> old = fSymbolTable;
528 AutoSymbolTable table(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700529 std::vector<Type::Field> fields;
530 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700531 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
Ethan Nicholas11d53972016-11-28 11:23:23 -0500532 *intf.fDeclarations[i],
ethannicholasb3058bd2016-07-01 08:22:01 -0700533 Variable::kGlobal_Storage);
ethannicholas7effa7a2016-10-14 09:56:33 -0700534 if (!decl) {
535 return nullptr;
536 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700537 for (const auto& var : decl->fVars) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500538 fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700539 &var.fVar->fType));
540 if (var.fValue) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500541 fErrors.error(decl->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700542 "initializers are not permitted on interface block fields");
543 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700544 if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
545 Modifiers::kOut_Flag |
546 Modifiers::kUniform_Flag |
547 Modifiers::kConst_Flag)) {
548 fErrors.error(decl->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700549 "interface block fields may not have storage qualifiers");
550 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500551 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700552 }
Ethan Nicholas19671772016-11-28 16:30:17 -0500553 Type* type = new Type(intf.fPosition, intf.fInterfaceName, fields);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500554 old->takeOwnership(type);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500555 SkString name = intf.fValueName.size() > 0 ? intf.fValueName : intf.fInterfaceName;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500556 Variable* var = new Variable(intf.fPosition, intf.fModifiers, name, *type,
557 Variable::kGlobal_Storage);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500558 old->takeOwnership(var);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500559 if (intf.fValueName.size()) {
ethannicholasd598f792016-07-25 10:08:54 -0700560 old->addWithoutOwnership(intf.fValueName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700561 } else {
562 for (size_t i = 0; i < fields.size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500563 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var,
ethannicholasd598f792016-07-25 10:08:54 -0700564 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700565 }
566 }
ethannicholasd598f792016-07-25 10:08:54 -0700567 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700568}
569
ethannicholasd598f792016-07-25 10:08:54 -0700570const Type* IRGenerator::convertType(const ASTType& type) {
571 const Symbol* result = (*fSymbolTable)[type.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700572 if (result && result->fKind == Symbol::kType_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700573 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700574 }
575 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'");
576 return nullptr;
577}
578
579std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) {
580 switch (expr.fKind) {
581 case ASTExpression::kIdentifier_Kind:
582 return this->convertIdentifier((ASTIdentifier&) expr);
583 case ASTExpression::kBool_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700584 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700585 ((ASTBoolLiteral&) expr).fValue));
586 case ASTExpression::kInt_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700587 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700588 ((ASTIntLiteral&) expr).fValue));
589 case ASTExpression::kFloat_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700590 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700591 ((ASTFloatLiteral&) expr).fValue));
592 case ASTExpression::kBinary_Kind:
593 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
594 case ASTExpression::kPrefix_Kind:
595 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
596 case ASTExpression::kSuffix_Kind:
597 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
598 case ASTExpression::kTernary_Kind:
599 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
600 default:
601 ABORT("unsupported expression type: %d\n", expr.fKind);
602 }
603}
604
605std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) {
ethannicholasd598f792016-07-25 10:08:54 -0700606 const Symbol* result = (*fSymbolTable)[identifier.fText];
ethannicholasb3058bd2016-07-01 08:22:01 -0700607 if (!result) {
608 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'");
609 return nullptr;
610 }
611 switch (result->fKind) {
612 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700613 std::vector<const FunctionDeclaration*> f = {
614 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -0700615 };
ethannicholasd598f792016-07-25 10:08:54 -0700616 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
617 identifier.fPosition,
618 f));
ethannicholasb3058bd2016-07-01 08:22:01 -0700619 }
620 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700621 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
622 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
623 identifier.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700624 f->fFunctions));
625 }
626 case Symbol::kVariable_Kind: {
Ethan Nicholascae3a4c2017-02-02 10:43:58 -0500627 Variable* var = (Variable*) result;
628 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
629 fSettings->fFlipY &&
630 (!fSettings->fCaps || !fSettings->fCaps->fragCoordConventionsExtensionString())) {
631 fInputs.fRTHeight = true;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500632 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500633 // default to kRead_RefKind; this will be corrected later if the variable is written to
634 return std::unique_ptr<VariableReference>(new VariableReference(
635 identifier.fPosition,
636 *var,
637 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -0700638 }
639 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700640 const Field* field = (const Field*) result;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500641 VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner,
642 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -0700643 return std::unique_ptr<Expression>(new FieldAccess(
644 std::unique_ptr<Expression>(base),
645 field->fFieldIndex,
646 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -0700647 }
648 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700649 const Type* t = (const Type*) result;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500650 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -0700651 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -0700652 }
653 default:
654 ABORT("unsupported symbol type %d\n", result->fKind);
655 }
656
657}
658
Ethan Nicholas11d53972016-11-28 11:23:23 -0500659std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700660 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700661 if (!expr) {
662 return nullptr;
663 }
ethannicholasd598f792016-07-25 10:08:54 -0700664 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700665 return expr;
666 }
667 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -0700668 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700669 return nullptr;
670 }
ethannicholasd598f792016-07-25 10:08:54 -0700671 if (!expr->fType.canCoerceTo(type)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500672 fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" +
ethannicholasd598f792016-07-25 10:08:54 -0700673 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700674 return nullptr;
675 }
ethannicholasd598f792016-07-25 10:08:54 -0700676 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700677 std::vector<std::unique_ptr<Expression>> args;
678 args.push_back(std::move(expr));
ethannicholasd598f792016-07-25 10:08:54 -0700679 ASTIdentifier id(Position(), type.description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700680 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
681 ASSERT(ctor);
682 return this->call(Position(), std::move(ctor), std::move(args));
683 }
ethannicholas5961bc92016-10-12 06:39:56 -0700684 std::vector<std::unique_ptr<Expression>> args;
685 args.push_back(std::move(expr));
686 return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700687}
688
ethannicholasf789b382016-08-03 12:43:36 -0700689static bool is_matrix_multiply(const Type& left, const Type& right) {
690 if (left.kind() == Type::kMatrix_Kind) {
691 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
692 }
693 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
694}
ethannicholasea4567c2016-10-17 11:24:37 -0700695
ethannicholasb3058bd2016-07-01 08:22:01 -0700696/**
697 * Determines the operand and result types of a binary expression. Returns true if the expression is
698 * legal, false otherwise. If false, the values of the out parameters are undefined.
699 */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500700static bool determine_binary_type(const Context& context,
701 Token::Kind op,
702 const Type& left,
703 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -0700704 const Type** outLeftType,
705 const Type** outRightType,
706 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700707 bool tryFlipped) {
708 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -0700709 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 switch (op) {
ethannicholasea4567c2016-10-17 11:24:37 -0700711 case Token::EQ:
712 *outLeftType = &left;
713 *outRightType = &left;
714 *outResultType = &left;
715 return right.canCoerceTo(left);
ethannicholasb3058bd2016-07-01 08:22:01 -0700716 case Token::EQEQ: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -0700717 case Token::NEQ:
718 isLogical = true;
719 validMatrixOrVectorOp = true;
720 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700721 case Token::LT: // fall through
722 case Token::GT: // fall through
723 case Token::LTEQ: // fall through
724 case Token::GTEQ:
725 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -0700726 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700727 break;
728 case Token::LOGICALOR: // fall through
729 case Token::LOGICALAND: // fall through
730 case Token::LOGICALXOR: // fall through
731 case Token::LOGICALOREQ: // fall through
732 case Token::LOGICALANDEQ: // fall through
733 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -0700734 *outLeftType = context.fBool_Type.get();
735 *outRightType = context.fBool_Type.get();
736 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500737 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -0700738 right.canCoerceTo(*context.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700739 case Token::STAR: // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -0500740 case Token::STAREQ:
ethannicholasf789b382016-08-03 12:43:36 -0700741 if (is_matrix_multiply(left, right)) {
742 // determine final component type
743 if (determine_binary_type(context, Token::STAR, left.componentType(),
744 right.componentType(), outLeftType, outRightType,
745 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500746 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasf789b382016-08-03 12:43:36 -0700747 left.rows());;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500748 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
ethannicholasf789b382016-08-03 12:43:36 -0700749 right.rows());;
750 int leftColumns = left.columns();
751 int leftRows = left.rows();
752 int rightColumns;
753 int rightRows;
754 if (right.kind() == Type::kVector_Kind) {
755 // matrix * vector treats the vector as a column vector, so we need to
756 // transpose it
757 rightColumns = right.rows();
758 rightRows = right.columns();
759 ASSERT(rightColumns == 1);
760 } else {
761 rightColumns = right.columns();
762 rightRows = right.rows();
763 }
764 if (rightColumns > 1) {
765 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
766 leftRows);
767 } else {
768 // result was a column vector, transpose it back to a row
769 *outResultType = &(*outResultType)->toCompound(context, leftRows,
770 rightColumns);
771 }
772 return leftColumns == rightRows;
773 } else {
774 return false;
775 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700776 }
ethannicholasea4567c2016-10-17 11:24:37 -0700777 isLogical = false;
778 validMatrixOrVectorOp = true;
779 break;
780 case Token::PLUS: // fall through
781 case Token::PLUSEQ: // fall through
782 case Token::MINUS: // fall through
783 case Token::MINUSEQ: // fall through
784 case Token::SLASH: // fall through
785 case Token::SLASHEQ:
786 isLogical = false;
787 validMatrixOrVectorOp = true;
788 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700789 default:
790 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -0700791 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700792 }
ethannicholasea4567c2016-10-17 11:24:37 -0700793 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
794 // FIXME: incorrect for shift
Ethan Nicholas11d53972016-11-28 11:23:23 -0500795 if (right.canCoerceTo(left) && (left.kind() == Type::kScalar_Kind ||
ethannicholasea4567c2016-10-17 11:24:37 -0700796 (isVectorOrMatrix && validMatrixOrVectorOp))) {
ethannicholasd598f792016-07-25 10:08:54 -0700797 *outLeftType = &left;
798 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700799 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -0700800 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700801 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700802 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700803 }
804 return true;
805 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500806 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -0700807 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500808 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -0700809 outRightType, outResultType, false)) {
810 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700811 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500812 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -0700813 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700814 }
815 return true;
816 }
817 return false;
818 }
819 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500820 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -0700821 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -0700822 }
823 return false;
824}
825
ethannicholas08a92112016-11-09 13:26:45 -0800826std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
827 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500828 const Expression& right) const {
ethannicholas08a92112016-11-09 13:26:45 -0800829 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
830 // precision to calculate the results and hope the result makes sense. The plan is to move the
831 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
832 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -0500833 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -0800834 right.fKind == Expression::kBoolLiteral_Kind) {
835 bool leftVal = ((BoolLiteral&) left).fValue;
836 bool rightVal = ((BoolLiteral&) right).fValue;
837 bool result;
838 switch (op) {
839 case Token::LOGICALAND: result = leftVal && rightVal; break;
840 case Token::LOGICALOR: result = leftVal || rightVal; break;
841 case Token::LOGICALXOR: result = leftVal ^ rightVal; break;
842 default: return nullptr;
843 }
844 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fPosition, result));
845 }
846 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fPosition, \
847 leftVal op rightVal))
848 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
849 int64_t leftVal = ((IntLiteral&) left).fValue;
850 int64_t rightVal = ((IntLiteral&) right).fValue;
851 switch (op) {
852 case Token::PLUS: return RESULT(Int, +);
853 case Token::MINUS: return RESULT(Int, -);
854 case Token::STAR: return RESULT(Int, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500855 case Token::SLASH:
856 if (rightVal) {
857 return RESULT(Int, /);
858 }
859 fErrors.error(right.fPosition, "division by zero");
860 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -0500861 case Token::PERCENT:
862 if (rightVal) {
863 return RESULT(Int, %);
864 }
865 fErrors.error(right.fPosition, "division by zero");
866 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -0800867 case Token::BITWISEAND: return RESULT(Int, &);
868 case Token::BITWISEOR: return RESULT(Int, |);
869 case Token::BITWISEXOR: return RESULT(Int, ^);
870 case Token::SHL: return RESULT(Int, <<);
871 case Token::SHR: return RESULT(Int, >>);
872 case Token::EQEQ: return RESULT(Bool, ==);
873 case Token::NEQ: return RESULT(Bool, !=);
874 case Token::GT: return RESULT(Bool, >);
875 case Token::GTEQ: return RESULT(Bool, >=);
876 case Token::LT: return RESULT(Bool, <);
877 case Token::LTEQ: return RESULT(Bool, <=);
878 default: return nullptr;
879 }
880 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500881 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -0800882 right.fKind == Expression::kFloatLiteral_Kind) {
883 double leftVal = ((FloatLiteral&) left).fValue;
884 double rightVal = ((FloatLiteral&) right).fValue;
885 switch (op) {
886 case Token::PLUS: return RESULT(Float, +);
887 case Token::MINUS: return RESULT(Float, -);
888 case Token::STAR: return RESULT(Float, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500889 case Token::SLASH:
890 if (rightVal) {
891 return RESULT(Float, /);
892 }
893 fErrors.error(right.fPosition, "division by zero");
894 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -0800895 case Token::EQEQ: return RESULT(Bool, ==);
896 case Token::NEQ: return RESULT(Bool, !=);
897 case Token::GT: return RESULT(Bool, >);
898 case Token::GTEQ: return RESULT(Bool, >=);
899 case Token::LT: return RESULT(Bool, <);
900 case Token::LTEQ: return RESULT(Bool, <=);
901 default: return nullptr;
902 }
903 }
904 #undef RESULT
905 return nullptr;
906}
907
ethannicholasb3058bd2016-07-01 08:22:01 -0700908std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
909 const ASTBinaryExpression& expression) {
910 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
911 if (!left) {
912 return nullptr;
913 }
914 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
915 if (!right) {
916 return nullptr;
917 }
ethannicholasd598f792016-07-25 10:08:54 -0700918 const Type* leftType;
919 const Type* rightType;
920 const Type* resultType;
921 if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500922 &rightType, &resultType,
923 !Token::IsAssignment(expression.fOperator))) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500924 fErrors.error(expression.fPosition, "type mismatch: '" +
925 Token::OperatorName(expression.fOperator) +
926 "' cannot operate on '" + left->fType.fName +
ethannicholasd598f792016-07-25 10:08:54 -0700927 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700928 return nullptr;
929 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500930 if (Token::IsAssignment(expression.fOperator)) {
931 this->markWrittenTo(*left, expression.fOperator != Token::EQ);
ethannicholasea4567c2016-10-17 11:24:37 -0700932 }
933 left = this->coerce(std::move(left), *leftType);
934 right = this->coerce(std::move(right), *rightType);
935 if (!left || !right) {
936 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700937 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500938 std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator,
ethannicholas08a92112016-11-09 13:26:45 -0800939 *right.get());
940 if (!result) {
941 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition,
942 std::move(left),
943 expression.fOperator,
944 std::move(right),
945 *resultType));
946 }
947 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700948}
949
Ethan Nicholas11d53972016-11-28 11:23:23 -0500950std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
ethannicholasb3058bd2016-07-01 08:22:01 -0700951 const ASTTernaryExpression& expression) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500952 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700953 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700954 if (!test) {
955 return nullptr;
956 }
957 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
958 if (!ifTrue) {
959 return nullptr;
960 }
961 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
962 if (!ifFalse) {
963 return nullptr;
964 }
ethannicholasd598f792016-07-25 10:08:54 -0700965 const Type* trueType;
966 const Type* falseType;
967 const Type* resultType;
968 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500969 &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500970 fErrors.error(expression.fPosition, "ternary operator result mismatch: '" +
971 ifTrue->fType.fName + "', '" +
ethannicholasd598f792016-07-25 10:08:54 -0700972 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700973 return nullptr;
974 }
ethannicholasd598f792016-07-25 10:08:54 -0700975 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500976 if (!ifTrue) {
977 return nullptr;
978 }
ethannicholasd598f792016-07-25 10:08:54 -0700979 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500980 if (!ifFalse) {
981 return nullptr;
982 }
ethannicholas08a92112016-11-09 13:26:45 -0800983 if (test->fKind == Expression::kBoolLiteral_Kind) {
984 // static boolean test, just return one of the branches
985 if (((BoolLiteral&) *test).fValue) {
986 return ifTrue;
987 } else {
988 return ifFalse;
989 }
990 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500991 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700992 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -0500993 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -0700994 std::move(ifFalse)));
995}
996
Ethan Nicholas11d53972016-11-28 11:23:23 -0500997std::unique_ptr<Expression> IRGenerator::call(Position position,
998 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -0700999 std::vector<std::unique_ptr<Expression>> arguments) {
1000 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001001 SkString msg = "call to '" + function.fName + "' expected " +
1002 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001003 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001004 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001005 msg += "s";
1006 }
ethannicholas5961bc92016-10-12 06:39:56 -07001007 msg += ", but found " + to_string((uint64_t) arguments.size());
ethannicholasb3058bd2016-07-01 08:22:01 -07001008 fErrors.error(position, msg);
1009 return nullptr;
1010 }
ethannicholas471e8942016-10-28 09:02:46 -07001011 std::vector<const Type*> types;
1012 const Type* returnType;
1013 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001014 SkString msg = "no match for " + function.fName + "(";
1015 SkString separator;
ethannicholas471e8942016-10-28 09:02:46 -07001016 for (size_t i = 0; i < arguments.size(); i++) {
1017 msg += separator;
1018 separator = ", ";
1019 msg += arguments[i]->fType.description();
1020 }
1021 msg += ")";
1022 fErrors.error(position, msg);
1023 return nullptr;
1024 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001025 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001026 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001027 if (!arguments[i]) {
1028 return nullptr;
1029 }
ethannicholasd598f792016-07-25 10:08:54 -07001030 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -05001031 this->markWrittenTo(*arguments[i], true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001032 }
1033 }
ethannicholas471e8942016-10-28 09:02:46 -07001034 return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001035 std::move(arguments)));
1036}
1037
1038/**
Ethan Nicholas11d53972016-11-28 11:23:23 -05001039 * Determines the cost of coercing the arguments of a function to the required types. Returns true
1040 * if the cost could be computed, false if the call is not valid. Cost has no particular meaning
ethannicholasb3058bd2016-07-01 08:22:01 -07001041 * other than "lower costs are preferred".
1042 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001043bool IRGenerator::determineCallCost(const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001044 const std::vector<std::unique_ptr<Expression>>& arguments,
1045 int* outCost) {
ethannicholasd598f792016-07-25 10:08:54 -07001046 if (function.fParameters.size() != arguments.size()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001047 return false;
1048 }
1049 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001050 std::vector<const Type*> types;
1051 const Type* ignored;
1052 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
1053 return false;
1054 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001055 for (size_t i = 0; i < arguments.size(); i++) {
1056 int cost;
ethannicholas471e8942016-10-28 09:02:46 -07001057 if (arguments[i]->fType.determineCoercionCost(*types[i], &cost)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001058 total += cost;
1059 } else {
1060 return false;
1061 }
1062 }
1063 *outCost = total;
1064 return true;
1065}
1066
Ethan Nicholas11d53972016-11-28 11:23:23 -05001067std::unique_ptr<Expression> IRGenerator::call(Position position,
1068 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001069 std::vector<std::unique_ptr<Expression>> arguments) {
1070 if (functionValue->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001071 return this->convertConstructor(position,
1072 ((TypeReference&) *functionValue).fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001073 std::move(arguments));
1074 }
1075 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
1076 fErrors.error(position, "'" + functionValue->description() + "' is not a function");
1077 return nullptr;
1078 }
1079 FunctionReference* ref = (FunctionReference*) functionValue.get();
1080 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -07001081 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001082 if (ref->fFunctions.size() > 1) {
1083 for (const auto& f : ref->fFunctions) {
1084 int cost;
ethannicholasd598f792016-07-25 10:08:54 -07001085 if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001086 bestCost = cost;
1087 best = f;
1088 }
1089 }
1090 if (best) {
ethannicholasd598f792016-07-25 10:08:54 -07001091 return this->call(position, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001092 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001093 SkString msg = "no match for " + ref->fFunctions[0]->fName + "(";
1094 SkString separator;
ethannicholasb3058bd2016-07-01 08:22:01 -07001095 for (size_t i = 0; i < arguments.size(); i++) {
1096 msg += separator;
1097 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -07001098 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -07001099 }
1100 msg += ")";
1101 fErrors.error(position, msg);
1102 return nullptr;
1103 }
ethannicholasd598f792016-07-25 10:08:54 -07001104 return this->call(position, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001105}
1106
1107std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas11d53972016-11-28 11:23:23 -05001108 Position position,
1109 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001110 std::vector<std::unique_ptr<Expression>> args) {
1111 // FIXME: add support for structs and arrays
ethannicholasd598f792016-07-25 10:08:54 -07001112 Type::Kind kind = type.kind();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001113 if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -07001114 kind != Type::kArray_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001115 fErrors.error(position, "cannot construct '" + type.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001116 return nullptr;
1117 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001118 if (type == *fContext.fFloat_Type && args.size() == 1 &&
ethannicholasb3058bd2016-07-01 08:22:01 -07001119 args[0]->fKind == Expression::kIntLiteral_Kind) {
1120 int64_t value = ((IntLiteral&) *args[0]).fValue;
ethannicholasd598f792016-07-25 10:08:54 -07001121 return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001122 }
1123 if (args.size() == 1 && args[0]->fType == type) {
1124 // argument is already the right type, just return it
1125 return std::move(args[0]);
1126 }
ethannicholasd598f792016-07-25 10:08:54 -07001127 if (type.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001128 if (args.size() != 1) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001129 fErrors.error(position, "invalid arguments to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001130 "' constructor, (expected exactly 1 argument, but found " +
ethannicholas5961bc92016-10-12 06:39:56 -07001131 to_string((uint64_t) args.size()) + ")");
ethannicholasea4567c2016-10-17 11:24:37 -07001132 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001133 }
ethannicholasd598f792016-07-25 10:08:54 -07001134 if (args[0]->fType == *fContext.fBool_Type) {
1135 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0));
1136 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1));
ethannicholasb3058bd2016-07-01 08:22:01 -07001137 return std::unique_ptr<Expression>(
1138 new TernaryExpression(position, std::move(args[0]),
1139 this->coerce(std::move(one), type),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001140 this->coerce(std::move(zero),
ethannicholasb3058bd2016-07-01 08:22:01 -07001141 type)));
ethannicholasd598f792016-07-25 10:08:54 -07001142 } else if (!args[0]->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001143 fErrors.error(position, "invalid argument to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001144 "' constructor (expected a number or bool, but found '" +
ethannicholasd598f792016-07-25 10:08:54 -07001145 args[0]->fType.description() + "')");
ethannicholasb3058bd2016-07-01 08:22:01 -07001146 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001147 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
ethannicholas5961bc92016-10-12 06:39:56 -07001148 type == *fContext.fUInt_Type)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001149 return std::unique_ptr<Expression>(new IntLiteral(fContext,
1150 position,
ethannicholas5961bc92016-10-12 06:39:56 -07001151 ((IntLiteral&) *args[0]).fValue,
1152 &type));
1153 }
1154 } else if (kind == Type::kArray_Kind) {
1155 const Type& base = type.componentType();
1156 for (size_t i = 0; i < args.size(); i++) {
1157 args[i] = this->coerce(std::move(args[i]), base);
ethannicholasea4567c2016-10-17 11:24:37 -07001158 if (!args[i]) {
1159 return nullptr;
1160 }
ethannicholas5961bc92016-10-12 06:39:56 -07001161 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001162 } else {
1163 ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind);
1164 int actual = 0;
1165 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas4578a8e2016-11-01 11:57:42 -04001166 if (args[i]->fType.kind() == Type::kVector_Kind ||
ethannicholasd598f792016-07-25 10:08:54 -07001167 args[i]->fType.kind() == Type::kMatrix_Kind) {
Ethan Nicholas4578a8e2016-11-01 11:57:42 -04001168 if (type.componentType().isNumber() !=
1169 args[i]->fType.componentType().isNumber()) {
1170 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid "
1171 "parameter to '" + type.description() +
1172 "' constructor");
ethannicholas7effa7a2016-10-14 09:56:33 -07001173 return nullptr;
1174 }
ethannicholasd598f792016-07-25 10:08:54 -07001175 actual += args[i]->fType.rows() * args[i]->fType.columns();
1176 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001177 actual += 1;
ethannicholasd598f792016-07-25 10:08:54 -07001178 if (type.kind() != Type::kScalar_Kind) {
1179 args[i] = this->coerce(std::move(args[i]), type.componentType());
ethannicholas7effa7a2016-10-14 09:56:33 -07001180 if (!args[i]) {
1181 return nullptr;
1182 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001183 }
1184 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001185 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid "
1186 "parameter to '" + type.description() + "' constructor");
ethannicholasb3058bd2016-07-01 08:22:01 -07001187 return nullptr;
1188 }
1189 }
ethannicholasd598f792016-07-25 10:08:54 -07001190 int min = type.rows() * type.columns();
1191 int max = type.columns() > 1 ? INT_MAX : min;
ethannicholasb3058bd2016-07-01 08:22:01 -07001192 if ((actual < min || actual > max) &&
1193 !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001194 fErrors.error(position, "invalid arguments to '" + type.description() +
1195 "' constructor (expected " + to_string(min) + " scalar" +
1196 (min == 1 ? "" : "s") + ", but found " + to_string(actual) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001197 ")");
1198 return nullptr;
1199 }
1200 }
1201 return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args)));
1202}
1203
1204std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1205 const ASTPrefixExpression& expression) {
1206 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1207 if (!base) {
1208 return nullptr;
1209 }
1210 switch (expression.fOperator) {
1211 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001212 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001213 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001214 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001215 return nullptr;
1216 }
1217 return base;
1218 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001219 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001220 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001221 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001222 return nullptr;
1223 }
1224 if (base->fKind == Expression::kIntLiteral_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001225 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001226 -((IntLiteral&) *base).fValue));
1227 }
1228 if (base->fKind == Expression::kFloatLiteral_Kind) {
1229 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001230 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001231 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001232 }
1233 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1234 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001235 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001236 fErrors.error(expression.fPosition,
1237 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001238 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001239 return nullptr;
1240 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001241 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001242 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001243 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001244 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001245 fErrors.error(expression.fPosition,
1246 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001247 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001248 return nullptr;
1249 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001250 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001251 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001252 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001253 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001254 fErrors.error(expression.fPosition,
1255 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001256 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001257 return nullptr;
1258 }
ethannicholas08a92112016-11-09 13:26:45 -08001259 if (base->fKind == Expression::kBoolLiteral_Kind) {
1260 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fPosition,
1261 !((BoolLiteral&) *base).fValue));
1262 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001263 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001264 case Token::BITWISENOT:
1265 if (base->fType != *fContext.fInt_Type) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001266 fErrors.error(expression.fPosition,
1267 "'" + Token::OperatorName(expression.fOperator) +
ethannicholas5961bc92016-10-12 06:39:56 -07001268 "' cannot operate on '" + base->fType.description() + "'");
1269 return nullptr;
1270 }
1271 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001272 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07001273 ABORT("unsupported prefix operator\n");
1274 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001275 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
ethannicholasb3058bd2016-07-01 08:22:01 -07001276 std::move(base)));
1277}
1278
1279std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1280 const ASTExpression& index) {
ethannicholas5961bc92016-10-12 06:39:56 -07001281 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1282 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001283 fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001284 "'");
1285 return nullptr;
1286 }
1287 std::unique_ptr<Expression> converted = this->convertExpression(index);
1288 if (!converted) {
1289 return nullptr;
1290 }
ethannicholas5961bc92016-10-12 06:39:56 -07001291 if (converted->fType != *fContext.fUInt_Type) {
1292 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1293 if (!converted) {
1294 return nullptr;
1295 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001296 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001297 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07001298 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001299}
1300
1301std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001302 const SkString& field) {
ethannicholasd598f792016-07-25 10:08:54 -07001303 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001304 for (size_t i = 0; i < fields.size(); i++) {
1305 if (fields[i].fName == field) {
1306 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1307 }
1308 }
ethannicholasd598f792016-07-25 10:08:54 -07001309 fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a "
ethannicholasb3058bd2016-07-01 08:22:01 -07001310 "field named '" + field + "");
1311 return nullptr;
1312}
1313
1314std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001315 const SkString& fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001316 if (base->fType.kind() != Type::kVector_Kind) {
1317 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001318 return nullptr;
1319 }
1320 std::vector<int> swizzleComponents;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001321 for (size_t i = 0; i < fields.size(); i++) {
1322 switch (fields[i]) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001323 case 'x': // fall through
1324 case 'r': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001325 case 's':
ethannicholasb3058bd2016-07-01 08:22:01 -07001326 swizzleComponents.push_back(0);
1327 break;
1328 case 'y': // fall through
1329 case 'g': // fall through
1330 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001331 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001332 swizzleComponents.push_back(1);
1333 break;
1334 }
1335 // fall through
1336 case 'z': // fall through
1337 case 'b': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001338 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001339 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001340 swizzleComponents.push_back(2);
1341 break;
1342 }
1343 // fall through
1344 case 'w': // fall through
1345 case 'a': // fall through
1346 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001347 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001348 swizzleComponents.push_back(3);
1349 break;
1350 }
1351 // fall through
1352 default:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001353 fErrors.error(base->fPosition, SkStringPrintf("invalid swizzle component '%c'",
1354 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07001355 return nullptr;
1356 }
1357 }
1358 ASSERT(swizzleComponents.size() > 0);
1359 if (swizzleComponents.size() > 4) {
1360 fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'");
1361 return nullptr;
1362 }
ethannicholasd598f792016-07-25 10:08:54 -07001363 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001364}
1365
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001366std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001367 auto found = fCapsMap.find(name);
1368 if (found == fCapsMap.end()) {
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001369 fErrors.error(position, "unknown capability flag '" + name + "'");
1370 return nullptr;
1371 }
1372 switch (found->second.fKind) {
1373 case CapValue::kBool_Kind:
1374 return std::unique_ptr<Expression>(new BoolLiteral(fContext, position,
1375 (bool) found->second.fValue));
1376 case CapValue::kInt_Kind:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001377 return std::unique_ptr<Expression>(new IntLiteral(fContext, position,
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001378 found->second.fValue));
1379 }
1380 ASSERT(false);
1381 return nullptr;
1382}
1383
ethannicholasb3058bd2016-07-01 08:22:01 -07001384std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
1385 const ASTSuffixExpression& expression) {
1386 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
1387 if (!base) {
1388 return nullptr;
1389 }
1390 switch (expression.fSuffix->fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07001391 case ASTSuffix::kIndex_Kind: {
1392 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
1393 if (expr) {
1394 return this->convertIndex(std::move(base), *expr);
1395 } else if (base->fKind == Expression::kTypeReference_Kind) {
1396 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001397 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
ethannicholas5961bc92016-10-12 06:39:56 -07001398 -1);
1399 fSymbolTable->takeOwnership(newType);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001400 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition,
ethannicholas5961bc92016-10-12 06:39:56 -07001401 *newType));
1402 } else {
1403 fErrors.error(expression.fPosition, "'[]' must follow a type name");
ethannicholasa54401d2016-10-14 08:37:32 -07001404 return nullptr;
ethannicholas5961bc92016-10-12 06:39:56 -07001405 }
1406 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001407 case ASTSuffix::kCall_Kind: {
1408 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
1409 std::vector<std::unique_ptr<Expression>> arguments;
1410 for (size_t i = 0; i < rawArguments->size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001411 std::unique_ptr<Expression> converted =
ethannicholasb3058bd2016-07-01 08:22:01 -07001412 this->convertExpression(*(*rawArguments)[i]);
1413 if (!converted) {
1414 return nullptr;
1415 }
1416 arguments.push_back(std::move(converted));
1417 }
1418 return this->call(expression.fPosition, std::move(base), std::move(arguments));
1419 }
1420 case ASTSuffix::kField_Kind: {
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001421 if (base->fType == *fContext.fSkCaps_Type) {
1422 return this->getCap(expression.fPosition,
1423 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1424 }
ethannicholasd598f792016-07-25 10:08:54 -07001425 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001426 case Type::kVector_Kind:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001427 return this->convertSwizzle(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001428 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1429 case Type::kStruct_Kind:
1430 return this->convertField(std::move(base),
1431 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1432 default:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001433 fErrors.error(base->fPosition, "cannot swizzle value of type '" +
ethannicholasd598f792016-07-25 10:08:54 -07001434 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001435 return nullptr;
1436 }
1437 }
1438 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001439 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001440 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001441 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001442 return nullptr;
1443 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001444 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001445 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001446 Token::PLUSPLUS));
1447 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001448 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001449 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001450 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001451 return nullptr;
1452 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001453 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001454 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001455 Token::MINUSMINUS));
1456 default:
1457 ABORT("unsupported suffix operator");
1458 }
1459}
1460
1461void IRGenerator::checkValid(const Expression& expr) {
1462 switch (expr.fKind) {
1463 case Expression::kFunctionReference_Kind:
1464 fErrors.error(expr.fPosition, "expected '(' to begin function call");
1465 break;
1466 case Expression::kTypeReference_Kind:
1467 fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation");
1468 break;
1469 default:
ethannicholasea4567c2016-10-17 11:24:37 -07001470 if (expr.fType == *fContext.fInvalid_Type) {
1471 fErrors.error(expr.fPosition, "invalid expression");
1472 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001473 }
1474}
1475
ethannicholasb3058bd2016-07-01 08:22:01 -07001476static bool has_duplicates(const Swizzle& swizzle) {
1477 int bits = 0;
1478 for (int idx : swizzle.fComponents) {
1479 ASSERT(idx >= 0 && idx <= 3);
1480 int bit = 1 << idx;
1481 if (bits & bit) {
1482 return true;
1483 }
1484 bits |= bit;
1485 }
1486 return false;
1487}
1488
Ethan Nicholas86a43402017-01-19 13:32:00 -05001489void IRGenerator::markWrittenTo(const Expression& expr, bool readWrite) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001490 switch (expr.fKind) {
1491 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001492 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07001493 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001494 fErrors.error(expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001495 "cannot modify immutable variable '" + var.fName + "'");
1496 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001497 ((VariableReference&) expr).setRefKind(readWrite ? VariableReference::kReadWrite_RefKind
1498 : VariableReference::kWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001499 break;
1500 }
1501 case Expression::kFieldAccess_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05001502 this->markWrittenTo(*((FieldAccess&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001503 break;
1504 case Expression::kSwizzle_Kind:
1505 if (has_duplicates((Swizzle&) expr)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001506 fErrors.error(expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001507 "cannot write to the same swizzle field more than once");
1508 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001509 this->markWrittenTo(*((Swizzle&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001510 break;
1511 case Expression::kIndex_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05001512 this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001513 break;
1514 default:
1515 fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'");
1516 break;
1517 }
1518}
1519
1520}