blob: 5ab04abbaaadd0254fa6f79a175ed722ab650dd6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
10#include "limits.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050011#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/sksl/SkSLCompiler.h"
14#include "src/sksl/SkSLParser.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/sksl/ir/SkSLBinaryExpression.h"
16#include "src/sksl/ir/SkSLBoolLiteral.h"
17#include "src/sksl/ir/SkSLBreakStatement.h"
18#include "src/sksl/ir/SkSLConstructor.h"
19#include "src/sksl/ir/SkSLContinueStatement.h"
20#include "src/sksl/ir/SkSLDiscardStatement.h"
21#include "src/sksl/ir/SkSLDoStatement.h"
22#include "src/sksl/ir/SkSLEnum.h"
23#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas9e6a3932019-05-17 16:31:21 -040024#include "src/sksl/ir/SkSLExternalFunctionCall.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040025#include "src/sksl/ir/SkSLExternalValueReference.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/sksl/ir/SkSLField.h"
27#include "src/sksl/ir/SkSLFieldAccess.h"
28#include "src/sksl/ir/SkSLFloatLiteral.h"
29#include "src/sksl/ir/SkSLForStatement.h"
30#include "src/sksl/ir/SkSLFunctionCall.h"
31#include "src/sksl/ir/SkSLFunctionDeclaration.h"
32#include "src/sksl/ir/SkSLFunctionDefinition.h"
33#include "src/sksl/ir/SkSLFunctionReference.h"
34#include "src/sksl/ir/SkSLIfStatement.h"
35#include "src/sksl/ir/SkSLIndexExpression.h"
36#include "src/sksl/ir/SkSLIntLiteral.h"
37#include "src/sksl/ir/SkSLInterfaceBlock.h"
38#include "src/sksl/ir/SkSLLayout.h"
Chris Daltonb0fd4b12019-10-29 13:41:22 -060039#include "src/sksl/ir/SkSLNop.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/sksl/ir/SkSLNullLiteral.h"
41#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
43#include "src/sksl/ir/SkSLReturnStatement.h"
44#include "src/sksl/ir/SkSLSetting.h"
45#include "src/sksl/ir/SkSLSwitchCase.h"
46#include "src/sksl/ir/SkSLSwitchStatement.h"
47#include "src/sksl/ir/SkSLSwizzle.h"
48#include "src/sksl/ir/SkSLTernaryExpression.h"
49#include "src/sksl/ir/SkSLUnresolvedFunction.h"
50#include "src/sksl/ir/SkSLVarDeclarations.h"
51#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
52#include "src/sksl/ir/SkSLVariable.h"
53#include "src/sksl/ir/SkSLVariableReference.h"
54#include "src/sksl/ir/SkSLWhileStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070055
56namespace SkSL {
57
58class AutoSymbolTable {
59public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050060 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070061 : fIR(ir)
62 , fPrevious(fIR->fSymbolTable) {
63 fIR->pushSymbolTable();
64 }
65
66 ~AutoSymbolTable() {
67 fIR->popSymbolTable();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040068 SkASSERT(fPrevious == fIR->fSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -070069 }
70
71 IRGenerator* fIR;
72 std::shared_ptr<SymbolTable> fPrevious;
73};
74
ethannicholas22f939e2016-10-13 13:25:34 -070075class AutoLoopLevel {
76public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050077 AutoLoopLevel(IRGenerator* ir)
ethannicholas22f939e2016-10-13 13:25:34 -070078 : fIR(ir) {
79 fIR->fLoopLevel++;
80 }
81
82 ~AutoLoopLevel() {
83 fIR->fLoopLevel--;
84 }
85
86 IRGenerator* fIR;
87};
88
Ethan Nicholasaf197692017-02-27 13:26:45 -050089class AutoSwitchLevel {
90public:
91 AutoSwitchLevel(IRGenerator* ir)
92 : fIR(ir) {
93 fIR->fSwitchLevel++;
94 }
95
96 ~AutoSwitchLevel() {
97 fIR->fSwitchLevel--;
98 }
99
100 IRGenerator* fIR;
101};
102
Ethan Nicholas11d53972016-11-28 11:23:23 -0500103IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
ethannicholasb3058bd2016-07-01 08:22:01 -0700104 ErrorReporter& errorReporter)
ethannicholasd598f792016-07-25 10:08:54 -0700105: fContext(*context)
106, fCurrentFunction(nullptr)
Ethan Nicholas762466e2017-06-29 10:03:38 -0400107, fRootSymbolTable(symbolTable)
108, fSymbolTable(symbolTable)
ethannicholas22f939e2016-10-13 13:25:34 -0700109, fLoopLevel(0)
Ethan Nicholasaf197692017-02-27 13:26:45 -0500110, fSwitchLevel(0)
ethannicholasd598f792016-07-25 10:08:54 -0700111, fErrors(errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700112
113void IRGenerator::pushSymbolTable() {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400114 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), &fErrors));
ethannicholasb3058bd2016-07-01 08:22:01 -0700115}
116
117void IRGenerator::popSymbolTable() {
118 fSymbolTable = fSymbolTable->fParent;
119}
120
Ethan Nicholas762466e2017-06-29 10:03:38 -0400121static void fill_caps(const SKSL_CAPS_CLASS& caps,
122 std::unordered_map<String, Program::Settings::Value>* capsMap) {
Brian Salomon23356442018-11-30 15:33:19 -0500123#define CAP(name) \
124 capsMap->insert(std::make_pair(String(#name), Program::Settings::Value(caps.name())))
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500125 CAP(fbFetchSupport);
Brian Salomond4013302018-04-04 13:58:33 +0000126 CAP(fbFetchNeedsCustomOutput);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500127 CAP(flatInterpolationSupport);
128 CAP(noperspectiveInterpolationSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500129 CAP(externalTextureSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500130 CAP(mustEnableAdvBlendEqs);
131 CAP(mustEnableSpecificAdvBlendEqs);
132 CAP(mustDeclareFragmentShaderOutput);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400133 CAP(mustDoOpBetweenFloorAndAbs);
Brian Salomonf8c187c2019-12-19 14:41:57 -0500134 CAP(mustGuardDivisionEvenAfterExplicitZeroCheck);
135 CAP(inBlendModesFailRandomlyForAllZeroVec);
Michael Ludwig24d438b2018-09-12 15:22:50 -0400136 CAP(atan2ImplementedAsAtanYOverX);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500137 CAP(canUseAnyFunctionInShader);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700138 CAP(floatIs32Bits);
Ethan Nicholas07990de2017-07-18 09:47:43 -0400139 CAP(integerSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500140#undef CAP
141}
142
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400143void IRGenerator::start(const Program::Settings* settings,
144 std::vector<std::unique_ptr<ProgramElement>>* inherited) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500145 fSettings = settings;
146 fCapsMap.clear();
147 if (settings->fCaps) {
148 fill_caps(*settings->fCaps, &fCapsMap);
Ethan Nicholas00543112018-07-31 09:44:36 -0400149 } else {
150 fCapsMap.insert(std::make_pair(String("integerSupport"),
151 Program::Settings::Value(true)));
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500152 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500153 this->pushSymbolTable();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400154 fInvocations = -1;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500155 fInputs.reset();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000156 fSkPerVertex = nullptr;
157 fRTAdjust = nullptr;
158 fRTAdjustInterfaceBlock = nullptr;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400159 if (inherited) {
160 for (const auto& e : *inherited) {
161 if (e->fKind == ProgramElement::kInterfaceBlock_Kind) {
162 InterfaceBlock& intf = (InterfaceBlock&) *e;
163 if (intf.fVariable.fName == Compiler::PERVERTEX_NAME) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400164 SkASSERT(!fSkPerVertex);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -0400165 fSkPerVertex = &intf.fVariable;
166 }
167 }
168 }
169 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500170 SkASSERT(fIntrinsics);
171 for (auto& pair : *fIntrinsics) {
172 pair.second.second = false;
173 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500174}
175
Ethan Nicholasfc994162019-06-06 10:04:27 -0400176std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, StringFragment name) {
177 return std::unique_ptr<Extension>(new Extension(offset, name));
ethannicholasb3058bd2016-07-01 08:22:01 -0700178}
179
Ethan Nicholasfc994162019-06-06 10:04:27 -0400180void IRGenerator::finish() {
181 this->popSymbolTable();
182 fSettings = nullptr;
183}
184
185std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700186 switch (statement.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400187 case ASTNode::Kind::kBlock:
188 return this->convertBlock(statement);
189 case ASTNode::Kind::kVarDeclarations:
190 return this->convertVarDeclarationStatement(statement);
191 case ASTNode::Kind::kIf:
192 return this->convertIf(statement);
193 case ASTNode::Kind::kFor:
194 return this->convertFor(statement);
195 case ASTNode::Kind::kWhile:
196 return this->convertWhile(statement);
197 case ASTNode::Kind::kDo:
198 return this->convertDo(statement);
199 case ASTNode::Kind::kSwitch:
200 return this->convertSwitch(statement);
201 case ASTNode::Kind::kReturn:
202 return this->convertReturn(statement);
203 case ASTNode::Kind::kBreak:
204 return this->convertBreak(statement);
205 case ASTNode::Kind::kContinue:
206 return this->convertContinue(statement);
207 case ASTNode::Kind::kDiscard:
208 return this->convertDiscard(statement);
209 default:
210 // it's an expression
211 std::unique_ptr<Statement> result = this->convertExpressionStatement(statement);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000212 if (fRTAdjust && Program::kGeometry_Kind == fKind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400213 SkASSERT(result->fKind == Statement::kExpression_Kind);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000214 Expression& expr = *((ExpressionStatement&) *result).fExpression;
215 if (expr.fKind == Expression::kFunctionCall_Kind) {
216 FunctionCall& fc = (FunctionCall&) expr;
217 if (fc.fFunction.fBuiltin && fc.fFunction.fName == "EmitVertex") {
218 std::vector<std::unique_ptr<Statement>> statements;
219 statements.push_back(getNormalizeSkPositionCode());
220 statements.push_back(std::move(result));
221 return std::unique_ptr<Block>(new Block(statement.fOffset,
222 std::move(statements),
223 fSymbolTable));
224 }
225 }
226 }
227 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700228 }
229}
230
Ethan Nicholasfc994162019-06-06 10:04:27 -0400231std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
232 SkASSERT(block.fKind == ASTNode::Kind::kBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 AutoSymbolTable table(this);
234 std::vector<std::unique_ptr<Statement>> statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400235 for (const auto& child : block) {
236 std::unique_ptr<Statement> statement = this->convertStatement(child);
ethannicholasb3058bd2016-07-01 08:22:01 -0700237 if (!statement) {
238 return nullptr;
239 }
240 statements.push_back(std::move(statement));
241 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700242 return std::unique_ptr<Block>(new Block(block.fOffset, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700243}
244
Ethan Nicholasfc994162019-06-06 10:04:27 -0400245std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(const ASTNode& s) {
246 SkASSERT(s.fKind == ASTNode::Kind::kVarDeclarations);
247 auto decl = this->convertVarDeclarations(s, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700248 if (!decl) {
249 return nullptr;
250 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700251 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700252}
253
Ethan Nicholasfc994162019-06-06 10:04:27 -0400254std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTNode& decls,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700255 Variable::Storage storage) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400256 SkASSERT(decls.fKind == ASTNode::Kind::kVarDeclarations);
257 auto iter = decls.begin();
258 const Modifiers& modifiers = iter++->getModifiers();
259 const ASTNode& rawType = *(iter++);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000260 std::vector<std::unique_ptr<VarDeclaration>> variables;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400261 const Type* baseType = this->convertType(rawType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700262 if (!baseType) {
263 return nullptr;
264 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500265 if (fKind != Program::kFragmentProcessor_Kind) {
266 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
267 baseType->kind() == Type::Kind::kMatrix_Kind) {
268 fErrors.error(decls.fOffset, "'in' variables may not have matrix type");
269 }
Brian Osman088913a2019-12-19 15:44:56 -0500270 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
271 (modifiers.fFlags & Modifiers::kUniform_Flag)) {
272 fErrors.error(decls.fOffset,
273 "'in uniform' variables only permitted within fragment processors");
274 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500275 if (modifiers.fLayout.fWhen.fLength) {
276 fErrors.error(decls.fOffset, "'when' is only permitted within fragment processors");
277 }
278 if (modifiers.fLayout.fFlags & Layout::kTracked_Flag) {
279 fErrors.error(decls.fOffset, "'tracked' is only permitted within fragment processors");
280 }
281 if (modifiers.fLayout.fCType != Layout::CType::kDefault) {
282 fErrors.error(decls.fOffset, "'ctype' is only permitted within fragment processors");
283 }
284 if (modifiers.fLayout.fKey) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400285 fErrors.error(decls.fOffset, "'key' is only permitted within fragment processors");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400286 }
Brian Osman2fe83fe2019-12-16 13:17:59 -0500287 }
288 if (modifiers.fLayout.fKey && (modifiers.fFlags & Modifiers::kUniform_Flag)) {
289 fErrors.error(decls.fOffset, "'key' is not permitted on 'uniform' variables");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400290 }
Brian Osmanf59a9612020-04-15 14:18:13 -0400291 if (modifiers.fLayout.fMarker.fLength) {
292 if (fKind != Program::kPipelineStage_Kind) {
293 fErrors.error(decls.fOffset, "'marker' is only permitted in runtime effects");
294 }
295 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
296 fErrors.error(decls.fOffset, "'marker' is only permitted on 'uniform' variables");
297 }
298 if (*baseType != *fContext.fFloat4x4_Type) {
299 fErrors.error(decls.fOffset, "'marker' is only permitted on float4x4 variables");
300 }
301 }
Brian Osmanb32d66b2020-04-30 17:12:03 -0400302 if (modifiers.fLayout.fFlags & Layout::kSRGBUnpremul_Flag) {
303 if (fKind != Program::kPipelineStage_Kind) {
304 fErrors.error(decls.fOffset, "'srgb_unpremul' is only permitted in runtime effects");
305 }
306 if (!(modifiers.fFlags & Modifiers::kUniform_Flag)) {
307 fErrors.error(decls.fOffset,
308 "'srgb_unpremul' is only permitted on 'uniform' variables");
309 }
310 auto validColorXformType = [](const Type& t) {
311 return t.kind() == Type::kVector_Kind && t.componentType().isFloat() &&
312 (t.columns() == 3 || t.columns() == 4);
313 };
314 if (!validColorXformType(*baseType) && !(baseType->kind() == Type::kArray_Kind &&
315 validColorXformType(baseType->componentType()))) {
316 fErrors.error(decls.fOffset,
317 "'srgb_unpremul' is only permitted on half3, half4, float3, or float4 "
318 "variables");
319 }
320 }
Brian Osman3c358422020-03-23 10:44:12 -0400321 if (modifiers.fFlags & Modifiers::kVarying_Flag) {
322 if (fKind != Program::kPipelineStage_Kind) {
323 fErrors.error(decls.fOffset, "'varying' is only permitted in runtime effects");
324 }
325 if (!baseType->isFloat() &&
326 !(baseType->kind() == Type::kVector_Kind && baseType->componentType().isFloat())) {
327 fErrors.error(decls.fOffset, "'varying' must be float scalar or vector");
328 }
329 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400330 for (; iter != decls.end(); ++iter) {
331 const ASTNode& varDecl = *iter;
332 if (modifiers.fLayout.fLocation == 0 && modifiers.fLayout.fIndex == 0 &&
333 (modifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind &&
334 varDecl.getVarData().fName != "sk_FragColor") {
335 fErrors.error(varDecl.fOffset,
Ethan Nicholas6c942712018-03-16 09:45:11 -0400336 "out location=0, index=0 is reserved for sk_FragColor");
337 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400338 const ASTNode::VarData& varData = varDecl.getVarData();
ethannicholasd598f792016-07-25 10:08:54 -0700339 const Type* type = baseType;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700340 std::vector<std::unique_ptr<Expression>> sizes;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400341 auto iter = varDecl.begin();
Brian Osman82d49702019-12-30 13:44:01 -0500342 if (varData.fSizeCount > 0 && (modifiers.fFlags & Modifiers::kIn_Flag)) {
343 fErrors.error(varDecl.fOffset, "'in' variables may not have array type");
344 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400345 for (size_t i = 0; i < varData.fSizeCount; ++i, ++iter) {
346 const ASTNode& rawSize = *iter;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700347 if (rawSize) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400348 auto size = this->coerce(this->convertExpression(rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700349 if (!size) {
350 return nullptr;
351 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700352 String name(type->fName);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500353 int64_t count;
ethannicholasb3058bd2016-07-01 08:22:01 -0700354 if (size->fKind == Expression::kIntLiteral_Kind) {
355 count = ((IntLiteral&) *size).fValue;
356 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700357 fErrors.error(size->fOffset, "array size must be positive");
Ethan Nicholas66d80062019-09-09 14:50:51 -0400358 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700359 }
360 name += "[" + to_string(count) + "]";
361 } else {
Ethan Nicholas66d80062019-09-09 14:50:51 -0400362 fErrors.error(size->fOffset, "array size must be specified");
363 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700364 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400365 type = (Type*) fSymbolTable->takeOwnership(
366 std::unique_ptr<Symbol>(new Type(name,
367 Type::kArray_Kind,
368 *type,
369 (int) count)));
ethannicholas14fe8cc2016-09-07 13:37:16 -0700370 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700371 } else {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400372 type = (Type*) fSymbolTable->takeOwnership(
373 std::unique_ptr<Symbol>(new Type(type->name() + "[]",
374 Type::kArray_Kind,
375 *type,
376 -1)));
ethannicholas14fe8cc2016-09-07 13:37:16 -0700377 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700378 }
379 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400380 auto var = std::unique_ptr<Variable>(new Variable(varDecl.fOffset, modifiers,
381 varData.fName, *type, storage));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000382 if (var->fName == Compiler::RTADJUST_NAME) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400383 SkASSERT(!fRTAdjust);
384 SkASSERT(var->fType == *fContext.fFloat4_Type);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000385 fRTAdjust = var.get();
386 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700387 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400388 if (iter != varDecl.end()) {
389 value = this->convertExpression(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700390 if (!value) {
391 return nullptr;
392 }
ethannicholasd598f792016-07-25 10:08:54 -0700393 value = this->coerce(std::move(value), *type);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500394 if (!value) {
395 return nullptr;
396 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400397 var->fWriteCount = 1;
Ethan Nicholas8f6c2ab2018-01-17 13:51:52 -0500398 var->fInitialValue = value.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700399 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400400 if (storage == Variable::kGlobal_Storage && var->fName == "sk_FragColor" &&
401 (*fSymbolTable)[var->fName]) {
ethannicholas5961bc92016-10-12 06:39:56 -0700402 // already defined, ignore
Ethan Nicholasfc994162019-06-06 10:04:27 -0400403 } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[var->fName] &&
404 (*fSymbolTable)[var->fName]->fKind == Symbol::kVariable_Kind &&
405 ((Variable*) (*fSymbolTable)[var->fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700406 // already defined, just update the modifiers
Ethan Nicholasfc994162019-06-06 10:04:27 -0400407 Variable* old = (Variable*) (*fSymbolTable)[var->fName];
ethannicholasf789b382016-08-03 12:43:36 -0700408 old->fModifiers = var->fModifiers;
409 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000410 variables.emplace_back(new VarDeclaration(var.get(), std::move(sizes),
411 std::move(value)));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400412 StringFragment name = var->fName;
413 fSymbolTable->add(name, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700414 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700415 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400416 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decls.fOffset,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700417 baseType,
418 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700419}
420
Ethan Nicholasfc994162019-06-06 10:04:27 -0400421std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(const ASTNode& m) {
422 SkASSERT(m.fKind == ASTNode::Kind::kModifiers);
423 Modifiers modifiers = m.getModifiers();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400424 if (modifiers.fLayout.fInvocations != -1) {
Ethan Nicholasf06576b2019-04-03 15:45:25 -0400425 if (fKind != Program::kGeometry_Kind) {
426 fErrors.error(m.fOffset, "'invocations' is only legal in geometry shaders");
427 return nullptr;
428 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400429 fInvocations = modifiers.fLayout.fInvocations;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600430 if (fSettings->fCaps && !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400431 modifiers.fLayout.fInvocations = -1;
432 Variable* invocationId = (Variable*) (*fSymbolTable)["sk_InvocationID"];
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400433 SkASSERT(invocationId);
Ethan Nicholasd1d52562018-03-20 16:30:34 -0400434 invocationId->fModifiers.fFlags = 0;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400435 invocationId->fModifiers.fLayout.fBuiltin = -1;
436 if (modifiers.fLayout.description() == "") {
437 return nullptr;
438 }
439 }
440 }
441 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fSettings->fCaps &&
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600442 !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400443 modifiers.fLayout.fMaxVertices *= fInvocations;
444 }
445 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700446}
447
Ethan Nicholasfc994162019-06-06 10:04:27 -0400448std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
449 SkASSERT(n.fKind == ASTNode::Kind::kIf);
450 auto iter = n.begin();
451 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
ethannicholasd598f792016-07-25 10:08:54 -0700452 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700453 if (!test) {
454 return nullptr;
455 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400456 std::unique_ptr<Statement> ifTrue = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700457 if (!ifTrue) {
458 return nullptr;
459 }
460 std::unique_ptr<Statement> ifFalse;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400461 if (iter != n.end()) {
462 ifFalse = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700463 if (!ifFalse) {
464 return nullptr;
465 }
466 }
ethannicholas08a92112016-11-09 13:26:45 -0800467 if (test->fKind == Expression::kBoolLiteral_Kind) {
468 // static boolean value, fold down to a single branch
469 if (((BoolLiteral&) *test).fValue) {
470 return ifTrue;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400471 } else if (ifFalse) {
ethannicholas08a92112016-11-09 13:26:45 -0800472 return ifFalse;
473 } else {
474 // False & no else clause. Not an error, so don't return null!
475 std::vector<std::unique_ptr<Statement>> empty;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400476 return std::unique_ptr<Statement>(new Block(n.fOffset, std::move(empty),
ethannicholas08a92112016-11-09 13:26:45 -0800477 fSymbolTable));
478 }
479 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400480 return std::unique_ptr<Statement>(new IfStatement(n.fOffset, n.getBool(), std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700481 std::move(ifTrue), std::move(ifFalse)));
482}
483
Ethan Nicholasfc994162019-06-06 10:04:27 -0400484std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
485 SkASSERT(f.fKind == ASTNode::Kind::kFor);
ethannicholas22f939e2016-10-13 13:25:34 -0700486 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700487 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700488 std::unique_ptr<Statement> initializer;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400489 auto iter = f.begin();
490 if (*iter) {
491 initializer = this->convertStatement(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700492 if (!initializer) {
493 return nullptr;
494 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700495 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400496 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700497 std::unique_ptr<Expression> test;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400498 if (*iter) {
499 test = this->coerce(this->convertExpression(*iter), *fContext.fBool_Type);
ethannicholas22f939e2016-10-13 13:25:34 -0700500 if (!test) {
501 return nullptr;
502 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700503 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400504 ++iter;
ethannicholas22f939e2016-10-13 13:25:34 -0700505 std::unique_ptr<Expression> next;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400506 if (*iter) {
507 next = this->convertExpression(*iter);
ethannicholas22f939e2016-10-13 13:25:34 -0700508 if (!next) {
509 return nullptr;
510 }
511 this->checkValid(*next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700512 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400513 ++iter;
514 std::unique_ptr<Statement> statement = this->convertStatement(*iter);
ethannicholasb3058bd2016-07-01 08:22:01 -0700515 if (!statement) {
516 return nullptr;
517 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700518 return std::unique_ptr<Statement>(new ForStatement(f.fOffset, std::move(initializer),
ethannicholasb3058bd2016-07-01 08:22:01 -0700519 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700520 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700521}
522
Ethan Nicholasfc994162019-06-06 10:04:27 -0400523std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTNode& w) {
524 SkASSERT(w.fKind == ASTNode::Kind::kWhile);
ethannicholas22f939e2016-10-13 13:25:34 -0700525 AutoLoopLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400526 auto iter = w.begin();
527 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
ethannicholasd598f792016-07-25 10:08:54 -0700528 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700529 if (!test) {
530 return nullptr;
531 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400532 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700533 if (!statement) {
534 return nullptr;
535 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700536 return std::unique_ptr<Statement>(new WhileStatement(w.fOffset, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700537 std::move(statement)));
538}
539
Ethan Nicholasfc994162019-06-06 10:04:27 -0400540std::unique_ptr<Statement> IRGenerator::convertDo(const ASTNode& d) {
541 SkASSERT(d.fKind == ASTNode::Kind::kDo);
ethannicholas22f939e2016-10-13 13:25:34 -0700542 AutoLoopLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400543 auto iter = d.begin();
544 std::unique_ptr<Statement> statement = this->convertStatement(*(iter++));
545 if (!statement) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700546 return nullptr;
547 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400548 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
549 *fContext.fBool_Type);
550 if (!test) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700551 return nullptr;
552 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700553 return std::unique_ptr<Statement>(new DoStatement(d.fOffset, std::move(statement),
ethannicholasb3058bd2016-07-01 08:22:01 -0700554 std::move(test)));
555}
556
Ethan Nicholasfc994162019-06-06 10:04:27 -0400557std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
558 SkASSERT(s.fKind == ASTNode::Kind::kSwitch);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500559 AutoSwitchLevel level(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400560 auto iter = s.begin();
561 std::unique_ptr<Expression> value = this->convertExpression(*(iter++));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500562 if (!value) {
563 return nullptr;
564 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500565 if (value->fType != *fContext.fUInt_Type && value->fType.kind() != Type::kEnum_Kind) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500566 value = this->coerce(std::move(value), *fContext.fInt_Type);
567 if (!value) {
568 return nullptr;
569 }
570 }
571 AutoSymbolTable table(this);
572 std::unordered_set<int> caseValues;
573 std::vector<std::unique_ptr<SwitchCase>> cases;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400574 for (; iter != s.end(); ++iter) {
575 const ASTNode& c = *iter;
576 SkASSERT(c.fKind == ASTNode::Kind::kSwitchCase);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500577 std::unique_ptr<Expression> caseValue;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400578 auto childIter = c.begin();
579 if (*childIter) {
580 caseValue = this->convertExpression(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500581 if (!caseValue) {
582 return nullptr;
583 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500584 caseValue = this->coerce(std::move(caseValue), value->fType);
585 if (!caseValue) {
586 return nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500587 }
588 if (!caseValue->isConstant()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700589 fErrors.error(caseValue->fOffset, "case value must be a constant");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500590 return nullptr;
591 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500592 int64_t v;
593 this->getConstantInt(*caseValue, &v);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500594 if (caseValues.find(v) != caseValues.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700595 fErrors.error(caseValue->fOffset, "duplicate case value");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500596 }
597 caseValues.insert(v);
598 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400599 ++childIter;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500600 std::vector<std::unique_ptr<Statement>> statements;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400601 for (; childIter != c.end(); ++childIter) {
602 std::unique_ptr<Statement> converted = this->convertStatement(*childIter);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500603 if (!converted) {
604 return nullptr;
605 }
606 statements.push_back(std::move(converted));
607 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400608 cases.emplace_back(new SwitchCase(c.fOffset, std::move(caseValue),
Ethan Nicholasaf197692017-02-27 13:26:45 -0500609 std::move(statements)));
610 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400611 return std::unique_ptr<Statement>(new SwitchStatement(s.fOffset, s.getBool(),
Ethan Nicholasc432b0c2017-07-18 13:22:37 -0400612 std::move(value), std::move(cases),
613 fSymbolTable));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500614}
615
Ethan Nicholasfc994162019-06-06 10:04:27 -0400616std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(const ASTNode& s) {
617 std::unique_ptr<Expression> e = this->convertExpression(s);
ethannicholasb3058bd2016-07-01 08:22:01 -0700618 if (!e) {
619 return nullptr;
620 }
621 this->checkValid(*e);
622 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
623}
624
Ethan Nicholasfc994162019-06-06 10:04:27 -0400625std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTNode& r) {
626 SkASSERT(r.fKind == ASTNode::Kind::kReturn);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400627 SkASSERT(fCurrentFunction);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000628 // early returns from a vertex main function will bypass the sk_Position normalization, so
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400629 // SkASSERT that we aren't doing that. It is of course possible to fix this by adding a
Robert Phillipsfe8da172018-01-24 14:52:02 +0000630 // normalization before each return, but it will probably never actually be necessary.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400631 SkASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->fName);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400632 if (r.begin() != r.end()) {
633 std::unique_ptr<Expression> result = this->convertExpression(*r.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -0700634 if (!result) {
635 return nullptr;
636 }
ethannicholasd598f792016-07-25 10:08:54 -0700637 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700638 fErrors.error(result->fOffset, "may not return a value from a void function");
ethannicholasb3058bd2016-07-01 08:22:01 -0700639 } else {
640 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
641 if (!result) {
642 return nullptr;
643 }
644 }
645 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
646 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700647 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700648 fErrors.error(r.fOffset, "expected function to return '" +
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500649 fCurrentFunction->fReturnType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700651 return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700652 }
653}
654
Ethan Nicholasfc994162019-06-06 10:04:27 -0400655std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTNode& b) {
656 SkASSERT(b.fKind == ASTNode::Kind::kBreak);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500657 if (fLoopLevel > 0 || fSwitchLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700658 return std::unique_ptr<Statement>(new BreakStatement(b.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700659 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700660 fErrors.error(b.fOffset, "break statement must be inside a loop or switch");
ethannicholas22f939e2016-10-13 13:25:34 -0700661 return nullptr;
662 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700663}
664
Ethan Nicholasfc994162019-06-06 10:04:27 -0400665std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
666 SkASSERT(c.fKind == ASTNode::Kind::kContinue);
ethannicholas22f939e2016-10-13 13:25:34 -0700667 if (fLoopLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700668 return std::unique_ptr<Statement>(new ContinueStatement(c.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700669 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700670 fErrors.error(c.fOffset, "continue statement must be inside a loop");
ethannicholas22f939e2016-10-13 13:25:34 -0700671 return nullptr;
672 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700673}
674
Ethan Nicholasfc994162019-06-06 10:04:27 -0400675std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
676 SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700677 return std::unique_ptr<Statement>(new DiscardStatement(d.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700678}
679
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500680std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400681 Layout invokeLayout;
682 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700683 FunctionDeclaration* invokeDecl = new FunctionDeclaration(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400684 invokeModifiers,
685 "_invoke",
686 std::vector<const Variable*>(),
687 *fContext.fVoid_Type);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500688 fProgramElements->push_back(std::unique_ptr<ProgramElement>(
689 new FunctionDefinition(-1, *invokeDecl, std::move(main))));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400690 fSymbolTable->add(invokeDecl->fName, std::unique_ptr<FunctionDeclaration>(invokeDecl));
691
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000692 std::vector<std::unique_ptr<VarDeclaration>> variables;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400693 Variable* loopIdx = (Variable*) (*fSymbolTable)["sk_InvocationID"];
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400694 SkASSERT(loopIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700695 std::unique_ptr<Expression> test(new BinaryExpression(-1,
696 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400697 Token::Kind::TK_LT,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700698 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, fInvocations)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400699 *fContext.fBool_Type));
700 std::unique_ptr<Expression> next(new PostfixExpression(
701 std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700702 new VariableReference(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400703 *loopIdx,
704 VariableReference::kReadWrite_RefKind)),
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400705 Token::Kind::TK_PLUSPLUS));
Ethan Nicholasfc994162019-06-06 10:04:27 -0400706 ASTNode endPrimitiveID(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, "EndPrimitive");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400707 std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400708 SkASSERT(endPrimitive);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400709
710 std::vector<std::unique_ptr<Statement>> loopBody;
711 std::vector<std::unique_ptr<Expression>> invokeArgs;
712 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700713 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400714 *invokeDecl,
715 std::vector<std::unique_ptr<Expression>>()))));
716 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700717 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400718 std::move(endPrimitive),
719 std::vector<std::unique_ptr<Expression>>()))));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700720 std::unique_ptr<Expression> assignment(new BinaryExpression(-1,
721 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400722 Token::Kind::TK_EQ,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700723 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, 0)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400724 *fContext.fInt_Type));
725 std::unique_ptr<Statement> initializer(new ExpressionStatement(std::move(assignment)));
726 std::unique_ptr<Statement> loop = std::unique_ptr<Statement>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700727 new ForStatement(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400728 std::move(initializer),
729 std::move(test),
730 std::move(next),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700731 std::unique_ptr<Block>(new Block(-1, std::move(loopBody))),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400732 fSymbolTable));
733 std::vector<std::unique_ptr<Statement>> children;
734 children.push_back(std::move(loop));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700735 return std::unique_ptr<Block>(new Block(-1, std::move(children)));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400736}
737
Robert Phillipsfe8da172018-01-24 14:52:02 +0000738std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
Ethan Nicholasb809efb2018-04-12 14:39:21 -0400739 // sk_Position = float4(sk_Position.xy * rtAdjust.xz + sk_Position.ww * rtAdjust.yw,
Robert Phillipsfe8da172018-01-24 14:52:02 +0000740 // 0,
741 // sk_Position.w);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400742 SkASSERT(fSkPerVertex && fRTAdjust);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000743 #define REF(var) std::unique_ptr<Expression>(\
744 new VariableReference(-1, *var, VariableReference::kRead_RefKind))
745 #define FIELD(var, idx) std::unique_ptr<Expression>(\
746 new FieldAccess(REF(var), idx, FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
747 #define POS std::unique_ptr<Expression>(new FieldAccess(REF(fSkPerVertex), 0, \
748 FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
749 #define ADJUST (fRTAdjustInterfaceBlock ? \
750 FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \
751 REF(fRTAdjust))
Ethan Nicholasb809efb2018-04-12 14:39:21 -0400752 #define SWIZZLE(expr, ...) std::unique_ptr<Expression>(new Swizzle(fContext, expr, \
753 { __VA_ARGS__ }))
754 #define OP(left, op, right) std::unique_ptr<Expression>( \
755 new BinaryExpression(-1, left, op, right, \
756 *fContext.fFloat2_Type))
Robert Phillipsfe8da172018-01-24 14:52:02 +0000757 std::vector<std::unique_ptr<Expression>> children;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400758 children.push_back(OP(OP(SWIZZLE(POS, 0, 1), Token::Kind::TK_STAR, SWIZZLE(ADJUST, 0, 2)),
759 Token::Kind::TK_PLUS,
760 OP(SWIZZLE(POS, 3, 3), Token::Kind::TK_STAR, SWIZZLE(ADJUST, 1, 3))));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000761 children.push_back(std::unique_ptr<Expression>(new FloatLiteral(fContext, -1, 0.0)));
762 children.push_back(SWIZZLE(POS, 3));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400763 std::unique_ptr<Expression> result = OP(POS, Token::Kind::TK_EQ,
Robert Phillipsfe8da172018-01-24 14:52:02 +0000764 std::unique_ptr<Expression>(new Constructor(-1,
765 *fContext.fFloat4_Type,
766 std::move(children))));
767 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(result)));
768}
769
Ethan Nicholasfc994162019-06-06 10:04:27 -0400770void IRGenerator::convertFunction(const ASTNode& f) {
771 auto iter = f.begin();
772 const Type* returnType = this->convertType(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700773 if (!returnType) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400774 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700775 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400776 const ASTNode::FunctionData& fd = f.getFunctionData();
ethannicholasd598f792016-07-25 10:08:54 -0700777 std::vector<const Variable*> parameters;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400778 for (size_t i = 0; i < fd.fParameterCount; ++i) {
779 const ASTNode& param = *(iter++);
780 SkASSERT(param.fKind == ASTNode::Kind::kParameter);
781 ASTNode::ParameterData pd = param.getParameterData();
782 auto paramIter = param.begin();
783 const Type* type = this->convertType(*(paramIter++));
ethannicholasb3058bd2016-07-01 08:22:01 -0700784 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400785 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700786 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400787 for (int j = (int) pd.fSizeCount; j >= 1; j--) {
788 int size = (param.begin() + j)->getInt();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400789 String name = type->name() + "[" + to_string(size) + "]";
Ethan Nicholas91164d12019-05-15 15:29:54 -0400790 type = (Type*) fSymbolTable->takeOwnership(
791 std::unique_ptr<Symbol>(new Type(std::move(name),
792 Type::kArray_Kind,
793 *type,
794 size)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700795 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400796 StringFragment name = pd.fName;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400797 Variable* var = (Variable*) fSymbolTable->takeOwnership(
Ethan Nicholasfc994162019-06-06 10:04:27 -0400798 std::unique_ptr<Symbol>(new Variable(param.fOffset,
799 pd.fModifiers,
Ethan Nicholas91164d12019-05-15 15:29:54 -0400800 name,
801 *type,
802 Variable::kParameter_Storage)));
ethannicholasd598f792016-07-25 10:08:54 -0700803 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700804 }
805
Ethan Nicholasfc994162019-06-06 10:04:27 -0400806 if (fd.fName == "main") {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400807 switch (fKind) {
808 case Program::kPipelineStage_Kind: {
809 bool valid;
810 switch (parameters.size()) {
Brian Osman7353dc52020-02-07 13:37:12 -0500811 case 2:
812 valid = parameters[0]->fType == *fContext.fFloat2_Type &&
Ethan Nicholas0d997662019-04-08 09:46:01 -0400813 parameters[0]->fModifiers.fFlags == 0 &&
Brian Osman7353dc52020-02-07 13:37:12 -0500814 parameters[1]->fType == *fContext.fHalf4_Type &&
815 parameters[1]->fModifiers.fFlags == (Modifiers::kIn_Flag |
Ethan Nicholas0d997662019-04-08 09:46:01 -0400816 Modifiers::kOut_Flag);
817 break;
818 case 1:
819 valid = parameters[0]->fType == *fContext.fHalf4_Type &&
820 parameters[0]->fModifiers.fFlags == (Modifiers::kIn_Flag |
821 Modifiers::kOut_Flag);
822 break;
823 default:
824 valid = false;
825 }
826 if (!valid) {
Brian Osman7353dc52020-02-07 13:37:12 -0500827 fErrors.error(f.fOffset, "pipeline stage 'main' must be declared main(float2, "
828 "inout half4) or main(inout half4)");
Ethan Nicholas0d997662019-04-08 09:46:01 -0400829 return;
830 }
831 break;
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500832 }
Ethan Nicholas746035a2019-04-23 13:31:09 -0400833 case Program::kGeneric_Kind:
Ethan Nicholas0d997662019-04-08 09:46:01 -0400834 break;
Ethan Nicholas0d997662019-04-08 09:46:01 -0400835 default:
836 if (parameters.size()) {
837 fErrors.error(f.fOffset, "shader 'main' must have zero parameters");
838 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400839 }
840 }
841
ethannicholasb3058bd2016-07-01 08:22:01 -0700842 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700843 const FunctionDeclaration* decl = nullptr;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400844 auto entry = (*fSymbolTable)[fd.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700845 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700846 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700847 switch (entry->fKind) {
848 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700849 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700850 break;
851 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700852 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700853 break;
854 default:
Ethan Nicholasfc994162019-06-06 10:04:27 -0400855 fErrors.error(f.fOffset, "symbol '" + fd.fName + "' was already defined");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400856 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700857 }
858 for (const auto& other : functions) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400859 SkASSERT(other->fName == fd.fName);
ethannicholasb3058bd2016-07-01 08:22:01 -0700860 if (parameters.size() == other->fParameters.size()) {
861 bool match = true;
862 for (size_t i = 0; i < parameters.size(); i++) {
863 if (parameters[i]->fType != other->fParameters[i]->fType) {
864 match = false;
865 break;
866 }
867 }
868 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700869 if (*returnType != other->fReturnType) {
Ethan Nicholasfc994162019-06-06 10:04:27 -0400870 FunctionDeclaration newDecl(f.fOffset, fd.fModifiers, fd.fName, parameters,
Ethan Nicholascb670962017-04-20 19:31:52 -0400871 *returnType);
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500872 fErrors.error(f.fOffset, "functions '" + newDecl.declaration() +
873 "' and '" + other->declaration() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700874 "' differ only in return type");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400875 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700876 }
877 decl = other;
878 for (size_t i = 0; i < parameters.size(); i++) {
879 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700880 fErrors.error(f.fOffset, "modifiers on parameter " +
881 to_string((uint64_t) i + 1) +
882 " differ between declaration and "
883 "definition");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400884 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700885 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700886 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500887 if (other->fDefined && !other->fBuiltin) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700888 fErrors.error(f.fOffset, "duplicate definition of " +
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500889 other->declaration());
ethannicholasb3058bd2016-07-01 08:22:01 -0700890 }
891 break;
892 }
893 }
894 }
895 }
896 if (!decl) {
897 // couldn't find an existing declaration
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700898 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -0400899 fd.fModifiers,
900 fd.fName,
ethannicholas471e8942016-10-28 09:02:46 -0700901 parameters,
902 *returnType));
903 decl = newDecl.get();
904 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700905 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400906 if (iter != f.end()) {
907 // compile body
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400908 SkASSERT(!fCurrentFunction);
ethannicholasd598f792016-07-25 10:08:54 -0700909 fCurrentFunction = decl;
910 decl->fDefined = true;
911 std::shared_ptr<SymbolTable> old = fSymbolTable;
912 AutoSymbolTable table(this);
Ethan Nicholasfc994162019-06-06 10:04:27 -0400913 if (fd.fName == "main" && fKind == Program::kPipelineStage_Kind) {
Brian Osman7353dc52020-02-07 13:37:12 -0500914 if (parameters.size() == 2) {
915 parameters[0]->fModifiers.fLayout.fBuiltin = SK_MAIN_COORDS_BUILTIN;
916 parameters[1]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN;
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500917 } else {
918 SkASSERT(parameters.size() == 1);
919 parameters[0]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN;
920 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400921 }
ethannicholasd598f792016-07-25 10:08:54 -0700922 for (size_t i = 0; i < parameters.size(); i++) {
923 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700924 }
Ethan Nicholasfc994162019-06-06 10:04:27 -0400925 bool needInvocationIDWorkaround = fInvocations != -1 && fd.fName == "main" &&
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600926 fSettings->fCaps &&
927 !fSettings->fCaps->gsInvocationsSupport();
Ethan Nicholasfc994162019-06-06 10:04:27 -0400928 std::unique_ptr<Block> body = this->convertBlock(*iter);
ethannicholasd598f792016-07-25 10:08:54 -0700929 fCurrentFunction = nullptr;
930 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400931 return;
932 }
933 if (needInvocationIDWorkaround) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500934 body = this->applyInvocationIDWorkaround(std::move(body));
ethannicholasd598f792016-07-25 10:08:54 -0700935 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400936 // conservatively assume all user-defined functions have side effects
937 ((Modifiers&) decl->fModifiers).fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400938 if (Program::kVertex_Kind == fKind && fd.fName == "main" && fRTAdjust) {
Robert Phillipsfe8da172018-01-24 14:52:02 +0000939 body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode());
940 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500941 std::unique_ptr<FunctionDefinition> result(new FunctionDefinition(f.fOffset, *decl,
942 std::move(body)));
943 result->fSource = &f;
944 fProgramElements->push_back(std::move(result));
ethannicholasb3058bd2016-07-01 08:22:01 -0700945 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700946}
947
Ethan Nicholasfc994162019-06-06 10:04:27 -0400948std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTNode& intf) {
949 SkASSERT(intf.fKind == ASTNode::Kind::kInterfaceBlock);
950 ASTNode::InterfaceBlockData id = intf.getInterfaceBlockData();
ethannicholasb3058bd2016-07-01 08:22:01 -0700951 std::shared_ptr<SymbolTable> old = fSymbolTable;
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500952 this->pushSymbolTable();
953 std::shared_ptr<SymbolTable> symbols = fSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700954 std::vector<Type::Field> fields;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400955 bool haveRuntimeArray = false;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000956 bool foundRTAdjust = false;
Ethan Nicholasfc994162019-06-06 10:04:27 -0400957 auto iter = intf.begin();
958 for (size_t i = 0; i < id.fDeclarationCount; ++i) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700959 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
Ethan Nicholasfc994162019-06-06 10:04:27 -0400960 *(iter++),
Ethan Nicholasa7ceb502019-01-11 10:31:48 -0500961 Variable::kInterfaceBlock_Storage);
ethannicholas7effa7a2016-10-14 09:56:33 -0700962 if (!decl) {
963 return nullptr;
964 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000965 for (const auto& stmt : decl->fVars) {
966 VarDeclaration& vd = (VarDeclaration&) *stmt;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400967 if (haveRuntimeArray) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000968 fErrors.error(decl->fOffset,
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400969 "only the last entry in an interface block may be a runtime-sized "
970 "array");
971 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000972 if (vd.fVar == fRTAdjust) {
973 foundRTAdjust = true;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400974 SkASSERT(vd.fVar->fType == *fContext.fFloat4_Type);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000975 fRTAdjustFieldIndex = fields.size();
976 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000977 fields.push_back(Type::Field(vd.fVar->fModifiers, vd.fVar->fName,
978 &vd.fVar->fType));
979 if (vd.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700980 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700981 "initializers are not permitted on interface block fields");
982 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000983 if (vd.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
Ethan Nicholasfc994162019-06-06 10:04:27 -0400984 Modifiers::kOut_Flag |
985 Modifiers::kUniform_Flag |
986 Modifiers::kBuffer_Flag |
987 Modifiers::kConst_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700988 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700989 "interface block fields may not have storage qualifiers");
990 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000991 if (vd.fVar->fType.kind() == Type::kArray_Kind &&
992 vd.fVar->fType.columns() == -1) {
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400993 haveRuntimeArray = true;
994 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500995 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700996 }
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500997 this->popSymbolTable();
Ethan Nicholas91164d12019-05-15 15:29:54 -0400998 Type* type = (Type*) old->takeOwnership(std::unique_ptr<Symbol>(new Type(intf.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -0400999 id.fTypeName,
Ethan Nicholas91164d12019-05-15 15:29:54 -04001000 fields)));
Ethan Nicholas50afc172017-02-16 14:49:57 -05001001 std::vector<std::unique_ptr<Expression>> sizes;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001002 for (size_t i = 0; i < id.fSizeCount; ++i) {
1003 const ASTNode& size = *(iter++);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001004 if (size) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001005 std::unique_ptr<Expression> converted = this->convertExpression(size);
Ethan Nicholas50afc172017-02-16 14:49:57 -05001006 if (!converted) {
1007 return nullptr;
1008 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001009 String name = type->fName;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001010 int64_t count;
1011 if (converted->fKind == Expression::kIntLiteral_Kind) {
1012 count = ((IntLiteral&) *converted).fValue;
1013 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001014 fErrors.error(converted->fOffset, "array size must be positive");
Ethan Nicholas66d80062019-09-09 14:50:51 -04001015 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001016 }
1017 name += "[" + to_string(count) + "]";
1018 } else {
Ethan Nicholas66d80062019-09-09 14:50:51 -04001019 fErrors.error(intf.fOffset, "array size must be specified");
1020 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001021 }
Ethan Nicholas91164d12019-05-15 15:29:54 -04001022 type = (Type*) symbols->takeOwnership(std::unique_ptr<Symbol>(
1023 new Type(name,
1024 Type::kArray_Kind,
1025 *type,
1026 (int) count)));
Ethan Nicholas50afc172017-02-16 14:49:57 -05001027 sizes.push_back(std::move(converted));
1028 } else {
Ethan Nicholas66d80062019-09-09 14:50:51 -04001029 fErrors.error(intf.fOffset, "array size must be specified");
1030 return nullptr;
Ethan Nicholas50afc172017-02-16 14:49:57 -05001031 }
1032 }
Ethan Nicholas91164d12019-05-15 15:29:54 -04001033 Variable* var = (Variable*) old->takeOwnership(std::unique_ptr<Symbol>(
1034 new Variable(intf.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001035 id.fModifiers,
1036 id.fInstanceName.fLength ? id.fInstanceName : id.fTypeName,
Ethan Nicholas91164d12019-05-15 15:29:54 -04001037 *type,
1038 Variable::kGlobal_Storage)));
Robert Phillipsfe8da172018-01-24 14:52:02 +00001039 if (foundRTAdjust) {
1040 fRTAdjustInterfaceBlock = var;
1041 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001042 if (id.fInstanceName.fLength) {
1043 old->addWithoutOwnership(id.fInstanceName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -07001044 } else {
1045 for (size_t i = 0; i < fields.size(); i++) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001046 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fOffset, *var,
ethannicholasd598f792016-07-25 10:08:54 -07001047 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001048 }
1049 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001050 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fOffset,
Ethan Nicholas8feeff92017-03-30 14:11:58 -04001051 var,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001052 id.fTypeName,
1053 id.fInstanceName,
Ethan Nicholas50afc172017-02-16 14:49:57 -05001054 std::move(sizes),
Ethan Nicholas68dd2c12018-03-01 15:05:17 -05001055 symbols));
ethannicholasb3058bd2016-07-01 08:22:01 -07001056}
1057
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001058void IRGenerator::getConstantInt(const Expression& value, int64_t* out) {
1059 switch (value.fKind) {
1060 case Expression::kIntLiteral_Kind:
1061 *out = ((const IntLiteral&) value).fValue;
1062 break;
1063 case Expression::kVariableReference_Kind: {
1064 const Variable& var = ((VariableReference&) value).fVariable;
1065 if ((var.fModifiers.fFlags & Modifiers::kConst_Flag) &&
1066 var.fInitialValue) {
1067 this->getConstantInt(*var.fInitialValue, out);
1068 }
1069 break;
1070 }
1071 default:
1072 fErrors.error(value.fOffset, "expected a constant int");
1073 }
1074}
1075
Ethan Nicholasfc994162019-06-06 10:04:27 -04001076void IRGenerator::convertEnum(const ASTNode& e) {
1077 SkASSERT(e.fKind == ASTNode::Kind::kEnum);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001078 std::vector<Variable*> variables;
1079 int64_t currentValue = 0;
1080 Layout layout;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001081 ASTNode enumType(e.fNodes, e.fOffset, ASTNode::Kind::kType,
1082 ASTNode::TypeData(e.getString(), false, false));
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001083 const Type* type = this->convertType(enumType);
1084 Modifiers modifiers(layout, Modifiers::kConst_Flag);
Ethan Nicholaseace9352018-10-15 20:09:54 +00001085 std::shared_ptr<SymbolTable> symbols(new SymbolTable(fSymbolTable, &fErrors));
1086 fSymbolTable = symbols;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001087 for (auto iter = e.begin(); iter != e.end(); ++iter) {
1088 const ASTNode& child = *iter;
1089 SkASSERT(child.fKind == ASTNode::Kind::kEnumCase);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001090 std::unique_ptr<Expression> value;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001091 if (child.begin() != child.end()) {
1092 value = this->convertExpression(*child.begin());
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001093 if (!value) {
Ethan Nicholaseace9352018-10-15 20:09:54 +00001094 fSymbolTable = symbols->fParent;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001095 return;
1096 }
1097 this->getConstantInt(*value, &currentValue);
1098 }
1099 value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue));
1100 ++currentValue;
Ethan Nicholasfc994162019-06-06 10:04:27 -04001101 auto var = std::unique_ptr<Variable>(new Variable(e.fOffset, modifiers, child.getString(),
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001102 *type, Variable::kGlobal_Storage,
1103 value.get()));
1104 variables.push_back(var.get());
Ethan Nicholasfc994162019-06-06 10:04:27 -04001105 symbols->add(child.getString(), std::move(var));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001106 symbols->takeOwnership(std::move(value));
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001107 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001108 fProgramElements->push_back(std::unique_ptr<ProgramElement>(new Enum(e.fOffset, e.getString(),
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001109 symbols)));
Ethan Nicholaseace9352018-10-15 20:09:54 +00001110 fSymbolTable = symbols->fParent;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001111}
1112
Ethan Nicholasfc994162019-06-06 10:04:27 -04001113const Type* IRGenerator::convertType(const ASTNode& type) {
1114 ASTNode::TypeData td = type.getTypeData();
1115 const Symbol* result = (*fSymbolTable)[td.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -07001116 if (result && result->fKind == Symbol::kType_Kind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001117 if (td.fIsNullable) {
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001118 if (((Type&) *result) == *fContext.fFragmentProcessor_Type) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001119 if (type.begin() != type.end()) {
1120 fErrors.error(type.fOffset, "type '" + td.fName + "' may not be used in "
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001121 "an array");
1122 }
Ethan Nicholas91164d12019-05-15 15:29:54 -04001123 result = fSymbolTable->takeOwnership(std::unique_ptr<Symbol>(
1124 new Type(String(result->fName) + "?",
1125 Type::kNullable_Kind,
1126 (const Type&) *result)));
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001127 } else {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001128 fErrors.error(type.fOffset, "type '" + td.fName + "' may not be nullable");
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001129 }
1130 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001131 for (const auto& size : type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001132 String name(result->fName);
1133 name += "[";
Ethan Nicholasfc994162019-06-06 10:04:27 -04001134 if (size) {
1135 name += to_string(size.getInt());
Ethan Nicholas50afc172017-02-16 14:49:57 -05001136 }
1137 name += "]";
Ethan Nicholas91164d12019-05-15 15:29:54 -04001138 result = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>(
1139 new Type(name,
1140 Type::kArray_Kind,
1141 (const Type&) *result,
Brian Osmanb9416ca2019-06-12 16:18:32 -04001142 size ? size.getInt()
1143 : 0)));
Ethan Nicholas50afc172017-02-16 14:49:57 -05001144 }
ethannicholasd598f792016-07-25 10:08:54 -07001145 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001146 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001147 fErrors.error(type.fOffset, "unknown type '" + td.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001148 return nullptr;
1149}
1150
Ethan Nicholasfc994162019-06-06 10:04:27 -04001151std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTNode& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001152 switch (expr.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001153 case ASTNode::Kind::kBinary:
1154 return this->convertBinaryExpression(expr);
1155 case ASTNode::Kind::kBool:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001156 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001157 expr.getBool()));
1158 case ASTNode::Kind::kCall:
1159 return this->convertCallExpression(expr);
1160 case ASTNode::Kind::kField:
1161 return this->convertFieldExpression(expr);
1162 case ASTNode::Kind::kFloat:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001163 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04001164 expr.getFloat()));
1165 case ASTNode::Kind::kIdentifier:
1166 return this->convertIdentifier(expr);
1167 case ASTNode::Kind::kIndex:
1168 return this->convertIndexExpression(expr);
1169 case ASTNode::Kind::kInt:
1170 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset,
1171 expr.getInt()));
1172 case ASTNode::Kind::kNull:
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001173 return std::unique_ptr<Expression>(new NullLiteral(fContext, expr.fOffset));
Ethan Nicholasfc994162019-06-06 10:04:27 -04001174 case ASTNode::Kind::kPostfix:
1175 return this->convertPostfixExpression(expr);
1176 case ASTNode::Kind::kPrefix:
1177 return this->convertPrefixExpression(expr);
1178 case ASTNode::Kind::kTernary:
1179 return this->convertTernaryExpression(expr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001180 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001181#ifdef SK_DEBUG
Ethan Nicholasfc994162019-06-06 10:04:27 -04001182 ABORT("unsupported expression: %s\n", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001183#endif
1184 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001185 }
1186}
1187
Ethan Nicholasfc994162019-06-06 10:04:27 -04001188std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identifier) {
1189 SkASSERT(identifier.fKind == ASTNode::Kind::kIdentifier);
1190 const Symbol* result = (*fSymbolTable)[identifier.getString()];
ethannicholasb3058bd2016-07-01 08:22:01 -07001191 if (!result) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001192 fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.getString() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001193 return nullptr;
1194 }
1195 switch (result->fKind) {
1196 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001197 std::vector<const FunctionDeclaration*> f = {
1198 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -07001199 };
ethannicholasd598f792016-07-25 10:08:54 -07001200 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001201 identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001202 f));
ethannicholasb3058bd2016-07-01 08:22:01 -07001203 }
1204 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001205 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
1206 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001207 identifier.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001208 f->fFunctions));
1209 }
1210 case Symbol::kVariable_Kind: {
Ethan Nicholas38657112017-02-09 17:01:22 -05001211 const Variable* var = (const Variable*) result;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001212 switch (var->fModifiers.fLayout.fBuiltin) {
1213 case SK_WIDTH_BUILTIN:
1214 fInputs.fRTWidth = true;
1215 break;
1216 case SK_HEIGHT_BUILTIN:
Greg Daniele6ab9982018-08-22 13:56:32 +00001217 fInputs.fRTHeight = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001218 break;
1219#ifndef SKSL_STANDALONE
1220 case SK_FRAGCOORD_BUILTIN:
Brian Osman9f313b62019-10-02 12:03:11 -04001221 fInputs.fFlipY = true;
1222 if (fSettings->fFlipY &&
1223 (!fSettings->fCaps ||
1224 !fSettings->fCaps->fragCoordConventionsExtensionString())) {
1225 fInputs.fRTHeight = true;
Ethan Nicholascd700e92018-08-24 16:43:57 -04001226 }
Greg Daniele6ab9982018-08-22 13:56:32 +00001227#endif
Ethan Nicholascd700e92018-08-24 16:43:57 -04001228 }
Ethan Nicholas33c59ed2019-08-13 10:21:38 -04001229 if (fKind == Program::kFragmentProcessor_Kind &&
1230 (var->fModifiers.fFlags & Modifiers::kIn_Flag) &&
1231 !(var->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1232 !var->fModifiers.fLayout.fKey &&
1233 var->fModifiers.fLayout.fBuiltin == -1 &&
1234 var->fType.nonnullable() != *fContext.fFragmentProcessor_Type &&
1235 var->fType.kind() != Type::kSampler_Kind) {
1236 bool valid = false;
1237 for (const auto& decl : fFile->root()) {
1238 if (decl.fKind == ASTNode::Kind::kSection) {
1239 ASTNode::SectionData section = decl.getSectionData();
1240 if (section.fName == "setData") {
1241 valid = true;
1242 break;
1243 }
1244 }
1245 }
1246 if (!valid) {
1247 fErrors.error(identifier.fOffset, "'in' variable must be either 'uniform' or "
1248 "'layout(key)', or there must be a custom "
1249 "@setData function");
1250 }
1251 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001252 // default to kRead_RefKind; this will be corrected later if the variable is written to
1253 return std::unique_ptr<VariableReference>(new VariableReference(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001254 identifier.fOffset,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001255 *var,
1256 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001257 }
1258 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001259 const Field* field = (const Field*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001260 VariableReference* base = new VariableReference(identifier.fOffset, field->fOwner,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001261 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -07001262 return std::unique_ptr<Expression>(new FieldAccess(
1263 std::unique_ptr<Expression>(base),
1264 field->fFieldIndex,
1265 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001266 }
1267 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001268 const Type* t = (const Type*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001269 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001270 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001271 }
Ethan Nicholas91164d12019-05-15 15:29:54 -04001272 case Symbol::kExternal_Kind: {
1273 ExternalValue* r = (ExternalValue*) result;
1274 return std::unique_ptr<ExternalValueReference>(
1275 new ExternalValueReference(identifier.fOffset, r));
1276 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001277 default:
1278 ABORT("unsupported symbol type %d\n", result->fKind);
1279 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001280}
1281
Ethan Nicholasfc994162019-06-06 10:04:27 -04001282std::unique_ptr<Section> IRGenerator::convertSection(const ASTNode& s) {
1283 ASTNode::SectionData section = s.getSectionData();
1284 return std::unique_ptr<Section>(new Section(s.fOffset, section.fName, section.fArgument,
1285 section.fText));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001286}
1287
1288
Ethan Nicholas11d53972016-11-28 11:23:23 -05001289std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001290 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001291 if (!expr) {
1292 return nullptr;
1293 }
ethannicholasd598f792016-07-25 10:08:54 -07001294 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001295 return expr;
1296 }
1297 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -07001298 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001299 return nullptr;
1300 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001301 if (expr->coercionCost(type) == INT_MAX) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001302 fErrors.error(expr->fOffset, "expected '" + type.displayName() + "', but found '" +
1303 expr->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001304 return nullptr;
1305 }
ethannicholasd598f792016-07-25 10:08:54 -07001306 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001307 std::vector<std::unique_ptr<Expression>> args;
1308 args.push_back(std::move(expr));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001309 std::unique_ptr<Expression> ctor;
1310 if (type == *fContext.fFloatLiteral_Type) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001311 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
1312 "float"));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001313 } else if (type == *fContext.fIntLiteral_Type) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001314 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
1315 "int"));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001316 } else {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001317 ctor = this->convertIdentifier(ASTNode(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier,
1318 type.fName));
Ethan Nicholase1f55022019-02-05 17:17:40 -05001319 }
1320 if (!ctor) {
1321 printf("error, null identifier: %s\n", String(type.fName).c_str());
1322 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001323 SkASSERT(ctor);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001324 return this->call(-1, std::move(ctor), std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001325 }
Ethan Nicholasee1c8a72019-02-22 10:50:47 -05001326 if (expr->fKind == Expression::kNullLiteral_Kind) {
1327 SkASSERT(type.kind() == Type::kNullable_Kind);
1328 return std::unique_ptr<Expression>(new NullLiteral(expr->fOffset, type));
1329 }
ethannicholas5961bc92016-10-12 06:39:56 -07001330 std::vector<std::unique_ptr<Expression>> args;
1331 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001332 return std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001333}
1334
ethannicholasf789b382016-08-03 12:43:36 -07001335static bool is_matrix_multiply(const Type& left, const Type& right) {
1336 if (left.kind() == Type::kMatrix_Kind) {
1337 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
1338 }
1339 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
1340}
ethannicholasea4567c2016-10-17 11:24:37 -07001341
ethannicholasb3058bd2016-07-01 08:22:01 -07001342/**
1343 * Determines the operand and result types of a binary expression. Returns true if the expression is
1344 * legal, false otherwise. If false, the values of the out parameters are undefined.
1345 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001346static bool determine_binary_type(const Context& context,
1347 Token::Kind op,
1348 const Type& left,
1349 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -07001350 const Type** outLeftType,
1351 const Type** outRightType,
1352 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001353 bool tryFlipped) {
1354 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -07001355 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -07001356 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001357 case Token::Kind::TK_EQ:
ethannicholasea4567c2016-10-17 11:24:37 -07001358 *outLeftType = &left;
1359 *outRightType = &left;
1360 *outResultType = &left;
1361 return right.canCoerceTo(left);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001362 case Token::Kind::TK_EQEQ: // fall through
1363 case Token::Kind::TK_NEQ:
Ethan Nicholas23463002018-03-28 15:16:15 -04001364 if (right.canCoerceTo(left)) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001365 *outLeftType = &left;
Ethan Nicholas23463002018-03-28 15:16:15 -04001366 *outRightType = &left;
1367 *outResultType = context.fBool_Type.get();
1368 return true;
1369 } if (left.canCoerceTo(right)) {
1370 *outLeftType = &right;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001371 *outRightType = &right;
1372 *outResultType = context.fBool_Type.get();
1373 return true;
1374 }
Ethan Nicholas23463002018-03-28 15:16:15 -04001375 return false;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001376 case Token::Kind::TK_LT: // fall through
1377 case Token::Kind::TK_GT: // fall through
1378 case Token::Kind::TK_LTEQ: // fall through
1379 case Token::Kind::TK_GTEQ:
ethannicholasb3058bd2016-07-01 08:22:01 -07001380 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -07001381 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001382 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001383 case Token::Kind::TK_LOGICALOR: // fall through
1384 case Token::Kind::TK_LOGICALAND: // fall through
1385 case Token::Kind::TK_LOGICALXOR: // fall through
1386 case Token::Kind::TK_LOGICALOREQ: // fall through
1387 case Token::Kind::TK_LOGICALANDEQ: // fall through
1388 case Token::Kind::TK_LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -07001389 *outLeftType = context.fBool_Type.get();
1390 *outRightType = context.fBool_Type.get();
1391 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001392 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -07001393 right.canCoerceTo(*context.fBool_Type);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001394 case Token::Kind::TK_STAREQ:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001395 if (left.kind() == Type::kScalar_Kind) {
1396 *outLeftType = &left;
1397 *outRightType = &left;
1398 *outResultType = &left;
1399 return right.canCoerceTo(left);
1400 }
1401 // fall through
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001402 case Token::Kind::TK_STAR:
ethannicholasf789b382016-08-03 12:43:36 -07001403 if (is_matrix_multiply(left, right)) {
1404 // determine final component type
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001405 if (determine_binary_type(context, Token::Kind::TK_STAR, left.componentType(),
ethannicholasf789b382016-08-03 12:43:36 -07001406 right.componentType(), outLeftType, outRightType,
1407 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001408 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
Brian Salomon23356442018-11-30 15:33:19 -05001409 left.rows());
Ethan Nicholas11d53972016-11-28 11:23:23 -05001410 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
Brian Salomon23356442018-11-30 15:33:19 -05001411 right.rows());
ethannicholasf789b382016-08-03 12:43:36 -07001412 int leftColumns = left.columns();
1413 int leftRows = left.rows();
1414 int rightColumns;
1415 int rightRows;
1416 if (right.kind() == Type::kVector_Kind) {
1417 // matrix * vector treats the vector as a column vector, so we need to
1418 // transpose it
1419 rightColumns = right.rows();
1420 rightRows = right.columns();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001421 SkASSERT(rightColumns == 1);
ethannicholasf789b382016-08-03 12:43:36 -07001422 } else {
1423 rightColumns = right.columns();
1424 rightRows = right.rows();
1425 }
1426 if (rightColumns > 1) {
1427 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
1428 leftRows);
1429 } else {
1430 // result was a column vector, transpose it back to a row
1431 *outResultType = &(*outResultType)->toCompound(context, leftRows,
1432 rightColumns);
1433 }
1434 return leftColumns == rightRows;
1435 } else {
1436 return false;
1437 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001438 }
ethannicholasea4567c2016-10-17 11:24:37 -07001439 isLogical = false;
1440 validMatrixOrVectorOp = true;
1441 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001442 case Token::Kind::TK_PLUSEQ:
1443 case Token::Kind::TK_MINUSEQ:
1444 case Token::Kind::TK_SLASHEQ:
1445 case Token::Kind::TK_PERCENTEQ:
1446 case Token::Kind::TK_SHLEQ:
1447 case Token::Kind::TK_SHREQ:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001448 if (left.kind() == Type::kScalar_Kind) {
1449 *outLeftType = &left;
1450 *outRightType = &left;
1451 *outResultType = &left;
1452 return right.canCoerceTo(left);
1453 }
1454 // fall through
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001455 case Token::Kind::TK_PLUS: // fall through
1456 case Token::Kind::TK_MINUS: // fall through
1457 case Token::Kind::TK_SLASH: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001458 isLogical = false;
1459 validMatrixOrVectorOp = true;
1460 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001461 case Token::Kind::TK_COMMA:
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001462 *outLeftType = &left;
1463 *outRightType = &right;
1464 *outResultType = &right;
1465 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001466 default:
1467 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -07001468 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001469 }
ethannicholasea4567c2016-10-17 11:24:37 -07001470 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001471 if (left.kind() == Type::kScalar_Kind && right.kind() == Type::kScalar_Kind &&
1472 right.canCoerceTo(left)) {
1473 if (left.priority() > right.priority()) {
1474 *outLeftType = &left;
1475 *outRightType = &left;
1476 } else {
1477 *outLeftType = &right;
1478 *outRightType = &right;
1479 }
1480 if (isLogical) {
1481 *outResultType = context.fBool_Type.get();
1482 } else {
1483 *outResultType = &left;
1484 }
1485 return true;
1486 }
1487 if (right.canCoerceTo(left) && isVectorOrMatrix && validMatrixOrVectorOp) {
ethannicholasd598f792016-07-25 10:08:54 -07001488 *outLeftType = &left;
1489 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001490 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -07001491 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -07001492 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001493 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001494 }
1495 return true;
1496 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001497 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -07001498 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001499 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001500 outRightType, outResultType, false)) {
1501 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001502 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001503 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -07001504 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001505 }
1506 return true;
1507 }
1508 return false;
1509 }
1510 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001511 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001512 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07001513 }
1514 return false;
1515}
1516
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001517static std::unique_ptr<Expression> short_circuit_boolean(const Context& context,
1518 const Expression& left,
1519 Token::Kind op,
1520 const Expression& right) {
1521 SkASSERT(left.fKind == Expression::kBoolLiteral_Kind);
1522 bool leftVal = ((BoolLiteral&) left).fValue;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001523 if (op == Token::Kind::TK_LOGICALAND) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001524 // (true && expr) -> (expr) and (false && expr) -> (false)
1525 return leftVal ? right.clone()
1526 : std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, false));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001527 } else if (op == Token::Kind::TK_LOGICALOR) {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001528 // (true || expr) -> (true) and (false || expr) -> (expr)
1529 return leftVal ? std::unique_ptr<Expression>(new BoolLiteral(context, left.fOffset, true))
1530 : right.clone();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001531 } else if (op == Token::Kind::TK_LOGICALXOR) {
Noah Lavine334d0ba2019-12-18 23:03:49 -05001532 // (true ^^ expr) -> !(expr) and (false ^^ expr) -> (expr)
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001533 return leftVal ? std::unique_ptr<Expression>(new PrefixExpression(
1534 Token::Kind::TK_LOGICALNOT,
1535 right.clone()))
Noah Lavine334d0ba2019-12-18 23:03:49 -05001536 : right.clone();
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001537 } else {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001538 return nullptr;
1539 }
1540}
1541
ethannicholas08a92112016-11-09 13:26:45 -08001542std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
1543 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001544 const Expression& right) const {
Michael Ludwig7b429ae2018-09-06 17:01:38 -04001545 // If the left side is a constant boolean literal, the right side does not need to be constant
1546 // for short circuit optimizations to allow the constant to be folded.
1547 if (left.fKind == Expression::kBoolLiteral_Kind && !right.isConstant()) {
1548 return short_circuit_boolean(fContext, left, op, right);
1549 } else if (right.fKind == Expression::kBoolLiteral_Kind && !left.isConstant()) {
1550 // There aren't side effects in SKSL within expressions, so (left OP right) is equivalent to
1551 // (right OP left) for short-circuit optimizations
1552 return short_circuit_boolean(fContext, right, op, left);
1553 }
1554
1555 // Other than the short-circuit cases above, constant folding requires both sides to be constant
Ethan Nicholascb670962017-04-20 19:31:52 -04001556 if (!left.isConstant() || !right.isConstant()) {
1557 return nullptr;
1558 }
ethannicholas08a92112016-11-09 13:26:45 -08001559 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
1560 // precision to calculate the results and hope the result makes sense. The plan is to move the
1561 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
1562 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -05001563 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001564 right.fKind == Expression::kBoolLiteral_Kind) {
1565 bool leftVal = ((BoolLiteral&) left).fValue;
1566 bool rightVal = ((BoolLiteral&) right).fValue;
1567 bool result;
1568 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001569 case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
1570 case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break;
1571 case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break;
ethannicholas08a92112016-11-09 13:26:45 -08001572 default: return nullptr;
1573 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001574 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fOffset, result));
ethannicholas08a92112016-11-09 13:26:45 -08001575 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001576 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \
ethannicholas08a92112016-11-09 13:26:45 -08001577 leftVal op rightVal))
Ethan Nicholase4489002020-04-29 14:00:14 -04001578 #define URESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \
1579 (uint32_t) leftVal op \
1580 (uint32_t) rightVal))
ethannicholas08a92112016-11-09 13:26:45 -08001581 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
1582 int64_t leftVal = ((IntLiteral&) left).fValue;
1583 int64_t rightVal = ((IntLiteral&) right).fValue;
1584 switch (op) {
Ethan Nicholas66869e92020-04-30 09:27:54 -04001585 case Token::Kind::TK_PLUS: return URESULT(Int, +);
1586 case Token::Kind::TK_MINUS: return URESULT(Int, -);
1587 case Token::Kind::TK_STAR: return URESULT(Int, *);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001588 case Token::Kind::TK_SLASH:
Ethan Nicholas66869e92020-04-30 09:27:54 -04001589 if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) {
1590 fErrors.error(right.fOffset, "arithmetic overflow");
1591 return nullptr;
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001592 }
Ethan Nicholas66869e92020-04-30 09:27:54 -04001593 if (!rightVal) {
1594 fErrors.error(right.fOffset, "division by zero");
1595 return nullptr;
1596 }
1597 return RESULT(Int, /);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001598 case Token::Kind::TK_PERCENT:
Ethan Nicholas66869e92020-04-30 09:27:54 -04001599 if (leftVal == std::numeric_limits<int64_t>::min() && rightVal == -1) {
1600 fErrors.error(right.fOffset, "arithmetic overflow");
1601 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001602 }
Ethan Nicholas66869e92020-04-30 09:27:54 -04001603 if (!rightVal) {
1604 fErrors.error(right.fOffset, "division by zero");
1605 return nullptr;
1606 }
1607 return RESULT(Int, %);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001608 case Token::Kind::TK_BITWISEAND: return RESULT(Int, &);
1609 case Token::Kind::TK_BITWISEOR: return RESULT(Int, |);
1610 case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^);
1611 case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
1612 case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
1613 case Token::Kind::TK_GT: return RESULT(Bool, >);
1614 case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
1615 case Token::Kind::TK_LT: return RESULT(Bool, <);
1616 case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
1617 case Token::Kind::TK_SHL:
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001618 if (rightVal >= 0 && rightVal <= 31) {
Ethan Nicholase4489002020-04-29 14:00:14 -04001619 return URESULT(Int, <<);
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001620 }
1621 fErrors.error(right.fOffset, "shift value out of range");
1622 return nullptr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001623 case Token::Kind::TK_SHR:
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001624 if (rightVal >= 0 && rightVal <= 31) {
Ethan Nicholase4489002020-04-29 14:00:14 -04001625 return URESULT(Int, >>);
Ethan Nicholasfeba68a2019-06-10 09:56:29 -04001626 }
1627 fErrors.error(right.fOffset, "shift value out of range");
1628 return nullptr;
1629
1630 default:
1631 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001632 }
1633 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001634 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001635 right.fKind == Expression::kFloatLiteral_Kind) {
1636 double leftVal = ((FloatLiteral&) left).fValue;
1637 double rightVal = ((FloatLiteral&) right).fValue;
1638 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001639 case Token::Kind::TK_PLUS: return RESULT(Float, +);
1640 case Token::Kind::TK_MINUS: return RESULT(Float, -);
1641 case Token::Kind::TK_STAR: return RESULT(Float, *);
1642 case Token::Kind::TK_SLASH:
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001643 if (rightVal) {
1644 return RESULT(Float, /);
1645 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001646 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001647 return nullptr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001648 case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
1649 case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
1650 case Token::Kind::TK_GT: return RESULT(Bool, >);
1651 case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
1652 case Token::Kind::TK_LT: return RESULT(Bool, <);
1653 case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
1654 default: return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001655 }
1656 }
Ethan Nicholase1f55022019-02-05 17:17:40 -05001657 if (left.fType.kind() == Type::kVector_Kind && left.fType.componentType().isFloat() &&
Ethan Nicholascb670962017-04-20 19:31:52 -04001658 left.fType == right.fType) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001659 std::vector<std::unique_ptr<Expression>> args;
Ethan Nicholas4cf5fd92019-06-10 16:15:56 -04001660 #define RETURN_VEC_COMPONENTWISE_RESULT(op) \
1661 for (int i = 0; i < left.fType.columns(); i++) { \
1662 float value = left.getFVecComponent(i) op \
1663 right.getFVecComponent(i); \
1664 args.emplace_back(new FloatLiteral(fContext, -1, value)); \
1665 } \
1666 return std::unique_ptr<Expression>(new Constructor(-1, left.fType, \
Brian Salomon23356442018-11-30 15:33:19 -05001667 std::move(args)))
Ethan Nicholascb670962017-04-20 19:31:52 -04001668 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001669 case Token::Kind::TK_EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001670 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001671 left.compareConstant(fContext, right)));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001672 case Token::Kind::TK_NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001673 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001674 !left.compareConstant(fContext, right)));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001675 case Token::Kind::TK_PLUS: RETURN_VEC_COMPONENTWISE_RESULT(+);
1676 case Token::Kind::TK_MINUS: RETURN_VEC_COMPONENTWISE_RESULT(-);
1677 case Token::Kind::TK_STAR: RETURN_VEC_COMPONENTWISE_RESULT(*);
1678 case Token::Kind::TK_SLASH:
Ethan Nicholas4cf5fd92019-06-10 16:15:56 -04001679 for (int i = 0; i < left.fType.columns(); i++) {
1680 SKSL_FLOAT rvalue = right.getFVecComponent(i);
1681 if (rvalue == 0.0) {
1682 fErrors.error(right.fOffset, "division by zero");
1683 return nullptr;
1684 }
1685 float value = left.getFVecComponent(i) / rvalue;
1686 args.emplace_back(new FloatLiteral(fContext, -1, value));
1687 }
1688 return std::unique_ptr<Expression>(new Constructor(-1, left.fType,
1689 std::move(args)));
Ethan Nicholascb670962017-04-20 19:31:52 -04001690 default: return nullptr;
1691 }
1692 }
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001693 if (left.fType.kind() == Type::kMatrix_Kind &&
1694 right.fType.kind() == Type::kMatrix_Kind &&
1695 left.fKind == right.fKind) {
1696 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001697 case Token::Kind::TK_EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001698 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001699 left.compareConstant(fContext, right)));
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001700 case Token::Kind::TK_NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001701 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001702 !left.compareConstant(fContext, right)));
1703 default:
1704 return nullptr;
1705 }
1706 }
ethannicholas08a92112016-11-09 13:26:45 -08001707 #undef RESULT
1708 return nullptr;
1709}
1710
Ethan Nicholasfc994162019-06-06 10:04:27 -04001711std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
1712 SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
1713 auto iter = expression.begin();
1714 std::unique_ptr<Expression> left = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001715 if (!left) {
1716 return nullptr;
1717 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001718 std::unique_ptr<Expression> right = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001719 if (!right) {
1720 return nullptr;
1721 }
ethannicholasd598f792016-07-25 10:08:54 -07001722 const Type* leftType;
1723 const Type* rightType;
1724 const Type* resultType;
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001725 const Type* rawLeftType;
1726 if (left->fKind == Expression::kIntLiteral_Kind && right->fType.isInteger()) {
1727 rawLeftType = &right->fType;
1728 } else {
1729 rawLeftType = &left->fType;
1730 }
1731 const Type* rawRightType;
1732 if (right->fKind == Expression::kIntLiteral_Kind && left->fType.isInteger()) {
1733 rawRightType = &left->fType;
1734 } else {
1735 rawRightType = &right->fType;
1736 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001737 Token::Kind op = expression.getToken().fKind;
1738 if (!determine_binary_type(fContext, op, *rawLeftType, *rawRightType, &leftType, &rightType,
1739 &resultType, !Compiler::IsAssignment(op))) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001740 fErrors.error(expression.fOffset, String("type mismatch: '") +
Ethan Nicholasfc994162019-06-06 10:04:27 -04001741 Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001742 "' cannot operate on '" + left->fType.displayName() +
1743 "', '" + right->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001744 return nullptr;
1745 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001746 if (Compiler::IsAssignment(op)) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001747 this->setRefKind(*left, op != Token::Kind::TK_EQ ? VariableReference::kReadWrite_RefKind :
1748 VariableReference::kWrite_RefKind);
ethannicholasea4567c2016-10-17 11:24:37 -07001749 }
1750 left = this->coerce(std::move(left), *leftType);
1751 right = this->coerce(std::move(right), *rightType);
1752 if (!left || !right) {
1753 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001754 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001755 std::unique_ptr<Expression> result = this->constantFold(*left.get(), op, *right.get());
ethannicholas08a92112016-11-09 13:26:45 -08001756 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001757 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001758 std::move(left),
Ethan Nicholasfc994162019-06-06 10:04:27 -04001759 op,
ethannicholas08a92112016-11-09 13:26:45 -08001760 std::move(right),
1761 *resultType));
1762 }
1763 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001764}
1765
Ethan Nicholasfc994162019-06-06 10:04:27 -04001766std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode& node) {
1767 SkASSERT(node.fKind == ASTNode::Kind::kTernary);
1768 auto iter = node.begin();
1769 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*(iter++)),
ethannicholasd598f792016-07-25 10:08:54 -07001770 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07001771 if (!test) {
1772 return nullptr;
1773 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001774 std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001775 if (!ifTrue) {
1776 return nullptr;
1777 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001778 std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07001779 if (!ifFalse) {
1780 return nullptr;
1781 }
ethannicholasd598f792016-07-25 10:08:54 -07001782 const Type* trueType;
1783 const Type* falseType;
1784 const Type* resultType;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001785 if (!determine_binary_type(fContext, Token::Kind::TK_EQEQ, ifTrue->fType, ifFalse->fType,
1786 &trueType, &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04001787 fErrors.error(node.fOffset, "ternary operator result mismatch: '" +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001788 ifTrue->fType.displayName() + "', '" +
1789 ifFalse->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001790 return nullptr;
1791 }
ethannicholasd598f792016-07-25 10:08:54 -07001792 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001793 if (!ifTrue) {
1794 return nullptr;
1795 }
ethannicholasd598f792016-07-25 10:08:54 -07001796 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001797 if (!ifFalse) {
1798 return nullptr;
1799 }
ethannicholas08a92112016-11-09 13:26:45 -08001800 if (test->fKind == Expression::kBoolLiteral_Kind) {
1801 // static boolean test, just return one of the branches
1802 if (((BoolLiteral&) *test).fValue) {
1803 return ifTrue;
1804 } else {
1805 return ifFalse;
1806 }
1807 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04001808 return std::unique_ptr<Expression>(new TernaryExpression(node.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001809 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001810 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -07001811 std::move(ifFalse)));
1812}
1813
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001814std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001815 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -07001816 std::vector<std::unique_ptr<Expression>> arguments) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001817 if (function.fBuiltin) {
Brian Osmanb08cc022020-04-02 11:38:40 -04001818 auto found = fIntrinsics->find(function.fName);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001819 if (found != fIntrinsics->end() && !found->second.second) {
1820 found->second.second = true;
1821 const FunctionDeclaration* old = fCurrentFunction;
1822 fCurrentFunction = nullptr;
1823 this->convertFunction(*((FunctionDefinition&) *found->second.first).fSource);
1824 fCurrentFunction = old;
1825 }
1826 }
ethannicholasd598f792016-07-25 10:08:54 -07001827 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001828 String msg = "call to '" + function.fName + "' expected " +
Ethan Nicholas11d53972016-11-28 11:23:23 -05001829 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001830 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001831 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001832 msg += "s";
1833 }
ethannicholas5961bc92016-10-12 06:39:56 -07001834 msg += ", but found " + to_string((uint64_t) arguments.size());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001835 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001836 return nullptr;
1837 }
Brian Osman5f6b41e2020-03-09 11:53:24 -04001838 if (fKind == Program::kPipelineStage_Kind && !function.fDefined && !function.fBuiltin) {
1839 String msg = "call to undefined function '" + function.fName + "'";
1840 fErrors.error(offset, msg);
1841 return nullptr;
1842 }
ethannicholas471e8942016-10-28 09:02:46 -07001843 std::vector<const Type*> types;
1844 const Type* returnType;
1845 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001846 String msg = "no match for " + function.fName + "(";
1847 String separator;
ethannicholas471e8942016-10-28 09:02:46 -07001848 for (size_t i = 0; i < arguments.size(); i++) {
1849 msg += separator;
1850 separator = ", ";
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001851 msg += arguments[i]->fType.displayName();
ethannicholas471e8942016-10-28 09:02:46 -07001852 }
1853 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001854 fErrors.error(offset, msg);
ethannicholas471e8942016-10-28 09:02:46 -07001855 return nullptr;
1856 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001857 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001858 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001859 if (!arguments[i]) {
1860 return nullptr;
1861 }
ethannicholasd598f792016-07-25 10:08:54 -07001862 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001863 this->setRefKind(*arguments[i],
1864 function.fParameters[i]->fModifiers.fFlags & Modifiers::kIn_Flag ?
1865 VariableReference::kReadWrite_RefKind :
1866 VariableReference::kPointer_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001867 }
1868 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001869 return std::unique_ptr<FunctionCall>(new FunctionCall(offset, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001870 std::move(arguments)));
1871}
1872
1873/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001874 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
1875 * particular meaning other than "lower costs are preferred". Returns INT_MAX if the call is not
1876 * valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001877 */
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001878int IRGenerator::callCost(const FunctionDeclaration& function,
1879 const std::vector<std::unique_ptr<Expression>>& arguments) {
ethannicholasd598f792016-07-25 10:08:54 -07001880 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001881 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001882 }
1883 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001884 std::vector<const Type*> types;
1885 const Type* ignored;
1886 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001887 return INT_MAX;
ethannicholas471e8942016-10-28 09:02:46 -07001888 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001889 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001890 int cost = arguments[i]->coercionCost(*types[i]);
1891 if (cost != INT_MAX) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001892 total += cost;
1893 } else {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001894 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001895 }
1896 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001897 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001898}
1899
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001900std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001901 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001902 std::vector<std::unique_ptr<Expression>> arguments) {
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001903 switch (functionValue->fKind) {
1904 case Expression::kTypeReference_Kind:
1905 return this->convertConstructor(offset,
1906 ((TypeReference&) *functionValue).fValue,
1907 std::move(arguments));
1908 case Expression::kExternalValue_Kind: {
1909 ExternalValue* v = ((ExternalValueReference&) *functionValue).fValue;
1910 if (!v->canCall()) {
1911 fErrors.error(offset, "this external value is not a function");
1912 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001913 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001914 int count = v->callParameterCount();
1915 if (count != (int) arguments.size()) {
1916 fErrors.error(offset, "external function expected " + to_string(count) +
1917 " arguments, but found " + to_string((int) arguments.size()));
1918 return nullptr;
1919 }
1920 static constexpr int PARAMETER_MAX = 16;
1921 SkASSERT(count < PARAMETER_MAX);
1922 const Type* types[PARAMETER_MAX];
1923 v->getCallParameterTypes(types);
1924 for (int i = 0; i < count; ++i) {
1925 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
1926 if (!arguments[i]) {
1927 return nullptr;
1928 }
1929 }
1930 return std::unique_ptr<Expression>(new ExternalFunctionCall(offset, v->callReturnType(),
1931 v, std::move(arguments)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001932 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001933 case Expression::kFunctionReference_Kind: {
1934 FunctionReference* ref = (FunctionReference*) functionValue.get();
1935 int bestCost = INT_MAX;
1936 const FunctionDeclaration* best = nullptr;
1937 if (ref->fFunctions.size() > 1) {
1938 for (const auto& f : ref->fFunctions) {
1939 int cost = this->callCost(*f, arguments);
1940 if (cost < bestCost) {
1941 bestCost = cost;
1942 best = f;
1943 }
1944 }
1945 if (best) {
1946 return this->call(offset, *best, std::move(arguments));
1947 }
1948 String msg = "no match for " + ref->fFunctions[0]->fName + "(";
1949 String separator;
1950 for (size_t i = 0; i < arguments.size(); i++) {
1951 msg += separator;
1952 separator = ", ";
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001953 msg += arguments[i]->fType.displayName();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001954 }
1955 msg += ")";
1956 fErrors.error(offset, msg);
1957 return nullptr;
1958 }
1959 return this->call(offset, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001960 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001961 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001962 fErrors.error(offset, "not a function");
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001963 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001964 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001965}
1966
Ethan Nicholas84645e32017-02-09 13:57:14 -05001967std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001968 int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001969 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001970 std::vector<std::unique_ptr<Expression>> args) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001971 SkASSERT(type.isNumber());
Ethan Nicholas84645e32017-02-09 13:57:14 -05001972 if (args.size() != 1) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001973 fErrors.error(offset, "invalid arguments to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001974 "' constructor, (expected exactly 1 argument, but found " +
1975 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -07001976 return nullptr;
1977 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001978 if (type == args[0]->fType) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001979 return std::move(args[0]);
1980 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001981 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kFloatLiteral_Kind) {
1982 double value = ((FloatLiteral&) *args[0]).fValue;
Ethan Nicholas00543112018-07-31 09:44:36 -04001983 return std::unique_ptr<Expression>(new FloatLiteral(offset, value, &type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001984 }
1985 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kIntLiteral_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001986 int64_t value = ((IntLiteral&) *args[0]).fValue;
Ethan Nicholas00543112018-07-31 09:44:36 -04001987 return std::unique_ptr<Expression>(new FloatLiteral(offset, (double) value, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001988 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001989 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
1990 type == *fContext.fUInt_Type)) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001991 return std::unique_ptr<Expression>(new IntLiteral(offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001992 ((IntLiteral&) *args[0]).fValue,
1993 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001994 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001995 if (args[0]->fType == *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001996 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0));
1997 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001998 return std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001999 new TernaryExpression(offset, std::move(args[0]),
Ethan Nicholas84645e32017-02-09 13:57:14 -05002000 this->coerce(std::move(one), type),
2001 this->coerce(std::move(zero),
2002 type)));
2003 }
2004 if (!args[0]->fType.isNumber()) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002005 fErrors.error(offset, "invalid argument to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002006 "' constructor (expected a number or bool, but found '" +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002007 args[0]->fType.displayName() + "')");
Ethan Nicholas84645e32017-02-09 13:57:14 -05002008 return nullptr;
2009 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002010 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002011}
2012
2013int component_count(const Type& type) {
2014 switch (type.kind()) {
2015 case Type::kVector_Kind:
2016 return type.columns();
2017 case Type::kMatrix_Kind:
2018 return type.columns() * type.rows();
2019 default:
2020 return 1;
2021 }
2022}
2023
2024std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002025 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05002026 const Type& type,
2027 std::vector<std::unique_ptr<Expression>> args) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002028 SkASSERT(type.kind() == Type::kVector_Kind || type.kind() == Type::kMatrix_Kind);
Ethan Nicholas84645e32017-02-09 13:57:14 -05002029 if (type.kind() == Type::kMatrix_Kind && args.size() == 1 &&
2030 args[0]->fType.kind() == Type::kMatrix_Kind) {
2031 // matrix from matrix is always legal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002032 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002033 }
2034 int actual = 0;
2035 int expected = type.rows() * type.columns();
2036 if (args.size() != 1 || expected != component_count(args[0]->fType) ||
2037 type.componentType().isNumber() != args[0]->fType.componentType().isNumber()) {
ethannicholas5961bc92016-10-12 06:39:56 -07002038 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05002039 if (args[i]->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002040 if (type.componentType().isNumber() !=
2041 args[i]->fType.componentType().isNumber()) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002042 fErrors.error(offset, "'" + args[i]->fType.displayName() + "' is not a valid "
2043 "parameter to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002044 "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002045 return nullptr;
2046 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05002047 actual += args[i]->fType.columns();
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002048 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
2049 actual += 1;
2050 if (type.kind() != Type::kScalar_Kind) {
2051 args[i] = this->coerce(std::move(args[i]), type.componentType());
2052 if (!args[i]) {
2053 return nullptr;
2054 }
2055 }
2056 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002057 fErrors.error(offset, "'" + args[i]->fType.displayName() + "' is not a valid "
2058 "parameter to '" + type.displayName() + "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002059 return nullptr;
2060 }
2061 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05002062 if (actual != 1 && actual != expected) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002063 fErrors.error(offset, "invalid arguments to '" + type.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002064 "' constructor (expected " + to_string(expected) +
2065 " scalars, but found " + to_string(actual) + ")");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00002066 return nullptr;
2067 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002068 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002069 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002070}
2071
Ethan Nicholas84645e32017-02-09 13:57:14 -05002072std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002073 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05002074 const Type& type,
2075 std::vector<std::unique_ptr<Expression>> args) {
2076 // FIXME: add support for structs
2077 Type::Kind kind = type.kind();
2078 if (args.size() == 1 && args[0]->fType == type) {
2079 // argument is already the right type, just return it
2080 return std::move(args[0]);
2081 }
2082 if (type.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002083 return this->convertNumberConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002084 } else if (kind == Type::kArray_Kind) {
2085 const Type& base = type.componentType();
2086 for (size_t i = 0; i < args.size(); i++) {
2087 args[i] = this->coerce(std::move(args[i]), base);
2088 if (!args[i]) {
2089 return nullptr;
2090 }
2091 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002092 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002093 } else if (kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002094 return this->convertCompoundConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05002095 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002096 fErrors.error(offset, "cannot construct '" + type.displayName() + "'");
Ethan Nicholas84645e32017-02-09 13:57:14 -05002097 return nullptr;
2098 }
2099}
2100
Ethan Nicholasfc994162019-06-06 10:04:27 -04002101std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode& expression) {
2102 SkASSERT(expression.fKind == ASTNode::Kind::kPrefix);
2103 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
ethannicholasb3058bd2016-07-01 08:22:01 -07002104 if (!base) {
2105 return nullptr;
2106 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002107 switch (expression.getToken().fKind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002108 case Token::Kind::TK_PLUS:
Ethan Nicholase1f55022019-02-05 17:17:40 -05002109 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind &&
2110 base->fType != *fContext.fFloatLiteral_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002111 fErrors.error(expression.fOffset,
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002112 "'+' cannot operate on '" + base->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002113 return nullptr;
2114 }
2115 return base;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002116 case Token::Kind::TK_MINUS:
ethannicholasb3058bd2016-07-01 08:22:01 -07002117 if (base->fKind == Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002118 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002119 -((IntLiteral&) *base).fValue));
2120 }
2121 if (base->fKind == Expression::kFloatLiteral_Kind) {
2122 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002123 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002124 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07002125 }
Ethan Nicholase1f55022019-02-05 17:17:40 -05002126 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
2127 fErrors.error(expression.fOffset,
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002128 "'-' cannot operate on '" + base->fType.displayName() + "'");
Ethan Nicholase1f55022019-02-05 17:17:40 -05002129 return nullptr;
2130 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002131 return std::unique_ptr<Expression>(new PrefixExpression(Token::Kind::TK_MINUS,
2132 std::move(base)));
2133 case Token::Kind::TK_PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07002134 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002135 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002136 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002137 "' cannot operate on '" + base->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002138 return nullptr;
2139 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002140 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002141 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002142 case Token::Kind::TK_MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07002143 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002144 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002145 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002146 "' cannot operate on '" + base->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002147 return nullptr;
2148 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002149 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002150 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002151 case Token::Kind::TK_LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07002152 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002153 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002154 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002155 "' cannot operate on '" + base->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002156 return nullptr;
2157 }
ethannicholas08a92112016-11-09 13:26:45 -08002158 if (base->fKind == Expression::kBoolLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002159 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08002160 !((BoolLiteral&) *base).fValue));
2161 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002162 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002163 case Token::Kind::TK_BITWISENOT:
Brian Osman73fb39c2019-09-24 16:22:55 -04002164 if (base->fType != *fContext.fInt_Type && base->fType != *fContext.fUInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002165 fErrors.error(expression.fOffset,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002166 String("'") + Compiler::OperatorName(expression.getToken().fKind) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002167 "' cannot operate on '" + base->fType.displayName() + "'");
ethannicholas5961bc92016-10-12 06:39:56 -07002168 return nullptr;
2169 }
2170 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05002171 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07002172 ABORT("unsupported prefix operator\n");
2173 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002174 return std::unique_ptr<Expression>(new PrefixExpression(expression.getToken().fKind,
ethannicholasb3058bd2016-07-01 08:22:01 -07002175 std::move(base)));
2176}
2177
2178std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
Ethan Nicholasfc994162019-06-06 10:04:27 -04002179 const ASTNode& index) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05002180 if (base->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002181 if (index.fKind == ASTNode::Kind::kInt) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05002182 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholasfc994162019-06-06 10:04:27 -04002183 SKSL_INT size = index.getInt();
Ethan Nicholas91164d12019-05-15 15:29:54 -04002184 Type* newType = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>(
2185 new Type(oldType.name() + "[" + to_string(size) + "]",
Ethan Nicholasfc994162019-06-06 10:04:27 -04002186 Type::kArray_Kind, oldType, size)));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002187 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
Ethan Nicholas50afc172017-02-16 14:49:57 -05002188 *newType));
2189
2190 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002191 fErrors.error(base->fOffset, "array size must be a constant");
Ethan Nicholas50afc172017-02-16 14:49:57 -05002192 return nullptr;
2193 }
2194 }
ethannicholas5961bc92016-10-12 06:39:56 -07002195 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
2196 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002197 fErrors.error(base->fOffset, "expected array, but found '" + base->fType.displayName() +
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002198 "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002199 return nullptr;
2200 }
2201 std::unique_ptr<Expression> converted = this->convertExpression(index);
2202 if (!converted) {
2203 return nullptr;
2204 }
ethannicholas5961bc92016-10-12 06:39:56 -07002205 if (converted->fType != *fContext.fUInt_Type) {
2206 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
2207 if (!converted) {
2208 return nullptr;
2209 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002210 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05002211 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07002212 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002213}
2214
2215std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002216 StringFragment field) {
Ethan Nicholas91164d12019-05-15 15:29:54 -04002217 if (base->fKind == Expression::kExternalValue_Kind) {
2218 ExternalValue& ev = *((ExternalValueReference&) *base).fValue;
2219 ExternalValue* result = ev.getChild(String(field).c_str());
2220 if (!result) {
2221 fErrors.error(base->fOffset, "external value does not have a child named '" + field +
2222 "'");
2223 return nullptr;
2224 }
2225 return std::unique_ptr<Expression>(new ExternalValueReference(base->fOffset, result));
2226 }
ethannicholasd598f792016-07-25 10:08:54 -07002227 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07002228 for (size_t i = 0; i < fields.size(); i++) {
2229 if (fields[i].fName == field) {
2230 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
2231 }
2232 }
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002233 fErrors.error(base->fOffset, "type '" + base->fType.displayName() + "' does not have a "
Brian Osmanb08cc022020-04-02 11:38:40 -04002234 "field named '" + field + "");
ethannicholasb3058bd2016-07-01 08:22:01 -07002235 return nullptr;
2236}
2237
2238std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002239 StringFragment fields) {
ethannicholasd598f792016-07-25 10:08:54 -07002240 if (base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002241 fErrors.error(base->fOffset, "cannot swizzle type '" + base->fType.displayName() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002242 return nullptr;
2243 }
2244 std::vector<int> swizzleComponents;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002245 for (size_t i = 0; i < fields.fLength; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05002246 switch (fields[i]) {
Ethan Nicholasac285b12019-02-12 16:05:18 -05002247 case '0':
Ethan Nicholasac285b12019-02-12 16:05:18 -05002248 swizzleComponents.push_back(SKSL_SWIZZLE_0);
2249 break;
2250 case '1':
Ethan Nicholasac285b12019-02-12 16:05:18 -05002251 swizzleComponents.push_back(SKSL_SWIZZLE_1);
2252 break;
Ethan Nicholase455f652019-09-13 12:52:55 -04002253 case 'x':
2254 case 'r':
Ethan Nicholas11d53972016-11-28 11:23:23 -05002255 case 's':
Ethan Nicholase455f652019-09-13 12:52:55 -04002256 case 'L':
ethannicholasb3058bd2016-07-01 08:22:01 -07002257 swizzleComponents.push_back(0);
2258 break;
Ethan Nicholase455f652019-09-13 12:52:55 -04002259 case 'y':
2260 case 'g':
ethannicholasb3058bd2016-07-01 08:22:01 -07002261 case 't':
Ethan Nicholase455f652019-09-13 12:52:55 -04002262 case 'T':
ethannicholasd598f792016-07-25 10:08:54 -07002263 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002264 swizzleComponents.push_back(1);
2265 break;
2266 }
2267 // fall through
Ethan Nicholase455f652019-09-13 12:52:55 -04002268 case 'z':
2269 case 'b':
Ethan Nicholas11d53972016-11-28 11:23:23 -05002270 case 'p':
Ethan Nicholase455f652019-09-13 12:52:55 -04002271 case 'R':
ethannicholasd598f792016-07-25 10:08:54 -07002272 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002273 swizzleComponents.push_back(2);
2274 break;
2275 }
2276 // fall through
Ethan Nicholase455f652019-09-13 12:52:55 -04002277 case 'w':
2278 case 'a':
ethannicholasb3058bd2016-07-01 08:22:01 -07002279 case 'q':
Ethan Nicholase455f652019-09-13 12:52:55 -04002280 case 'B':
ethannicholasd598f792016-07-25 10:08:54 -07002281 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002282 swizzleComponents.push_back(3);
2283 break;
2284 }
2285 // fall through
2286 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002287 fErrors.error(base->fOffset, String::printf("invalid swizzle component '%c'",
2288 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07002289 return nullptr;
2290 }
2291 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002292 SkASSERT(swizzleComponents.size() > 0);
ethannicholasb3058bd2016-07-01 08:22:01 -07002293 if (swizzleComponents.size() > 4) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002294 fErrors.error(base->fOffset, "too many components in swizzle mask '" + fields + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002295 return nullptr;
2296 }
ethannicholasd598f792016-07-25 10:08:54 -07002297 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07002298}
2299
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002300std::unique_ptr<Expression> IRGenerator::getCap(int offset, String name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002301 auto found = fCapsMap.find(name);
2302 if (found == fCapsMap.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002303 fErrors.error(offset, "unknown capability flag '" + name + "'");
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002304 return nullptr;
2305 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002306 String fullName = "sk_Caps." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002307 return std::unique_ptr<Expression>(new Setting(offset, fullName,
2308 found->second.literal(fContext, offset)));
Ethan Nicholas762466e2017-06-29 10:03:38 -04002309}
2310
Ethan Nicholas00543112018-07-31 09:44:36 -04002311std::unique_ptr<Expression> IRGenerator::getArg(int offset, String name) const {
Ethan Nicholas762466e2017-06-29 10:03:38 -04002312 auto found = fSettings->fArgs.find(name);
2313 if (found == fSettings->fArgs.end()) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04002314 return nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002315 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002316 String fullName = "sk_Args." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002317 return std::unique_ptr<Expression>(new Setting(offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04002318 fullName,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002319 found->second.literal(fContext, offset)));
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002320}
2321
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002322std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type,
2323 StringFragment field) {
2324 std::unique_ptr<Expression> result;
2325 for (const auto& e : *fProgramElements) {
2326 if (e->fKind == ProgramElement::kEnum_Kind && type.name() == ((Enum&) *e).fTypeName) {
2327 std::shared_ptr<SymbolTable> old = fSymbolTable;
2328 fSymbolTable = ((Enum&) *e).fSymbols;
Ethan Nicholasfc994162019-06-06 10:04:27 -04002329 result = convertIdentifier(ASTNode(&fFile->fNodes, offset, ASTNode::Kind::kIdentifier,
2330 field));
Ethan Nicholas9fdab9f2020-05-01 11:02:15 -04002331 if (!result) {
2332 fSymbolTable = old;
2333 return nullptr;
2334 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -05002335 SkASSERT(result->fKind == Expression::kVariableReference_Kind);
2336 const Variable& v = ((VariableReference&) *result).fVariable;
2337 SkASSERT(v.fInitialValue);
2338 SkASSERT(v.fInitialValue->fKind == Expression::kIntLiteral_Kind);
2339 result.reset(new IntLiteral(offset, ((IntLiteral&) *v.fInitialValue).fValue, &type));
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002340 fSymbolTable = old;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05002341 break;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002342 }
2343 }
2344 if (!result) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05002345 auto found = fIntrinsics->find(type.fName);
2346 if (found != fIntrinsics->end()) {
2347 SkASSERT(!found->second.second);
2348 found->second.second = true;
2349 fProgramElements->push_back(found->second.first->clone());
2350 return this->convertTypeField(offset, type, field);
2351 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002352 fErrors.error(offset, "type '" + type.fName + "' does not have a field named '" + field +
2353 "'");
2354 }
2355 return result;
2356}
2357
Ethan Nicholasfc994162019-06-06 10:04:27 -04002358std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& index) {
2359 SkASSERT(index.fKind == ASTNode::Kind::kIndex);
2360 auto iter = index.begin();
2361 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
ethannicholasb3058bd2016-07-01 08:22:01 -07002362 if (!base) {
2363 return nullptr;
2364 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002365 if (iter != index.end()) {
2366 return this->convertIndex(std::move(base), *(iter++));
2367 } else if (base->fKind == Expression::kTypeReference_Kind) {
2368 const Type& oldType = ((TypeReference&) *base).fValue;
2369 Type* newType = (Type*) fSymbolTable->takeOwnership(std::unique_ptr<Symbol>(
Ethan Nicholas91164d12019-05-15 15:29:54 -04002370 new Type(oldType.name() + "[]",
2371 Type::kArray_Kind,
2372 oldType,
2373 -1)));
Ethan Nicholasfc994162019-06-06 10:04:27 -04002374 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
2375 *newType));
ethannicholasb3058bd2016-07-01 08:22:01 -07002376 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002377 fErrors.error(index.fOffset, "'[]' must follow a type name");
2378 return nullptr;
2379}
2380
2381std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
2382 SkASSERT(callNode.fKind == ASTNode::Kind::kCall);
2383 auto iter = callNode.begin();
2384 std::unique_ptr<Expression> base = this->convertExpression(*(iter++));
2385 if (!base) {
2386 return nullptr;
2387 }
2388 std::vector<std::unique_ptr<Expression>> arguments;
2389 for (; iter != callNode.end(); ++iter) {
2390 std::unique_ptr<Expression> converted = this->convertExpression(*iter);
2391 if (!converted) {
2392 return nullptr;
2393 }
2394 arguments.push_back(std::move(converted));
2395 }
2396 return this->call(callNode.fOffset, std::move(base), std::move(arguments));
2397}
2398
2399std::unique_ptr<Expression> IRGenerator::convertFieldExpression(const ASTNode& fieldNode) {
2400 std::unique_ptr<Expression> base = this->convertExpression(*fieldNode.begin());
2401 if (!base) {
2402 return nullptr;
2403 }
2404 StringFragment field = fieldNode.getString();
2405 if (base->fType == *fContext.fSkCaps_Type) {
2406 return this->getCap(fieldNode.fOffset, field);
2407 }
2408 if (base->fType == *fContext.fSkArgs_Type) {
2409 return this->getArg(fieldNode.fOffset, field);
2410 }
2411 if (base->fKind == Expression::kTypeReference_Kind) {
2412 return this->convertTypeField(base->fOffset, ((TypeReference&) *base).fValue,
2413 field);
2414 }
2415 if (base->fKind == Expression::kExternalValue_Kind) {
2416 return this->convertField(std::move(base), field);
2417 }
2418 switch (base->fType.kind()) {
2419 case Type::kVector_Kind:
2420 return this->convertSwizzle(std::move(base), field);
2421 case Type::kOther_Kind:
2422 case Type::kStruct_Kind:
2423 return this->convertField(std::move(base), field);
2424 default:
2425 fErrors.error(base->fOffset, "cannot swizzle value of type '" +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002426 base->fType.displayName() + "'");
Ethan Nicholasfc994162019-06-06 10:04:27 -04002427 return nullptr;
2428 }
2429}
2430
2431std::unique_ptr<Expression> IRGenerator::convertPostfixExpression(const ASTNode& expression) {
2432 std::unique_ptr<Expression> base = this->convertExpression(*expression.begin());
2433 if (!base) {
2434 return nullptr;
2435 }
2436 if (!base->fType.isNumber()) {
2437 fErrors.error(expression.fOffset,
2438 "'" + String(Compiler::OperatorName(expression.getToken().fKind)) +
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002439 "' cannot operate on '" + base->fType.displayName() + "'");
Ethan Nicholasfc994162019-06-06 10:04:27 -04002440 return nullptr;
2441 }
2442 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
2443 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
2444 expression.getToken().fKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07002445}
2446
2447void IRGenerator::checkValid(const Expression& expr) {
2448 switch (expr.fKind) {
2449 case Expression::kFunctionReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002450 fErrors.error(expr.fOffset, "expected '(' to begin function call");
ethannicholasb3058bd2016-07-01 08:22:01 -07002451 break;
2452 case Expression::kTypeReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002453 fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation");
ethannicholasb3058bd2016-07-01 08:22:01 -07002454 break;
2455 default:
ethannicholasea4567c2016-10-17 11:24:37 -07002456 if (expr.fType == *fContext.fInvalid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002457 fErrors.error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07002458 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002459 }
2460}
2461
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002462bool IRGenerator::checkSwizzleWrite(const Swizzle& swizzle) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002463 int bits = 0;
2464 for (int idx : swizzle.fComponents) {
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002465 if (idx < 0) {
2466 fErrors.error(swizzle.fOffset, "cannot write to a swizzle mask containing a constant");
2467 return false;
2468 }
2469 SkASSERT(idx <= 3);
ethannicholasb3058bd2016-07-01 08:22:01 -07002470 int bit = 1 << idx;
2471 if (bits & bit) {
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002472 fErrors.error(swizzle.fOffset,
2473 "cannot write to the same swizzle field more than once");
2474 return false;
ethannicholasb3058bd2016-07-01 08:22:01 -07002475 }
2476 bits |= bit;
2477 }
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002478 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07002479}
2480
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002481void IRGenerator::setRefKind(const Expression& expr, VariableReference::RefKind kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002482 switch (expr.fKind) {
2483 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07002484 const Variable& var = ((VariableReference&) expr).fVariable;
Brian Osman3c358422020-03-23 10:44:12 -04002485 if (var.fModifiers.fFlags &
2486 (Modifiers::kConst_Flag | Modifiers::kUniform_Flag | Modifiers::kVarying_Flag)) {
2487 fErrors.error(expr.fOffset, "cannot modify immutable variable '" + var.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002488 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002489 ((VariableReference&) expr).setRefKind(kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002490 break;
2491 }
2492 case Expression::kFieldAccess_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002493 this->setRefKind(*((FieldAccess&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002494 break;
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002495 case Expression::kSwizzle_Kind: {
2496 const Swizzle& swizzle = (Swizzle&) expr;
2497 this->checkSwizzleWrite(swizzle);
2498 this->setRefKind(*swizzle.fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002499 break;
Ethan Nicholascb0f4092019-04-19 11:26:50 -04002500 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002501 case Expression::kIndex_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002502 this->setRefKind(*((IndexExpression&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002503 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -05002504 case Expression::kTernary_Kind: {
2505 TernaryExpression& t = (TernaryExpression&) expr;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002506 this->setRefKind(*t.fIfTrue, kind);
2507 this->setRefKind(*t.fIfFalse, kind);
Ethan Nicholasa583b812018-01-18 13:32:11 -05002508 break;
2509 }
Ethan Nicholas91164d12019-05-15 15:29:54 -04002510 case Expression::kExternalValue_Kind: {
2511 const ExternalValue& v = *((ExternalValueReference&) expr).fValue;
2512 if (!v.canWrite()) {
2513 fErrors.error(expr.fOffset,
2514 "cannot modify immutable external value '" + v.fName + "'");
2515 }
2516 break;
2517 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002518 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002519 fErrors.error(expr.fOffset, "cannot assign to this expression");
ethannicholasb3058bd2016-07-01 08:22:01 -07002520 break;
2521 }
2522}
2523
Robert Phillipsfe8da172018-01-24 14:52:02 +00002524void IRGenerator::convertProgram(Program::Kind kind,
2525 const char* text,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002526 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002527 SymbolTable& types,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002528 std::vector<std::unique_ptr<ProgramElement>>* out) {
Robert Phillipsfe8da172018-01-24 14:52:02 +00002529 fKind = kind;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002530 fProgramElements = out;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002531 Parser parser(text, length, types, fErrors);
Ethan Nicholasfc994162019-06-06 10:04:27 -04002532 fFile = parser.file();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002533 if (fErrors.errorCount()) {
2534 return;
2535 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002536 SkASSERT(fFile);
2537 for (const auto& decl : fFile->root()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002538 switch (decl.fKind) {
Ethan Nicholasfc994162019-06-06 10:04:27 -04002539 case ASTNode::Kind::kVarDeclarations: {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002540 std::unique_ptr<VarDeclarations> s = this->convertVarDeclarations(
Ethan Nicholasfc994162019-06-06 10:04:27 -04002541 decl,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002542 Variable::kGlobal_Storage);
2543 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002544 fProgramElements->push_back(std::move(s));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002545 }
2546 break;
2547 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002548 case ASTNode::Kind::kEnum: {
2549 this->convertEnum(decl);
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002550 break;
2551 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002552 case ASTNode::Kind::kFunction: {
2553 this->convertFunction(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002554 break;
2555 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002556 case ASTNode::Kind::kModifiers: {
2557 std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002558 if (f) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002559 fProgramElements->push_back(std::move(f));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002560 }
2561 break;
2562 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002563 case ASTNode::Kind::kInterfaceBlock: {
2564 std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002565 if (i) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002566 fProgramElements->push_back(std::move(i));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002567 }
2568 break;
2569 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002570 case ASTNode::Kind::kExtension: {
2571 std::unique_ptr<Extension> e = this->convertExtension(decl.fOffset,
2572 decl.getString());
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002573 if (e) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002574 fProgramElements->push_back(std::move(e));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002575 }
2576 break;
2577 }
Ethan Nicholasfc994162019-06-06 10:04:27 -04002578 case ASTNode::Kind::kSection: {
2579 std::unique_ptr<Section> s = this->convertSection(decl);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002580 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002581 fProgramElements->push_back(std::move(s));
Ethan Nicholas762466e2017-06-29 10:03:38 -04002582 }
2583 break;
2584 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002585 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002586#ifdef SK_DEBUG
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002587 ABORT("unsupported declaration: %s\n", decl.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002588#endif
2589 break;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002590 }
2591 }
2592}
2593
2594
ethannicholasb3058bd2016-07-01 08:22:01 -07002595}