blob: 56d8e5e952612da8620950d7193d74344ae3feb0 [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"
Ethan Nicholasaf197692017-02-27 13:26:45 -050011#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070012
Ethan Nicholas941e7e22016-12-12 15:33:30 -050013#include "SkSLCompiler.h"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040014#include "SkSLParser.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070015#include "ast/SkSLASTBoolLiteral.h"
16#include "ast/SkSLASTFieldSuffix.h"
17#include "ast/SkSLASTFloatLiteral.h"
18#include "ast/SkSLASTIndexSuffix.h"
19#include "ast/SkSLASTIntLiteral.h"
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040020#include "ir/SkSLAppendStage.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070021#include "ir/SkSLBinaryExpression.h"
22#include "ir/SkSLBoolLiteral.h"
23#include "ir/SkSLBreakStatement.h"
24#include "ir/SkSLConstructor.h"
25#include "ir/SkSLContinueStatement.h"
26#include "ir/SkSLDiscardStatement.h"
27#include "ir/SkSLDoStatement.h"
Ethan Nicholasaae47c82017-11-10 15:34:03 -050028#include "ir/SkSLEnum.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070029#include "ir/SkSLExpressionStatement.h"
30#include "ir/SkSLField.h"
31#include "ir/SkSLFieldAccess.h"
32#include "ir/SkSLFloatLiteral.h"
33#include "ir/SkSLForStatement.h"
34#include "ir/SkSLFunctionCall.h"
35#include "ir/SkSLFunctionDeclaration.h"
36#include "ir/SkSLFunctionDefinition.h"
37#include "ir/SkSLFunctionReference.h"
38#include "ir/SkSLIfStatement.h"
39#include "ir/SkSLIndexExpression.h"
40#include "ir/SkSLInterfaceBlock.h"
41#include "ir/SkSLIntLiteral.h"
42#include "ir/SkSLLayout.h"
43#include "ir/SkSLPostfixExpression.h"
44#include "ir/SkSLPrefixExpression.h"
45#include "ir/SkSLReturnStatement.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040046#include "ir/SkSLSetting.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050047#include "ir/SkSLSwitchCase.h"
48#include "ir/SkSLSwitchStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070049#include "ir/SkSLSwizzle.h"
50#include "ir/SkSLTernaryExpression.h"
51#include "ir/SkSLUnresolvedFunction.h"
52#include "ir/SkSLVariable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070053#include "ir/SkSLVarDeclarations.h"
54#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070055#include "ir/SkSLVariableReference.h"
56#include "ir/SkSLWhileStatement.h"
57
58namespace SkSL {
59
60class AutoSymbolTable {
61public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050062 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070063 : fIR(ir)
64 , fPrevious(fIR->fSymbolTable) {
65 fIR->pushSymbolTable();
66 }
67
68 ~AutoSymbolTable() {
69 fIR->popSymbolTable();
70 ASSERT(fPrevious == fIR->fSymbolTable);
71 }
72
73 IRGenerator* fIR;
74 std::shared_ptr<SymbolTable> fPrevious;
75};
76
ethannicholas22f939e2016-10-13 13:25:34 -070077class AutoLoopLevel {
78public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050079 AutoLoopLevel(IRGenerator* ir)
ethannicholas22f939e2016-10-13 13:25:34 -070080 : fIR(ir) {
81 fIR->fLoopLevel++;
82 }
83
84 ~AutoLoopLevel() {
85 fIR->fLoopLevel--;
86 }
87
88 IRGenerator* fIR;
89};
90
Ethan Nicholasaf197692017-02-27 13:26:45 -050091class AutoSwitchLevel {
92public:
93 AutoSwitchLevel(IRGenerator* ir)
94 : fIR(ir) {
95 fIR->fSwitchLevel++;
96 }
97
98 ~AutoSwitchLevel() {
99 fIR->fSwitchLevel--;
100 }
101
102 IRGenerator* fIR;
103};
104
Ethan Nicholas11d53972016-11-28 11:23:23 -0500105IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
ethannicholasb3058bd2016-07-01 08:22:01 -0700106 ErrorReporter& errorReporter)
ethannicholasd598f792016-07-25 10:08:54 -0700107: fContext(*context)
108, fCurrentFunction(nullptr)
Ethan Nicholas762466e2017-06-29 10:03:38 -0400109, fRootSymbolTable(symbolTable)
110, fSymbolTable(symbolTable)
ethannicholas22f939e2016-10-13 13:25:34 -0700111, fLoopLevel(0)
Ethan Nicholasaf197692017-02-27 13:26:45 -0500112, fSwitchLevel(0)
Ethan Nicholas762466e2017-06-29 10:03:38 -0400113, fTmpCount(0)
ethannicholasd598f792016-07-25 10:08:54 -0700114, fErrors(errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700115
116void IRGenerator::pushSymbolTable() {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400117 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), &fErrors));
ethannicholasb3058bd2016-07-01 08:22:01 -0700118}
119
120void IRGenerator::popSymbolTable() {
121 fSymbolTable = fSymbolTable->fParent;
122}
123
Ethan Nicholas762466e2017-06-29 10:03:38 -0400124static void fill_caps(const SKSL_CAPS_CLASS& caps,
125 std::unordered_map<String, Program::Settings::Value>* capsMap) {
126#define CAP(name) capsMap->insert(std::make_pair(String(#name), \
127 Program::Settings::Value(caps.name())));
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500128 CAP(fbFetchSupport);
129 CAP(fbFetchNeedsCustomOutput);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500130 CAP(dropsTileOnZeroDivide);
131 CAP(flatInterpolationSupport);
132 CAP(noperspectiveInterpolationSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500133 CAP(externalTextureSupport);
134 CAP(texelFetchSupport);
135 CAP(imageLoadStoreSupport);
136 CAP(mustEnableAdvBlendEqs);
137 CAP(mustEnableSpecificAdvBlendEqs);
138 CAP(mustDeclareFragmentShaderOutput);
139 CAP(canUseAnyFunctionInShader);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700140 CAP(floatIs32Bits);
Ethan Nicholas07990de2017-07-18 09:47:43 -0400141 CAP(integerSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500142#undef CAP
143}
144
145void IRGenerator::start(const Program::Settings* settings) {
146 fSettings = settings;
147 fCapsMap.clear();
148 if (settings->fCaps) {
149 fill_caps(*settings->fCaps, &fCapsMap);
150 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500151 this->pushSymbolTable();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400152 fInvocations = -1;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500153 fInputs.reset();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000154 fSkPerVertex = nullptr;
155 fRTAdjust = nullptr;
156 fRTAdjustInterfaceBlock = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500157}
158
159void IRGenerator::finish() {
160 this->popSymbolTable();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500161 fSettings = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500162}
163
ethannicholasb3058bd2016-07-01 08:22:01 -0700164std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700165 return std::unique_ptr<Extension>(new Extension(extension.fOffset, extension.fName));
ethannicholasb3058bd2016-07-01 08:22:01 -0700166}
167
168std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) {
169 switch (statement.fKind) {
170 case ASTStatement::kBlock_Kind:
171 return this->convertBlock((ASTBlock&) statement);
172 case ASTStatement::kVarDeclaration_Kind:
173 return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000174 case ASTStatement::kExpression_Kind: {
175 std::unique_ptr<Statement> result =
176 this->convertExpressionStatement((ASTExpressionStatement&) statement);
177 if (fRTAdjust && Program::kGeometry_Kind == fKind) {
178 ASSERT(result->fKind == Statement::kExpression_Kind);
179 Expression& expr = *((ExpressionStatement&) *result).fExpression;
180 if (expr.fKind == Expression::kFunctionCall_Kind) {
181 FunctionCall& fc = (FunctionCall&) expr;
182 if (fc.fFunction.fBuiltin && fc.fFunction.fName == "EmitVertex") {
183 std::vector<std::unique_ptr<Statement>> statements;
184 statements.push_back(getNormalizeSkPositionCode());
185 statements.push_back(std::move(result));
186 return std::unique_ptr<Block>(new Block(statement.fOffset,
187 std::move(statements),
188 fSymbolTable));
189 }
190 }
191 }
192 return result;
193 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700194 case ASTStatement::kIf_Kind:
195 return this->convertIf((ASTIfStatement&) statement);
196 case ASTStatement::kFor_Kind:
197 return this->convertFor((ASTForStatement&) statement);
198 case ASTStatement::kWhile_Kind:
199 return this->convertWhile((ASTWhileStatement&) statement);
200 case ASTStatement::kDo_Kind:
201 return this->convertDo((ASTDoStatement&) statement);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500202 case ASTStatement::kSwitch_Kind:
203 return this->convertSwitch((ASTSwitchStatement&) statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 case ASTStatement::kReturn_Kind:
205 return this->convertReturn((ASTReturnStatement&) statement);
206 case ASTStatement::kBreak_Kind:
207 return this->convertBreak((ASTBreakStatement&) statement);
208 case ASTStatement::kContinue_Kind:
209 return this->convertContinue((ASTContinueStatement&) statement);
210 case ASTStatement::kDiscard_Kind:
211 return this->convertDiscard((ASTDiscardStatement&) statement);
212 default:
213 ABORT("unsupported statement type: %d\n", statement.fKind);
214 }
215}
216
217std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) {
218 AutoSymbolTable table(this);
219 std::vector<std::unique_ptr<Statement>> statements;
220 for (size_t i = 0; i < block.fStatements.size(); i++) {
221 std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]);
222 if (!statement) {
223 return nullptr;
224 }
225 statements.push_back(std::move(statement));
226 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700227 return std::unique_ptr<Block>(new Block(block.fOffset, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700228}
229
230std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
231 const ASTVarDeclarationStatement& s) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700232 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 if (!decl) {
234 return nullptr;
235 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700236 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700237}
238
ethannicholas14fe8cc2016-09-07 13:37:16 -0700239std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl,
240 Variable::Storage storage) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000241 std::vector<std::unique_ptr<VarDeclaration>> variables;
ethannicholasd598f792016-07-25 10:08:54 -0700242 const Type* baseType = this->convertType(*decl.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700243 if (!baseType) {
244 return nullptr;
245 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700246 for (const auto& varDecl : decl.fVars) {
Ethan Nicholas6c942712018-03-16 09:45:11 -0400247 if (decl.fModifiers.fLayout.fLocation == 0 && decl.fModifiers.fLayout.fIndex == 0 &&
248 (decl.fModifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind &&
249 varDecl.fName != "sk_FragColor") {
250 fErrors.error(decl.fOffset,
251 "out location=0, index=0 is reserved for sk_FragColor");
252 }
ethannicholasd598f792016-07-25 10:08:54 -0700253 const Type* type = baseType;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700254 std::vector<std::unique_ptr<Expression>> sizes;
255 for (const auto& rawSize : varDecl.fSizes) {
256 if (rawSize) {
257 auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700258 if (!size) {
259 return nullptr;
260 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700261 String name(type->fName);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500262 int64_t count;
ethannicholasb3058bd2016-07-01 08:22:01 -0700263 if (size->fKind == Expression::kIntLiteral_Kind) {
264 count = ((IntLiteral&) *size).fValue;
265 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700266 fErrors.error(size->fOffset, "array size must be positive");
ethannicholasb3058bd2016-07-01 08:22:01 -0700267 }
268 name += "[" + to_string(count) + "]";
269 } else {
270 count = -1;
271 name += "[]";
272 }
ethannicholasd598f792016-07-25 10:08:54 -0700273 type = new Type(name, Type::kArray_Kind, *type, (int) count);
274 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700275 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700276 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700277 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
ethannicholasd598f792016-07-25 10:08:54 -0700278 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700279 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700280 }
281 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000282 auto var = std::unique_ptr<Variable>(new Variable(decl.fOffset, decl.fModifiers,
283 varDecl.fName, *type, storage));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000284 if (var->fName == Compiler::RTADJUST_NAME) {
285 ASSERT(!fRTAdjust);
286 ASSERT(var->fType == *fContext.fFloat4_Type);
287 fRTAdjust = var.get();
288 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700289 std::unique_ptr<Expression> value;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700290 if (varDecl.fValue) {
291 value = this->convertExpression(*varDecl.fValue);
ethannicholasb3058bd2016-07-01 08:22:01 -0700292 if (!value) {
293 return nullptr;
294 }
ethannicholasd598f792016-07-25 10:08:54 -0700295 value = this->coerce(std::move(value), *type);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500296 if (!value) {
297 return nullptr;
298 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400299 var->fWriteCount = 1;
Ethan Nicholas8f6c2ab2018-01-17 13:51:52 -0500300 var->fInitialValue = value.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700301 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700302 if (storage == Variable::kGlobal_Storage && varDecl.fName == "sk_FragColor" &&
ethannicholasea4567c2016-10-17 11:24:37 -0700303 (*fSymbolTable)[varDecl.fName]) {
ethannicholas5961bc92016-10-12 06:39:56 -0700304 // already defined, ignore
ethannicholasea4567c2016-10-17 11:24:37 -0700305 } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] &&
306 (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -0700307 ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700308 // already defined, just update the modifiers
ethannicholas14fe8cc2016-09-07 13:37:16 -0700309 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
ethannicholasf789b382016-08-03 12:43:36 -0700310 old->fModifiers = var->fModifiers;
311 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000312 variables.emplace_back(new VarDeclaration(var.get(), std::move(sizes),
313 std::move(value)));
ethannicholas14fe8cc2016-09-07 13:37:16 -0700314 fSymbolTable->add(varDecl.fName, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700315 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700316 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700317 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fOffset,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700318 baseType,
319 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700320}
321
ethannicholas5961bc92016-10-12 06:39:56 -0700322std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(
323 const ASTModifiersDeclaration& m) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400324 Modifiers modifiers = m.fModifiers;
325 if (modifiers.fLayout.fInvocations != -1) {
326 fInvocations = modifiers.fLayout.fInvocations;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600327 if (fSettings->fCaps && !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400328 modifiers.fLayout.fInvocations = -1;
329 Variable* invocationId = (Variable*) (*fSymbolTable)["sk_InvocationID"];
330 ASSERT(invocationId);
Ethan Nicholasd1d52562018-03-20 16:30:34 -0400331 invocationId->fModifiers.fFlags = 0;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400332 invocationId->fModifiers.fLayout.fBuiltin = -1;
333 if (modifiers.fLayout.description() == "") {
334 return nullptr;
335 }
336 }
337 }
338 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fSettings->fCaps &&
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600339 !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400340 modifiers.fLayout.fMaxVertices *= fInvocations;
341 }
342 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700343}
344
ethannicholasb3058bd2016-07-01 08:22:01 -0700345std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500346 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700347 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700348 if (!test) {
349 return nullptr;
350 }
351 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
352 if (!ifTrue) {
353 return nullptr;
354 }
355 std::unique_ptr<Statement> ifFalse;
356 if (s.fIfFalse) {
357 ifFalse = this->convertStatement(*s.fIfFalse);
358 if (!ifFalse) {
359 return nullptr;
360 }
361 }
ethannicholas08a92112016-11-09 13:26:45 -0800362 if (test->fKind == Expression::kBoolLiteral_Kind) {
363 // static boolean value, fold down to a single branch
364 if (((BoolLiteral&) *test).fValue) {
365 return ifTrue;
366 } else if (s.fIfFalse) {
367 return ifFalse;
368 } else {
369 // False & no else clause. Not an error, so don't return null!
370 std::vector<std::unique_ptr<Statement>> empty;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700371 return std::unique_ptr<Statement>(new Block(s.fOffset, std::move(empty),
ethannicholas08a92112016-11-09 13:26:45 -0800372 fSymbolTable));
373 }
374 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700375 return std::unique_ptr<Statement>(new IfStatement(s.fOffset, s.fIsStatic, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700376 std::move(ifTrue), std::move(ifFalse)));
377}
378
379std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) {
ethannicholas22f939e2016-10-13 13:25:34 -0700380 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700381 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700382 std::unique_ptr<Statement> initializer;
383 if (f.fInitializer) {
384 initializer = this->convertStatement(*f.fInitializer);
385 if (!initializer) {
386 return nullptr;
387 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700388 }
ethannicholas22f939e2016-10-13 13:25:34 -0700389 std::unique_ptr<Expression> test;
390 if (f.fTest) {
391 test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type);
392 if (!test) {
393 return nullptr;
394 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700395 }
ethannicholas22f939e2016-10-13 13:25:34 -0700396 std::unique_ptr<Expression> next;
397 if (f.fNext) {
398 next = this->convertExpression(*f.fNext);
399 if (!next) {
400 return nullptr;
401 }
402 this->checkValid(*next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700403 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700404 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement);
405 if (!statement) {
406 return nullptr;
407 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700408 return std::unique_ptr<Statement>(new ForStatement(f.fOffset, std::move(initializer),
ethannicholasb3058bd2016-07-01 08:22:01 -0700409 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700410 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700411}
412
413std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) {
ethannicholas22f939e2016-10-13 13:25:34 -0700414 AutoLoopLevel level(this);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500415 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700416 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700417 if (!test) {
418 return nullptr;
419 }
420 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement);
421 if (!statement) {
422 return nullptr;
423 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700424 return std::unique_ptr<Statement>(new WhileStatement(w.fOffset, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 std::move(statement)));
426}
427
428std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) {
ethannicholas22f939e2016-10-13 13:25:34 -0700429 AutoLoopLevel level(this);
ethannicholasd598f792016-07-25 10:08:54 -0700430 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest),
431 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700432 if (!test) {
433 return nullptr;
434 }
435 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement);
436 if (!statement) {
437 return nullptr;
438 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700439 return std::unique_ptr<Statement>(new DoStatement(d.fOffset, std::move(statement),
ethannicholasb3058bd2016-07-01 08:22:01 -0700440 std::move(test)));
441}
442
Ethan Nicholasaf197692017-02-27 13:26:45 -0500443std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTSwitchStatement& s) {
444 AutoSwitchLevel level(this);
445 std::unique_ptr<Expression> value = this->convertExpression(*s.fValue);
446 if (!value) {
447 return nullptr;
448 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500449 if (value->fType != *fContext.fUInt_Type && value->fType.kind() != Type::kEnum_Kind) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500450 value = this->coerce(std::move(value), *fContext.fInt_Type);
451 if (!value) {
452 return nullptr;
453 }
454 }
455 AutoSymbolTable table(this);
456 std::unordered_set<int> caseValues;
457 std::vector<std::unique_ptr<SwitchCase>> cases;
458 for (const auto& c : s.fCases) {
459 std::unique_ptr<Expression> caseValue;
460 if (c->fValue) {
461 caseValue = this->convertExpression(*c->fValue);
462 if (!caseValue) {
463 return nullptr;
464 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500465 caseValue = this->coerce(std::move(caseValue), value->fType);
466 if (!caseValue) {
467 return nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500468 }
469 if (!caseValue->isConstant()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700470 fErrors.error(caseValue->fOffset, "case value must be a constant");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500471 return nullptr;
472 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500473 int64_t v;
474 this->getConstantInt(*caseValue, &v);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500475 if (caseValues.find(v) != caseValues.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700476 fErrors.error(caseValue->fOffset, "duplicate case value");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500477 }
478 caseValues.insert(v);
479 }
480 std::vector<std::unique_ptr<Statement>> statements;
481 for (const auto& s : c->fStatements) {
482 std::unique_ptr<Statement> converted = this->convertStatement(*s);
483 if (!converted) {
484 return nullptr;
485 }
486 statements.push_back(std::move(converted));
487 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700488 cases.emplace_back(new SwitchCase(c->fOffset, std::move(caseValue),
Ethan Nicholasaf197692017-02-27 13:26:45 -0500489 std::move(statements)));
490 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700491 return std::unique_ptr<Statement>(new SwitchStatement(s.fOffset, s.fIsStatic,
Ethan Nicholasc432b0c2017-07-18 13:22:37 -0400492 std::move(value), std::move(cases),
493 fSymbolTable));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500494}
495
ethannicholasb3058bd2016-07-01 08:22:01 -0700496std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(
497 const ASTExpressionStatement& s) {
498 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
499 if (!e) {
500 return nullptr;
501 }
502 this->checkValid(*e);
503 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
504}
505
506std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) {
507 ASSERT(fCurrentFunction);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000508 // early returns from a vertex main function will bypass the sk_Position normalization, so
509 // assert that we aren't doing that. It is of course possible to fix this by adding a
510 // normalization before each return, but it will probably never actually be necessary.
511 ASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->fName);
ethannicholasb3058bd2016-07-01 08:22:01 -0700512 if (r.fExpression) {
513 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression);
514 if (!result) {
515 return nullptr;
516 }
ethannicholasd598f792016-07-25 10:08:54 -0700517 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700518 fErrors.error(result->fOffset, "may not return a value from a void function");
ethannicholasb3058bd2016-07-01 08:22:01 -0700519 } else {
520 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
521 if (!result) {
522 return nullptr;
523 }
524 }
525 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
526 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700527 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700528 fErrors.error(r.fOffset, "expected function to return '" +
529 fCurrentFunction->fReturnType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700530 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700531 return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700532 }
533}
534
535std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500536 if (fLoopLevel > 0 || fSwitchLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700537 return std::unique_ptr<Statement>(new BreakStatement(b.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700538 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700539 fErrors.error(b.fOffset, "break statement must be inside a loop or switch");
ethannicholas22f939e2016-10-13 13:25:34 -0700540 return nullptr;
541 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700542}
543
544std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) {
ethannicholas22f939e2016-10-13 13:25:34 -0700545 if (fLoopLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700546 return std::unique_ptr<Statement>(new ContinueStatement(c.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700547 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700548 fErrors.error(c.fOffset, "continue statement must be inside a loop");
ethannicholas22f939e2016-10-13 13:25:34 -0700549 return nullptr;
550 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700551}
552
553std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700554 return std::unique_ptr<Statement>(new DiscardStatement(d.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700555}
556
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500557std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400558 Layout invokeLayout;
559 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700560 FunctionDeclaration* invokeDecl = new FunctionDeclaration(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400561 invokeModifiers,
562 "_invoke",
563 std::vector<const Variable*>(),
564 *fContext.fVoid_Type);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500565 fProgramElements->push_back(std::unique_ptr<ProgramElement>(
566 new FunctionDefinition(-1, *invokeDecl, std::move(main))));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400567 fSymbolTable->add(invokeDecl->fName, std::unique_ptr<FunctionDeclaration>(invokeDecl));
568
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000569 std::vector<std::unique_ptr<VarDeclaration>> variables;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400570 Variable* loopIdx = (Variable*) (*fSymbolTable)["sk_InvocationID"];
571 ASSERT(loopIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700572 std::unique_ptr<Expression> test(new BinaryExpression(-1,
573 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400574 Token::LT,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700575 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, fInvocations)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400576 *fContext.fBool_Type));
577 std::unique_ptr<Expression> next(new PostfixExpression(
578 std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700579 new VariableReference(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400580 *loopIdx,
581 VariableReference::kReadWrite_RefKind)),
582 Token::PLUSPLUS));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700583 ASTIdentifier endPrimitiveID = ASTIdentifier(-1, "EndPrimitive");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400584 std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
585 ASSERT(endPrimitive);
586
587 std::vector<std::unique_ptr<Statement>> loopBody;
588 std::vector<std::unique_ptr<Expression>> invokeArgs;
589 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700590 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400591 *invokeDecl,
592 std::vector<std::unique_ptr<Expression>>()))));
593 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700594 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400595 std::move(endPrimitive),
596 std::vector<std::unique_ptr<Expression>>()))));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700597 std::unique_ptr<Expression> assignment(new BinaryExpression(-1,
598 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400599 Token::EQ,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700600 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, 0)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400601 *fContext.fInt_Type));
602 std::unique_ptr<Statement> initializer(new ExpressionStatement(std::move(assignment)));
603 std::unique_ptr<Statement> loop = std::unique_ptr<Statement>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700604 new ForStatement(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400605 std::move(initializer),
606 std::move(test),
607 std::move(next),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700608 std::unique_ptr<Block>(new Block(-1, std::move(loopBody))),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400609 fSymbolTable));
610 std::vector<std::unique_ptr<Statement>> children;
611 children.push_back(std::move(loop));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700612 return std::unique_ptr<Block>(new Block(-1, std::move(children)));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400613}
614
Robert Phillipsfe8da172018-01-24 14:52:02 +0000615std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
616 // sk_Position = float4(sk_Position.x * rtAdjust.x + sk_Position.w * rtAdjust.y,
617 // sk_Position.y * rtAdjust.z + sk_Position.w * rtAdjust.w,
618 // 0,
619 // sk_Position.w);
620 ASSERT(fSkPerVertex && fRTAdjust);
621 #define REF(var) std::unique_ptr<Expression>(\
622 new VariableReference(-1, *var, VariableReference::kRead_RefKind))
623 #define FIELD(var, idx) std::unique_ptr<Expression>(\
624 new FieldAccess(REF(var), idx, FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
625 #define POS std::unique_ptr<Expression>(new FieldAccess(REF(fSkPerVertex), 0, \
626 FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
627 #define ADJUST (fRTAdjustInterfaceBlock ? \
628 FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \
629 REF(fRTAdjust))
630 #define SWIZZLE(expr, field) std::unique_ptr<Expression>(new Swizzle(fContext, expr, { field }))
631 #define OP(left, op, right) std::unique_ptr<Expression>(\
632 new BinaryExpression(-1, left, op, right, *fContext.fFloat_Type))
633 std::vector<std::unique_ptr<Expression>> children;
634 children.push_back(OP(OP(SWIZZLE(POS, 0), Token::STAR, SWIZZLE(ADJUST, 0)),
635 Token::PLUS,
636 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 1))));
637 children.push_back(OP(OP(SWIZZLE(POS, 1), Token::STAR, SWIZZLE(ADJUST, 2)),
638 Token::PLUS,
639 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 3))));
640 children.push_back(std::unique_ptr<Expression>(new FloatLiteral(fContext, -1, 0.0)));
641 children.push_back(SWIZZLE(POS, 3));
642 std::unique_ptr<Expression> result = OP(POS, Token::EQ,
643 std::unique_ptr<Expression>(new Constructor(-1,
644 *fContext.fFloat4_Type,
645 std::move(children))));
646 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(result)));
647}
648
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500649void IRGenerator::convertFunction(const ASTFunction& f) {
ethannicholasd598f792016-07-25 10:08:54 -0700650 const Type* returnType = this->convertType(*f.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700651 if (!returnType) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400652 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700653 }
ethannicholasd598f792016-07-25 10:08:54 -0700654 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700655 for (const auto& param : f.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700656 const Type* type = this->convertType(*param->fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700657 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400658 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700659 }
660 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) {
661 int size = param->fSizes[j];
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400662 String name = type->name() + "[" + to_string(size) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700663 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size);
664 fSymbolTable->takeOwnership(newType);
665 type = newType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700666 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700667 StringFragment name = param->fName;
668 Variable* var = new Variable(param->fOffset, param->fModifiers, name, *type,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000669 Variable::kParameter_Storage);
ethannicholasd598f792016-07-25 10:08:54 -0700670 fSymbolTable->takeOwnership(var);
671 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700672 }
673
674 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700675 const FunctionDeclaration* decl = nullptr;
676 auto entry = (*fSymbolTable)[f.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700677 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700678 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700679 switch (entry->fKind) {
680 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700681 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700682 break;
683 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700684 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700685 break;
686 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700687 fErrors.error(f.fOffset, "symbol '" + f.fName + "' was already defined");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400688 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700689 }
690 for (const auto& other : functions) {
691 ASSERT(other->fName == f.fName);
692 if (parameters.size() == other->fParameters.size()) {
693 bool match = true;
694 for (size_t i = 0; i < parameters.size(); i++) {
695 if (parameters[i]->fType != other->fParameters[i]->fType) {
696 match = false;
697 break;
698 }
699 }
700 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700701 if (*returnType != other->fReturnType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700702 FunctionDeclaration newDecl(f.fOffset, f.fModifiers, f.fName, parameters,
Ethan Nicholascb670962017-04-20 19:31:52 -0400703 *returnType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700704 fErrors.error(f.fOffset, "functions '" + newDecl.description() +
705 "' and '" + other->description() +
706 "' differ only in return type");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400707 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700708 }
709 decl = other;
710 for (size_t i = 0; i < parameters.size(); i++) {
711 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700712 fErrors.error(f.fOffset, "modifiers on parameter " +
713 to_string((uint64_t) i + 1) +
714 " differ between declaration and "
715 "definition");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400716 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700717 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700718 }
719 if (other->fDefined) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700720 fErrors.error(f.fOffset, "duplicate definition of " +
721 other->description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700722 }
723 break;
724 }
725 }
726 }
727 }
728 if (!decl) {
729 // couldn't find an existing declaration
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700730 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fOffset,
Ethan Nicholascb670962017-04-20 19:31:52 -0400731 f.fModifiers,
ethannicholas471e8942016-10-28 09:02:46 -0700732 f.fName,
733 parameters,
734 *returnType));
735 decl = newDecl.get();
736 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700737 }
ethannicholasd598f792016-07-25 10:08:54 -0700738 if (f.fBody) {
739 ASSERT(!fCurrentFunction);
740 fCurrentFunction = decl;
741 decl->fDefined = true;
742 std::shared_ptr<SymbolTable> old = fSymbolTable;
743 AutoSymbolTable table(this);
744 for (size_t i = 0; i < parameters.size(); i++) {
745 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700746 }
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600747 bool needInvocationIDWorkaround = fInvocations != -1 && f.fName == "main" &&
748 fSettings->fCaps &&
749 !fSettings->fCaps->gsInvocationsSupport();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400750 ASSERT(!fExtraVars.size());
ethannicholasd598f792016-07-25 10:08:54 -0700751 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400752 for (auto& v : fExtraVars) {
753 body->fStatements.insert(body->fStatements.begin(), std::move(v));
754 }
755 fExtraVars.clear();
ethannicholasd598f792016-07-25 10:08:54 -0700756 fCurrentFunction = nullptr;
757 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400758 return;
759 }
760 if (needInvocationIDWorkaround) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500761 body = this->applyInvocationIDWorkaround(std::move(body));
ethannicholasd598f792016-07-25 10:08:54 -0700762 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400763 // conservatively assume all user-defined functions have side effects
764 ((Modifiers&) decl->fModifiers).fFlags |= Modifiers::kHasSideEffects_Flag;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000765 if (Program::kVertex_Kind == fKind && f.fName == "main" && fRTAdjust) {
766 body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode());
767 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500768 fProgramElements->push_back(std::unique_ptr<FunctionDefinition>(
769 new FunctionDefinition(f.fOffset, *decl, std::move(body))));
ethannicholasb3058bd2016-07-01 08:22:01 -0700770 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700771}
772
773std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) {
774 std::shared_ptr<SymbolTable> old = fSymbolTable;
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500775 this->pushSymbolTable();
776 std::shared_ptr<SymbolTable> symbols = fSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700777 std::vector<Type::Field> fields;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400778 bool haveRuntimeArray = false;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000779 bool foundRTAdjust = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700780 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700781 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
Ethan Nicholas11d53972016-11-28 11:23:23 -0500782 *intf.fDeclarations[i],
ethannicholasb3058bd2016-07-01 08:22:01 -0700783 Variable::kGlobal_Storage);
ethannicholas7effa7a2016-10-14 09:56:33 -0700784 if (!decl) {
785 return nullptr;
786 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000787 for (const auto& stmt : decl->fVars) {
788 VarDeclaration& vd = (VarDeclaration&) *stmt;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400789 if (haveRuntimeArray) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000790 fErrors.error(decl->fOffset,
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400791 "only the last entry in an interface block may be a runtime-sized "
792 "array");
793 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000794 if (vd.fVar == fRTAdjust) {
795 foundRTAdjust = true;
796 ASSERT(vd.fVar->fType == *fContext.fFloat4_Type);
797 fRTAdjustFieldIndex = fields.size();
798 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000799 fields.push_back(Type::Field(vd.fVar->fModifiers, vd.fVar->fName,
800 &vd.fVar->fType));
801 if (vd.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700802 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700803 "initializers are not permitted on interface block fields");
804 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000805 if (vd.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
806 Modifiers::kOut_Flag |
807 Modifiers::kUniform_Flag |
808 Modifiers::kBuffer_Flag |
809 Modifiers::kConst_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700810 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700811 "interface block fields may not have storage qualifiers");
812 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000813 if (vd.fVar->fType.kind() == Type::kArray_Kind &&
814 vd.fVar->fType.columns() == -1) {
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400815 haveRuntimeArray = true;
816 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500817 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700818 }
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500819 this->popSymbolTable();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700820 Type* type = new Type(intf.fOffset, intf.fTypeName, fields);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500821 old->takeOwnership(type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500822 std::vector<std::unique_ptr<Expression>> sizes;
823 for (const auto& size : intf.fSizes) {
824 if (size) {
825 std::unique_ptr<Expression> converted = this->convertExpression(*size);
826 if (!converted) {
827 return nullptr;
828 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400829 String name = type->fName;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500830 int64_t count;
831 if (converted->fKind == Expression::kIntLiteral_Kind) {
832 count = ((IntLiteral&) *converted).fValue;
833 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700834 fErrors.error(converted->fOffset, "array size must be positive");
Ethan Nicholas50afc172017-02-16 14:49:57 -0500835 }
836 name += "[" + to_string(count) + "]";
837 } else {
838 count = -1;
839 name += "[]";
840 }
841 type = new Type(name, Type::kArray_Kind, *type, (int) count);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500842 symbols->takeOwnership((Type*) type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500843 sizes.push_back(std::move(converted));
844 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700845 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500846 symbols->takeOwnership((Type*) type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500847 sizes.push_back(nullptr);
848 }
849 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700850 Variable* var = new Variable(intf.fOffset, intf.fModifiers,
851 intf.fInstanceName.fLength ? intf.fInstanceName : intf.fTypeName,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000852 *type, Variable::kGlobal_Storage);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000853 if (foundRTAdjust) {
854 fRTAdjustInterfaceBlock = var;
855 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500856 old->takeOwnership(var);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700857 if (intf.fInstanceName.fLength) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500858 old->addWithoutOwnership(intf.fInstanceName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700859 } else {
860 for (size_t i = 0; i < fields.size(); i++) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700861 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fOffset, *var,
ethannicholasd598f792016-07-25 10:08:54 -0700862 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700863 }
864 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000865 if (var->fName == Compiler::PERVERTEX_NAME) {
866 ASSERT(!fSkPerVertex);
867 fSkPerVertex = var;
868 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700869 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fOffset,
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400870 var,
Ethan Nicholas50afc172017-02-16 14:49:57 -0500871 intf.fTypeName,
872 intf.fInstanceName,
873 std::move(sizes),
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500874 symbols));
ethannicholasb3058bd2016-07-01 08:22:01 -0700875}
876
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500877void IRGenerator::getConstantInt(const Expression& value, int64_t* out) {
878 switch (value.fKind) {
879 case Expression::kIntLiteral_Kind:
880 *out = ((const IntLiteral&) value).fValue;
881 break;
882 case Expression::kVariableReference_Kind: {
883 const Variable& var = ((VariableReference&) value).fVariable;
884 if ((var.fModifiers.fFlags & Modifiers::kConst_Flag) &&
885 var.fInitialValue) {
886 this->getConstantInt(*var.fInitialValue, out);
887 }
888 break;
889 }
890 default:
891 fErrors.error(value.fOffset, "expected a constant int");
892 }
893}
894
895void IRGenerator::convertEnum(const ASTEnum& e) {
896 std::vector<Variable*> variables;
897 int64_t currentValue = 0;
898 Layout layout;
899 ASTType enumType(e.fOffset, e.fTypeName, ASTType::kIdentifier_Kind, {});
900 const Type* type = this->convertType(enumType);
901 Modifiers modifiers(layout, Modifiers::kConst_Flag);
902 std::shared_ptr<SymbolTable> symbols(new SymbolTable(fSymbolTable, &fErrors));
903 fSymbolTable = symbols;
904 for (size_t i = 0; i < e.fNames.size(); i++) {
905 std::unique_ptr<Expression> value;
906 if (e.fValues[i]) {
907 value = this->convertExpression(*e.fValues[i]);
908 if (!value) {
909 fSymbolTable = symbols->fParent;
910 return;
911 }
912 this->getConstantInt(*value, &currentValue);
913 }
914 value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue));
915 ++currentValue;
916 auto var = std::unique_ptr<Variable>(new Variable(e.fOffset, modifiers, e.fNames[i],
917 *type, Variable::kGlobal_Storage,
918 value.get()));
919 variables.push_back(var.get());
920 symbols->add(e.fNames[i], std::move(var));
921 symbols->takeOwnership(value.release());
922 }
923 fProgramElements->push_back(std::unique_ptr<ProgramElement>(new Enum(e.fOffset, e.fTypeName,
924 symbols)));
925 fSymbolTable = symbols->fParent;
926}
927
ethannicholasd598f792016-07-25 10:08:54 -0700928const Type* IRGenerator::convertType(const ASTType& type) {
929 const Symbol* result = (*fSymbolTable)[type.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700930 if (result && result->fKind == Symbol::kType_Kind) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500931 for (int size : type.fSizes) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700932 String name(result->fName);
933 name += "[";
Ethan Nicholas50afc172017-02-16 14:49:57 -0500934 if (size != -1) {
935 name += to_string(size);
936 }
937 name += "]";
938 result = new Type(name, Type::kArray_Kind, (const Type&) *result, size);
939 fSymbolTable->takeOwnership((Type*) result);
940 }
ethannicholasd598f792016-07-25 10:08:54 -0700941 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700942 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700943 fErrors.error(type.fOffset, "unknown type '" + type.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700944 return nullptr;
945}
946
947std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) {
948 switch (expr.fKind) {
949 case ASTExpression::kIdentifier_Kind:
950 return this->convertIdentifier((ASTIdentifier&) expr);
951 case ASTExpression::kBool_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700952 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700953 ((ASTBoolLiteral&) expr).fValue));
954 case ASTExpression::kInt_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700955 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700956 ((ASTIntLiteral&) expr).fValue));
957 case ASTExpression::kFloat_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700958 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700959 ((ASTFloatLiteral&) expr).fValue));
960 case ASTExpression::kBinary_Kind:
961 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
962 case ASTExpression::kPrefix_Kind:
963 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
964 case ASTExpression::kSuffix_Kind:
965 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
966 case ASTExpression::kTernary_Kind:
967 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
968 default:
969 ABORT("unsupported expression type: %d\n", expr.fKind);
970 }
971}
972
973std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) {
ethannicholasd598f792016-07-25 10:08:54 -0700974 const Symbol* result = (*fSymbolTable)[identifier.fText];
ethannicholasb3058bd2016-07-01 08:22:01 -0700975 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700976 fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.fText + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700977 return nullptr;
978 }
979 switch (result->fKind) {
980 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700981 std::vector<const FunctionDeclaration*> f = {
982 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -0700983 };
ethannicholasd598f792016-07-25 10:08:54 -0700984 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700985 identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -0700986 f));
ethannicholasb3058bd2016-07-01 08:22:01 -0700987 }
988 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700989 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
990 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700991 identifier.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700992 f->fFunctions));
993 }
994 case Symbol::kVariable_Kind: {
Ethan Nicholas38657112017-02-09 17:01:22 -0500995 const Variable* var = (const Variable*) result;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400996#ifndef SKSL_STANDALONE
Ethan Nicholas38657112017-02-09 17:01:22 -0500997 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) {
998 fInputs.fFlipY = true;
999 if (fSettings->fFlipY &&
1000 (!fSettings->fCaps ||
1001 !fSettings->fCaps->fragCoordConventionsExtensionString())) {
1002 fInputs.fRTHeight = true;
1003 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001004 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001005#endif
Ethan Nicholas86a43402017-01-19 13:32:00 -05001006 // default to kRead_RefKind; this will be corrected later if the variable is written to
1007 return std::unique_ptr<VariableReference>(new VariableReference(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001008 identifier.fOffset,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001009 *var,
1010 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001011 }
1012 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001013 const Field* field = (const Field*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001014 VariableReference* base = new VariableReference(identifier.fOffset, field->fOwner,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001015 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -07001016 return std::unique_ptr<Expression>(new FieldAccess(
1017 std::unique_ptr<Expression>(base),
1018 field->fFieldIndex,
1019 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001020 }
1021 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001022 const Type* t = (const Type*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001023 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001024 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001025 }
1026 default:
1027 ABORT("unsupported symbol type %d\n", result->fKind);
1028 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001029}
1030
Ethan Nicholas762466e2017-06-29 10:03:38 -04001031std::unique_ptr<Section> IRGenerator::convertSection(const ASTSection& s) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001032 return std::unique_ptr<Section>(new Section(s.fOffset, s.fName, s.fArgument, s.fText));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001033}
1034
1035
Ethan Nicholas11d53972016-11-28 11:23:23 -05001036std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001037 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001038 if (!expr) {
1039 return nullptr;
1040 }
ethannicholasd598f792016-07-25 10:08:54 -07001041 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001042 return expr;
1043 }
1044 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -07001045 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001046 return nullptr;
1047 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001048 if (expr->coercionCost(type) == INT_MAX) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001049 fErrors.error(expr->fOffset, "expected '" + type.description() + "', but found '" +
ethannicholasd598f792016-07-25 10:08:54 -07001050 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001051 return nullptr;
1052 }
ethannicholasd598f792016-07-25 10:08:54 -07001053 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001054 std::vector<std::unique_ptr<Expression>> args;
1055 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001056 ASTIdentifier id(-1, type.fName);
ethannicholasb3058bd2016-07-01 08:22:01 -07001057 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
1058 ASSERT(ctor);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001059 return this->call(-1, std::move(ctor), std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001060 }
ethannicholas5961bc92016-10-12 06:39:56 -07001061 std::vector<std::unique_ptr<Expression>> args;
1062 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001063 return std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001064}
1065
ethannicholasf789b382016-08-03 12:43:36 -07001066static bool is_matrix_multiply(const Type& left, const Type& right) {
1067 if (left.kind() == Type::kMatrix_Kind) {
1068 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
1069 }
1070 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
1071}
ethannicholasea4567c2016-10-17 11:24:37 -07001072
ethannicholasb3058bd2016-07-01 08:22:01 -07001073/**
1074 * Determines the operand and result types of a binary expression. Returns true if the expression is
1075 * legal, false otherwise. If false, the values of the out parameters are undefined.
1076 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001077static bool determine_binary_type(const Context& context,
1078 Token::Kind op,
1079 const Type& left,
1080 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -07001081 const Type** outLeftType,
1082 const Type** outRightType,
1083 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001084 bool tryFlipped) {
1085 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -07001086 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -07001087 switch (op) {
ethannicholasea4567c2016-10-17 11:24:37 -07001088 case Token::EQ:
1089 *outLeftType = &left;
1090 *outRightType = &left;
1091 *outResultType = &left;
1092 return right.canCoerceTo(left);
ethannicholasb3058bd2016-07-01 08:22:01 -07001093 case Token::EQEQ: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001094 case Token::NEQ:
Ethan Nicholas23463002018-03-28 15:16:15 -04001095 if (right.canCoerceTo(left)) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001096 *outLeftType = &left;
Ethan Nicholas23463002018-03-28 15:16:15 -04001097 *outRightType = &left;
1098 *outResultType = context.fBool_Type.get();
1099 return true;
1100 } if (left.canCoerceTo(right)) {
1101 *outLeftType = &right;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001102 *outRightType = &right;
1103 *outResultType = context.fBool_Type.get();
1104 return true;
1105 }
Ethan Nicholas23463002018-03-28 15:16:15 -04001106 return false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001107 case Token::LT: // fall through
1108 case Token::GT: // fall through
1109 case Token::LTEQ: // fall through
1110 case Token::GTEQ:
1111 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -07001112 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001113 break;
1114 case Token::LOGICALOR: // fall through
1115 case Token::LOGICALAND: // fall through
1116 case Token::LOGICALXOR: // fall through
1117 case Token::LOGICALOREQ: // fall through
1118 case Token::LOGICALANDEQ: // fall through
1119 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -07001120 *outLeftType = context.fBool_Type.get();
1121 *outRightType = context.fBool_Type.get();
1122 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001123 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -07001124 right.canCoerceTo(*context.fBool_Type);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001125 case Token::STAREQ:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001126 if (left.kind() == Type::kScalar_Kind) {
1127 *outLeftType = &left;
1128 *outRightType = &left;
1129 *outResultType = &left;
1130 return right.canCoerceTo(left);
1131 }
1132 // fall through
1133 case Token::STAR:
ethannicholasf789b382016-08-03 12:43:36 -07001134 if (is_matrix_multiply(left, right)) {
1135 // determine final component type
1136 if (determine_binary_type(context, Token::STAR, left.componentType(),
1137 right.componentType(), outLeftType, outRightType,
1138 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001139 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001140 left.rows());;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001141 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001142 right.rows());;
1143 int leftColumns = left.columns();
1144 int leftRows = left.rows();
1145 int rightColumns;
1146 int rightRows;
1147 if (right.kind() == Type::kVector_Kind) {
1148 // matrix * vector treats the vector as a column vector, so we need to
1149 // transpose it
1150 rightColumns = right.rows();
1151 rightRows = right.columns();
1152 ASSERT(rightColumns == 1);
1153 } else {
1154 rightColumns = right.columns();
1155 rightRows = right.rows();
1156 }
1157 if (rightColumns > 1) {
1158 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
1159 leftRows);
1160 } else {
1161 // result was a column vector, transpose it back to a row
1162 *outResultType = &(*outResultType)->toCompound(context, leftRows,
1163 rightColumns);
1164 }
1165 return leftColumns == rightRows;
1166 } else {
1167 return false;
1168 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001169 }
ethannicholasea4567c2016-10-17 11:24:37 -07001170 isLogical = false;
1171 validMatrixOrVectorOp = true;
1172 break;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001173 case Token::PLUSEQ:
1174 case Token::MINUSEQ:
1175 case Token::SLASHEQ:
1176 case Token::PERCENTEQ:
1177 case Token::SHLEQ:
1178 case Token::SHREQ:
1179 if (left.kind() == Type::kScalar_Kind) {
1180 *outLeftType = &left;
1181 *outRightType = &left;
1182 *outResultType = &left;
1183 return right.canCoerceTo(left);
1184 }
1185 // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001186 case Token::PLUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001187 case Token::MINUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001188 case Token::SLASH: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001189 isLogical = false;
1190 validMatrixOrVectorOp = true;
1191 break;
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001192 case Token::COMMA:
1193 *outLeftType = &left;
1194 *outRightType = &right;
1195 *outResultType = &right;
1196 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001197 default:
1198 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -07001199 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001200 }
ethannicholasea4567c2016-10-17 11:24:37 -07001201 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001202 if (left.kind() == Type::kScalar_Kind && right.kind() == Type::kScalar_Kind &&
1203 right.canCoerceTo(left)) {
1204 if (left.priority() > right.priority()) {
1205 *outLeftType = &left;
1206 *outRightType = &left;
1207 } else {
1208 *outLeftType = &right;
1209 *outRightType = &right;
1210 }
1211 if (isLogical) {
1212 *outResultType = context.fBool_Type.get();
1213 } else {
1214 *outResultType = &left;
1215 }
1216 return true;
1217 }
1218 if (right.canCoerceTo(left) && isVectorOrMatrix && validMatrixOrVectorOp) {
ethannicholasd598f792016-07-25 10:08:54 -07001219 *outLeftType = &left;
1220 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001221 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -07001222 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -07001223 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001224 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001225 }
1226 return true;
1227 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001228 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -07001229 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001230 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001231 outRightType, outResultType, false)) {
1232 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001233 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001234 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -07001235 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001236 }
1237 return true;
1238 }
1239 return false;
1240 }
1241 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001242 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001243 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07001244 }
1245 return false;
1246}
1247
ethannicholas08a92112016-11-09 13:26:45 -08001248std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
1249 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001250 const Expression& right) const {
Ethan Nicholascb670962017-04-20 19:31:52 -04001251 if (!left.isConstant() || !right.isConstant()) {
1252 return nullptr;
1253 }
ethannicholas08a92112016-11-09 13:26:45 -08001254 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
1255 // precision to calculate the results and hope the result makes sense. The plan is to move the
1256 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
1257 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -05001258 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001259 right.fKind == Expression::kBoolLiteral_Kind) {
1260 bool leftVal = ((BoolLiteral&) left).fValue;
1261 bool rightVal = ((BoolLiteral&) right).fValue;
1262 bool result;
1263 switch (op) {
1264 case Token::LOGICALAND: result = leftVal && rightVal; break;
1265 case Token::LOGICALOR: result = leftVal || rightVal; break;
1266 case Token::LOGICALXOR: result = leftVal ^ rightVal; break;
1267 default: return nullptr;
1268 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001269 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fOffset, result));
ethannicholas08a92112016-11-09 13:26:45 -08001270 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001271 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \
ethannicholas08a92112016-11-09 13:26:45 -08001272 leftVal op rightVal))
1273 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
1274 int64_t leftVal = ((IntLiteral&) left).fValue;
1275 int64_t rightVal = ((IntLiteral&) right).fValue;
1276 switch (op) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001277 case Token::PLUS: return RESULT(Int, +);
1278 case Token::MINUS: return RESULT(Int, -);
1279 case Token::STAR: return RESULT(Int, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001280 case Token::SLASH:
1281 if (rightVal) {
1282 return RESULT(Int, /);
1283 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001284 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001285 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001286 case Token::PERCENT:
1287 if (rightVal) {
1288 return RESULT(Int, %);
1289 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001290 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001291 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001292 case Token::BITWISEAND: return RESULT(Int, &);
1293 case Token::BITWISEOR: return RESULT(Int, |);
1294 case Token::BITWISEXOR: return RESULT(Int, ^);
1295 case Token::SHL: return RESULT(Int, <<);
1296 case Token::SHR: return RESULT(Int, >>);
1297 case Token::EQEQ: return RESULT(Bool, ==);
1298 case Token::NEQ: return RESULT(Bool, !=);
1299 case Token::GT: return RESULT(Bool, >);
1300 case Token::GTEQ: return RESULT(Bool, >=);
1301 case Token::LT: return RESULT(Bool, <);
1302 case Token::LTEQ: return RESULT(Bool, <=);
1303 default: return nullptr;
1304 }
1305 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001306 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001307 right.fKind == Expression::kFloatLiteral_Kind) {
1308 double leftVal = ((FloatLiteral&) left).fValue;
1309 double rightVal = ((FloatLiteral&) right).fValue;
1310 switch (op) {
1311 case Token::PLUS: return RESULT(Float, +);
1312 case Token::MINUS: return RESULT(Float, -);
1313 case Token::STAR: return RESULT(Float, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001314 case Token::SLASH:
1315 if (rightVal) {
1316 return RESULT(Float, /);
1317 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001318 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001319 return nullptr;
Ethan Nicholascb670962017-04-20 19:31:52 -04001320 case Token::EQEQ: return RESULT(Bool, ==);
1321 case Token::NEQ: return RESULT(Bool, !=);
1322 case Token::GT: return RESULT(Bool, >);
1323 case Token::GTEQ: return RESULT(Bool, >=);
1324 case Token::LT: return RESULT(Bool, <);
1325 case Token::LTEQ: return RESULT(Bool, <=);
ethannicholas08a92112016-11-09 13:26:45 -08001326 default: return nullptr;
1327 }
1328 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001329 if (left.fType.kind() == Type::kVector_Kind &&
1330 left.fType.componentType() == *fContext.fFloat_Type &&
1331 left.fType == right.fType) {
1332 ASSERT(left.fKind == Expression::kConstructor_Kind);
1333 ASSERT(right.fKind == Expression::kConstructor_Kind);
1334 std::vector<std::unique_ptr<Expression>> args;
1335 #define RETURN_VEC_COMPONENTWISE_RESULT(op) \
1336 for (int i = 0; i < left.fType.columns(); i++) { \
1337 float value = ((Constructor&) left).getFVecComponent(i) op \
1338 ((Constructor&) right).getFVecComponent(i); \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001339 args.emplace_back(new FloatLiteral(fContext, -1, value)); \
Ethan Nicholascb670962017-04-20 19:31:52 -04001340 } \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001341 return std::unique_ptr<Expression>(new Constructor(-1, left.fType, \
Ethan Nicholascb670962017-04-20 19:31:52 -04001342 std::move(args)));
1343 switch (op) {
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001344 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001345 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001346 left.compareConstant(fContext, right)));
1347 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001348 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001349 !left.compareConstant(fContext, right)));
Ethan Nicholascb670962017-04-20 19:31:52 -04001350 case Token::PLUS: RETURN_VEC_COMPONENTWISE_RESULT(+);
1351 case Token::MINUS: RETURN_VEC_COMPONENTWISE_RESULT(-);
1352 case Token::STAR: RETURN_VEC_COMPONENTWISE_RESULT(*);
1353 case Token::SLASH: RETURN_VEC_COMPONENTWISE_RESULT(/);
1354 default: return nullptr;
1355 }
1356 }
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001357 if (left.fType.kind() == Type::kMatrix_Kind &&
1358 right.fType.kind() == Type::kMatrix_Kind &&
1359 left.fKind == right.fKind) {
1360 switch (op) {
1361 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001362 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001363 left.compareConstant(fContext, right)));
1364 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001365 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001366 !left.compareConstant(fContext, right)));
1367 default:
1368 return nullptr;
1369 }
1370 }
ethannicholas08a92112016-11-09 13:26:45 -08001371 #undef RESULT
1372 return nullptr;
1373}
1374
ethannicholasb3058bd2016-07-01 08:22:01 -07001375std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
1376 const ASTBinaryExpression& expression) {
1377 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
1378 if (!left) {
1379 return nullptr;
1380 }
1381 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
1382 if (!right) {
1383 return nullptr;
1384 }
ethannicholasd598f792016-07-25 10:08:54 -07001385 const Type* leftType;
1386 const Type* rightType;
1387 const Type* resultType;
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001388 const Type* rawLeftType;
1389 if (left->fKind == Expression::kIntLiteral_Kind && right->fType.isInteger()) {
1390 rawLeftType = &right->fType;
1391 } else {
1392 rawLeftType = &left->fType;
1393 }
1394 const Type* rawRightType;
1395 if (right->fKind == Expression::kIntLiteral_Kind && left->fType.isInteger()) {
1396 rawRightType = &left->fType;
1397 } else {
1398 rawRightType = &right->fType;
1399 }
1400 if (!determine_binary_type(fContext, expression.fOperator, *rawLeftType, *rawRightType,
1401 &leftType, &rightType, &resultType,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001402 !Compiler::IsAssignment(expression.fOperator))) {
1403 fErrors.error(expression.fOffset, String("type mismatch: '") +
1404 Compiler::OperatorName(expression.fOperator) +
1405 "' cannot operate on '" + left->fType.fName +
1406 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001407 return nullptr;
1408 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001409 if (Compiler::IsAssignment(expression.fOperator)) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001410 this->setRefKind(*left, expression.fOperator != Token::EQ ?
1411 VariableReference::kReadWrite_RefKind :
1412 VariableReference::kWrite_RefKind);
ethannicholasea4567c2016-10-17 11:24:37 -07001413 }
1414 left = this->coerce(std::move(left), *leftType);
1415 right = this->coerce(std::move(right), *rightType);
1416 if (!left || !right) {
1417 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001418 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001419 std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator,
ethannicholas08a92112016-11-09 13:26:45 -08001420 *right.get());
1421 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001422 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001423 std::move(left),
1424 expression.fOperator,
1425 std::move(right),
1426 *resultType));
1427 }
1428 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001429}
1430
Ethan Nicholas11d53972016-11-28 11:23:23 -05001431std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
ethannicholasb3058bd2016-07-01 08:22:01 -07001432 const ASTTernaryExpression& expression) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001433 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -07001434 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07001435 if (!test) {
1436 return nullptr;
1437 }
1438 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
1439 if (!ifTrue) {
1440 return nullptr;
1441 }
1442 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
1443 if (!ifFalse) {
1444 return nullptr;
1445 }
ethannicholasd598f792016-07-25 10:08:54 -07001446 const Type* trueType;
1447 const Type* falseType;
1448 const Type* resultType;
1449 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001450 &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001451 fErrors.error(expression.fOffset, "ternary operator result mismatch: '" +
1452 ifTrue->fType.fName + "', '" +
1453 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001454 return nullptr;
1455 }
ethannicholasd598f792016-07-25 10:08:54 -07001456 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001457 if (!ifTrue) {
1458 return nullptr;
1459 }
ethannicholasd598f792016-07-25 10:08:54 -07001460 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001461 if (!ifFalse) {
1462 return nullptr;
1463 }
ethannicholas08a92112016-11-09 13:26:45 -08001464 if (test->fKind == Expression::kBoolLiteral_Kind) {
1465 // static boolean test, just return one of the branches
1466 if (((BoolLiteral&) *test).fValue) {
1467 return ifTrue;
1468 } else {
1469 return ifFalse;
1470 }
1471 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001472 return std::unique_ptr<Expression>(new TernaryExpression(expression.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001473 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001474 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -07001475 std::move(ifFalse)));
1476}
1477
Ethan Nicholas5338f992017-04-19 15:54:07 -04001478// scales the texture coordinates by the texture size for sampling rectangle textures.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001479// For float2coordinates, implements the transformation:
Ethan Nicholas5338f992017-04-19 15:54:07 -04001480// texture(sampler, coord) -> texture(sampler, textureSize(sampler) * coord)
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001481// For float3coordinates, implements the transformation:
1482// texture(sampler, coord) -> texture(sampler, float3textureSize(sampler), 1.0) * coord))
Ethan Nicholas5338f992017-04-19 15:54:07 -04001483void IRGenerator::fixRectSampling(std::vector<std::unique_ptr<Expression>>& arguments) {
1484 ASSERT(arguments.size() == 2);
1485 ASSERT(arguments[0]->fType == *fContext.fSampler2DRect_Type);
1486 ASSERT(arguments[0]->fKind == Expression::kVariableReference_Kind);
1487 const Variable& sampler = ((VariableReference&) *arguments[0]).fVariable;
1488 const Symbol* textureSizeSymbol = (*fSymbolTable)["textureSize"];
1489 ASSERT(textureSizeSymbol->fKind == Symbol::kFunctionDeclaration_Kind);
1490 const FunctionDeclaration& textureSize = (FunctionDeclaration&) *textureSizeSymbol;
1491 std::vector<std::unique_ptr<Expression>> sizeArguments;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001492 sizeArguments.emplace_back(new VariableReference(-1, sampler));
1493 std::unique_ptr<Expression> float2ize = call(-1, textureSize, std::move(sizeArguments));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001494 const Type& type = arguments[1]->fType;
1495 std::unique_ptr<Expression> scale;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001496 if (type == *fContext.fFloat2_Type) {
1497 scale = std::move(float2ize);
Ethan Nicholas5338f992017-04-19 15:54:07 -04001498 } else {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001499 ASSERT(type == *fContext.fFloat3_Type);
1500 std::vector<std::unique_ptr<Expression>> float3rguments;
1501 float3rguments.push_back(std::move(float2ize));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001502 float3rguments.emplace_back(new FloatLiteral(fContext, -1, 1.0));
1503 scale.reset(new Constructor(-1, *fContext.fFloat3_Type, std::move(float3rguments)));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001504 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001505 arguments[1].reset(new BinaryExpression(-1, std::move(scale), Token::STAR,
Ethan Nicholas5338f992017-04-19 15:54:07 -04001506 std::move(arguments[1]), type));
1507}
1508
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001509std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001510 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -07001511 std::vector<std::unique_ptr<Expression>> arguments) {
1512 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001513 String msg = "call to '" + function.fName + "' expected " +
Ethan Nicholas11d53972016-11-28 11:23:23 -05001514 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001515 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001516 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001517 msg += "s";
1518 }
ethannicholas5961bc92016-10-12 06:39:56 -07001519 msg += ", but found " + to_string((uint64_t) arguments.size());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001520 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001521 return nullptr;
1522 }
ethannicholas471e8942016-10-28 09:02:46 -07001523 std::vector<const Type*> types;
1524 const Type* returnType;
1525 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001526 String msg = "no match for " + function.fName + "(";
1527 String separator;
ethannicholas471e8942016-10-28 09:02:46 -07001528 for (size_t i = 0; i < arguments.size(); i++) {
1529 msg += separator;
1530 separator = ", ";
1531 msg += arguments[i]->fType.description();
1532 }
1533 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001534 fErrors.error(offset, msg);
ethannicholas471e8942016-10-28 09:02:46 -07001535 return nullptr;
1536 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001537 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001538 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001539 if (!arguments[i]) {
1540 return nullptr;
1541 }
ethannicholasd598f792016-07-25 10:08:54 -07001542 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001543 this->setRefKind(*arguments[i],
1544 function.fParameters[i]->fModifiers.fFlags & Modifiers::kIn_Flag ?
1545 VariableReference::kReadWrite_RefKind :
1546 VariableReference::kPointer_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001547 }
1548 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04001549 if (function.fBuiltin && function.fName == "texture" &&
1550 arguments[0]->fType == *fContext.fSampler2DRect_Type) {
1551 this->fixRectSampling(arguments);
1552 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001553 return std::unique_ptr<FunctionCall>(new FunctionCall(offset, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001554 std::move(arguments)));
1555}
1556
1557/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001558 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
1559 * particular meaning other than "lower costs are preferred". Returns INT_MAX if the call is not
1560 * valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001561 */
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001562int IRGenerator::callCost(const FunctionDeclaration& function,
1563 const std::vector<std::unique_ptr<Expression>>& arguments) {
ethannicholasd598f792016-07-25 10:08:54 -07001564 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001565 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001566 }
1567 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001568 std::vector<const Type*> types;
1569 const Type* ignored;
1570 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001571 return INT_MAX;
ethannicholas471e8942016-10-28 09:02:46 -07001572 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001573 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001574 int cost = arguments[i]->coercionCost(*types[i]);
1575 if (cost != INT_MAX) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001576 total += cost;
1577 } else {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001578 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001579 }
1580 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001581 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001582}
1583
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001584std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001585 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001586 std::vector<std::unique_ptr<Expression>> arguments) {
1587 if (functionValue->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001588 return this->convertConstructor(offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001589 ((TypeReference&) *functionValue).fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001590 std::move(arguments));
1591 }
1592 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001593 fErrors.error(offset, "'" + functionValue->description() + "' is not a function");
ethannicholasb3058bd2016-07-01 08:22:01 -07001594 return nullptr;
1595 }
1596 FunctionReference* ref = (FunctionReference*) functionValue.get();
1597 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -07001598 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001599 if (ref->fFunctions.size() > 1) {
1600 for (const auto& f : ref->fFunctions) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001601 int cost = this->callCost(*f, arguments);
1602 if (cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001603 bestCost = cost;
1604 best = f;
1605 }
1606 }
1607 if (best) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001608 return this->call(offset, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001609 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001610 String msg = "no match for " + ref->fFunctions[0]->fName + "(";
1611 String separator;
ethannicholasb3058bd2016-07-01 08:22:01 -07001612 for (size_t i = 0; i < arguments.size(); i++) {
1613 msg += separator;
1614 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -07001615 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -07001616 }
1617 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001618 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001619 return nullptr;
1620 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001621 return this->call(offset, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001622}
1623
Ethan Nicholas84645e32017-02-09 13:57:14 -05001624std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001625 int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001626 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001627 std::vector<std::unique_ptr<Expression>> args) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001628 ASSERT(type.isNumber());
1629 if (args.size() != 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001630 fErrors.error(offset, "invalid arguments to '" + type.description() +
1631 "' constructor, (expected exactly 1 argument, but found " +
1632 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -07001633 return nullptr;
1634 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001635 if (type == args[0]->fType) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001636 return std::move(args[0]);
1637 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001638 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kFloatLiteral_Kind) {
1639 double value = ((FloatLiteral&) *args[0]).fValue;
1640 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, value, &type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001641 }
1642 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kIntLiteral_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001643 int64_t value = ((IntLiteral&) *args[0]).fValue;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001644 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, (double) value,
1645 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001646 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001647 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
1648 type == *fContext.fUInt_Type)) {
1649 return std::unique_ptr<Expression>(new IntLiteral(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001650 offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001651 ((IntLiteral&) *args[0]).fValue,
1652 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001653 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001654 if (args[0]->fType == *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001655 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0));
1656 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001657 return std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001658 new TernaryExpression(offset, std::move(args[0]),
Ethan Nicholas84645e32017-02-09 13:57:14 -05001659 this->coerce(std::move(one), type),
1660 this->coerce(std::move(zero),
1661 type)));
1662 }
1663 if (!args[0]->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001664 fErrors.error(offset, "invalid argument to '" + type.description() +
1665 "' constructor (expected a number or bool, but found '" +
1666 args[0]->fType.description() + "')");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001667 return nullptr;
1668 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001669 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001670}
1671
1672int component_count(const Type& type) {
1673 switch (type.kind()) {
1674 case Type::kVector_Kind:
1675 return type.columns();
1676 case Type::kMatrix_Kind:
1677 return type.columns() * type.rows();
1678 default:
1679 return 1;
1680 }
1681}
1682
1683std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001684 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001685 const Type& type,
1686 std::vector<std::unique_ptr<Expression>> args) {
1687 ASSERT(type.kind() == Type::kVector_Kind || type.kind() == Type::kMatrix_Kind);
1688 if (type.kind() == Type::kMatrix_Kind && args.size() == 1 &&
1689 args[0]->fType.kind() == Type::kMatrix_Kind) {
1690 // matrix from matrix is always legal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001691 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001692 }
1693 int actual = 0;
1694 int expected = type.rows() * type.columns();
1695 if (args.size() != 1 || expected != component_count(args[0]->fType) ||
1696 type.componentType().isNumber() != args[0]->fType.componentType().isNumber()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001697 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001698 if (args[i]->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001699 if (type.componentType().isNumber() !=
1700 args[i]->fType.componentType().isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001701 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1702 "parameter to '" + type.description() +
1703 "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001704 return nullptr;
1705 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001706 actual += args[i]->fType.columns();
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001707 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
1708 actual += 1;
1709 if (type.kind() != Type::kScalar_Kind) {
1710 args[i] = this->coerce(std::move(args[i]), type.componentType());
1711 if (!args[i]) {
1712 return nullptr;
1713 }
1714 }
1715 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001716 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1717 "parameter to '" + type.description() + "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001718 return nullptr;
1719 }
1720 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001721 if (actual != 1 && actual != expected) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001722 fErrors.error(offset, "invalid arguments to '" + type.description() +
1723 "' constructor (expected " + to_string(expected) +
1724 " scalars, but found " + to_string(actual) + ")");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001725 return nullptr;
1726 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001727 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001728 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001729}
1730
Ethan Nicholas84645e32017-02-09 13:57:14 -05001731std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001732 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001733 const Type& type,
1734 std::vector<std::unique_ptr<Expression>> args) {
1735 // FIXME: add support for structs
1736 Type::Kind kind = type.kind();
1737 if (args.size() == 1 && args[0]->fType == type) {
1738 // argument is already the right type, just return it
1739 return std::move(args[0]);
1740 }
1741 if (type.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001742 return this->convertNumberConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001743 } else if (kind == Type::kArray_Kind) {
1744 const Type& base = type.componentType();
1745 for (size_t i = 0; i < args.size(); i++) {
1746 args[i] = this->coerce(std::move(args[i]), base);
1747 if (!args[i]) {
1748 return nullptr;
1749 }
1750 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001751 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001752 } else if (kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001753 return this->convertCompoundConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001754 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001755 fErrors.error(offset, "cannot construct '" + type.description() + "'");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001756 return nullptr;
1757 }
1758}
1759
ethannicholasb3058bd2016-07-01 08:22:01 -07001760std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1761 const ASTPrefixExpression& expression) {
1762 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1763 if (!base) {
1764 return nullptr;
1765 }
1766 switch (expression.fOperator) {
1767 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001768 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001769 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001770 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001771 return nullptr;
1772 }
1773 return base;
1774 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001775 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001776 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001777 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001778 return nullptr;
1779 }
1780 if (base->fKind == Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001781 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001782 -((IntLiteral&) *base).fValue));
1783 }
1784 if (base->fKind == Expression::kFloatLiteral_Kind) {
1785 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001786 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001787 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001788 }
1789 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1790 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001791 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001792 fErrors.error(expression.fOffset,
1793 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001794 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001795 return nullptr;
1796 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001797 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001798 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001799 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001800 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001801 fErrors.error(expression.fOffset,
1802 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001803 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001804 return nullptr;
1805 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001806 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001807 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001808 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001809 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001810 fErrors.error(expression.fOffset,
1811 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001812 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001813 return nullptr;
1814 }
ethannicholas08a92112016-11-09 13:26:45 -08001815 if (base->fKind == Expression::kBoolLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001816 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001817 !((BoolLiteral&) *base).fValue));
1818 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001819 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001820 case Token::BITWISENOT:
1821 if (base->fType != *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001822 fErrors.error(expression.fOffset,
1823 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholas5961bc92016-10-12 06:39:56 -07001824 "' cannot operate on '" + base->fType.description() + "'");
1825 return nullptr;
1826 }
1827 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001828 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07001829 ABORT("unsupported prefix operator\n");
1830 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001831 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
ethannicholasb3058bd2016-07-01 08:22:01 -07001832 std::move(base)));
1833}
1834
1835std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1836 const ASTExpression& index) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05001837 if (base->fKind == Expression::kTypeReference_Kind) {
1838 if (index.fKind == ASTExpression::kInt_Kind) {
1839 const Type& oldType = ((TypeReference&) *base).fValue;
1840 int64_t size = ((const ASTIntLiteral&) index).fValue;
1841 Type* newType = new Type(oldType.name() + "[" + to_string(size) + "]",
1842 Type::kArray_Kind, oldType, size);
1843 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001844 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
Ethan Nicholas50afc172017-02-16 14:49:57 -05001845 *newType));
1846
1847 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001848 fErrors.error(base->fOffset, "array size must be a constant");
Ethan Nicholas50afc172017-02-16 14:49:57 -05001849 return nullptr;
1850 }
1851 }
ethannicholas5961bc92016-10-12 06:39:56 -07001852 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1853 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001854 fErrors.error(base->fOffset, "expected array, but found '" + base->fType.description() +
1855 "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001856 return nullptr;
1857 }
1858 std::unique_ptr<Expression> converted = this->convertExpression(index);
1859 if (!converted) {
1860 return nullptr;
1861 }
ethannicholas5961bc92016-10-12 06:39:56 -07001862 if (converted->fType != *fContext.fUInt_Type) {
1863 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1864 if (!converted) {
1865 return nullptr;
1866 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001867 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001868 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07001869 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001870}
1871
1872std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001873 StringFragment field) {
ethannicholasd598f792016-07-25 10:08:54 -07001874 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001875 for (size_t i = 0; i < fields.size(); i++) {
1876 if (fields[i].fName == field) {
1877 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1878 }
1879 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001880 fErrors.error(base->fOffset, "type '" + base->fType.description() + "' does not have a "
1881 "field named '" + field + "");
ethannicholasb3058bd2016-07-01 08:22:01 -07001882 return nullptr;
1883}
1884
1885std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001886 StringFragment fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001887 if (base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001888 fErrors.error(base->fOffset, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001889 return nullptr;
1890 }
1891 std::vector<int> swizzleComponents;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001892 for (size_t i = 0; i < fields.fLength; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001893 switch (fields[i]) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001894 case 'x': // fall through
1895 case 'r': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001896 case 's':
ethannicholasb3058bd2016-07-01 08:22:01 -07001897 swizzleComponents.push_back(0);
1898 break;
1899 case 'y': // fall through
1900 case 'g': // fall through
1901 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001902 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001903 swizzleComponents.push_back(1);
1904 break;
1905 }
1906 // fall through
1907 case 'z': // fall through
1908 case 'b': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001909 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001910 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001911 swizzleComponents.push_back(2);
1912 break;
1913 }
1914 // fall through
1915 case 'w': // fall through
1916 case 'a': // fall through
1917 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001918 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001919 swizzleComponents.push_back(3);
1920 break;
1921 }
1922 // fall through
1923 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001924 fErrors.error(base->fOffset, String::printf("invalid swizzle component '%c'",
1925 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07001926 return nullptr;
1927 }
1928 }
1929 ASSERT(swizzleComponents.size() > 0);
1930 if (swizzleComponents.size() > 4) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001931 fErrors.error(base->fOffset, "too many components in swizzle mask '" + fields + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001932 return nullptr;
1933 }
ethannicholasd598f792016-07-25 10:08:54 -07001934 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001935}
1936
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001937std::unique_ptr<Expression> IRGenerator::getCap(int offset, String name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001938 auto found = fCapsMap.find(name);
1939 if (found == fCapsMap.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001940 fErrors.error(offset, "unknown capability flag '" + name + "'");
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001941 return nullptr;
1942 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001943 String fullName = "sk_Caps." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001944 return std::unique_ptr<Expression>(new Setting(offset, fullName,
1945 found->second.literal(fContext, offset)));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001946}
1947
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001948std::unique_ptr<Expression> IRGenerator::getArg(int offset, String name) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001949 auto found = fSettings->fArgs.find(name);
1950 if (found == fSettings->fArgs.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001951 fErrors.error(offset, "unknown argument '" + name + "'");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001952 return nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001953 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001954 String fullName = "sk_Args." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001955 return std::unique_ptr<Expression>(new Setting(offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001956 fullName,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001957 found->second.literal(fContext, offset)));
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001958}
1959
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001960std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type,
1961 StringFragment field) {
1962 std::unique_ptr<Expression> result;
1963 for (const auto& e : *fProgramElements) {
1964 if (e->fKind == ProgramElement::kEnum_Kind && type.name() == ((Enum&) *e).fTypeName) {
1965 std::shared_ptr<SymbolTable> old = fSymbolTable;
1966 fSymbolTable = ((Enum&) *e).fSymbols;
1967 result = convertIdentifier(ASTIdentifier(offset, field));
1968 fSymbolTable = old;
1969 }
1970 }
1971 if (!result) {
1972 fErrors.error(offset, "type '" + type.fName + "' does not have a field named '" + field +
1973 "'");
1974 }
1975 return result;
1976}
1977
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001978std::unique_ptr<Expression> IRGenerator::convertAppend(int offset,
1979 const std::vector<std::unique_ptr<ASTExpression>>& args) {
1980#ifndef SKSL_STANDALONE
1981 if (args.size() < 2) {
1982 fErrors.error(offset, "'append' requires at least two arguments");
1983 return nullptr;
1984 }
1985 std::unique_ptr<Expression> pipeline = this->convertExpression(*args[0]);
1986 if (!pipeline) {
1987 return nullptr;
1988 }
1989 if (pipeline->fType != *fContext.fSkRasterPipeline_Type) {
1990 fErrors.error(offset, "first argument of 'append' must have type 'SkRasterPipeline'");
1991 return nullptr;
1992 }
1993 if (ASTExpression::kIdentifier_Kind != args[1]->fKind) {
1994 fErrors.error(offset, "'" + args[1]->description() + "' is not a valid stage");
1995 return nullptr;
1996 }
1997 StringFragment name = ((const ASTIdentifier&) *args[1]).fText;
1998 SkRasterPipeline::StockStage stage = SkRasterPipeline::premul;
1999 std::vector<std::unique_ptr<Expression>> stageArgs;
2000 stageArgs.push_back(std::move(pipeline));
2001 for (size_t i = 2; i < args.size(); ++i) {
2002 std::unique_ptr<Expression> arg = this->convertExpression(*args[i]);
2003 if (!arg) {
2004 return nullptr;
2005 }
2006 stageArgs.push_back(std::move(arg));
2007 }
2008 size_t expectedArgs = 0;
2009 // FIXME use a map
2010 if ("premul" == name) {
2011 stage = SkRasterPipeline::premul;
2012 }
2013 else if ("unpremul" == name) {
2014 stage = SkRasterPipeline::unpremul;
2015 }
2016 else if ("clamp_0" == name) {
2017 stage = SkRasterPipeline::clamp_0;
2018 }
2019 else if ("clamp_1" == name) {
2020 stage = SkRasterPipeline::clamp_1;
2021 }
2022 else if ("matrix_4x5" == name) {
2023 expectedArgs = 1;
2024 stage = SkRasterPipeline::matrix_4x5;
2025 if (1 == stageArgs.size() && stageArgs[0]->fType.fName != "float[20]") {
2026 fErrors.error(offset, "pipeline stage '" + name + "' expected a float[20] argument");
2027 return nullptr;
2028 }
2029 }
2030 else {
2031 bool found = false;
2032 for (const auto& e : *fProgramElements) {
2033 if (ProgramElement::kFunction_Kind == e->fKind) {
2034 const FunctionDefinition& f = (const FunctionDefinition&) *e;
2035 if (f.fDeclaration.fName == name) {
2036 stage = SkRasterPipeline::callback;
2037 std::vector<const FunctionDeclaration*> functions = { &f.fDeclaration };
2038 stageArgs.emplace_back(new FunctionReference(fContext, offset, functions));
2039 found = true;
2040 break;
2041 }
2042 }
2043 }
2044 if (!found) {
2045 fErrors.error(offset, "'" + name + "' is not a valid pipeline stage");
2046 return nullptr;
2047 }
2048 }
2049 if (args.size() != expectedArgs + 2) {
2050 fErrors.error(offset, "pipeline stage '" + name + "' expected an additional argument " +
2051 "count of " + to_string((int) expectedArgs) + ", but found " +
2052 to_string((int) args.size() - 1));
2053 return nullptr;
2054 }
2055 return std::unique_ptr<Expression>(new AppendStage(fContext, offset, stage,
2056 std::move(stageArgs)));
2057#else
2058 ASSERT(false);
2059 return nullptr;
2060#endif
2061}
2062
ethannicholasb3058bd2016-07-01 08:22:01 -07002063std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
2064 const ASTSuffixExpression& expression) {
2065 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
2066 if (!base) {
2067 return nullptr;
2068 }
2069 switch (expression.fSuffix->fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07002070 case ASTSuffix::kIndex_Kind: {
2071 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
2072 if (expr) {
2073 return this->convertIndex(std::move(base), *expr);
2074 } else if (base->fKind == Expression::kTypeReference_Kind) {
2075 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05002076 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
ethannicholas5961bc92016-10-12 06:39:56 -07002077 -1);
2078 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002079 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
ethannicholas5961bc92016-10-12 06:39:56 -07002080 *newType));
2081 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002082 fErrors.error(expression.fOffset, "'[]' must follow a type name");
ethannicholasa54401d2016-10-14 08:37:32 -07002083 return nullptr;
ethannicholas5961bc92016-10-12 06:39:56 -07002084 }
2085 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002086 case ASTSuffix::kCall_Kind: {
2087 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04002088 if (Expression::kFunctionReference_Kind == base->fKind &&
2089 "append" == ((const FunctionReference&) *base).fFunctions[0]->fName) {
2090 return convertAppend(expression.fOffset, *rawArguments);
2091 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002092 std::vector<std::unique_ptr<Expression>> arguments;
2093 for (size_t i = 0; i < rawArguments->size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05002094 std::unique_ptr<Expression> converted =
ethannicholasb3058bd2016-07-01 08:22:01 -07002095 this->convertExpression(*(*rawArguments)[i]);
2096 if (!converted) {
2097 return nullptr;
2098 }
2099 arguments.push_back(std::move(converted));
2100 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002101 return this->call(expression.fOffset, std::move(base), std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07002102 }
2103 case ASTSuffix::kField_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002104 StringFragment field = ((ASTFieldSuffix&) *expression.fSuffix).fField;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002105 if (base->fType == *fContext.fSkCaps_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002106 return this->getCap(expression.fOffset, field);
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002107 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002108 if (base->fType == *fContext.fSkArgs_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002109 return this->getArg(expression.fOffset, field);
2110 }
2111 if (base->fKind == Expression::kTypeReference_Kind) {
2112 return this->convertTypeField(base->fOffset, ((TypeReference&) *base).fValue,
2113 field);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002114 }
ethannicholasd598f792016-07-25 10:08:54 -07002115 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002116 case Type::kVector_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002117 return this->convertSwizzle(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002118 case Type::kStruct_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002119 return this->convertField(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002120 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002121 fErrors.error(base->fOffset, "cannot swizzle value of type '" +
2122 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002123 return nullptr;
2124 }
2125 }
2126 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002127 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002128 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002129 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002130 return nullptr;
2131 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002132 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002133 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002134 Token::PLUSPLUS));
2135 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002136 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002137 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002138 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002139 return nullptr;
2140 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002141 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002142 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002143 Token::MINUSMINUS));
2144 default:
2145 ABORT("unsupported suffix operator");
2146 }
2147}
2148
2149void IRGenerator::checkValid(const Expression& expr) {
2150 switch (expr.fKind) {
2151 case Expression::kFunctionReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002152 fErrors.error(expr.fOffset, "expected '(' to begin function call");
ethannicholasb3058bd2016-07-01 08:22:01 -07002153 break;
2154 case Expression::kTypeReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002155 fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation");
ethannicholasb3058bd2016-07-01 08:22:01 -07002156 break;
2157 default:
ethannicholasea4567c2016-10-17 11:24:37 -07002158 if (expr.fType == *fContext.fInvalid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002159 fErrors.error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07002160 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002161 }
2162}
2163
ethannicholasb3058bd2016-07-01 08:22:01 -07002164static bool has_duplicates(const Swizzle& swizzle) {
2165 int bits = 0;
2166 for (int idx : swizzle.fComponents) {
2167 ASSERT(idx >= 0 && idx <= 3);
2168 int bit = 1 << idx;
2169 if (bits & bit) {
2170 return true;
2171 }
2172 bits |= bit;
2173 }
2174 return false;
2175}
2176
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002177void IRGenerator::setRefKind(const Expression& expr, VariableReference::RefKind kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002178 switch (expr.fKind) {
2179 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07002180 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07002181 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002182 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002183 "cannot modify immutable variable '" + var.fName + "'");
2184 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002185 ((VariableReference&) expr).setRefKind(kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002186 break;
2187 }
2188 case Expression::kFieldAccess_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002189 this->setRefKind(*((FieldAccess&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002190 break;
2191 case Expression::kSwizzle_Kind:
2192 if (has_duplicates((Swizzle&) expr)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002193 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002194 "cannot write to the same swizzle field more than once");
2195 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002196 this->setRefKind(*((Swizzle&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002197 break;
2198 case Expression::kIndex_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002199 this->setRefKind(*((IndexExpression&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002200 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -05002201 case Expression::kTernary_Kind: {
2202 TernaryExpression& t = (TernaryExpression&) expr;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002203 this->setRefKind(*t.fIfTrue, kind);
2204 this->setRefKind(*t.fIfFalse, kind);
Ethan Nicholasa583b812018-01-18 13:32:11 -05002205 break;
2206 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002207 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002208 fErrors.error(expr.fOffset, "cannot assign to '" + expr.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002209 break;
2210 }
2211}
2212
Robert Phillipsfe8da172018-01-24 14:52:02 +00002213void IRGenerator::convertProgram(Program::Kind kind,
2214 const char* text,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002215 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002216 SymbolTable& types,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002217 std::vector<std::unique_ptr<ProgramElement>>* out) {
Robert Phillipsfe8da172018-01-24 14:52:02 +00002218 fKind = kind;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002219 fProgramElements = out;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002220 Parser parser(text, length, types, fErrors);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002221 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
2222 if (fErrors.errorCount()) {
2223 return;
2224 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002225 for (size_t i = 0; i < parsed.size(); i++) {
2226 ASTDeclaration& decl = *parsed[i];
2227 switch (decl.fKind) {
2228 case ASTDeclaration::kVar_Kind: {
2229 std::unique_ptr<VarDeclarations> s = this->convertVarDeclarations(
2230 (ASTVarDeclarations&) decl,
2231 Variable::kGlobal_Storage);
2232 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002233 fProgramElements->push_back(std::move(s));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002234 }
2235 break;
2236 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002237 case ASTDeclaration::kEnum_Kind: {
2238 this->convertEnum((ASTEnum&) decl);
2239 break;
2240 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002241 case ASTDeclaration::kFunction_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002242 this->convertFunction((ASTFunction&) decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002243 break;
2244 }
2245 case ASTDeclaration::kModifiers_Kind: {
2246 std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(
2247 (ASTModifiersDeclaration&) decl);
2248 if (f) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002249 fProgramElements->push_back(std::move(f));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002250 }
2251 break;
2252 }
2253 case ASTDeclaration::kInterfaceBlock_Kind: {
2254 std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(
2255 (ASTInterfaceBlock&) decl);
2256 if (i) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002257 fProgramElements->push_back(std::move(i));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002258 }
2259 break;
2260 }
2261 case ASTDeclaration::kExtension_Kind: {
2262 std::unique_ptr<Extension> e = this->convertExtension((ASTExtension&) decl);
2263 if (e) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002264 fProgramElements->push_back(std::move(e));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002265 }
2266 break;
2267 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002268 case ASTDeclaration::kSection_Kind: {
2269 std::unique_ptr<Section> s = this->convertSection((ASTSection&) decl);
2270 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002271 fProgramElements->push_back(std::move(s));
Ethan Nicholas762466e2017-06-29 10:03:38 -04002272 }
2273 break;
2274 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002275 default:
2276 ABORT("unsupported declaration: %s\n", decl.description().c_str());
2277 }
2278 }
2279}
2280
2281
ethannicholasb3058bd2016-07-01 08:22:01 -07002282}