blob: ebb4c52a3484e9b282d75ffd6a78231f82fb9ff3 [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 Nicholasde4d3012017-01-19 16:58:02 -0500627 const Variable* var = (const Variable*) result;
628 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) {
629 fInputs.fFlipY = true;
630 if (fSettings->fFlipY &&
631 (!fSettings->fCaps ||
632 !fSettings->fCaps->fragCoordConventionsExtensionString())) {
633 fInputs.fRTHeight = true;
634 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500635 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500636 // default to kRead_RefKind; this will be corrected later if the variable is written to
637 return std::unique_ptr<VariableReference>(new VariableReference(
638 identifier.fPosition,
639 *var,
640 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -0700641 }
642 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700643 const Field* field = (const Field*) result;
Ethan Nicholas86a43402017-01-19 13:32:00 -0500644 VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner,
645 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -0700646 return std::unique_ptr<Expression>(new FieldAccess(
647 std::unique_ptr<Expression>(base),
648 field->fFieldIndex,
649 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 }
651 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700652 const Type* t = (const Type*) result;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500653 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -0700654 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -0700655 }
656 default:
657 ABORT("unsupported symbol type %d\n", result->fKind);
658 }
659
660}
661
Ethan Nicholas11d53972016-11-28 11:23:23 -0500662std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700663 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700664 if (!expr) {
665 return nullptr;
666 }
ethannicholasd598f792016-07-25 10:08:54 -0700667 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700668 return expr;
669 }
670 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -0700671 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700672 return nullptr;
673 }
ethannicholasd598f792016-07-25 10:08:54 -0700674 if (!expr->fType.canCoerceTo(type)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500675 fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" +
ethannicholasd598f792016-07-25 10:08:54 -0700676 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700677 return nullptr;
678 }
ethannicholasd598f792016-07-25 10:08:54 -0700679 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700680 std::vector<std::unique_ptr<Expression>> args;
681 args.push_back(std::move(expr));
ethannicholasd598f792016-07-25 10:08:54 -0700682 ASTIdentifier id(Position(), type.description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700683 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
684 ASSERT(ctor);
685 return this->call(Position(), std::move(ctor), std::move(args));
686 }
ethannicholas5961bc92016-10-12 06:39:56 -0700687 std::vector<std::unique_ptr<Expression>> args;
688 args.push_back(std::move(expr));
689 return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700690}
691
ethannicholasf789b382016-08-03 12:43:36 -0700692static bool is_matrix_multiply(const Type& left, const Type& right) {
693 if (left.kind() == Type::kMatrix_Kind) {
694 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
695 }
696 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
697}
ethannicholasea4567c2016-10-17 11:24:37 -0700698
ethannicholasb3058bd2016-07-01 08:22:01 -0700699/**
700 * Determines the operand and result types of a binary expression. Returns true if the expression is
701 * legal, false otherwise. If false, the values of the out parameters are undefined.
702 */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500703static bool determine_binary_type(const Context& context,
704 Token::Kind op,
705 const Type& left,
706 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -0700707 const Type** outLeftType,
708 const Type** outRightType,
709 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 bool tryFlipped) {
711 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -0700712 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -0700713 switch (op) {
ethannicholasea4567c2016-10-17 11:24:37 -0700714 case Token::EQ:
715 *outLeftType = &left;
716 *outRightType = &left;
717 *outResultType = &left;
718 return right.canCoerceTo(left);
ethannicholasb3058bd2016-07-01 08:22:01 -0700719 case Token::EQEQ: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -0700720 case Token::NEQ:
721 isLogical = true;
722 validMatrixOrVectorOp = true;
723 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700724 case Token::LT: // fall through
725 case Token::GT: // fall through
726 case Token::LTEQ: // fall through
727 case Token::GTEQ:
728 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -0700729 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700730 break;
731 case Token::LOGICALOR: // fall through
732 case Token::LOGICALAND: // fall through
733 case Token::LOGICALXOR: // fall through
734 case Token::LOGICALOREQ: // fall through
735 case Token::LOGICALANDEQ: // fall through
736 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -0700737 *outLeftType = context.fBool_Type.get();
738 *outRightType = context.fBool_Type.get();
739 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500740 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -0700741 right.canCoerceTo(*context.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700742 case Token::STAR: // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -0500743 case Token::STAREQ:
ethannicholasf789b382016-08-03 12:43:36 -0700744 if (is_matrix_multiply(left, right)) {
745 // determine final component type
746 if (determine_binary_type(context, Token::STAR, left.componentType(),
747 right.componentType(), outLeftType, outRightType,
748 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500749 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasf789b382016-08-03 12:43:36 -0700750 left.rows());;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500751 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
ethannicholasf789b382016-08-03 12:43:36 -0700752 right.rows());;
753 int leftColumns = left.columns();
754 int leftRows = left.rows();
755 int rightColumns;
756 int rightRows;
757 if (right.kind() == Type::kVector_Kind) {
758 // matrix * vector treats the vector as a column vector, so we need to
759 // transpose it
760 rightColumns = right.rows();
761 rightRows = right.columns();
762 ASSERT(rightColumns == 1);
763 } else {
764 rightColumns = right.columns();
765 rightRows = right.rows();
766 }
767 if (rightColumns > 1) {
768 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
769 leftRows);
770 } else {
771 // result was a column vector, transpose it back to a row
772 *outResultType = &(*outResultType)->toCompound(context, leftRows,
773 rightColumns);
774 }
775 return leftColumns == rightRows;
776 } else {
777 return false;
778 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700779 }
ethannicholasea4567c2016-10-17 11:24:37 -0700780 isLogical = false;
781 validMatrixOrVectorOp = true;
782 break;
783 case Token::PLUS: // fall through
784 case Token::PLUSEQ: // fall through
785 case Token::MINUS: // fall through
786 case Token::MINUSEQ: // fall through
787 case Token::SLASH: // fall through
788 case Token::SLASHEQ:
789 isLogical = false;
790 validMatrixOrVectorOp = true;
791 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700792 default:
793 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -0700794 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700795 }
ethannicholasea4567c2016-10-17 11:24:37 -0700796 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
797 // FIXME: incorrect for shift
Ethan Nicholas11d53972016-11-28 11:23:23 -0500798 if (right.canCoerceTo(left) && (left.kind() == Type::kScalar_Kind ||
ethannicholasea4567c2016-10-17 11:24:37 -0700799 (isVectorOrMatrix && validMatrixOrVectorOp))) {
ethannicholasd598f792016-07-25 10:08:54 -0700800 *outLeftType = &left;
801 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700802 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -0700803 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700804 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700805 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700806 }
807 return true;
808 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500809 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -0700810 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500811 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -0700812 outRightType, outResultType, false)) {
813 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700814 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500815 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -0700816 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700817 }
818 return true;
819 }
820 return false;
821 }
822 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500823 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -0700824 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -0700825 }
826 return false;
827}
828
ethannicholas08a92112016-11-09 13:26:45 -0800829std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
830 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500831 const Expression& right) const {
ethannicholas08a92112016-11-09 13:26:45 -0800832 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
833 // precision to calculate the results and hope the result makes sense. The plan is to move the
834 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
835 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -0500836 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -0800837 right.fKind == Expression::kBoolLiteral_Kind) {
838 bool leftVal = ((BoolLiteral&) left).fValue;
839 bool rightVal = ((BoolLiteral&) right).fValue;
840 bool result;
841 switch (op) {
842 case Token::LOGICALAND: result = leftVal && rightVal; break;
843 case Token::LOGICALOR: result = leftVal || rightVal; break;
844 case Token::LOGICALXOR: result = leftVal ^ rightVal; break;
845 default: return nullptr;
846 }
847 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fPosition, result));
848 }
849 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fPosition, \
850 leftVal op rightVal))
851 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
852 int64_t leftVal = ((IntLiteral&) left).fValue;
853 int64_t rightVal = ((IntLiteral&) right).fValue;
854 switch (op) {
855 case Token::PLUS: return RESULT(Int, +);
856 case Token::MINUS: return RESULT(Int, -);
857 case Token::STAR: return RESULT(Int, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500858 case Token::SLASH:
859 if (rightVal) {
860 return RESULT(Int, /);
861 }
862 fErrors.error(right.fPosition, "division by zero");
863 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -0500864 case Token::PERCENT:
865 if (rightVal) {
866 return RESULT(Int, %);
867 }
868 fErrors.error(right.fPosition, "division by zero");
869 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -0800870 case Token::BITWISEAND: return RESULT(Int, &);
871 case Token::BITWISEOR: return RESULT(Int, |);
872 case Token::BITWISEXOR: return RESULT(Int, ^);
873 case Token::SHL: return RESULT(Int, <<);
874 case Token::SHR: return RESULT(Int, >>);
875 case Token::EQEQ: return RESULT(Bool, ==);
876 case Token::NEQ: return RESULT(Bool, !=);
877 case Token::GT: return RESULT(Bool, >);
878 case Token::GTEQ: return RESULT(Bool, >=);
879 case Token::LT: return RESULT(Bool, <);
880 case Token::LTEQ: return RESULT(Bool, <=);
881 default: return nullptr;
882 }
883 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500884 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -0800885 right.fKind == Expression::kFloatLiteral_Kind) {
886 double leftVal = ((FloatLiteral&) left).fValue;
887 double rightVal = ((FloatLiteral&) right).fValue;
888 switch (op) {
889 case Token::PLUS: return RESULT(Float, +);
890 case Token::MINUS: return RESULT(Float, -);
891 case Token::STAR: return RESULT(Float, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500892 case Token::SLASH:
893 if (rightVal) {
894 return RESULT(Float, /);
895 }
896 fErrors.error(right.fPosition, "division by zero");
897 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -0800898 case Token::EQEQ: return RESULT(Bool, ==);
899 case Token::NEQ: return RESULT(Bool, !=);
900 case Token::GT: return RESULT(Bool, >);
901 case Token::GTEQ: return RESULT(Bool, >=);
902 case Token::LT: return RESULT(Bool, <);
903 case Token::LTEQ: return RESULT(Bool, <=);
904 default: return nullptr;
905 }
906 }
907 #undef RESULT
908 return nullptr;
909}
910
ethannicholasb3058bd2016-07-01 08:22:01 -0700911std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
912 const ASTBinaryExpression& expression) {
913 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
914 if (!left) {
915 return nullptr;
916 }
917 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
918 if (!right) {
919 return nullptr;
920 }
ethannicholasd598f792016-07-25 10:08:54 -0700921 const Type* leftType;
922 const Type* rightType;
923 const Type* resultType;
924 if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500925 &rightType, &resultType,
926 !Token::IsAssignment(expression.fOperator))) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500927 fErrors.error(expression.fPosition, "type mismatch: '" +
928 Token::OperatorName(expression.fOperator) +
929 "' cannot operate on '" + left->fType.fName +
ethannicholasd598f792016-07-25 10:08:54 -0700930 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700931 return nullptr;
932 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500933 if (Token::IsAssignment(expression.fOperator)) {
934 this->markWrittenTo(*left, expression.fOperator != Token::EQ);
ethannicholasea4567c2016-10-17 11:24:37 -0700935 }
936 left = this->coerce(std::move(left), *leftType);
937 right = this->coerce(std::move(right), *rightType);
938 if (!left || !right) {
939 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700940 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500941 std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator,
ethannicholas08a92112016-11-09 13:26:45 -0800942 *right.get());
943 if (!result) {
944 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition,
945 std::move(left),
946 expression.fOperator,
947 std::move(right),
948 *resultType));
949 }
950 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700951}
952
Ethan Nicholas11d53972016-11-28 11:23:23 -0500953std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
ethannicholasb3058bd2016-07-01 08:22:01 -0700954 const ASTTernaryExpression& expression) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500955 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700956 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700957 if (!test) {
958 return nullptr;
959 }
960 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
961 if (!ifTrue) {
962 return nullptr;
963 }
964 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
965 if (!ifFalse) {
966 return nullptr;
967 }
ethannicholasd598f792016-07-25 10:08:54 -0700968 const Type* trueType;
969 const Type* falseType;
970 const Type* resultType;
971 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500972 &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500973 fErrors.error(expression.fPosition, "ternary operator result mismatch: '" +
974 ifTrue->fType.fName + "', '" +
ethannicholasd598f792016-07-25 10:08:54 -0700975 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700976 return nullptr;
977 }
ethannicholasd598f792016-07-25 10:08:54 -0700978 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500979 if (!ifTrue) {
980 return nullptr;
981 }
ethannicholasd598f792016-07-25 10:08:54 -0700982 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500983 if (!ifFalse) {
984 return nullptr;
985 }
ethannicholas08a92112016-11-09 13:26:45 -0800986 if (test->fKind == Expression::kBoolLiteral_Kind) {
987 // static boolean test, just return one of the branches
988 if (((BoolLiteral&) *test).fValue) {
989 return ifTrue;
990 } else {
991 return ifFalse;
992 }
993 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500994 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700995 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -0500996 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -0700997 std::move(ifFalse)));
998}
999
Ethan Nicholas11d53972016-11-28 11:23:23 -05001000std::unique_ptr<Expression> IRGenerator::call(Position position,
1001 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -07001002 std::vector<std::unique_ptr<Expression>> arguments) {
1003 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001004 SkString msg = "call to '" + function.fName + "' expected " +
1005 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001006 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001007 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001008 msg += "s";
1009 }
ethannicholas5961bc92016-10-12 06:39:56 -07001010 msg += ", but found " + to_string((uint64_t) arguments.size());
ethannicholasb3058bd2016-07-01 08:22:01 -07001011 fErrors.error(position, msg);
1012 return nullptr;
1013 }
ethannicholas471e8942016-10-28 09:02:46 -07001014 std::vector<const Type*> types;
1015 const Type* returnType;
1016 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001017 SkString msg = "no match for " + function.fName + "(";
1018 SkString separator;
ethannicholas471e8942016-10-28 09:02:46 -07001019 for (size_t i = 0; i < arguments.size(); i++) {
1020 msg += separator;
1021 separator = ", ";
1022 msg += arguments[i]->fType.description();
1023 }
1024 msg += ")";
1025 fErrors.error(position, msg);
1026 return nullptr;
1027 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001028 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001029 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001030 if (!arguments[i]) {
1031 return nullptr;
1032 }
ethannicholasd598f792016-07-25 10:08:54 -07001033 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -05001034 this->markWrittenTo(*arguments[i], true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001035 }
1036 }
ethannicholas471e8942016-10-28 09:02:46 -07001037 return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001038 std::move(arguments)));
1039}
1040
1041/**
Ethan Nicholas11d53972016-11-28 11:23:23 -05001042 * Determines the cost of coercing the arguments of a function to the required types. Returns true
1043 * if the cost could be computed, false if the call is not valid. Cost has no particular meaning
ethannicholasb3058bd2016-07-01 08:22:01 -07001044 * other than "lower costs are preferred".
1045 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001046bool IRGenerator::determineCallCost(const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001047 const std::vector<std::unique_ptr<Expression>>& arguments,
1048 int* outCost) {
ethannicholasd598f792016-07-25 10:08:54 -07001049 if (function.fParameters.size() != arguments.size()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001050 return false;
1051 }
1052 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001053 std::vector<const Type*> types;
1054 const Type* ignored;
1055 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
1056 return false;
1057 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001058 for (size_t i = 0; i < arguments.size(); i++) {
1059 int cost;
ethannicholas471e8942016-10-28 09:02:46 -07001060 if (arguments[i]->fType.determineCoercionCost(*types[i], &cost)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001061 total += cost;
1062 } else {
1063 return false;
1064 }
1065 }
1066 *outCost = total;
1067 return true;
1068}
1069
Ethan Nicholas11d53972016-11-28 11:23:23 -05001070std::unique_ptr<Expression> IRGenerator::call(Position position,
1071 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001072 std::vector<std::unique_ptr<Expression>> arguments) {
1073 if (functionValue->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001074 return this->convertConstructor(position,
1075 ((TypeReference&) *functionValue).fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001076 std::move(arguments));
1077 }
1078 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
1079 fErrors.error(position, "'" + functionValue->description() + "' is not a function");
1080 return nullptr;
1081 }
1082 FunctionReference* ref = (FunctionReference*) functionValue.get();
1083 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -07001084 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001085 if (ref->fFunctions.size() > 1) {
1086 for (const auto& f : ref->fFunctions) {
1087 int cost;
ethannicholasd598f792016-07-25 10:08:54 -07001088 if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001089 bestCost = cost;
1090 best = f;
1091 }
1092 }
1093 if (best) {
ethannicholasd598f792016-07-25 10:08:54 -07001094 return this->call(position, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001095 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001096 SkString msg = "no match for " + ref->fFunctions[0]->fName + "(";
1097 SkString separator;
ethannicholasb3058bd2016-07-01 08:22:01 -07001098 for (size_t i = 0; i < arguments.size(); i++) {
1099 msg += separator;
1100 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -07001101 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -07001102 }
1103 msg += ")";
1104 fErrors.error(position, msg);
1105 return nullptr;
1106 }
ethannicholasd598f792016-07-25 10:08:54 -07001107 return this->call(position, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001108}
1109
1110std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas11d53972016-11-28 11:23:23 -05001111 Position position,
1112 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001113 std::vector<std::unique_ptr<Expression>> args) {
1114 // FIXME: add support for structs and arrays
ethannicholasd598f792016-07-25 10:08:54 -07001115 Type::Kind kind = type.kind();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001116 if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -07001117 kind != Type::kArray_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001118 fErrors.error(position, "cannot construct '" + type.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001119 return nullptr;
1120 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001121 if (type == *fContext.fFloat_Type && args.size() == 1 &&
ethannicholasb3058bd2016-07-01 08:22:01 -07001122 args[0]->fKind == Expression::kIntLiteral_Kind) {
1123 int64_t value = ((IntLiteral&) *args[0]).fValue;
ethannicholasd598f792016-07-25 10:08:54 -07001124 return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001125 }
1126 if (args.size() == 1 && args[0]->fType == type) {
1127 // argument is already the right type, just return it
1128 return std::move(args[0]);
1129 }
ethannicholasd598f792016-07-25 10:08:54 -07001130 if (type.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001131 if (args.size() != 1) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001132 fErrors.error(position, "invalid arguments to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001133 "' constructor, (expected exactly 1 argument, but found " +
ethannicholas5961bc92016-10-12 06:39:56 -07001134 to_string((uint64_t) args.size()) + ")");
ethannicholasea4567c2016-10-17 11:24:37 -07001135 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001136 }
ethannicholasd598f792016-07-25 10:08:54 -07001137 if (args[0]->fType == *fContext.fBool_Type) {
1138 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0));
1139 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1));
ethannicholasb3058bd2016-07-01 08:22:01 -07001140 return std::unique_ptr<Expression>(
1141 new TernaryExpression(position, std::move(args[0]),
1142 this->coerce(std::move(one), type),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001143 this->coerce(std::move(zero),
ethannicholasb3058bd2016-07-01 08:22:01 -07001144 type)));
ethannicholasd598f792016-07-25 10:08:54 -07001145 } else if (!args[0]->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001146 fErrors.error(position, "invalid argument to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001147 "' constructor (expected a number or bool, but found '" +
ethannicholasd598f792016-07-25 10:08:54 -07001148 args[0]->fType.description() + "')");
ethannicholasb3058bd2016-07-01 08:22:01 -07001149 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001150 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
ethannicholas5961bc92016-10-12 06:39:56 -07001151 type == *fContext.fUInt_Type)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001152 return std::unique_ptr<Expression>(new IntLiteral(fContext,
1153 position,
ethannicholas5961bc92016-10-12 06:39:56 -07001154 ((IntLiteral&) *args[0]).fValue,
1155 &type));
1156 }
1157 } else if (kind == Type::kArray_Kind) {
1158 const Type& base = type.componentType();
1159 for (size_t i = 0; i < args.size(); i++) {
1160 args[i] = this->coerce(std::move(args[i]), base);
ethannicholasea4567c2016-10-17 11:24:37 -07001161 if (!args[i]) {
1162 return nullptr;
1163 }
ethannicholas5961bc92016-10-12 06:39:56 -07001164 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001165 } else {
1166 ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind);
1167 int actual = 0;
1168 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas4578a8e2016-11-01 11:57:42 -04001169 if (args[i]->fType.kind() == Type::kVector_Kind ||
ethannicholasd598f792016-07-25 10:08:54 -07001170 args[i]->fType.kind() == Type::kMatrix_Kind) {
Ethan Nicholas4578a8e2016-11-01 11:57:42 -04001171 if (type.componentType().isNumber() !=
1172 args[i]->fType.componentType().isNumber()) {
1173 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid "
1174 "parameter to '" + type.description() +
1175 "' constructor");
ethannicholas7effa7a2016-10-14 09:56:33 -07001176 return nullptr;
1177 }
ethannicholasd598f792016-07-25 10:08:54 -07001178 actual += args[i]->fType.rows() * args[i]->fType.columns();
1179 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001180 actual += 1;
ethannicholasd598f792016-07-25 10:08:54 -07001181 if (type.kind() != Type::kScalar_Kind) {
1182 args[i] = this->coerce(std::move(args[i]), type.componentType());
ethannicholas7effa7a2016-10-14 09:56:33 -07001183 if (!args[i]) {
1184 return nullptr;
1185 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001186 }
1187 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001188 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid "
1189 "parameter to '" + type.description() + "' constructor");
ethannicholasb3058bd2016-07-01 08:22:01 -07001190 return nullptr;
1191 }
1192 }
ethannicholasd598f792016-07-25 10:08:54 -07001193 int min = type.rows() * type.columns();
1194 int max = type.columns() > 1 ? INT_MAX : min;
ethannicholasb3058bd2016-07-01 08:22:01 -07001195 if ((actual < min || actual > max) &&
1196 !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001197 fErrors.error(position, "invalid arguments to '" + type.description() +
1198 "' constructor (expected " + to_string(min) + " scalar" +
1199 (min == 1 ? "" : "s") + ", but found " + to_string(actual) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001200 ")");
1201 return nullptr;
1202 }
1203 }
1204 return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args)));
1205}
1206
1207std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1208 const ASTPrefixExpression& expression) {
1209 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1210 if (!base) {
1211 return nullptr;
1212 }
1213 switch (expression.fOperator) {
1214 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001215 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001216 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001217 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001218 return nullptr;
1219 }
1220 return base;
1221 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001222 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001223 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001224 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001225 return nullptr;
1226 }
1227 if (base->fKind == Expression::kIntLiteral_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001228 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001229 -((IntLiteral&) *base).fValue));
1230 }
1231 if (base->fKind == Expression::kFloatLiteral_Kind) {
1232 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001233 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001234 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001235 }
1236 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1237 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001238 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001239 fErrors.error(expression.fPosition,
1240 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001241 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001242 return nullptr;
1243 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001244 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001245 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001246 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001247 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001248 fErrors.error(expression.fPosition,
1249 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001250 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001251 return nullptr;
1252 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001253 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001254 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001255 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001256 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001257 fErrors.error(expression.fPosition,
1258 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001259 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001260 return nullptr;
1261 }
ethannicholas08a92112016-11-09 13:26:45 -08001262 if (base->fKind == Expression::kBoolLiteral_Kind) {
1263 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fPosition,
1264 !((BoolLiteral&) *base).fValue));
1265 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001266 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001267 case Token::BITWISENOT:
1268 if (base->fType != *fContext.fInt_Type) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001269 fErrors.error(expression.fPosition,
1270 "'" + Token::OperatorName(expression.fOperator) +
ethannicholas5961bc92016-10-12 06:39:56 -07001271 "' cannot operate on '" + base->fType.description() + "'");
1272 return nullptr;
1273 }
1274 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001275 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07001276 ABORT("unsupported prefix operator\n");
1277 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001278 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
ethannicholasb3058bd2016-07-01 08:22:01 -07001279 std::move(base)));
1280}
1281
1282std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1283 const ASTExpression& index) {
ethannicholas5961bc92016-10-12 06:39:56 -07001284 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1285 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001286 fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001287 "'");
1288 return nullptr;
1289 }
1290 std::unique_ptr<Expression> converted = this->convertExpression(index);
1291 if (!converted) {
1292 return nullptr;
1293 }
ethannicholas5961bc92016-10-12 06:39:56 -07001294 if (converted->fType != *fContext.fUInt_Type) {
1295 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1296 if (!converted) {
1297 return nullptr;
1298 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001299 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001300 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07001301 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001302}
1303
1304std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001305 const SkString& field) {
ethannicholasd598f792016-07-25 10:08:54 -07001306 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001307 for (size_t i = 0; i < fields.size(); i++) {
1308 if (fields[i].fName == field) {
1309 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1310 }
1311 }
ethannicholasd598f792016-07-25 10:08:54 -07001312 fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a "
ethannicholasb3058bd2016-07-01 08:22:01 -07001313 "field named '" + field + "");
1314 return nullptr;
1315}
1316
1317std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001318 const SkString& fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001319 if (base->fType.kind() != Type::kVector_Kind) {
1320 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001321 return nullptr;
1322 }
1323 std::vector<int> swizzleComponents;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001324 for (size_t i = 0; i < fields.size(); i++) {
1325 switch (fields[i]) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001326 case 'x': // fall through
1327 case 'r': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001328 case 's':
ethannicholasb3058bd2016-07-01 08:22:01 -07001329 swizzleComponents.push_back(0);
1330 break;
1331 case 'y': // fall through
1332 case 'g': // fall through
1333 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001334 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001335 swizzleComponents.push_back(1);
1336 break;
1337 }
1338 // fall through
1339 case 'z': // fall through
1340 case 'b': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001341 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001342 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001343 swizzleComponents.push_back(2);
1344 break;
1345 }
1346 // fall through
1347 case 'w': // fall through
1348 case 'a': // fall through
1349 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001350 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001351 swizzleComponents.push_back(3);
1352 break;
1353 }
1354 // fall through
1355 default:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001356 fErrors.error(base->fPosition, SkStringPrintf("invalid swizzle component '%c'",
1357 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07001358 return nullptr;
1359 }
1360 }
1361 ASSERT(swizzleComponents.size() > 0);
1362 if (swizzleComponents.size() > 4) {
1363 fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'");
1364 return nullptr;
1365 }
ethannicholasd598f792016-07-25 10:08:54 -07001366 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001367}
1368
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001369std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001370 auto found = fCapsMap.find(name);
1371 if (found == fCapsMap.end()) {
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001372 fErrors.error(position, "unknown capability flag '" + name + "'");
1373 return nullptr;
1374 }
1375 switch (found->second.fKind) {
1376 case CapValue::kBool_Kind:
1377 return std::unique_ptr<Expression>(new BoolLiteral(fContext, position,
1378 (bool) found->second.fValue));
1379 case CapValue::kInt_Kind:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001380 return std::unique_ptr<Expression>(new IntLiteral(fContext, position,
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001381 found->second.fValue));
1382 }
1383 ASSERT(false);
1384 return nullptr;
1385}
1386
ethannicholasb3058bd2016-07-01 08:22:01 -07001387std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
1388 const ASTSuffixExpression& expression) {
1389 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
1390 if (!base) {
1391 return nullptr;
1392 }
1393 switch (expression.fSuffix->fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07001394 case ASTSuffix::kIndex_Kind: {
1395 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
1396 if (expr) {
1397 return this->convertIndex(std::move(base), *expr);
1398 } else if (base->fKind == Expression::kTypeReference_Kind) {
1399 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001400 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
ethannicholas5961bc92016-10-12 06:39:56 -07001401 -1);
1402 fSymbolTable->takeOwnership(newType);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001403 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition,
ethannicholas5961bc92016-10-12 06:39:56 -07001404 *newType));
1405 } else {
1406 fErrors.error(expression.fPosition, "'[]' must follow a type name");
ethannicholasa54401d2016-10-14 08:37:32 -07001407 return nullptr;
ethannicholas5961bc92016-10-12 06:39:56 -07001408 }
1409 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001410 case ASTSuffix::kCall_Kind: {
1411 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
1412 std::vector<std::unique_ptr<Expression>> arguments;
1413 for (size_t i = 0; i < rawArguments->size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001414 std::unique_ptr<Expression> converted =
ethannicholasb3058bd2016-07-01 08:22:01 -07001415 this->convertExpression(*(*rawArguments)[i]);
1416 if (!converted) {
1417 return nullptr;
1418 }
1419 arguments.push_back(std::move(converted));
1420 }
1421 return this->call(expression.fPosition, std::move(base), std::move(arguments));
1422 }
1423 case ASTSuffix::kField_Kind: {
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001424 if (base->fType == *fContext.fSkCaps_Type) {
1425 return this->getCap(expression.fPosition,
1426 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1427 }
ethannicholasd598f792016-07-25 10:08:54 -07001428 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001429 case Type::kVector_Kind:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001430 return this->convertSwizzle(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001431 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1432 case Type::kStruct_Kind:
1433 return this->convertField(std::move(base),
1434 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1435 default:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001436 fErrors.error(base->fPosition, "cannot swizzle value of type '" +
ethannicholasd598f792016-07-25 10:08:54 -07001437 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001438 return nullptr;
1439 }
1440 }
1441 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001442 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001443 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001444 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001445 return nullptr;
1446 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001447 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001448 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001449 Token::PLUSPLUS));
1450 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001451 if (!base->fType.isNumber()) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001452 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001453 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001454 return nullptr;
1455 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001456 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001457 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07001458 Token::MINUSMINUS));
1459 default:
1460 ABORT("unsupported suffix operator");
1461 }
1462}
1463
1464void IRGenerator::checkValid(const Expression& expr) {
1465 switch (expr.fKind) {
1466 case Expression::kFunctionReference_Kind:
1467 fErrors.error(expr.fPosition, "expected '(' to begin function call");
1468 break;
1469 case Expression::kTypeReference_Kind:
1470 fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation");
1471 break;
1472 default:
ethannicholasea4567c2016-10-17 11:24:37 -07001473 if (expr.fType == *fContext.fInvalid_Type) {
1474 fErrors.error(expr.fPosition, "invalid expression");
1475 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001476 }
1477}
1478
ethannicholasb3058bd2016-07-01 08:22:01 -07001479static bool has_duplicates(const Swizzle& swizzle) {
1480 int bits = 0;
1481 for (int idx : swizzle.fComponents) {
1482 ASSERT(idx >= 0 && idx <= 3);
1483 int bit = 1 << idx;
1484 if (bits & bit) {
1485 return true;
1486 }
1487 bits |= bit;
1488 }
1489 return false;
1490}
1491
Ethan Nicholas86a43402017-01-19 13:32:00 -05001492void IRGenerator::markWrittenTo(const Expression& expr, bool readWrite) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001493 switch (expr.fKind) {
1494 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001495 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07001496 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001497 fErrors.error(expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001498 "cannot modify immutable variable '" + var.fName + "'");
1499 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001500 ((VariableReference&) expr).setRefKind(readWrite ? VariableReference::kReadWrite_RefKind
1501 : VariableReference::kWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001502 break;
1503 }
1504 case Expression::kFieldAccess_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05001505 this->markWrittenTo(*((FieldAccess&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001506 break;
1507 case Expression::kSwizzle_Kind:
1508 if (has_duplicates((Swizzle&) expr)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001509 fErrors.error(expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001510 "cannot write to the same swizzle field more than once");
1511 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001512 this->markWrittenTo(*((Swizzle&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001513 break;
1514 case Expression::kIndex_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05001515 this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07001516 break;
1517 default:
1518 fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'");
1519 break;
1520 }
1521}
1522
1523}